target-mips: fix decoding of microMIPS POOL32Axf instructions
[qemu/ar7.git] / blockdev.c
blob41b0a49344f5b5da34ca87552f50f23c58d361b1
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 "sysemu/sysemu.h"
42 #include "block/block_int.h"
43 #include "qmp-commands.h"
44 #include "trace.h"
45 #include "sysemu/arch_init.h"
47 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
48 extern QemuOptsList qemu_common_drive_opts;
49 extern QemuOptsList qemu_old_drive_opts;
51 static const char *const if_name[IF_COUNT] = {
52 [IF_NONE] = "none",
53 [IF_IDE] = "ide",
54 [IF_SCSI] = "scsi",
55 [IF_FLOPPY] = "floppy",
56 [IF_PFLASH] = "pflash",
57 [IF_MTD] = "mtd",
58 [IF_SD] = "sd",
59 [IF_VIRTIO] = "virtio",
60 [IF_XEN] = "xen",
63 static const int if_max_devs[IF_COUNT] = {
65 * Do not change these numbers! They govern how drive option
66 * index maps to unit and bus. That mapping is ABI.
68 * All controllers used to imlement if=T drives need to support
69 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
70 * Otherwise, some index values map to "impossible" bus, unit
71 * values.
73 * For instance, if you change [IF_SCSI] to 255, -drive
74 * if=scsi,index=12 no longer means bus=1,unit=5, but
75 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
76 * the drive can't be set up. Regression.
78 [IF_IDE] = 2,
79 [IF_SCSI] = 7,
83 * We automatically delete the drive when a device using it gets
84 * unplugged. Questionable feature, but we can't just drop it.
85 * Device models call blockdev_mark_auto_del() to schedule the
86 * automatic deletion, and generic qdev code calls blockdev_auto_del()
87 * when deletion is actually safe.
89 void blockdev_mark_auto_del(BlockDriverState *bs)
91 DriveInfo *dinfo = drive_get_by_blockdev(bs);
93 if (bs->job) {
94 block_job_cancel(bs->job);
96 if (dinfo) {
97 dinfo->auto_del = 1;
101 void blockdev_auto_del(BlockDriverState *bs)
103 DriveInfo *dinfo = drive_get_by_blockdev(bs);
105 if (dinfo && dinfo->auto_del) {
106 drive_put_ref(dinfo);
110 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
112 int max_devs = if_max_devs[type];
113 return max_devs ? index / max_devs : 0;
116 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
118 int max_devs = if_max_devs[type];
119 return max_devs ? index % max_devs : index;
122 QemuOpts *drive_def(const char *optstr)
124 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
127 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
128 const char *optstr)
130 QemuOpts *opts;
131 char buf[32];
133 opts = drive_def(optstr);
134 if (!opts) {
135 return NULL;
137 if (type != IF_DEFAULT) {
138 qemu_opt_set(opts, "if", if_name[type]);
140 if (index >= 0) {
141 snprintf(buf, sizeof(buf), "%d", index);
142 qemu_opt_set(opts, "index", buf);
144 if (file)
145 qemu_opt_set(opts, "file", file);
146 return opts;
149 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
151 DriveInfo *dinfo;
153 /* seek interface, bus and unit */
155 QTAILQ_FOREACH(dinfo, &drives, next) {
156 if (dinfo->type == type &&
157 dinfo->bus == bus &&
158 dinfo->unit == unit)
159 return dinfo;
162 return NULL;
165 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
167 return drive_get(type,
168 drive_index_to_bus_id(type, index),
169 drive_index_to_unit_id(type, index));
172 int drive_get_max_bus(BlockInterfaceType type)
174 int max_bus;
175 DriveInfo *dinfo;
177 max_bus = -1;
178 QTAILQ_FOREACH(dinfo, &drives, next) {
179 if(dinfo->type == type &&
180 dinfo->bus > max_bus)
181 max_bus = dinfo->bus;
183 return max_bus;
186 /* Get a block device. This should only be used for single-drive devices
187 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
188 appropriate bus. */
189 DriveInfo *drive_get_next(BlockInterfaceType type)
191 static int next_block_unit[IF_COUNT];
193 return drive_get(type, 0, next_block_unit[type]++);
196 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
198 DriveInfo *dinfo;
200 QTAILQ_FOREACH(dinfo, &drives, next) {
201 if (dinfo->bdrv == bs) {
202 return dinfo;
205 return NULL;
208 static void bdrv_format_print(void *opaque, const char *name)
210 error_printf(" %s", name);
213 static void drive_uninit(DriveInfo *dinfo)
215 qemu_opts_del(dinfo->opts);
216 bdrv_delete(dinfo->bdrv);
217 g_free(dinfo->id);
218 QTAILQ_REMOVE(&drives, dinfo, next);
219 g_free(dinfo->serial);
220 g_free(dinfo);
223 void drive_put_ref(DriveInfo *dinfo)
225 assert(dinfo->refcount);
226 if (--dinfo->refcount == 0) {
227 drive_uninit(dinfo);
231 void drive_get_ref(DriveInfo *dinfo)
233 dinfo->refcount++;
236 typedef struct {
237 QEMUBH *bh;
238 DriveInfo *dinfo;
239 } DrivePutRefBH;
241 static void drive_put_ref_bh(void *opaque)
243 DrivePutRefBH *s = opaque;
245 drive_put_ref(s->dinfo);
246 qemu_bh_delete(s->bh);
247 g_free(s);
251 * Release a drive reference in a BH
253 * It is not possible to use drive_put_ref() from a callback function when the
254 * callers still need the drive. In such cases we schedule a BH to release the
255 * reference.
257 static void drive_put_ref_bh_schedule(DriveInfo *dinfo)
259 DrivePutRefBH *s;
261 s = g_new(DrivePutRefBH, 1);
262 s->bh = qemu_bh_new(drive_put_ref_bh, s);
263 s->dinfo = dinfo;
264 qemu_bh_schedule(s->bh);
267 static int parse_block_error_action(const char *buf, bool is_read)
269 if (!strcmp(buf, "ignore")) {
270 return BLOCKDEV_ON_ERROR_IGNORE;
271 } else if (!is_read && !strcmp(buf, "enospc")) {
272 return BLOCKDEV_ON_ERROR_ENOSPC;
273 } else if (!strcmp(buf, "stop")) {
274 return BLOCKDEV_ON_ERROR_STOP;
275 } else if (!strcmp(buf, "report")) {
276 return BLOCKDEV_ON_ERROR_REPORT;
277 } else {
278 error_report("'%s' invalid %s error action",
279 buf, is_read ? "read" : "write");
280 return -1;
284 static bool do_check_io_limits(BlockIOLimit *io_limits, Error **errp)
286 bool bps_flag;
287 bool iops_flag;
289 assert(io_limits);
291 bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
292 && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
293 || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
294 iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
295 && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
296 || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
297 if (bps_flag || iops_flag) {
298 error_setg(errp, "bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
299 "cannot be used at the same time");
300 return false;
303 if (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] < 0 ||
304 io_limits->bps[BLOCK_IO_LIMIT_WRITE] < 0 ||
305 io_limits->bps[BLOCK_IO_LIMIT_READ] < 0 ||
306 io_limits->iops[BLOCK_IO_LIMIT_TOTAL] < 0 ||
307 io_limits->iops[BLOCK_IO_LIMIT_WRITE] < 0 ||
308 io_limits->iops[BLOCK_IO_LIMIT_READ] < 0) {
309 error_setg(errp, "bps and iops values must be 0 or greater");
310 return false;
313 return true;
316 static DriveInfo *blockdev_init(QemuOpts *all_opts,
317 BlockInterfaceType block_default_type)
319 const char *buf;
320 const char *file = NULL;
321 const char *serial;
322 const char *mediastr = "";
323 BlockInterfaceType type;
324 enum { MEDIA_DISK, MEDIA_CDROM } media;
325 int bus_id, unit_id;
326 int cyls, heads, secs, translation;
327 int max_devs;
328 int index;
329 int ro = 0;
330 int bdrv_flags = 0;
331 int on_read_error, on_write_error;
332 const char *devaddr;
333 DriveInfo *dinfo;
334 BlockIOLimit io_limits;
335 int snapshot = 0;
336 bool copy_on_read;
337 int ret;
338 Error *error = NULL;
339 QemuOpts *opts;
340 QDict *bs_opts;
341 const char *id;
342 bool has_driver_specific_opts;
344 translation = BIOS_ATA_TRANSLATION_AUTO;
345 media = MEDIA_DISK;
347 /* Check common options by copying from all_opts to opts, all other options
348 * are stored in bs_opts. */
349 id = qemu_opts_id(all_opts);
350 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
351 if (error_is_set(&error)) {
352 qerror_report_err(error);
353 error_free(error);
354 return NULL;
357 bs_opts = qdict_new();
358 qemu_opts_to_qdict(all_opts, bs_opts);
359 qemu_opts_absorb_qdict(opts, bs_opts, &error);
360 if (error_is_set(&error)) {
361 qerror_report_err(error);
362 error_free(error);
363 return NULL;
366 if (id) {
367 qdict_del(bs_opts, "id");
370 has_driver_specific_opts = !!qdict_size(bs_opts);
372 /* extract parameters */
373 bus_id = qemu_opt_get_number(opts, "bus", 0);
374 unit_id = qemu_opt_get_number(opts, "unit", -1);
375 index = qemu_opt_get_number(opts, "index", -1);
377 cyls = qemu_opt_get_number(opts, "cyls", 0);
378 heads = qemu_opt_get_number(opts, "heads", 0);
379 secs = qemu_opt_get_number(opts, "secs", 0);
381 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
382 ro = qemu_opt_get_bool(opts, "read-only", 0);
383 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
385 file = qemu_opt_get(opts, "file");
386 serial = qemu_opt_get(opts, "serial");
388 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
389 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
391 if (type == IF_COUNT) {
392 error_report("unsupported bus type '%s'", buf);
393 return NULL;
395 } else {
396 type = block_default_type;
399 max_devs = if_max_devs[type];
401 if (cyls || heads || secs) {
402 if (cyls < 1) {
403 error_report("invalid physical cyls number");
404 return NULL;
406 if (heads < 1) {
407 error_report("invalid physical heads number");
408 return NULL;
410 if (secs < 1) {
411 error_report("invalid physical secs number");
412 return NULL;
416 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
417 if (!cyls) {
418 error_report("'%s' trans must be used with cyls, heads and secs",
419 buf);
420 return NULL;
422 if (!strcmp(buf, "none"))
423 translation = BIOS_ATA_TRANSLATION_NONE;
424 else if (!strcmp(buf, "lba"))
425 translation = BIOS_ATA_TRANSLATION_LBA;
426 else if (!strcmp(buf, "auto"))
427 translation = BIOS_ATA_TRANSLATION_AUTO;
428 else {
429 error_report("'%s' invalid translation type", buf);
430 return NULL;
434 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
435 if (!strcmp(buf, "disk")) {
436 media = MEDIA_DISK;
437 } else if (!strcmp(buf, "cdrom")) {
438 if (cyls || secs || heads) {
439 error_report("CHS can't be set with media=%s", buf);
440 return NULL;
442 media = MEDIA_CDROM;
443 } else {
444 error_report("'%s' invalid media", buf);
445 return NULL;
449 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
450 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
451 error_report("invalid discard option");
452 return NULL;
456 bdrv_flags = 0;
457 if (qemu_opt_get_bool(opts, "cache.writeback", true)) {
458 bdrv_flags |= BDRV_O_CACHE_WB;
460 if (qemu_opt_get_bool(opts, "cache.direct", false)) {
461 bdrv_flags |= BDRV_O_NOCACHE;
463 if (qemu_opt_get_bool(opts, "cache.no-flush", true)) {
464 bdrv_flags |= BDRV_O_NO_FLUSH;
467 #ifdef CONFIG_LINUX_AIO
468 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
469 if (!strcmp(buf, "native")) {
470 bdrv_flags |= BDRV_O_NATIVE_AIO;
471 } else if (!strcmp(buf, "threads")) {
472 /* this is the default */
473 } else {
474 error_report("invalid aio option");
475 return NULL;
478 #endif
480 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
481 if (is_help_option(buf)) {
482 error_printf("Supported formats:");
483 bdrv_iterate_format(bdrv_format_print, NULL);
484 error_printf("\n");
485 return NULL;
488 qdict_put(bs_opts, "driver", qstring_from_str(buf));
491 /* disk I/O throttling */
492 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
493 qemu_opt_get_number(opts, "throttling.bps-total", 0);
494 io_limits.bps[BLOCK_IO_LIMIT_READ] =
495 qemu_opt_get_number(opts, "throttling.bps-read", 0);
496 io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
497 qemu_opt_get_number(opts, "throttling.bps-write", 0);
498 io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
499 qemu_opt_get_number(opts, "throttling.iops-total", 0);
500 io_limits.iops[BLOCK_IO_LIMIT_READ] =
501 qemu_opt_get_number(opts, "throttling.iops-read", 0);
502 io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
503 qemu_opt_get_number(opts, "throttling.iops-write", 0);
505 if (!do_check_io_limits(&io_limits, &error)) {
506 error_report("%s", error_get_pretty(error));
507 error_free(error);
508 return NULL;
511 if (qemu_opt_get(opts, "boot") != NULL) {
512 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
513 "ignored. Future versions will reject this parameter. Please "
514 "update your scripts.\n");
517 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
518 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
519 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
520 error_report("werror is not supported by this bus type");
521 return NULL;
524 on_write_error = parse_block_error_action(buf, 0);
525 if (on_write_error < 0) {
526 return NULL;
530 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
531 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
532 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
533 error_report("rerror is not supported by this bus type");
534 return NULL;
537 on_read_error = parse_block_error_action(buf, 1);
538 if (on_read_error < 0) {
539 return NULL;
543 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
544 if (type != IF_VIRTIO) {
545 error_report("addr is not supported by this bus type");
546 return NULL;
550 /* compute bus and unit according index */
552 if (index != -1) {
553 if (bus_id != 0 || unit_id != -1) {
554 error_report("index cannot be used with bus and unit");
555 return NULL;
557 bus_id = drive_index_to_bus_id(type, index);
558 unit_id = drive_index_to_unit_id(type, index);
561 /* if user doesn't specify a unit_id,
562 * try to find the first free
565 if (unit_id == -1) {
566 unit_id = 0;
567 while (drive_get(type, bus_id, unit_id) != NULL) {
568 unit_id++;
569 if (max_devs && unit_id >= max_devs) {
570 unit_id -= max_devs;
571 bus_id++;
576 /* check unit id */
578 if (max_devs && unit_id >= max_devs) {
579 error_report("unit %d too big (max is %d)",
580 unit_id, max_devs - 1);
581 return NULL;
585 * catch multiple definitions
588 if (drive_get(type, bus_id, unit_id) != NULL) {
589 error_report("drive with bus=%d, unit=%d (index=%d) exists",
590 bus_id, unit_id, index);
591 return NULL;
594 /* init */
596 dinfo = g_malloc0(sizeof(*dinfo));
597 if ((buf = qemu_opts_id(opts)) != NULL) {
598 dinfo->id = g_strdup(buf);
599 } else {
600 /* no id supplied -> create one */
601 dinfo->id = g_malloc0(32);
602 if (type == IF_IDE || type == IF_SCSI)
603 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
604 if (max_devs)
605 snprintf(dinfo->id, 32, "%s%i%s%i",
606 if_name[type], bus_id, mediastr, unit_id);
607 else
608 snprintf(dinfo->id, 32, "%s%s%i",
609 if_name[type], mediastr, unit_id);
611 dinfo->bdrv = bdrv_new(dinfo->id);
612 dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
613 dinfo->bdrv->read_only = ro;
614 dinfo->devaddr = devaddr;
615 dinfo->type = type;
616 dinfo->bus = bus_id;
617 dinfo->unit = unit_id;
618 dinfo->cyls = cyls;
619 dinfo->heads = heads;
620 dinfo->secs = secs;
621 dinfo->trans = translation;
622 dinfo->opts = all_opts;
623 dinfo->refcount = 1;
624 if (serial != NULL) {
625 dinfo->serial = g_strdup(serial);
627 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
629 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
631 /* disk I/O throttling */
632 bdrv_set_io_limits(dinfo->bdrv, &io_limits);
634 switch(type) {
635 case IF_IDE:
636 case IF_SCSI:
637 case IF_XEN:
638 case IF_NONE:
639 dinfo->media_cd = media == MEDIA_CDROM;
640 break;
641 case IF_SD:
642 case IF_FLOPPY:
643 case IF_PFLASH:
644 case IF_MTD:
645 break;
646 case IF_VIRTIO:
648 /* add virtio block device */
649 QemuOpts *devopts;
650 devopts = qemu_opts_create_nofail(qemu_find_opts("device"));
651 if (arch_type == QEMU_ARCH_S390X) {
652 qemu_opt_set(devopts, "driver", "virtio-blk-s390");
653 } else {
654 qemu_opt_set(devopts, "driver", "virtio-blk-pci");
656 qemu_opt_set(devopts, "drive", dinfo->id);
657 if (devaddr)
658 qemu_opt_set(devopts, "addr", devaddr);
659 break;
661 default:
662 abort();
664 if (!file || !*file) {
665 if (has_driver_specific_opts) {
666 file = NULL;
667 } else {
668 return dinfo;
671 if (snapshot) {
672 /* always use cache=unsafe with snapshot */
673 bdrv_flags &= ~BDRV_O_CACHE_MASK;
674 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
677 if (copy_on_read) {
678 bdrv_flags |= BDRV_O_COPY_ON_READ;
681 if (runstate_check(RUN_STATE_INMIGRATE)) {
682 bdrv_flags |= BDRV_O_INCOMING;
685 if (media == MEDIA_CDROM) {
686 /* CDROM is fine for any interface, don't check. */
687 ro = 1;
688 } else if (ro == 1) {
689 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
690 type != IF_NONE && type != IF_PFLASH) {
691 error_report("read-only not supported by this bus type");
692 goto err;
696 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
698 if (ro && copy_on_read) {
699 error_report("warning: disabling copy_on_read on read-only drive");
702 QINCREF(bs_opts);
703 ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, NULL);
705 if (ret < 0) {
706 if (ret == -EMEDIUMTYPE) {
707 error_report("could not open disk image %s: not in %s format",
708 file ?: dinfo->id, qdict_get_str(bs_opts, "driver"));
709 } else {
710 error_report("could not open disk image %s: %s",
711 file ?: dinfo->id, strerror(-ret));
713 goto err;
716 if (bdrv_key_required(dinfo->bdrv))
717 autostart = 0;
719 QDECREF(bs_opts);
720 qemu_opts_del(opts);
722 return dinfo;
724 err:
725 qemu_opts_del(opts);
726 QDECREF(bs_opts);
727 bdrv_delete(dinfo->bdrv);
728 g_free(dinfo->id);
729 QTAILQ_REMOVE(&drives, dinfo, next);
730 g_free(dinfo);
731 return NULL;
734 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to)
736 const char *value;
738 value = qemu_opt_get(opts, from);
739 if (value) {
740 qemu_opt_set(opts, to, value);
741 qemu_opt_unset(opts, from);
745 DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
747 const char *value;
750 * Check that only old options are used by copying into a QemuOpts with
751 * stricter checks. Going through a QDict seems to be the easiest way to
752 * achieve this...
754 QemuOpts* check_opts;
755 QDict *qdict;
756 Error *local_err = NULL;
758 qdict = qemu_opts_to_qdict(all_opts, NULL);
759 check_opts = qemu_opts_from_qdict(&qemu_old_drive_opts, qdict, &local_err);
760 QDECREF(qdict);
762 if (error_is_set(&local_err)) {
763 qerror_report_err(local_err);
764 error_free(local_err);
765 return NULL;
767 qemu_opts_del(check_opts);
769 /* Change legacy command line options into QMP ones */
770 qemu_opt_rename(all_opts, "iops", "throttling.iops-total");
771 qemu_opt_rename(all_opts, "iops_rd", "throttling.iops-read");
772 qemu_opt_rename(all_opts, "iops_wr", "throttling.iops-write");
774 qemu_opt_rename(all_opts, "bps", "throttling.bps-total");
775 qemu_opt_rename(all_opts, "bps_rd", "throttling.bps-read");
776 qemu_opt_rename(all_opts, "bps_wr", "throttling.bps-write");
778 qemu_opt_rename(all_opts, "readonly", "read-only");
780 value = qemu_opt_get(all_opts, "cache");
781 if (value) {
782 int flags = 0;
784 if (bdrv_parse_cache_flags(value, &flags) != 0) {
785 error_report("invalid cache option");
786 return NULL;
789 /* Specific options take precedence */
790 if (!qemu_opt_get(all_opts, "cache.writeback")) {
791 qemu_opt_set_bool(all_opts, "cache.writeback",
792 !!(flags & BDRV_O_CACHE_WB));
794 if (!qemu_opt_get(all_opts, "cache.direct")) {
795 qemu_opt_set_bool(all_opts, "cache.direct",
796 !!(flags & BDRV_O_NOCACHE));
798 if (!qemu_opt_get(all_opts, "cache.no-flush")) {
799 qemu_opt_set_bool(all_opts, "cache.no-flush",
800 !!(flags & BDRV_O_NO_FLUSH));
802 qemu_opt_unset(all_opts, "cache");
805 return blockdev_init(all_opts, block_default_type);
808 void do_commit(Monitor *mon, const QDict *qdict)
810 const char *device = qdict_get_str(qdict, "device");
811 BlockDriverState *bs;
812 int ret;
814 if (!strcmp(device, "all")) {
815 ret = bdrv_commit_all();
816 } else {
817 bs = bdrv_find(device);
818 if (!bs) {
819 monitor_printf(mon, "Device '%s' not found\n", device);
820 return;
822 ret = bdrv_commit(bs);
824 if (ret < 0) {
825 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
826 strerror(-ret));
830 static void blockdev_do_action(int kind, void *data, Error **errp)
832 TransactionAction action;
833 TransactionActionList list;
835 action.kind = kind;
836 action.data = data;
837 list.value = &action;
838 list.next = NULL;
839 qmp_transaction(&list, errp);
842 void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
843 bool has_format, const char *format,
844 bool has_mode, enum NewImageMode mode,
845 Error **errp)
847 BlockdevSnapshot snapshot = {
848 .device = (char *) device,
849 .snapshot_file = (char *) snapshot_file,
850 .has_format = has_format,
851 .format = (char *) format,
852 .has_mode = has_mode,
853 .mode = mode,
855 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
856 &snapshot, errp);
860 /* New and old BlockDriverState structs for group snapshots */
862 typedef struct BlkTransactionState BlkTransactionState;
864 /* Only prepare() may fail. In a single transaction, only one of commit() or
865 abort() will be called, clean() will always be called if it present. */
866 typedef struct BdrvActionOps {
867 /* Size of state struct, in bytes. */
868 size_t instance_size;
869 /* Prepare the work, must NOT be NULL. */
870 void (*prepare)(BlkTransactionState *common, Error **errp);
871 /* Commit the changes, can be NULL. */
872 void (*commit)(BlkTransactionState *common);
873 /* Abort the changes on fail, can be NULL. */
874 void (*abort)(BlkTransactionState *common);
875 /* Clean up resource in the end, can be NULL. */
876 void (*clean)(BlkTransactionState *common);
877 } BdrvActionOps;
880 * This structure must be arranged as first member in child type, assuming
881 * that compiler will also arrange it to the same address with parent instance.
882 * Later it will be used in free().
884 struct BlkTransactionState {
885 TransactionAction *action;
886 const BdrvActionOps *ops;
887 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
890 /* external snapshot private data */
891 typedef struct ExternalSnapshotState {
892 BlkTransactionState common;
893 BlockDriverState *old_bs;
894 BlockDriverState *new_bs;
895 } ExternalSnapshotState;
897 static void external_snapshot_prepare(BlkTransactionState *common,
898 Error **errp)
900 BlockDriver *drv;
901 int flags, ret;
902 Error *local_err = NULL;
903 const char *device;
904 const char *new_image_file;
905 const char *format = "qcow2";
906 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
907 ExternalSnapshotState *state =
908 DO_UPCAST(ExternalSnapshotState, common, common);
909 TransactionAction *action = common->action;
911 /* get parameters */
912 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
914 device = action->blockdev_snapshot_sync->device;
915 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
916 if (action->blockdev_snapshot_sync->has_format) {
917 format = action->blockdev_snapshot_sync->format;
919 if (action->blockdev_snapshot_sync->has_mode) {
920 mode = action->blockdev_snapshot_sync->mode;
923 /* start processing */
924 drv = bdrv_find_format(format);
925 if (!drv) {
926 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
927 return;
930 state->old_bs = bdrv_find(device);
931 if (!state->old_bs) {
932 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
933 return;
936 if (!bdrv_is_inserted(state->old_bs)) {
937 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
938 return;
941 if (bdrv_in_use(state->old_bs)) {
942 error_set(errp, QERR_DEVICE_IN_USE, device);
943 return;
946 if (!bdrv_is_read_only(state->old_bs)) {
947 if (bdrv_flush(state->old_bs)) {
948 error_set(errp, QERR_IO_ERROR);
949 return;
953 flags = state->old_bs->open_flags;
955 /* create new image w/backing file */
956 if (mode != NEW_IMAGE_MODE_EXISTING) {
957 bdrv_img_create(new_image_file, format,
958 state->old_bs->filename,
959 state->old_bs->drv->format_name,
960 NULL, -1, flags, &local_err, false);
961 if (error_is_set(&local_err)) {
962 error_propagate(errp, local_err);
963 return;
967 /* We will manually add the backing_hd field to the bs later */
968 state->new_bs = bdrv_new("");
969 /* TODO Inherit bs->options or only take explicit options with an
970 * extended QMP command? */
971 ret = bdrv_open(state->new_bs, new_image_file, NULL,
972 flags | BDRV_O_NO_BACKING, drv);
973 if (ret != 0) {
974 error_setg_file_open(errp, -ret, new_image_file);
978 static void external_snapshot_commit(BlkTransactionState *common)
980 ExternalSnapshotState *state =
981 DO_UPCAST(ExternalSnapshotState, common, common);
983 /* This removes our old bs and adds the new bs */
984 bdrv_append(state->new_bs, state->old_bs);
985 /* We don't need (or want) to use the transactional
986 * bdrv_reopen_multiple() across all the entries at once, because we
987 * don't want to abort all of them if one of them fails the reopen */
988 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
989 NULL);
992 static void external_snapshot_abort(BlkTransactionState *common)
994 ExternalSnapshotState *state =
995 DO_UPCAST(ExternalSnapshotState, common, common);
996 if (state->new_bs) {
997 bdrv_delete(state->new_bs);
1001 typedef struct DriveBackupState {
1002 BlkTransactionState common;
1003 BlockDriverState *bs;
1004 BlockJob *job;
1005 } DriveBackupState;
1007 static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1009 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1010 DriveBackup *backup;
1011 Error *local_err = NULL;
1013 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1014 backup = common->action->drive_backup;
1016 qmp_drive_backup(backup->device, backup->target,
1017 backup->has_format, backup->format,
1018 backup->sync,
1019 backup->has_mode, backup->mode,
1020 backup->has_speed, backup->speed,
1021 backup->has_on_source_error, backup->on_source_error,
1022 backup->has_on_target_error, backup->on_target_error,
1023 &local_err);
1024 if (error_is_set(&local_err)) {
1025 error_propagate(errp, local_err);
1026 state->bs = NULL;
1027 state->job = NULL;
1028 return;
1031 state->bs = bdrv_find(backup->device);
1032 state->job = state->bs->job;
1035 static void drive_backup_abort(BlkTransactionState *common)
1037 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1038 BlockDriverState *bs = state->bs;
1040 /* Only cancel if it's the job we started */
1041 if (bs && bs->job && bs->job == state->job) {
1042 block_job_cancel_sync(bs->job);
1046 static void abort_prepare(BlkTransactionState *common, Error **errp)
1048 error_setg(errp, "Transaction aborted using Abort action");
1051 static void abort_commit(BlkTransactionState *common)
1053 g_assert_not_reached(); /* this action never succeeds */
1056 static const BdrvActionOps actions[] = {
1057 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1058 .instance_size = sizeof(ExternalSnapshotState),
1059 .prepare = external_snapshot_prepare,
1060 .commit = external_snapshot_commit,
1061 .abort = external_snapshot_abort,
1063 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1064 .instance_size = sizeof(DriveBackupState),
1065 .prepare = drive_backup_prepare,
1066 .abort = drive_backup_abort,
1068 [TRANSACTION_ACTION_KIND_ABORT] = {
1069 .instance_size = sizeof(BlkTransactionState),
1070 .prepare = abort_prepare,
1071 .commit = abort_commit,
1076 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1077 * then we do not pivot any of the devices in the group, and abandon the
1078 * snapshots
1080 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1082 TransactionActionList *dev_entry = dev_list;
1083 BlkTransactionState *state, *next;
1084 Error *local_err = NULL;
1086 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
1087 QSIMPLEQ_INIT(&snap_bdrv_states);
1089 /* drain all i/o before any snapshots */
1090 bdrv_drain_all();
1092 /* We don't do anything in this loop that commits us to the snapshot */
1093 while (NULL != dev_entry) {
1094 TransactionAction *dev_info = NULL;
1095 const BdrvActionOps *ops;
1097 dev_info = dev_entry->value;
1098 dev_entry = dev_entry->next;
1100 assert(dev_info->kind < ARRAY_SIZE(actions));
1102 ops = &actions[dev_info->kind];
1103 state = g_malloc0(ops->instance_size);
1104 state->ops = ops;
1105 state->action = dev_info;
1106 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
1108 state->ops->prepare(state, &local_err);
1109 if (error_is_set(&local_err)) {
1110 error_propagate(errp, local_err);
1111 goto delete_and_fail;
1115 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1116 if (state->ops->commit) {
1117 state->ops->commit(state);
1121 /* success */
1122 goto exit;
1124 delete_and_fail:
1126 * failure, and it is all-or-none; abandon each new bs, and keep using
1127 * the original bs for all images
1129 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1130 if (state->ops->abort) {
1131 state->ops->abort(state);
1134 exit:
1135 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1136 if (state->ops->clean) {
1137 state->ops->clean(state);
1139 g_free(state);
1144 static void eject_device(BlockDriverState *bs, int force, Error **errp)
1146 if (bdrv_in_use(bs)) {
1147 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
1148 return;
1150 if (!bdrv_dev_has_removable_media(bs)) {
1151 error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
1152 return;
1155 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
1156 bdrv_dev_eject_request(bs, force);
1157 if (!force) {
1158 error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
1159 return;
1163 bdrv_close(bs);
1166 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1168 BlockDriverState *bs;
1170 bs = bdrv_find(device);
1171 if (!bs) {
1172 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1173 return;
1176 eject_device(bs, force, errp);
1179 void qmp_block_passwd(const char *device, const char *password, Error **errp)
1181 BlockDriverState *bs;
1182 int err;
1184 bs = bdrv_find(device);
1185 if (!bs) {
1186 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1187 return;
1190 err = bdrv_set_key(bs, password);
1191 if (err == -EINVAL) {
1192 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1193 return;
1194 } else if (err < 0) {
1195 error_set(errp, QERR_INVALID_PASSWORD);
1196 return;
1200 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1201 int bdrv_flags, BlockDriver *drv,
1202 const char *password, Error **errp)
1204 int ret;
1206 ret = bdrv_open(bs, filename, NULL, bdrv_flags, drv);
1207 if (ret < 0) {
1208 error_setg_file_open(errp, -ret, filename);
1209 return;
1212 if (bdrv_key_required(bs)) {
1213 if (password) {
1214 if (bdrv_set_key(bs, password) < 0) {
1215 error_set(errp, QERR_INVALID_PASSWORD);
1217 } else {
1218 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
1219 bdrv_get_encrypted_filename(bs));
1221 } else if (password) {
1222 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1226 void qmp_change_blockdev(const char *device, const char *filename,
1227 bool has_format, const char *format, Error **errp)
1229 BlockDriverState *bs;
1230 BlockDriver *drv = NULL;
1231 int bdrv_flags;
1232 Error *err = NULL;
1234 bs = bdrv_find(device);
1235 if (!bs) {
1236 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1237 return;
1240 if (format) {
1241 drv = bdrv_find_whitelisted_format(format, bs->read_only);
1242 if (!drv) {
1243 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1244 return;
1248 eject_device(bs, 0, &err);
1249 if (error_is_set(&err)) {
1250 error_propagate(errp, err);
1251 return;
1254 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1255 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1257 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1260 /* throttling disk I/O limits */
1261 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1262 int64_t bps_wr, int64_t iops, int64_t iops_rd,
1263 int64_t iops_wr, Error **errp)
1265 BlockIOLimit io_limits;
1266 BlockDriverState *bs;
1268 bs = bdrv_find(device);
1269 if (!bs) {
1270 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1271 return;
1274 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
1275 io_limits.bps[BLOCK_IO_LIMIT_READ] = bps_rd;
1276 io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
1277 io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
1278 io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
1279 io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
1281 if (!do_check_io_limits(&io_limits, errp)) {
1282 return;
1285 bs->io_limits = io_limits;
1287 if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
1288 bdrv_io_limits_enable(bs);
1289 } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
1290 bdrv_io_limits_disable(bs);
1291 } else {
1292 if (bs->block_timer) {
1293 qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
1298 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1300 const char *id = qdict_get_str(qdict, "id");
1301 BlockDriverState *bs;
1303 bs = bdrv_find(id);
1304 if (!bs) {
1305 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1306 return -1;
1308 if (bdrv_in_use(bs)) {
1309 qerror_report(QERR_DEVICE_IN_USE, id);
1310 return -1;
1313 /* quiesce block driver; prevent further io */
1314 bdrv_drain_all();
1315 bdrv_flush(bs);
1316 bdrv_close(bs);
1318 /* if we have a device attached to this BlockDriverState
1319 * then we need to make the drive anonymous until the device
1320 * can be removed. If this is a drive with no device backing
1321 * then we can just get rid of the block driver state right here.
1323 if (bdrv_get_attached_dev(bs)) {
1324 bdrv_make_anon(bs);
1326 /* Further I/O must not pause the guest */
1327 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
1328 BLOCKDEV_ON_ERROR_REPORT);
1329 } else {
1330 drive_uninit(drive_get_by_blockdev(bs));
1333 return 0;
1336 void qmp_block_resize(const char *device, int64_t size, Error **errp)
1338 BlockDriverState *bs;
1339 int ret;
1341 bs = bdrv_find(device);
1342 if (!bs) {
1343 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1344 return;
1347 if (size < 0) {
1348 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
1349 return;
1352 /* complete all in-flight operations before resizing the device */
1353 bdrv_drain_all();
1355 ret = bdrv_truncate(bs, size);
1356 switch (ret) {
1357 case 0:
1358 break;
1359 case -ENOMEDIUM:
1360 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1361 break;
1362 case -ENOTSUP:
1363 error_set(errp, QERR_UNSUPPORTED);
1364 break;
1365 case -EACCES:
1366 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1367 break;
1368 case -EBUSY:
1369 error_set(errp, QERR_DEVICE_IN_USE, device);
1370 break;
1371 default:
1372 error_setg_errno(errp, -ret, "Could not resize");
1373 break;
1377 static void block_job_cb(void *opaque, int ret)
1379 BlockDriverState *bs = opaque;
1380 QObject *obj;
1382 trace_block_job_cb(bs, bs->job, ret);
1384 assert(bs->job);
1385 obj = qobject_from_block_job(bs->job);
1386 if (ret < 0) {
1387 QDict *dict = qobject_to_qdict(obj);
1388 qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
1391 if (block_job_is_cancelled(bs->job)) {
1392 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1393 } else {
1394 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1396 qobject_decref(obj);
1398 drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
1401 void qmp_block_stream(const char *device, bool has_base,
1402 const char *base, bool has_speed, int64_t speed,
1403 bool has_on_error, BlockdevOnError on_error,
1404 Error **errp)
1406 BlockDriverState *bs;
1407 BlockDriverState *base_bs = NULL;
1408 Error *local_err = NULL;
1410 if (!has_on_error) {
1411 on_error = BLOCKDEV_ON_ERROR_REPORT;
1414 bs = bdrv_find(device);
1415 if (!bs) {
1416 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1417 return;
1420 if (base) {
1421 base_bs = bdrv_find_backing_image(bs, base);
1422 if (base_bs == NULL) {
1423 error_set(errp, QERR_BASE_NOT_FOUND, base);
1424 return;
1428 stream_start(bs, base_bs, base, has_speed ? speed : 0,
1429 on_error, block_job_cb, bs, &local_err);
1430 if (error_is_set(&local_err)) {
1431 error_propagate(errp, local_err);
1432 return;
1435 /* Grab a reference so hotplug does not delete the BlockDriverState from
1436 * underneath us.
1438 drive_get_ref(drive_get_by_blockdev(bs));
1440 trace_qmp_block_stream(bs, bs->job);
1443 void qmp_block_commit(const char *device,
1444 bool has_base, const char *base, const char *top,
1445 bool has_speed, int64_t speed,
1446 Error **errp)
1448 BlockDriverState *bs;
1449 BlockDriverState *base_bs, *top_bs;
1450 Error *local_err = NULL;
1451 /* This will be part of the QMP command, if/when the
1452 * BlockdevOnError change for blkmirror makes it in
1454 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
1456 /* drain all i/o before commits */
1457 bdrv_drain_all();
1459 bs = bdrv_find(device);
1460 if (!bs) {
1461 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1462 return;
1465 /* default top_bs is the active layer */
1466 top_bs = bs;
1468 if (top) {
1469 if (strcmp(bs->filename, top) != 0) {
1470 top_bs = bdrv_find_backing_image(bs, top);
1474 if (top_bs == NULL) {
1475 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1476 return;
1479 if (has_base && base) {
1480 base_bs = bdrv_find_backing_image(top_bs, base);
1481 } else {
1482 base_bs = bdrv_find_base(top_bs);
1485 if (base_bs == NULL) {
1486 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1487 return;
1490 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1491 &local_err);
1492 if (local_err != NULL) {
1493 error_propagate(errp, local_err);
1494 return;
1496 /* Grab a reference so hotplug does not delete the BlockDriverState from
1497 * underneath us.
1499 drive_get_ref(drive_get_by_blockdev(bs));
1502 void qmp_drive_backup(const char *device, const char *target,
1503 bool has_format, const char *format,
1504 enum MirrorSyncMode sync,
1505 bool has_mode, enum NewImageMode mode,
1506 bool has_speed, int64_t speed,
1507 bool has_on_source_error, BlockdevOnError on_source_error,
1508 bool has_on_target_error, BlockdevOnError on_target_error,
1509 Error **errp)
1511 BlockDriverState *bs;
1512 BlockDriverState *target_bs;
1513 BlockDriverState *source = NULL;
1514 BlockDriver *drv = NULL;
1515 Error *local_err = NULL;
1516 int flags;
1517 int64_t size;
1518 int ret;
1520 if (!has_speed) {
1521 speed = 0;
1523 if (!has_on_source_error) {
1524 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1526 if (!has_on_target_error) {
1527 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1529 if (!has_mode) {
1530 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1533 bs = bdrv_find(device);
1534 if (!bs) {
1535 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1536 return;
1539 if (!bdrv_is_inserted(bs)) {
1540 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1541 return;
1544 if (!has_format) {
1545 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1547 if (format) {
1548 drv = bdrv_find_format(format);
1549 if (!drv) {
1550 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1551 return;
1555 if (bdrv_in_use(bs)) {
1556 error_set(errp, QERR_DEVICE_IN_USE, device);
1557 return;
1560 flags = bs->open_flags | BDRV_O_RDWR;
1562 /* See if we have a backing HD we can use to create our new image
1563 * on top of. */
1564 if (sync == MIRROR_SYNC_MODE_TOP) {
1565 source = bs->backing_hd;
1566 if (!source) {
1567 sync = MIRROR_SYNC_MODE_FULL;
1570 if (sync == MIRROR_SYNC_MODE_NONE) {
1571 source = bs;
1574 size = bdrv_getlength(bs);
1575 if (size < 0) {
1576 error_setg_errno(errp, -size, "bdrv_getlength failed");
1577 return;
1580 if (mode != NEW_IMAGE_MODE_EXISTING) {
1581 assert(format && drv);
1582 if (source) {
1583 bdrv_img_create(target, format, source->filename,
1584 source->drv->format_name, NULL,
1585 size, flags, &local_err, false);
1586 } else {
1587 bdrv_img_create(target, format, NULL, NULL, NULL,
1588 size, flags, &local_err, false);
1592 if (error_is_set(&local_err)) {
1593 error_propagate(errp, local_err);
1594 return;
1597 target_bs = bdrv_new("");
1598 ret = bdrv_open(target_bs, target, NULL, flags, drv);
1599 if (ret < 0) {
1600 bdrv_delete(target_bs);
1601 error_setg_file_open(errp, -ret, target);
1602 return;
1605 backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
1606 block_job_cb, bs, &local_err);
1607 if (local_err != NULL) {
1608 bdrv_delete(target_bs);
1609 error_propagate(errp, local_err);
1610 return;
1613 /* Grab a reference so hotplug does not delete the BlockDriverState from
1614 * underneath us.
1616 drive_get_ref(drive_get_by_blockdev(bs));
1619 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1621 void qmp_drive_mirror(const char *device, const char *target,
1622 bool has_format, const char *format,
1623 enum MirrorSyncMode sync,
1624 bool has_mode, enum NewImageMode mode,
1625 bool has_speed, int64_t speed,
1626 bool has_granularity, uint32_t granularity,
1627 bool has_buf_size, int64_t buf_size,
1628 bool has_on_source_error, BlockdevOnError on_source_error,
1629 bool has_on_target_error, BlockdevOnError on_target_error,
1630 Error **errp)
1632 BlockDriverState *bs;
1633 BlockDriverState *source, *target_bs;
1634 BlockDriver *drv = NULL;
1635 Error *local_err = NULL;
1636 int flags;
1637 int64_t size;
1638 int ret;
1640 if (!has_speed) {
1641 speed = 0;
1643 if (!has_on_source_error) {
1644 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1646 if (!has_on_target_error) {
1647 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1649 if (!has_mode) {
1650 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1652 if (!has_granularity) {
1653 granularity = 0;
1655 if (!has_buf_size) {
1656 buf_size = DEFAULT_MIRROR_BUF_SIZE;
1659 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1660 error_set(errp, QERR_INVALID_PARAMETER, device);
1661 return;
1663 if (granularity & (granularity - 1)) {
1664 error_set(errp, QERR_INVALID_PARAMETER, device);
1665 return;
1668 bs = bdrv_find(device);
1669 if (!bs) {
1670 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1671 return;
1674 if (!bdrv_is_inserted(bs)) {
1675 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1676 return;
1679 if (!has_format) {
1680 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1682 if (format) {
1683 drv = bdrv_find_format(format);
1684 if (!drv) {
1685 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1686 return;
1690 if (bdrv_in_use(bs)) {
1691 error_set(errp, QERR_DEVICE_IN_USE, device);
1692 return;
1695 flags = bs->open_flags | BDRV_O_RDWR;
1696 source = bs->backing_hd;
1697 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
1698 sync = MIRROR_SYNC_MODE_FULL;
1701 size = bdrv_getlength(bs);
1702 if (size < 0) {
1703 error_setg_errno(errp, -size, "bdrv_getlength failed");
1704 return;
1707 if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
1708 /* create new image w/o backing file */
1709 assert(format && drv);
1710 bdrv_img_create(target, format,
1711 NULL, NULL, NULL, size, flags, &local_err, false);
1712 } else {
1713 switch (mode) {
1714 case NEW_IMAGE_MODE_EXISTING:
1715 ret = 0;
1716 break;
1717 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
1718 /* create new image with backing file */
1719 bdrv_img_create(target, format,
1720 source->filename,
1721 source->drv->format_name,
1722 NULL, size, flags, &local_err, false);
1723 break;
1724 default:
1725 abort();
1729 if (error_is_set(&local_err)) {
1730 error_propagate(errp, local_err);
1731 return;
1734 /* Mirroring takes care of copy-on-write using the source's backing
1735 * file.
1737 target_bs = bdrv_new("");
1738 ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv);
1739 if (ret < 0) {
1740 bdrv_delete(target_bs);
1741 error_setg_file_open(errp, -ret, target);
1742 return;
1745 mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
1746 on_source_error, on_target_error,
1747 block_job_cb, bs, &local_err);
1748 if (local_err != NULL) {
1749 bdrv_delete(target_bs);
1750 error_propagate(errp, local_err);
1751 return;
1754 /* Grab a reference so hotplug does not delete the BlockDriverState from
1755 * underneath us.
1757 drive_get_ref(drive_get_by_blockdev(bs));
1760 static BlockJob *find_block_job(const char *device)
1762 BlockDriverState *bs;
1764 bs = bdrv_find(device);
1765 if (!bs || !bs->job) {
1766 return NULL;
1768 return bs->job;
1771 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
1773 BlockJob *job = find_block_job(device);
1775 if (!job) {
1776 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1777 return;
1780 block_job_set_speed(job, speed, errp);
1783 void qmp_block_job_cancel(const char *device,
1784 bool has_force, bool force, Error **errp)
1786 BlockJob *job = find_block_job(device);
1788 if (!has_force) {
1789 force = false;
1792 if (!job) {
1793 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1794 return;
1796 if (job->paused && !force) {
1797 error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
1798 return;
1801 trace_qmp_block_job_cancel(job);
1802 block_job_cancel(job);
1805 void qmp_block_job_pause(const char *device, Error **errp)
1807 BlockJob *job = find_block_job(device);
1809 if (!job) {
1810 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1811 return;
1814 trace_qmp_block_job_pause(job);
1815 block_job_pause(job);
1818 void qmp_block_job_resume(const char *device, Error **errp)
1820 BlockJob *job = find_block_job(device);
1822 if (!job) {
1823 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1824 return;
1827 trace_qmp_block_job_resume(job);
1828 block_job_resume(job);
1831 void qmp_block_job_complete(const char *device, Error **errp)
1833 BlockJob *job = find_block_job(device);
1835 if (!job) {
1836 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1837 return;
1840 trace_qmp_block_job_complete(job);
1841 block_job_complete(job, errp);
1844 static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
1846 BlockJobInfoList **prev = opaque;
1847 BlockJob *job = bs->job;
1849 if (job) {
1850 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
1851 elem->value = block_job_query(bs->job);
1852 (*prev)->next = elem;
1853 *prev = elem;
1857 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
1859 /* Dummy is a fake list element for holding the head pointer */
1860 BlockJobInfoList dummy = {};
1861 BlockJobInfoList *prev = &dummy;
1862 bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
1863 return dummy.next;
1866 QemuOptsList qemu_common_drive_opts = {
1867 .name = "drive",
1868 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
1869 .desc = {
1871 .name = "bus",
1872 .type = QEMU_OPT_NUMBER,
1873 .help = "bus number",
1875 .name = "unit",
1876 .type = QEMU_OPT_NUMBER,
1877 .help = "unit number (i.e. lun for scsi)",
1879 .name = "if",
1880 .type = QEMU_OPT_STRING,
1881 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1883 .name = "index",
1884 .type = QEMU_OPT_NUMBER,
1885 .help = "index number",
1887 .name = "cyls",
1888 .type = QEMU_OPT_NUMBER,
1889 .help = "number of cylinders (ide disk geometry)",
1891 .name = "heads",
1892 .type = QEMU_OPT_NUMBER,
1893 .help = "number of heads (ide disk geometry)",
1895 .name = "secs",
1896 .type = QEMU_OPT_NUMBER,
1897 .help = "number of sectors (ide disk geometry)",
1899 .name = "trans",
1900 .type = QEMU_OPT_STRING,
1901 .help = "chs translation (auto, lba. none)",
1903 .name = "media",
1904 .type = QEMU_OPT_STRING,
1905 .help = "media type (disk, cdrom)",
1907 .name = "snapshot",
1908 .type = QEMU_OPT_BOOL,
1909 .help = "enable/disable snapshot mode",
1911 .name = "file",
1912 .type = QEMU_OPT_STRING,
1913 .help = "disk image",
1915 .name = "discard",
1916 .type = QEMU_OPT_STRING,
1917 .help = "discard operation (ignore/off, unmap/on)",
1919 .name = "cache.writeback",
1920 .type = QEMU_OPT_BOOL,
1921 .help = "enables writeback mode for any caches",
1923 .name = "cache.direct",
1924 .type = QEMU_OPT_BOOL,
1925 .help = "enables use of O_DIRECT (bypass the host page cache)",
1927 .name = "cache.no-flush",
1928 .type = QEMU_OPT_BOOL,
1929 .help = "ignore any flush requests for the device",
1931 .name = "aio",
1932 .type = QEMU_OPT_STRING,
1933 .help = "host AIO implementation (threads, native)",
1935 .name = "format",
1936 .type = QEMU_OPT_STRING,
1937 .help = "disk format (raw, qcow2, ...)",
1939 .name = "serial",
1940 .type = QEMU_OPT_STRING,
1941 .help = "disk serial number",
1943 .name = "rerror",
1944 .type = QEMU_OPT_STRING,
1945 .help = "read error action",
1947 .name = "werror",
1948 .type = QEMU_OPT_STRING,
1949 .help = "write error action",
1951 .name = "addr",
1952 .type = QEMU_OPT_STRING,
1953 .help = "pci address (virtio only)",
1955 .name = "read-only",
1956 .type = QEMU_OPT_BOOL,
1957 .help = "open drive file as read-only",
1959 .name = "throttling.iops-total",
1960 .type = QEMU_OPT_NUMBER,
1961 .help = "limit total I/O operations per second",
1963 .name = "throttling.iops-read",
1964 .type = QEMU_OPT_NUMBER,
1965 .help = "limit read operations per second",
1967 .name = "throttling.iops-write",
1968 .type = QEMU_OPT_NUMBER,
1969 .help = "limit write operations per second",
1971 .name = "throttling.bps-total",
1972 .type = QEMU_OPT_NUMBER,
1973 .help = "limit total bytes per second",
1975 .name = "throttling.bps-read",
1976 .type = QEMU_OPT_NUMBER,
1977 .help = "limit read bytes per second",
1979 .name = "throttling.bps-write",
1980 .type = QEMU_OPT_NUMBER,
1981 .help = "limit write bytes per second",
1983 .name = "copy-on-read",
1984 .type = QEMU_OPT_BOOL,
1985 .help = "copy read data from backing file into image file",
1987 .name = "boot",
1988 .type = QEMU_OPT_BOOL,
1989 .help = "(deprecated, ignored)",
1991 { /* end of list */ }
1995 QemuOptsList qemu_old_drive_opts = {
1996 .name = "drive",
1997 .head = QTAILQ_HEAD_INITIALIZER(qemu_old_drive_opts.head),
1998 .desc = {
2000 .name = "bus",
2001 .type = QEMU_OPT_NUMBER,
2002 .help = "bus number",
2004 .name = "unit",
2005 .type = QEMU_OPT_NUMBER,
2006 .help = "unit number (i.e. lun for scsi)",
2008 .name = "if",
2009 .type = QEMU_OPT_STRING,
2010 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
2012 .name = "index",
2013 .type = QEMU_OPT_NUMBER,
2014 .help = "index number",
2016 .name = "cyls",
2017 .type = QEMU_OPT_NUMBER,
2018 .help = "number of cylinders (ide disk geometry)",
2020 .name = "heads",
2021 .type = QEMU_OPT_NUMBER,
2022 .help = "number of heads (ide disk geometry)",
2024 .name = "secs",
2025 .type = QEMU_OPT_NUMBER,
2026 .help = "number of sectors (ide disk geometry)",
2028 .name = "trans",
2029 .type = QEMU_OPT_STRING,
2030 .help = "chs translation (auto, lba. none)",
2032 .name = "media",
2033 .type = QEMU_OPT_STRING,
2034 .help = "media type (disk, cdrom)",
2036 .name = "snapshot",
2037 .type = QEMU_OPT_BOOL,
2038 .help = "enable/disable snapshot mode",
2040 .name = "file",
2041 .type = QEMU_OPT_STRING,
2042 .help = "disk image",
2044 .name = "discard",
2045 .type = QEMU_OPT_STRING,
2046 .help = "discard operation (ignore/off, unmap/on)",
2048 .name = "cache",
2049 .type = QEMU_OPT_STRING,
2050 .help = "host cache usage (none, writeback, writethrough, "
2051 "directsync, unsafe)",
2053 .name = "aio",
2054 .type = QEMU_OPT_STRING,
2055 .help = "host AIO implementation (threads, native)",
2057 .name = "format",
2058 .type = QEMU_OPT_STRING,
2059 .help = "disk format (raw, qcow2, ...)",
2061 .name = "serial",
2062 .type = QEMU_OPT_STRING,
2063 .help = "disk serial number",
2065 .name = "rerror",
2066 .type = QEMU_OPT_STRING,
2067 .help = "read error action",
2069 .name = "werror",
2070 .type = QEMU_OPT_STRING,
2071 .help = "write error action",
2073 .name = "addr",
2074 .type = QEMU_OPT_STRING,
2075 .help = "pci address (virtio only)",
2077 .name = "readonly",
2078 .type = QEMU_OPT_BOOL,
2079 .help = "open drive file as read-only",
2081 .name = "iops",
2082 .type = QEMU_OPT_NUMBER,
2083 .help = "limit total I/O operations per second",
2085 .name = "iops_rd",
2086 .type = QEMU_OPT_NUMBER,
2087 .help = "limit read operations per second",
2089 .name = "iops_wr",
2090 .type = QEMU_OPT_NUMBER,
2091 .help = "limit write operations per second",
2093 .name = "bps",
2094 .type = QEMU_OPT_NUMBER,
2095 .help = "limit total bytes per second",
2097 .name = "bps_rd",
2098 .type = QEMU_OPT_NUMBER,
2099 .help = "limit read bytes per second",
2101 .name = "bps_wr",
2102 .type = QEMU_OPT_NUMBER,
2103 .help = "limit write bytes per second",
2105 .name = "copy-on-read",
2106 .type = QEMU_OPT_BOOL,
2107 .help = "copy read data from backing file into image file",
2109 .name = "boot",
2110 .type = QEMU_OPT_BOOL,
2111 .help = "(deprecated, ignored)",
2113 { /* end of list */ }
2117 QemuOptsList qemu_drive_opts = {
2118 .name = "drive",
2119 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
2120 .desc = {
2122 * no elements => accept any params
2123 * validation will happen later
2125 { /* end of list */ }