blob: 17029fd6c0a491b136b01046ed17546f9d282d40 [file] [log] [blame]
Markus Armbruster666daa62010-06-02 18:48:27 +02001/*
2 * QEMU host block devices
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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.
Markus Armbruster3618a092013-03-14 13:59:53 +01008 *
9 * This file incorporates work covered by the following copyright and
10 * permission notice:
11 *
12 * Copyright (c) 2003-2008 Fabrice Bellard
13 *
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:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
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.
Markus Armbruster666daa62010-06-02 18:48:27 +020031 */
32
Markus Armbruster26f54e92014-10-07 13:59:04 +020033#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/blockdev.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010035#include "hw/block/block.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010036#include "block/blockjob.h"
Alberto Garcia76f4afb2015-06-08 18:17:44 +020037#include "block/throttle-groups.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010038#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010039#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010040#include "qemu/option.h"
41#include "qemu/config-file.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010042#include "qapi/qmp/types.h"
Kevin Wolfd26c9a12013-09-23 15:26:03 +020043#include "qapi-visit.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010044#include "qapi/qmp/qerror.h"
Kevin Wolfd26c9a12013-09-23 15:26:03 +020045#include "qapi/qmp-output-visitor.h"
Peter Lieven9e7dac72014-08-13 19:20:17 +020046#include "qapi/util.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010047#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010048#include "block/block_int.h"
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -020049#include "qmp-commands.h"
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +000050#include "trace.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010051#include "sysemu/arch_init.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020052
Markus Armbruster19609662011-01-28 11:21:39 +010053static const char *const if_name[IF_COUNT] = {
54 [IF_NONE] = "none",
55 [IF_IDE] = "ide",
56 [IF_SCSI] = "scsi",
57 [IF_FLOPPY] = "floppy",
58 [IF_PFLASH] = "pflash",
59 [IF_MTD] = "mtd",
60 [IF_SD] = "sd",
61 [IF_VIRTIO] = "virtio",
62 [IF_XEN] = "xen",
63};
64
John Snow21dff8c2014-10-01 14:19:25 -040065static int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010066 /*
67 * Do not change these numbers! They govern how drive option
68 * index maps to unit and bus. That mapping is ABI.
69 *
70 * All controllers used to imlement if=T drives need to support
71 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
72 * Otherwise, some index values map to "impossible" bus, unit
73 * values.
74 *
75 * For instance, if you change [IF_SCSI] to 255, -drive
76 * if=scsi,index=12 no longer means bus=1,unit=5, but
77 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
78 * the drive can't be set up. Regression.
79 */
80 [IF_IDE] = 2,
81 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +010082};
83
John Snow21dff8c2014-10-01 14:19:25 -040084/**
85 * Boards may call this to offer board-by-board overrides
86 * of the default, global values.
87 */
88void override_max_devs(BlockInterfaceType type, int max_devs)
89{
Markus Armbruster18e46a02014-10-07 13:59:06 +020090 BlockBackend *blk;
John Snow21dff8c2014-10-01 14:19:25 -040091 DriveInfo *dinfo;
92
93 if (max_devs <= 0) {
94 return;
95 }
96
Markus Armbruster18e46a02014-10-07 13:59:06 +020097 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
98 dinfo = blk_legacy_dinfo(blk);
John Snow21dff8c2014-10-01 14:19:25 -040099 if (dinfo->type == type) {
100 fprintf(stderr, "Cannot override units-per-bus property of"
101 " the %s interface, because a drive of that type has"
102 " already been added.\n", if_name[type]);
103 g_assert_not_reached();
104 }
105 }
106
107 if_max_devs[type] = max_devs;
108}
109
Markus Armbruster14bafc52010-06-25 08:09:10 +0200110/*
111 * We automatically delete the drive when a device using it gets
112 * unplugged. Questionable feature, but we can't just drop it.
113 * Device models call blockdev_mark_auto_del() to schedule the
114 * automatic deletion, and generic qdev code calls blockdev_auto_del()
115 * when deletion is actually safe.
116 */
Markus Armbruster4be74632014-10-07 13:59:18 +0200117void blockdev_mark_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200118{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200119 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster4be74632014-10-07 13:59:18 +0200120 BlockDriverState *bs = blk_bs(blk);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100121 AioContext *aio_context;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200122
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200123 if (!dinfo) {
Kevin Wolf2d246f02013-09-18 15:14:47 +0200124 return;
125 }
126
Max Reitz5433c242015-10-19 17:53:29 +0200127 if (bs) {
128 aio_context = bdrv_get_aio_context(bs);
129 aio_context_acquire(aio_context);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100130
Max Reitz5433c242015-10-19 17:53:29 +0200131 if (bs->job) {
132 block_job_cancel(bs->job);
133 }
134
135 aio_context_release(aio_context);
Paolo Bonzini12bde0e2012-03-30 13:17:10 +0200136 }
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100137
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200138 dinfo->auto_del = 1;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200139}
140
Markus Armbruster4be74632014-10-07 13:59:18 +0200141void blockdev_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200142{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200143 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200144
Ryan Harper0fc0f1f2010-12-08 10:05:00 -0600145 if (dinfo && dinfo->auto_del) {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +0200146 blk_unref(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200147 }
148}
149
John Snowd8f94e12014-10-01 14:19:27 -0400150/**
151 * Returns the current mapping of how many units per bus
152 * a particular interface can support.
153 *
154 * A positive integer indicates n units per bus.
155 * 0 implies the mapping has not been established.
156 * -1 indicates an invalid BlockInterfaceType was given.
157 */
158int drive_get_max_devs(BlockInterfaceType type)
159{
160 if (type >= IF_IDE && type < IF_COUNT) {
161 return if_max_devs[type];
162 }
163
164 return -1;
165}
166
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100167static int drive_index_to_bus_id(BlockInterfaceType type, int index)
168{
169 int max_devs = if_max_devs[type];
170 return max_devs ? index / max_devs : 0;
171}
172
173static int drive_index_to_unit_id(BlockInterfaceType type, int index)
174{
175 int max_devs = if_max_devs[type];
176 return max_devs ? index % max_devs : index;
177}
178
Markus Armbruster2292dda2011-01-28 11:21:41 +0100179QemuOpts *drive_def(const char *optstr)
180{
Markus Armbruster70b94332015-02-13 12:50:26 +0100181 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100182}
183
184QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +0100185 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +0200186{
Markus Armbruster666daa62010-06-02 18:48:27 +0200187 QemuOpts *opts;
188
Markus Armbruster2292dda2011-01-28 11:21:41 +0100189 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200190 if (!opts) {
191 return NULL;
192 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100193 if (type != IF_DEFAULT) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100194 qemu_opt_set(opts, "if", if_name[type], &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100195 }
196 if (index >= 0) {
Markus Armbrustera8b18f82015-02-13 15:50:43 +0100197 qemu_opt_set_number(opts, "index", index, &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100198 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200199 if (file)
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100200 qemu_opt_set(opts, "file", file, &error_abort);
Markus Armbruster666daa62010-06-02 18:48:27 +0200201 return opts;
202}
203
204DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
205{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200206 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200207 DriveInfo *dinfo;
208
Markus Armbruster18e46a02014-10-07 13:59:06 +0200209 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
210 dinfo = blk_legacy_dinfo(blk);
211 if (dinfo && dinfo->type == type
212 && dinfo->bus == bus && dinfo->unit == unit) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200213 return dinfo;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200214 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200215 }
216
217 return NULL;
218}
219
John Snowa66c9dc2014-10-01 14:19:24 -0400220bool drive_check_orphaned(void)
221{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200222 BlockBackend *blk;
John Snowa66c9dc2014-10-01 14:19:24 -0400223 DriveInfo *dinfo;
224 bool rs = false;
225
Markus Armbruster18e46a02014-10-07 13:59:06 +0200226 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
227 dinfo = blk_legacy_dinfo(blk);
John Snowa66c9dc2014-10-01 14:19:24 -0400228 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
229 /* Unless this is a default drive, this may be an oversight. */
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200230 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
John Snowa66c9dc2014-10-01 14:19:24 -0400231 dinfo->type != IF_NONE) {
232 fprintf(stderr, "Warning: Orphaned drive without device: "
233 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
Max Reitz5433c242015-10-19 17:53:29 +0200234 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
235 if_name[dinfo->type], dinfo->bus, dinfo->unit);
John Snowa66c9dc2014-10-01 14:19:24 -0400236 rs = true;
237 }
238 }
239
240 return rs;
241}
242
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100243DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
244{
245 return drive_get(type,
246 drive_index_to_bus_id(type, index),
247 drive_index_to_unit_id(type, index));
248}
249
Markus Armbruster666daa62010-06-02 18:48:27 +0200250int drive_get_max_bus(BlockInterfaceType type)
251{
252 int max_bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200253 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200254 DriveInfo *dinfo;
255
256 max_bus = -1;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200257 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
258 dinfo = blk_legacy_dinfo(blk);
259 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200260 max_bus = dinfo->bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200261 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200262 }
263 return max_bus;
264}
265
Markus Armbruster13839972011-01-28 11:21:37 +0100266/* Get a block device. This should only be used for single-drive devices
267 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
268 appropriate bus. */
269DriveInfo *drive_get_next(BlockInterfaceType type)
270{
271 static int next_block_unit[IF_COUNT];
272
273 return drive_get(type, 0, next_block_unit[type]++);
274}
275
Markus Armbruster666daa62010-06-02 18:48:27 +0200276static void bdrv_format_print(void *opaque, const char *name)
277{
Markus Armbruster807105a2011-01-17 19:31:27 +0100278 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200279}
280
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000281typedef struct {
282 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800283 BlockDriverState *bs;
284} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000285
Kevin Wolfb6810722013-09-20 11:33:11 +0200286static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200287{
288 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200289 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200290 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200291 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200292 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200293 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200294 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200295 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200296 } else {
Kevin Wolfb6810722013-09-20 11:33:11 +0200297 error_setg(errp, "'%s' invalid %s error action",
298 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200299 return -1;
300 }
301}
302
Benoît Canetcc0681c2013-09-02 14:14:39 +0200303static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800304{
Benoît Canetcc0681c2013-09-02 14:14:39 +0200305 if (throttle_conflicting(cfg)) {
306 error_setg(errp, "bps/iops/max total values and read/write values"
307 " cannot be used at the same time");
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800308 return false;
309 }
310
Benoît Canetcc0681c2013-09-02 14:14:39 +0200311 if (!throttle_is_valid(cfg)) {
312 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
Stefan Hajnoczi7d81c142013-02-13 16:53:43 +0100313 return false;
314 }
315
Stefan Hajnocziee2bdc32015-08-04 11:22:12 +0100316 if (throttle_max_is_missing_limit(cfg)) {
317 error_setg(errp, "bps_max/iops_max require corresponding"
318 " bps/iops values");
319 return false;
320 }
321
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800322 return true;
323}
324
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200325typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
326
Max Reitzfbf81752015-10-19 17:53:31 +0200327/* All parameters but @opts are optional and may be set to NULL. */
328static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
329 const char **throttling_group, ThrottleConfig *throttle_cfg,
330 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
331{
332 const char *discard;
333 Error *local_error = NULL;
334 const char *aio;
335
336 if (bdrv_flags) {
337 if (!qemu_opt_get_bool(opts, "read-only", false)) {
338 *bdrv_flags |= BDRV_O_RDWR;
339 }
340 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
341 *bdrv_flags |= BDRV_O_COPY_ON_READ;
342 }
343
344 if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
345 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
346 error_setg(errp, "Invalid discard option");
347 return;
348 }
349 }
350
351 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) {
352 *bdrv_flags |= BDRV_O_CACHE_WB;
353 }
354 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
355 *bdrv_flags |= BDRV_O_NOCACHE;
356 }
357 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
358 *bdrv_flags |= BDRV_O_NO_FLUSH;
359 }
360
361 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
362 if (!strcmp(aio, "native")) {
363 *bdrv_flags |= BDRV_O_NATIVE_AIO;
364 } else if (!strcmp(aio, "threads")) {
365 /* this is the default */
366 } else {
367 error_setg(errp, "invalid aio option");
368 return;
369 }
370 }
371 }
372
373 /* disk I/O throttling */
374 if (throttling_group) {
375 *throttling_group = qemu_opt_get(opts, "throttling.group");
376 }
377
378 if (throttle_cfg) {
379 memset(throttle_cfg, 0, sizeof(*throttle_cfg));
380 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
381 qemu_opt_get_number(opts, "throttling.bps-total", 0);
382 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
383 qemu_opt_get_number(opts, "throttling.bps-read", 0);
384 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
385 qemu_opt_get_number(opts, "throttling.bps-write", 0);
386 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
387 qemu_opt_get_number(opts, "throttling.iops-total", 0);
388 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
389 qemu_opt_get_number(opts, "throttling.iops-read", 0);
390 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
391 qemu_opt_get_number(opts, "throttling.iops-write", 0);
392
393 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
394 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
395 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
396 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
397 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
398 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
399 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
400 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
401 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
402 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
403 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
404 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
405
406 throttle_cfg->op_size =
407 qemu_opt_get_number(opts, "throttling.iops-size", 0);
408
409 if (!check_throttle_config(throttle_cfg, errp)) {
410 return;
411 }
412 }
413
414 if (detect_zeroes) {
415 *detect_zeroes =
416 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
417 qemu_opt_get(opts, "detect-zeroes"),
418 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
419 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
420 &local_error);
421 if (local_error) {
422 error_propagate(errp, local_error);
423 return;
424 }
425
426 if (bdrv_flags &&
427 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
428 !(*bdrv_flags & BDRV_O_UNMAP))
429 {
430 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
431 "without setting discard operation to unmap");
432 return;
433 }
434 }
435}
436
Kevin Wolff298d072013-09-10 12:01:20 +0200437/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200438static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
439 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200440{
441 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200442 int bdrv_flags = 0;
443 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200444 bool account_invalid, account_failed;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200445 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200446 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200447 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200448 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100449 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100450 QemuOpts *opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100451 const char *id;
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200452 bool has_driver_specific_opts;
Max Reitzfbf81752015-10-19 17:53:31 +0200453 BlockdevDetectZeroesOptions detect_zeroes =
454 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
455 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200456
Kevin Wolff298d072013-09-10 12:01:20 +0200457 /* Check common options by copying from bs_opts to opts, all other options
458 * stay in bs_opts for processing by bdrv_open(). */
459 id = qdict_get_try_str(bs_opts, "id");
Kevin Wolf00063832013-03-15 10:35:07 +0100460 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100461 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200462 error_propagate(errp, error);
Markus Armbruster6376f952014-05-28 11:17:01 +0200463 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100464 }
465
Kevin Wolf00063832013-03-15 10:35:07 +0100466 qemu_opts_absorb_qdict(opts, bs_opts, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100467 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200468 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100469 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100470 }
471
472 if (id) {
473 qdict_del(bs_opts, "id");
474 }
475
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200476 has_driver_specific_opts = !!qdict_size(bs_opts);
477
Markus Armbruster666daa62010-06-02 18:48:27 +0200478 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200479 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200480
Alberto Garcia362e9292015-10-28 17:33:04 +0200481 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
482 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
483
Max Reitzfbf81752015-10-19 17:53:31 +0200484 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
485 &detect_zeroes, &error);
486 if (error) {
487 error_propagate(errp, error);
488 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200489 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200490
491 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100492 if (is_help_option(buf)) {
493 error_printf("Supported formats:");
494 bdrv_iterate_format(bdrv_format_print, NULL);
495 error_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100496 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200497 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200498
Max Reitze4342ce2015-02-05 13:58:14 -0500499 if (qdict_haskey(bs_opts, "driver")) {
500 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100501 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400502 }
Max Reitze4342ce2015-02-05 13:58:14 -0500503 qdict_put(bs_opts, "driver", qstring_from_str(buf));
Markus Armbruster666daa62010-06-02 18:48:27 +0200504 }
505
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200506 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200507 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200508 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100509 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200510 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100511 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200512 }
513 }
514
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200515 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200516 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200517 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100518 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200519 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100520 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200521 }
522 }
523
Max Reitz5ec18f82015-10-19 17:53:30 +0200524 if (snapshot) {
525 /* always use cache=unsafe with snapshot */
526 bdrv_flags &= ~BDRV_O_CACHE_MASK;
527 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
528 }
529
Kevin Wolf326642b2013-07-11 12:52:34 +0200530 /* init */
Max Reitze4342ce2015-02-05 13:58:14 -0500531 if ((!file || !*file) && !has_driver_specific_opts) {
Max Reitz5ec18f82015-10-19 17:53:30 +0200532 BlockBackendRootState *blk_rs;
533
534 blk = blk_new(qemu_opts_id(opts), errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500535 if (!blk) {
536 goto early_err;
537 }
538
Max Reitz5ec18f82015-10-19 17:53:30 +0200539 blk_rs = blk_get_root_state(blk);
540 blk_rs->open_flags = bdrv_flags;
Max Reitzfbf81752015-10-19 17:53:31 +0200541 blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR);
Max Reitz5ec18f82015-10-19 17:53:30 +0200542 blk_rs->detect_zeroes = detect_zeroes;
543
544 if (throttle_enabled(&cfg)) {
545 if (!throttling_group) {
546 throttling_group = blk_name(blk);
547 }
548 blk_rs->throttle_group = g_strdup(throttling_group);
549 blk_rs->throttle_state = throttle_group_incref(throttling_group);
550 blk_rs->throttle_state->cfg = cfg;
551 }
Max Reitze4342ce2015-02-05 13:58:14 -0500552
553 QDECREF(bs_opts);
554 } else {
555 if (file && !*file) {
556 file = NULL;
557 }
558
Max Reitze4342ce2015-02-05 13:58:14 -0500559 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
560 errp);
561 if (!blk) {
562 goto err_no_bs_opts;
563 }
564 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500565
Max Reitz5ec18f82015-10-19 17:53:30 +0200566 bs->detect_zeroes = detect_zeroes;
567
568 /* disk I/O throttling */
569 if (throttle_enabled(&cfg)) {
570 if (!throttling_group) {
571 throttling_group = blk_name(blk);
572 }
573 bdrv_io_limits_enable(bs, throttling_group);
574 bdrv_set_io_limits(bs, &cfg);
575 }
576
577 if (bdrv_key_required(bs)) {
578 autostart = 0;
579 }
Alberto Garcia362e9292015-10-28 17:33:04 +0200580
581 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
Max Reitz5ec18f82015-10-19 17:53:30 +0200582 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200583
Max Reitz373340b2015-10-19 17:53:22 +0200584 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200585
Max Reitze4342ce2015-02-05 13:58:14 -0500586err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100587 qemu_opts_del(opts);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200588 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100589
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100590early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100591 qemu_opts_del(opts);
Markus Armbruster6376f952014-05-28 11:17:01 +0200592err_no_opts:
593 QDECREF(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100594 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200595}
596
Max Reitzbd745e22015-10-19 17:53:32 +0200597static QemuOptsList qemu_root_bds_opts;
598
599/* Takes the ownership of bs_opts */
600static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
601{
602 BlockDriverState *bs;
603 QemuOpts *opts;
604 Error *local_error = NULL;
605 BlockdevDetectZeroesOptions detect_zeroes;
606 int ret;
607 int bdrv_flags = 0;
608
609 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
610 if (!opts) {
611 goto fail;
612 }
613
614 qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
615 if (local_error) {
616 error_propagate(errp, local_error);
617 goto fail;
618 }
619
620 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
621 &detect_zeroes, &local_error);
622 if (local_error) {
623 error_propagate(errp, local_error);
624 goto fail;
625 }
626
627 bs = NULL;
628 ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp);
629 if (ret < 0) {
630 goto fail_no_bs_opts;
631 }
632
633 bs->detect_zeroes = detect_zeroes;
634
635fail_no_bs_opts:
636 qemu_opts_del(opts);
637 return bs;
638
639fail:
640 qemu_opts_del(opts);
641 QDECREF(bs_opts);
642 return NULL;
643}
644
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200645static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
646 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200647{
648 const char *value;
649
650 value = qemu_opt_get(opts, from);
651 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200652 if (qemu_opt_find(opts, to)) {
653 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
654 "same time", to, from);
655 return;
656 }
Jun Li20d6cd42014-09-24 13:45:27 +0800657 }
658
659 /* rename all items in opts */
660 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100661 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200662 qemu_opt_unset(opts, from);
663 }
664}
665
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200666QemuOptsList qemu_legacy_drive_opts = {
667 .name = "drive",
668 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
669 .desc = {
670 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200671 .name = "bus",
672 .type = QEMU_OPT_NUMBER,
673 .help = "bus number",
674 },{
675 .name = "unit",
676 .type = QEMU_OPT_NUMBER,
677 .help = "unit number (i.e. lun for scsi)",
678 },{
679 .name = "index",
680 .type = QEMU_OPT_NUMBER,
681 .help = "index number",
682 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200683 .name = "media",
684 .type = QEMU_OPT_STRING,
685 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200686 },{
687 .name = "if",
688 .type = QEMU_OPT_STRING,
689 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200690 },{
691 .name = "cyls",
692 .type = QEMU_OPT_NUMBER,
693 .help = "number of cylinders (ide disk geometry)",
694 },{
695 .name = "heads",
696 .type = QEMU_OPT_NUMBER,
697 .help = "number of heads (ide disk geometry)",
698 },{
699 .name = "secs",
700 .type = QEMU_OPT_NUMBER,
701 .help = "number of sectors (ide disk geometry)",
702 },{
703 .name = "trans",
704 .type = QEMU_OPT_STRING,
705 .help = "chs translation (auto, lba, none)",
Kevin Wolf26929292013-09-09 17:01:03 +0200706 },{
707 .name = "boot",
708 .type = QEMU_OPT_BOOL,
709 .help = "(deprecated, ignored)",
Kevin Wolf394c7d42013-09-13 14:09:17 +0200710 },{
711 .name = "addr",
712 .type = QEMU_OPT_STRING,
713 .help = "pci address (virtio only)",
Max Reitzd095b462013-12-20 19:28:14 +0100714 },{
Kevin Wolfbcf83152014-06-06 15:21:48 +0200715 .name = "serial",
716 .type = QEMU_OPT_STRING,
717 .help = "disk serial number",
718 },{
Max Reitzd095b462013-12-20 19:28:14 +0100719 .name = "file",
720 .type = QEMU_OPT_STRING,
721 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200722 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200723
724 /* Options that are passed on, but have special semantics with -drive */
725 {
726 .name = "read-only",
727 .type = QEMU_OPT_BOOL,
728 .help = "open drive file as read-only",
729 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100730 .name = "rerror",
731 .type = QEMU_OPT_STRING,
732 .help = "read error action",
733 },{
734 .name = "werror",
735 .type = QEMU_OPT_STRING,
736 .help = "write error action",
737 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200738 .name = "copy-on-read",
739 .type = QEMU_OPT_BOOL,
740 .help = "copy read data from backing file into image file",
741 },
742
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200743 { /* end of list */ }
744 },
745};
746
Markus Armbruster60e19e02014-06-06 14:50:58 +0200747DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
Kevin Wolf57975222013-07-17 14:41:54 +0200748{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200749 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200750 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200751 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200752 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200753 QemuOpts *legacy_opts;
754 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200755 BlockInterfaceType type;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200756 int cyls, heads, secs, translation;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200757 int max_devs, bus_id, unit_id, index;
Kevin Wolf394c7d42013-09-13 14:09:17 +0200758 const char *devaddr;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100759 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800760 bool read_only = false;
761 bool copy_on_read;
Kevin Wolfbcf83152014-06-06 15:21:48 +0200762 const char *serial;
Max Reitzd095b462013-12-20 19:28:14 +0100763 const char *filename;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200764 Error *local_err = NULL;
Kevin Wolf247147f2014-09-24 16:37:14 +0200765 int i;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200766
Kevin Wolf57975222013-07-17 14:41:54 +0200767 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200768 static const struct {
769 const char *from;
770 const char *to;
771 } opt_renames[] = {
772 { "iops", "throttling.iops-total" },
773 { "iops_rd", "throttling.iops-read" },
774 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200775
Kevin Wolf247147f2014-09-24 16:37:14 +0200776 { "bps", "throttling.bps-total" },
777 { "bps_rd", "throttling.bps-read" },
778 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200779
Kevin Wolf247147f2014-09-24 16:37:14 +0200780 { "iops_max", "throttling.iops-total-max" },
781 { "iops_rd_max", "throttling.iops-read-max" },
782 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200783
Kevin Wolf247147f2014-09-24 16:37:14 +0200784 { "bps_max", "throttling.bps-total-max" },
785 { "bps_rd_max", "throttling.bps-read-max" },
786 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200787
Kevin Wolf247147f2014-09-24 16:37:14 +0200788 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200789
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200790 { "group", "throttling.group" },
791
Kevin Wolf247147f2014-09-24 16:37:14 +0200792 { "readonly", "read-only" },
793 };
794
795 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200796 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
797 &local_err);
798 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100799 error_report_err(local_err);
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200800 return NULL;
801 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200802 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200803
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200804 value = qemu_opt_get(all_opts, "cache");
805 if (value) {
806 int flags = 0;
807
808 if (bdrv_parse_cache_flags(value, &flags) != 0) {
809 error_report("invalid cache option");
810 return NULL;
811 }
812
813 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200814 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
815 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100816 !!(flags & BDRV_O_CACHE_WB), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200817 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200818 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
819 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100820 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200821 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200822 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
823 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100824 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200825 }
826 qemu_opt_unset(all_opts, "cache");
827 }
828
Kevin Wolff298d072013-09-10 12:01:20 +0200829 /* Get a QDict for processing the options */
830 bs_opts = qdict_new();
831 qemu_opts_to_qdict(all_opts, bs_opts);
832
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800833 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
834 &error_abort);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200835 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100836 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100837 error_report_err(local_err);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200838 goto fail;
839 }
840
Kevin Wolf26929292013-09-09 17:01:03 +0200841 /* Deprecated option boot=[on|off] */
842 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
843 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
844 "ignored. Future versions will reject this parameter. Please "
845 "update your scripts.\n");
846 }
847
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200848 /* Media type */
849 value = qemu_opt_get(legacy_opts, "media");
850 if (value) {
851 if (!strcmp(value, "disk")) {
852 media = MEDIA_DISK;
853 } else if (!strcmp(value, "cdrom")) {
854 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800855 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200856 } else {
857 error_report("'%s' invalid media", value);
858 goto fail;
859 }
860 }
861
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200862 /* copy-on-read is disabled with a warning for read-only devices */
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800863 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200864 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
865
866 if (read_only && copy_on_read) {
867 error_report("warning: disabling copy-on-read on read-only drive");
868 copy_on_read = false;
869 }
870
871 qdict_put(bs_opts, "read-only",
872 qstring_from_str(read_only ? "on" : "off"));
873 qdict_put(bs_opts, "copy-on-read",
874 qstring_from_str(copy_on_read ? "on" :"off"));
875
Kevin Wolf593d4642013-08-28 17:24:51 +0200876 /* Controller type */
877 value = qemu_opt_get(legacy_opts, "if");
878 if (value) {
879 for (type = 0;
880 type < IF_COUNT && strcmp(value, if_name[type]);
881 type++) {
882 }
883 if (type == IF_COUNT) {
884 error_report("unsupported bus type '%s'", value);
885 goto fail;
886 }
887 } else {
888 type = block_default_type;
889 }
890
Kevin Wolfb41a7332013-09-09 16:49:49 +0200891 /* Geometry */
892 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
893 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
894 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
895
896 if (cyls || heads || secs) {
897 if (cyls < 1) {
898 error_report("invalid physical cyls number");
899 goto fail;
900 }
901 if (heads < 1) {
902 error_report("invalid physical heads number");
903 goto fail;
904 }
905 if (secs < 1) {
906 error_report("invalid physical secs number");
907 goto fail;
908 }
909 }
910
911 translation = BIOS_ATA_TRANSLATION_AUTO;
912 value = qemu_opt_get(legacy_opts, "trans");
913 if (value != NULL) {
914 if (!cyls) {
915 error_report("'%s' trans must be used with cyls, heads and secs",
916 value);
917 goto fail;
918 }
919 if (!strcmp(value, "none")) {
920 translation = BIOS_ATA_TRANSLATION_NONE;
921 } else if (!strcmp(value, "lba")) {
922 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +0100923 } else if (!strcmp(value, "large")) {
924 translation = BIOS_ATA_TRANSLATION_LARGE;
925 } else if (!strcmp(value, "rechs")) {
926 translation = BIOS_ATA_TRANSLATION_RECHS;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200927 } else if (!strcmp(value, "auto")) {
928 translation = BIOS_ATA_TRANSLATION_AUTO;
929 } else {
930 error_report("'%s' invalid translation type", value);
931 goto fail;
932 }
933 }
934
935 if (media == MEDIA_CDROM) {
936 if (cyls || secs || heads) {
937 error_report("CHS can't be set with media=cdrom");
938 goto fail;
939 }
940 }
941
Kevin Wolf87a899c2013-09-10 15:48:13 +0200942 /* Device address specified by bus/unit or index.
943 * If none was specified, try to find the first free one. */
944 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
945 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
946 index = qemu_opt_get_number(legacy_opts, "index", -1);
947
948 max_devs = if_max_devs[type];
949
950 if (index != -1) {
951 if (bus_id != 0 || unit_id != -1) {
952 error_report("index cannot be used with bus and unit");
953 goto fail;
954 }
955 bus_id = drive_index_to_bus_id(type, index);
956 unit_id = drive_index_to_unit_id(type, index);
957 }
958
959 if (unit_id == -1) {
960 unit_id = 0;
961 while (drive_get(type, bus_id, unit_id) != NULL) {
962 unit_id++;
963 if (max_devs && unit_id >= max_devs) {
964 unit_id -= max_devs;
965 bus_id++;
966 }
967 }
968 }
969
970 if (max_devs && unit_id >= max_devs) {
971 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
972 goto fail;
973 }
974
975 if (drive_get(type, bus_id, unit_id) != NULL) {
976 error_report("drive with bus=%d, unit=%d (index=%d) exists",
977 bus_id, unit_id, index);
978 goto fail;
979 }
980
Kevin Wolfbcf83152014-06-06 15:21:48 +0200981 /* Serial number */
982 serial = qemu_opt_get(legacy_opts, "serial");
983
Kevin Wolf87a899c2013-09-10 15:48:13 +0200984 /* no id supplied -> create one */
985 if (qemu_opts_id(all_opts) == NULL) {
986 char *new_id;
987 const char *mediastr = "";
988 if (type == IF_IDE || type == IF_SCSI) {
989 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
990 }
991 if (max_devs) {
992 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
993 mediastr, unit_id);
994 } else {
995 new_id = g_strdup_printf("%s%s%i", if_name[type],
996 mediastr, unit_id);
997 }
998 qdict_put(bs_opts, "id", qstring_from_str(new_id));
999 g_free(new_id);
1000 }
1001
Kevin Wolf394c7d42013-09-13 14:09:17 +02001002 /* Add virtio block device */
1003 devaddr = qemu_opt_get(legacy_opts, "addr");
1004 if (devaddr && type != IF_VIRTIO) {
1005 error_report("addr is not supported by this bus type");
1006 goto fail;
1007 }
1008
1009 if (type == IF_VIRTIO) {
1010 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001011 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1012 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001013 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +02001014 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001015 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001016 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001017 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001018 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1019 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001020 if (devaddr) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001021 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001022 }
1023 }
1024
Max Reitzd095b462013-12-20 19:28:14 +01001025 filename = qemu_opt_get(legacy_opts, "file");
1026
Kevin Wolfee13ed12014-02-09 09:52:32 +01001027 /* Check werror/rerror compatibility with if=... */
1028 werror = qemu_opt_get(legacy_opts, "werror");
1029 if (werror != NULL) {
1030 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1031 type != IF_NONE) {
1032 error_report("werror is not supported by this bus type");
1033 goto fail;
1034 }
1035 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1036 }
1037
1038 rerror = qemu_opt_get(legacy_opts, "rerror");
1039 if (rerror != NULL) {
1040 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1041 type != IF_NONE) {
1042 error_report("rerror is not supported by this bus type");
1043 goto fail;
1044 }
1045 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1046 }
1047
Kevin Wolf2d246f02013-09-18 15:14:47 +02001048 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster18e46a02014-10-07 13:59:06 +02001049 blk = blockdev_init(filename, bs_opts, &local_err);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001050 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +02001051 if (!blk) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001052 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001053 error_report_err(local_err);
Kevin Wolfb6810722013-09-20 11:33:11 +02001054 }
Kevin Wolf2d246f02013-09-18 15:14:47 +02001055 goto fail;
Kevin Wolfb6810722013-09-20 11:33:11 +02001056 } else {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001057 assert(!local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001058 }
1059
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001060 /* Create legacy DriveInfo */
1061 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001062 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001063
Kevin Wolfb41a7332013-09-09 16:49:49 +02001064 dinfo->cyls = cyls;
1065 dinfo->heads = heads;
1066 dinfo->secs = secs;
1067 dinfo->trans = translation;
1068
Kevin Wolfee13ed12014-02-09 09:52:32 +01001069 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001070 dinfo->bus = bus_id;
1071 dinfo->unit = unit_id;
Kevin Wolf394c7d42013-09-13 14:09:17 +02001072 dinfo->devaddr = devaddr;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001073 dinfo->serial = g_strdup(serial);
1074
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001075 blk_set_legacy_dinfo(blk, dinfo);
1076
Kevin Wolfe34ef042013-09-19 14:24:10 +02001077 switch(type) {
1078 case IF_IDE:
1079 case IF_SCSI:
1080 case IF_XEN:
1081 case IF_NONE:
1082 dinfo->media_cd = media == MEDIA_CDROM;
1083 break;
1084 default:
1085 break;
1086 }
1087
Kevin Wolf2d246f02013-09-18 15:14:47 +02001088fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001089 qemu_opts_del(legacy_opts);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001090 QDECREF(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001091 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001092}
1093
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001094void hmp_commit(Monitor *mon, const QDict *qdict)
Markus Armbruster666daa62010-06-02 18:48:27 +02001095{
Markus Armbruster666daa62010-06-02 18:48:27 +02001096 const char *device = qdict_get_str(qdict, "device");
Fam Zhenga0e85442015-03-02 19:36:48 +08001097 BlockBackend *blk;
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001098 int ret;
Markus Armbruster666daa62010-06-02 18:48:27 +02001099
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001100 if (!strcmp(device, "all")) {
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001101 ret = bdrv_commit_all();
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001102 } else {
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001103 BlockDriverState *bs;
1104 AioContext *aio_context;
1105
Fam Zhenga0e85442015-03-02 19:36:48 +08001106 blk = blk_by_name(device);
1107 if (!blk) {
Jeff Cody58513bd2013-01-18 12:45:35 -05001108 monitor_printf(mon, "Device '%s' not found\n", device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +02001109 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001110 }
Max Reitz5433c242015-10-19 17:53:29 +02001111 if (!blk_is_available(blk)) {
1112 monitor_printf(mon, "Device '%s' has no medium\n", device);
1113 return;
1114 }
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001115
1116 bs = blk_bs(blk);
1117 aio_context = bdrv_get_aio_context(bs);
1118 aio_context_acquire(aio_context);
1119
1120 ret = bdrv_commit(bs);
1121
1122 aio_context_release(aio_context);
Jeff Cody58513bd2013-01-18 12:45:35 -05001123 }
1124 if (ret < 0) {
1125 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1126 strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +02001127 }
1128}
1129
Eric Blake6a8f9662015-10-26 16:34:54 -06001130static void blockdev_do_action(TransactionActionKind type, void *data,
1131 Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001132{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001133 TransactionAction action;
1134 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001135
Eric Blake6a8f9662015-10-26 16:34:54 -06001136 action.type = type;
1137 action.u.data = data;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001138 list.value = &action;
1139 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001140 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001141}
1142
Benoît Canet0901f672014-01-23 21:31:38 +01001143void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1144 bool has_node_name, const char *node_name,
1145 const char *snapshot_file,
1146 bool has_snapshot_node_name,
1147 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001148 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001149 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001150{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001151 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001152 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001153 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001154 .has_node_name = has_node_name,
1155 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001156 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001157 .has_snapshot_node_name = has_snapshot_node_name,
1158 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001159 .has_format = has_format,
1160 .format = (char *) format,
1161 .has_mode = has_mode,
1162 .mode = mode,
1163 };
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001164 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1165 &snapshot, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001166}
1167
Alberto Garcia43de7e22015-10-26 14:27:16 +02001168void qmp_blockdev_snapshot(const char *node, const char *overlay,
1169 Error **errp)
1170{
1171 BlockdevSnapshot snapshot_data = {
1172 .node = (char *) node,
1173 .overlay = (char *) overlay
1174 };
1175
1176 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
1177 &snapshot_data, errp);
1178}
1179
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001180void qmp_blockdev_snapshot_internal_sync(const char *device,
1181 const char *name,
1182 Error **errp)
1183{
1184 BlockdevSnapshotInternal snapshot = {
1185 .device = (char *) device,
1186 .name = (char *) name
1187 };
1188
1189 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1190 &snapshot, errp);
1191}
1192
Wenchao Xia44e3e052013-09-11 14:04:36 +08001193SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1194 bool has_id,
1195 const char *id,
1196 bool has_name,
1197 const char *name,
1198 Error **errp)
1199{
Fam Zhenga0e85442015-03-02 19:36:48 +08001200 BlockDriverState *bs;
1201 BlockBackend *blk;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001202 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001203 QEMUSnapshotInfo sn;
1204 Error *local_err = NULL;
1205 SnapshotInfo *info = NULL;
1206 int ret;
1207
Fam Zhenga0e85442015-03-02 19:36:48 +08001208 blk = blk_by_name(device);
1209 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001210 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1211 "Device '%s' not found", device);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001212 return NULL;
1213 }
Max Reitz5433c242015-10-19 17:53:29 +02001214
1215 aio_context = blk_get_aio_context(blk);
1216 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001217
1218 if (!has_id) {
1219 id = NULL;
1220 }
1221
1222 if (!has_name) {
1223 name = NULL;
1224 }
1225
1226 if (!id && !name) {
1227 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001228 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001229 }
1230
Max Reitz5433c242015-10-19 17:53:29 +02001231 if (!blk_is_available(blk)) {
1232 error_setg(errp, "Device '%s' has no medium", device);
1233 goto out_aio_context;
1234 }
1235 bs = blk_bs(blk);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001236
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001237 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1238 goto out_aio_context;
1239 }
1240
Wenchao Xia44e3e052013-09-11 14:04:36 +08001241 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001242 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001243 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001244 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001245 }
1246 if (!ret) {
1247 error_setg(errp,
1248 "Snapshot with id '%s' and name '%s' does not exist on "
1249 "device '%s'",
1250 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001251 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001252 }
1253
1254 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001255 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001256 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001257 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001258 }
1259
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001260 aio_context_release(aio_context);
1261
Markus Armbruster5839e532014-08-19 10:31:08 +02001262 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001263 info->id = g_strdup(sn.id_str);
1264 info->name = g_strdup(sn.name);
1265 info->date_nsec = sn.date_nsec;
1266 info->date_sec = sn.date_sec;
1267 info->vm_state_size = sn.vm_state_size;
1268 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1269 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1270
1271 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001272
1273out_aio_context:
1274 aio_context_release(aio_context);
1275 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001276}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001277
John Snow341ebc22015-04-17 19:49:52 -04001278/**
1279 * block_dirty_bitmap_lookup:
1280 * Return a dirty bitmap (if present), after validating
1281 * the node reference and bitmap names.
1282 *
1283 * @node: The name of the BDS node to search for bitmaps
1284 * @name: The name of the bitmap to search for
1285 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1286 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1287 * @errp: Output pointer for error information. Can be NULL.
1288 *
1289 * @return: A bitmap object on success, or NULL on failure.
1290 */
1291static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1292 const char *name,
1293 BlockDriverState **pbs,
1294 AioContext **paio,
1295 Error **errp)
1296{
1297 BlockDriverState *bs;
1298 BdrvDirtyBitmap *bitmap;
1299 AioContext *aio_context;
1300
1301 if (!node) {
1302 error_setg(errp, "Node cannot be NULL");
1303 return NULL;
1304 }
1305 if (!name) {
1306 error_setg(errp, "Bitmap name cannot be NULL");
1307 return NULL;
1308 }
1309 bs = bdrv_lookup_bs(node, node, NULL);
1310 if (!bs) {
1311 error_setg(errp, "Node '%s' not found", node);
1312 return NULL;
1313 }
1314
1315 aio_context = bdrv_get_aio_context(bs);
1316 aio_context_acquire(aio_context);
1317
1318 bitmap = bdrv_find_dirty_bitmap(bs, name);
1319 if (!bitmap) {
1320 error_setg(errp, "Dirty bitmap '%s' not found", name);
1321 goto fail;
1322 }
1323
1324 if (pbs) {
1325 *pbs = bs;
1326 }
1327 if (paio) {
1328 *paio = aio_context;
1329 } else {
1330 aio_context_release(aio_context);
1331 }
1332
1333 return bitmap;
1334
1335 fail:
1336 aio_context_release(aio_context);
1337 return NULL;
1338}
1339
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001340/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001341
John Snow50f43f02015-11-05 18:13:09 -05001342typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001343
John Snow50f43f02015-11-05 18:13:09 -05001344/**
1345 * BlkActionOps:
1346 * Table of operations that define an Action.
1347 *
1348 * @instance_size: Size of state struct, in bytes.
1349 * @prepare: Prepare the work, must NOT be NULL.
1350 * @commit: Commit the changes, can be NULL.
1351 * @abort: Abort the changes on fail, can be NULL.
1352 * @clean: Clean up resources after all transaction actions have called
1353 * commit() or abort(). Can be NULL.
1354 *
1355 * Only prepare() may fail. In a single transaction, only one of commit() or
1356 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001357 */
John Snow50f43f02015-11-05 18:13:09 -05001358typedef struct BlkActionOps {
1359 size_t instance_size;
1360 void (*prepare)(BlkActionState *common, Error **errp);
1361 void (*commit)(BlkActionState *common);
1362 void (*abort)(BlkActionState *common);
1363 void (*clean)(BlkActionState *common);
1364} BlkActionOps;
1365
1366/**
1367 * BlkActionState:
1368 * Describes one Action's state within a Transaction.
1369 *
1370 * @action: QAPI-defined enum identifying which Action to perform.
1371 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001372 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001373 * @entry: List membership for all Actions in this Transaction.
1374 *
1375 * This structure must be arranged as first member in a subclassed type,
1376 * assuming that the compiler will also arrange it to the same offsets as the
1377 * base class.
1378 */
1379struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001380 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001381 const BlkActionOps *ops;
John Snow94d16a62015-11-05 18:13:18 -05001382 BlockJobTxn *block_job_txn;
1383 TransactionProperties *txn_props;
John Snow50f43f02015-11-05 18:13:09 -05001384 QSIMPLEQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001385};
1386
Wenchao Xiabbe86012013-09-11 14:04:34 +08001387/* internal snapshot private data */
1388typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001389 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001390 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001391 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001392 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001393 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001394} InternalSnapshotState;
1395
John Snow94d16a62015-11-05 18:13:18 -05001396
1397static int action_check_completion_mode(BlkActionState *s, Error **errp)
1398{
1399 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1400 error_setg(errp,
1401 "Action '%s' does not support Transaction property "
1402 "completion-mode = %s",
1403 TransactionActionKind_lookup[s->action->type],
1404 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1405 return -1;
1406 }
1407 return 0;
1408}
1409
John Snow50f43f02015-11-05 18:13:09 -05001410static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001411 Error **errp)
1412{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001413 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001414 const char *device;
1415 const char *name;
Fam Zhenga0e85442015-03-02 19:36:48 +08001416 BlockBackend *blk;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001417 BlockDriverState *bs;
1418 QEMUSnapshotInfo old_sn, *sn;
1419 bool ret;
1420 qemu_timeval tv;
1421 BlockdevSnapshotInternal *internal;
1422 InternalSnapshotState *state;
1423 int ret1;
1424
Eric Blake6a8f9662015-10-26 16:34:54 -06001425 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001426 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake6a8f9662015-10-26 16:34:54 -06001427 internal = common->action->u.blockdev_snapshot_internal_sync;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001428 state = DO_UPCAST(InternalSnapshotState, common, common);
1429
1430 /* 1. parse input */
1431 device = internal->device;
1432 name = internal->name;
1433
1434 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001435 if (action_check_completion_mode(common, errp) < 0) {
1436 return;
1437 }
1438
Fam Zhenga0e85442015-03-02 19:36:48 +08001439 blk = blk_by_name(device);
1440 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001441 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1442 "Device '%s' not found", device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001443 return;
1444 }
1445
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001446 /* AioContext is released in .clean() */
Max Reitz5433c242015-10-19 17:53:29 +02001447 state->aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001448 aio_context_acquire(state->aio_context);
1449
Max Reitz5433c242015-10-19 17:53:29 +02001450 if (!blk_is_available(blk)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001451 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001452 return;
1453 }
Max Reitz5433c242015-10-19 17:53:29 +02001454 bs = blk_bs(blk);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001455
Fam Zheng507306c2015-10-23 11:08:13 +08001456 state->bs = bs;
1457 bdrv_drained_begin(bs);
1458
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001459 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1460 return;
1461 }
1462
Wenchao Xiabbe86012013-09-11 14:04:34 +08001463 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001464 error_setg(errp, "Device '%s' is read only", device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001465 return;
1466 }
1467
1468 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001469 error_setg(errp, "Block format '%s' used by device '%s' "
1470 "does not support internal snapshots",
1471 bs->drv->format_name, device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001472 return;
1473 }
1474
1475 if (!strlen(name)) {
1476 error_setg(errp, "Name is empty");
1477 return;
1478 }
1479
1480 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001481 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1482 &local_err);
1483 if (local_err) {
1484 error_propagate(errp, local_err);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001485 return;
1486 } else if (ret) {
1487 error_setg(errp,
1488 "Snapshot with name '%s' already exists on device '%s'",
1489 name, device);
1490 return;
1491 }
1492
1493 /* 3. take the snapshot */
1494 sn = &state->sn;
1495 pstrcpy(sn->name, sizeof(sn->name), name);
1496 qemu_gettimeofday(&tv);
1497 sn->date_sec = tv.tv_sec;
1498 sn->date_nsec = tv.tv_usec * 1000;
1499 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1500
1501 ret1 = bdrv_snapshot_create(bs, sn);
1502 if (ret1 < 0) {
1503 error_setg_errno(errp, -ret1,
1504 "Failed to create snapshot '%s' on device '%s'",
1505 name, device);
1506 return;
1507 }
1508
1509 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001510 state->created = true;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001511}
1512
John Snow50f43f02015-11-05 18:13:09 -05001513static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001514{
1515 InternalSnapshotState *state =
1516 DO_UPCAST(InternalSnapshotState, common, common);
1517 BlockDriverState *bs = state->bs;
1518 QEMUSnapshotInfo *sn = &state->sn;
1519 Error *local_error = NULL;
1520
Fam Zheng507306c2015-10-23 11:08:13 +08001521 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001522 return;
1523 }
1524
1525 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1526 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1527 "device '%s' in abort: %s",
1528 sn->id_str,
1529 sn->name,
1530 bdrv_get_device_name(bs),
1531 error_get_pretty(local_error));
1532 error_free(local_error);
1533 }
1534}
1535
John Snow50f43f02015-11-05 18:13:09 -05001536static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001537{
1538 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1539 common, common);
1540
1541 if (state->aio_context) {
Fam Zheng507306c2015-10-23 11:08:13 +08001542 if (state->bs) {
1543 bdrv_drained_end(state->bs);
1544 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001545 aio_context_release(state->aio_context);
1546 }
1547}
1548
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001549/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001550typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001551 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001552 BlockDriverState *old_bs;
1553 BlockDriverState *new_bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001554 AioContext *aio_context;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001555} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001556
John Snow50f43f02015-11-05 18:13:09 -05001557static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001558 Error **errp)
1559{
Alberto Garcia43de7e22015-10-26 14:27:16 +02001560 int flags = 0, ret;
1561 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001562 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001563 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001564 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001565 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001566 /* Reference to the new image (for 'blockdev-snapshot') */
1567 const char *snapshot_ref;
1568 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001569 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001570 ExternalSnapshotState *state =
1571 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001572 TransactionAction *action = common->action;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001573
Alberto Garcia43de7e22015-10-26 14:27:16 +02001574 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1575 * purpose but a different set of parameters */
1576 switch (action->type) {
1577 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1578 {
1579 BlockdevSnapshot *s = action->u.blockdev_snapshot;
1580 device = s->node;
1581 node_name = s->node;
1582 new_image_file = NULL;
1583 snapshot_ref = s->overlay;
1584 }
1585 break;
1586 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1587 {
1588 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1589 device = s->has_device ? s->device : NULL;
1590 node_name = s->has_node_name ? s->node_name : NULL;
1591 new_image_file = s->snapshot_file;
1592 snapshot_ref = NULL;
1593 }
1594 break;
1595 default:
1596 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001597 }
1598
1599 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001600 if (action_check_completion_mode(common, errp) < 0) {
1601 return;
1602 }
1603
Alberto Garcia43de7e22015-10-26 14:27:16 +02001604 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1605 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001606 return;
1607 }
1608
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001609 /* Acquire AioContext now so any threads operating on old_bs stop */
1610 state->aio_context = bdrv_get_aio_context(state->old_bs);
1611 aio_context_acquire(state->aio_context);
Fam Zhengda763e82015-10-23 11:08:10 +08001612 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001613
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001614 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001615 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001616 return;
1617 }
1618
Fam Zheng3718d8a2014-05-23 21:29:43 +08001619 if (bdrv_op_is_blocked(state->old_bs,
1620 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001621 return;
1622 }
1623
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001624 if (!bdrv_is_read_only(state->old_bs)) {
1625 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001626 error_setg(errp, QERR_IO_ERROR);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001627 return;
1628 }
1629 }
1630
Benoît Canet212a5a82014-01-23 21:31:36 +01001631 if (!bdrv_is_first_non_filter(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001632 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
Benoît Canetf6186f42013-10-02 14:33:48 +02001633 return;
1634 }
1635
Alberto Garcia43de7e22015-10-26 14:27:16 +02001636 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
1637 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1638 const char *format = s->has_format ? s->format : "qcow2";
1639 enum NewImageMode mode;
1640 const char *snapshot_node_name =
1641 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001642
Alberto Garcia43de7e22015-10-26 14:27:16 +02001643 if (node_name && !snapshot_node_name) {
1644 error_setg(errp, "New snapshot node name missing");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001645 return;
1646 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001647
1648 if (snapshot_node_name &&
1649 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1650 error_setg(errp, "New snapshot node name already in use");
1651 return;
1652 }
1653
1654 flags = state->old_bs->open_flags;
1655
1656 /* create new image w/backing file */
1657 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1658 if (mode != NEW_IMAGE_MODE_EXISTING) {
1659 bdrv_img_create(new_image_file, format,
1660 state->old_bs->filename,
1661 state->old_bs->drv->format_name,
1662 NULL, -1, flags, &local_err, false);
1663 if (local_err) {
1664 error_propagate(errp, local_err);
1665 return;
1666 }
1667 }
1668
1669 options = qdict_new();
1670 if (s->has_snapshot_node_name) {
1671 qdict_put(options, "node-name",
1672 qstring_from_str(snapshot_node_name));
1673 }
1674 qdict_put(options, "driver", qstring_from_str(format));
1675
1676 flags |= BDRV_O_NO_BACKING;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001677 }
1678
Max Reitzf67503e2014-02-18 18:33:05 +01001679 assert(state->new_bs == NULL);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001680 ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options,
1681 flags, errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001682 /* We will manually add the backing_hd field to the bs later */
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001683 if (ret != 0) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001684 return;
1685 }
1686
1687 if (state->new_bs->blk != NULL) {
1688 error_setg(errp, "The snapshot is already in use by %s",
1689 blk_name(state->new_bs->blk));
1690 return;
1691 }
1692
1693 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1694 errp)) {
1695 return;
1696 }
1697
1698 if (state->new_bs->backing != NULL) {
1699 error_setg(errp, "The snapshot already has a backing image");
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001700 return;
1701 }
1702
1703 if (!state->new_bs->drv->supports_backing) {
1704 error_setg(errp, "The snapshot does not support backing images");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001705 }
1706}
1707
John Snow50f43f02015-11-05 18:13:09 -05001708static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001709{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001710 ExternalSnapshotState *state =
1711 DO_UPCAST(ExternalSnapshotState, common, common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001712
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001713 bdrv_set_aio_context(state->new_bs, state->aio_context);
1714
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001715 /* This removes our old bs and adds the new bs */
1716 bdrv_append(state->new_bs, state->old_bs);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001717 /* We don't need (or want) to use the transactional
1718 * bdrv_reopen_multiple() across all the entries at once, because we
1719 * don't want to abort all of them if one of them fails the reopen */
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001720 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001721 NULL);
1722}
1723
John Snow50f43f02015-11-05 18:13:09 -05001724static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001725{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001726 ExternalSnapshotState *state =
1727 DO_UPCAST(ExternalSnapshotState, common, common);
1728 if (state->new_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001729 bdrv_unref(state->new_bs);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001730 }
Fam Zhengda763e82015-10-23 11:08:10 +08001731}
1732
John Snow50f43f02015-11-05 18:13:09 -05001733static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001734{
1735 ExternalSnapshotState *state =
1736 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001737 if (state->aio_context) {
Fam Zhengda763e82015-10-23 11:08:10 +08001738 bdrv_drained_end(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001739 aio_context_release(state->aio_context);
1740 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001741}
1742
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001743typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001744 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001745 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001746 AioContext *aio_context;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001747 BlockJob *job;
1748} DriveBackupState;
1749
John Snow78f51fd2015-11-05 18:13:17 -05001750static void do_drive_backup(const char *device, const char *target,
1751 bool has_format, const char *format,
1752 enum MirrorSyncMode sync,
1753 bool has_mode, enum NewImageMode mode,
1754 bool has_speed, int64_t speed,
1755 bool has_bitmap, const char *bitmap,
1756 bool has_on_source_error,
1757 BlockdevOnError on_source_error,
1758 bool has_on_target_error,
1759 BlockdevOnError on_target_error,
1760 BlockJobTxn *txn, Error **errp);
1761
John Snow50f43f02015-11-05 18:13:09 -05001762static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001763{
1764 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Fam Zhenga0e85442015-03-02 19:36:48 +08001765 BlockBackend *blk;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001766 DriveBackup *backup;
1767 Error *local_err = NULL;
1768
Eric Blake6a8f9662015-10-26 16:34:54 -06001769 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1770 backup = common->action->u.drive_backup;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001771
Fam Zhenga0e85442015-03-02 19:36:48 +08001772 blk = blk_by_name(backup->device);
1773 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001774 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1775 "Device '%s' not found", backup->device);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001776 return;
1777 }
1778
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001779 if (!blk_is_available(blk)) {
1780 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1781 return;
1782 }
1783
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001784 /* AioContext is released in .clean() */
Max Reitz5433c242015-10-19 17:53:29 +02001785 state->aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001786 aio_context_acquire(state->aio_context);
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001787 bdrv_drained_begin(blk_bs(blk));
1788 state->bs = blk_bs(blk);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001789
John Snow78f51fd2015-11-05 18:13:17 -05001790 do_drive_backup(backup->device, backup->target,
1791 backup->has_format, backup->format,
1792 backup->sync,
1793 backup->has_mode, backup->mode,
1794 backup->has_speed, backup->speed,
1795 backup->has_bitmap, backup->bitmap,
1796 backup->has_on_source_error, backup->on_source_error,
1797 backup->has_on_target_error, backup->on_target_error,
John Snow94d16a62015-11-05 18:13:18 -05001798 common->block_job_txn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001799 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001800 error_propagate(errp, local_err);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001801 return;
1802 }
1803
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001804 state->job = state->bs->job;
1805}
1806
John Snow50f43f02015-11-05 18:13:09 -05001807static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001808{
1809 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1810 BlockDriverState *bs = state->bs;
1811
1812 /* Only cancel if it's the job we started */
1813 if (bs && bs->job && bs->job == state->job) {
1814 block_job_cancel_sync(bs->job);
1815 }
1816}
1817
John Snow50f43f02015-11-05 18:13:09 -05001818static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001819{
1820 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1821
1822 if (state->aio_context) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001823 bdrv_drained_end(state->bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001824 aio_context_release(state->aio_context);
1825 }
1826}
1827
Fam Zhengbd8baec2014-12-18 18:37:06 +08001828typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001829 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001830 BlockDriverState *bs;
1831 BlockJob *job;
1832 AioContext *aio_context;
1833} BlockdevBackupState;
1834
John Snow78f51fd2015-11-05 18:13:17 -05001835static void do_blockdev_backup(const char *device, const char *target,
1836 enum MirrorSyncMode sync,
1837 bool has_speed, int64_t speed,
1838 bool has_on_source_error,
1839 BlockdevOnError on_source_error,
1840 bool has_on_target_error,
1841 BlockdevOnError on_target_error,
1842 BlockJobTxn *txn, Error **errp);
1843
John Snow50f43f02015-11-05 18:13:09 -05001844static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001845{
1846 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1847 BlockdevBackup *backup;
Max Reitz5433c242015-10-19 17:53:29 +02001848 BlockBackend *blk, *target;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001849 Error *local_err = NULL;
1850
Eric Blake6a8f9662015-10-26 16:34:54 -06001851 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1852 backup = common->action->u.blockdev_backup;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001853
Fam Zhenga0e85442015-03-02 19:36:48 +08001854 blk = blk_by_name(backup->device);
1855 if (!blk) {
Markus Armbruster5b347c52015-03-17 08:36:14 +01001856 error_setg(errp, "Device '%s' not found", backup->device);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001857 return;
1858 }
1859
Fam Zhengff52bf32015-10-23 11:08:12 +08001860 if (!blk_is_available(blk)) {
1861 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1862 return;
1863 }
1864
Max Reitz5433c242015-10-19 17:53:29 +02001865 target = blk_by_name(backup->target);
1866 if (!target) {
Markus Armbruster5b347c52015-03-17 08:36:14 +01001867 error_setg(errp, "Device '%s' not found", backup->target);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001868 return;
1869 }
1870
1871 /* AioContext is released in .clean() */
Max Reitz5433c242015-10-19 17:53:29 +02001872 state->aio_context = blk_get_aio_context(blk);
1873 if (state->aio_context != blk_get_aio_context(target)) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001874 state->aio_context = NULL;
1875 error_setg(errp, "Backup between two IO threads is not implemented");
1876 return;
1877 }
1878 aio_context_acquire(state->aio_context);
Fam Zhengff52bf32015-10-23 11:08:12 +08001879 state->bs = blk_bs(blk);
1880 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001881
John Snow78f51fd2015-11-05 18:13:17 -05001882 do_blockdev_backup(backup->device, backup->target,
1883 backup->sync,
1884 backup->has_speed, backup->speed,
1885 backup->has_on_source_error, backup->on_source_error,
1886 backup->has_on_target_error, backup->on_target_error,
John Snow94d16a62015-11-05 18:13:18 -05001887 common->block_job_txn, &local_err);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001888 if (local_err) {
1889 error_propagate(errp, local_err);
1890 return;
1891 }
1892
Fam Zhengbd8baec2014-12-18 18:37:06 +08001893 state->job = state->bs->job;
1894}
1895
John Snow50f43f02015-11-05 18:13:09 -05001896static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001897{
1898 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1899 BlockDriverState *bs = state->bs;
1900
1901 /* Only cancel if it's the job we started */
1902 if (bs && bs->job && bs->job == state->job) {
1903 block_job_cancel_sync(bs->job);
1904 }
1905}
1906
John Snow50f43f02015-11-05 18:13:09 -05001907static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001908{
1909 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1910
1911 if (state->aio_context) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001912 bdrv_drained_end(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001913 aio_context_release(state->aio_context);
1914 }
1915}
1916
Fam Zhengdf9a6812015-11-09 18:16:54 +08001917typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05001918 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001919 BdrvDirtyBitmap *bitmap;
1920 BlockDriverState *bs;
1921 AioContext *aio_context;
1922 HBitmap *backup;
1923 bool prepared;
1924} BlockDirtyBitmapState;
1925
John Snow50f43f02015-11-05 18:13:09 -05001926static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08001927 Error **errp)
1928{
1929 Error *local_err = NULL;
1930 BlockDirtyBitmapAdd *action;
1931 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1932 common, common);
1933
John Snow94d16a62015-11-05 18:13:18 -05001934 if (action_check_completion_mode(common, errp) < 0) {
1935 return;
1936 }
1937
John Snow50f43f02015-11-05 18:13:09 -05001938 action = common->action->u.block_dirty_bitmap_add;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001939 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1940 qmp_block_dirty_bitmap_add(action->node, action->name,
1941 action->has_granularity, action->granularity,
1942 &local_err);
1943
1944 if (!local_err) {
1945 state->prepared = true;
1946 } else {
1947 error_propagate(errp, local_err);
1948 }
1949}
1950
John Snow50f43f02015-11-05 18:13:09 -05001951static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08001952{
1953 BlockDirtyBitmapAdd *action;
1954 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1955 common, common);
1956
John Snow50f43f02015-11-05 18:13:09 -05001957 action = common->action->u.block_dirty_bitmap_add;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001958 /* Should not be able to fail: IF the bitmap was added via .prepare(),
1959 * then the node reference and bitmap name must have been valid.
1960 */
1961 if (state->prepared) {
1962 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
1963 }
1964}
1965
John Snow50f43f02015-11-05 18:13:09 -05001966static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08001967 Error **errp)
1968{
1969 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1970 common, common);
1971 BlockDirtyBitmap *action;
1972
John Snow94d16a62015-11-05 18:13:18 -05001973 if (action_check_completion_mode(common, errp) < 0) {
1974 return;
1975 }
1976
John Snow50f43f02015-11-05 18:13:09 -05001977 action = common->action->u.block_dirty_bitmap_clear;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001978 state->bitmap = block_dirty_bitmap_lookup(action->node,
1979 action->name,
1980 &state->bs,
1981 &state->aio_context,
1982 errp);
1983 if (!state->bitmap) {
1984 return;
1985 }
1986
1987 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
1988 error_setg(errp, "Cannot modify a frozen bitmap");
1989 return;
1990 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
1991 error_setg(errp, "Cannot clear a disabled bitmap");
1992 return;
1993 }
1994
1995 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
1996 /* AioContext is released in .clean() */
1997}
1998
John Snow50f43f02015-11-05 18:13:09 -05001999static void block_dirty_bitmap_clear_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002000{
2001 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2002 common, common);
2003
2004 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2005}
2006
John Snow50f43f02015-11-05 18:13:09 -05002007static void block_dirty_bitmap_clear_commit(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002008{
2009 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2010 common, common);
2011
2012 hbitmap_free(state->backup);
2013}
2014
John Snow50f43f02015-11-05 18:13:09 -05002015static void block_dirty_bitmap_clear_clean(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002016{
2017 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2018 common, common);
2019
2020 if (state->aio_context) {
2021 aio_context_release(state->aio_context);
2022 }
2023}
2024
John Snow50f43f02015-11-05 18:13:09 -05002025static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002026{
2027 error_setg(errp, "Transaction aborted using Abort action");
2028}
2029
John Snow50f43f02015-11-05 18:13:09 -05002030static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002031{
Stefan Weildfc6f862013-07-25 18:21:28 +02002032 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002033}
2034
John Snow50f43f02015-11-05 18:13:09 -05002035static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002036 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2037 .instance_size = sizeof(ExternalSnapshotState),
2038 .prepare = external_snapshot_prepare,
2039 .commit = external_snapshot_commit,
2040 .abort = external_snapshot_abort,
2041 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002042 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002043 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002044 .prepare = external_snapshot_prepare,
2045 .commit = external_snapshot_commit,
2046 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002047 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002048 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002049 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2050 .instance_size = sizeof(DriveBackupState),
2051 .prepare = drive_backup_prepare,
2052 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002053 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002054 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002055 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2056 .instance_size = sizeof(BlockdevBackupState),
2057 .prepare = blockdev_backup_prepare,
2058 .abort = blockdev_backup_abort,
2059 .clean = blockdev_backup_clean,
2060 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002061 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002062 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002063 .prepare = abort_prepare,
2064 .commit = abort_commit,
2065 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002066 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2067 .instance_size = sizeof(InternalSnapshotState),
2068 .prepare = internal_snapshot_prepare,
2069 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002070 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002071 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002072 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2073 .instance_size = sizeof(BlockDirtyBitmapState),
2074 .prepare = block_dirty_bitmap_add_prepare,
2075 .abort = block_dirty_bitmap_add_abort,
2076 },
2077 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2078 .instance_size = sizeof(BlockDirtyBitmapState),
2079 .prepare = block_dirty_bitmap_clear_prepare,
2080 .commit = block_dirty_bitmap_clear_commit,
2081 .abort = block_dirty_bitmap_clear_abort,
2082 .clean = block_dirty_bitmap_clear_clean,
2083 }
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002084};
2085
John Snow94d16a62015-11-05 18:13:18 -05002086/**
2087 * Allocate a TransactionProperties structure if necessary, and fill
2088 * that structure with desired defaults if they are unset.
2089 */
2090static TransactionProperties *get_transaction_properties(
2091 TransactionProperties *props)
2092{
2093 if (!props) {
2094 props = g_new0(TransactionProperties, 1);
2095 }
2096
2097 if (!props->has_completion_mode) {
2098 props->has_completion_mode = true;
2099 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2100 }
2101
2102 return props;
2103}
2104
Jeff Cody8802d1f2012-02-28 15:54:06 -05002105/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002106 * 'Atomic' group operations. The operations are performed as a set, and if
2107 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002108 */
John Snow94d16a62015-11-05 18:13:18 -05002109void qmp_transaction(TransactionActionList *dev_list,
2110 bool has_props,
2111 struct TransactionProperties *props,
2112 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002113{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002114 TransactionActionList *dev_entry = dev_list;
John Snow94d16a62015-11-05 18:13:18 -05002115 BlockJobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002116 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002117 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002118
John Snow50f43f02015-11-05 18:13:09 -05002119 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002120 QSIMPLEQ_INIT(&snap_bdrv_states);
2121
John Snow94d16a62015-11-05 18:13:18 -05002122 /* Does this transaction get canceled as a group on failure?
2123 * If not, we don't really need to make a BlockJobTxn.
2124 */
2125 props = get_transaction_properties(props);
2126 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2127 block_job_txn = block_job_txn_new();
2128 }
2129
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002130 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002131 bdrv_drain_all();
2132
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002133 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002134 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002135 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002136 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002137
Jeff Cody8802d1f2012-02-28 15:54:06 -05002138 dev_info = dev_entry->value;
2139 dev_entry = dev_entry->next;
2140
Eric Blake6a8f9662015-10-26 16:34:54 -06002141 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002142
Eric Blake6a8f9662015-10-26 16:34:54 -06002143 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002144 assert(ops->instance_size > 0);
2145
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002146 state = g_malloc0(ops->instance_size);
2147 state->ops = ops;
2148 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002149 state->block_job_txn = block_job_txn;
2150 state->txn_props = props;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002151 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002152
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002153 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002154 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002155 error_propagate(errp, local_err);
2156 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002157 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002158 }
2159
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002160 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002161 if (state->ops->commit) {
2162 state->ops->commit(state);
2163 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002164 }
2165
2166 /* success */
2167 goto exit;
2168
2169delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002170 /* failure, and it is all-or-none; roll back all operations */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002171 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2172 if (state->ops->abort) {
2173 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002174 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002175 }
2176exit:
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002177 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2178 if (state->ops->clean) {
2179 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002180 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002181 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002182 }
John Snow94d16a62015-11-05 18:13:18 -05002183 if (!has_props) {
2184 qapi_free_TransactionProperties(props);
2185 }
2186 block_job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002187}
2188
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002189void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002190{
Max Reitz38f54bd2015-10-26 21:39:12 +01002191 Error *local_err = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +02002192
Max Reitz38f54bd2015-10-26 21:39:12 +01002193 qmp_blockdev_open_tray(device, has_force, force, &local_err);
2194 if (local_err) {
2195 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002196 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002197 }
Luiz Capitulino92d48552011-12-07 15:47:23 -02002198
Max Reitz38f54bd2015-10-26 21:39:12 +01002199 qmp_blockdev_remove_medium(device, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002200}
2201
Benoît Canet12d3ba82014-01-23 21:31:35 +01002202void qmp_block_passwd(bool has_device, const char *device,
2203 bool has_node_name, const char *node_name,
2204 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002205{
Benoît Canet12d3ba82014-01-23 21:31:35 +01002206 Error *local_err = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +02002207 BlockDriverState *bs;
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002208 AioContext *aio_context;
Markus Armbruster666daa62010-06-02 18:48:27 +02002209
Benoît Canet12d3ba82014-01-23 21:31:35 +01002210 bs = bdrv_lookup_bs(has_device ? device : NULL,
2211 has_node_name ? node_name : NULL,
2212 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002213 if (local_err) {
Benoît Canet12d3ba82014-01-23 21:31:35 +01002214 error_propagate(errp, local_err);
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02002215 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002216 }
2217
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002218 aio_context = bdrv_get_aio_context(bs);
2219 aio_context_acquire(aio_context);
2220
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002221 bdrv_add_key(bs, password, errp);
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002222
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002223 aio_context_release(aio_context);
Markus Armbruster666daa62010-06-02 18:48:27 +02002224}
2225
Max Reitz7d8a9f72015-10-26 21:39:08 +01002226void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
2227 Error **errp)
2228{
2229 BlockBackend *blk;
2230 bool locked;
2231
2232 if (!has_force) {
2233 force = false;
2234 }
2235
2236 blk = blk_by_name(device);
2237 if (!blk) {
2238 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2239 "Device '%s' not found", device);
2240 return;
2241 }
2242
2243 if (!blk_dev_has_removable_media(blk)) {
2244 error_setg(errp, "Device '%s' is not removable", device);
2245 return;
2246 }
2247
2248 if (blk_dev_is_tray_open(blk)) {
2249 return;
2250 }
2251
2252 locked = blk_dev_is_medium_locked(blk);
2253 if (locked) {
2254 blk_dev_eject_request(blk, force);
2255 }
2256
2257 if (!locked || force) {
2258 blk_dev_change_media_cb(blk, false);
2259 }
2260}
2261
Max Reitzabaaf592015-10-26 21:39:09 +01002262void qmp_blockdev_close_tray(const char *device, Error **errp)
2263{
2264 BlockBackend *blk;
2265
2266 blk = blk_by_name(device);
2267 if (!blk) {
2268 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2269 "Device '%s' not found", device);
2270 return;
2271 }
2272
2273 if (!blk_dev_has_removable_media(blk)) {
2274 error_setg(errp, "Device '%s' is not removable", device);
2275 return;
2276 }
2277
2278 if (!blk_dev_is_tray_open(blk)) {
2279 return;
2280 }
2281
2282 blk_dev_change_media_cb(blk, true);
2283}
2284
Max Reitz2814f672015-10-26 21:39:10 +01002285void qmp_blockdev_remove_medium(const char *device, Error **errp)
2286{
2287 BlockBackend *blk;
2288 BlockDriverState *bs;
2289 AioContext *aio_context;
2290 bool has_device;
2291
2292 blk = blk_by_name(device);
2293 if (!blk) {
2294 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2295 "Device '%s' not found", device);
2296 return;
2297 }
2298
2299 /* For BBs without a device, we can exchange the BDS tree at will */
2300 has_device = blk_get_attached_dev(blk);
2301
2302 if (has_device && !blk_dev_has_removable_media(blk)) {
2303 error_setg(errp, "Device '%s' is not removable", device);
2304 return;
2305 }
2306
2307 if (has_device && !blk_dev_is_tray_open(blk)) {
2308 error_setg(errp, "Tray of device '%s' is not open", device);
2309 return;
2310 }
2311
2312 bs = blk_bs(blk);
2313 if (!bs) {
2314 return;
2315 }
2316
2317 aio_context = bdrv_get_aio_context(bs);
2318 aio_context_acquire(aio_context);
2319
2320 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2321 goto out;
2322 }
2323
2324 /* This follows the convention established by bdrv_make_anon() */
2325 if (bs->device_list.tqe_prev) {
2326 QTAILQ_REMOVE(&bdrv_states, bs, device_list);
2327 bs->device_list.tqe_prev = NULL;
2328 }
2329
2330 blk_remove_bs(blk);
2331
2332out:
2333 aio_context_release(aio_context);
2334}
2335
Max Reitzd1299882015-10-26 21:39:11 +01002336static void qmp_blockdev_insert_anon_medium(const char *device,
2337 BlockDriverState *bs, Error **errp)
2338{
2339 BlockBackend *blk;
2340 bool has_device;
2341
2342 blk = blk_by_name(device);
2343 if (!blk) {
2344 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2345 "Device '%s' not found", device);
2346 return;
2347 }
2348
2349 /* For BBs without a device, we can exchange the BDS tree at will */
2350 has_device = blk_get_attached_dev(blk);
2351
2352 if (has_device && !blk_dev_has_removable_media(blk)) {
2353 error_setg(errp, "Device '%s' is not removable", device);
2354 return;
2355 }
2356
2357 if (has_device && !blk_dev_is_tray_open(blk)) {
2358 error_setg(errp, "Tray of device '%s' is not open", device);
2359 return;
2360 }
2361
2362 if (blk_bs(blk)) {
2363 error_setg(errp, "There already is a medium in device '%s'", device);
2364 return;
2365 }
2366
2367 blk_insert_bs(blk, bs);
2368
2369 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
2370}
2371
2372void qmp_blockdev_insert_medium(const char *device, const char *node_name,
2373 Error **errp)
2374{
2375 BlockDriverState *bs;
2376
2377 bs = bdrv_find_node(node_name);
2378 if (!bs) {
2379 error_setg(errp, "Node '%s' not found", node_name);
2380 return;
2381 }
2382
2383 if (bs->blk) {
2384 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2385 blk_name(bs->blk));
2386 return;
2387 }
2388
2389 qmp_blockdev_insert_anon_medium(device, bs, errp);
2390}
2391
Max Reitz24fb4132015-11-06 16:27:06 +01002392void qmp_blockdev_change_medium(const char *device, const char *filename,
2393 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002394 bool has_read_only,
2395 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002396 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002397{
2398 BlockBackend *blk;
2399 BlockDriverState *medium_bs = NULL;
2400 int bdrv_flags, ret;
2401 QDict *options = NULL;
2402 Error *err = NULL;
2403
2404 blk = blk_by_name(device);
2405 if (!blk) {
2406 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2407 "Device '%s' not found", device);
2408 goto fail;
2409 }
2410
2411 if (blk_bs(blk)) {
2412 blk_update_root_state(blk);
2413 }
2414
2415 bdrv_flags = blk_get_open_flags_from_root_state(blk);
2416
Max Reitz39ff43d2015-11-11 04:49:44 +01002417 if (!has_read_only) {
2418 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2419 }
2420
2421 switch (read_only) {
2422 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2423 break;
2424
2425 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2426 bdrv_flags &= ~BDRV_O_RDWR;
2427 break;
2428
2429 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2430 bdrv_flags |= BDRV_O_RDWR;
2431 break;
2432
2433 default:
2434 abort();
2435 }
2436
Max Reitz24fb4132015-11-06 16:27:06 +01002437 if (has_format) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002438 options = qdict_new();
2439 qdict_put(options, "driver", qstring_from_str(format));
2440 }
2441
2442 assert(!medium_bs);
2443 ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2444 if (ret < 0) {
2445 goto fail;
2446 }
2447
2448 blk_apply_root_state(blk, medium_bs);
2449
2450 bdrv_add_key(medium_bs, NULL, &err);
2451 if (err) {
2452 error_propagate(errp, err);
2453 goto fail;
2454 }
2455
2456 qmp_blockdev_open_tray(device, false, false, &err);
2457 if (err) {
2458 error_propagate(errp, err);
2459 goto fail;
2460 }
2461
2462 qmp_blockdev_remove_medium(device, &err);
2463 if (err) {
2464 error_propagate(errp, err);
2465 goto fail;
2466 }
2467
2468 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2469 if (err) {
2470 error_propagate(errp, err);
2471 goto fail;
2472 }
2473
2474 qmp_blockdev_close_tray(device, errp);
2475
2476fail:
2477 /* If the medium has been inserted, the device has its own reference, so
2478 * ours must be relinquished; and if it has not been inserted successfully,
2479 * the reference must be relinquished anyway */
2480 bdrv_unref(medium_bs);
2481}
2482
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002483/* throttling disk I/O limits */
Luiz Capitulino80047da2011-12-14 16:49:14 -02002484void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
Benoît Canet3e9fab62013-09-02 14:14:40 +02002485 int64_t bps_wr,
2486 int64_t iops,
2487 int64_t iops_rd,
2488 int64_t iops_wr,
2489 bool has_bps_max,
2490 int64_t bps_max,
2491 bool has_bps_rd_max,
2492 int64_t bps_rd_max,
2493 bool has_bps_wr_max,
2494 int64_t bps_wr_max,
2495 bool has_iops_max,
2496 int64_t iops_max,
2497 bool has_iops_rd_max,
2498 int64_t iops_rd_max,
2499 bool has_iops_wr_max,
Benoît Canet2024c1d2013-09-02 14:14:41 +02002500 int64_t iops_wr_max,
2501 bool has_iops_size,
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002502 int64_t iops_size,
2503 bool has_group,
2504 const char *group, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002505{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002506 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002507 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002508 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002509 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002510
Fam Zhenga0e85442015-03-02 19:36:48 +08002511 blk = blk_by_name(device);
2512 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01002513 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2514 "Device '%s' not found", device);
Luiz Capitulino80047da2011-12-14 16:49:14 -02002515 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002516 }
Max Reitz5433c242015-10-19 17:53:29 +02002517
2518 aio_context = blk_get_aio_context(blk);
2519 aio_context_acquire(aio_context);
2520
Fam Zhenga0e85442015-03-02 19:36:48 +08002521 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002522 if (!bs) {
2523 error_setg(errp, "Device '%s' has no medium", device);
2524 goto out;
2525 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002526
Benoît Canetcc0681c2013-09-02 14:14:39 +02002527 memset(&cfg, 0, sizeof(cfg));
2528 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2529 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
2530 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002531
Benoît Canetcc0681c2013-09-02 14:14:39 +02002532 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2533 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
2534 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2535
Benoît Canet3e9fab62013-09-02 14:14:40 +02002536 if (has_bps_max) {
2537 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
2538 }
2539 if (has_bps_rd_max) {
2540 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2541 }
2542 if (has_bps_wr_max) {
2543 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2544 }
2545 if (has_iops_max) {
2546 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2547 }
2548 if (has_iops_rd_max) {
2549 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2550 }
2551 if (has_iops_wr_max) {
2552 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2553 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002554
Benoît Canet2024c1d2013-09-02 14:14:41 +02002555 if (has_iops_size) {
2556 cfg.op_size = iops_size;
2557 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002558
2559 if (!check_throttle_config(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002560 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002561 }
2562
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002563 if (throttle_enabled(&cfg)) {
2564 /* Enable I/O limits if they're not enabled yet, otherwise
2565 * just update the throttling group. */
Alberto Garciaa0d64a62015-11-04 15:15:36 +02002566 if (!bs->throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002567 bdrv_io_limits_enable(bs, has_group ? group : device);
2568 } else if (has_group) {
2569 bdrv_io_limits_update_group(bs, group);
2570 }
2571 /* Set the new throttling configuration */
Benoît Canetcc0681c2013-09-02 14:14:39 +02002572 bdrv_set_io_limits(bs, &cfg);
Alberto Garciaa0d64a62015-11-04 15:15:36 +02002573 } else if (bs->throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002574 /* If all throttling settings are set to 0, disable I/O limits */
2575 bdrv_io_limits_disable(bs);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002576 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002577
Max Reitz5433c242015-10-19 17:53:29 +02002578out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002579 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002580}
2581
John Snow341ebc22015-04-17 19:49:52 -04002582void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2583 bool has_granularity, uint32_t granularity,
2584 Error **errp)
2585{
2586 AioContext *aio_context;
2587 BlockDriverState *bs;
2588
2589 if (!name || name[0] == '\0') {
2590 error_setg(errp, "Bitmap name cannot be empty");
2591 return;
2592 }
2593
2594 bs = bdrv_lookup_bs(node, node, errp);
2595 if (!bs) {
2596 return;
2597 }
2598
2599 aio_context = bdrv_get_aio_context(bs);
2600 aio_context_acquire(aio_context);
2601
2602 if (has_granularity) {
2603 if (granularity < 512 || !is_power_of_2(granularity)) {
2604 error_setg(errp, "Granularity must be power of 2 "
2605 "and at least 512");
2606 goto out;
2607 }
2608 } else {
2609 /* Default to cluster size, if available: */
2610 granularity = bdrv_get_default_bitmap_granularity(bs);
2611 }
2612
2613 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2614
2615 out:
2616 aio_context_release(aio_context);
2617}
2618
2619void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2620 Error **errp)
2621{
2622 AioContext *aio_context;
2623 BlockDriverState *bs;
2624 BdrvDirtyBitmap *bitmap;
2625
2626 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2627 if (!bitmap || !bs) {
2628 return;
2629 }
2630
John Snow9bd2b082015-04-17 19:49:57 -04002631 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2632 error_setg(errp,
2633 "Bitmap '%s' is currently frozen and cannot be removed",
2634 name);
2635 goto out;
2636 }
John Snow20dca812015-04-17 19:50:02 -04002637 bdrv_dirty_bitmap_make_anon(bitmap);
John Snow341ebc22015-04-17 19:49:52 -04002638 bdrv_release_dirty_bitmap(bs, bitmap);
2639
John Snow9bd2b082015-04-17 19:49:57 -04002640 out:
John Snow341ebc22015-04-17 19:49:52 -04002641 aio_context_release(aio_context);
2642}
2643
John Snowe74e6b72015-04-17 19:49:59 -04002644/**
2645 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2646 * immediately after a full backup operation.
2647 */
2648void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2649 Error **errp)
2650{
2651 AioContext *aio_context;
2652 BdrvDirtyBitmap *bitmap;
2653 BlockDriverState *bs;
2654
2655 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2656 if (!bitmap || !bs) {
2657 return;
2658 }
2659
2660 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2661 error_setg(errp,
2662 "Bitmap '%s' is currently frozen and cannot be modified",
2663 name);
2664 goto out;
2665 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2666 error_setg(errp,
2667 "Bitmap '%s' is currently disabled and cannot be cleared",
2668 name);
2669 goto out;
2670 }
2671
Fam Zhengdf9a6812015-11-09 18:16:54 +08002672 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04002673
2674 out:
2675 aio_context_release(aio_context);
2676}
2677
Markus Armbruster072ebe62015-03-05 17:00:56 +01002678void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06002679{
2680 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002681 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06002682 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002683 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08002684 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06002685
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002686 blk = blk_by_name(id);
2687 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02002688 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01002689 return;
Ryan Harper9063f812010-11-12 11:07:13 -06002690 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002691
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02002692 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02002693 error_report("Deleting device added with blockdev-add"
2694 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01002695 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02002696 }
2697
Max Reitz5433c242015-10-19 17:53:29 +02002698 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002699 aio_context_acquire(aio_context);
2700
Max Reitz5433c242015-10-19 17:53:29 +02002701 bs = blk_bs(blk);
2702 if (bs) {
2703 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2704 error_report_err(local_err);
2705 aio_context_release(aio_context);
2706 return;
2707 }
Ryan Harper9063f812010-11-12 11:07:13 -06002708
Max Reitz5433c242015-10-19 17:53:29 +02002709 bdrv_close(bs);
2710 }
Ryan Harper9063f812010-11-12 11:07:13 -06002711
Markus Armbrusterfa879d62011-08-03 15:07:40 +02002712 /* if we have a device attached to this BlockDriverState
Ryan Harperd22b2f42011-03-29 20:51:47 -05002713 * then we need to make the drive anonymous until the device
2714 * can be removed. If this is a drive with no device backing
2715 * then we can just get rid of the block driver state right here.
2716 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02002717 if (blk_get_attached_dev(blk)) {
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002718 blk_hide_on_behalf_of_hmp_drive_del(blk);
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02002719 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02002720 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2721 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05002722 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02002723 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06002724 }
2725
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002726 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06002727}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002728
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002729void qmp_block_resize(bool has_device, const char *device,
2730 bool has_node_name, const char *node_name,
2731 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002732{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002733 Error *local_err = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002734 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002735 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02002736 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002737
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002738 bs = bdrv_lookup_bs(has_device ? device : NULL,
2739 has_node_name ? node_name : NULL,
2740 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002741 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002742 error_propagate(errp, local_err);
2743 return;
2744 }
2745
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002746 aio_context = bdrv_get_aio_context(bs);
2747 aio_context_acquire(aio_context);
2748
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002749 if (!bdrv_is_first_non_filter(bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002750 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002751 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002752 }
2753
2754 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002755 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002756 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002757 }
2758
Jeff Cody9c75e162014-06-25 16:55:30 -04002759 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002760 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002761 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04002762 }
2763
Peter Lieven92b7a082013-03-11 11:04:24 +01002764 /* complete all in-flight operations before resizing the device */
2765 bdrv_drain_all();
2766
Kevin Wolf87329012013-05-02 15:32:55 +02002767 ret = bdrv_truncate(bs, size);
2768 switch (ret) {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002769 case 0:
2770 break;
2771 case -ENOMEDIUM:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002772 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002773 break;
2774 case -ENOTSUP:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002775 error_setg(errp, QERR_UNSUPPORTED);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002776 break;
2777 case -EACCES:
Alberto Garcia81e5f782015-04-08 12:29:19 +03002778 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002779 break;
2780 case -EBUSY:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002781 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002782 break;
2783 default:
Kevin Wolf87329012013-05-02 15:32:55 +02002784 error_setg_errno(errp, -ret, "Could not resize");
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002785 break;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002786 }
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002787
2788out:
2789 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002790}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002791
Jeff Cody9abf2db2012-09-27 13:29:14 -04002792static void block_job_cb(void *opaque, int ret)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002793{
Stefan Hajnoczi723c5d92014-10-21 12:03:53 +01002794 /* Note that this function may be executed from another AioContext besides
2795 * the QEMU main loop. If you need to access anything that assumes the
2796 * QEMU global mutex, use a BH or introduce a mutex.
2797 */
2798
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002799 BlockDriverState *bs = opaque;
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002800 const char *msg = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002801
Jeff Cody9abf2db2012-09-27 13:29:14 -04002802 trace_block_job_cb(bs, bs->job, ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002803
2804 assert(bs->job);
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002805
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002806 if (ret < 0) {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002807 msg = strerror(-ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002808 }
2809
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002810 if (block_job_is_cancelled(bs->job)) {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002811 block_job_event_cancelled(bs->job);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002812 } else {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002813 block_job_event_completed(bs->job, msg);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002814 }
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002815}
2816
Jeff Cody13d8cc52014-06-25 15:40:11 -04002817void qmp_block_stream(const char *device,
2818 bool has_base, const char *base,
2819 bool has_backing_file, const char *backing_file,
2820 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002821 bool has_on_error, BlockdevOnError on_error,
2822 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002823{
Fam Zhenga0e85442015-03-02 19:36:48 +08002824 BlockBackend *blk;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002825 BlockDriverState *bs;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002826 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002827 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002828 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002829 const char *base_name = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002830
Paolo Bonzini1d809092012-09-28 17:22:59 +02002831 if (!has_on_error) {
2832 on_error = BLOCKDEV_ON_ERROR_REPORT;
2833 }
2834
Fam Zhenga0e85442015-03-02 19:36:48 +08002835 blk = blk_by_name(device);
2836 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01002837 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2838 "Device '%s' not found", device);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002839 return;
2840 }
2841
Max Reitz5433c242015-10-19 17:53:29 +02002842 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002843 aio_context_acquire(aio_context);
2844
Max Reitz5433c242015-10-19 17:53:29 +02002845 if (!blk_is_available(blk)) {
2846 error_setg(errp, "Device '%s' has no medium", device);
2847 goto out;
2848 }
2849 bs = blk_bs(blk);
2850
Fam Zheng628ff682014-05-23 21:29:44 +08002851 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002852 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08002853 }
2854
Jeff Cody13d8cc52014-06-25 15:40:11 -04002855 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002856 base_bs = bdrv_find_backing_image(bs, base);
2857 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002858 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002859 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002860 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002861 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04002862 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002863 }
2864
Jeff Cody13d8cc52014-06-25 15:40:11 -04002865 /* if we are streaming the entire chain, the result will have no backing
2866 * file, and specifying one is therefore an error */
2867 if (base_bs == NULL && has_backing_file) {
2868 error_setg(errp, "backing file specified, but streaming the "
2869 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002870 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002871 }
2872
2873 /* backing_file string overrides base bs filename */
2874 base_name = has_backing_file ? backing_file : base_name;
2875
2876 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002877 on_error, block_job_cb, bs, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002878 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002879 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002880 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002881 }
2882
2883 trace_qmp_block_stream(bs, bs->job);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002884
2885out:
2886 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002887}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00002888
Jeff Codyed61fc12012-09-27 13:29:16 -04002889void qmp_block_commit(const char *device,
Jeff Cody7676e2c2014-06-30 15:14:15 +02002890 bool has_base, const char *base,
2891 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04002892 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04002893 bool has_speed, int64_t speed,
2894 Error **errp)
2895{
Fam Zhenga0e85442015-03-02 19:36:48 +08002896 BlockBackend *blk;
Jeff Codyed61fc12012-09-27 13:29:16 -04002897 BlockDriverState *bs;
2898 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002899 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04002900 Error *local_err = NULL;
2901 /* This will be part of the QMP command, if/when the
2902 * BlockdevOnError change for blkmirror makes it in
2903 */
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02002904 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
Jeff Codyed61fc12012-09-27 13:29:16 -04002905
Max Reitz54504662014-04-10 19:36:25 +02002906 if (!has_speed) {
2907 speed = 0;
2908 }
2909
Jeff Cody7676e2c2014-06-30 15:14:15 +02002910 /* Important Note:
2911 * libvirt relies on the DeviceNotFound error class in order to probe for
2912 * live commit feature versions; for this to work, we must make sure to
2913 * perform the device lookup before any generic errors that may occur in a
2914 * scenario in which all optional arguments are omitted. */
Fam Zhenga0e85442015-03-02 19:36:48 +08002915 blk = blk_by_name(device);
2916 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01002917 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2918 "Device '%s' not found", device);
Jeff Codyed61fc12012-09-27 13:29:16 -04002919 return;
2920 }
Jeff Codyed61fc12012-09-27 13:29:16 -04002921
Max Reitz5433c242015-10-19 17:53:29 +02002922 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002923 aio_context_acquire(aio_context);
2924
Max Reitz5433c242015-10-19 17:53:29 +02002925 if (!blk_is_available(blk)) {
2926 error_setg(errp, "Device '%s' has no medium", device);
2927 goto out;
2928 }
2929 bs = blk_bs(blk);
2930
Fam Zhengbb000212014-09-11 13:14:00 +08002931 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002932 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08002933 }
2934
Jeff Codyed61fc12012-09-27 13:29:16 -04002935 /* default top_bs is the active layer */
2936 top_bs = bs;
2937
Jeff Cody7676e2c2014-06-30 15:14:15 +02002938 if (has_top && top) {
Jeff Codyed61fc12012-09-27 13:29:16 -04002939 if (strcmp(bs->filename, top) != 0) {
2940 top_bs = bdrv_find_backing_image(bs, top);
2941 }
2942 }
2943
2944 if (top_bs == NULL) {
2945 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002946 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04002947 }
2948
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002949 assert(bdrv_get_aio_context(top_bs) == aio_context);
2950
Jeff Codyd5208c42012-10-16 15:49:10 -04002951 if (has_base && base) {
2952 base_bs = bdrv_find_backing_image(top_bs, base);
2953 } else {
2954 base_bs = bdrv_find_base(top_bs);
2955 }
2956
2957 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002958 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002959 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04002960 }
2961
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002962 assert(bdrv_get_aio_context(base_bs) == aio_context);
2963
Fam Zhengbb000212014-09-11 13:14:00 +08002964 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2965 goto out;
2966 }
2967
Jeff Cody7676e2c2014-06-30 15:14:15 +02002968 /* Do not allow attempts to commit an image into itself */
2969 if (top_bs == base_bs) {
2970 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002971 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02002972 }
2973
Fam Zheng20a63d22013-12-16 14:45:31 +08002974 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04002975 if (has_backing_file) {
2976 error_setg(errp, "'backing-file' specified,"
2977 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002978 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04002979 }
Fam Zheng20a63d22013-12-16 14:45:31 +08002980 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2981 bs, &local_err);
2982 } else {
2983 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
Jeff Cody54e26902014-06-25 15:40:10 -04002984 has_backing_file ? backing_file : NULL, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08002985 }
Jeff Codyed61fc12012-09-27 13:29:16 -04002986 if (local_err != NULL) {
2987 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002988 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04002989 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002990
2991out:
2992 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04002993}
2994
John Snow78f51fd2015-11-05 18:13:17 -05002995static void do_drive_backup(const char *device, const char *target,
2996 bool has_format, const char *format,
2997 enum MirrorSyncMode sync,
2998 bool has_mode, enum NewImageMode mode,
2999 bool has_speed, int64_t speed,
3000 bool has_bitmap, const char *bitmap,
3001 bool has_on_source_error,
3002 BlockdevOnError on_source_error,
3003 bool has_on_target_error,
3004 BlockdevOnError on_target_error,
3005 BlockJobTxn *txn, Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003006{
Fam Zhenga0e85442015-03-02 19:36:48 +08003007 BlockBackend *blk;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003008 BlockDriverState *bs;
3009 BlockDriverState *target_bs;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003010 BlockDriverState *source = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003011 BdrvDirtyBitmap *bmap = NULL;
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003012 AioContext *aio_context;
Max Reitze6641712015-08-26 19:47:48 +02003013 QDict *options = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003014 Error *local_err = NULL;
3015 int flags;
3016 int64_t size;
3017 int ret;
3018
3019 if (!has_speed) {
3020 speed = 0;
3021 }
3022 if (!has_on_source_error) {
3023 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3024 }
3025 if (!has_on_target_error) {
3026 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3027 }
3028 if (!has_mode) {
3029 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3030 }
3031
Fam Zhenga0e85442015-03-02 19:36:48 +08003032 blk = blk_by_name(device);
3033 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01003034 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3035 "Device '%s' not found", device);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003036 return;
3037 }
3038
Max Reitz5433c242015-10-19 17:53:29 +02003039 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003040 aio_context_acquire(aio_context);
3041
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003042 /* Although backup_run has this check too, we need to use bs->drv below, so
3043 * do an early check redundantly. */
Max Reitz5433c242015-10-19 17:53:29 +02003044 if (!blk_is_available(blk)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003045 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003046 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003047 }
Max Reitz5433c242015-10-19 17:53:29 +02003048 bs = blk_bs(blk);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003049
3050 if (!has_format) {
3051 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3052 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003053
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003054 /* Early check to avoid creating target */
Fam Zheng3718d8a2014-05-23 21:29:43 +08003055 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003056 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003057 }
3058
3059 flags = bs->open_flags | BDRV_O_RDWR;
3060
Ian Mainfc5d3f82013-07-26 11:39:04 -07003061 /* See if we have a backing HD we can use to create our new image
3062 * on top of. */
3063 if (sync == MIRROR_SYNC_MODE_TOP) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003064 source = backing_bs(bs);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003065 if (!source) {
3066 sync = MIRROR_SYNC_MODE_FULL;
3067 }
3068 }
3069 if (sync == MIRROR_SYNC_MODE_NONE) {
3070 source = bs;
3071 }
3072
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003073 size = bdrv_getlength(bs);
3074 if (size < 0) {
3075 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003076 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003077 }
3078
3079 if (mode != NEW_IMAGE_MODE_EXISTING) {
Max Reitze6641712015-08-26 19:47:48 +02003080 assert(format);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003081 if (source) {
3082 bdrv_img_create(target, format, source->filename,
3083 source->drv->format_name, NULL,
3084 size, flags, &local_err, false);
3085 } else {
3086 bdrv_img_create(target, format, NULL, NULL, NULL,
3087 size, flags, &local_err, false);
3088 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003089 }
3090
Markus Armbruster84d18f02014-01-30 15:07:28 +01003091 if (local_err) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003092 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003093 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003094 }
3095
Max Reitze6641712015-08-26 19:47:48 +02003096 if (format) {
3097 options = qdict_new();
3098 qdict_put(options, "driver", qstring_from_str(format));
3099 }
3100
Max Reitzf67503e2014-02-18 18:33:05 +01003101 target_bs = NULL;
Max Reitz6ebf9aa2015-08-26 19:47:49 +02003102 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003103 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02003104 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003105 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003106 }
3107
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003108 bdrv_set_aio_context(target_bs, aio_context);
3109
John Snowd58d8452015-04-17 19:49:58 -04003110 if (has_bitmap) {
3111 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3112 if (!bmap) {
3113 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
3114 goto out;
3115 }
3116 }
3117
3118 backup_start(bs, target_bs, speed, sync, bmap,
3119 on_source_error, on_target_error,
John Snow78f51fd2015-11-05 18:13:17 -05003120 block_job_cb, bs, txn, &local_err);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003121 if (local_err != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08003122 bdrv_unref(target_bs);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003123 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003124 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003125 }
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003126
3127out:
3128 aio_context_release(aio_context);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003129}
3130
John Snow78f51fd2015-11-05 18:13:17 -05003131void qmp_drive_backup(const char *device, const char *target,
3132 bool has_format, const char *format,
3133 enum MirrorSyncMode sync,
3134 bool has_mode, enum NewImageMode mode,
3135 bool has_speed, int64_t speed,
3136 bool has_bitmap, const char *bitmap,
3137 bool has_on_source_error, BlockdevOnError on_source_error,
3138 bool has_on_target_error, BlockdevOnError on_target_error,
3139 Error **errp)
3140{
3141 return do_drive_backup(device, target, has_format, format, sync,
3142 has_mode, mode, has_speed, speed,
3143 has_bitmap, bitmap,
3144 has_on_source_error, on_source_error,
3145 has_on_target_error, on_target_error,
3146 NULL, errp);
3147}
3148
Benoît Canetc13163f2014-01-23 21:31:34 +01003149BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3150{
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003151 return bdrv_named_nodes_list(errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003152}
3153
John Snow78f51fd2015-11-05 18:13:17 -05003154void do_blockdev_backup(const char *device, const char *target,
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003155 enum MirrorSyncMode sync,
3156 bool has_speed, int64_t speed,
3157 bool has_on_source_error,
3158 BlockdevOnError on_source_error,
3159 bool has_on_target_error,
3160 BlockdevOnError on_target_error,
John Snow78f51fd2015-11-05 18:13:17 -05003161 BlockJobTxn *txn, Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003162{
Max Reitz5433c242015-10-19 17:53:29 +02003163 BlockBackend *blk, *target_blk;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003164 BlockDriverState *bs;
3165 BlockDriverState *target_bs;
3166 Error *local_err = NULL;
3167 AioContext *aio_context;
3168
3169 if (!has_speed) {
3170 speed = 0;
3171 }
3172 if (!has_on_source_error) {
3173 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3174 }
3175 if (!has_on_target_error) {
3176 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3177 }
3178
Fam Zhenga0e85442015-03-02 19:36:48 +08003179 blk = blk_by_name(device);
3180 if (!blk) {
Markus Armbruster5b347c52015-03-17 08:36:14 +01003181 error_setg(errp, "Device '%s' not found", device);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003182 return;
3183 }
3184
Max Reitz5433c242015-10-19 17:53:29 +02003185 aio_context = blk_get_aio_context(blk);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003186 aio_context_acquire(aio_context);
3187
Max Reitz5433c242015-10-19 17:53:29 +02003188 if (!blk_is_available(blk)) {
3189 error_setg(errp, "Device '%s' has no medium", device);
3190 goto out;
3191 }
3192 bs = blk_bs(blk);
3193
3194 target_blk = blk_by_name(target);
3195 if (!target_blk) {
Markus Armbruster5b347c52015-03-17 08:36:14 +01003196 error_setg(errp, "Device '%s' not found", target);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003197 goto out;
3198 }
Max Reitz5433c242015-10-19 17:53:29 +02003199
3200 if (!blk_is_available(target_blk)) {
3201 error_setg(errp, "Device '%s' has no medium", target);
3202 goto out;
3203 }
3204 target_bs = blk_bs(target_blk);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003205
3206 bdrv_ref(target_bs);
3207 bdrv_set_aio_context(target_bs, aio_context);
John Snowd58d8452015-04-17 19:49:58 -04003208 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
John Snow78f51fd2015-11-05 18:13:17 -05003209 on_target_error, block_job_cb, bs, txn, &local_err);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003210 if (local_err != NULL) {
3211 bdrv_unref(target_bs);
3212 error_propagate(errp, local_err);
3213 }
3214out:
3215 aio_context_release(aio_context);
3216}
3217
John Snow78f51fd2015-11-05 18:13:17 -05003218void qmp_blockdev_backup(const char *device, const char *target,
3219 enum MirrorSyncMode sync,
3220 bool has_speed, int64_t speed,
3221 bool has_on_source_error,
3222 BlockdevOnError on_source_error,
3223 bool has_on_target_error,
3224 BlockdevOnError on_target_error,
3225 Error **errp)
3226{
3227 do_blockdev_backup(device, target, sync, has_speed, speed,
3228 has_on_source_error, on_source_error,
3229 has_on_target_error, on_target_error,
3230 NULL, errp);
3231}
3232
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003233void qmp_drive_mirror(const char *device, const char *target,
3234 bool has_format, const char *format,
Benoît Canet4c828dc2014-06-16 12:00:55 +02003235 bool has_node_name, const char *node_name,
Benoît Canet09158f02014-06-27 18:25:25 +02003236 bool has_replaces, const char *replaces,
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003237 enum MirrorSyncMode sync,
3238 bool has_mode, enum NewImageMode mode,
Paolo Bonzinib952b552012-10-18 16:49:28 +02003239 bool has_speed, int64_t speed,
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003240 bool has_granularity, uint32_t granularity,
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003241 bool has_buf_size, int64_t buf_size,
Paolo Bonzinib952b552012-10-18 16:49:28 +02003242 bool has_on_source_error, BlockdevOnError on_source_error,
3243 bool has_on_target_error, BlockdevOnError on_target_error,
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003244 bool has_unmap, bool unmap,
Paolo Bonzinib952b552012-10-18 16:49:28 +02003245 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003246{
Fam Zhenga0e85442015-03-02 19:36:48 +08003247 BlockBackend *blk;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003248 BlockDriverState *bs;
3249 BlockDriverState *source, *target_bs;
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003250 AioContext *aio_context;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003251 Error *local_err = NULL;
Max Reitze6641712015-08-26 19:47:48 +02003252 QDict *options;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003253 int flags;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003254 int64_t size;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003255 int ret;
3256
3257 if (!has_speed) {
3258 speed = 0;
3259 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003260 if (!has_on_source_error) {
3261 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3262 }
3263 if (!has_on_target_error) {
3264 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3265 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003266 if (!has_mode) {
3267 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3268 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003269 if (!has_granularity) {
3270 granularity = 0;
3271 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003272 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003273 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003274 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003275 if (!has_unmap) {
3276 unmap = true;
3277 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003278
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003279 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003280 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3281 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003282 return;
3283 }
3284 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003285 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3286 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003287 return;
3288 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003289
Fam Zhenga0e85442015-03-02 19:36:48 +08003290 blk = blk_by_name(device);
3291 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01003292 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3293 "Device '%s' not found", device);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003294 return;
3295 }
3296
Max Reitz5433c242015-10-19 17:53:29 +02003297 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003298 aio_context_acquire(aio_context);
3299
Max Reitz5433c242015-10-19 17:53:29 +02003300 if (!blk_is_available(blk)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003301 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003302 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003303 }
Max Reitz5433c242015-10-19 17:53:29 +02003304 bs = blk_bs(blk);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003305
3306 if (!has_format) {
3307 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3308 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003309
Fam Zheng3718d8a2014-05-23 21:29:43 +08003310 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003311 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003312 }
3313
3314 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003315 source = backing_bs(bs);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003316 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3317 sync = MIRROR_SYNC_MODE_FULL;
3318 }
Max Reitz117e0c82013-11-25 20:28:55 +01003319 if (sync == MIRROR_SYNC_MODE_NONE) {
3320 source = bs;
3321 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003322
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003323 size = bdrv_getlength(bs);
3324 if (size < 0) {
3325 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003326 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003327 }
3328
Benoît Canet09158f02014-06-27 18:25:25 +02003329 if (has_replaces) {
3330 BlockDriverState *to_replace_bs;
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003331 AioContext *replace_aio_context;
3332 int64_t replace_size;
Benoît Canet09158f02014-06-27 18:25:25 +02003333
3334 if (!has_node_name) {
3335 error_setg(errp, "a node-name must be provided when replacing a"
3336 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003337 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003338 }
3339
Wen Congyange12f3782015-07-17 10:12:22 +08003340 to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
Benoît Canet09158f02014-06-27 18:25:25 +02003341
3342 if (!to_replace_bs) {
3343 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003344 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003345 }
3346
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003347 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3348 aio_context_acquire(replace_aio_context);
3349 replace_size = bdrv_getlength(to_replace_bs);
3350 aio_context_release(replace_aio_context);
3351
3352 if (size != replace_size) {
Benoît Canet09158f02014-06-27 18:25:25 +02003353 error_setg(errp, "cannot replace image with a mirror image of "
3354 "different size");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003355 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003356 }
3357 }
3358
Max Reitz14526862013-11-06 19:50:44 +01003359 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
3360 && mode != NEW_IMAGE_MODE_EXISTING)
3361 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003362 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003363 assert(format);
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003364 bdrv_img_create(target, format,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003365 NULL, NULL, NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003366 } else {
3367 switch (mode) {
3368 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003369 break;
3370 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3371 /* create new image with backing file */
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003372 bdrv_img_create(target, format,
3373 source->filename,
3374 source->drv->format_name,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003375 NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003376 break;
3377 default:
3378 abort();
3379 }
3380 }
3381
Markus Armbruster84d18f02014-01-30 15:07:28 +01003382 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003383 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003384 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003385 }
3386
Max Reitze6641712015-08-26 19:47:48 +02003387 options = qdict_new();
Benoît Canet4c828dc2014-06-16 12:00:55 +02003388 if (has_node_name) {
Benoît Canet4c828dc2014-06-16 12:00:55 +02003389 qdict_put(options, "node-name", qstring_from_str(node_name));
3390 }
Max Reitze6641712015-08-26 19:47:48 +02003391 if (format) {
3392 qdict_put(options, "driver", qstring_from_str(format));
3393 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003394
Paolo Bonzinib812f672013-01-21 17:09:43 +01003395 /* Mirroring takes care of copy-on-write using the source's backing
3396 * file.
3397 */
Max Reitzf67503e2014-02-18 18:33:05 +01003398 target_bs = NULL;
Benoît Canet4c828dc2014-06-16 12:00:55 +02003399 ret = bdrv_open(&target_bs, target, NULL, options,
Max Reitz6ebf9aa2015-08-26 19:47:49 +02003400 flags | BDRV_O_NO_BACKING, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003401 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02003402 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003403 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003404 }
3405
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003406 bdrv_set_aio_context(target_bs, aio_context);
3407
Benoît Canet09158f02014-06-27 18:25:25 +02003408 /* pass the node name to replace to mirror start since it's loose coupling
3409 * and will allow to check whether the node still exist at mirror completion
3410 */
3411 mirror_start(bs, target_bs,
3412 has_replaces ? replaces : NULL,
3413 speed, granularity, buf_size, sync,
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003414 on_source_error, on_target_error,
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003415 unmap,
Paolo Bonzinib952b552012-10-18 16:49:28 +02003416 block_job_cb, bs, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003417 if (local_err != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08003418 bdrv_unref(target_bs);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003419 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003420 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003421 }
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003422
3423out:
3424 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003425}
3426
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003427/* Get the block job for a given device name and acquire its AioContext */
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003428static BlockJob *find_block_job(const char *device, AioContext **aio_context,
3429 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003430{
Fam Zhenga0e85442015-03-02 19:36:48 +08003431 BlockBackend *blk;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003432 BlockDriverState *bs;
3433
Max Reitz5433c242015-10-19 17:53:29 +02003434 *aio_context = NULL;
3435
Fam Zhenga0e85442015-03-02 19:36:48 +08003436 blk = blk_by_name(device);
3437 if (!blk) {
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003438 goto notfound;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003439 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003440
Max Reitz5433c242015-10-19 17:53:29 +02003441 *aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003442 aio_context_acquire(*aio_context);
3443
Max Reitz5433c242015-10-19 17:53:29 +02003444 if (!blk_is_available(blk)) {
3445 goto notfound;
3446 }
3447 bs = blk_bs(blk);
3448
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003449 if (!bs->job) {
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003450 goto notfound;
3451 }
3452
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003453 return bs->job;
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003454
3455notfound:
Markus Armbruster2e3a0262015-01-29 10:36:59 +01003456 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3457 "No active block job on device '%s'", device);
Max Reitz5433c242015-10-19 17:53:29 +02003458 if (*aio_context) {
3459 aio_context_release(*aio_context);
3460 *aio_context = NULL;
3461 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003462 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003463}
3464
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003465void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003466{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003467 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003468 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003469
3470 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003471 return;
3472 }
3473
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003474 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003475 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003476}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003477
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003478void qmp_block_job_cancel(const char *device,
3479 bool has_force, bool force, Error **errp)
3480{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003481 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003482 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003483
3484 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003485 return;
3486 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003487
3488 if (!has_force) {
3489 force = false;
3490 }
3491
Fam Zheng751ebd72015-04-03 22:05:18 +08003492 if (job->user_paused && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003493 error_setg(errp, "The block job for device '%s' is currently paused",
3494 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003495 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003496 }
3497
3498 trace_qmp_block_job_cancel(job);
3499 block_job_cancel(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003500out:
3501 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003502}
3503
3504void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003505{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003506 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003507 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003508
Fam Zheng751ebd72015-04-03 22:05:18 +08003509 if (!job || job->user_paused) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003510 return;
3511 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003512
Fam Zheng751ebd72015-04-03 22:05:18 +08003513 job->user_paused = true;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003514 trace_qmp_block_job_pause(job);
3515 block_job_pause(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003516 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003517}
3518
3519void qmp_block_job_resume(const char *device, Error **errp)
3520{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003521 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003522 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003523
Fam Zheng751ebd72015-04-03 22:05:18 +08003524 if (!job || !job->user_paused) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02003525 return;
3526 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003527
Fam Zheng751ebd72015-04-03 22:05:18 +08003528 job->user_paused = false;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003529 trace_qmp_block_job_resume(job);
3530 block_job_resume(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003531 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003532}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003533
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003534void qmp_block_job_complete(const char *device, Error **errp)
3535{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003536 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003537 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003538
3539 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003540 return;
3541 }
3542
3543 trace_qmp_block_job_complete(job);
3544 block_job_complete(job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003545 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003546}
3547
Jeff Codyfa40e652014-07-01 09:52:16 +02003548void qmp_change_backing_file(const char *device,
3549 const char *image_node_name,
3550 const char *backing_file,
3551 Error **errp)
3552{
Fam Zhenga0e85442015-03-02 19:36:48 +08003553 BlockBackend *blk;
Jeff Codyfa40e652014-07-01 09:52:16 +02003554 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003555 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02003556 BlockDriverState *image_bs = NULL;
3557 Error *local_err = NULL;
3558 bool ro;
3559 int open_flags;
3560 int ret;
3561
Fam Zhenga0e85442015-03-02 19:36:48 +08003562 blk = blk_by_name(device);
3563 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01003564 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3565 "Device '%s' not found", device);
Jeff Codyfa40e652014-07-01 09:52:16 +02003566 return;
3567 }
3568
Max Reitz5433c242015-10-19 17:53:29 +02003569 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003570 aio_context_acquire(aio_context);
3571
Max Reitz5433c242015-10-19 17:53:29 +02003572 if (!blk_is_available(blk)) {
3573 error_setg(errp, "Device '%s' has no medium", device);
3574 goto out;
3575 }
3576 bs = blk_bs(blk);
3577
Jeff Codyfa40e652014-07-01 09:52:16 +02003578 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3579 if (local_err) {
3580 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003581 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003582 }
3583
3584 if (!image_bs) {
3585 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003586 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003587 }
3588
3589 if (bdrv_find_base(image_bs) == image_bs) {
3590 error_setg(errp, "not allowing backing file change on an image "
3591 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003592 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003593 }
3594
3595 /* even though we are not necessarily operating on bs, we need it to
3596 * determine if block ops are currently prohibited on the chain */
3597 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003598 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003599 }
3600
3601 /* final sanity check */
3602 if (!bdrv_chain_contains(bs, image_bs)) {
3603 error_setg(errp, "'%s' and image file are not in the same chain",
3604 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003605 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003606 }
3607
3608 /* if not r/w, reopen to make r/w */
3609 open_flags = image_bs->open_flags;
3610 ro = bdrv_is_read_only(image_bs);
3611
3612 if (ro) {
3613 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3614 if (local_err) {
3615 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003616 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003617 }
3618 }
3619
3620 ret = bdrv_change_backing_file(image_bs, backing_file,
3621 image_bs->drv ? image_bs->drv->format_name : "");
3622
3623 if (ret < 0) {
3624 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3625 backing_file);
3626 /* don't exit here, so we can try to restore open flags if
3627 * appropriate */
3628 }
3629
3630 if (ro) {
3631 bdrv_reopen(image_bs, open_flags, &local_err);
3632 if (local_err) {
3633 error_propagate(errp, local_err); /* will preserve prior errp */
3634 }
3635 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003636
3637out:
3638 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02003639}
3640
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003641void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3642{
3643 QmpOutputVisitor *ov = qmp_output_visitor_new();
Max Reitzbe4b67b2015-10-19 17:53:09 +02003644 BlockDriverState *bs;
3645 BlockBackend *blk = NULL;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003646 QObject *obj;
3647 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003648 Error *local_err = NULL;
3649
Markus Armbruster60e19e02014-06-06 14:50:58 +02003650 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003651 * cache.direct=false instead of silently switching to aio=threads, except
Markus Armbruster60e19e02014-06-06 14:50:58 +02003652 * when called from drive_new().
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003653 *
3654 * For now, simply forbidding the combination for all drivers will do. */
3655 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
Kevin Wolfc6e0bd92014-03-06 15:47:32 +01003656 bool direct = options->has_cache &&
3657 options->cache->has_direct &&
3658 options->cache->direct;
3659 if (!direct) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003660 error_setg(errp, "aio=native requires cache.direct=true");
3661 goto fail;
3662 }
3663 }
3664
3665 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
3666 &options, NULL, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003667 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003668 error_propagate(errp, local_err);
3669 goto fail;
3670 }
3671
3672 obj = qmp_output_get_qobject(ov);
3673 qdict = qobject_to_qdict(obj);
3674
3675 qdict_flatten(qdict);
3676
Max Reitzbe4b67b2015-10-19 17:53:09 +02003677 if (options->has_id) {
3678 blk = blockdev_init(NULL, qdict, &local_err);
3679 if (local_err) {
3680 error_propagate(errp, local_err);
3681 goto fail;
3682 }
3683
3684 bs = blk_bs(blk);
3685 } else {
Max Reitzbe4b67b2015-10-19 17:53:09 +02003686 if (!qdict_get_try_str(qdict, "node-name")) {
3687 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3688 "the root node");
3689 goto fail;
3690 }
3691
Max Reitzbd745e22015-10-19 17:53:32 +02003692 bs = bds_tree_init(qdict, errp);
3693 if (!bs) {
Max Reitzbe4b67b2015-10-19 17:53:09 +02003694 goto fail;
3695 }
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003696 }
3697
Max Reitzbe4b67b2015-10-19 17:53:09 +02003698 if (bs && bdrv_key_required(bs)) {
3699 if (blk) {
3700 blk_unref(blk);
3701 } else {
3702 bdrv_unref(bs);
3703 }
Kevin Wolf8ae8e902014-03-06 15:43:42 +01003704 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3705 goto fail;
3706 }
3707
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003708fail:
3709 qmp_output_visitor_cleanup(ov);
3710}
3711
Alberto Garcia81b936a2015-11-02 16:51:55 +02003712void qmp_x_blockdev_del(bool has_id, const char *id,
3713 bool has_node_name, const char *node_name, Error **errp)
3714{
3715 AioContext *aio_context;
3716 BlockBackend *blk;
3717 BlockDriverState *bs;
3718
3719 if (has_id && has_node_name) {
3720 error_setg(errp, "Only one of id and node-name must be specified");
3721 return;
3722 } else if (!has_id && !has_node_name) {
3723 error_setg(errp, "No block device specified");
3724 return;
3725 }
3726
3727 if (has_id) {
3728 blk = blk_by_name(id);
3729 if (!blk) {
3730 error_setg(errp, "Cannot find block backend %s", id);
3731 return;
3732 }
3733 if (blk_get_refcnt(blk) > 1) {
3734 error_setg(errp, "Block backend %s is in use", id);
3735 return;
3736 }
3737 bs = blk_bs(blk);
3738 aio_context = blk_get_aio_context(blk);
3739 } else {
3740 bs = bdrv_find_node(node_name);
3741 if (!bs) {
3742 error_setg(errp, "Cannot find node %s", node_name);
3743 return;
3744 }
3745 blk = bs->blk;
3746 if (blk) {
3747 error_setg(errp, "Node %s is in use by %s",
3748 node_name, blk_name(blk));
3749 return;
3750 }
3751 aio_context = bdrv_get_aio_context(bs);
3752 }
3753
3754 aio_context_acquire(aio_context);
3755
3756 if (bs) {
3757 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3758 goto out;
3759 }
3760
3761 if (bs->refcnt > 1 || !QLIST_EMPTY(&bs->parents)) {
3762 error_setg(errp, "Block device %s is in use",
3763 bdrv_get_device_or_node_name(bs));
3764 goto out;
3765 }
3766 }
3767
3768 if (blk) {
3769 blk_unref(blk);
3770 } else {
3771 bdrv_unref(bs);
3772 }
3773
3774out:
3775 aio_context_release(aio_context);
3776}
3777
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003778BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3779{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003780 BlockJobInfoList *head = NULL, **p_next = &head;
3781 BlockDriverState *bs;
3782
3783 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003784 AioContext *aio_context = bdrv_get_aio_context(bs);
3785
3786 aio_context_acquire(aio_context);
3787
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003788 if (bs->job) {
3789 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3790 elem->value = block_job_query(bs->job);
3791 *p_next = elem;
3792 p_next = &elem->next;
3793 }
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003794
3795 aio_context_release(aio_context);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003796 }
3797
3798 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003799}
Paolo Bonzini4d454572012-11-26 16:03:42 +01003800
Kevin Wolf00063832013-03-15 10:35:07 +01003801QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01003802 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01003803 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01003804 .desc = {
3805 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01003806 .name = "snapshot",
3807 .type = QEMU_OPT_BOOL,
3808 .help = "enable/disable snapshot mode",
3809 },{
Paolo Bonzinia9384af2013-02-08 14:06:12 +01003810 .name = "discard",
3811 .type = QEMU_OPT_STRING,
3812 .help = "discard operation (ignore/off, unmap/on)",
3813 },{
Kevin Wolf54861b92015-04-07 16:55:00 +02003814 .name = BDRV_OPT_CACHE_WB,
Kevin Wolf29c4e2b2013-07-18 16:31:25 +02003815 .type = QEMU_OPT_BOOL,
3816 .help = "enables writeback mode for any caches",
3817 },{
Kevin Wolf54861b92015-04-07 16:55:00 +02003818 .name = BDRV_OPT_CACHE_DIRECT,
Kevin Wolf29c4e2b2013-07-18 16:31:25 +02003819 .type = QEMU_OPT_BOOL,
3820 .help = "enables use of O_DIRECT (bypass the host page cache)",
3821 },{
Kevin Wolf54861b92015-04-07 16:55:00 +02003822 .name = BDRV_OPT_CACHE_NO_FLUSH,
Kevin Wolf29c4e2b2013-07-18 16:31:25 +02003823 .type = QEMU_OPT_BOOL,
3824 .help = "ignore any flush requests for the device",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003825 },{
3826 .name = "aio",
3827 .type = QEMU_OPT_STRING,
3828 .help = "host AIO implementation (threads, native)",
3829 },{
3830 .name = "format",
3831 .type = QEMU_OPT_STRING,
3832 .help = "disk format (raw, qcow2, ...)",
3833 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003834 .name = "rerror",
3835 .type = QEMU_OPT_STRING,
3836 .help = "read error action",
3837 },{
3838 .name = "werror",
3839 .type = QEMU_OPT_STRING,
3840 .help = "write error action",
3841 },{
Kevin Wolf0f227a92013-07-19 20:07:29 +02003842 .name = "read-only",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003843 .type = QEMU_OPT_BOOL,
3844 .help = "open drive file as read-only",
3845 },{
Kevin Wolf57975222013-07-17 14:41:54 +02003846 .name = "throttling.iops-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003847 .type = QEMU_OPT_NUMBER,
3848 .help = "limit total I/O operations per second",
3849 },{
Kevin Wolf57975222013-07-17 14:41:54 +02003850 .name = "throttling.iops-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003851 .type = QEMU_OPT_NUMBER,
3852 .help = "limit read operations per second",
3853 },{
Kevin Wolf57975222013-07-17 14:41:54 +02003854 .name = "throttling.iops-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003855 .type = QEMU_OPT_NUMBER,
3856 .help = "limit write operations per second",
3857 },{
Kevin Wolf57975222013-07-17 14:41:54 +02003858 .name = "throttling.bps-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003859 .type = QEMU_OPT_NUMBER,
3860 .help = "limit total bytes per second",
3861 },{
Kevin Wolf57975222013-07-17 14:41:54 +02003862 .name = "throttling.bps-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003863 .type = QEMU_OPT_NUMBER,
3864 .help = "limit read bytes per second",
3865 },{
Kevin Wolf57975222013-07-17 14:41:54 +02003866 .name = "throttling.bps-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003867 .type = QEMU_OPT_NUMBER,
3868 .help = "limit write bytes per second",
3869 },{
Benoît Canet3e9fab62013-09-02 14:14:40 +02003870 .name = "throttling.iops-total-max",
3871 .type = QEMU_OPT_NUMBER,
3872 .help = "I/O operations burst",
3873 },{
3874 .name = "throttling.iops-read-max",
3875 .type = QEMU_OPT_NUMBER,
3876 .help = "I/O operations read burst",
3877 },{
3878 .name = "throttling.iops-write-max",
3879 .type = QEMU_OPT_NUMBER,
3880 .help = "I/O operations write burst",
3881 },{
3882 .name = "throttling.bps-total-max",
3883 .type = QEMU_OPT_NUMBER,
3884 .help = "total bytes burst",
3885 },{
3886 .name = "throttling.bps-read-max",
3887 .type = QEMU_OPT_NUMBER,
3888 .help = "total bytes read burst",
3889 },{
3890 .name = "throttling.bps-write-max",
3891 .type = QEMU_OPT_NUMBER,
3892 .help = "total bytes write burst",
3893 },{
Benoît Canet2024c1d2013-09-02 14:14:41 +02003894 .name = "throttling.iops-size",
3895 .type = QEMU_OPT_NUMBER,
3896 .help = "when limiting by iops max size of an I/O in bytes",
3897 },{
Alberto Garcia76f4afb2015-06-08 18:17:44 +02003898 .name = "throttling.group",
3899 .type = QEMU_OPT_STRING,
3900 .help = "name of the block throttling group",
3901 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003902 .name = "copy-on-read",
3903 .type = QEMU_OPT_BOOL,
3904 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02003905 },{
3906 .name = "detect-zeroes",
3907 .type = QEMU_OPT_STRING,
3908 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02003909 },{
3910 .name = "stats-account-invalid",
3911 .type = QEMU_OPT_BOOL,
3912 .help = "whether to account for invalid I/O operations "
3913 "in the statistics",
3914 },{
3915 .name = "stats-account-failed",
3916 .type = QEMU_OPT_BOOL,
3917 .help = "whether to account for failed I/O operations "
3918 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003919 },
3920 { /* end of list */ }
3921 },
3922};
Kevin Wolf00063832013-03-15 10:35:07 +01003923
Max Reitzbd745e22015-10-19 17:53:32 +02003924static QemuOptsList qemu_root_bds_opts = {
3925 .name = "root-bds",
3926 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3927 .desc = {
3928 {
3929 .name = "discard",
3930 .type = QEMU_OPT_STRING,
3931 .help = "discard operation (ignore/off, unmap/on)",
3932 },{
3933 .name = "cache.writeback",
3934 .type = QEMU_OPT_BOOL,
3935 .help = "enables writeback mode for any caches",
3936 },{
3937 .name = "cache.direct",
3938 .type = QEMU_OPT_BOOL,
3939 .help = "enables use of O_DIRECT (bypass the host page cache)",
3940 },{
3941 .name = "cache.no-flush",
3942 .type = QEMU_OPT_BOOL,
3943 .help = "ignore any flush requests for the device",
3944 },{
3945 .name = "aio",
3946 .type = QEMU_OPT_STRING,
3947 .help = "host AIO implementation (threads, native)",
3948 },{
3949 .name = "read-only",
3950 .type = QEMU_OPT_BOOL,
3951 .help = "open drive file as read-only",
3952 },{
3953 .name = "copy-on-read",
3954 .type = QEMU_OPT_BOOL,
3955 .help = "copy read data from backing file into image file",
3956 },{
3957 .name = "detect-zeroes",
3958 .type = QEMU_OPT_STRING,
3959 .help = "try to optimize zero writes (off, on, unmap)",
3960 },
3961 { /* end of list */ }
3962 },
3963};
3964
Kevin Wolf00063832013-03-15 10:35:07 +01003965QemuOptsList qemu_drive_opts = {
3966 .name = "drive",
3967 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3968 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02003969 /*
3970 * no elements => accept any params
3971 * validation will happen later
3972 */
Kevin Wolf00063832013-03-15 10:35:07 +01003973 { /* end of list */ }
3974 },
3975};