qemu-iotests: Test for loading VM state from qcow2
[qemu.git] / blockdev.c
blobb260477f1b00e1b3d2e968d177e8c4b615aa1c49
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/blockdev.h"
34 #include "hw/block/block.h"
35 #include "block/blockjob.h"
36 #include "monitor/monitor.h"
37 #include "qapi/qmp/qerror.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 "sysemu/sysemu.h"
44 #include "block/block_int.h"
45 #include "qmp-commands.h"
46 #include "trace.h"
47 #include "sysemu/arch_init.h"
49 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
50 extern QemuOptsList qemu_common_drive_opts;
52 static const char *const if_name[IF_COUNT] = {
53 [IF_NONE] = "none",
54 [IF_IDE] = "ide",
55 [IF_SCSI] = "scsi",
56 [IF_FLOPPY] = "floppy",
57 [IF_PFLASH] = "pflash",
58 [IF_MTD] = "mtd",
59 [IF_SD] = "sd",
60 [IF_VIRTIO] = "virtio",
61 [IF_XEN] = "xen",
64 static const int if_max_devs[IF_COUNT] = {
66 * Do not change these numbers! They govern how drive option
67 * index maps to unit and bus. That mapping is ABI.
69 * All controllers used to imlement if=T drives need to support
70 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
71 * Otherwise, some index values map to "impossible" bus, unit
72 * values.
74 * For instance, if you change [IF_SCSI] to 255, -drive
75 * if=scsi,index=12 no longer means bus=1,unit=5, but
76 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
77 * the drive can't be set up. Regression.
79 [IF_IDE] = 2,
80 [IF_SCSI] = 7,
84 * We automatically delete the drive when a device using it gets
85 * unplugged. Questionable feature, but we can't just drop it.
86 * Device models call blockdev_mark_auto_del() to schedule the
87 * automatic deletion, and generic qdev code calls blockdev_auto_del()
88 * when deletion is actually safe.
90 void blockdev_mark_auto_del(BlockDriverState *bs)
92 DriveInfo *dinfo = drive_get_by_blockdev(bs);
94 if (dinfo && !dinfo->enable_auto_del) {
95 return;
98 if (bs->job) {
99 block_job_cancel(bs->job);
101 if (dinfo) {
102 dinfo->auto_del = 1;
106 void blockdev_auto_del(BlockDriverState *bs)
108 DriveInfo *dinfo = drive_get_by_blockdev(bs);
110 if (dinfo && dinfo->auto_del) {
111 drive_put_ref(dinfo);
115 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
117 int max_devs = if_max_devs[type];
118 return max_devs ? index / max_devs : 0;
121 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
123 int max_devs = if_max_devs[type];
124 return max_devs ? index % max_devs : index;
127 QemuOpts *drive_def(const char *optstr)
129 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
132 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
133 const char *optstr)
135 QemuOpts *opts;
136 char buf[32];
138 opts = drive_def(optstr);
139 if (!opts) {
140 return NULL;
142 if (type != IF_DEFAULT) {
143 qemu_opt_set(opts, "if", if_name[type]);
145 if (index >= 0) {
146 snprintf(buf, sizeof(buf), "%d", index);
147 qemu_opt_set(opts, "index", buf);
149 if (file)
150 qemu_opt_set(opts, "file", file);
151 return opts;
154 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
156 DriveInfo *dinfo;
158 /* seek interface, bus and unit */
160 QTAILQ_FOREACH(dinfo, &drives, next) {
161 if (dinfo->type == type &&
162 dinfo->bus == bus &&
163 dinfo->unit == unit)
164 return dinfo;
167 return NULL;
170 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
172 return drive_get(type,
173 drive_index_to_bus_id(type, index),
174 drive_index_to_unit_id(type, index));
177 int drive_get_max_bus(BlockInterfaceType type)
179 int max_bus;
180 DriveInfo *dinfo;
182 max_bus = -1;
183 QTAILQ_FOREACH(dinfo, &drives, next) {
184 if(dinfo->type == type &&
185 dinfo->bus > max_bus)
186 max_bus = dinfo->bus;
188 return max_bus;
191 /* Get a block device. This should only be used for single-drive devices
192 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
193 appropriate bus. */
194 DriveInfo *drive_get_next(BlockInterfaceType type)
196 static int next_block_unit[IF_COUNT];
198 return drive_get(type, 0, next_block_unit[type]++);
201 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
203 DriveInfo *dinfo;
205 QTAILQ_FOREACH(dinfo, &drives, next) {
206 if (dinfo->bdrv == bs) {
207 return dinfo;
210 return NULL;
213 static void bdrv_format_print(void *opaque, const char *name)
215 error_printf(" %s", name);
218 static void drive_uninit(DriveInfo *dinfo)
220 if (dinfo->opts) {
221 qemu_opts_del(dinfo->opts);
224 bdrv_unref(dinfo->bdrv);
225 g_free(dinfo->id);
226 QTAILQ_REMOVE(&drives, dinfo, next);
227 g_free(dinfo->serial);
228 g_free(dinfo);
231 void drive_put_ref(DriveInfo *dinfo)
233 assert(dinfo->refcount);
234 if (--dinfo->refcount == 0) {
235 drive_uninit(dinfo);
239 void drive_get_ref(DriveInfo *dinfo)
241 dinfo->refcount++;
244 typedef struct {
245 QEMUBH *bh;
246 BlockDriverState *bs;
247 } BDRVPutRefBH;
249 static void bdrv_put_ref_bh(void *opaque)
251 BDRVPutRefBH *s = opaque;
253 bdrv_unref(s->bs);
254 qemu_bh_delete(s->bh);
255 g_free(s);
259 * Release a BDS reference in a BH
261 * It is not safe to use bdrv_unref() from a callback function when the callers
262 * still need the BlockDriverState. In such cases we schedule a BH to release
263 * the reference.
265 static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
267 BDRVPutRefBH *s;
269 s = g_new(BDRVPutRefBH, 1);
270 s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
271 s->bs = bs;
272 qemu_bh_schedule(s->bh);
275 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
277 if (!strcmp(buf, "ignore")) {
278 return BLOCKDEV_ON_ERROR_IGNORE;
279 } else if (!is_read && !strcmp(buf, "enospc")) {
280 return BLOCKDEV_ON_ERROR_ENOSPC;
281 } else if (!strcmp(buf, "stop")) {
282 return BLOCKDEV_ON_ERROR_STOP;
283 } else if (!strcmp(buf, "report")) {
284 return BLOCKDEV_ON_ERROR_REPORT;
285 } else {
286 error_setg(errp, "'%s' invalid %s error action",
287 buf, is_read ? "read" : "write");
288 return -1;
292 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
294 if (throttle_conflicting(cfg)) {
295 error_setg(errp, "bps/iops/max total values and read/write values"
296 " cannot be used at the same time");
297 return false;
300 if (!throttle_is_valid(cfg)) {
301 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
302 return false;
305 return true;
308 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
310 /* Takes the ownership of bs_opts */
311 static DriveInfo *blockdev_init(QDict *bs_opts,
312 BlockInterfaceType type,
313 Error **errp)
315 const char *buf;
316 const char *file = NULL;
317 const char *serial;
318 int ro = 0;
319 int bdrv_flags = 0;
320 int on_read_error, on_write_error;
321 DriveInfo *dinfo;
322 ThrottleConfig cfg;
323 int snapshot = 0;
324 bool copy_on_read;
325 int ret;
326 Error *error = NULL;
327 QemuOpts *opts;
328 const char *id;
329 bool has_driver_specific_opts;
330 BlockDriver *drv = NULL;
332 /* Check common options by copying from bs_opts to opts, all other options
333 * stay in bs_opts for processing by bdrv_open(). */
334 id = qdict_get_try_str(bs_opts, "id");
335 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
336 if (error_is_set(&error)) {
337 error_propagate(errp, error);
338 return NULL;
341 qemu_opts_absorb_qdict(opts, bs_opts, &error);
342 if (error_is_set(&error)) {
343 error_propagate(errp, error);
344 return NULL;
347 if (id) {
348 qdict_del(bs_opts, "id");
351 has_driver_specific_opts = !!qdict_size(bs_opts);
353 /* extract parameters */
354 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
355 ro = qemu_opt_get_bool(opts, "read-only", 0);
356 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
358 file = qemu_opt_get(opts, "file");
359 serial = qemu_opt_get(opts, "serial");
361 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
362 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
363 error_setg(errp, "invalid discard option");
364 return NULL;
368 if (qemu_opt_get_bool(opts, "cache.writeback", true)) {
369 bdrv_flags |= BDRV_O_CACHE_WB;
371 if (qemu_opt_get_bool(opts, "cache.direct", false)) {
372 bdrv_flags |= BDRV_O_NOCACHE;
374 if (qemu_opt_get_bool(opts, "cache.no-flush", false)) {
375 bdrv_flags |= BDRV_O_NO_FLUSH;
378 #ifdef CONFIG_LINUX_AIO
379 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
380 if (!strcmp(buf, "native")) {
381 bdrv_flags |= BDRV_O_NATIVE_AIO;
382 } else if (!strcmp(buf, "threads")) {
383 /* this is the default */
384 } else {
385 error_setg(errp, "invalid aio option");
386 return NULL;
389 #endif
391 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
392 if (is_help_option(buf)) {
393 error_printf("Supported formats:");
394 bdrv_iterate_format(bdrv_format_print, NULL);
395 error_printf("\n");
396 return NULL;
399 drv = bdrv_find_format(buf);
400 if (!drv) {
401 error_setg(errp, "'%s' invalid format", buf);
402 return NULL;
406 /* disk I/O throttling */
407 memset(&cfg, 0, sizeof(cfg));
408 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
409 qemu_opt_get_number(opts, "throttling.bps-total", 0);
410 cfg.buckets[THROTTLE_BPS_READ].avg =
411 qemu_opt_get_number(opts, "throttling.bps-read", 0);
412 cfg.buckets[THROTTLE_BPS_WRITE].avg =
413 qemu_opt_get_number(opts, "throttling.bps-write", 0);
414 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
415 qemu_opt_get_number(opts, "throttling.iops-total", 0);
416 cfg.buckets[THROTTLE_OPS_READ].avg =
417 qemu_opt_get_number(opts, "throttling.iops-read", 0);
418 cfg.buckets[THROTTLE_OPS_WRITE].avg =
419 qemu_opt_get_number(opts, "throttling.iops-write", 0);
421 cfg.buckets[THROTTLE_BPS_TOTAL].max =
422 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
423 cfg.buckets[THROTTLE_BPS_READ].max =
424 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
425 cfg.buckets[THROTTLE_BPS_WRITE].max =
426 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
427 cfg.buckets[THROTTLE_OPS_TOTAL].max =
428 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
429 cfg.buckets[THROTTLE_OPS_READ].max =
430 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
431 cfg.buckets[THROTTLE_OPS_WRITE].max =
432 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
434 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
436 if (!check_throttle_config(&cfg, &error)) {
437 error_propagate(errp, error);
438 return NULL;
441 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
442 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
443 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
444 error_setg(errp, "werror is not supported by this bus type");
445 return NULL;
448 on_write_error = parse_block_error_action(buf, 0, &error);
449 if (error_is_set(&error)) {
450 error_propagate(errp, error);
451 return NULL;
455 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
456 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
457 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
458 error_report("rerror is not supported by this bus type");
459 return NULL;
462 on_read_error = parse_block_error_action(buf, 1, &error);
463 if (error_is_set(&error)) {
464 error_propagate(errp, error);
465 return NULL;
469 /* init */
470 dinfo = g_malloc0(sizeof(*dinfo));
471 dinfo->id = g_strdup(qemu_opts_id(opts));
472 dinfo->bdrv = bdrv_new(dinfo->id);
473 dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
474 dinfo->bdrv->read_only = ro;
475 dinfo->type = type;
476 dinfo->refcount = 1;
477 if (serial != NULL) {
478 dinfo->serial = g_strdup(serial);
480 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
482 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
484 /* disk I/O throttling */
485 if (throttle_enabled(&cfg)) {
486 bdrv_io_limits_enable(dinfo->bdrv);
487 bdrv_set_io_limits(dinfo->bdrv, &cfg);
490 if (!file || !*file) {
491 if (has_driver_specific_opts) {
492 file = NULL;
493 } else {
494 return dinfo;
497 if (snapshot) {
498 /* always use cache=unsafe with snapshot */
499 bdrv_flags &= ~BDRV_O_CACHE_MASK;
500 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
503 if (copy_on_read) {
504 bdrv_flags |= BDRV_O_COPY_ON_READ;
507 if (runstate_check(RUN_STATE_INMIGRATE)) {
508 bdrv_flags |= BDRV_O_INCOMING;
511 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
513 QINCREF(bs_opts);
514 ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv, &error);
516 if (ret < 0) {
517 error_setg(errp, "could not open disk image %s: %s",
518 file ?: dinfo->id, error_get_pretty(error));
519 error_free(error);
520 goto err;
523 if (bdrv_key_required(dinfo->bdrv))
524 autostart = 0;
526 QDECREF(bs_opts);
527 qemu_opts_del(opts);
529 return dinfo;
531 err:
532 qemu_opts_del(opts);
533 QDECREF(bs_opts);
534 bdrv_unref(dinfo->bdrv);
535 g_free(dinfo->id);
536 QTAILQ_REMOVE(&drives, dinfo, next);
537 g_free(dinfo);
538 return NULL;
541 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to)
543 const char *value;
545 value = qemu_opt_get(opts, from);
546 if (value) {
547 qemu_opt_set(opts, to, value);
548 qemu_opt_unset(opts, from);
552 QemuOptsList qemu_legacy_drive_opts = {
553 .name = "drive",
554 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
555 .desc = {
557 .name = "bus",
558 .type = QEMU_OPT_NUMBER,
559 .help = "bus number",
561 .name = "unit",
562 .type = QEMU_OPT_NUMBER,
563 .help = "unit number (i.e. lun for scsi)",
565 .name = "index",
566 .type = QEMU_OPT_NUMBER,
567 .help = "index number",
569 .name = "media",
570 .type = QEMU_OPT_STRING,
571 .help = "media type (disk, cdrom)",
573 .name = "if",
574 .type = QEMU_OPT_STRING,
575 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
577 .name = "cyls",
578 .type = QEMU_OPT_NUMBER,
579 .help = "number of cylinders (ide disk geometry)",
581 .name = "heads",
582 .type = QEMU_OPT_NUMBER,
583 .help = "number of heads (ide disk geometry)",
585 .name = "secs",
586 .type = QEMU_OPT_NUMBER,
587 .help = "number of sectors (ide disk geometry)",
589 .name = "trans",
590 .type = QEMU_OPT_STRING,
591 .help = "chs translation (auto, lba, none)",
593 .name = "boot",
594 .type = QEMU_OPT_BOOL,
595 .help = "(deprecated, ignored)",
597 .name = "addr",
598 .type = QEMU_OPT_STRING,
599 .help = "pci address (virtio only)",
602 /* Options that are passed on, but have special semantics with -drive */
604 .name = "read-only",
605 .type = QEMU_OPT_BOOL,
606 .help = "open drive file as read-only",
608 .name = "copy-on-read",
609 .type = QEMU_OPT_BOOL,
610 .help = "copy read data from backing file into image file",
613 { /* end of list */ }
617 DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
619 const char *value;
620 DriveInfo *dinfo = NULL;
621 QDict *bs_opts;
622 QemuOpts *legacy_opts;
623 DriveMediaType media = MEDIA_DISK;
624 BlockInterfaceType type;
625 int cyls, heads, secs, translation;
626 int max_devs, bus_id, unit_id, index;
627 const char *devaddr;
628 bool read_only = false;
629 bool copy_on_read;
630 Error *local_err = NULL;
632 /* Change legacy command line options into QMP ones */
633 qemu_opt_rename(all_opts, "iops", "throttling.iops-total");
634 qemu_opt_rename(all_opts, "iops_rd", "throttling.iops-read");
635 qemu_opt_rename(all_opts, "iops_wr", "throttling.iops-write");
637 qemu_opt_rename(all_opts, "bps", "throttling.bps-total");
638 qemu_opt_rename(all_opts, "bps_rd", "throttling.bps-read");
639 qemu_opt_rename(all_opts, "bps_wr", "throttling.bps-write");
641 qemu_opt_rename(all_opts, "iops_max", "throttling.iops-total-max");
642 qemu_opt_rename(all_opts, "iops_rd_max", "throttling.iops-read-max");
643 qemu_opt_rename(all_opts, "iops_wr_max", "throttling.iops-write-max");
645 qemu_opt_rename(all_opts, "bps_max", "throttling.bps-total-max");
646 qemu_opt_rename(all_opts, "bps_rd_max", "throttling.bps-read-max");
647 qemu_opt_rename(all_opts, "bps_wr_max", "throttling.bps-write-max");
649 qemu_opt_rename(all_opts,
650 "iops_size", "throttling.iops-size");
652 qemu_opt_rename(all_opts, "readonly", "read-only");
654 value = qemu_opt_get(all_opts, "cache");
655 if (value) {
656 int flags = 0;
658 if (bdrv_parse_cache_flags(value, &flags) != 0) {
659 error_report("invalid cache option");
660 return NULL;
663 /* Specific options take precedence */
664 if (!qemu_opt_get(all_opts, "cache.writeback")) {
665 qemu_opt_set_bool(all_opts, "cache.writeback",
666 !!(flags & BDRV_O_CACHE_WB));
668 if (!qemu_opt_get(all_opts, "cache.direct")) {
669 qemu_opt_set_bool(all_opts, "cache.direct",
670 !!(flags & BDRV_O_NOCACHE));
672 if (!qemu_opt_get(all_opts, "cache.no-flush")) {
673 qemu_opt_set_bool(all_opts, "cache.no-flush",
674 !!(flags & BDRV_O_NO_FLUSH));
676 qemu_opt_unset(all_opts, "cache");
679 /* Get a QDict for processing the options */
680 bs_opts = qdict_new();
681 qemu_opts_to_qdict(all_opts, bs_opts);
683 legacy_opts = qemu_opts_create_nofail(&qemu_legacy_drive_opts);
684 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
685 if (error_is_set(&local_err)) {
686 qerror_report_err(local_err);
687 error_free(local_err);
688 goto fail;
691 /* Deprecated option boot=[on|off] */
692 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
693 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
694 "ignored. Future versions will reject this parameter. Please "
695 "update your scripts.\n");
698 /* Media type */
699 value = qemu_opt_get(legacy_opts, "media");
700 if (value) {
701 if (!strcmp(value, "disk")) {
702 media = MEDIA_DISK;
703 } else if (!strcmp(value, "cdrom")) {
704 media = MEDIA_CDROM;
705 read_only = true;
706 } else {
707 error_report("'%s' invalid media", value);
708 goto fail;
712 /* copy-on-read is disabled with a warning for read-only devices */
713 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
714 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
716 if (read_only && copy_on_read) {
717 error_report("warning: disabling copy-on-read on read-only drive");
718 copy_on_read = false;
721 qdict_put(bs_opts, "read-only",
722 qstring_from_str(read_only ? "on" : "off"));
723 qdict_put(bs_opts, "copy-on-read",
724 qstring_from_str(copy_on_read ? "on" :"off"));
726 /* Controller type */
727 value = qemu_opt_get(legacy_opts, "if");
728 if (value) {
729 for (type = 0;
730 type < IF_COUNT && strcmp(value, if_name[type]);
731 type++) {
733 if (type == IF_COUNT) {
734 error_report("unsupported bus type '%s'", value);
735 goto fail;
737 } else {
738 type = block_default_type;
741 /* Geometry */
742 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
743 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
744 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
746 if (cyls || heads || secs) {
747 if (cyls < 1) {
748 error_report("invalid physical cyls number");
749 goto fail;
751 if (heads < 1) {
752 error_report("invalid physical heads number");
753 goto fail;
755 if (secs < 1) {
756 error_report("invalid physical secs number");
757 goto fail;
761 translation = BIOS_ATA_TRANSLATION_AUTO;
762 value = qemu_opt_get(legacy_opts, "trans");
763 if (value != NULL) {
764 if (!cyls) {
765 error_report("'%s' trans must be used with cyls, heads and secs",
766 value);
767 goto fail;
769 if (!strcmp(value, "none")) {
770 translation = BIOS_ATA_TRANSLATION_NONE;
771 } else if (!strcmp(value, "lba")) {
772 translation = BIOS_ATA_TRANSLATION_LBA;
773 } else if (!strcmp(value, "auto")) {
774 translation = BIOS_ATA_TRANSLATION_AUTO;
775 } else {
776 error_report("'%s' invalid translation type", value);
777 goto fail;
781 if (media == MEDIA_CDROM) {
782 if (cyls || secs || heads) {
783 error_report("CHS can't be set with media=cdrom");
784 goto fail;
788 /* Device address specified by bus/unit or index.
789 * If none was specified, try to find the first free one. */
790 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
791 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
792 index = qemu_opt_get_number(legacy_opts, "index", -1);
794 max_devs = if_max_devs[type];
796 if (index != -1) {
797 if (bus_id != 0 || unit_id != -1) {
798 error_report("index cannot be used with bus and unit");
799 goto fail;
801 bus_id = drive_index_to_bus_id(type, index);
802 unit_id = drive_index_to_unit_id(type, index);
805 if (unit_id == -1) {
806 unit_id = 0;
807 while (drive_get(type, bus_id, unit_id) != NULL) {
808 unit_id++;
809 if (max_devs && unit_id >= max_devs) {
810 unit_id -= max_devs;
811 bus_id++;
816 if (max_devs && unit_id >= max_devs) {
817 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
818 goto fail;
821 if (drive_get(type, bus_id, unit_id) != NULL) {
822 error_report("drive with bus=%d, unit=%d (index=%d) exists",
823 bus_id, unit_id, index);
824 goto fail;
827 /* no id supplied -> create one */
828 if (qemu_opts_id(all_opts) == NULL) {
829 char *new_id;
830 const char *mediastr = "";
831 if (type == IF_IDE || type == IF_SCSI) {
832 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
834 if (max_devs) {
835 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
836 mediastr, unit_id);
837 } else {
838 new_id = g_strdup_printf("%s%s%i", if_name[type],
839 mediastr, unit_id);
841 qdict_put(bs_opts, "id", qstring_from_str(new_id));
842 g_free(new_id);
845 /* Add virtio block device */
846 devaddr = qemu_opt_get(legacy_opts, "addr");
847 if (devaddr && type != IF_VIRTIO) {
848 error_report("addr is not supported by this bus type");
849 goto fail;
852 if (type == IF_VIRTIO) {
853 QemuOpts *devopts;
854 devopts = qemu_opts_create_nofail(qemu_find_opts("device"));
855 if (arch_type == QEMU_ARCH_S390X) {
856 qemu_opt_set(devopts, "driver", "virtio-blk-s390");
857 } else {
858 qemu_opt_set(devopts, "driver", "virtio-blk-pci");
860 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"));
861 if (devaddr) {
862 qemu_opt_set(devopts, "addr", devaddr);
866 /* Actual block device init: Functionality shared with blockdev-add */
867 dinfo = blockdev_init(bs_opts, type, &local_err);
868 if (dinfo == NULL) {
869 if (error_is_set(&local_err)) {
870 qerror_report_err(local_err);
871 error_free(local_err);
873 goto fail;
874 } else {
875 assert(!error_is_set(&local_err));
878 /* Set legacy DriveInfo fields */
879 dinfo->enable_auto_del = true;
880 dinfo->opts = all_opts;
882 dinfo->cyls = cyls;
883 dinfo->heads = heads;
884 dinfo->secs = secs;
885 dinfo->trans = translation;
887 dinfo->bus = bus_id;
888 dinfo->unit = unit_id;
889 dinfo->devaddr = devaddr;
891 switch(type) {
892 case IF_IDE:
893 case IF_SCSI:
894 case IF_XEN:
895 case IF_NONE:
896 dinfo->media_cd = media == MEDIA_CDROM;
897 break;
898 default:
899 break;
902 fail:
903 qemu_opts_del(legacy_opts);
904 return dinfo;
907 void do_commit(Monitor *mon, const QDict *qdict)
909 const char *device = qdict_get_str(qdict, "device");
910 BlockDriverState *bs;
911 int ret;
913 if (!strcmp(device, "all")) {
914 ret = bdrv_commit_all();
915 } else {
916 bs = bdrv_find(device);
917 if (!bs) {
918 monitor_printf(mon, "Device '%s' not found\n", device);
919 return;
921 ret = bdrv_commit(bs);
923 if (ret < 0) {
924 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
925 strerror(-ret));
929 static void blockdev_do_action(int kind, void *data, Error **errp)
931 TransactionAction action;
932 TransactionActionList list;
934 action.kind = kind;
935 action.data = data;
936 list.value = &action;
937 list.next = NULL;
938 qmp_transaction(&list, errp);
941 void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
942 bool has_format, const char *format,
943 bool has_mode, enum NewImageMode mode,
944 Error **errp)
946 BlockdevSnapshot snapshot = {
947 .device = (char *) device,
948 .snapshot_file = (char *) snapshot_file,
949 .has_format = has_format,
950 .format = (char *) format,
951 .has_mode = has_mode,
952 .mode = mode,
954 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
955 &snapshot, errp);
958 void qmp_blockdev_snapshot_internal_sync(const char *device,
959 const char *name,
960 Error **errp)
962 BlockdevSnapshotInternal snapshot = {
963 .device = (char *) device,
964 .name = (char *) name
967 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
968 &snapshot, errp);
971 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
972 bool has_id,
973 const char *id,
974 bool has_name,
975 const char *name,
976 Error **errp)
978 BlockDriverState *bs = bdrv_find(device);
979 QEMUSnapshotInfo sn;
980 Error *local_err = NULL;
981 SnapshotInfo *info = NULL;
982 int ret;
984 if (!bs) {
985 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
986 return NULL;
989 if (!has_id) {
990 id = NULL;
993 if (!has_name) {
994 name = NULL;
997 if (!id && !name) {
998 error_setg(errp, "Name or id must be provided");
999 return NULL;
1002 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1003 if (error_is_set(&local_err)) {
1004 error_propagate(errp, local_err);
1005 return NULL;
1007 if (!ret) {
1008 error_setg(errp,
1009 "Snapshot with id '%s' and name '%s' does not exist on "
1010 "device '%s'",
1011 STR_OR_NULL(id), STR_OR_NULL(name), device);
1012 return NULL;
1015 bdrv_snapshot_delete(bs, id, name, &local_err);
1016 if (error_is_set(&local_err)) {
1017 error_propagate(errp, local_err);
1018 return NULL;
1021 info = g_malloc0(sizeof(SnapshotInfo));
1022 info->id = g_strdup(sn.id_str);
1023 info->name = g_strdup(sn.name);
1024 info->date_nsec = sn.date_nsec;
1025 info->date_sec = sn.date_sec;
1026 info->vm_state_size = sn.vm_state_size;
1027 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1028 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1030 return info;
1033 /* New and old BlockDriverState structs for group snapshots */
1035 typedef struct BlkTransactionState BlkTransactionState;
1037 /* Only prepare() may fail. In a single transaction, only one of commit() or
1038 abort() will be called, clean() will always be called if it present. */
1039 typedef struct BdrvActionOps {
1040 /* Size of state struct, in bytes. */
1041 size_t instance_size;
1042 /* Prepare the work, must NOT be NULL. */
1043 void (*prepare)(BlkTransactionState *common, Error **errp);
1044 /* Commit the changes, can be NULL. */
1045 void (*commit)(BlkTransactionState *common);
1046 /* Abort the changes on fail, can be NULL. */
1047 void (*abort)(BlkTransactionState *common);
1048 /* Clean up resource in the end, can be NULL. */
1049 void (*clean)(BlkTransactionState *common);
1050 } BdrvActionOps;
1053 * This structure must be arranged as first member in child type, assuming
1054 * that compiler will also arrange it to the same address with parent instance.
1055 * Later it will be used in free().
1057 struct BlkTransactionState {
1058 TransactionAction *action;
1059 const BdrvActionOps *ops;
1060 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
1063 /* internal snapshot private data */
1064 typedef struct InternalSnapshotState {
1065 BlkTransactionState common;
1066 BlockDriverState *bs;
1067 QEMUSnapshotInfo sn;
1068 } InternalSnapshotState;
1070 static void internal_snapshot_prepare(BlkTransactionState *common,
1071 Error **errp)
1073 const char *device;
1074 const char *name;
1075 BlockDriverState *bs;
1076 QEMUSnapshotInfo old_sn, *sn;
1077 bool ret;
1078 qemu_timeval tv;
1079 BlockdevSnapshotInternal *internal;
1080 InternalSnapshotState *state;
1081 int ret1;
1083 g_assert(common->action->kind ==
1084 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1085 internal = common->action->blockdev_snapshot_internal_sync;
1086 state = DO_UPCAST(InternalSnapshotState, common, common);
1088 /* 1. parse input */
1089 device = internal->device;
1090 name = internal->name;
1092 /* 2. check for validation */
1093 bs = bdrv_find(device);
1094 if (!bs) {
1095 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1096 return;
1099 if (!bdrv_is_inserted(bs)) {
1100 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1101 return;
1104 if (bdrv_is_read_only(bs)) {
1105 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1106 return;
1109 if (!bdrv_can_snapshot(bs)) {
1110 error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
1111 bs->drv->format_name, device, "internal snapshot");
1112 return;
1115 if (!strlen(name)) {
1116 error_setg(errp, "Name is empty");
1117 return;
1120 /* check whether a snapshot with name exist */
1121 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, errp);
1122 if (error_is_set(errp)) {
1123 return;
1124 } else if (ret) {
1125 error_setg(errp,
1126 "Snapshot with name '%s' already exists on device '%s'",
1127 name, device);
1128 return;
1131 /* 3. take the snapshot */
1132 sn = &state->sn;
1133 pstrcpy(sn->name, sizeof(sn->name), name);
1134 qemu_gettimeofday(&tv);
1135 sn->date_sec = tv.tv_sec;
1136 sn->date_nsec = tv.tv_usec * 1000;
1137 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1139 ret1 = bdrv_snapshot_create(bs, sn);
1140 if (ret1 < 0) {
1141 error_setg_errno(errp, -ret1,
1142 "Failed to create snapshot '%s' on device '%s'",
1143 name, device);
1144 return;
1147 /* 4. succeed, mark a snapshot is created */
1148 state->bs = bs;
1151 static void internal_snapshot_abort(BlkTransactionState *common)
1153 InternalSnapshotState *state =
1154 DO_UPCAST(InternalSnapshotState, common, common);
1155 BlockDriverState *bs = state->bs;
1156 QEMUSnapshotInfo *sn = &state->sn;
1157 Error *local_error = NULL;
1159 if (!bs) {
1160 return;
1163 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1164 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1165 "device '%s' in abort: %s",
1166 sn->id_str,
1167 sn->name,
1168 bdrv_get_device_name(bs),
1169 error_get_pretty(local_error));
1170 error_free(local_error);
1174 /* external snapshot private data */
1175 typedef struct ExternalSnapshotState {
1176 BlkTransactionState common;
1177 BlockDriverState *old_bs;
1178 BlockDriverState *new_bs;
1179 } ExternalSnapshotState;
1181 static void external_snapshot_prepare(BlkTransactionState *common,
1182 Error **errp)
1184 BlockDriver *drv;
1185 int flags, ret;
1186 Error *local_err = NULL;
1187 const char *device;
1188 const char *new_image_file;
1189 const char *format = "qcow2";
1190 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1191 ExternalSnapshotState *state =
1192 DO_UPCAST(ExternalSnapshotState, common, common);
1193 TransactionAction *action = common->action;
1195 /* get parameters */
1196 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1198 device = action->blockdev_snapshot_sync->device;
1199 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1200 if (action->blockdev_snapshot_sync->has_format) {
1201 format = action->blockdev_snapshot_sync->format;
1203 if (action->blockdev_snapshot_sync->has_mode) {
1204 mode = action->blockdev_snapshot_sync->mode;
1207 /* start processing */
1208 drv = bdrv_find_format(format);
1209 if (!drv) {
1210 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1211 return;
1214 state->old_bs = bdrv_find(device);
1215 if (!state->old_bs) {
1216 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1217 return;
1220 if (!bdrv_is_inserted(state->old_bs)) {
1221 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1222 return;
1225 if (bdrv_in_use(state->old_bs)) {
1226 error_set(errp, QERR_DEVICE_IN_USE, device);
1227 return;
1230 if (!bdrv_is_read_only(state->old_bs)) {
1231 if (bdrv_flush(state->old_bs)) {
1232 error_set(errp, QERR_IO_ERROR);
1233 return;
1237 if (bdrv_check_ext_snapshot(state->old_bs) != EXT_SNAPSHOT_ALLOWED) {
1238 error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
1239 return;
1242 flags = state->old_bs->open_flags;
1244 /* create new image w/backing file */
1245 if (mode != NEW_IMAGE_MODE_EXISTING) {
1246 bdrv_img_create(new_image_file, format,
1247 state->old_bs->filename,
1248 state->old_bs->drv->format_name,
1249 NULL, -1, flags, &local_err, false);
1250 if (error_is_set(&local_err)) {
1251 error_propagate(errp, local_err);
1252 return;
1256 /* We will manually add the backing_hd field to the bs later */
1257 state->new_bs = bdrv_new("");
1258 /* TODO Inherit bs->options or only take explicit options with an
1259 * extended QMP command? */
1260 ret = bdrv_open(state->new_bs, new_image_file, NULL,
1261 flags | BDRV_O_NO_BACKING, drv, &local_err);
1262 if (ret != 0) {
1263 error_propagate(errp, local_err);
1267 static void external_snapshot_commit(BlkTransactionState *common)
1269 ExternalSnapshotState *state =
1270 DO_UPCAST(ExternalSnapshotState, common, common);
1272 /* This removes our old bs and adds the new bs */
1273 bdrv_append(state->new_bs, state->old_bs);
1274 /* We don't need (or want) to use the transactional
1275 * bdrv_reopen_multiple() across all the entries at once, because we
1276 * don't want to abort all of them if one of them fails the reopen */
1277 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
1278 NULL);
1281 static void external_snapshot_abort(BlkTransactionState *common)
1283 ExternalSnapshotState *state =
1284 DO_UPCAST(ExternalSnapshotState, common, common);
1285 if (state->new_bs) {
1286 bdrv_unref(state->new_bs);
1290 typedef struct DriveBackupState {
1291 BlkTransactionState common;
1292 BlockDriverState *bs;
1293 BlockJob *job;
1294 } DriveBackupState;
1296 static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1298 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1299 DriveBackup *backup;
1300 Error *local_err = NULL;
1302 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1303 backup = common->action->drive_backup;
1305 qmp_drive_backup(backup->device, backup->target,
1306 backup->has_format, backup->format,
1307 backup->sync,
1308 backup->has_mode, backup->mode,
1309 backup->has_speed, backup->speed,
1310 backup->has_on_source_error, backup->on_source_error,
1311 backup->has_on_target_error, backup->on_target_error,
1312 &local_err);
1313 if (error_is_set(&local_err)) {
1314 error_propagate(errp, local_err);
1315 state->bs = NULL;
1316 state->job = NULL;
1317 return;
1320 state->bs = bdrv_find(backup->device);
1321 state->job = state->bs->job;
1324 static void drive_backup_abort(BlkTransactionState *common)
1326 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1327 BlockDriverState *bs = state->bs;
1329 /* Only cancel if it's the job we started */
1330 if (bs && bs->job && bs->job == state->job) {
1331 block_job_cancel_sync(bs->job);
1335 static void abort_prepare(BlkTransactionState *common, Error **errp)
1337 error_setg(errp, "Transaction aborted using Abort action");
1340 static void abort_commit(BlkTransactionState *common)
1342 g_assert_not_reached(); /* this action never succeeds */
1345 static const BdrvActionOps actions[] = {
1346 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1347 .instance_size = sizeof(ExternalSnapshotState),
1348 .prepare = external_snapshot_prepare,
1349 .commit = external_snapshot_commit,
1350 .abort = external_snapshot_abort,
1352 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1353 .instance_size = sizeof(DriveBackupState),
1354 .prepare = drive_backup_prepare,
1355 .abort = drive_backup_abort,
1357 [TRANSACTION_ACTION_KIND_ABORT] = {
1358 .instance_size = sizeof(BlkTransactionState),
1359 .prepare = abort_prepare,
1360 .commit = abort_commit,
1362 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1363 .instance_size = sizeof(InternalSnapshotState),
1364 .prepare = internal_snapshot_prepare,
1365 .abort = internal_snapshot_abort,
1370 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1371 * then we do not pivot any of the devices in the group, and abandon the
1372 * snapshots
1374 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1376 TransactionActionList *dev_entry = dev_list;
1377 BlkTransactionState *state, *next;
1378 Error *local_err = NULL;
1380 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
1381 QSIMPLEQ_INIT(&snap_bdrv_states);
1383 /* drain all i/o before any snapshots */
1384 bdrv_drain_all();
1386 /* We don't do anything in this loop that commits us to the snapshot */
1387 while (NULL != dev_entry) {
1388 TransactionAction *dev_info = NULL;
1389 const BdrvActionOps *ops;
1391 dev_info = dev_entry->value;
1392 dev_entry = dev_entry->next;
1394 assert(dev_info->kind < ARRAY_SIZE(actions));
1396 ops = &actions[dev_info->kind];
1397 assert(ops->instance_size > 0);
1399 state = g_malloc0(ops->instance_size);
1400 state->ops = ops;
1401 state->action = dev_info;
1402 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
1404 state->ops->prepare(state, &local_err);
1405 if (error_is_set(&local_err)) {
1406 error_propagate(errp, local_err);
1407 goto delete_and_fail;
1411 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1412 if (state->ops->commit) {
1413 state->ops->commit(state);
1417 /* success */
1418 goto exit;
1420 delete_and_fail:
1422 * failure, and it is all-or-none; abandon each new bs, and keep using
1423 * the original bs for all images
1425 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1426 if (state->ops->abort) {
1427 state->ops->abort(state);
1430 exit:
1431 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1432 if (state->ops->clean) {
1433 state->ops->clean(state);
1435 g_free(state);
1440 static void eject_device(BlockDriverState *bs, int force, Error **errp)
1442 if (bdrv_in_use(bs)) {
1443 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
1444 return;
1446 if (!bdrv_dev_has_removable_media(bs)) {
1447 error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
1448 return;
1451 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
1452 bdrv_dev_eject_request(bs, force);
1453 if (!force) {
1454 error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
1455 return;
1459 bdrv_close(bs);
1462 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1464 BlockDriverState *bs;
1466 bs = bdrv_find(device);
1467 if (!bs) {
1468 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1469 return;
1472 eject_device(bs, force, errp);
1475 void qmp_block_passwd(const char *device, const char *password, Error **errp)
1477 BlockDriverState *bs;
1478 int err;
1480 bs = bdrv_find(device);
1481 if (!bs) {
1482 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1483 return;
1486 err = bdrv_set_key(bs, password);
1487 if (err == -EINVAL) {
1488 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1489 return;
1490 } else if (err < 0) {
1491 error_set(errp, QERR_INVALID_PASSWORD);
1492 return;
1496 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1497 int bdrv_flags, BlockDriver *drv,
1498 const char *password, Error **errp)
1500 Error *local_err = NULL;
1501 int ret;
1503 ret = bdrv_open(bs, filename, NULL, bdrv_flags, drv, &local_err);
1504 if (ret < 0) {
1505 error_propagate(errp, local_err);
1506 return;
1509 if (bdrv_key_required(bs)) {
1510 if (password) {
1511 if (bdrv_set_key(bs, password) < 0) {
1512 error_set(errp, QERR_INVALID_PASSWORD);
1514 } else {
1515 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
1516 bdrv_get_encrypted_filename(bs));
1518 } else if (password) {
1519 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1523 void qmp_change_blockdev(const char *device, const char *filename,
1524 bool has_format, const char *format, Error **errp)
1526 BlockDriverState *bs;
1527 BlockDriver *drv = NULL;
1528 int bdrv_flags;
1529 Error *err = NULL;
1531 bs = bdrv_find(device);
1532 if (!bs) {
1533 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1534 return;
1537 if (format) {
1538 drv = bdrv_find_whitelisted_format(format, bs->read_only);
1539 if (!drv) {
1540 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1541 return;
1545 eject_device(bs, 0, &err);
1546 if (error_is_set(&err)) {
1547 error_propagate(errp, err);
1548 return;
1551 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1552 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1554 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1557 /* throttling disk I/O limits */
1558 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1559 int64_t bps_wr,
1560 int64_t iops,
1561 int64_t iops_rd,
1562 int64_t iops_wr,
1563 bool has_bps_max,
1564 int64_t bps_max,
1565 bool has_bps_rd_max,
1566 int64_t bps_rd_max,
1567 bool has_bps_wr_max,
1568 int64_t bps_wr_max,
1569 bool has_iops_max,
1570 int64_t iops_max,
1571 bool has_iops_rd_max,
1572 int64_t iops_rd_max,
1573 bool has_iops_wr_max,
1574 int64_t iops_wr_max,
1575 bool has_iops_size,
1576 int64_t iops_size, Error **errp)
1578 ThrottleConfig cfg;
1579 BlockDriverState *bs;
1581 bs = bdrv_find(device);
1582 if (!bs) {
1583 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1584 return;
1587 memset(&cfg, 0, sizeof(cfg));
1588 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1589 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1590 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1592 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1593 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
1594 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1596 if (has_bps_max) {
1597 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
1599 if (has_bps_rd_max) {
1600 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
1602 if (has_bps_wr_max) {
1603 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
1605 if (has_iops_max) {
1606 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
1608 if (has_iops_rd_max) {
1609 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
1611 if (has_iops_wr_max) {
1612 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
1615 if (has_iops_size) {
1616 cfg.op_size = iops_size;
1619 if (!check_throttle_config(&cfg, errp)) {
1620 return;
1623 if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
1624 bdrv_io_limits_enable(bs);
1625 } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
1626 bdrv_io_limits_disable(bs);
1629 if (bs->io_limits_enabled) {
1630 bdrv_set_io_limits(bs, &cfg);
1634 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1636 const char *id = qdict_get_str(qdict, "id");
1637 BlockDriverState *bs;
1639 bs = bdrv_find(id);
1640 if (!bs) {
1641 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1642 return -1;
1644 if (bdrv_in_use(bs)) {
1645 qerror_report(QERR_DEVICE_IN_USE, id);
1646 return -1;
1649 /* quiesce block driver; prevent further io */
1650 bdrv_drain_all();
1651 bdrv_flush(bs);
1652 bdrv_close(bs);
1654 /* if we have a device attached to this BlockDriverState
1655 * then we need to make the drive anonymous until the device
1656 * can be removed. If this is a drive with no device backing
1657 * then we can just get rid of the block driver state right here.
1659 if (bdrv_get_attached_dev(bs)) {
1660 bdrv_make_anon(bs);
1662 /* Further I/O must not pause the guest */
1663 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
1664 BLOCKDEV_ON_ERROR_REPORT);
1665 } else {
1666 drive_uninit(drive_get_by_blockdev(bs));
1669 return 0;
1672 void qmp_block_resize(const char *device, int64_t size, Error **errp)
1674 BlockDriverState *bs;
1675 int ret;
1677 bs = bdrv_find(device);
1678 if (!bs) {
1679 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1680 return;
1683 if (size < 0) {
1684 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
1685 return;
1688 /* complete all in-flight operations before resizing the device */
1689 bdrv_drain_all();
1691 ret = bdrv_truncate(bs, size);
1692 switch (ret) {
1693 case 0:
1694 break;
1695 case -ENOMEDIUM:
1696 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1697 break;
1698 case -ENOTSUP:
1699 error_set(errp, QERR_UNSUPPORTED);
1700 break;
1701 case -EACCES:
1702 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1703 break;
1704 case -EBUSY:
1705 error_set(errp, QERR_DEVICE_IN_USE, device);
1706 break;
1707 default:
1708 error_setg_errno(errp, -ret, "Could not resize");
1709 break;
1713 static void block_job_cb(void *opaque, int ret)
1715 BlockDriverState *bs = opaque;
1716 QObject *obj;
1718 trace_block_job_cb(bs, bs->job, ret);
1720 assert(bs->job);
1721 obj = qobject_from_block_job(bs->job);
1722 if (ret < 0) {
1723 QDict *dict = qobject_to_qdict(obj);
1724 qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
1727 if (block_job_is_cancelled(bs->job)) {
1728 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1729 } else {
1730 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1732 qobject_decref(obj);
1734 bdrv_put_ref_bh_schedule(bs);
1737 void qmp_block_stream(const char *device, bool has_base,
1738 const char *base, bool has_speed, int64_t speed,
1739 bool has_on_error, BlockdevOnError on_error,
1740 Error **errp)
1742 BlockDriverState *bs;
1743 BlockDriverState *base_bs = NULL;
1744 Error *local_err = NULL;
1746 if (!has_on_error) {
1747 on_error = BLOCKDEV_ON_ERROR_REPORT;
1750 bs = bdrv_find(device);
1751 if (!bs) {
1752 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1753 return;
1756 if (base) {
1757 base_bs = bdrv_find_backing_image(bs, base);
1758 if (base_bs == NULL) {
1759 error_set(errp, QERR_BASE_NOT_FOUND, base);
1760 return;
1764 stream_start(bs, base_bs, base, has_speed ? speed : 0,
1765 on_error, block_job_cb, bs, &local_err);
1766 if (error_is_set(&local_err)) {
1767 error_propagate(errp, local_err);
1768 return;
1771 trace_qmp_block_stream(bs, bs->job);
1774 void qmp_block_commit(const char *device,
1775 bool has_base, const char *base, const char *top,
1776 bool has_speed, int64_t speed,
1777 Error **errp)
1779 BlockDriverState *bs;
1780 BlockDriverState *base_bs, *top_bs;
1781 Error *local_err = NULL;
1782 /* This will be part of the QMP command, if/when the
1783 * BlockdevOnError change for blkmirror makes it in
1785 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
1787 /* drain all i/o before commits */
1788 bdrv_drain_all();
1790 bs = bdrv_find(device);
1791 if (!bs) {
1792 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1793 return;
1796 /* default top_bs is the active layer */
1797 top_bs = bs;
1799 if (top) {
1800 if (strcmp(bs->filename, top) != 0) {
1801 top_bs = bdrv_find_backing_image(bs, top);
1805 if (top_bs == NULL) {
1806 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1807 return;
1810 if (has_base && base) {
1811 base_bs = bdrv_find_backing_image(top_bs, base);
1812 } else {
1813 base_bs = bdrv_find_base(top_bs);
1816 if (base_bs == NULL) {
1817 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1818 return;
1821 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1822 &local_err);
1823 if (local_err != NULL) {
1824 error_propagate(errp, local_err);
1825 return;
1829 void qmp_drive_backup(const char *device, const char *target,
1830 bool has_format, const char *format,
1831 enum MirrorSyncMode sync,
1832 bool has_mode, enum NewImageMode mode,
1833 bool has_speed, int64_t speed,
1834 bool has_on_source_error, BlockdevOnError on_source_error,
1835 bool has_on_target_error, BlockdevOnError on_target_error,
1836 Error **errp)
1838 BlockDriverState *bs;
1839 BlockDriverState *target_bs;
1840 BlockDriverState *source = NULL;
1841 BlockDriver *drv = NULL;
1842 Error *local_err = NULL;
1843 int flags;
1844 int64_t size;
1845 int ret;
1847 if (!has_speed) {
1848 speed = 0;
1850 if (!has_on_source_error) {
1851 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1853 if (!has_on_target_error) {
1854 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1856 if (!has_mode) {
1857 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1860 bs = bdrv_find(device);
1861 if (!bs) {
1862 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1863 return;
1866 if (!bdrv_is_inserted(bs)) {
1867 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1868 return;
1871 if (!has_format) {
1872 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1874 if (format) {
1875 drv = bdrv_find_format(format);
1876 if (!drv) {
1877 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1878 return;
1882 if (bdrv_in_use(bs)) {
1883 error_set(errp, QERR_DEVICE_IN_USE, device);
1884 return;
1887 flags = bs->open_flags | BDRV_O_RDWR;
1889 /* See if we have a backing HD we can use to create our new image
1890 * on top of. */
1891 if (sync == MIRROR_SYNC_MODE_TOP) {
1892 source = bs->backing_hd;
1893 if (!source) {
1894 sync = MIRROR_SYNC_MODE_FULL;
1897 if (sync == MIRROR_SYNC_MODE_NONE) {
1898 source = bs;
1901 size = bdrv_getlength(bs);
1902 if (size < 0) {
1903 error_setg_errno(errp, -size, "bdrv_getlength failed");
1904 return;
1907 if (mode != NEW_IMAGE_MODE_EXISTING) {
1908 assert(format && drv);
1909 if (source) {
1910 bdrv_img_create(target, format, source->filename,
1911 source->drv->format_name, NULL,
1912 size, flags, &local_err, false);
1913 } else {
1914 bdrv_img_create(target, format, NULL, NULL, NULL,
1915 size, flags, &local_err, false);
1919 if (error_is_set(&local_err)) {
1920 error_propagate(errp, local_err);
1921 return;
1924 target_bs = bdrv_new("");
1925 ret = bdrv_open(target_bs, target, NULL, flags, drv, &local_err);
1926 if (ret < 0) {
1927 bdrv_unref(target_bs);
1928 error_propagate(errp, local_err);
1929 return;
1932 backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
1933 block_job_cb, bs, &local_err);
1934 if (local_err != NULL) {
1935 bdrv_unref(target_bs);
1936 error_propagate(errp, local_err);
1937 return;
1941 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1943 void qmp_drive_mirror(const char *device, const char *target,
1944 bool has_format, const char *format,
1945 enum MirrorSyncMode sync,
1946 bool has_mode, enum NewImageMode mode,
1947 bool has_speed, int64_t speed,
1948 bool has_granularity, uint32_t granularity,
1949 bool has_buf_size, int64_t buf_size,
1950 bool has_on_source_error, BlockdevOnError on_source_error,
1951 bool has_on_target_error, BlockdevOnError on_target_error,
1952 Error **errp)
1954 BlockDriverState *bs;
1955 BlockDriverState *source, *target_bs;
1956 BlockDriver *drv = NULL;
1957 Error *local_err = NULL;
1958 int flags;
1959 int64_t size;
1960 int ret;
1962 if (!has_speed) {
1963 speed = 0;
1965 if (!has_on_source_error) {
1966 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1968 if (!has_on_target_error) {
1969 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1971 if (!has_mode) {
1972 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1974 if (!has_granularity) {
1975 granularity = 0;
1977 if (!has_buf_size) {
1978 buf_size = DEFAULT_MIRROR_BUF_SIZE;
1981 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1982 error_set(errp, QERR_INVALID_PARAMETER, device);
1983 return;
1985 if (granularity & (granularity - 1)) {
1986 error_set(errp, QERR_INVALID_PARAMETER, device);
1987 return;
1990 bs = bdrv_find(device);
1991 if (!bs) {
1992 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1993 return;
1996 if (!bdrv_is_inserted(bs)) {
1997 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1998 return;
2001 if (!has_format) {
2002 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2004 if (format) {
2005 drv = bdrv_find_format(format);
2006 if (!drv) {
2007 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
2008 return;
2012 if (bdrv_in_use(bs)) {
2013 error_set(errp, QERR_DEVICE_IN_USE, device);
2014 return;
2017 flags = bs->open_flags | BDRV_O_RDWR;
2018 source = bs->backing_hd;
2019 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
2020 sync = MIRROR_SYNC_MODE_FULL;
2023 size = bdrv_getlength(bs);
2024 if (size < 0) {
2025 error_setg_errno(errp, -size, "bdrv_getlength failed");
2026 return;
2029 if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
2030 /* create new image w/o backing file */
2031 assert(format && drv);
2032 bdrv_img_create(target, format,
2033 NULL, NULL, NULL, size, flags, &local_err, false);
2034 } else {
2035 switch (mode) {
2036 case NEW_IMAGE_MODE_EXISTING:
2037 break;
2038 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2039 /* create new image with backing file */
2040 bdrv_img_create(target, format,
2041 source->filename,
2042 source->drv->format_name,
2043 NULL, size, flags, &local_err, false);
2044 break;
2045 default:
2046 abort();
2050 if (error_is_set(&local_err)) {
2051 error_propagate(errp, local_err);
2052 return;
2055 /* Mirroring takes care of copy-on-write using the source's backing
2056 * file.
2058 target_bs = bdrv_new("");
2059 ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv,
2060 &local_err);
2061 if (ret < 0) {
2062 bdrv_unref(target_bs);
2063 error_propagate(errp, local_err);
2064 return;
2067 mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
2068 on_source_error, on_target_error,
2069 block_job_cb, bs, &local_err);
2070 if (local_err != NULL) {
2071 bdrv_unref(target_bs);
2072 error_propagate(errp, local_err);
2073 return;
2077 static BlockJob *find_block_job(const char *device)
2079 BlockDriverState *bs;
2081 bs = bdrv_find(device);
2082 if (!bs || !bs->job) {
2083 return NULL;
2085 return bs->job;
2088 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2090 BlockJob *job = find_block_job(device);
2092 if (!job) {
2093 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2094 return;
2097 block_job_set_speed(job, speed, errp);
2100 void qmp_block_job_cancel(const char *device,
2101 bool has_force, bool force, Error **errp)
2103 BlockJob *job = find_block_job(device);
2105 if (!has_force) {
2106 force = false;
2109 if (!job) {
2110 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2111 return;
2113 if (job->paused && !force) {
2114 error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
2115 return;
2118 trace_qmp_block_job_cancel(job);
2119 block_job_cancel(job);
2122 void qmp_block_job_pause(const char *device, Error **errp)
2124 BlockJob *job = find_block_job(device);
2126 if (!job) {
2127 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2128 return;
2131 trace_qmp_block_job_pause(job);
2132 block_job_pause(job);
2135 void qmp_block_job_resume(const char *device, Error **errp)
2137 BlockJob *job = find_block_job(device);
2139 if (!job) {
2140 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2141 return;
2144 trace_qmp_block_job_resume(job);
2145 block_job_resume(job);
2148 void qmp_block_job_complete(const char *device, Error **errp)
2150 BlockJob *job = find_block_job(device);
2152 if (!job) {
2153 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2154 return;
2157 trace_qmp_block_job_complete(job);
2158 block_job_complete(job, errp);
2161 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
2163 QmpOutputVisitor *ov = qmp_output_visitor_new();
2164 QObject *obj;
2165 QDict *qdict;
2166 Error *local_err = NULL;
2168 /* Require an ID in the top level */
2169 if (!options->has_id) {
2170 error_setg(errp, "Block device needs an ID");
2171 goto fail;
2174 /* TODO Sort it out in raw-posix and drive_init: Reject aio=native with
2175 * cache.direct=false instead of silently switching to aio=threads, except
2176 * if called from drive_init.
2178 * For now, simply forbidding the combination for all drivers will do. */
2179 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
2180 bool direct = options->cache->has_direct && options->cache->direct;
2181 if (!options->has_cache && !direct) {
2182 error_setg(errp, "aio=native requires cache.direct=true");
2183 goto fail;
2187 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
2188 &options, NULL, &local_err);
2189 if (error_is_set(&local_err)) {
2190 error_propagate(errp, local_err);
2191 goto fail;
2194 obj = qmp_output_get_qobject(ov);
2195 qdict = qobject_to_qdict(obj);
2197 qdict_flatten(qdict);
2199 blockdev_init(qdict, IF_NONE, &local_err);
2200 if (error_is_set(&local_err)) {
2201 error_propagate(errp, local_err);
2202 goto fail;
2205 fail:
2206 qmp_output_visitor_cleanup(ov);
2209 static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
2211 BlockJobInfoList **prev = opaque;
2212 BlockJob *job = bs->job;
2214 if (job) {
2215 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
2216 elem->value = block_job_query(bs->job);
2217 (*prev)->next = elem;
2218 *prev = elem;
2222 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
2224 /* Dummy is a fake list element for holding the head pointer */
2225 BlockJobInfoList dummy = {};
2226 BlockJobInfoList *prev = &dummy;
2227 bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
2228 return dummy.next;
2231 QemuOptsList qemu_common_drive_opts = {
2232 .name = "drive",
2233 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
2234 .desc = {
2236 .name = "snapshot",
2237 .type = QEMU_OPT_BOOL,
2238 .help = "enable/disable snapshot mode",
2240 .name = "file",
2241 .type = QEMU_OPT_STRING,
2242 .help = "disk image",
2244 .name = "discard",
2245 .type = QEMU_OPT_STRING,
2246 .help = "discard operation (ignore/off, unmap/on)",
2248 .name = "cache.writeback",
2249 .type = QEMU_OPT_BOOL,
2250 .help = "enables writeback mode for any caches",
2252 .name = "cache.direct",
2253 .type = QEMU_OPT_BOOL,
2254 .help = "enables use of O_DIRECT (bypass the host page cache)",
2256 .name = "cache.no-flush",
2257 .type = QEMU_OPT_BOOL,
2258 .help = "ignore any flush requests for the device",
2260 .name = "aio",
2261 .type = QEMU_OPT_STRING,
2262 .help = "host AIO implementation (threads, native)",
2264 .name = "format",
2265 .type = QEMU_OPT_STRING,
2266 .help = "disk format (raw, qcow2, ...)",
2268 .name = "serial",
2269 .type = QEMU_OPT_STRING,
2270 .help = "disk serial number",
2272 .name = "rerror",
2273 .type = QEMU_OPT_STRING,
2274 .help = "read error action",
2276 .name = "werror",
2277 .type = QEMU_OPT_STRING,
2278 .help = "write error action",
2280 .name = "read-only",
2281 .type = QEMU_OPT_BOOL,
2282 .help = "open drive file as read-only",
2284 .name = "throttling.iops-total",
2285 .type = QEMU_OPT_NUMBER,
2286 .help = "limit total I/O operations per second",
2288 .name = "throttling.iops-read",
2289 .type = QEMU_OPT_NUMBER,
2290 .help = "limit read operations per second",
2292 .name = "throttling.iops-write",
2293 .type = QEMU_OPT_NUMBER,
2294 .help = "limit write operations per second",
2296 .name = "throttling.bps-total",
2297 .type = QEMU_OPT_NUMBER,
2298 .help = "limit total bytes per second",
2300 .name = "throttling.bps-read",
2301 .type = QEMU_OPT_NUMBER,
2302 .help = "limit read bytes per second",
2304 .name = "throttling.bps-write",
2305 .type = QEMU_OPT_NUMBER,
2306 .help = "limit write bytes per second",
2308 .name = "throttling.iops-total-max",
2309 .type = QEMU_OPT_NUMBER,
2310 .help = "I/O operations burst",
2312 .name = "throttling.iops-read-max",
2313 .type = QEMU_OPT_NUMBER,
2314 .help = "I/O operations read burst",
2316 .name = "throttling.iops-write-max",
2317 .type = QEMU_OPT_NUMBER,
2318 .help = "I/O operations write burst",
2320 .name = "throttling.bps-total-max",
2321 .type = QEMU_OPT_NUMBER,
2322 .help = "total bytes burst",
2324 .name = "throttling.bps-read-max",
2325 .type = QEMU_OPT_NUMBER,
2326 .help = "total bytes read burst",
2328 .name = "throttling.bps-write-max",
2329 .type = QEMU_OPT_NUMBER,
2330 .help = "total bytes write burst",
2332 .name = "throttling.iops-size",
2333 .type = QEMU_OPT_NUMBER,
2334 .help = "when limiting by iops max size of an I/O in bytes",
2336 .name = "copy-on-read",
2337 .type = QEMU_OPT_BOOL,
2338 .help = "copy read data from backing file into image file",
2340 { /* end of list */ }
2344 QemuOptsList qemu_drive_opts = {
2345 .name = "drive",
2346 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
2347 .desc = {
2349 * no elements => accept any params
2350 * validation will happen later
2352 { /* end of list */ }