memory: replace cpu_physical_memory_reset_dirty() with test-and-clear
[qemu.git] / blockdev.c
blobde94a8bcb383212d665a67a75891b9517eb3ccf4
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 "monitor/monitor.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40 #include "qapi/qmp/types.h"
41 #include "qapi-visit.h"
42 #include "qapi/qmp-output-visitor.h"
43 #include "qapi/util.h"
44 #include "sysemu/sysemu.h"
45 #include "block/block_int.h"
46 #include "qmp-commands.h"
47 #include "trace.h"
48 #include "sysemu/arch_init.h"
50 static const char *const if_name[IF_COUNT] = {
51 [IF_NONE] = "none",
52 [IF_IDE] = "ide",
53 [IF_SCSI] = "scsi",
54 [IF_FLOPPY] = "floppy",
55 [IF_PFLASH] = "pflash",
56 [IF_MTD] = "mtd",
57 [IF_SD] = "sd",
58 [IF_VIRTIO] = "virtio",
59 [IF_XEN] = "xen",
62 static int if_max_devs[IF_COUNT] = {
64 * Do not change these numbers! They govern how drive option
65 * index maps to unit and bus. That mapping is ABI.
67 * All controllers used to imlement if=T drives need to support
68 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
69 * Otherwise, some index values map to "impossible" bus, unit
70 * values.
72 * For instance, if you change [IF_SCSI] to 255, -drive
73 * if=scsi,index=12 no longer means bus=1,unit=5, but
74 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
75 * the drive can't be set up. Regression.
77 [IF_IDE] = 2,
78 [IF_SCSI] = 7,
81 /**
82 * Boards may call this to offer board-by-board overrides
83 * of the default, global values.
85 void override_max_devs(BlockInterfaceType type, int max_devs)
87 BlockBackend *blk;
88 DriveInfo *dinfo;
90 if (max_devs <= 0) {
91 return;
94 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
95 dinfo = blk_legacy_dinfo(blk);
96 if (dinfo->type == type) {
97 fprintf(stderr, "Cannot override units-per-bus property of"
98 " the %s interface, because a drive of that type has"
99 " already been added.\n", if_name[type]);
100 g_assert_not_reached();
104 if_max_devs[type] = max_devs;
108 * We automatically delete the drive when a device using it gets
109 * unplugged. Questionable feature, but we can't just drop it.
110 * Device models call blockdev_mark_auto_del() to schedule the
111 * automatic deletion, and generic qdev code calls blockdev_auto_del()
112 * when deletion is actually safe.
114 void blockdev_mark_auto_del(BlockBackend *blk)
116 DriveInfo *dinfo = blk_legacy_dinfo(blk);
117 BlockDriverState *bs = blk_bs(blk);
118 AioContext *aio_context;
120 if (!dinfo) {
121 return;
124 aio_context = bdrv_get_aio_context(bs);
125 aio_context_acquire(aio_context);
127 if (bs->job) {
128 block_job_cancel(bs->job);
131 aio_context_release(aio_context);
133 dinfo->auto_del = 1;
136 void blockdev_auto_del(BlockBackend *blk)
138 DriveInfo *dinfo = blk_legacy_dinfo(blk);
140 if (dinfo && dinfo->auto_del) {
141 blk_unref(blk);
146 * Returns the current mapping of how many units per bus
147 * a particular interface can support.
149 * A positive integer indicates n units per bus.
150 * 0 implies the mapping has not been established.
151 * -1 indicates an invalid BlockInterfaceType was given.
153 int drive_get_max_devs(BlockInterfaceType type)
155 if (type >= IF_IDE && type < IF_COUNT) {
156 return if_max_devs[type];
159 return -1;
162 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
164 int max_devs = if_max_devs[type];
165 return max_devs ? index / max_devs : 0;
168 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
170 int max_devs = if_max_devs[type];
171 return max_devs ? index % max_devs : index;
174 QemuOpts *drive_def(const char *optstr)
176 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
179 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
180 const char *optstr)
182 QemuOpts *opts;
184 opts = drive_def(optstr);
185 if (!opts) {
186 return NULL;
188 if (type != IF_DEFAULT) {
189 qemu_opt_set(opts, "if", if_name[type], &error_abort);
191 if (index >= 0) {
192 qemu_opt_set_number(opts, "index", index, &error_abort);
194 if (file)
195 qemu_opt_set(opts, "file", file, &error_abort);
196 return opts;
199 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
201 BlockBackend *blk;
202 DriveInfo *dinfo;
204 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
205 dinfo = blk_legacy_dinfo(blk);
206 if (dinfo && dinfo->type == type
207 && dinfo->bus == bus && dinfo->unit == unit) {
208 return dinfo;
212 return NULL;
215 bool drive_check_orphaned(void)
217 BlockBackend *blk;
218 DriveInfo *dinfo;
219 bool rs = false;
221 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
222 dinfo = blk_legacy_dinfo(blk);
223 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
224 /* Unless this is a default drive, this may be an oversight. */
225 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
226 dinfo->type != IF_NONE) {
227 fprintf(stderr, "Warning: Orphaned drive without device: "
228 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
229 blk_name(blk), blk_bs(blk)->filename, if_name[dinfo->type],
230 dinfo->bus, dinfo->unit);
231 rs = true;
235 return rs;
238 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
240 return drive_get(type,
241 drive_index_to_bus_id(type, index),
242 drive_index_to_unit_id(type, index));
245 int drive_get_max_bus(BlockInterfaceType type)
247 int max_bus;
248 BlockBackend *blk;
249 DriveInfo *dinfo;
251 max_bus = -1;
252 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
253 dinfo = blk_legacy_dinfo(blk);
254 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
255 max_bus = dinfo->bus;
258 return max_bus;
261 /* Get a block device. This should only be used for single-drive devices
262 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
263 appropriate bus. */
264 DriveInfo *drive_get_next(BlockInterfaceType type)
266 static int next_block_unit[IF_COUNT];
268 return drive_get(type, 0, next_block_unit[type]++);
271 static void bdrv_format_print(void *opaque, const char *name)
273 error_printf(" %s", name);
276 typedef struct {
277 QEMUBH *bh;
278 BlockDriverState *bs;
279 } BDRVPutRefBH;
281 static void bdrv_put_ref_bh(void *opaque)
283 BDRVPutRefBH *s = opaque;
285 bdrv_unref(s->bs);
286 qemu_bh_delete(s->bh);
287 g_free(s);
291 * Release a BDS reference in a BH
293 * It is not safe to use bdrv_unref() from a callback function when the callers
294 * still need the BlockDriverState. In such cases we schedule a BH to release
295 * the reference.
297 static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
299 BDRVPutRefBH *s;
301 s = g_new(BDRVPutRefBH, 1);
302 s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
303 s->bs = bs;
304 qemu_bh_schedule(s->bh);
307 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
309 if (!strcmp(buf, "ignore")) {
310 return BLOCKDEV_ON_ERROR_IGNORE;
311 } else if (!is_read && !strcmp(buf, "enospc")) {
312 return BLOCKDEV_ON_ERROR_ENOSPC;
313 } else if (!strcmp(buf, "stop")) {
314 return BLOCKDEV_ON_ERROR_STOP;
315 } else if (!strcmp(buf, "report")) {
316 return BLOCKDEV_ON_ERROR_REPORT;
317 } else {
318 error_setg(errp, "'%s' invalid %s error action",
319 buf, is_read ? "read" : "write");
320 return -1;
324 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
326 if (throttle_conflicting(cfg)) {
327 error_setg(errp, "bps/iops/max total values and read/write values"
328 " cannot be used at the same time");
329 return false;
332 if (!throttle_is_valid(cfg)) {
333 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
334 return false;
337 return true;
340 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
342 /* Takes the ownership of bs_opts */
343 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
344 Error **errp)
346 const char *buf;
347 int ro = 0;
348 int bdrv_flags = 0;
349 int on_read_error, on_write_error;
350 BlockBackend *blk;
351 BlockDriverState *bs;
352 ThrottleConfig cfg;
353 int snapshot = 0;
354 bool copy_on_read;
355 Error *error = NULL;
356 QemuOpts *opts;
357 const char *id;
358 bool has_driver_specific_opts;
359 BlockdevDetectZeroesOptions detect_zeroes;
361 /* Check common options by copying from bs_opts to opts, all other options
362 * stay in bs_opts for processing by bdrv_open(). */
363 id = qdict_get_try_str(bs_opts, "id");
364 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
365 if (error) {
366 error_propagate(errp, error);
367 goto err_no_opts;
370 qemu_opts_absorb_qdict(opts, bs_opts, &error);
371 if (error) {
372 error_propagate(errp, error);
373 goto early_err;
376 if (id) {
377 qdict_del(bs_opts, "id");
380 has_driver_specific_opts = !!qdict_size(bs_opts);
382 /* extract parameters */
383 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
384 ro = qemu_opt_get_bool(opts, "read-only", 0);
385 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
387 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
388 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
389 error_setg(errp, "invalid discard option");
390 goto early_err;
394 if (qemu_opt_get_bool(opts, "cache.writeback", true)) {
395 bdrv_flags |= BDRV_O_CACHE_WB;
397 if (qemu_opt_get_bool(opts, "cache.direct", false)) {
398 bdrv_flags |= BDRV_O_NOCACHE;
400 if (qemu_opt_get_bool(opts, "cache.no-flush", false)) {
401 bdrv_flags |= BDRV_O_NO_FLUSH;
404 #ifdef CONFIG_LINUX_AIO
405 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
406 if (!strcmp(buf, "native")) {
407 bdrv_flags |= BDRV_O_NATIVE_AIO;
408 } else if (!strcmp(buf, "threads")) {
409 /* this is the default */
410 } else {
411 error_setg(errp, "invalid aio option");
412 goto early_err;
415 #endif
417 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
418 if (is_help_option(buf)) {
419 error_printf("Supported formats:");
420 bdrv_iterate_format(bdrv_format_print, NULL);
421 error_printf("\n");
422 goto early_err;
425 if (qdict_haskey(bs_opts, "driver")) {
426 error_setg(errp, "Cannot specify both 'driver' and 'format'");
427 goto early_err;
429 qdict_put(bs_opts, "driver", qstring_from_str(buf));
432 /* disk I/O throttling */
433 memset(&cfg, 0, sizeof(cfg));
434 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
435 qemu_opt_get_number(opts, "throttling.bps-total", 0);
436 cfg.buckets[THROTTLE_BPS_READ].avg =
437 qemu_opt_get_number(opts, "throttling.bps-read", 0);
438 cfg.buckets[THROTTLE_BPS_WRITE].avg =
439 qemu_opt_get_number(opts, "throttling.bps-write", 0);
440 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
441 qemu_opt_get_number(opts, "throttling.iops-total", 0);
442 cfg.buckets[THROTTLE_OPS_READ].avg =
443 qemu_opt_get_number(opts, "throttling.iops-read", 0);
444 cfg.buckets[THROTTLE_OPS_WRITE].avg =
445 qemu_opt_get_number(opts, "throttling.iops-write", 0);
447 cfg.buckets[THROTTLE_BPS_TOTAL].max =
448 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
449 cfg.buckets[THROTTLE_BPS_READ].max =
450 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
451 cfg.buckets[THROTTLE_BPS_WRITE].max =
452 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
453 cfg.buckets[THROTTLE_OPS_TOTAL].max =
454 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
455 cfg.buckets[THROTTLE_OPS_READ].max =
456 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
457 cfg.buckets[THROTTLE_OPS_WRITE].max =
458 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
460 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
462 if (!check_throttle_config(&cfg, &error)) {
463 error_propagate(errp, error);
464 goto early_err;
467 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
468 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
469 on_write_error = parse_block_error_action(buf, 0, &error);
470 if (error) {
471 error_propagate(errp, error);
472 goto early_err;
476 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
477 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
478 on_read_error = parse_block_error_action(buf, 1, &error);
479 if (error) {
480 error_propagate(errp, error);
481 goto early_err;
485 detect_zeroes =
486 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
487 qemu_opt_get(opts, "detect-zeroes"),
488 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
489 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
490 &error);
491 if (error) {
492 error_propagate(errp, error);
493 goto early_err;
496 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
497 !(bdrv_flags & BDRV_O_UNMAP)) {
498 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
499 "without setting discard operation to unmap");
500 goto early_err;
503 /* init */
504 if ((!file || !*file) && !has_driver_specific_opts) {
505 blk = blk_new_with_bs(qemu_opts_id(opts), errp);
506 if (!blk) {
507 goto early_err;
510 bs = blk_bs(blk);
511 bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
512 bs->read_only = ro;
514 QDECREF(bs_opts);
515 } else {
516 if (file && !*file) {
517 file = NULL;
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 if (copy_on_read) {
527 bdrv_flags |= BDRV_O_COPY_ON_READ;
530 if (runstate_check(RUN_STATE_INMIGRATE)) {
531 bdrv_flags |= BDRV_O_INCOMING;
534 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
536 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
537 errp);
538 if (!blk) {
539 goto err_no_bs_opts;
541 bs = blk_bs(blk);
544 bs->detect_zeroes = detect_zeroes;
546 bdrv_set_on_error(bs, on_read_error, on_write_error);
548 /* disk I/O throttling */
549 if (throttle_enabled(&cfg)) {
550 bdrv_io_limits_enable(bs);
551 bdrv_set_io_limits(bs, &cfg);
554 if (bdrv_key_required(bs)) {
555 autostart = 0;
558 err_no_bs_opts:
559 qemu_opts_del(opts);
560 return blk;
562 early_err:
563 qemu_opts_del(opts);
564 err_no_opts:
565 QDECREF(bs_opts);
566 return NULL;
569 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
570 Error **errp)
572 const char *value;
574 value = qemu_opt_get(opts, from);
575 if (value) {
576 if (qemu_opt_find(opts, to)) {
577 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
578 "same time", to, from);
579 return;
583 /* rename all items in opts */
584 while ((value = qemu_opt_get(opts, from))) {
585 qemu_opt_set(opts, to, value, &error_abort);
586 qemu_opt_unset(opts, from);
590 QemuOptsList qemu_legacy_drive_opts = {
591 .name = "drive",
592 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
593 .desc = {
595 .name = "bus",
596 .type = QEMU_OPT_NUMBER,
597 .help = "bus number",
599 .name = "unit",
600 .type = QEMU_OPT_NUMBER,
601 .help = "unit number (i.e. lun for scsi)",
603 .name = "index",
604 .type = QEMU_OPT_NUMBER,
605 .help = "index number",
607 .name = "media",
608 .type = QEMU_OPT_STRING,
609 .help = "media type (disk, cdrom)",
611 .name = "if",
612 .type = QEMU_OPT_STRING,
613 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
615 .name = "cyls",
616 .type = QEMU_OPT_NUMBER,
617 .help = "number of cylinders (ide disk geometry)",
619 .name = "heads",
620 .type = QEMU_OPT_NUMBER,
621 .help = "number of heads (ide disk geometry)",
623 .name = "secs",
624 .type = QEMU_OPT_NUMBER,
625 .help = "number of sectors (ide disk geometry)",
627 .name = "trans",
628 .type = QEMU_OPT_STRING,
629 .help = "chs translation (auto, lba, none)",
631 .name = "boot",
632 .type = QEMU_OPT_BOOL,
633 .help = "(deprecated, ignored)",
635 .name = "addr",
636 .type = QEMU_OPT_STRING,
637 .help = "pci address (virtio only)",
639 .name = "serial",
640 .type = QEMU_OPT_STRING,
641 .help = "disk serial number",
643 .name = "file",
644 .type = QEMU_OPT_STRING,
645 .help = "file name",
648 /* Options that are passed on, but have special semantics with -drive */
650 .name = "read-only",
651 .type = QEMU_OPT_BOOL,
652 .help = "open drive file as read-only",
654 .name = "rerror",
655 .type = QEMU_OPT_STRING,
656 .help = "read error action",
658 .name = "werror",
659 .type = QEMU_OPT_STRING,
660 .help = "write error action",
662 .name = "copy-on-read",
663 .type = QEMU_OPT_BOOL,
664 .help = "copy read data from backing file into image file",
667 { /* end of list */ }
671 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
673 const char *value;
674 BlockBackend *blk;
675 DriveInfo *dinfo = NULL;
676 QDict *bs_opts;
677 QemuOpts *legacy_opts;
678 DriveMediaType media = MEDIA_DISK;
679 BlockInterfaceType type;
680 int cyls, heads, secs, translation;
681 int max_devs, bus_id, unit_id, index;
682 const char *devaddr;
683 const char *werror, *rerror;
684 bool read_only = false;
685 bool copy_on_read;
686 const char *serial;
687 const char *filename;
688 Error *local_err = NULL;
689 int i;
691 /* Change legacy command line options into QMP ones */
692 static const struct {
693 const char *from;
694 const char *to;
695 } opt_renames[] = {
696 { "iops", "throttling.iops-total" },
697 { "iops_rd", "throttling.iops-read" },
698 { "iops_wr", "throttling.iops-write" },
700 { "bps", "throttling.bps-total" },
701 { "bps_rd", "throttling.bps-read" },
702 { "bps_wr", "throttling.bps-write" },
704 { "iops_max", "throttling.iops-total-max" },
705 { "iops_rd_max", "throttling.iops-read-max" },
706 { "iops_wr_max", "throttling.iops-write-max" },
708 { "bps_max", "throttling.bps-total-max" },
709 { "bps_rd_max", "throttling.bps-read-max" },
710 { "bps_wr_max", "throttling.bps-write-max" },
712 { "iops_size", "throttling.iops-size" },
714 { "readonly", "read-only" },
717 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
718 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
719 &local_err);
720 if (local_err) {
721 error_report_err(local_err);
722 return NULL;
726 value = qemu_opt_get(all_opts, "cache");
727 if (value) {
728 int flags = 0;
730 if (bdrv_parse_cache_flags(value, &flags) != 0) {
731 error_report("invalid cache option");
732 return NULL;
735 /* Specific options take precedence */
736 if (!qemu_opt_get(all_opts, "cache.writeback")) {
737 qemu_opt_set_bool(all_opts, "cache.writeback",
738 !!(flags & BDRV_O_CACHE_WB), &error_abort);
740 if (!qemu_opt_get(all_opts, "cache.direct")) {
741 qemu_opt_set_bool(all_opts, "cache.direct",
742 !!(flags & BDRV_O_NOCACHE), &error_abort);
744 if (!qemu_opt_get(all_opts, "cache.no-flush")) {
745 qemu_opt_set_bool(all_opts, "cache.no-flush",
746 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
748 qemu_opt_unset(all_opts, "cache");
751 /* Get a QDict for processing the options */
752 bs_opts = qdict_new();
753 qemu_opts_to_qdict(all_opts, bs_opts);
755 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
756 &error_abort);
757 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
758 if (local_err) {
759 error_report_err(local_err);
760 goto fail;
763 /* Deprecated option boot=[on|off] */
764 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
765 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
766 "ignored. Future versions will reject this parameter. Please "
767 "update your scripts.\n");
770 /* Media type */
771 value = qemu_opt_get(legacy_opts, "media");
772 if (value) {
773 if (!strcmp(value, "disk")) {
774 media = MEDIA_DISK;
775 } else if (!strcmp(value, "cdrom")) {
776 media = MEDIA_CDROM;
777 read_only = true;
778 } else {
779 error_report("'%s' invalid media", value);
780 goto fail;
784 /* copy-on-read is disabled with a warning for read-only devices */
785 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
786 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
788 if (read_only && copy_on_read) {
789 error_report("warning: disabling copy-on-read on read-only drive");
790 copy_on_read = false;
793 qdict_put(bs_opts, "read-only",
794 qstring_from_str(read_only ? "on" : "off"));
795 qdict_put(bs_opts, "copy-on-read",
796 qstring_from_str(copy_on_read ? "on" :"off"));
798 /* Controller type */
799 value = qemu_opt_get(legacy_opts, "if");
800 if (value) {
801 for (type = 0;
802 type < IF_COUNT && strcmp(value, if_name[type]);
803 type++) {
805 if (type == IF_COUNT) {
806 error_report("unsupported bus type '%s'", value);
807 goto fail;
809 } else {
810 type = block_default_type;
813 /* Geometry */
814 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
815 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
816 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
818 if (cyls || heads || secs) {
819 if (cyls < 1) {
820 error_report("invalid physical cyls number");
821 goto fail;
823 if (heads < 1) {
824 error_report("invalid physical heads number");
825 goto fail;
827 if (secs < 1) {
828 error_report("invalid physical secs number");
829 goto fail;
833 translation = BIOS_ATA_TRANSLATION_AUTO;
834 value = qemu_opt_get(legacy_opts, "trans");
835 if (value != NULL) {
836 if (!cyls) {
837 error_report("'%s' trans must be used with cyls, heads and secs",
838 value);
839 goto fail;
841 if (!strcmp(value, "none")) {
842 translation = BIOS_ATA_TRANSLATION_NONE;
843 } else if (!strcmp(value, "lba")) {
844 translation = BIOS_ATA_TRANSLATION_LBA;
845 } else if (!strcmp(value, "large")) {
846 translation = BIOS_ATA_TRANSLATION_LARGE;
847 } else if (!strcmp(value, "rechs")) {
848 translation = BIOS_ATA_TRANSLATION_RECHS;
849 } else if (!strcmp(value, "auto")) {
850 translation = BIOS_ATA_TRANSLATION_AUTO;
851 } else {
852 error_report("'%s' invalid translation type", value);
853 goto fail;
857 if (media == MEDIA_CDROM) {
858 if (cyls || secs || heads) {
859 error_report("CHS can't be set with media=cdrom");
860 goto fail;
864 /* Device address specified by bus/unit or index.
865 * If none was specified, try to find the first free one. */
866 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
867 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
868 index = qemu_opt_get_number(legacy_opts, "index", -1);
870 max_devs = if_max_devs[type];
872 if (index != -1) {
873 if (bus_id != 0 || unit_id != -1) {
874 error_report("index cannot be used with bus and unit");
875 goto fail;
877 bus_id = drive_index_to_bus_id(type, index);
878 unit_id = drive_index_to_unit_id(type, index);
881 if (unit_id == -1) {
882 unit_id = 0;
883 while (drive_get(type, bus_id, unit_id) != NULL) {
884 unit_id++;
885 if (max_devs && unit_id >= max_devs) {
886 unit_id -= max_devs;
887 bus_id++;
892 if (max_devs && unit_id >= max_devs) {
893 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
894 goto fail;
897 if (drive_get(type, bus_id, unit_id) != NULL) {
898 error_report("drive with bus=%d, unit=%d (index=%d) exists",
899 bus_id, unit_id, index);
900 goto fail;
903 /* Serial number */
904 serial = qemu_opt_get(legacy_opts, "serial");
906 /* no id supplied -> create one */
907 if (qemu_opts_id(all_opts) == NULL) {
908 char *new_id;
909 const char *mediastr = "";
910 if (type == IF_IDE || type == IF_SCSI) {
911 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
913 if (max_devs) {
914 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
915 mediastr, unit_id);
916 } else {
917 new_id = g_strdup_printf("%s%s%i", if_name[type],
918 mediastr, unit_id);
920 qdict_put(bs_opts, "id", qstring_from_str(new_id));
921 g_free(new_id);
924 /* Add virtio block device */
925 devaddr = qemu_opt_get(legacy_opts, "addr");
926 if (devaddr && type != IF_VIRTIO) {
927 error_report("addr is not supported by this bus type");
928 goto fail;
931 if (type == IF_VIRTIO) {
932 QemuOpts *devopts;
933 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
934 &error_abort);
935 if (arch_type == QEMU_ARCH_S390X) {
936 qemu_opt_set(devopts, "driver", "virtio-blk-s390", &error_abort);
937 } else {
938 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
940 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
941 &error_abort);
942 if (devaddr) {
943 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
947 filename = qemu_opt_get(legacy_opts, "file");
949 /* Check werror/rerror compatibility with if=... */
950 werror = qemu_opt_get(legacy_opts, "werror");
951 if (werror != NULL) {
952 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
953 type != IF_NONE) {
954 error_report("werror is not supported by this bus type");
955 goto fail;
957 qdict_put(bs_opts, "werror", qstring_from_str(werror));
960 rerror = qemu_opt_get(legacy_opts, "rerror");
961 if (rerror != NULL) {
962 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
963 type != IF_NONE) {
964 error_report("rerror is not supported by this bus type");
965 goto fail;
967 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
970 /* Actual block device init: Functionality shared with blockdev-add */
971 blk = blockdev_init(filename, bs_opts, &local_err);
972 bs_opts = NULL;
973 if (!blk) {
974 if (local_err) {
975 error_report_err(local_err);
977 goto fail;
978 } else {
979 assert(!local_err);
982 /* Create legacy DriveInfo */
983 dinfo = g_malloc0(sizeof(*dinfo));
984 dinfo->opts = all_opts;
986 dinfo->cyls = cyls;
987 dinfo->heads = heads;
988 dinfo->secs = secs;
989 dinfo->trans = translation;
991 dinfo->type = type;
992 dinfo->bus = bus_id;
993 dinfo->unit = unit_id;
994 dinfo->devaddr = devaddr;
995 dinfo->serial = g_strdup(serial);
997 blk_set_legacy_dinfo(blk, dinfo);
999 switch(type) {
1000 case IF_IDE:
1001 case IF_SCSI:
1002 case IF_XEN:
1003 case IF_NONE:
1004 dinfo->media_cd = media == MEDIA_CDROM;
1005 break;
1006 default:
1007 break;
1010 fail:
1011 qemu_opts_del(legacy_opts);
1012 QDECREF(bs_opts);
1013 return dinfo;
1016 void hmp_commit(Monitor *mon, const QDict *qdict)
1018 const char *device = qdict_get_str(qdict, "device");
1019 BlockBackend *blk;
1020 int ret;
1022 if (!strcmp(device, "all")) {
1023 ret = bdrv_commit_all();
1024 } else {
1025 blk = blk_by_name(device);
1026 if (!blk) {
1027 monitor_printf(mon, "Device '%s' not found\n", device);
1028 return;
1030 ret = bdrv_commit(blk_bs(blk));
1032 if (ret < 0) {
1033 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1034 strerror(-ret));
1038 static void blockdev_do_action(int kind, void *data, Error **errp)
1040 TransactionAction action;
1041 TransactionActionList list;
1043 action.kind = kind;
1044 action.data = data;
1045 list.value = &action;
1046 list.next = NULL;
1047 qmp_transaction(&list, errp);
1050 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1051 bool has_node_name, const char *node_name,
1052 const char *snapshot_file,
1053 bool has_snapshot_node_name,
1054 const char *snapshot_node_name,
1055 bool has_format, const char *format,
1056 bool has_mode, NewImageMode mode, Error **errp)
1058 BlockdevSnapshot snapshot = {
1059 .has_device = has_device,
1060 .device = (char *) device,
1061 .has_node_name = has_node_name,
1062 .node_name = (char *) node_name,
1063 .snapshot_file = (char *) snapshot_file,
1064 .has_snapshot_node_name = has_snapshot_node_name,
1065 .snapshot_node_name = (char *) snapshot_node_name,
1066 .has_format = has_format,
1067 .format = (char *) format,
1068 .has_mode = has_mode,
1069 .mode = mode,
1071 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1072 &snapshot, errp);
1075 void qmp_blockdev_snapshot_internal_sync(const char *device,
1076 const char *name,
1077 Error **errp)
1079 BlockdevSnapshotInternal snapshot = {
1080 .device = (char *) device,
1081 .name = (char *) name
1084 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1085 &snapshot, errp);
1088 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1089 bool has_id,
1090 const char *id,
1091 bool has_name,
1092 const char *name,
1093 Error **errp)
1095 BlockDriverState *bs;
1096 BlockBackend *blk;
1097 AioContext *aio_context;
1098 QEMUSnapshotInfo sn;
1099 Error *local_err = NULL;
1100 SnapshotInfo *info = NULL;
1101 int ret;
1103 blk = blk_by_name(device);
1104 if (!blk) {
1105 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1106 return NULL;
1108 bs = blk_bs(blk);
1110 if (!has_id) {
1111 id = NULL;
1114 if (!has_name) {
1115 name = NULL;
1118 if (!id && !name) {
1119 error_setg(errp, "Name or id must be provided");
1120 return NULL;
1123 aio_context = bdrv_get_aio_context(bs);
1124 aio_context_acquire(aio_context);
1126 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1127 goto out_aio_context;
1130 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1131 if (local_err) {
1132 error_propagate(errp, local_err);
1133 goto out_aio_context;
1135 if (!ret) {
1136 error_setg(errp,
1137 "Snapshot with id '%s' and name '%s' does not exist on "
1138 "device '%s'",
1139 STR_OR_NULL(id), STR_OR_NULL(name), device);
1140 goto out_aio_context;
1143 bdrv_snapshot_delete(bs, id, name, &local_err);
1144 if (local_err) {
1145 error_propagate(errp, local_err);
1146 goto out_aio_context;
1149 aio_context_release(aio_context);
1151 info = g_new0(SnapshotInfo, 1);
1152 info->id = g_strdup(sn.id_str);
1153 info->name = g_strdup(sn.name);
1154 info->date_nsec = sn.date_nsec;
1155 info->date_sec = sn.date_sec;
1156 info->vm_state_size = sn.vm_state_size;
1157 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1158 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1160 return info;
1162 out_aio_context:
1163 aio_context_release(aio_context);
1164 return NULL;
1168 * block_dirty_bitmap_lookup:
1169 * Return a dirty bitmap (if present), after validating
1170 * the node reference and bitmap names.
1172 * @node: The name of the BDS node to search for bitmaps
1173 * @name: The name of the bitmap to search for
1174 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1175 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1176 * @errp: Output pointer for error information. Can be NULL.
1178 * @return: A bitmap object on success, or NULL on failure.
1180 static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1181 const char *name,
1182 BlockDriverState **pbs,
1183 AioContext **paio,
1184 Error **errp)
1186 BlockDriverState *bs;
1187 BdrvDirtyBitmap *bitmap;
1188 AioContext *aio_context;
1190 if (!node) {
1191 error_setg(errp, "Node cannot be NULL");
1192 return NULL;
1194 if (!name) {
1195 error_setg(errp, "Bitmap name cannot be NULL");
1196 return NULL;
1198 bs = bdrv_lookup_bs(node, node, NULL);
1199 if (!bs) {
1200 error_setg(errp, "Node '%s' not found", node);
1201 return NULL;
1204 aio_context = bdrv_get_aio_context(bs);
1205 aio_context_acquire(aio_context);
1207 bitmap = bdrv_find_dirty_bitmap(bs, name);
1208 if (!bitmap) {
1209 error_setg(errp, "Dirty bitmap '%s' not found", name);
1210 goto fail;
1213 if (pbs) {
1214 *pbs = bs;
1216 if (paio) {
1217 *paio = aio_context;
1218 } else {
1219 aio_context_release(aio_context);
1222 return bitmap;
1224 fail:
1225 aio_context_release(aio_context);
1226 return NULL;
1229 /* New and old BlockDriverState structs for atomic group operations */
1231 typedef struct BlkTransactionState BlkTransactionState;
1233 /* Only prepare() may fail. In a single transaction, only one of commit() or
1234 abort() will be called, clean() will always be called if it present. */
1235 typedef struct BdrvActionOps {
1236 /* Size of state struct, in bytes. */
1237 size_t instance_size;
1238 /* Prepare the work, must NOT be NULL. */
1239 void (*prepare)(BlkTransactionState *common, Error **errp);
1240 /* Commit the changes, can be NULL. */
1241 void (*commit)(BlkTransactionState *common);
1242 /* Abort the changes on fail, can be NULL. */
1243 void (*abort)(BlkTransactionState *common);
1244 /* Clean up resource in the end, can be NULL. */
1245 void (*clean)(BlkTransactionState *common);
1246 } BdrvActionOps;
1249 * This structure must be arranged as first member in child type, assuming
1250 * that compiler will also arrange it to the same address with parent instance.
1251 * Later it will be used in free().
1253 struct BlkTransactionState {
1254 TransactionAction *action;
1255 const BdrvActionOps *ops;
1256 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
1259 /* internal snapshot private data */
1260 typedef struct InternalSnapshotState {
1261 BlkTransactionState common;
1262 BlockDriverState *bs;
1263 AioContext *aio_context;
1264 QEMUSnapshotInfo sn;
1265 } InternalSnapshotState;
1267 static void internal_snapshot_prepare(BlkTransactionState *common,
1268 Error **errp)
1270 Error *local_err = NULL;
1271 const char *device;
1272 const char *name;
1273 BlockBackend *blk;
1274 BlockDriverState *bs;
1275 QEMUSnapshotInfo old_sn, *sn;
1276 bool ret;
1277 qemu_timeval tv;
1278 BlockdevSnapshotInternal *internal;
1279 InternalSnapshotState *state;
1280 int ret1;
1282 g_assert(common->action->kind ==
1283 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1284 internal = common->action->blockdev_snapshot_internal_sync;
1285 state = DO_UPCAST(InternalSnapshotState, common, common);
1287 /* 1. parse input */
1288 device = internal->device;
1289 name = internal->name;
1291 /* 2. check for validation */
1292 blk = blk_by_name(device);
1293 if (!blk) {
1294 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1295 return;
1297 bs = blk_bs(blk);
1299 /* AioContext is released in .clean() */
1300 state->aio_context = bdrv_get_aio_context(bs);
1301 aio_context_acquire(state->aio_context);
1303 if (!bdrv_is_inserted(bs)) {
1304 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1305 return;
1308 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1309 return;
1312 if (bdrv_is_read_only(bs)) {
1313 error_setg(errp, "Device '%s' is read only", device);
1314 return;
1317 if (!bdrv_can_snapshot(bs)) {
1318 error_setg(errp, "Block format '%s' used by device '%s' "
1319 "does not support internal snapshots",
1320 bs->drv->format_name, device);
1321 return;
1324 if (!strlen(name)) {
1325 error_setg(errp, "Name is empty");
1326 return;
1329 /* check whether a snapshot with name exist */
1330 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1331 &local_err);
1332 if (local_err) {
1333 error_propagate(errp, local_err);
1334 return;
1335 } else if (ret) {
1336 error_setg(errp,
1337 "Snapshot with name '%s' already exists on device '%s'",
1338 name, device);
1339 return;
1342 /* 3. take the snapshot */
1343 sn = &state->sn;
1344 pstrcpy(sn->name, sizeof(sn->name), name);
1345 qemu_gettimeofday(&tv);
1346 sn->date_sec = tv.tv_sec;
1347 sn->date_nsec = tv.tv_usec * 1000;
1348 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1350 ret1 = bdrv_snapshot_create(bs, sn);
1351 if (ret1 < 0) {
1352 error_setg_errno(errp, -ret1,
1353 "Failed to create snapshot '%s' on device '%s'",
1354 name, device);
1355 return;
1358 /* 4. succeed, mark a snapshot is created */
1359 state->bs = bs;
1362 static void internal_snapshot_abort(BlkTransactionState *common)
1364 InternalSnapshotState *state =
1365 DO_UPCAST(InternalSnapshotState, common, common);
1366 BlockDriverState *bs = state->bs;
1367 QEMUSnapshotInfo *sn = &state->sn;
1368 Error *local_error = NULL;
1370 if (!bs) {
1371 return;
1374 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1375 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1376 "device '%s' in abort: %s",
1377 sn->id_str,
1378 sn->name,
1379 bdrv_get_device_name(bs),
1380 error_get_pretty(local_error));
1381 error_free(local_error);
1385 static void internal_snapshot_clean(BlkTransactionState *common)
1387 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1388 common, common);
1390 if (state->aio_context) {
1391 aio_context_release(state->aio_context);
1395 /* external snapshot private data */
1396 typedef struct ExternalSnapshotState {
1397 BlkTransactionState common;
1398 BlockDriverState *old_bs;
1399 BlockDriverState *new_bs;
1400 AioContext *aio_context;
1401 } ExternalSnapshotState;
1403 static void external_snapshot_prepare(BlkTransactionState *common,
1404 Error **errp)
1406 BlockDriver *drv;
1407 int flags, ret;
1408 QDict *options = NULL;
1409 Error *local_err = NULL;
1410 bool has_device = false;
1411 const char *device;
1412 bool has_node_name = false;
1413 const char *node_name;
1414 bool has_snapshot_node_name = false;
1415 const char *snapshot_node_name;
1416 const char *new_image_file;
1417 const char *format = "qcow2";
1418 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1419 ExternalSnapshotState *state =
1420 DO_UPCAST(ExternalSnapshotState, common, common);
1421 TransactionAction *action = common->action;
1423 /* get parameters */
1424 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1426 has_device = action->blockdev_snapshot_sync->has_device;
1427 device = action->blockdev_snapshot_sync->device;
1428 has_node_name = action->blockdev_snapshot_sync->has_node_name;
1429 node_name = action->blockdev_snapshot_sync->node_name;
1430 has_snapshot_node_name =
1431 action->blockdev_snapshot_sync->has_snapshot_node_name;
1432 snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name;
1434 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1435 if (action->blockdev_snapshot_sync->has_format) {
1436 format = action->blockdev_snapshot_sync->format;
1438 if (action->blockdev_snapshot_sync->has_mode) {
1439 mode = action->blockdev_snapshot_sync->mode;
1442 /* start processing */
1443 drv = bdrv_find_format(format);
1444 if (!drv) {
1445 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1446 return;
1449 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
1450 has_node_name ? node_name : NULL,
1451 &local_err);
1452 if (local_err) {
1453 error_propagate(errp, local_err);
1454 return;
1457 if (has_node_name && !has_snapshot_node_name) {
1458 error_setg(errp, "New snapshot node name missing");
1459 return;
1462 if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) {
1463 error_setg(errp, "New snapshot node name already existing");
1464 return;
1467 /* Acquire AioContext now so any threads operating on old_bs stop */
1468 state->aio_context = bdrv_get_aio_context(state->old_bs);
1469 aio_context_acquire(state->aio_context);
1471 if (!bdrv_is_inserted(state->old_bs)) {
1472 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1473 return;
1476 if (bdrv_op_is_blocked(state->old_bs,
1477 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1478 return;
1481 if (!bdrv_is_read_only(state->old_bs)) {
1482 if (bdrv_flush(state->old_bs)) {
1483 error_set(errp, QERR_IO_ERROR);
1484 return;
1488 if (!bdrv_is_first_non_filter(state->old_bs)) {
1489 error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
1490 return;
1493 flags = state->old_bs->open_flags;
1495 /* create new image w/backing file */
1496 if (mode != NEW_IMAGE_MODE_EXISTING) {
1497 bdrv_img_create(new_image_file, format,
1498 state->old_bs->filename,
1499 state->old_bs->drv->format_name,
1500 NULL, -1, flags, &local_err, false);
1501 if (local_err) {
1502 error_propagate(errp, local_err);
1503 return;
1507 if (has_snapshot_node_name) {
1508 options = qdict_new();
1509 qdict_put(options, "node-name",
1510 qstring_from_str(snapshot_node_name));
1513 /* TODO Inherit bs->options or only take explicit options with an
1514 * extended QMP command? */
1515 assert(state->new_bs == NULL);
1516 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
1517 flags | BDRV_O_NO_BACKING, drv, &local_err);
1518 /* We will manually add the backing_hd field to the bs later */
1519 if (ret != 0) {
1520 error_propagate(errp, local_err);
1524 static void external_snapshot_commit(BlkTransactionState *common)
1526 ExternalSnapshotState *state =
1527 DO_UPCAST(ExternalSnapshotState, common, common);
1529 bdrv_set_aio_context(state->new_bs, state->aio_context);
1531 /* This removes our old bs and adds the new bs */
1532 bdrv_append(state->new_bs, state->old_bs);
1533 /* We don't need (or want) to use the transactional
1534 * bdrv_reopen_multiple() across all the entries at once, because we
1535 * don't want to abort all of them if one of them fails the reopen */
1536 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
1537 NULL);
1539 aio_context_release(state->aio_context);
1542 static void external_snapshot_abort(BlkTransactionState *common)
1544 ExternalSnapshotState *state =
1545 DO_UPCAST(ExternalSnapshotState, common, common);
1546 if (state->new_bs) {
1547 bdrv_unref(state->new_bs);
1549 if (state->aio_context) {
1550 aio_context_release(state->aio_context);
1554 typedef struct DriveBackupState {
1555 BlkTransactionState common;
1556 BlockDriverState *bs;
1557 AioContext *aio_context;
1558 BlockJob *job;
1559 } DriveBackupState;
1561 static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1563 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1564 BlockDriverState *bs;
1565 BlockBackend *blk;
1566 DriveBackup *backup;
1567 Error *local_err = NULL;
1569 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1570 backup = common->action->drive_backup;
1572 blk = blk_by_name(backup->device);
1573 if (!blk) {
1574 error_set(errp, QERR_DEVICE_NOT_FOUND, backup->device);
1575 return;
1577 bs = blk_bs(blk);
1579 /* AioContext is released in .clean() */
1580 state->aio_context = bdrv_get_aio_context(bs);
1581 aio_context_acquire(state->aio_context);
1583 qmp_drive_backup(backup->device, backup->target,
1584 backup->has_format, backup->format,
1585 backup->sync,
1586 backup->has_mode, backup->mode,
1587 backup->has_speed, backup->speed,
1588 backup->has_bitmap, backup->bitmap,
1589 backup->has_on_source_error, backup->on_source_error,
1590 backup->has_on_target_error, backup->on_target_error,
1591 &local_err);
1592 if (local_err) {
1593 error_propagate(errp, local_err);
1594 return;
1597 state->bs = bs;
1598 state->job = state->bs->job;
1601 static void drive_backup_abort(BlkTransactionState *common)
1603 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1604 BlockDriverState *bs = state->bs;
1606 /* Only cancel if it's the job we started */
1607 if (bs && bs->job && bs->job == state->job) {
1608 block_job_cancel_sync(bs->job);
1612 static void drive_backup_clean(BlkTransactionState *common)
1614 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1616 if (state->aio_context) {
1617 aio_context_release(state->aio_context);
1621 typedef struct BlockdevBackupState {
1622 BlkTransactionState common;
1623 BlockDriverState *bs;
1624 BlockJob *job;
1625 AioContext *aio_context;
1626 } BlockdevBackupState;
1628 static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp)
1630 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1631 BlockdevBackup *backup;
1632 BlockDriverState *bs, *target;
1633 BlockBackend *blk;
1634 Error *local_err = NULL;
1636 assert(common->action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1637 backup = common->action->blockdev_backup;
1639 blk = blk_by_name(backup->device);
1640 if (!blk) {
1641 error_setg(errp, "Device '%s' not found", backup->device);
1642 return;
1644 bs = blk_bs(blk);
1646 blk = blk_by_name(backup->target);
1647 if (!blk) {
1648 error_setg(errp, "Device '%s' not found", backup->target);
1649 return;
1651 target = blk_bs(blk);
1653 /* AioContext is released in .clean() */
1654 state->aio_context = bdrv_get_aio_context(bs);
1655 if (state->aio_context != bdrv_get_aio_context(target)) {
1656 state->aio_context = NULL;
1657 error_setg(errp, "Backup between two IO threads is not implemented");
1658 return;
1660 aio_context_acquire(state->aio_context);
1662 qmp_blockdev_backup(backup->device, backup->target,
1663 backup->sync,
1664 backup->has_speed, backup->speed,
1665 backup->has_on_source_error, backup->on_source_error,
1666 backup->has_on_target_error, backup->on_target_error,
1667 &local_err);
1668 if (local_err) {
1669 error_propagate(errp, local_err);
1670 return;
1673 state->bs = bs;
1674 state->job = state->bs->job;
1677 static void blockdev_backup_abort(BlkTransactionState *common)
1679 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1680 BlockDriverState *bs = state->bs;
1682 /* Only cancel if it's the job we started */
1683 if (bs && bs->job && bs->job == state->job) {
1684 block_job_cancel_sync(bs->job);
1688 static void blockdev_backup_clean(BlkTransactionState *common)
1690 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1692 if (state->aio_context) {
1693 aio_context_release(state->aio_context);
1697 static void abort_prepare(BlkTransactionState *common, Error **errp)
1699 error_setg(errp, "Transaction aborted using Abort action");
1702 static void abort_commit(BlkTransactionState *common)
1704 g_assert_not_reached(); /* this action never succeeds */
1707 static const BdrvActionOps actions[] = {
1708 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1709 .instance_size = sizeof(ExternalSnapshotState),
1710 .prepare = external_snapshot_prepare,
1711 .commit = external_snapshot_commit,
1712 .abort = external_snapshot_abort,
1714 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1715 .instance_size = sizeof(DriveBackupState),
1716 .prepare = drive_backup_prepare,
1717 .abort = drive_backup_abort,
1718 .clean = drive_backup_clean,
1720 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
1721 .instance_size = sizeof(BlockdevBackupState),
1722 .prepare = blockdev_backup_prepare,
1723 .abort = blockdev_backup_abort,
1724 .clean = blockdev_backup_clean,
1726 [TRANSACTION_ACTION_KIND_ABORT] = {
1727 .instance_size = sizeof(BlkTransactionState),
1728 .prepare = abort_prepare,
1729 .commit = abort_commit,
1731 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1732 .instance_size = sizeof(InternalSnapshotState),
1733 .prepare = internal_snapshot_prepare,
1734 .abort = internal_snapshot_abort,
1735 .clean = internal_snapshot_clean,
1740 * 'Atomic' group operations. The operations are performed as a set, and if
1741 * any fail then we roll back all operations in the group.
1743 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1745 TransactionActionList *dev_entry = dev_list;
1746 BlkTransactionState *state, *next;
1747 Error *local_err = NULL;
1749 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
1750 QSIMPLEQ_INIT(&snap_bdrv_states);
1752 /* drain all i/o before any operations */
1753 bdrv_drain_all();
1755 /* We don't do anything in this loop that commits us to the operations */
1756 while (NULL != dev_entry) {
1757 TransactionAction *dev_info = NULL;
1758 const BdrvActionOps *ops;
1760 dev_info = dev_entry->value;
1761 dev_entry = dev_entry->next;
1763 assert(dev_info->kind < ARRAY_SIZE(actions));
1765 ops = &actions[dev_info->kind];
1766 assert(ops->instance_size > 0);
1768 state = g_malloc0(ops->instance_size);
1769 state->ops = ops;
1770 state->action = dev_info;
1771 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
1773 state->ops->prepare(state, &local_err);
1774 if (local_err) {
1775 error_propagate(errp, local_err);
1776 goto delete_and_fail;
1780 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1781 if (state->ops->commit) {
1782 state->ops->commit(state);
1786 /* success */
1787 goto exit;
1789 delete_and_fail:
1790 /* failure, and it is all-or-none; roll back all operations */
1791 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1792 if (state->ops->abort) {
1793 state->ops->abort(state);
1796 exit:
1797 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1798 if (state->ops->clean) {
1799 state->ops->clean(state);
1801 g_free(state);
1806 static void eject_device(BlockBackend *blk, int force, Error **errp)
1808 BlockDriverState *bs = blk_bs(blk);
1809 AioContext *aio_context;
1811 aio_context = bdrv_get_aio_context(bs);
1812 aio_context_acquire(aio_context);
1814 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
1815 goto out;
1817 if (!blk_dev_has_removable_media(blk)) {
1818 error_setg(errp, "Device '%s' is not removable",
1819 bdrv_get_device_name(bs));
1820 goto out;
1823 if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) {
1824 blk_dev_eject_request(blk, force);
1825 if (!force) {
1826 error_setg(errp, "Device '%s' is locked",
1827 bdrv_get_device_name(bs));
1828 goto out;
1832 bdrv_close(bs);
1834 out:
1835 aio_context_release(aio_context);
1838 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1840 BlockBackend *blk;
1842 blk = blk_by_name(device);
1843 if (!blk) {
1844 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1845 return;
1848 eject_device(blk, force, errp);
1851 void qmp_block_passwd(bool has_device, const char *device,
1852 bool has_node_name, const char *node_name,
1853 const char *password, Error **errp)
1855 Error *local_err = NULL;
1856 BlockDriverState *bs;
1857 AioContext *aio_context;
1859 bs = bdrv_lookup_bs(has_device ? device : NULL,
1860 has_node_name ? node_name : NULL,
1861 &local_err);
1862 if (local_err) {
1863 error_propagate(errp, local_err);
1864 return;
1867 aio_context = bdrv_get_aio_context(bs);
1868 aio_context_acquire(aio_context);
1870 bdrv_add_key(bs, password, errp);
1872 aio_context_release(aio_context);
1875 /* Assumes AioContext is held */
1876 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1877 int bdrv_flags, BlockDriver *drv,
1878 const char *password, Error **errp)
1880 Error *local_err = NULL;
1881 int ret;
1883 ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
1884 if (ret < 0) {
1885 error_propagate(errp, local_err);
1886 return;
1889 bdrv_add_key(bs, password, errp);
1892 void qmp_change_blockdev(const char *device, const char *filename,
1893 const char *format, Error **errp)
1895 BlockBackend *blk;
1896 BlockDriverState *bs;
1897 AioContext *aio_context;
1898 BlockDriver *drv = NULL;
1899 int bdrv_flags;
1900 Error *err = NULL;
1902 blk = blk_by_name(device);
1903 if (!blk) {
1904 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1905 return;
1907 bs = blk_bs(blk);
1909 aio_context = bdrv_get_aio_context(bs);
1910 aio_context_acquire(aio_context);
1912 if (format) {
1913 drv = bdrv_find_whitelisted_format(format, bs->read_only);
1914 if (!drv) {
1915 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1916 goto out;
1920 eject_device(blk, 0, &err);
1921 if (err) {
1922 error_propagate(errp, err);
1923 goto out;
1926 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1927 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1929 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1931 out:
1932 aio_context_release(aio_context);
1935 /* throttling disk I/O limits */
1936 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1937 int64_t bps_wr,
1938 int64_t iops,
1939 int64_t iops_rd,
1940 int64_t iops_wr,
1941 bool has_bps_max,
1942 int64_t bps_max,
1943 bool has_bps_rd_max,
1944 int64_t bps_rd_max,
1945 bool has_bps_wr_max,
1946 int64_t bps_wr_max,
1947 bool has_iops_max,
1948 int64_t iops_max,
1949 bool has_iops_rd_max,
1950 int64_t iops_rd_max,
1951 bool has_iops_wr_max,
1952 int64_t iops_wr_max,
1953 bool has_iops_size,
1954 int64_t iops_size, Error **errp)
1956 ThrottleConfig cfg;
1957 BlockDriverState *bs;
1958 BlockBackend *blk;
1959 AioContext *aio_context;
1961 blk = blk_by_name(device);
1962 if (!blk) {
1963 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1964 return;
1966 bs = blk_bs(blk);
1968 memset(&cfg, 0, sizeof(cfg));
1969 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1970 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1971 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1973 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1974 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
1975 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1977 if (has_bps_max) {
1978 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
1980 if (has_bps_rd_max) {
1981 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
1983 if (has_bps_wr_max) {
1984 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
1986 if (has_iops_max) {
1987 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
1989 if (has_iops_rd_max) {
1990 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
1992 if (has_iops_wr_max) {
1993 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
1996 if (has_iops_size) {
1997 cfg.op_size = iops_size;
2000 if (!check_throttle_config(&cfg, errp)) {
2001 return;
2004 aio_context = bdrv_get_aio_context(bs);
2005 aio_context_acquire(aio_context);
2007 if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
2008 bdrv_io_limits_enable(bs);
2009 } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
2010 bdrv_io_limits_disable(bs);
2013 if (bs->io_limits_enabled) {
2014 bdrv_set_io_limits(bs, &cfg);
2017 aio_context_release(aio_context);
2020 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2021 bool has_granularity, uint32_t granularity,
2022 Error **errp)
2024 AioContext *aio_context;
2025 BlockDriverState *bs;
2027 if (!name || name[0] == '\0') {
2028 error_setg(errp, "Bitmap name cannot be empty");
2029 return;
2032 bs = bdrv_lookup_bs(node, node, errp);
2033 if (!bs) {
2034 return;
2037 aio_context = bdrv_get_aio_context(bs);
2038 aio_context_acquire(aio_context);
2040 if (has_granularity) {
2041 if (granularity < 512 || !is_power_of_2(granularity)) {
2042 error_setg(errp, "Granularity must be power of 2 "
2043 "and at least 512");
2044 goto out;
2046 } else {
2047 /* Default to cluster size, if available: */
2048 granularity = bdrv_get_default_bitmap_granularity(bs);
2051 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2053 out:
2054 aio_context_release(aio_context);
2057 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2058 Error **errp)
2060 AioContext *aio_context;
2061 BlockDriverState *bs;
2062 BdrvDirtyBitmap *bitmap;
2064 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2065 if (!bitmap || !bs) {
2066 return;
2069 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2070 error_setg(errp,
2071 "Bitmap '%s' is currently frozen and cannot be removed",
2072 name);
2073 goto out;
2075 bdrv_dirty_bitmap_make_anon(bitmap);
2076 bdrv_release_dirty_bitmap(bs, bitmap);
2078 out:
2079 aio_context_release(aio_context);
2083 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2084 * immediately after a full backup operation.
2086 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2087 Error **errp)
2089 AioContext *aio_context;
2090 BdrvDirtyBitmap *bitmap;
2091 BlockDriverState *bs;
2093 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2094 if (!bitmap || !bs) {
2095 return;
2098 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2099 error_setg(errp,
2100 "Bitmap '%s' is currently frozen and cannot be modified",
2101 name);
2102 goto out;
2103 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2104 error_setg(errp,
2105 "Bitmap '%s' is currently disabled and cannot be cleared",
2106 name);
2107 goto out;
2110 bdrv_clear_dirty_bitmap(bitmap);
2112 out:
2113 aio_context_release(aio_context);
2116 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2118 const char *id = qdict_get_str(qdict, "id");
2119 BlockBackend *blk;
2120 BlockDriverState *bs;
2121 AioContext *aio_context;
2122 Error *local_err = NULL;
2124 blk = blk_by_name(id);
2125 if (!blk) {
2126 error_report("Device '%s' not found", id);
2127 return;
2129 bs = blk_bs(blk);
2131 if (!blk_legacy_dinfo(blk)) {
2132 error_report("Deleting device added with blockdev-add"
2133 " is not supported");
2134 return;
2137 aio_context = bdrv_get_aio_context(bs);
2138 aio_context_acquire(aio_context);
2140 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2141 error_report_err(local_err);
2142 aio_context_release(aio_context);
2143 return;
2146 /* quiesce block driver; prevent further io */
2147 bdrv_drain_all();
2148 bdrv_flush(bs);
2149 bdrv_close(bs);
2151 /* if we have a device attached to this BlockDriverState
2152 * then we need to make the drive anonymous until the device
2153 * can be removed. If this is a drive with no device backing
2154 * then we can just get rid of the block driver state right here.
2156 if (blk_get_attached_dev(blk)) {
2157 blk_hide_on_behalf_of_hmp_drive_del(blk);
2158 /* Further I/O must not pause the guest */
2159 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
2160 BLOCKDEV_ON_ERROR_REPORT);
2161 } else {
2162 blk_unref(blk);
2165 aio_context_release(aio_context);
2168 void qmp_block_resize(bool has_device, const char *device,
2169 bool has_node_name, const char *node_name,
2170 int64_t size, Error **errp)
2172 Error *local_err = NULL;
2173 BlockDriverState *bs;
2174 AioContext *aio_context;
2175 int ret;
2177 bs = bdrv_lookup_bs(has_device ? device : NULL,
2178 has_node_name ? node_name : NULL,
2179 &local_err);
2180 if (local_err) {
2181 error_propagate(errp, local_err);
2182 return;
2185 aio_context = bdrv_get_aio_context(bs);
2186 aio_context_acquire(aio_context);
2188 if (!bdrv_is_first_non_filter(bs)) {
2189 error_set(errp, QERR_FEATURE_DISABLED, "resize");
2190 goto out;
2193 if (size < 0) {
2194 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2195 goto out;
2198 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2199 error_set(errp, QERR_DEVICE_IN_USE, device);
2200 goto out;
2203 /* complete all in-flight operations before resizing the device */
2204 bdrv_drain_all();
2206 ret = bdrv_truncate(bs, size);
2207 switch (ret) {
2208 case 0:
2209 break;
2210 case -ENOMEDIUM:
2211 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2212 break;
2213 case -ENOTSUP:
2214 error_set(errp, QERR_UNSUPPORTED);
2215 break;
2216 case -EACCES:
2217 error_setg(errp, "Device '%s' is read only", device);
2218 break;
2219 case -EBUSY:
2220 error_set(errp, QERR_DEVICE_IN_USE, device);
2221 break;
2222 default:
2223 error_setg_errno(errp, -ret, "Could not resize");
2224 break;
2227 out:
2228 aio_context_release(aio_context);
2231 static void block_job_cb(void *opaque, int ret)
2233 /* Note that this function may be executed from another AioContext besides
2234 * the QEMU main loop. If you need to access anything that assumes the
2235 * QEMU global mutex, use a BH or introduce a mutex.
2238 BlockDriverState *bs = opaque;
2239 const char *msg = NULL;
2241 trace_block_job_cb(bs, bs->job, ret);
2243 assert(bs->job);
2245 if (ret < 0) {
2246 msg = strerror(-ret);
2249 if (block_job_is_cancelled(bs->job)) {
2250 block_job_event_cancelled(bs->job);
2251 } else {
2252 block_job_event_completed(bs->job, msg);
2255 bdrv_put_ref_bh_schedule(bs);
2258 void qmp_block_stream(const char *device,
2259 bool has_base, const char *base,
2260 bool has_backing_file, const char *backing_file,
2261 bool has_speed, int64_t speed,
2262 bool has_on_error, BlockdevOnError on_error,
2263 Error **errp)
2265 BlockBackend *blk;
2266 BlockDriverState *bs;
2267 BlockDriverState *base_bs = NULL;
2268 AioContext *aio_context;
2269 Error *local_err = NULL;
2270 const char *base_name = NULL;
2272 if (!has_on_error) {
2273 on_error = BLOCKDEV_ON_ERROR_REPORT;
2276 blk = blk_by_name(device);
2277 if (!blk) {
2278 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2279 return;
2281 bs = blk_bs(blk);
2283 aio_context = bdrv_get_aio_context(bs);
2284 aio_context_acquire(aio_context);
2286 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2287 goto out;
2290 if (has_base) {
2291 base_bs = bdrv_find_backing_image(bs, base);
2292 if (base_bs == NULL) {
2293 error_set(errp, QERR_BASE_NOT_FOUND, base);
2294 goto out;
2296 assert(bdrv_get_aio_context(base_bs) == aio_context);
2297 base_name = base;
2300 /* if we are streaming the entire chain, the result will have no backing
2301 * file, and specifying one is therefore an error */
2302 if (base_bs == NULL && has_backing_file) {
2303 error_setg(errp, "backing file specified, but streaming the "
2304 "entire chain");
2305 goto out;
2308 /* backing_file string overrides base bs filename */
2309 base_name = has_backing_file ? backing_file : base_name;
2311 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
2312 on_error, block_job_cb, bs, &local_err);
2313 if (local_err) {
2314 error_propagate(errp, local_err);
2315 goto out;
2318 trace_qmp_block_stream(bs, bs->job);
2320 out:
2321 aio_context_release(aio_context);
2324 void qmp_block_commit(const char *device,
2325 bool has_base, const char *base,
2326 bool has_top, const char *top,
2327 bool has_backing_file, const char *backing_file,
2328 bool has_speed, int64_t speed,
2329 Error **errp)
2331 BlockBackend *blk;
2332 BlockDriverState *bs;
2333 BlockDriverState *base_bs, *top_bs;
2334 AioContext *aio_context;
2335 Error *local_err = NULL;
2336 /* This will be part of the QMP command, if/when the
2337 * BlockdevOnError change for blkmirror makes it in
2339 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2341 if (!has_speed) {
2342 speed = 0;
2345 /* Important Note:
2346 * libvirt relies on the DeviceNotFound error class in order to probe for
2347 * live commit feature versions; for this to work, we must make sure to
2348 * perform the device lookup before any generic errors that may occur in a
2349 * scenario in which all optional arguments are omitted. */
2350 blk = blk_by_name(device);
2351 if (!blk) {
2352 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2353 return;
2355 bs = blk_bs(blk);
2357 aio_context = bdrv_get_aio_context(bs);
2358 aio_context_acquire(aio_context);
2360 /* drain all i/o before commits */
2361 bdrv_drain_all();
2363 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
2364 goto out;
2367 /* default top_bs is the active layer */
2368 top_bs = bs;
2370 if (has_top && top) {
2371 if (strcmp(bs->filename, top) != 0) {
2372 top_bs = bdrv_find_backing_image(bs, top);
2376 if (top_bs == NULL) {
2377 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2378 goto out;
2381 assert(bdrv_get_aio_context(top_bs) == aio_context);
2383 if (has_base && base) {
2384 base_bs = bdrv_find_backing_image(top_bs, base);
2385 } else {
2386 base_bs = bdrv_find_base(top_bs);
2389 if (base_bs == NULL) {
2390 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
2391 goto out;
2394 assert(bdrv_get_aio_context(base_bs) == aio_context);
2396 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2397 goto out;
2400 /* Do not allow attempts to commit an image into itself */
2401 if (top_bs == base_bs) {
2402 error_setg(errp, "cannot commit an image into itself");
2403 goto out;
2406 if (top_bs == bs) {
2407 if (has_backing_file) {
2408 error_setg(errp, "'backing-file' specified,"
2409 " but 'top' is the active layer");
2410 goto out;
2412 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2413 bs, &local_err);
2414 } else {
2415 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
2416 has_backing_file ? backing_file : NULL, &local_err);
2418 if (local_err != NULL) {
2419 error_propagate(errp, local_err);
2420 goto out;
2423 out:
2424 aio_context_release(aio_context);
2427 void qmp_drive_backup(const char *device, const char *target,
2428 bool has_format, const char *format,
2429 enum MirrorSyncMode sync,
2430 bool has_mode, enum NewImageMode mode,
2431 bool has_speed, int64_t speed,
2432 bool has_bitmap, const char *bitmap,
2433 bool has_on_source_error, BlockdevOnError on_source_error,
2434 bool has_on_target_error, BlockdevOnError on_target_error,
2435 Error **errp)
2437 BlockBackend *blk;
2438 BlockDriverState *bs;
2439 BlockDriverState *target_bs;
2440 BlockDriverState *source = NULL;
2441 BdrvDirtyBitmap *bmap = NULL;
2442 AioContext *aio_context;
2443 BlockDriver *drv = NULL;
2444 Error *local_err = NULL;
2445 int flags;
2446 int64_t size;
2447 int ret;
2449 if (!has_speed) {
2450 speed = 0;
2452 if (!has_on_source_error) {
2453 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2455 if (!has_on_target_error) {
2456 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2458 if (!has_mode) {
2459 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2462 blk = blk_by_name(device);
2463 if (!blk) {
2464 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2465 return;
2467 bs = blk_bs(blk);
2469 aio_context = bdrv_get_aio_context(bs);
2470 aio_context_acquire(aio_context);
2472 /* Although backup_run has this check too, we need to use bs->drv below, so
2473 * do an early check redundantly. */
2474 if (!bdrv_is_inserted(bs)) {
2475 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2476 goto out;
2479 if (!has_format) {
2480 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2482 if (format) {
2483 drv = bdrv_find_format(format);
2484 if (!drv) {
2485 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
2486 goto out;
2490 /* Early check to avoid creating target */
2491 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
2492 goto out;
2495 flags = bs->open_flags | BDRV_O_RDWR;
2497 /* See if we have a backing HD we can use to create our new image
2498 * on top of. */
2499 if (sync == MIRROR_SYNC_MODE_TOP) {
2500 source = bs->backing_hd;
2501 if (!source) {
2502 sync = MIRROR_SYNC_MODE_FULL;
2505 if (sync == MIRROR_SYNC_MODE_NONE) {
2506 source = bs;
2509 size = bdrv_getlength(bs);
2510 if (size < 0) {
2511 error_setg_errno(errp, -size, "bdrv_getlength failed");
2512 goto out;
2515 if (mode != NEW_IMAGE_MODE_EXISTING) {
2516 assert(format && drv);
2517 if (source) {
2518 bdrv_img_create(target, format, source->filename,
2519 source->drv->format_name, NULL,
2520 size, flags, &local_err, false);
2521 } else {
2522 bdrv_img_create(target, format, NULL, NULL, NULL,
2523 size, flags, &local_err, false);
2527 if (local_err) {
2528 error_propagate(errp, local_err);
2529 goto out;
2532 target_bs = NULL;
2533 ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err);
2534 if (ret < 0) {
2535 error_propagate(errp, local_err);
2536 goto out;
2539 bdrv_set_aio_context(target_bs, aio_context);
2541 if (has_bitmap) {
2542 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
2543 if (!bmap) {
2544 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
2545 goto out;
2549 backup_start(bs, target_bs, speed, sync, bmap,
2550 on_source_error, on_target_error,
2551 block_job_cb, bs, &local_err);
2552 if (local_err != NULL) {
2553 bdrv_unref(target_bs);
2554 error_propagate(errp, local_err);
2555 goto out;
2558 out:
2559 aio_context_release(aio_context);
2562 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
2564 return bdrv_named_nodes_list(errp);
2567 void qmp_blockdev_backup(const char *device, const char *target,
2568 enum MirrorSyncMode sync,
2569 bool has_speed, int64_t speed,
2570 bool has_on_source_error,
2571 BlockdevOnError on_source_error,
2572 bool has_on_target_error,
2573 BlockdevOnError on_target_error,
2574 Error **errp)
2576 BlockBackend *blk;
2577 BlockDriverState *bs;
2578 BlockDriverState *target_bs;
2579 Error *local_err = NULL;
2580 AioContext *aio_context;
2582 if (!has_speed) {
2583 speed = 0;
2585 if (!has_on_source_error) {
2586 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2588 if (!has_on_target_error) {
2589 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2592 blk = blk_by_name(device);
2593 if (!blk) {
2594 error_setg(errp, "Device '%s' not found", device);
2595 return;
2597 bs = blk_bs(blk);
2599 aio_context = bdrv_get_aio_context(bs);
2600 aio_context_acquire(aio_context);
2602 blk = blk_by_name(target);
2603 if (!blk) {
2604 error_setg(errp, "Device '%s' not found", target);
2605 goto out;
2607 target_bs = blk_bs(blk);
2609 bdrv_ref(target_bs);
2610 bdrv_set_aio_context(target_bs, aio_context);
2611 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
2612 on_target_error, block_job_cb, bs, &local_err);
2613 if (local_err != NULL) {
2614 bdrv_unref(target_bs);
2615 error_propagate(errp, local_err);
2617 out:
2618 aio_context_release(aio_context);
2621 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2623 void qmp_drive_mirror(const char *device, const char *target,
2624 bool has_format, const char *format,
2625 bool has_node_name, const char *node_name,
2626 bool has_replaces, const char *replaces,
2627 enum MirrorSyncMode sync,
2628 bool has_mode, enum NewImageMode mode,
2629 bool has_speed, int64_t speed,
2630 bool has_granularity, uint32_t granularity,
2631 bool has_buf_size, int64_t buf_size,
2632 bool has_on_source_error, BlockdevOnError on_source_error,
2633 bool has_on_target_error, BlockdevOnError on_target_error,
2634 Error **errp)
2636 BlockBackend *blk;
2637 BlockDriverState *bs;
2638 BlockDriverState *source, *target_bs;
2639 AioContext *aio_context;
2640 BlockDriver *drv = NULL;
2641 Error *local_err = NULL;
2642 QDict *options = NULL;
2643 int flags;
2644 int64_t size;
2645 int ret;
2647 if (!has_speed) {
2648 speed = 0;
2650 if (!has_on_source_error) {
2651 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2653 if (!has_on_target_error) {
2654 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2656 if (!has_mode) {
2657 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2659 if (!has_granularity) {
2660 granularity = 0;
2662 if (!has_buf_size) {
2663 buf_size = DEFAULT_MIRROR_BUF_SIZE;
2666 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
2667 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2668 "a value in range [512B, 64MB]");
2669 return;
2671 if (granularity & (granularity - 1)) {
2672 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2");
2673 return;
2676 blk = blk_by_name(device);
2677 if (!blk) {
2678 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2679 return;
2681 bs = blk_bs(blk);
2683 aio_context = bdrv_get_aio_context(bs);
2684 aio_context_acquire(aio_context);
2686 if (!bdrv_is_inserted(bs)) {
2687 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2688 goto out;
2691 if (!has_format) {
2692 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2694 if (format) {
2695 drv = bdrv_find_format(format);
2696 if (!drv) {
2697 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
2698 goto out;
2702 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
2703 goto out;
2706 flags = bs->open_flags | BDRV_O_RDWR;
2707 source = bs->backing_hd;
2708 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
2709 sync = MIRROR_SYNC_MODE_FULL;
2711 if (sync == MIRROR_SYNC_MODE_NONE) {
2712 source = bs;
2715 size = bdrv_getlength(bs);
2716 if (size < 0) {
2717 error_setg_errno(errp, -size, "bdrv_getlength failed");
2718 goto out;
2721 if (has_replaces) {
2722 BlockDriverState *to_replace_bs;
2723 AioContext *replace_aio_context;
2724 int64_t replace_size;
2726 if (!has_node_name) {
2727 error_setg(errp, "a node-name must be provided when replacing a"
2728 " named node of the graph");
2729 goto out;
2732 to_replace_bs = check_to_replace_node(replaces, &local_err);
2734 if (!to_replace_bs) {
2735 error_propagate(errp, local_err);
2736 goto out;
2739 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
2740 aio_context_acquire(replace_aio_context);
2741 replace_size = bdrv_getlength(to_replace_bs);
2742 aio_context_release(replace_aio_context);
2744 if (size != replace_size) {
2745 error_setg(errp, "cannot replace image with a mirror image of "
2746 "different size");
2747 goto out;
2751 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
2752 && mode != NEW_IMAGE_MODE_EXISTING)
2754 /* create new image w/o backing file */
2755 assert(format && drv);
2756 bdrv_img_create(target, format,
2757 NULL, NULL, NULL, size, flags, &local_err, false);
2758 } else {
2759 switch (mode) {
2760 case NEW_IMAGE_MODE_EXISTING:
2761 break;
2762 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2763 /* create new image with backing file */
2764 bdrv_img_create(target, format,
2765 source->filename,
2766 source->drv->format_name,
2767 NULL, size, flags, &local_err, false);
2768 break;
2769 default:
2770 abort();
2774 if (local_err) {
2775 error_propagate(errp, local_err);
2776 goto out;
2779 if (has_node_name) {
2780 options = qdict_new();
2781 qdict_put(options, "node-name", qstring_from_str(node_name));
2784 /* Mirroring takes care of copy-on-write using the source's backing
2785 * file.
2787 target_bs = NULL;
2788 ret = bdrv_open(&target_bs, target, NULL, options,
2789 flags | BDRV_O_NO_BACKING, drv, &local_err);
2790 if (ret < 0) {
2791 error_propagate(errp, local_err);
2792 goto out;
2795 bdrv_set_aio_context(target_bs, aio_context);
2797 /* pass the node name to replace to mirror start since it's loose coupling
2798 * and will allow to check whether the node still exist at mirror completion
2800 mirror_start(bs, target_bs,
2801 has_replaces ? replaces : NULL,
2802 speed, granularity, buf_size, sync,
2803 on_source_error, on_target_error,
2804 block_job_cb, bs, &local_err);
2805 if (local_err != NULL) {
2806 bdrv_unref(target_bs);
2807 error_propagate(errp, local_err);
2808 goto out;
2811 out:
2812 aio_context_release(aio_context);
2815 /* Get the block job for a given device name and acquire its AioContext */
2816 static BlockJob *find_block_job(const char *device, AioContext **aio_context,
2817 Error **errp)
2819 BlockBackend *blk;
2820 BlockDriverState *bs;
2822 blk = blk_by_name(device);
2823 if (!blk) {
2824 goto notfound;
2826 bs = blk_bs(blk);
2828 *aio_context = bdrv_get_aio_context(bs);
2829 aio_context_acquire(*aio_context);
2831 if (!bs->job) {
2832 aio_context_release(*aio_context);
2833 goto notfound;
2836 return bs->job;
2838 notfound:
2839 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
2840 "No active block job on device '%s'", device);
2841 *aio_context = NULL;
2842 return NULL;
2845 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2847 AioContext *aio_context;
2848 BlockJob *job = find_block_job(device, &aio_context, errp);
2850 if (!job) {
2851 return;
2854 block_job_set_speed(job, speed, errp);
2855 aio_context_release(aio_context);
2858 void qmp_block_job_cancel(const char *device,
2859 bool has_force, bool force, Error **errp)
2861 AioContext *aio_context;
2862 BlockJob *job = find_block_job(device, &aio_context, errp);
2864 if (!job) {
2865 return;
2868 if (!has_force) {
2869 force = false;
2872 if (job->user_paused && !force) {
2873 error_setg(errp, "The block job for device '%s' is currently paused",
2874 device);
2875 goto out;
2878 trace_qmp_block_job_cancel(job);
2879 block_job_cancel(job);
2880 out:
2881 aio_context_release(aio_context);
2884 void qmp_block_job_pause(const char *device, Error **errp)
2886 AioContext *aio_context;
2887 BlockJob *job = find_block_job(device, &aio_context, errp);
2889 if (!job || job->user_paused) {
2890 return;
2893 job->user_paused = true;
2894 trace_qmp_block_job_pause(job);
2895 block_job_pause(job);
2896 aio_context_release(aio_context);
2899 void qmp_block_job_resume(const char *device, Error **errp)
2901 AioContext *aio_context;
2902 BlockJob *job = find_block_job(device, &aio_context, errp);
2904 if (!job || !job->user_paused) {
2905 return;
2908 job->user_paused = false;
2909 trace_qmp_block_job_resume(job);
2910 block_job_resume(job);
2911 aio_context_release(aio_context);
2914 void qmp_block_job_complete(const char *device, Error **errp)
2916 AioContext *aio_context;
2917 BlockJob *job = find_block_job(device, &aio_context, errp);
2919 if (!job) {
2920 return;
2923 trace_qmp_block_job_complete(job);
2924 block_job_complete(job, errp);
2925 aio_context_release(aio_context);
2928 void qmp_change_backing_file(const char *device,
2929 const char *image_node_name,
2930 const char *backing_file,
2931 Error **errp)
2933 BlockBackend *blk;
2934 BlockDriverState *bs = NULL;
2935 AioContext *aio_context;
2936 BlockDriverState *image_bs = NULL;
2937 Error *local_err = NULL;
2938 bool ro;
2939 int open_flags;
2940 int ret;
2942 blk = blk_by_name(device);
2943 if (!blk) {
2944 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2945 return;
2947 bs = blk_bs(blk);
2949 aio_context = bdrv_get_aio_context(bs);
2950 aio_context_acquire(aio_context);
2952 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
2953 if (local_err) {
2954 error_propagate(errp, local_err);
2955 goto out;
2958 if (!image_bs) {
2959 error_setg(errp, "image file not found");
2960 goto out;
2963 if (bdrv_find_base(image_bs) == image_bs) {
2964 error_setg(errp, "not allowing backing file change on an image "
2965 "without a backing file");
2966 goto out;
2969 /* even though we are not necessarily operating on bs, we need it to
2970 * determine if block ops are currently prohibited on the chain */
2971 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
2972 goto out;
2975 /* final sanity check */
2976 if (!bdrv_chain_contains(bs, image_bs)) {
2977 error_setg(errp, "'%s' and image file are not in the same chain",
2978 device);
2979 goto out;
2982 /* if not r/w, reopen to make r/w */
2983 open_flags = image_bs->open_flags;
2984 ro = bdrv_is_read_only(image_bs);
2986 if (ro) {
2987 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
2988 if (local_err) {
2989 error_propagate(errp, local_err);
2990 goto out;
2994 ret = bdrv_change_backing_file(image_bs, backing_file,
2995 image_bs->drv ? image_bs->drv->format_name : "");
2997 if (ret < 0) {
2998 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
2999 backing_file);
3000 /* don't exit here, so we can try to restore open flags if
3001 * appropriate */
3004 if (ro) {
3005 bdrv_reopen(image_bs, open_flags, &local_err);
3006 if (local_err) {
3007 error_propagate(errp, local_err); /* will preserve prior errp */
3011 out:
3012 aio_context_release(aio_context);
3015 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3017 QmpOutputVisitor *ov = qmp_output_visitor_new();
3018 BlockBackend *blk;
3019 QObject *obj;
3020 QDict *qdict;
3021 Error *local_err = NULL;
3023 /* Require an ID in the top level */
3024 if (!options->has_id) {
3025 error_setg(errp, "Block device needs an ID");
3026 goto fail;
3029 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3030 * cache.direct=false instead of silently switching to aio=threads, except
3031 * when called from drive_new().
3033 * For now, simply forbidding the combination for all drivers will do. */
3034 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3035 bool direct = options->has_cache &&
3036 options->cache->has_direct &&
3037 options->cache->direct;
3038 if (!direct) {
3039 error_setg(errp, "aio=native requires cache.direct=true");
3040 goto fail;
3044 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
3045 &options, NULL, &local_err);
3046 if (local_err) {
3047 error_propagate(errp, local_err);
3048 goto fail;
3051 obj = qmp_output_get_qobject(ov);
3052 qdict = qobject_to_qdict(obj);
3054 qdict_flatten(qdict);
3056 blk = blockdev_init(NULL, qdict, &local_err);
3057 if (local_err) {
3058 error_propagate(errp, local_err);
3059 goto fail;
3062 if (bdrv_key_required(blk_bs(blk))) {
3063 blk_unref(blk);
3064 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3065 goto fail;
3068 fail:
3069 qmp_output_visitor_cleanup(ov);
3072 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3074 BlockJobInfoList *head = NULL, **p_next = &head;
3075 BlockDriverState *bs;
3077 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3078 AioContext *aio_context = bdrv_get_aio_context(bs);
3080 aio_context_acquire(aio_context);
3082 if (bs->job) {
3083 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3084 elem->value = block_job_query(bs->job);
3085 *p_next = elem;
3086 p_next = &elem->next;
3089 aio_context_release(aio_context);
3092 return head;
3095 QemuOptsList qemu_common_drive_opts = {
3096 .name = "drive",
3097 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3098 .desc = {
3100 .name = "snapshot",
3101 .type = QEMU_OPT_BOOL,
3102 .help = "enable/disable snapshot mode",
3104 .name = "discard",
3105 .type = QEMU_OPT_STRING,
3106 .help = "discard operation (ignore/off, unmap/on)",
3108 .name = "cache.writeback",
3109 .type = QEMU_OPT_BOOL,
3110 .help = "enables writeback mode for any caches",
3112 .name = "cache.direct",
3113 .type = QEMU_OPT_BOOL,
3114 .help = "enables use of O_DIRECT (bypass the host page cache)",
3116 .name = "cache.no-flush",
3117 .type = QEMU_OPT_BOOL,
3118 .help = "ignore any flush requests for the device",
3120 .name = "aio",
3121 .type = QEMU_OPT_STRING,
3122 .help = "host AIO implementation (threads, native)",
3124 .name = "format",
3125 .type = QEMU_OPT_STRING,
3126 .help = "disk format (raw, qcow2, ...)",
3128 .name = "rerror",
3129 .type = QEMU_OPT_STRING,
3130 .help = "read error action",
3132 .name = "werror",
3133 .type = QEMU_OPT_STRING,
3134 .help = "write error action",
3136 .name = "read-only",
3137 .type = QEMU_OPT_BOOL,
3138 .help = "open drive file as read-only",
3140 .name = "throttling.iops-total",
3141 .type = QEMU_OPT_NUMBER,
3142 .help = "limit total I/O operations per second",
3144 .name = "throttling.iops-read",
3145 .type = QEMU_OPT_NUMBER,
3146 .help = "limit read operations per second",
3148 .name = "throttling.iops-write",
3149 .type = QEMU_OPT_NUMBER,
3150 .help = "limit write operations per second",
3152 .name = "throttling.bps-total",
3153 .type = QEMU_OPT_NUMBER,
3154 .help = "limit total bytes per second",
3156 .name = "throttling.bps-read",
3157 .type = QEMU_OPT_NUMBER,
3158 .help = "limit read bytes per second",
3160 .name = "throttling.bps-write",
3161 .type = QEMU_OPT_NUMBER,
3162 .help = "limit write bytes per second",
3164 .name = "throttling.iops-total-max",
3165 .type = QEMU_OPT_NUMBER,
3166 .help = "I/O operations burst",
3168 .name = "throttling.iops-read-max",
3169 .type = QEMU_OPT_NUMBER,
3170 .help = "I/O operations read burst",
3172 .name = "throttling.iops-write-max",
3173 .type = QEMU_OPT_NUMBER,
3174 .help = "I/O operations write burst",
3176 .name = "throttling.bps-total-max",
3177 .type = QEMU_OPT_NUMBER,
3178 .help = "total bytes burst",
3180 .name = "throttling.bps-read-max",
3181 .type = QEMU_OPT_NUMBER,
3182 .help = "total bytes read burst",
3184 .name = "throttling.bps-write-max",
3185 .type = QEMU_OPT_NUMBER,
3186 .help = "total bytes write burst",
3188 .name = "throttling.iops-size",
3189 .type = QEMU_OPT_NUMBER,
3190 .help = "when limiting by iops max size of an I/O in bytes",
3192 .name = "copy-on-read",
3193 .type = QEMU_OPT_BOOL,
3194 .help = "copy read data from backing file into image file",
3196 .name = "detect-zeroes",
3197 .type = QEMU_OPT_STRING,
3198 .help = "try to optimize zero writes (off, on, unmap)",
3200 { /* end of list */ }
3204 QemuOptsList qemu_drive_opts = {
3205 .name = "drive",
3206 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3207 .desc = {
3209 * no elements => accept any params
3210 * validation will happen later
3212 { /* end of list */ }