target-arm: add SDER definition
[qemu-kvm.git] / blockdev.c
blob57910b82c7adc3ce59173afeeebcd37ff2a3dfd0
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;
183 char buf[32];
185 opts = drive_def(optstr);
186 if (!opts) {
187 return NULL;
189 if (type != IF_DEFAULT) {
190 qemu_opt_set(opts, "if", if_name[type]);
192 if (index >= 0) {
193 snprintf(buf, sizeof(buf), "%d", index);
194 qemu_opt_set(opts, "index", buf);
196 if (file)
197 qemu_opt_set(opts, "file", file);
198 return opts;
201 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
203 BlockBackend *blk;
204 DriveInfo *dinfo;
206 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
207 dinfo = blk_legacy_dinfo(blk);
208 if (dinfo && dinfo->type == type
209 && dinfo->bus == bus && dinfo->unit == unit) {
210 return dinfo;
214 return NULL;
217 bool drive_check_orphaned(void)
219 BlockBackend *blk;
220 DriveInfo *dinfo;
221 bool rs = false;
223 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
224 dinfo = blk_legacy_dinfo(blk);
225 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
226 /* Unless this is a default drive, this may be an oversight. */
227 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
228 dinfo->type != IF_NONE) {
229 fprintf(stderr, "Warning: Orphaned drive without device: "
230 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
231 blk_name(blk), blk_bs(blk)->filename, if_name[dinfo->type],
232 dinfo->bus, dinfo->unit);
233 rs = true;
237 return rs;
240 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
242 return drive_get(type,
243 drive_index_to_bus_id(type, index),
244 drive_index_to_unit_id(type, index));
247 int drive_get_max_bus(BlockInterfaceType type)
249 int max_bus;
250 BlockBackend *blk;
251 DriveInfo *dinfo;
253 max_bus = -1;
254 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
255 dinfo = blk_legacy_dinfo(blk);
256 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
257 max_bus = dinfo->bus;
260 return max_bus;
263 /* Get a block device. This should only be used for single-drive devices
264 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
265 appropriate bus. */
266 DriveInfo *drive_get_next(BlockInterfaceType type)
268 static int next_block_unit[IF_COUNT];
270 return drive_get(type, 0, next_block_unit[type]++);
273 static void bdrv_format_print(void *opaque, const char *name)
275 error_printf(" %s", name);
278 typedef struct {
279 QEMUBH *bh;
280 BlockDriverState *bs;
281 } BDRVPutRefBH;
283 static void bdrv_put_ref_bh(void *opaque)
285 BDRVPutRefBH *s = opaque;
287 bdrv_unref(s->bs);
288 qemu_bh_delete(s->bh);
289 g_free(s);
293 * Release a BDS reference in a BH
295 * It is not safe to use bdrv_unref() from a callback function when the callers
296 * still need the BlockDriverState. In such cases we schedule a BH to release
297 * the reference.
299 static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
301 BDRVPutRefBH *s;
303 s = g_new(BDRVPutRefBH, 1);
304 s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
305 s->bs = bs;
306 qemu_bh_schedule(s->bh);
309 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
311 if (!strcmp(buf, "ignore")) {
312 return BLOCKDEV_ON_ERROR_IGNORE;
313 } else if (!is_read && !strcmp(buf, "enospc")) {
314 return BLOCKDEV_ON_ERROR_ENOSPC;
315 } else if (!strcmp(buf, "stop")) {
316 return BLOCKDEV_ON_ERROR_STOP;
317 } else if (!strcmp(buf, "report")) {
318 return BLOCKDEV_ON_ERROR_REPORT;
319 } else {
320 error_setg(errp, "'%s' invalid %s error action",
321 buf, is_read ? "read" : "write");
322 return -1;
326 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
328 if (throttle_conflicting(cfg)) {
329 error_setg(errp, "bps/iops/max total values and read/write values"
330 " cannot be used at the same time");
331 return false;
334 if (!throttle_is_valid(cfg)) {
335 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
336 return false;
339 return true;
342 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
344 /* Takes the ownership of bs_opts */
345 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
346 Error **errp)
348 const char *buf;
349 int ro = 0;
350 int bdrv_flags = 0;
351 int on_read_error, on_write_error;
352 BlockBackend *blk;
353 BlockDriverState *bs;
354 ThrottleConfig cfg;
355 int snapshot = 0;
356 bool copy_on_read;
357 int ret;
358 Error *error = NULL;
359 QemuOpts *opts;
360 const char *id;
361 bool has_driver_specific_opts;
362 BlockdevDetectZeroesOptions detect_zeroes;
363 BlockDriver *drv = NULL;
365 /* Check common options by copying from bs_opts to opts, all other options
366 * stay in bs_opts for processing by bdrv_open(). */
367 id = qdict_get_try_str(bs_opts, "id");
368 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
369 if (error) {
370 error_propagate(errp, error);
371 goto err_no_opts;
374 qemu_opts_absorb_qdict(opts, bs_opts, &error);
375 if (error) {
376 error_propagate(errp, error);
377 goto early_err;
380 if (id) {
381 qdict_del(bs_opts, "id");
384 has_driver_specific_opts = !!qdict_size(bs_opts);
386 /* extract parameters */
387 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
388 ro = qemu_opt_get_bool(opts, "read-only", 0);
389 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
391 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
392 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
393 error_setg(errp, "invalid discard option");
394 goto early_err;
398 if (qemu_opt_get_bool(opts, "cache.writeback", true)) {
399 bdrv_flags |= BDRV_O_CACHE_WB;
401 if (qemu_opt_get_bool(opts, "cache.direct", false)) {
402 bdrv_flags |= BDRV_O_NOCACHE;
404 if (qemu_opt_get_bool(opts, "cache.no-flush", false)) {
405 bdrv_flags |= BDRV_O_NO_FLUSH;
408 #ifdef CONFIG_LINUX_AIO
409 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
410 if (!strcmp(buf, "native")) {
411 bdrv_flags |= BDRV_O_NATIVE_AIO;
412 } else if (!strcmp(buf, "threads")) {
413 /* this is the default */
414 } else {
415 error_setg(errp, "invalid aio option");
416 goto early_err;
419 #endif
421 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
422 if (is_help_option(buf)) {
423 error_printf("Supported formats:");
424 bdrv_iterate_format(bdrv_format_print, NULL);
425 error_printf("\n");
426 goto early_err;
429 drv = bdrv_find_format(buf);
430 if (!drv) {
431 error_setg(errp, "'%s' invalid format", buf);
432 goto early_err;
436 /* disk I/O throttling */
437 memset(&cfg, 0, sizeof(cfg));
438 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
439 qemu_opt_get_number(opts, "throttling.bps-total", 0);
440 cfg.buckets[THROTTLE_BPS_READ].avg =
441 qemu_opt_get_number(opts, "throttling.bps-read", 0);
442 cfg.buckets[THROTTLE_BPS_WRITE].avg =
443 qemu_opt_get_number(opts, "throttling.bps-write", 0);
444 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
445 qemu_opt_get_number(opts, "throttling.iops-total", 0);
446 cfg.buckets[THROTTLE_OPS_READ].avg =
447 qemu_opt_get_number(opts, "throttling.iops-read", 0);
448 cfg.buckets[THROTTLE_OPS_WRITE].avg =
449 qemu_opt_get_number(opts, "throttling.iops-write", 0);
451 cfg.buckets[THROTTLE_BPS_TOTAL].max =
452 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
453 cfg.buckets[THROTTLE_BPS_READ].max =
454 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
455 cfg.buckets[THROTTLE_BPS_WRITE].max =
456 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
457 cfg.buckets[THROTTLE_OPS_TOTAL].max =
458 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
459 cfg.buckets[THROTTLE_OPS_READ].max =
460 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
461 cfg.buckets[THROTTLE_OPS_WRITE].max =
462 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
464 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
466 if (!check_throttle_config(&cfg, &error)) {
467 error_propagate(errp, error);
468 goto early_err;
471 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
472 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
473 on_write_error = parse_block_error_action(buf, 0, &error);
474 if (error) {
475 error_propagate(errp, error);
476 goto early_err;
480 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
481 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
482 on_read_error = parse_block_error_action(buf, 1, &error);
483 if (error) {
484 error_propagate(errp, error);
485 goto early_err;
489 detect_zeroes =
490 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
491 qemu_opt_get(opts, "detect-zeroes"),
492 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
493 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
494 &error);
495 if (error) {
496 error_propagate(errp, error);
497 goto early_err;
500 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
501 !(bdrv_flags & BDRV_O_UNMAP)) {
502 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
503 "without setting discard operation to unmap");
504 goto early_err;
507 /* init */
508 blk = blk_new_with_bs(qemu_opts_id(opts), errp);
509 if (!blk) {
510 goto early_err;
512 bs = blk_bs(blk);
513 bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
514 bs->read_only = ro;
515 bs->detect_zeroes = detect_zeroes;
517 bdrv_set_on_error(bs, on_read_error, on_write_error);
519 /* disk I/O throttling */
520 if (throttle_enabled(&cfg)) {
521 bdrv_io_limits_enable(bs);
522 bdrv_set_io_limits(bs, &cfg);
525 if (!file || !*file) {
526 if (has_driver_specific_opts) {
527 file = NULL;
528 } else {
529 QDECREF(bs_opts);
530 qemu_opts_del(opts);
531 return blk;
534 if (snapshot) {
535 /* always use cache=unsafe with snapshot */
536 bdrv_flags &= ~BDRV_O_CACHE_MASK;
537 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
540 if (copy_on_read) {
541 bdrv_flags |= BDRV_O_COPY_ON_READ;
544 if (runstate_check(RUN_STATE_INMIGRATE)) {
545 bdrv_flags |= BDRV_O_INCOMING;
548 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
550 QINCREF(bs_opts);
551 ret = bdrv_open(&bs, file, NULL, bs_opts, bdrv_flags, drv, &error);
552 assert(bs == blk_bs(blk));
554 if (ret < 0) {
555 error_setg(errp, "could not open disk image %s: %s",
556 file ?: blk_name(blk), error_get_pretty(error));
557 error_free(error);
558 goto err;
561 if (bdrv_key_required(bs)) {
562 autostart = 0;
565 QDECREF(bs_opts);
566 qemu_opts_del(opts);
568 return blk;
570 err:
571 blk_unref(blk);
572 early_err:
573 qemu_opts_del(opts);
574 err_no_opts:
575 QDECREF(bs_opts);
576 return NULL;
579 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
580 Error **errp)
582 const char *value;
584 value = qemu_opt_get(opts, from);
585 if (value) {
586 if (qemu_opt_find(opts, to)) {
587 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
588 "same time", to, from);
589 return;
593 /* rename all items in opts */
594 while ((value = qemu_opt_get(opts, from))) {
595 qemu_opt_set(opts, to, value);
596 qemu_opt_unset(opts, from);
600 QemuOptsList qemu_legacy_drive_opts = {
601 .name = "drive",
602 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
603 .desc = {
605 .name = "bus",
606 .type = QEMU_OPT_NUMBER,
607 .help = "bus number",
609 .name = "unit",
610 .type = QEMU_OPT_NUMBER,
611 .help = "unit number (i.e. lun for scsi)",
613 .name = "index",
614 .type = QEMU_OPT_NUMBER,
615 .help = "index number",
617 .name = "media",
618 .type = QEMU_OPT_STRING,
619 .help = "media type (disk, cdrom)",
621 .name = "if",
622 .type = QEMU_OPT_STRING,
623 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
625 .name = "cyls",
626 .type = QEMU_OPT_NUMBER,
627 .help = "number of cylinders (ide disk geometry)",
629 .name = "heads",
630 .type = QEMU_OPT_NUMBER,
631 .help = "number of heads (ide disk geometry)",
633 .name = "secs",
634 .type = QEMU_OPT_NUMBER,
635 .help = "number of sectors (ide disk geometry)",
637 .name = "trans",
638 .type = QEMU_OPT_STRING,
639 .help = "chs translation (auto, lba, none)",
641 .name = "boot",
642 .type = QEMU_OPT_BOOL,
643 .help = "(deprecated, ignored)",
645 .name = "addr",
646 .type = QEMU_OPT_STRING,
647 .help = "pci address (virtio only)",
649 .name = "serial",
650 .type = QEMU_OPT_STRING,
651 .help = "disk serial number",
653 .name = "file",
654 .type = QEMU_OPT_STRING,
655 .help = "file name",
658 /* Options that are passed on, but have special semantics with -drive */
660 .name = "read-only",
661 .type = QEMU_OPT_BOOL,
662 .help = "open drive file as read-only",
664 .name = "rerror",
665 .type = QEMU_OPT_STRING,
666 .help = "read error action",
668 .name = "werror",
669 .type = QEMU_OPT_STRING,
670 .help = "write error action",
672 .name = "copy-on-read",
673 .type = QEMU_OPT_BOOL,
674 .help = "copy read data from backing file into image file",
677 { /* end of list */ }
681 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
683 const char *value;
684 BlockBackend *blk;
685 DriveInfo *dinfo = NULL;
686 QDict *bs_opts;
687 QemuOpts *legacy_opts;
688 DriveMediaType media = MEDIA_DISK;
689 BlockInterfaceType type;
690 int cyls, heads, secs, translation;
691 int max_devs, bus_id, unit_id, index;
692 const char *devaddr;
693 const char *werror, *rerror;
694 bool read_only = false;
695 bool copy_on_read;
696 const char *serial;
697 const char *filename;
698 Error *local_err = NULL;
699 int i;
701 /* Change legacy command line options into QMP ones */
702 static const struct {
703 const char *from;
704 const char *to;
705 } opt_renames[] = {
706 { "iops", "throttling.iops-total" },
707 { "iops_rd", "throttling.iops-read" },
708 { "iops_wr", "throttling.iops-write" },
710 { "bps", "throttling.bps-total" },
711 { "bps_rd", "throttling.bps-read" },
712 { "bps_wr", "throttling.bps-write" },
714 { "iops_max", "throttling.iops-total-max" },
715 { "iops_rd_max", "throttling.iops-read-max" },
716 { "iops_wr_max", "throttling.iops-write-max" },
718 { "bps_max", "throttling.bps-total-max" },
719 { "bps_rd_max", "throttling.bps-read-max" },
720 { "bps_wr_max", "throttling.bps-write-max" },
722 { "iops_size", "throttling.iops-size" },
724 { "readonly", "read-only" },
727 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
728 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
729 &local_err);
730 if (local_err) {
731 error_report("%s", error_get_pretty(local_err));
732 error_free(local_err);
733 return NULL;
737 value = qemu_opt_get(all_opts, "cache");
738 if (value) {
739 int flags = 0;
741 if (bdrv_parse_cache_flags(value, &flags) != 0) {
742 error_report("invalid cache option");
743 return NULL;
746 /* Specific options take precedence */
747 if (!qemu_opt_get(all_opts, "cache.writeback")) {
748 qemu_opt_set_bool(all_opts, "cache.writeback",
749 !!(flags & BDRV_O_CACHE_WB));
751 if (!qemu_opt_get(all_opts, "cache.direct")) {
752 qemu_opt_set_bool(all_opts, "cache.direct",
753 !!(flags & BDRV_O_NOCACHE));
755 if (!qemu_opt_get(all_opts, "cache.no-flush")) {
756 qemu_opt_set_bool(all_opts, "cache.no-flush",
757 !!(flags & BDRV_O_NO_FLUSH));
759 qemu_opt_unset(all_opts, "cache");
762 /* Get a QDict for processing the options */
763 bs_opts = qdict_new();
764 qemu_opts_to_qdict(all_opts, bs_opts);
766 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
767 &error_abort);
768 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
769 if (local_err) {
770 error_report("%s", error_get_pretty(local_err));
771 error_free(local_err);
772 goto fail;
775 /* Deprecated option boot=[on|off] */
776 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
777 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
778 "ignored. Future versions will reject this parameter. Please "
779 "update your scripts.\n");
782 /* Media type */
783 value = qemu_opt_get(legacy_opts, "media");
784 if (value) {
785 if (!strcmp(value, "disk")) {
786 media = MEDIA_DISK;
787 } else if (!strcmp(value, "cdrom")) {
788 media = MEDIA_CDROM;
789 read_only = true;
790 } else {
791 error_report("'%s' invalid media", value);
792 goto fail;
796 /* copy-on-read is disabled with a warning for read-only devices */
797 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
798 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
800 if (read_only && copy_on_read) {
801 error_report("warning: disabling copy-on-read on read-only drive");
802 copy_on_read = false;
805 qdict_put(bs_opts, "read-only",
806 qstring_from_str(read_only ? "on" : "off"));
807 qdict_put(bs_opts, "copy-on-read",
808 qstring_from_str(copy_on_read ? "on" :"off"));
810 /* Controller type */
811 value = qemu_opt_get(legacy_opts, "if");
812 if (value) {
813 for (type = 0;
814 type < IF_COUNT && strcmp(value, if_name[type]);
815 type++) {
817 if (type == IF_COUNT) {
818 error_report("unsupported bus type '%s'", value);
819 goto fail;
821 } else {
822 type = block_default_type;
825 /* Geometry */
826 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
827 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
828 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
830 if (cyls || heads || secs) {
831 if (cyls < 1) {
832 error_report("invalid physical cyls number");
833 goto fail;
835 if (heads < 1) {
836 error_report("invalid physical heads number");
837 goto fail;
839 if (secs < 1) {
840 error_report("invalid physical secs number");
841 goto fail;
845 translation = BIOS_ATA_TRANSLATION_AUTO;
846 value = qemu_opt_get(legacy_opts, "trans");
847 if (value != NULL) {
848 if (!cyls) {
849 error_report("'%s' trans must be used with cyls, heads and secs",
850 value);
851 goto fail;
853 if (!strcmp(value, "none")) {
854 translation = BIOS_ATA_TRANSLATION_NONE;
855 } else if (!strcmp(value, "lba")) {
856 translation = BIOS_ATA_TRANSLATION_LBA;
857 } else if (!strcmp(value, "large")) {
858 translation = BIOS_ATA_TRANSLATION_LARGE;
859 } else if (!strcmp(value, "rechs")) {
860 translation = BIOS_ATA_TRANSLATION_RECHS;
861 } else if (!strcmp(value, "auto")) {
862 translation = BIOS_ATA_TRANSLATION_AUTO;
863 } else {
864 error_report("'%s' invalid translation type", value);
865 goto fail;
869 if (media == MEDIA_CDROM) {
870 if (cyls || secs || heads) {
871 error_report("CHS can't be set with media=cdrom");
872 goto fail;
876 /* Device address specified by bus/unit or index.
877 * If none was specified, try to find the first free one. */
878 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
879 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
880 index = qemu_opt_get_number(legacy_opts, "index", -1);
882 max_devs = if_max_devs[type];
884 if (index != -1) {
885 if (bus_id != 0 || unit_id != -1) {
886 error_report("index cannot be used with bus and unit");
887 goto fail;
889 bus_id = drive_index_to_bus_id(type, index);
890 unit_id = drive_index_to_unit_id(type, index);
893 if (unit_id == -1) {
894 unit_id = 0;
895 while (drive_get(type, bus_id, unit_id) != NULL) {
896 unit_id++;
897 if (max_devs && unit_id >= max_devs) {
898 unit_id -= max_devs;
899 bus_id++;
904 if (max_devs && unit_id >= max_devs) {
905 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
906 goto fail;
909 if (drive_get(type, bus_id, unit_id) != NULL) {
910 error_report("drive with bus=%d, unit=%d (index=%d) exists",
911 bus_id, unit_id, index);
912 goto fail;
915 /* Serial number */
916 serial = qemu_opt_get(legacy_opts, "serial");
918 /* no id supplied -> create one */
919 if (qemu_opts_id(all_opts) == NULL) {
920 char *new_id;
921 const char *mediastr = "";
922 if (type == IF_IDE || type == IF_SCSI) {
923 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
925 if (max_devs) {
926 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
927 mediastr, unit_id);
928 } else {
929 new_id = g_strdup_printf("%s%s%i", if_name[type],
930 mediastr, unit_id);
932 qdict_put(bs_opts, "id", qstring_from_str(new_id));
933 g_free(new_id);
936 /* Add virtio block device */
937 devaddr = qemu_opt_get(legacy_opts, "addr");
938 if (devaddr && type != IF_VIRTIO) {
939 error_report("addr is not supported by this bus type");
940 goto fail;
943 if (type == IF_VIRTIO) {
944 QemuOpts *devopts;
945 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
946 &error_abort);
947 if (arch_type == QEMU_ARCH_S390X) {
948 qemu_opt_set(devopts, "driver", "virtio-blk-s390");
949 } else {
950 qemu_opt_set(devopts, "driver", "virtio-blk-pci");
952 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"));
953 if (devaddr) {
954 qemu_opt_set(devopts, "addr", devaddr);
958 filename = qemu_opt_get(legacy_opts, "file");
960 /* Check werror/rerror compatibility with if=... */
961 werror = qemu_opt_get(legacy_opts, "werror");
962 if (werror != NULL) {
963 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
964 type != IF_NONE) {
965 error_report("werror is not supported by this bus type");
966 goto fail;
968 qdict_put(bs_opts, "werror", qstring_from_str(werror));
971 rerror = qemu_opt_get(legacy_opts, "rerror");
972 if (rerror != NULL) {
973 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
974 type != IF_NONE) {
975 error_report("rerror is not supported by this bus type");
976 goto fail;
978 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
981 /* Actual block device init: Functionality shared with blockdev-add */
982 blk = blockdev_init(filename, bs_opts, &local_err);
983 bs_opts = NULL;
984 if (!blk) {
985 if (local_err) {
986 error_report("%s", error_get_pretty(local_err));
987 error_free(local_err);
989 goto fail;
990 } else {
991 assert(!local_err);
994 /* Create legacy DriveInfo */
995 dinfo = g_malloc0(sizeof(*dinfo));
996 dinfo->opts = all_opts;
998 dinfo->cyls = cyls;
999 dinfo->heads = heads;
1000 dinfo->secs = secs;
1001 dinfo->trans = translation;
1003 dinfo->type = type;
1004 dinfo->bus = bus_id;
1005 dinfo->unit = unit_id;
1006 dinfo->devaddr = devaddr;
1007 dinfo->serial = g_strdup(serial);
1009 blk_set_legacy_dinfo(blk, dinfo);
1011 switch(type) {
1012 case IF_IDE:
1013 case IF_SCSI:
1014 case IF_XEN:
1015 case IF_NONE:
1016 dinfo->media_cd = media == MEDIA_CDROM;
1017 break;
1018 default:
1019 break;
1022 fail:
1023 qemu_opts_del(legacy_opts);
1024 QDECREF(bs_opts);
1025 return dinfo;
1028 void do_commit(Monitor *mon, const QDict *qdict)
1030 const char *device = qdict_get_str(qdict, "device");
1031 BlockDriverState *bs;
1032 int ret;
1034 if (!strcmp(device, "all")) {
1035 ret = bdrv_commit_all();
1036 } else {
1037 bs = bdrv_find(device);
1038 if (!bs) {
1039 monitor_printf(mon, "Device '%s' not found\n", device);
1040 return;
1042 ret = bdrv_commit(bs);
1044 if (ret < 0) {
1045 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1046 strerror(-ret));
1050 static void blockdev_do_action(int kind, void *data, Error **errp)
1052 TransactionAction action;
1053 TransactionActionList list;
1055 action.kind = kind;
1056 action.data = data;
1057 list.value = &action;
1058 list.next = NULL;
1059 qmp_transaction(&list, errp);
1062 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1063 bool has_node_name, const char *node_name,
1064 const char *snapshot_file,
1065 bool has_snapshot_node_name,
1066 const char *snapshot_node_name,
1067 bool has_format, const char *format,
1068 bool has_mode, NewImageMode mode, Error **errp)
1070 BlockdevSnapshot snapshot = {
1071 .has_device = has_device,
1072 .device = (char *) device,
1073 .has_node_name = has_node_name,
1074 .node_name = (char *) node_name,
1075 .snapshot_file = (char *) snapshot_file,
1076 .has_snapshot_node_name = has_snapshot_node_name,
1077 .snapshot_node_name = (char *) snapshot_node_name,
1078 .has_format = has_format,
1079 .format = (char *) format,
1080 .has_mode = has_mode,
1081 .mode = mode,
1083 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1084 &snapshot, errp);
1087 void qmp_blockdev_snapshot_internal_sync(const char *device,
1088 const char *name,
1089 Error **errp)
1091 BlockdevSnapshotInternal snapshot = {
1092 .device = (char *) device,
1093 .name = (char *) name
1096 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1097 &snapshot, errp);
1100 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1101 bool has_id,
1102 const char *id,
1103 bool has_name,
1104 const char *name,
1105 Error **errp)
1107 BlockDriverState *bs = bdrv_find(device);
1108 QEMUSnapshotInfo sn;
1109 Error *local_err = NULL;
1110 SnapshotInfo *info = NULL;
1111 int ret;
1113 if (!bs) {
1114 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1115 return NULL;
1118 if (!has_id) {
1119 id = NULL;
1122 if (!has_name) {
1123 name = NULL;
1126 if (!id && !name) {
1127 error_setg(errp, "Name or id must be provided");
1128 return NULL;
1131 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1132 if (local_err) {
1133 error_propagate(errp, local_err);
1134 return NULL;
1136 if (!ret) {
1137 error_setg(errp,
1138 "Snapshot with id '%s' and name '%s' does not exist on "
1139 "device '%s'",
1140 STR_OR_NULL(id), STR_OR_NULL(name), device);
1141 return NULL;
1144 bdrv_snapshot_delete(bs, id, name, &local_err);
1145 if (local_err) {
1146 error_propagate(errp, local_err);
1147 return NULL;
1150 info = g_new0(SnapshotInfo, 1);
1151 info->id = g_strdup(sn.id_str);
1152 info->name = g_strdup(sn.name);
1153 info->date_nsec = sn.date_nsec;
1154 info->date_sec = sn.date_sec;
1155 info->vm_state_size = sn.vm_state_size;
1156 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1157 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1159 return info;
1162 /* New and old BlockDriverState structs for group snapshots */
1164 typedef struct BlkTransactionState BlkTransactionState;
1166 /* Only prepare() may fail. In a single transaction, only one of commit() or
1167 abort() will be called, clean() will always be called if it present. */
1168 typedef struct BdrvActionOps {
1169 /* Size of state struct, in bytes. */
1170 size_t instance_size;
1171 /* Prepare the work, must NOT be NULL. */
1172 void (*prepare)(BlkTransactionState *common, Error **errp);
1173 /* Commit the changes, can be NULL. */
1174 void (*commit)(BlkTransactionState *common);
1175 /* Abort the changes on fail, can be NULL. */
1176 void (*abort)(BlkTransactionState *common);
1177 /* Clean up resource in the end, can be NULL. */
1178 void (*clean)(BlkTransactionState *common);
1179 } BdrvActionOps;
1182 * This structure must be arranged as first member in child type, assuming
1183 * that compiler will also arrange it to the same address with parent instance.
1184 * Later it will be used in free().
1186 struct BlkTransactionState {
1187 TransactionAction *action;
1188 const BdrvActionOps *ops;
1189 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
1192 /* internal snapshot private data */
1193 typedef struct InternalSnapshotState {
1194 BlkTransactionState common;
1195 BlockDriverState *bs;
1196 QEMUSnapshotInfo sn;
1197 } InternalSnapshotState;
1199 static void internal_snapshot_prepare(BlkTransactionState *common,
1200 Error **errp)
1202 Error *local_err = NULL;
1203 const char *device;
1204 const char *name;
1205 BlockDriverState *bs;
1206 QEMUSnapshotInfo old_sn, *sn;
1207 bool ret;
1208 qemu_timeval tv;
1209 BlockdevSnapshotInternal *internal;
1210 InternalSnapshotState *state;
1211 int ret1;
1213 g_assert(common->action->kind ==
1214 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1215 internal = common->action->blockdev_snapshot_internal_sync;
1216 state = DO_UPCAST(InternalSnapshotState, common, common);
1218 /* 1. parse input */
1219 device = internal->device;
1220 name = internal->name;
1222 /* 2. check for validation */
1223 bs = bdrv_find(device);
1224 if (!bs) {
1225 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1226 return;
1229 if (!bdrv_is_inserted(bs)) {
1230 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1231 return;
1234 if (bdrv_is_read_only(bs)) {
1235 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1236 return;
1239 if (!bdrv_can_snapshot(bs)) {
1240 error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
1241 bs->drv->format_name, device, "internal snapshot");
1242 return;
1245 if (!strlen(name)) {
1246 error_setg(errp, "Name is empty");
1247 return;
1250 /* check whether a snapshot with name exist */
1251 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1252 &local_err);
1253 if (local_err) {
1254 error_propagate(errp, local_err);
1255 return;
1256 } else if (ret) {
1257 error_setg(errp,
1258 "Snapshot with name '%s' already exists on device '%s'",
1259 name, device);
1260 return;
1263 /* 3. take the snapshot */
1264 sn = &state->sn;
1265 pstrcpy(sn->name, sizeof(sn->name), name);
1266 qemu_gettimeofday(&tv);
1267 sn->date_sec = tv.tv_sec;
1268 sn->date_nsec = tv.tv_usec * 1000;
1269 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1271 ret1 = bdrv_snapshot_create(bs, sn);
1272 if (ret1 < 0) {
1273 error_setg_errno(errp, -ret1,
1274 "Failed to create snapshot '%s' on device '%s'",
1275 name, device);
1276 return;
1279 /* 4. succeed, mark a snapshot is created */
1280 state->bs = bs;
1283 static void internal_snapshot_abort(BlkTransactionState *common)
1285 InternalSnapshotState *state =
1286 DO_UPCAST(InternalSnapshotState, common, common);
1287 BlockDriverState *bs = state->bs;
1288 QEMUSnapshotInfo *sn = &state->sn;
1289 Error *local_error = NULL;
1291 if (!bs) {
1292 return;
1295 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1296 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1297 "device '%s' in abort: %s",
1298 sn->id_str,
1299 sn->name,
1300 bdrv_get_device_name(bs),
1301 error_get_pretty(local_error));
1302 error_free(local_error);
1306 /* external snapshot private data */
1307 typedef struct ExternalSnapshotState {
1308 BlkTransactionState common;
1309 BlockDriverState *old_bs;
1310 BlockDriverState *new_bs;
1311 } ExternalSnapshotState;
1313 static void external_snapshot_prepare(BlkTransactionState *common,
1314 Error **errp)
1316 BlockDriver *drv;
1317 int flags, ret;
1318 QDict *options = NULL;
1319 Error *local_err = NULL;
1320 bool has_device = false;
1321 const char *device;
1322 bool has_node_name = false;
1323 const char *node_name;
1324 bool has_snapshot_node_name = false;
1325 const char *snapshot_node_name;
1326 const char *new_image_file;
1327 const char *format = "qcow2";
1328 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1329 ExternalSnapshotState *state =
1330 DO_UPCAST(ExternalSnapshotState, common, common);
1331 TransactionAction *action = common->action;
1333 /* get parameters */
1334 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1336 has_device = action->blockdev_snapshot_sync->has_device;
1337 device = action->blockdev_snapshot_sync->device;
1338 has_node_name = action->blockdev_snapshot_sync->has_node_name;
1339 node_name = action->blockdev_snapshot_sync->node_name;
1340 has_snapshot_node_name =
1341 action->blockdev_snapshot_sync->has_snapshot_node_name;
1342 snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name;
1344 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1345 if (action->blockdev_snapshot_sync->has_format) {
1346 format = action->blockdev_snapshot_sync->format;
1348 if (action->blockdev_snapshot_sync->has_mode) {
1349 mode = action->blockdev_snapshot_sync->mode;
1352 /* start processing */
1353 drv = bdrv_find_format(format);
1354 if (!drv) {
1355 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1356 return;
1359 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
1360 has_node_name ? node_name : NULL,
1361 &local_err);
1362 if (local_err) {
1363 error_propagate(errp, local_err);
1364 return;
1367 if (has_node_name && !has_snapshot_node_name) {
1368 error_setg(errp, "New snapshot node name missing");
1369 return;
1372 if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) {
1373 error_setg(errp, "New snapshot node name already existing");
1374 return;
1377 if (!bdrv_is_inserted(state->old_bs)) {
1378 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1379 return;
1382 if (bdrv_op_is_blocked(state->old_bs,
1383 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1384 return;
1387 if (!bdrv_is_read_only(state->old_bs)) {
1388 if (bdrv_flush(state->old_bs)) {
1389 error_set(errp, QERR_IO_ERROR);
1390 return;
1394 if (!bdrv_is_first_non_filter(state->old_bs)) {
1395 error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
1396 return;
1399 flags = state->old_bs->open_flags;
1401 /* create new image w/backing file */
1402 if (mode != NEW_IMAGE_MODE_EXISTING) {
1403 bdrv_img_create(new_image_file, format,
1404 state->old_bs->filename,
1405 state->old_bs->drv->format_name,
1406 NULL, -1, flags, &local_err, false);
1407 if (local_err) {
1408 error_propagate(errp, local_err);
1409 return;
1413 if (has_snapshot_node_name) {
1414 options = qdict_new();
1415 qdict_put(options, "node-name",
1416 qstring_from_str(snapshot_node_name));
1419 /* TODO Inherit bs->options or only take explicit options with an
1420 * extended QMP command? */
1421 assert(state->new_bs == NULL);
1422 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
1423 flags | BDRV_O_NO_BACKING, drv, &local_err);
1424 /* We will manually add the backing_hd field to the bs later */
1425 if (ret != 0) {
1426 error_propagate(errp, local_err);
1430 static void external_snapshot_commit(BlkTransactionState *common)
1432 ExternalSnapshotState *state =
1433 DO_UPCAST(ExternalSnapshotState, common, common);
1435 /* This removes our old bs and adds the new bs */
1436 bdrv_append(state->new_bs, state->old_bs);
1437 /* We don't need (or want) to use the transactional
1438 * bdrv_reopen_multiple() across all the entries at once, because we
1439 * don't want to abort all of them if one of them fails the reopen */
1440 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
1441 NULL);
1444 static void external_snapshot_abort(BlkTransactionState *common)
1446 ExternalSnapshotState *state =
1447 DO_UPCAST(ExternalSnapshotState, common, common);
1448 if (state->new_bs) {
1449 bdrv_unref(state->new_bs);
1453 typedef struct DriveBackupState {
1454 BlkTransactionState common;
1455 BlockDriverState *bs;
1456 BlockJob *job;
1457 } DriveBackupState;
1459 static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1461 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1462 DriveBackup *backup;
1463 Error *local_err = NULL;
1465 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1466 backup = common->action->drive_backup;
1468 qmp_drive_backup(backup->device, backup->target,
1469 backup->has_format, backup->format,
1470 backup->sync,
1471 backup->has_mode, backup->mode,
1472 backup->has_speed, backup->speed,
1473 backup->has_on_source_error, backup->on_source_error,
1474 backup->has_on_target_error, backup->on_target_error,
1475 &local_err);
1476 if (local_err) {
1477 error_propagate(errp, local_err);
1478 state->bs = NULL;
1479 state->job = NULL;
1480 return;
1483 state->bs = bdrv_find(backup->device);
1484 state->job = state->bs->job;
1487 static void drive_backup_abort(BlkTransactionState *common)
1489 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1490 BlockDriverState *bs = state->bs;
1492 /* Only cancel if it's the job we started */
1493 if (bs && bs->job && bs->job == state->job) {
1494 block_job_cancel_sync(bs->job);
1498 static void abort_prepare(BlkTransactionState *common, Error **errp)
1500 error_setg(errp, "Transaction aborted using Abort action");
1503 static void abort_commit(BlkTransactionState *common)
1505 g_assert_not_reached(); /* this action never succeeds */
1508 static const BdrvActionOps actions[] = {
1509 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1510 .instance_size = sizeof(ExternalSnapshotState),
1511 .prepare = external_snapshot_prepare,
1512 .commit = external_snapshot_commit,
1513 .abort = external_snapshot_abort,
1515 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1516 .instance_size = sizeof(DriveBackupState),
1517 .prepare = drive_backup_prepare,
1518 .abort = drive_backup_abort,
1520 [TRANSACTION_ACTION_KIND_ABORT] = {
1521 .instance_size = sizeof(BlkTransactionState),
1522 .prepare = abort_prepare,
1523 .commit = abort_commit,
1525 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1526 .instance_size = sizeof(InternalSnapshotState),
1527 .prepare = internal_snapshot_prepare,
1528 .abort = internal_snapshot_abort,
1533 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1534 * then we do not pivot any of the devices in the group, and abandon the
1535 * snapshots
1537 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1539 TransactionActionList *dev_entry = dev_list;
1540 BlkTransactionState *state, *next;
1541 Error *local_err = NULL;
1543 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
1544 QSIMPLEQ_INIT(&snap_bdrv_states);
1546 /* drain all i/o before any snapshots */
1547 bdrv_drain_all();
1549 /* We don't do anything in this loop that commits us to the snapshot */
1550 while (NULL != dev_entry) {
1551 TransactionAction *dev_info = NULL;
1552 const BdrvActionOps *ops;
1554 dev_info = dev_entry->value;
1555 dev_entry = dev_entry->next;
1557 assert(dev_info->kind < ARRAY_SIZE(actions));
1559 ops = &actions[dev_info->kind];
1560 assert(ops->instance_size > 0);
1562 state = g_malloc0(ops->instance_size);
1563 state->ops = ops;
1564 state->action = dev_info;
1565 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
1567 state->ops->prepare(state, &local_err);
1568 if (local_err) {
1569 error_propagate(errp, local_err);
1570 goto delete_and_fail;
1574 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1575 if (state->ops->commit) {
1576 state->ops->commit(state);
1580 /* success */
1581 goto exit;
1583 delete_and_fail:
1585 * failure, and it is all-or-none; abandon each new bs, and keep using
1586 * the original bs for all images
1588 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1589 if (state->ops->abort) {
1590 state->ops->abort(state);
1593 exit:
1594 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1595 if (state->ops->clean) {
1596 state->ops->clean(state);
1598 g_free(state);
1603 static void eject_device(BlockBackend *blk, int force, Error **errp)
1605 BlockDriverState *bs = blk_bs(blk);
1607 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
1608 return;
1610 if (!blk_dev_has_removable_media(blk)) {
1611 error_setg(errp, "Device '%s' is not removable",
1612 bdrv_get_device_name(bs));
1613 return;
1616 if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) {
1617 blk_dev_eject_request(blk, force);
1618 if (!force) {
1619 error_setg(errp, "Device '%s' is locked",
1620 bdrv_get_device_name(bs));
1621 return;
1625 bdrv_close(bs);
1628 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1630 BlockBackend *blk;
1632 blk = blk_by_name(device);
1633 if (!blk) {
1634 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1635 return;
1638 eject_device(blk, force, errp);
1641 void qmp_block_passwd(bool has_device, const char *device,
1642 bool has_node_name, const char *node_name,
1643 const char *password, Error **errp)
1645 Error *local_err = NULL;
1646 BlockDriverState *bs;
1647 int err;
1649 bs = bdrv_lookup_bs(has_device ? device : NULL,
1650 has_node_name ? node_name : NULL,
1651 &local_err);
1652 if (local_err) {
1653 error_propagate(errp, local_err);
1654 return;
1657 err = bdrv_set_key(bs, password);
1658 if (err == -EINVAL) {
1659 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1660 return;
1661 } else if (err < 0) {
1662 error_set(errp, QERR_INVALID_PASSWORD);
1663 return;
1667 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1668 int bdrv_flags, BlockDriver *drv,
1669 const char *password, Error **errp)
1671 Error *local_err = NULL;
1672 int ret;
1674 ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
1675 if (ret < 0) {
1676 error_propagate(errp, local_err);
1677 return;
1680 if (bdrv_key_required(bs)) {
1681 if (password) {
1682 if (bdrv_set_key(bs, password) < 0) {
1683 error_set(errp, QERR_INVALID_PASSWORD);
1685 } else {
1686 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
1687 bdrv_get_encrypted_filename(bs));
1689 } else if (password) {
1690 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1694 void qmp_change_blockdev(const char *device, const char *filename,
1695 const char *format, Error **errp)
1697 BlockBackend *blk;
1698 BlockDriverState *bs;
1699 BlockDriver *drv = NULL;
1700 int bdrv_flags;
1701 Error *err = NULL;
1703 blk = blk_by_name(device);
1704 if (!blk) {
1705 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1706 return;
1708 bs = blk_bs(blk);
1710 if (format) {
1711 drv = bdrv_find_whitelisted_format(format, bs->read_only);
1712 if (!drv) {
1713 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1714 return;
1718 eject_device(blk, 0, &err);
1719 if (err) {
1720 error_propagate(errp, err);
1721 return;
1724 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1725 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1727 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1730 /* throttling disk I/O limits */
1731 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1732 int64_t bps_wr,
1733 int64_t iops,
1734 int64_t iops_rd,
1735 int64_t iops_wr,
1736 bool has_bps_max,
1737 int64_t bps_max,
1738 bool has_bps_rd_max,
1739 int64_t bps_rd_max,
1740 bool has_bps_wr_max,
1741 int64_t bps_wr_max,
1742 bool has_iops_max,
1743 int64_t iops_max,
1744 bool has_iops_rd_max,
1745 int64_t iops_rd_max,
1746 bool has_iops_wr_max,
1747 int64_t iops_wr_max,
1748 bool has_iops_size,
1749 int64_t iops_size, Error **errp)
1751 ThrottleConfig cfg;
1752 BlockDriverState *bs;
1753 AioContext *aio_context;
1755 bs = bdrv_find(device);
1756 if (!bs) {
1757 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1758 return;
1761 memset(&cfg, 0, sizeof(cfg));
1762 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1763 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1764 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1766 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1767 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
1768 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1770 if (has_bps_max) {
1771 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
1773 if (has_bps_rd_max) {
1774 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
1776 if (has_bps_wr_max) {
1777 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
1779 if (has_iops_max) {
1780 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
1782 if (has_iops_rd_max) {
1783 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
1785 if (has_iops_wr_max) {
1786 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
1789 if (has_iops_size) {
1790 cfg.op_size = iops_size;
1793 if (!check_throttle_config(&cfg, errp)) {
1794 return;
1797 aio_context = bdrv_get_aio_context(bs);
1798 aio_context_acquire(aio_context);
1800 if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
1801 bdrv_io_limits_enable(bs);
1802 } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
1803 bdrv_io_limits_disable(bs);
1806 if (bs->io_limits_enabled) {
1807 bdrv_set_io_limits(bs, &cfg);
1810 aio_context_release(aio_context);
1813 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1815 const char *id = qdict_get_str(qdict, "id");
1816 BlockBackend *blk;
1817 BlockDriverState *bs;
1818 AioContext *aio_context;
1819 Error *local_err = NULL;
1821 blk = blk_by_name(id);
1822 if (!blk) {
1823 error_report("Device '%s' not found", id);
1824 return -1;
1826 bs = blk_bs(blk);
1828 if (!blk_legacy_dinfo(blk)) {
1829 error_report("Deleting device added with blockdev-add"
1830 " is not supported");
1831 return -1;
1834 aio_context = bdrv_get_aio_context(bs);
1835 aio_context_acquire(aio_context);
1837 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
1838 error_report("%s", error_get_pretty(local_err));
1839 error_free(local_err);
1840 aio_context_release(aio_context);
1841 return -1;
1844 /* quiesce block driver; prevent further io */
1845 bdrv_drain_all();
1846 bdrv_flush(bs);
1847 bdrv_close(bs);
1849 /* if we have a device attached to this BlockDriverState
1850 * then we need to make the drive anonymous until the device
1851 * can be removed. If this is a drive with no device backing
1852 * then we can just get rid of the block driver state right here.
1854 if (blk_get_attached_dev(blk)) {
1855 blk_hide_on_behalf_of_do_drive_del(blk);
1856 /* Further I/O must not pause the guest */
1857 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
1858 BLOCKDEV_ON_ERROR_REPORT);
1859 } else {
1860 blk_unref(blk);
1863 aio_context_release(aio_context);
1864 return 0;
1867 void qmp_block_resize(bool has_device, const char *device,
1868 bool has_node_name, const char *node_name,
1869 int64_t size, Error **errp)
1871 Error *local_err = NULL;
1872 BlockDriverState *bs;
1873 AioContext *aio_context;
1874 int ret;
1876 bs = bdrv_lookup_bs(has_device ? device : NULL,
1877 has_node_name ? node_name : NULL,
1878 &local_err);
1879 if (local_err) {
1880 error_propagate(errp, local_err);
1881 return;
1884 aio_context = bdrv_get_aio_context(bs);
1885 aio_context_acquire(aio_context);
1887 if (!bdrv_is_first_non_filter(bs)) {
1888 error_set(errp, QERR_FEATURE_DISABLED, "resize");
1889 goto out;
1892 if (size < 0) {
1893 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
1894 goto out;
1897 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
1898 error_set(errp, QERR_DEVICE_IN_USE, device);
1899 goto out;
1902 /* complete all in-flight operations before resizing the device */
1903 bdrv_drain_all();
1905 ret = bdrv_truncate(bs, size);
1906 switch (ret) {
1907 case 0:
1908 break;
1909 case -ENOMEDIUM:
1910 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1911 break;
1912 case -ENOTSUP:
1913 error_set(errp, QERR_UNSUPPORTED);
1914 break;
1915 case -EACCES:
1916 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1917 break;
1918 case -EBUSY:
1919 error_set(errp, QERR_DEVICE_IN_USE, device);
1920 break;
1921 default:
1922 error_setg_errno(errp, -ret, "Could not resize");
1923 break;
1926 out:
1927 aio_context_release(aio_context);
1930 static void block_job_cb(void *opaque, int ret)
1932 /* Note that this function may be executed from another AioContext besides
1933 * the QEMU main loop. If you need to access anything that assumes the
1934 * QEMU global mutex, use a BH or introduce a mutex.
1937 BlockDriverState *bs = opaque;
1938 const char *msg = NULL;
1940 trace_block_job_cb(bs, bs->job, ret);
1942 assert(bs->job);
1944 if (ret < 0) {
1945 msg = strerror(-ret);
1948 if (block_job_is_cancelled(bs->job)) {
1949 block_job_event_cancelled(bs->job);
1950 } else {
1951 block_job_event_completed(bs->job, msg);
1954 bdrv_put_ref_bh_schedule(bs);
1957 void qmp_block_stream(const char *device,
1958 bool has_base, const char *base,
1959 bool has_backing_file, const char *backing_file,
1960 bool has_speed, int64_t speed,
1961 bool has_on_error, BlockdevOnError on_error,
1962 Error **errp)
1964 BlockDriverState *bs;
1965 BlockDriverState *base_bs = NULL;
1966 AioContext *aio_context;
1967 Error *local_err = NULL;
1968 const char *base_name = NULL;
1970 if (!has_on_error) {
1971 on_error = BLOCKDEV_ON_ERROR_REPORT;
1974 bs = bdrv_find(device);
1975 if (!bs) {
1976 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1977 return;
1980 aio_context = bdrv_get_aio_context(bs);
1981 aio_context_acquire(aio_context);
1983 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
1984 goto out;
1987 if (has_base) {
1988 base_bs = bdrv_find_backing_image(bs, base);
1989 if (base_bs == NULL) {
1990 error_set(errp, QERR_BASE_NOT_FOUND, base);
1991 goto out;
1993 assert(bdrv_get_aio_context(base_bs) == aio_context);
1994 base_name = base;
1997 /* if we are streaming the entire chain, the result will have no backing
1998 * file, and specifying one is therefore an error */
1999 if (base_bs == NULL && has_backing_file) {
2000 error_setg(errp, "backing file specified, but streaming the "
2001 "entire chain");
2002 goto out;
2005 /* backing_file string overrides base bs filename */
2006 base_name = has_backing_file ? backing_file : base_name;
2008 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
2009 on_error, block_job_cb, bs, &local_err);
2010 if (local_err) {
2011 error_propagate(errp, local_err);
2012 goto out;
2015 trace_qmp_block_stream(bs, bs->job);
2017 out:
2018 aio_context_release(aio_context);
2021 void qmp_block_commit(const char *device,
2022 bool has_base, const char *base,
2023 bool has_top, const char *top,
2024 bool has_backing_file, const char *backing_file,
2025 bool has_speed, int64_t speed,
2026 Error **errp)
2028 BlockDriverState *bs;
2029 BlockDriverState *base_bs, *top_bs;
2030 AioContext *aio_context;
2031 Error *local_err = NULL;
2032 /* This will be part of the QMP command, if/when the
2033 * BlockdevOnError change for blkmirror makes it in
2035 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2037 if (!has_speed) {
2038 speed = 0;
2041 /* Important Note:
2042 * libvirt relies on the DeviceNotFound error class in order to probe for
2043 * live commit feature versions; for this to work, we must make sure to
2044 * perform the device lookup before any generic errors that may occur in a
2045 * scenario in which all optional arguments are omitted. */
2046 bs = bdrv_find(device);
2047 if (!bs) {
2048 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2049 return;
2052 aio_context = bdrv_get_aio_context(bs);
2053 aio_context_acquire(aio_context);
2055 /* drain all i/o before commits */
2056 bdrv_drain_all();
2058 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT, errp)) {
2059 goto out;
2062 /* default top_bs is the active layer */
2063 top_bs = bs;
2065 if (has_top && top) {
2066 if (strcmp(bs->filename, top) != 0) {
2067 top_bs = bdrv_find_backing_image(bs, top);
2071 if (top_bs == NULL) {
2072 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2073 goto out;
2076 assert(bdrv_get_aio_context(top_bs) == aio_context);
2078 if (has_base && base) {
2079 base_bs = bdrv_find_backing_image(top_bs, base);
2080 } else {
2081 base_bs = bdrv_find_base(top_bs);
2084 if (base_bs == NULL) {
2085 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
2086 goto out;
2089 assert(bdrv_get_aio_context(base_bs) == aio_context);
2091 /* Do not allow attempts to commit an image into itself */
2092 if (top_bs == base_bs) {
2093 error_setg(errp, "cannot commit an image into itself");
2094 goto out;
2097 if (top_bs == bs) {
2098 if (has_backing_file) {
2099 error_setg(errp, "'backing-file' specified,"
2100 " but 'top' is the active layer");
2101 goto out;
2103 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2104 bs, &local_err);
2105 } else {
2106 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
2107 has_backing_file ? backing_file : NULL, &local_err);
2109 if (local_err != NULL) {
2110 error_propagate(errp, local_err);
2111 goto out;
2114 out:
2115 aio_context_release(aio_context);
2118 void qmp_drive_backup(const char *device, const char *target,
2119 bool has_format, const char *format,
2120 enum MirrorSyncMode sync,
2121 bool has_mode, enum NewImageMode mode,
2122 bool has_speed, int64_t speed,
2123 bool has_on_source_error, BlockdevOnError on_source_error,
2124 bool has_on_target_error, BlockdevOnError on_target_error,
2125 Error **errp)
2127 BlockDriverState *bs;
2128 BlockDriverState *target_bs;
2129 BlockDriverState *source = NULL;
2130 AioContext *aio_context;
2131 BlockDriver *drv = NULL;
2132 Error *local_err = NULL;
2133 int flags;
2134 int64_t size;
2135 int ret;
2137 if (!has_speed) {
2138 speed = 0;
2140 if (!has_on_source_error) {
2141 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2143 if (!has_on_target_error) {
2144 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2146 if (!has_mode) {
2147 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2150 bs = bdrv_find(device);
2151 if (!bs) {
2152 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2153 return;
2156 aio_context = bdrv_get_aio_context(bs);
2157 aio_context_acquire(aio_context);
2159 if (!bdrv_is_inserted(bs)) {
2160 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2161 goto out;
2164 if (!has_format) {
2165 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2167 if (format) {
2168 drv = bdrv_find_format(format);
2169 if (!drv) {
2170 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
2171 goto out;
2175 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
2176 goto out;
2179 flags = bs->open_flags | BDRV_O_RDWR;
2181 /* See if we have a backing HD we can use to create our new image
2182 * on top of. */
2183 if (sync == MIRROR_SYNC_MODE_TOP) {
2184 source = bs->backing_hd;
2185 if (!source) {
2186 sync = MIRROR_SYNC_MODE_FULL;
2189 if (sync == MIRROR_SYNC_MODE_NONE) {
2190 source = bs;
2193 size = bdrv_getlength(bs);
2194 if (size < 0) {
2195 error_setg_errno(errp, -size, "bdrv_getlength failed");
2196 goto out;
2199 if (mode != NEW_IMAGE_MODE_EXISTING) {
2200 assert(format && drv);
2201 if (source) {
2202 bdrv_img_create(target, format, source->filename,
2203 source->drv->format_name, NULL,
2204 size, flags, &local_err, false);
2205 } else {
2206 bdrv_img_create(target, format, NULL, NULL, NULL,
2207 size, flags, &local_err, false);
2211 if (local_err) {
2212 error_propagate(errp, local_err);
2213 goto out;
2216 target_bs = NULL;
2217 ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err);
2218 if (ret < 0) {
2219 error_propagate(errp, local_err);
2220 goto out;
2223 bdrv_set_aio_context(target_bs, aio_context);
2225 backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
2226 block_job_cb, bs, &local_err);
2227 if (local_err != NULL) {
2228 bdrv_unref(target_bs);
2229 error_propagate(errp, local_err);
2230 goto out;
2233 out:
2234 aio_context_release(aio_context);
2237 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
2239 return bdrv_named_nodes_list();
2242 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2244 void qmp_drive_mirror(const char *device, const char *target,
2245 bool has_format, const char *format,
2246 bool has_node_name, const char *node_name,
2247 bool has_replaces, const char *replaces,
2248 enum MirrorSyncMode sync,
2249 bool has_mode, enum NewImageMode mode,
2250 bool has_speed, int64_t speed,
2251 bool has_granularity, uint32_t granularity,
2252 bool has_buf_size, int64_t buf_size,
2253 bool has_on_source_error, BlockdevOnError on_source_error,
2254 bool has_on_target_error, BlockdevOnError on_target_error,
2255 Error **errp)
2257 BlockDriverState *bs;
2258 BlockDriverState *source, *target_bs;
2259 AioContext *aio_context;
2260 BlockDriver *drv = NULL;
2261 Error *local_err = NULL;
2262 QDict *options = NULL;
2263 int flags;
2264 int64_t size;
2265 int ret;
2267 if (!has_speed) {
2268 speed = 0;
2270 if (!has_on_source_error) {
2271 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2273 if (!has_on_target_error) {
2274 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2276 if (!has_mode) {
2277 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2279 if (!has_granularity) {
2280 granularity = 0;
2282 if (!has_buf_size) {
2283 buf_size = DEFAULT_MIRROR_BUF_SIZE;
2286 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
2287 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2288 "a value in range [512B, 64MB]");
2289 return;
2291 if (granularity & (granularity - 1)) {
2292 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2");
2293 return;
2296 bs = bdrv_find(device);
2297 if (!bs) {
2298 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2299 return;
2302 aio_context = bdrv_get_aio_context(bs);
2303 aio_context_acquire(aio_context);
2305 if (!bdrv_is_inserted(bs)) {
2306 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2307 goto out;
2310 if (!has_format) {
2311 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2313 if (format) {
2314 drv = bdrv_find_format(format);
2315 if (!drv) {
2316 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
2317 goto out;
2321 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
2322 goto out;
2325 flags = bs->open_flags | BDRV_O_RDWR;
2326 source = bs->backing_hd;
2327 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
2328 sync = MIRROR_SYNC_MODE_FULL;
2330 if (sync == MIRROR_SYNC_MODE_NONE) {
2331 source = bs;
2334 size = bdrv_getlength(bs);
2335 if (size < 0) {
2336 error_setg_errno(errp, -size, "bdrv_getlength failed");
2337 goto out;
2340 if (has_replaces) {
2341 BlockDriverState *to_replace_bs;
2342 AioContext *replace_aio_context;
2343 int64_t replace_size;
2345 if (!has_node_name) {
2346 error_setg(errp, "a node-name must be provided when replacing a"
2347 " named node of the graph");
2348 goto out;
2351 to_replace_bs = check_to_replace_node(replaces, &local_err);
2353 if (!to_replace_bs) {
2354 error_propagate(errp, local_err);
2355 goto out;
2358 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
2359 aio_context_acquire(replace_aio_context);
2360 replace_size = bdrv_getlength(to_replace_bs);
2361 aio_context_release(replace_aio_context);
2363 if (size != replace_size) {
2364 error_setg(errp, "cannot replace image with a mirror image of "
2365 "different size");
2366 goto out;
2370 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
2371 && mode != NEW_IMAGE_MODE_EXISTING)
2373 /* create new image w/o backing file */
2374 assert(format && drv);
2375 bdrv_img_create(target, format,
2376 NULL, NULL, NULL, size, flags, &local_err, false);
2377 } else {
2378 switch (mode) {
2379 case NEW_IMAGE_MODE_EXISTING:
2380 break;
2381 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2382 /* create new image with backing file */
2383 bdrv_img_create(target, format,
2384 source->filename,
2385 source->drv->format_name,
2386 NULL, size, flags, &local_err, false);
2387 break;
2388 default:
2389 abort();
2393 if (local_err) {
2394 error_propagate(errp, local_err);
2395 goto out;
2398 if (has_node_name) {
2399 options = qdict_new();
2400 qdict_put(options, "node-name", qstring_from_str(node_name));
2403 /* Mirroring takes care of copy-on-write using the source's backing
2404 * file.
2406 target_bs = NULL;
2407 ret = bdrv_open(&target_bs, target, NULL, options,
2408 flags | BDRV_O_NO_BACKING, drv, &local_err);
2409 if (ret < 0) {
2410 error_propagate(errp, local_err);
2411 goto out;
2414 bdrv_set_aio_context(target_bs, aio_context);
2416 /* pass the node name to replace to mirror start since it's loose coupling
2417 * and will allow to check whether the node still exist at mirror completion
2419 mirror_start(bs, target_bs,
2420 has_replaces ? replaces : NULL,
2421 speed, granularity, buf_size, sync,
2422 on_source_error, on_target_error,
2423 block_job_cb, bs, &local_err);
2424 if (local_err != NULL) {
2425 bdrv_unref(target_bs);
2426 error_propagate(errp, local_err);
2427 goto out;
2430 out:
2431 aio_context_release(aio_context);
2434 /* Get the block job for a given device name and acquire its AioContext */
2435 static BlockJob *find_block_job(const char *device, AioContext **aio_context)
2437 BlockDriverState *bs;
2439 bs = bdrv_find(device);
2440 if (!bs) {
2441 goto notfound;
2444 *aio_context = bdrv_get_aio_context(bs);
2445 aio_context_acquire(*aio_context);
2447 if (!bs->job) {
2448 aio_context_release(*aio_context);
2449 goto notfound;
2452 return bs->job;
2454 notfound:
2455 *aio_context = NULL;
2456 return NULL;
2459 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2461 AioContext *aio_context;
2462 BlockJob *job = find_block_job(device, &aio_context);
2464 if (!job) {
2465 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2466 return;
2469 block_job_set_speed(job, speed, errp);
2470 aio_context_release(aio_context);
2473 void qmp_block_job_cancel(const char *device,
2474 bool has_force, bool force, Error **errp)
2476 AioContext *aio_context;
2477 BlockJob *job = find_block_job(device, &aio_context);
2479 if (!job) {
2480 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2481 return;
2484 if (!has_force) {
2485 force = false;
2488 if (job->paused && !force) {
2489 error_setg(errp, "The block job for device '%s' is currently paused",
2490 device);
2491 goto out;
2494 trace_qmp_block_job_cancel(job);
2495 block_job_cancel(job);
2496 out:
2497 aio_context_release(aio_context);
2500 void qmp_block_job_pause(const char *device, Error **errp)
2502 AioContext *aio_context;
2503 BlockJob *job = find_block_job(device, &aio_context);
2505 if (!job) {
2506 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2507 return;
2510 trace_qmp_block_job_pause(job);
2511 block_job_pause(job);
2512 aio_context_release(aio_context);
2515 void qmp_block_job_resume(const char *device, Error **errp)
2517 AioContext *aio_context;
2518 BlockJob *job = find_block_job(device, &aio_context);
2520 if (!job) {
2521 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2522 return;
2525 trace_qmp_block_job_resume(job);
2526 block_job_resume(job);
2527 aio_context_release(aio_context);
2530 void qmp_block_job_complete(const char *device, Error **errp)
2532 AioContext *aio_context;
2533 BlockJob *job = find_block_job(device, &aio_context);
2535 if (!job) {
2536 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2537 return;
2540 trace_qmp_block_job_complete(job);
2541 block_job_complete(job, errp);
2542 aio_context_release(aio_context);
2545 void qmp_change_backing_file(const char *device,
2546 const char *image_node_name,
2547 const char *backing_file,
2548 Error **errp)
2550 BlockDriverState *bs = NULL;
2551 BlockDriverState *image_bs = NULL;
2552 Error *local_err = NULL;
2553 bool ro;
2554 int open_flags;
2555 int ret;
2557 /* find the top layer BDS of the chain */
2558 bs = bdrv_find(device);
2559 if (!bs) {
2560 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2561 return;
2564 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
2565 if (local_err) {
2566 error_propagate(errp, local_err);
2567 return;
2570 if (!image_bs) {
2571 error_setg(errp, "image file not found");
2572 return;
2575 if (bdrv_find_base(image_bs) == image_bs) {
2576 error_setg(errp, "not allowing backing file change on an image "
2577 "without a backing file");
2578 return;
2581 /* even though we are not necessarily operating on bs, we need it to
2582 * determine if block ops are currently prohibited on the chain */
2583 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
2584 return;
2587 /* final sanity check */
2588 if (!bdrv_chain_contains(bs, image_bs)) {
2589 error_setg(errp, "'%s' and image file are not in the same chain",
2590 device);
2591 return;
2594 /* if not r/w, reopen to make r/w */
2595 open_flags = image_bs->open_flags;
2596 ro = bdrv_is_read_only(image_bs);
2598 if (ro) {
2599 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
2600 if (local_err) {
2601 error_propagate(errp, local_err);
2602 return;
2606 ret = bdrv_change_backing_file(image_bs, backing_file,
2607 image_bs->drv ? image_bs->drv->format_name : "");
2609 if (ret < 0) {
2610 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
2611 backing_file);
2612 /* don't exit here, so we can try to restore open flags if
2613 * appropriate */
2616 if (ro) {
2617 bdrv_reopen(image_bs, open_flags, &local_err);
2618 if (local_err) {
2619 error_propagate(errp, local_err); /* will preserve prior errp */
2624 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
2626 QmpOutputVisitor *ov = qmp_output_visitor_new();
2627 BlockBackend *blk;
2628 QObject *obj;
2629 QDict *qdict;
2630 Error *local_err = NULL;
2632 /* Require an ID in the top level */
2633 if (!options->has_id) {
2634 error_setg(errp, "Block device needs an ID");
2635 goto fail;
2638 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
2639 * cache.direct=false instead of silently switching to aio=threads, except
2640 * when called from drive_new().
2642 * For now, simply forbidding the combination for all drivers will do. */
2643 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
2644 bool direct = options->has_cache &&
2645 options->cache->has_direct &&
2646 options->cache->direct;
2647 if (!direct) {
2648 error_setg(errp, "aio=native requires cache.direct=true");
2649 goto fail;
2653 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
2654 &options, NULL, &local_err);
2655 if (local_err) {
2656 error_propagate(errp, local_err);
2657 goto fail;
2660 obj = qmp_output_get_qobject(ov);
2661 qdict = qobject_to_qdict(obj);
2663 qdict_flatten(qdict);
2665 blk = blockdev_init(NULL, qdict, &local_err);
2666 if (local_err) {
2667 error_propagate(errp, local_err);
2668 goto fail;
2671 if (bdrv_key_required(blk_bs(blk))) {
2672 blk_unref(blk);
2673 error_setg(errp, "blockdev-add doesn't support encrypted devices");
2674 goto fail;
2677 fail:
2678 qmp_output_visitor_cleanup(ov);
2681 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
2683 BlockJobInfoList *head = NULL, **p_next = &head;
2684 BlockDriverState *bs;
2686 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
2687 AioContext *aio_context = bdrv_get_aio_context(bs);
2689 aio_context_acquire(aio_context);
2691 if (bs->job) {
2692 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
2693 elem->value = block_job_query(bs->job);
2694 *p_next = elem;
2695 p_next = &elem->next;
2698 aio_context_release(aio_context);
2701 return head;
2704 QemuOptsList qemu_common_drive_opts = {
2705 .name = "drive",
2706 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
2707 .desc = {
2709 .name = "snapshot",
2710 .type = QEMU_OPT_BOOL,
2711 .help = "enable/disable snapshot mode",
2713 .name = "discard",
2714 .type = QEMU_OPT_STRING,
2715 .help = "discard operation (ignore/off, unmap/on)",
2717 .name = "cache.writeback",
2718 .type = QEMU_OPT_BOOL,
2719 .help = "enables writeback mode for any caches",
2721 .name = "cache.direct",
2722 .type = QEMU_OPT_BOOL,
2723 .help = "enables use of O_DIRECT (bypass the host page cache)",
2725 .name = "cache.no-flush",
2726 .type = QEMU_OPT_BOOL,
2727 .help = "ignore any flush requests for the device",
2729 .name = "aio",
2730 .type = QEMU_OPT_STRING,
2731 .help = "host AIO implementation (threads, native)",
2733 .name = "format",
2734 .type = QEMU_OPT_STRING,
2735 .help = "disk format (raw, qcow2, ...)",
2737 .name = "rerror",
2738 .type = QEMU_OPT_STRING,
2739 .help = "read error action",
2741 .name = "werror",
2742 .type = QEMU_OPT_STRING,
2743 .help = "write error action",
2745 .name = "read-only",
2746 .type = QEMU_OPT_BOOL,
2747 .help = "open drive file as read-only",
2749 .name = "throttling.iops-total",
2750 .type = QEMU_OPT_NUMBER,
2751 .help = "limit total I/O operations per second",
2753 .name = "throttling.iops-read",
2754 .type = QEMU_OPT_NUMBER,
2755 .help = "limit read operations per second",
2757 .name = "throttling.iops-write",
2758 .type = QEMU_OPT_NUMBER,
2759 .help = "limit write operations per second",
2761 .name = "throttling.bps-total",
2762 .type = QEMU_OPT_NUMBER,
2763 .help = "limit total bytes per second",
2765 .name = "throttling.bps-read",
2766 .type = QEMU_OPT_NUMBER,
2767 .help = "limit read bytes per second",
2769 .name = "throttling.bps-write",
2770 .type = QEMU_OPT_NUMBER,
2771 .help = "limit write bytes per second",
2773 .name = "throttling.iops-total-max",
2774 .type = QEMU_OPT_NUMBER,
2775 .help = "I/O operations burst",
2777 .name = "throttling.iops-read-max",
2778 .type = QEMU_OPT_NUMBER,
2779 .help = "I/O operations read burst",
2781 .name = "throttling.iops-write-max",
2782 .type = QEMU_OPT_NUMBER,
2783 .help = "I/O operations write burst",
2785 .name = "throttling.bps-total-max",
2786 .type = QEMU_OPT_NUMBER,
2787 .help = "total bytes burst",
2789 .name = "throttling.bps-read-max",
2790 .type = QEMU_OPT_NUMBER,
2791 .help = "total bytes read burst",
2793 .name = "throttling.bps-write-max",
2794 .type = QEMU_OPT_NUMBER,
2795 .help = "total bytes write burst",
2797 .name = "throttling.iops-size",
2798 .type = QEMU_OPT_NUMBER,
2799 .help = "when limiting by iops max size of an I/O in bytes",
2801 .name = "copy-on-read",
2802 .type = QEMU_OPT_BOOL,
2803 .help = "copy read data from backing file into image file",
2805 .name = "detect-zeroes",
2806 .type = QEMU_OPT_STRING,
2807 .help = "try to optimize zero writes (off, on, unmap)",
2809 { /* end of list */ }
2813 QemuOptsList qemu_drive_opts = {
2814 .name = "drive",
2815 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
2816 .desc = {
2818 * no elements => accept any params
2819 * validation will happen later
2821 { /* end of list */ }