blob: 539aa7d1e79c67d2ed7ef212c83d80066eb8ee67 [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
Peter Maydelld38ea872016-01-29 17:50:05 +000033#include "qemu/osdep.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020034#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/blockdev.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010036#include "hw/block/block.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010037#include "block/blockjob.h"
Alberto Garcia76f4afb2015-06-08 18:17:44 +020038#include "block/throttle-groups.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010040#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/option.h"
42#include "qemu/config-file.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010043#include "qapi/qmp/types.h"
Kevin Wolfd26c9a12013-09-23 15:26:03 +020044#include "qapi-visit.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010045#include "qapi/qmp/qerror.h"
Kevin Wolfd26c9a12013-09-23 15:26:03 +020046#include "qapi/qmp-output-visitor.h"
Peter Lieven9e7dac72014-08-13 19:20:17 +020047#include "qapi/util.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010048#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010049#include "block/block_int.h"
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -020050#include "qmp-commands.h"
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +000051#include "trace.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010052#include "sysemu/arch_init.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020053#include "qemu/cutils.h"
54#include "qemu/help_option.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020055
Max Reitz9c4218e2016-01-29 16:36:12 +010056static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
57 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
58
Colin Lordbf18bee2016-06-06 14:15:22 -040059static int do_open_tray(const char *device, bool force, Error **errp);
60
Markus Armbruster19609662011-01-28 11:21:39 +010061static const char *const if_name[IF_COUNT] = {
62 [IF_NONE] = "none",
63 [IF_IDE] = "ide",
64 [IF_SCSI] = "scsi",
65 [IF_FLOPPY] = "floppy",
66 [IF_PFLASH] = "pflash",
67 [IF_MTD] = "mtd",
68 [IF_SD] = "sd",
69 [IF_VIRTIO] = "virtio",
70 [IF_XEN] = "xen",
71};
72
John Snow21dff8c2014-10-01 14:19:25 -040073static int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010074 /*
75 * Do not change these numbers! They govern how drive option
76 * index maps to unit and bus. That mapping is ABI.
77 *
Wei Jiangang547cb152016-04-26 18:12:43 +080078 * All controllers used to implement if=T drives need to support
Markus Armbruster27d6bf42011-01-28 11:21:40 +010079 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
80 * Otherwise, some index values map to "impossible" bus, unit
81 * values.
82 *
83 * For instance, if you change [IF_SCSI] to 255, -drive
84 * if=scsi,index=12 no longer means bus=1,unit=5, but
85 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
86 * the drive can't be set up. Regression.
87 */
88 [IF_IDE] = 2,
89 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +010090};
91
John Snow21dff8c2014-10-01 14:19:25 -040092/**
93 * Boards may call this to offer board-by-board overrides
94 * of the default, global values.
95 */
96void override_max_devs(BlockInterfaceType type, int max_devs)
97{
Markus Armbruster18e46a02014-10-07 13:59:06 +020098 BlockBackend *blk;
John Snow21dff8c2014-10-01 14:19:25 -040099 DriveInfo *dinfo;
100
101 if (max_devs <= 0) {
102 return;
103 }
104
Markus Armbruster18e46a02014-10-07 13:59:06 +0200105 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
106 dinfo = blk_legacy_dinfo(blk);
John Snow21dff8c2014-10-01 14:19:25 -0400107 if (dinfo->type == type) {
108 fprintf(stderr, "Cannot override units-per-bus property of"
109 " the %s interface, because a drive of that type has"
110 " already been added.\n", if_name[type]);
111 g_assert_not_reached();
112 }
113 }
114
115 if_max_devs[type] = max_devs;
116}
117
Markus Armbruster14bafc52010-06-25 08:09:10 +0200118/*
119 * We automatically delete the drive when a device using it gets
120 * unplugged. Questionable feature, but we can't just drop it.
121 * Device models call blockdev_mark_auto_del() to schedule the
122 * automatic deletion, and generic qdev code calls blockdev_auto_del()
123 * when deletion is actually safe.
124 */
Markus Armbruster4be74632014-10-07 13:59:18 +0200125void blockdev_mark_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200126{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200127 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster4be74632014-10-07 13:59:18 +0200128 BlockDriverState *bs = blk_bs(blk);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100129 AioContext *aio_context;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200130
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200131 if (!dinfo) {
Kevin Wolf2d246f02013-09-18 15:14:47 +0200132 return;
133 }
134
Max Reitz5433c242015-10-19 17:53:29 +0200135 if (bs) {
136 aio_context = bdrv_get_aio_context(bs);
137 aio_context_acquire(aio_context);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100138
Max Reitz5433c242015-10-19 17:53:29 +0200139 if (bs->job) {
140 block_job_cancel(bs->job);
141 }
142
143 aio_context_release(aio_context);
Paolo Bonzini12bde0e2012-03-30 13:17:10 +0200144 }
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100145
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200146 dinfo->auto_del = 1;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200147}
148
Markus Armbruster4be74632014-10-07 13:59:18 +0200149void blockdev_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200150{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200151 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200152
Ryan Harper0fc0f1f2010-12-08 10:05:00 -0600153 if (dinfo && dinfo->auto_del) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100154 monitor_remove_blk(blk);
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +0200155 blk_unref(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200156 }
157}
158
John Snowd8f94e12014-10-01 14:19:27 -0400159/**
160 * Returns the current mapping of how many units per bus
161 * a particular interface can support.
162 *
163 * A positive integer indicates n units per bus.
164 * 0 implies the mapping has not been established.
165 * -1 indicates an invalid BlockInterfaceType was given.
166 */
167int drive_get_max_devs(BlockInterfaceType type)
168{
169 if (type >= IF_IDE && type < IF_COUNT) {
170 return if_max_devs[type];
171 }
172
173 return -1;
174}
175
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100176static int drive_index_to_bus_id(BlockInterfaceType type, int index)
177{
178 int max_devs = if_max_devs[type];
179 return max_devs ? index / max_devs : 0;
180}
181
182static int drive_index_to_unit_id(BlockInterfaceType type, int index)
183{
184 int max_devs = if_max_devs[type];
185 return max_devs ? index % max_devs : index;
186}
187
Markus Armbruster2292dda2011-01-28 11:21:41 +0100188QemuOpts *drive_def(const char *optstr)
189{
Markus Armbruster70b94332015-02-13 12:50:26 +0100190 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100191}
192
193QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +0100194 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +0200195{
Markus Armbruster666daa62010-06-02 18:48:27 +0200196 QemuOpts *opts;
197
Markus Armbruster2292dda2011-01-28 11:21:41 +0100198 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200199 if (!opts) {
200 return NULL;
201 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100202 if (type != IF_DEFAULT) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100203 qemu_opt_set(opts, "if", if_name[type], &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100204 }
205 if (index >= 0) {
Markus Armbrustera8b18f82015-02-13 15:50:43 +0100206 qemu_opt_set_number(opts, "index", index, &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100207 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200208 if (file)
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100209 qemu_opt_set(opts, "file", file, &error_abort);
Markus Armbruster666daa62010-06-02 18:48:27 +0200210 return opts;
211}
212
213DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
214{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200215 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200216 DriveInfo *dinfo;
217
Markus Armbruster18e46a02014-10-07 13:59:06 +0200218 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
219 dinfo = blk_legacy_dinfo(blk);
220 if (dinfo && dinfo->type == type
221 && dinfo->bus == bus && dinfo->unit == unit) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200222 return dinfo;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200223 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200224 }
225
226 return NULL;
227}
228
John Snowa66c9dc2014-10-01 14:19:24 -0400229bool drive_check_orphaned(void)
230{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200231 BlockBackend *blk;
John Snowa66c9dc2014-10-01 14:19:24 -0400232 DriveInfo *dinfo;
233 bool rs = false;
234
Markus Armbruster18e46a02014-10-07 13:59:06 +0200235 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
236 dinfo = blk_legacy_dinfo(blk);
John Snowa66c9dc2014-10-01 14:19:24 -0400237 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
238 /* Unless this is a default drive, this may be an oversight. */
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200239 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
John Snowa66c9dc2014-10-01 14:19:24 -0400240 dinfo->type != IF_NONE) {
241 fprintf(stderr, "Warning: Orphaned drive without device: "
242 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
Max Reitz5433c242015-10-19 17:53:29 +0200243 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
244 if_name[dinfo->type], dinfo->bus, dinfo->unit);
John Snowa66c9dc2014-10-01 14:19:24 -0400245 rs = true;
246 }
247 }
248
249 return rs;
250}
251
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100252DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
253{
254 return drive_get(type,
255 drive_index_to_bus_id(type, index),
256 drive_index_to_unit_id(type, index));
257}
258
Markus Armbruster666daa62010-06-02 18:48:27 +0200259int drive_get_max_bus(BlockInterfaceType type)
260{
261 int max_bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200262 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200263 DriveInfo *dinfo;
264
265 max_bus = -1;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200266 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
267 dinfo = blk_legacy_dinfo(blk);
268 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200269 max_bus = dinfo->bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200270 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200271 }
272 return max_bus;
273}
274
Markus Armbruster13839972011-01-28 11:21:37 +0100275/* Get a block device. This should only be used for single-drive devices
276 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
277 appropriate bus. */
278DriveInfo *drive_get_next(BlockInterfaceType type)
279{
280 static int next_block_unit[IF_COUNT];
281
282 return drive_get(type, 0, next_block_unit[type]++);
283}
284
Markus Armbruster666daa62010-06-02 18:48:27 +0200285static void bdrv_format_print(void *opaque, const char *name)
286{
Markus Armbruster807105a2011-01-17 19:31:27 +0100287 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200288}
289
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000290typedef struct {
291 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800292 BlockDriverState *bs;
293} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000294
Kevin Wolfb6810722013-09-20 11:33:11 +0200295static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200296{
297 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200298 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200299 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200300 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200301 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200302 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200303 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200304 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200305 } else {
Kevin Wolfb6810722013-09-20 11:33:11 +0200306 error_setg(errp, "'%s' invalid %s error action",
307 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200308 return -1;
309 }
310}
311
Alberto Garcia40119ef2015-11-16 11:28:38 +0200312static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
313 Error **errp)
314{
315 const QListEntry *entry;
316 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
317 switch (qobject_type(entry->value)) {
318
319 case QTYPE_QSTRING: {
320 unsigned long long length;
321 const char *str = qstring_get_str(qobject_to_qstring(entry->value));
322 if (parse_uint_full(str, &length, 10) == 0 &&
323 length > 0 && length <= UINT_MAX) {
324 block_acct_add_interval(stats, (unsigned) length);
325 } else {
326 error_setg(errp, "Invalid interval length: %s", str);
327 return false;
328 }
329 break;
330 }
331
332 case QTYPE_QINT: {
333 int64_t length = qint_get_int(qobject_to_qint(entry->value));
334 if (length > 0 && length <= UINT_MAX) {
335 block_acct_add_interval(stats, (unsigned) length);
336 } else {
337 error_setg(errp, "Invalid interval length: %" PRId64, length);
338 return false;
339 }
340 break;
341 }
342
343 default:
344 error_setg(errp, "The specification of stats-intervals is invalid");
345 return false;
346 }
347 }
348 return true;
349}
350
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200351typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
352
Max Reitzfbf81752015-10-19 17:53:31 +0200353/* All parameters but @opts are optional and may be set to NULL. */
354static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
355 const char **throttling_group, ThrottleConfig *throttle_cfg,
356 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
357{
358 const char *discard;
359 Error *local_error = NULL;
360 const char *aio;
361
362 if (bdrv_flags) {
363 if (!qemu_opt_get_bool(opts, "read-only", false)) {
364 *bdrv_flags |= BDRV_O_RDWR;
365 }
366 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
367 *bdrv_flags |= BDRV_O_COPY_ON_READ;
368 }
369
370 if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
371 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
372 error_setg(errp, "Invalid discard option");
373 return;
374 }
375 }
376
Max Reitzfbf81752015-10-19 17:53:31 +0200377 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
378 if (!strcmp(aio, "native")) {
379 *bdrv_flags |= BDRV_O_NATIVE_AIO;
380 } else if (!strcmp(aio, "threads")) {
381 /* this is the default */
382 } else {
383 error_setg(errp, "invalid aio option");
384 return;
385 }
386 }
387 }
388
389 /* disk I/O throttling */
390 if (throttling_group) {
391 *throttling_group = qemu_opt_get(opts, "throttling.group");
392 }
393
394 if (throttle_cfg) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200395 throttle_config_init(throttle_cfg);
Max Reitzfbf81752015-10-19 17:53:31 +0200396 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
397 qemu_opt_get_number(opts, "throttling.bps-total", 0);
398 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
399 qemu_opt_get_number(opts, "throttling.bps-read", 0);
400 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
401 qemu_opt_get_number(opts, "throttling.bps-write", 0);
402 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
403 qemu_opt_get_number(opts, "throttling.iops-total", 0);
404 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
405 qemu_opt_get_number(opts, "throttling.iops-read", 0);
406 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
407 qemu_opt_get_number(opts, "throttling.iops-write", 0);
408
409 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
410 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
411 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
412 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
413 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
414 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
415 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
416 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
417 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
418 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
419 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
420 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
421
Alberto Garcia8a0fc182016-02-18 12:27:02 +0200422 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
423 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
424 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
425 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
426 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
427 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
428 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
429 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
430 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
431 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
432 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
433 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
434
Max Reitzfbf81752015-10-19 17:53:31 +0200435 throttle_cfg->op_size =
436 qemu_opt_get_number(opts, "throttling.iops-size", 0);
437
Alberto Garciad5851082016-02-18 12:26:59 +0200438 if (!throttle_is_valid(throttle_cfg, errp)) {
Max Reitzfbf81752015-10-19 17:53:31 +0200439 return;
440 }
441 }
442
443 if (detect_zeroes) {
444 *detect_zeroes =
445 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
446 qemu_opt_get(opts, "detect-zeroes"),
Eric Blake7fb1cf12015-11-18 01:52:57 -0700447 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Max Reitzfbf81752015-10-19 17:53:31 +0200448 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
449 &local_error);
450 if (local_error) {
451 error_propagate(errp, local_error);
452 return;
453 }
454
455 if (bdrv_flags &&
456 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
457 !(*bdrv_flags & BDRV_O_UNMAP))
458 {
459 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
460 "without setting discard operation to unmap");
461 return;
462 }
463 }
464}
465
Kevin Wolff298d072013-09-10 12:01:20 +0200466/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200467static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
468 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200469{
470 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200471 int bdrv_flags = 0;
472 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200473 bool account_invalid, account_failed;
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100474 bool writethrough;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200475 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200476 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200477 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200478 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100479 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100480 QemuOpts *opts;
Alberto Garcia40119ef2015-11-16 11:28:38 +0200481 QDict *interval_dict = NULL;
482 QList *interval_list = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100483 const char *id;
Max Reitzfbf81752015-10-19 17:53:31 +0200484 BlockdevDetectZeroesOptions detect_zeroes =
485 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
486 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200487
Kevin Wolff298d072013-09-10 12:01:20 +0200488 /* Check common options by copying from bs_opts to opts, all other options
489 * stay in bs_opts for processing by bdrv_open(). */
490 id = qdict_get_try_str(bs_opts, "id");
Kevin Wolf00063832013-03-15 10:35:07 +0100491 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100492 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200493 error_propagate(errp, error);
Markus Armbruster6376f952014-05-28 11:17:01 +0200494 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100495 }
496
Kevin Wolf00063832013-03-15 10:35:07 +0100497 qemu_opts_absorb_qdict(opts, bs_opts, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100498 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200499 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100500 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100501 }
502
503 if (id) {
504 qdict_del(bs_opts, "id");
505 }
506
Markus Armbruster666daa62010-06-02 18:48:27 +0200507 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200508 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200509
Alberto Garcia362e9292015-10-28 17:33:04 +0200510 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
511 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
512
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100513 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
514
Alberto Garciaff356ee2016-07-08 17:03:00 +0300515 id = qemu_opts_id(opts);
516
Alberto Garcia40119ef2015-11-16 11:28:38 +0200517 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
518 qdict_array_split(interval_dict, &interval_list);
519
520 if (qdict_size(interval_dict) != 0) {
521 error_setg(errp, "Invalid option stats-intervals.%s",
522 qdict_first(interval_dict)->key);
523 goto early_err;
524 }
Alberto Garcia2be55062015-10-28 17:33:07 +0200525
Max Reitzfbf81752015-10-19 17:53:31 +0200526 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
527 &detect_zeroes, &error);
528 if (error) {
529 error_propagate(errp, error);
530 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200531 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200532
533 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100534 if (is_help_option(buf)) {
535 error_printf("Supported formats:");
536 bdrv_iterate_format(bdrv_format_print, NULL);
537 error_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100538 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200539 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200540
Max Reitze4342ce2015-02-05 13:58:14 -0500541 if (qdict_haskey(bs_opts, "driver")) {
542 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100543 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400544 }
Max Reitze4342ce2015-02-05 13:58:14 -0500545 qdict_put(bs_opts, "driver", qstring_from_str(buf));
Markus Armbruster666daa62010-06-02 18:48:27 +0200546 }
547
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200548 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200549 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200550 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100551 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200552 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100553 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200554 }
555 }
556
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200557 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200558 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200559 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100560 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200561 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100562 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200563 }
564 }
565
Max Reitz5ec18f82015-10-19 17:53:30 +0200566 if (snapshot) {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200567 bdrv_flags |= BDRV_O_SNAPSHOT;
Max Reitz5ec18f82015-10-19 17:53:30 +0200568 }
569
Kevin Wolf326642b2013-07-11 12:52:34 +0200570 /* init */
Kevin Wolf39c4ae92015-11-13 14:45:42 +0100571 if ((!file || !*file) && !qdict_size(bs_opts)) {
Max Reitz5ec18f82015-10-19 17:53:30 +0200572 BlockBackendRootState *blk_rs;
573
Max Reitz109525a2016-05-17 16:41:34 +0200574 blk = blk_new();
Max Reitz5ec18f82015-10-19 17:53:30 +0200575 blk_rs = blk_get_root_state(blk);
576 blk_rs->open_flags = bdrv_flags;
Max Reitzfbf81752015-10-19 17:53:31 +0200577 blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR);
Max Reitz5ec18f82015-10-19 17:53:30 +0200578 blk_rs->detect_zeroes = detect_zeroes;
579
Max Reitze4342ce2015-02-05 13:58:14 -0500580 QDECREF(bs_opts);
581 } else {
582 if (file && !*file) {
583 file = NULL;
584 }
585
Kevin Wolf91a097e2015-05-08 17:49:53 +0200586 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
587 * with other callers) rather than what we want as the real defaults.
588 * Apply the defaults here instead. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200589 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
590 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Kevin Wolf72e775c2016-03-15 14:34:37 +0100591 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200592
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100593 if (runstate_check(RUN_STATE_INMIGRATE)) {
594 bdrv_flags |= BDRV_O_INACTIVE;
595 }
596
Max Reitzefaa7c42016-03-16 19:54:38 +0100597 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500598 if (!blk) {
599 goto err_no_bs_opts;
600 }
601 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500602
Max Reitz5ec18f82015-10-19 17:53:30 +0200603 bs->detect_zeroes = detect_zeroes;
604
Max Reitz5ec18f82015-10-19 17:53:30 +0200605 if (bdrv_key_required(bs)) {
606 autostart = 0;
607 }
Alberto Garcia362e9292015-10-28 17:33:04 +0200608
609 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
Alberto Garcia2be55062015-10-28 17:33:07 +0200610
Alberto Garcia40119ef2015-11-16 11:28:38 +0200611 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
612 blk_unref(blk);
613 blk = NULL;
614 goto err_no_bs_opts;
Alberto Garcia2be55062015-10-28 17:33:07 +0200615 }
Max Reitz5ec18f82015-10-19 17:53:30 +0200616 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200617
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100618 /* disk I/O throttling */
619 if (throttle_enabled(&cfg)) {
620 if (!throttling_group) {
Alberto Garciaff356ee2016-07-08 17:03:00 +0300621 throttling_group = id;
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100622 }
623 blk_io_limits_enable(blk, throttling_group);
624 blk_set_io_limits(blk, &cfg);
625 }
626
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100627 blk_set_enable_write_cache(blk, !writethrough);
Max Reitz373340b2015-10-19 17:53:22 +0200628 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200629
Alberto Garciaff356ee2016-07-08 17:03:00 +0300630 if (!monitor_add_blk(blk, id, errp)) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100631 blk_unref(blk);
632 blk = NULL;
633 goto err_no_bs_opts;
634 }
635
Max Reitze4342ce2015-02-05 13:58:14 -0500636err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100637 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200638 QDECREF(interval_dict);
639 QDECREF(interval_list);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200640 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100641
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100642early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100643 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200644 QDECREF(interval_dict);
645 QDECREF(interval_list);
Markus Armbruster6376f952014-05-28 11:17:01 +0200646err_no_opts:
647 QDECREF(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100648 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200649}
650
Max Reitzbd745e22015-10-19 17:53:32 +0200651static QemuOptsList qemu_root_bds_opts;
652
653/* Takes the ownership of bs_opts */
654static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
655{
656 BlockDriverState *bs;
657 QemuOpts *opts;
658 Error *local_error = NULL;
659 BlockdevDetectZeroesOptions detect_zeroes;
Max Reitzbd745e22015-10-19 17:53:32 +0200660 int bdrv_flags = 0;
661
662 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
663 if (!opts) {
664 goto fail;
665 }
666
667 qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
668 if (local_error) {
669 error_propagate(errp, local_error);
670 goto fail;
671 }
672
673 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
674 &detect_zeroes, &local_error);
675 if (local_error) {
676 error_propagate(errp, local_error);
677 goto fail;
678 }
679
Kevin Wolfa81d6162016-03-07 14:23:04 +0100680 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
681 * with other callers) rather than what we want as the real defaults.
682 * Apply the defaults here instead. */
Kevin Wolfa81d6162016-03-07 14:23:04 +0100683 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
684 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
685
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100686 if (runstate_check(RUN_STATE_INMIGRATE)) {
687 bdrv_flags |= BDRV_O_INACTIVE;
688 }
689
Max Reitz5b363932016-05-17 16:41:31 +0200690 bs = bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
691 if (!bs) {
Max Reitzbd745e22015-10-19 17:53:32 +0200692 goto fail_no_bs_opts;
693 }
694
695 bs->detect_zeroes = detect_zeroes;
696
697fail_no_bs_opts:
698 qemu_opts_del(opts);
699 return bs;
700
701fail:
702 qemu_opts_del(opts);
703 QDECREF(bs_opts);
704 return NULL;
705}
706
Max Reitz9c4218e2016-01-29 16:36:12 +0100707void blockdev_close_all_bdrv_states(void)
708{
709 BlockDriverState *bs, *next_bs;
710
711 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
712 AioContext *ctx = bdrv_get_aio_context(bs);
713
714 aio_context_acquire(ctx);
715 bdrv_unref(bs);
716 aio_context_release(ctx);
717 }
718}
719
Max Reitz262b4e82016-03-16 19:54:41 +0100720/* Iterates over the list of monitor-owned BlockDriverStates */
721BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
722{
723 return bs ? QTAILQ_NEXT(bs, monitor_list)
724 : QTAILQ_FIRST(&monitor_bdrv_states);
725}
726
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200727static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
728 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200729{
730 const char *value;
731
732 value = qemu_opt_get(opts, from);
733 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200734 if (qemu_opt_find(opts, to)) {
735 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
736 "same time", to, from);
737 return;
738 }
Jun Li20d6cd42014-09-24 13:45:27 +0800739 }
740
741 /* rename all items in opts */
742 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100743 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200744 qemu_opt_unset(opts, from);
745 }
746}
747
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200748QemuOptsList qemu_legacy_drive_opts = {
749 .name = "drive",
750 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
751 .desc = {
752 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200753 .name = "bus",
754 .type = QEMU_OPT_NUMBER,
755 .help = "bus number",
756 },{
757 .name = "unit",
758 .type = QEMU_OPT_NUMBER,
759 .help = "unit number (i.e. lun for scsi)",
760 },{
761 .name = "index",
762 .type = QEMU_OPT_NUMBER,
763 .help = "index number",
764 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200765 .name = "media",
766 .type = QEMU_OPT_STRING,
767 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200768 },{
769 .name = "if",
770 .type = QEMU_OPT_STRING,
771 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200772 },{
773 .name = "cyls",
774 .type = QEMU_OPT_NUMBER,
775 .help = "number of cylinders (ide disk geometry)",
776 },{
777 .name = "heads",
778 .type = QEMU_OPT_NUMBER,
779 .help = "number of heads (ide disk geometry)",
780 },{
781 .name = "secs",
782 .type = QEMU_OPT_NUMBER,
783 .help = "number of sectors (ide disk geometry)",
784 },{
785 .name = "trans",
786 .type = QEMU_OPT_STRING,
787 .help = "chs translation (auto, lba, none)",
Kevin Wolf26929292013-09-09 17:01:03 +0200788 },{
789 .name = "boot",
790 .type = QEMU_OPT_BOOL,
791 .help = "(deprecated, ignored)",
Kevin Wolf394c7d42013-09-13 14:09:17 +0200792 },{
793 .name = "addr",
794 .type = QEMU_OPT_STRING,
795 .help = "pci address (virtio only)",
Max Reitzd095b462013-12-20 19:28:14 +0100796 },{
Kevin Wolfbcf83152014-06-06 15:21:48 +0200797 .name = "serial",
798 .type = QEMU_OPT_STRING,
799 .help = "disk serial number",
800 },{
Max Reitzd095b462013-12-20 19:28:14 +0100801 .name = "file",
802 .type = QEMU_OPT_STRING,
803 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200804 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200805
806 /* Options that are passed on, but have special semantics with -drive */
807 {
808 .name = "read-only",
809 .type = QEMU_OPT_BOOL,
810 .help = "open drive file as read-only",
811 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100812 .name = "rerror",
813 .type = QEMU_OPT_STRING,
814 .help = "read error action",
815 },{
816 .name = "werror",
817 .type = QEMU_OPT_STRING,
818 .help = "write error action",
819 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200820 .name = "copy-on-read",
821 .type = QEMU_OPT_BOOL,
822 .help = "copy read data from backing file into image file",
823 },
824
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200825 { /* end of list */ }
826 },
827};
828
Markus Armbruster60e19e02014-06-06 14:50:58 +0200829DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
Kevin Wolf57975222013-07-17 14:41:54 +0200830{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200831 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200832 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200833 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200834 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200835 QemuOpts *legacy_opts;
836 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200837 BlockInterfaceType type;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200838 int cyls, heads, secs, translation;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200839 int max_devs, bus_id, unit_id, index;
Kevin Wolf394c7d42013-09-13 14:09:17 +0200840 const char *devaddr;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100841 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800842 bool read_only = false;
843 bool copy_on_read;
Kevin Wolfbcf83152014-06-06 15:21:48 +0200844 const char *serial;
Max Reitzd095b462013-12-20 19:28:14 +0100845 const char *filename;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200846 Error *local_err = NULL;
Kevin Wolf247147f2014-09-24 16:37:14 +0200847 int i;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200848
Kevin Wolf57975222013-07-17 14:41:54 +0200849 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200850 static const struct {
851 const char *from;
852 const char *to;
853 } opt_renames[] = {
854 { "iops", "throttling.iops-total" },
855 { "iops_rd", "throttling.iops-read" },
856 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200857
Kevin Wolf247147f2014-09-24 16:37:14 +0200858 { "bps", "throttling.bps-total" },
859 { "bps_rd", "throttling.bps-read" },
860 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200861
Kevin Wolf247147f2014-09-24 16:37:14 +0200862 { "iops_max", "throttling.iops-total-max" },
863 { "iops_rd_max", "throttling.iops-read-max" },
864 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200865
Kevin Wolf247147f2014-09-24 16:37:14 +0200866 { "bps_max", "throttling.bps-total-max" },
867 { "bps_rd_max", "throttling.bps-read-max" },
868 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200869
Kevin Wolf247147f2014-09-24 16:37:14 +0200870 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200871
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200872 { "group", "throttling.group" },
873
Kevin Wolf247147f2014-09-24 16:37:14 +0200874 { "readonly", "read-only" },
875 };
876
877 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200878 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
879 &local_err);
880 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100881 error_report_err(local_err);
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200882 return NULL;
883 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200884 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200885
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200886 value = qemu_opt_get(all_opts, "cache");
887 if (value) {
888 int flags = 0;
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100889 bool writethrough;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200890
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100891 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200892 error_report("invalid cache option");
893 return NULL;
894 }
895
896 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200897 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
898 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100899 !writethrough, &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200900 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200901 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
902 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100903 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200904 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200905 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
906 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100907 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200908 }
909 qemu_opt_unset(all_opts, "cache");
910 }
911
Kevin Wolff298d072013-09-10 12:01:20 +0200912 /* Get a QDict for processing the options */
913 bs_opts = qdict_new();
914 qemu_opts_to_qdict(all_opts, bs_opts);
915
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800916 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
917 &error_abort);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200918 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100919 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100920 error_report_err(local_err);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200921 goto fail;
922 }
923
Kevin Wolf26929292013-09-09 17:01:03 +0200924 /* Deprecated option boot=[on|off] */
925 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
926 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
927 "ignored. Future versions will reject this parameter. Please "
928 "update your scripts.\n");
929 }
930
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200931 /* Media type */
932 value = qemu_opt_get(legacy_opts, "media");
933 if (value) {
934 if (!strcmp(value, "disk")) {
935 media = MEDIA_DISK;
936 } else if (!strcmp(value, "cdrom")) {
937 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800938 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200939 } else {
940 error_report("'%s' invalid media", value);
941 goto fail;
942 }
943 }
944
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200945 /* copy-on-read is disabled with a warning for read-only devices */
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800946 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200947 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
948
949 if (read_only && copy_on_read) {
950 error_report("warning: disabling copy-on-read on read-only drive");
951 copy_on_read = false;
952 }
953
954 qdict_put(bs_opts, "read-only",
955 qstring_from_str(read_only ? "on" : "off"));
956 qdict_put(bs_opts, "copy-on-read",
957 qstring_from_str(copy_on_read ? "on" :"off"));
958
Kevin Wolf593d4642013-08-28 17:24:51 +0200959 /* Controller type */
960 value = qemu_opt_get(legacy_opts, "if");
961 if (value) {
962 for (type = 0;
963 type < IF_COUNT && strcmp(value, if_name[type]);
964 type++) {
965 }
966 if (type == IF_COUNT) {
967 error_report("unsupported bus type '%s'", value);
968 goto fail;
969 }
970 } else {
971 type = block_default_type;
972 }
973
Kevin Wolfb41a7332013-09-09 16:49:49 +0200974 /* Geometry */
975 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
976 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
977 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
978
979 if (cyls || heads || secs) {
980 if (cyls < 1) {
981 error_report("invalid physical cyls number");
982 goto fail;
983 }
984 if (heads < 1) {
985 error_report("invalid physical heads number");
986 goto fail;
987 }
988 if (secs < 1) {
989 error_report("invalid physical secs number");
990 goto fail;
991 }
992 }
993
994 translation = BIOS_ATA_TRANSLATION_AUTO;
995 value = qemu_opt_get(legacy_opts, "trans");
996 if (value != NULL) {
997 if (!cyls) {
998 error_report("'%s' trans must be used with cyls, heads and secs",
999 value);
1000 goto fail;
1001 }
1002 if (!strcmp(value, "none")) {
1003 translation = BIOS_ATA_TRANSLATION_NONE;
1004 } else if (!strcmp(value, "lba")) {
1005 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01001006 } else if (!strcmp(value, "large")) {
1007 translation = BIOS_ATA_TRANSLATION_LARGE;
1008 } else if (!strcmp(value, "rechs")) {
1009 translation = BIOS_ATA_TRANSLATION_RECHS;
Kevin Wolfb41a7332013-09-09 16:49:49 +02001010 } else if (!strcmp(value, "auto")) {
1011 translation = BIOS_ATA_TRANSLATION_AUTO;
1012 } else {
1013 error_report("'%s' invalid translation type", value);
1014 goto fail;
1015 }
1016 }
1017
1018 if (media == MEDIA_CDROM) {
1019 if (cyls || secs || heads) {
1020 error_report("CHS can't be set with media=cdrom");
1021 goto fail;
1022 }
1023 }
1024
Kevin Wolf87a899c2013-09-10 15:48:13 +02001025 /* Device address specified by bus/unit or index.
1026 * If none was specified, try to find the first free one. */
1027 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
1028 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
1029 index = qemu_opt_get_number(legacy_opts, "index", -1);
1030
1031 max_devs = if_max_devs[type];
1032
1033 if (index != -1) {
1034 if (bus_id != 0 || unit_id != -1) {
1035 error_report("index cannot be used with bus and unit");
1036 goto fail;
1037 }
1038 bus_id = drive_index_to_bus_id(type, index);
1039 unit_id = drive_index_to_unit_id(type, index);
1040 }
1041
1042 if (unit_id == -1) {
1043 unit_id = 0;
1044 while (drive_get(type, bus_id, unit_id) != NULL) {
1045 unit_id++;
1046 if (max_devs && unit_id >= max_devs) {
1047 unit_id -= max_devs;
1048 bus_id++;
1049 }
1050 }
1051 }
1052
1053 if (max_devs && unit_id >= max_devs) {
1054 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1055 goto fail;
1056 }
1057
1058 if (drive_get(type, bus_id, unit_id) != NULL) {
1059 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1060 bus_id, unit_id, index);
1061 goto fail;
1062 }
1063
Kevin Wolfbcf83152014-06-06 15:21:48 +02001064 /* Serial number */
1065 serial = qemu_opt_get(legacy_opts, "serial");
1066
Kevin Wolf87a899c2013-09-10 15:48:13 +02001067 /* no id supplied -> create one */
1068 if (qemu_opts_id(all_opts) == NULL) {
1069 char *new_id;
1070 const char *mediastr = "";
1071 if (type == IF_IDE || type == IF_SCSI) {
1072 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1073 }
1074 if (max_devs) {
1075 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1076 mediastr, unit_id);
1077 } else {
1078 new_id = g_strdup_printf("%s%s%i", if_name[type],
1079 mediastr, unit_id);
1080 }
1081 qdict_put(bs_opts, "id", qstring_from_str(new_id));
1082 g_free(new_id);
1083 }
1084
Kevin Wolf394c7d42013-09-13 14:09:17 +02001085 /* Add virtio block device */
1086 devaddr = qemu_opt_get(legacy_opts, "addr");
1087 if (devaddr && type != IF_VIRTIO) {
1088 error_report("addr is not supported by this bus type");
1089 goto fail;
1090 }
1091
1092 if (type == IF_VIRTIO) {
1093 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001094 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1095 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001096 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +02001097 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001098 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001099 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001100 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001101 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1102 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001103 if (devaddr) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001104 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001105 }
1106 }
1107
Max Reitzd095b462013-12-20 19:28:14 +01001108 filename = qemu_opt_get(legacy_opts, "file");
1109
Kevin Wolfee13ed12014-02-09 09:52:32 +01001110 /* Check werror/rerror compatibility with if=... */
1111 werror = qemu_opt_get(legacy_opts, "werror");
1112 if (werror != NULL) {
1113 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1114 type != IF_NONE) {
1115 error_report("werror is not supported by this bus type");
1116 goto fail;
1117 }
1118 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1119 }
1120
1121 rerror = qemu_opt_get(legacy_opts, "rerror");
1122 if (rerror != NULL) {
1123 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1124 type != IF_NONE) {
1125 error_report("rerror is not supported by this bus type");
1126 goto fail;
1127 }
1128 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1129 }
1130
Kevin Wolf2d246f02013-09-18 15:14:47 +02001131 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster18e46a02014-10-07 13:59:06 +02001132 blk = blockdev_init(filename, bs_opts, &local_err);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001133 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +02001134 if (!blk) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001135 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001136 error_report_err(local_err);
Kevin Wolfb6810722013-09-20 11:33:11 +02001137 }
Kevin Wolf2d246f02013-09-18 15:14:47 +02001138 goto fail;
Kevin Wolfb6810722013-09-20 11:33:11 +02001139 } else {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001140 assert(!local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001141 }
1142
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001143 /* Create legacy DriveInfo */
1144 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001145 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001146
Kevin Wolfb41a7332013-09-09 16:49:49 +02001147 dinfo->cyls = cyls;
1148 dinfo->heads = heads;
1149 dinfo->secs = secs;
1150 dinfo->trans = translation;
1151
Kevin Wolfee13ed12014-02-09 09:52:32 +01001152 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001153 dinfo->bus = bus_id;
1154 dinfo->unit = unit_id;
Kevin Wolf394c7d42013-09-13 14:09:17 +02001155 dinfo->devaddr = devaddr;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001156 dinfo->serial = g_strdup(serial);
1157
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001158 blk_set_legacy_dinfo(blk, dinfo);
1159
Kevin Wolfe34ef042013-09-19 14:24:10 +02001160 switch(type) {
1161 case IF_IDE:
1162 case IF_SCSI:
1163 case IF_XEN:
1164 case IF_NONE:
1165 dinfo->media_cd = media == MEDIA_CDROM;
1166 break;
1167 default:
1168 break;
1169 }
1170
Kevin Wolf2d246f02013-09-18 15:14:47 +02001171fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001172 qemu_opts_del(legacy_opts);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001173 QDECREF(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001174 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001175}
1176
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02001177static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1178{
1179 BlockDriverState *bs;
1180
1181 bs = bdrv_lookup_bs(name, name, errp);
1182 if (bs == NULL) {
1183 return NULL;
1184 }
1185
1186 if (!bdrv_is_root_node(bs)) {
1187 error_setg(errp, "Need a root block node");
1188 return NULL;
1189 }
1190
1191 if (!bdrv_is_inserted(bs)) {
1192 error_setg(errp, "Device has no medium");
1193 return NULL;
1194 }
1195
1196 return bs;
1197}
1198
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001199void hmp_commit(Monitor *mon, const QDict *qdict)
Markus Armbruster666daa62010-06-02 18:48:27 +02001200{
Markus Armbruster666daa62010-06-02 18:48:27 +02001201 const char *device = qdict_get_str(qdict, "device");
Fam Zhenga0e85442015-03-02 19:36:48 +08001202 BlockBackend *blk;
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001203 int ret;
Markus Armbruster666daa62010-06-02 18:48:27 +02001204
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001205 if (!strcmp(device, "all")) {
Max Reitzda31d592016-03-16 19:54:32 +01001206 ret = blk_commit_all();
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001207 } else {
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001208 BlockDriverState *bs;
1209 AioContext *aio_context;
1210
Fam Zhenga0e85442015-03-02 19:36:48 +08001211 blk = blk_by_name(device);
1212 if (!blk) {
Jeff Cody58513bd2013-01-18 12:45:35 -05001213 monitor_printf(mon, "Device '%s' not found\n", device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +02001214 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001215 }
Max Reitz5433c242015-10-19 17:53:29 +02001216 if (!blk_is_available(blk)) {
1217 monitor_printf(mon, "Device '%s' has no medium\n", device);
1218 return;
1219 }
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001220
1221 bs = blk_bs(blk);
1222 aio_context = bdrv_get_aio_context(bs);
1223 aio_context_acquire(aio_context);
1224
1225 ret = bdrv_commit(bs);
1226
1227 aio_context_release(aio_context);
Jeff Cody58513bd2013-01-18 12:45:35 -05001228 }
1229 if (ret < 0) {
1230 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1231 strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +02001232 }
1233}
1234
Eric Blake10f75902016-03-03 09:16:50 -07001235static void blockdev_do_action(TransactionAction *action, Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001236{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001237 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001238
Eric Blake10f75902016-03-03 09:16:50 -07001239 list.value = action;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001240 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001241 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001242}
1243
Benoît Canet0901f672014-01-23 21:31:38 +01001244void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1245 bool has_node_name, const char *node_name,
1246 const char *snapshot_file,
1247 bool has_snapshot_node_name,
1248 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001249 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001250 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001251{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001252 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001253 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001254 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001255 .has_node_name = has_node_name,
1256 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001257 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001258 .has_snapshot_node_name = has_snapshot_node_name,
1259 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001260 .has_format = has_format,
1261 .format = (char *) format,
1262 .has_mode = has_mode,
1263 .mode = mode,
1264 };
Eric Blake10f75902016-03-03 09:16:50 -07001265 TransactionAction action = {
1266 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001267 .u.blockdev_snapshot_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001268 };
1269 blockdev_do_action(&action, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001270}
1271
Alberto Garcia43de7e22015-10-26 14:27:16 +02001272void qmp_blockdev_snapshot(const char *node, const char *overlay,
1273 Error **errp)
1274{
1275 BlockdevSnapshot snapshot_data = {
1276 .node = (char *) node,
1277 .overlay = (char *) overlay
1278 };
Eric Blake10f75902016-03-03 09:16:50 -07001279 TransactionAction action = {
1280 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
Eric Blake32bafa82016-03-17 16:48:37 -06001281 .u.blockdev_snapshot.data = &snapshot_data,
Eric Blake10f75902016-03-03 09:16:50 -07001282 };
1283 blockdev_do_action(&action, errp);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001284}
1285
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001286void qmp_blockdev_snapshot_internal_sync(const char *device,
1287 const char *name,
1288 Error **errp)
1289{
1290 BlockdevSnapshotInternal snapshot = {
1291 .device = (char *) device,
1292 .name = (char *) name
1293 };
Eric Blake10f75902016-03-03 09:16:50 -07001294 TransactionAction action = {
1295 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001296 .u.blockdev_snapshot_internal_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001297 };
1298 blockdev_do_action(&action, errp);
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001299}
1300
Wenchao Xia44e3e052013-09-11 14:04:36 +08001301SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1302 bool has_id,
1303 const char *id,
1304 bool has_name,
1305 const char *name,
1306 Error **errp)
1307{
Fam Zhenga0e85442015-03-02 19:36:48 +08001308 BlockDriverState *bs;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001309 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001310 QEMUSnapshotInfo sn;
1311 Error *local_err = NULL;
1312 SnapshotInfo *info = NULL;
1313 int ret;
1314
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001315 bs = qmp_get_root_bs(device, errp);
1316 if (!bs) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001317 return NULL;
1318 }
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001319 aio_context = bdrv_get_aio_context(bs);
Max Reitz5433c242015-10-19 17:53:29 +02001320 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001321
1322 if (!has_id) {
1323 id = NULL;
1324 }
1325
1326 if (!has_name) {
1327 name = NULL;
1328 }
1329
1330 if (!id && !name) {
1331 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001332 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001333 }
1334
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001335 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1336 goto out_aio_context;
1337 }
1338
Wenchao Xia44e3e052013-09-11 14:04:36 +08001339 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001340 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001341 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001342 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001343 }
1344 if (!ret) {
1345 error_setg(errp,
1346 "Snapshot with id '%s' and name '%s' does not exist on "
1347 "device '%s'",
1348 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001349 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001350 }
1351
1352 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001353 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001354 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001355 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001356 }
1357
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001358 aio_context_release(aio_context);
1359
Markus Armbruster5839e532014-08-19 10:31:08 +02001360 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001361 info->id = g_strdup(sn.id_str);
1362 info->name = g_strdup(sn.name);
1363 info->date_nsec = sn.date_nsec;
1364 info->date_sec = sn.date_sec;
1365 info->vm_state_size = sn.vm_state_size;
1366 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1367 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1368
1369 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001370
1371out_aio_context:
1372 aio_context_release(aio_context);
1373 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001374}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001375
John Snow341ebc22015-04-17 19:49:52 -04001376/**
1377 * block_dirty_bitmap_lookup:
1378 * Return a dirty bitmap (if present), after validating
1379 * the node reference and bitmap names.
1380 *
1381 * @node: The name of the BDS node to search for bitmaps
1382 * @name: The name of the bitmap to search for
1383 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1384 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1385 * @errp: Output pointer for error information. Can be NULL.
1386 *
1387 * @return: A bitmap object on success, or NULL on failure.
1388 */
1389static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1390 const char *name,
1391 BlockDriverState **pbs,
1392 AioContext **paio,
1393 Error **errp)
1394{
1395 BlockDriverState *bs;
1396 BdrvDirtyBitmap *bitmap;
1397 AioContext *aio_context;
1398
1399 if (!node) {
1400 error_setg(errp, "Node cannot be NULL");
1401 return NULL;
1402 }
1403 if (!name) {
1404 error_setg(errp, "Bitmap name cannot be NULL");
1405 return NULL;
1406 }
1407 bs = bdrv_lookup_bs(node, node, NULL);
1408 if (!bs) {
1409 error_setg(errp, "Node '%s' not found", node);
1410 return NULL;
1411 }
1412
1413 aio_context = bdrv_get_aio_context(bs);
1414 aio_context_acquire(aio_context);
1415
1416 bitmap = bdrv_find_dirty_bitmap(bs, name);
1417 if (!bitmap) {
1418 error_setg(errp, "Dirty bitmap '%s' not found", name);
1419 goto fail;
1420 }
1421
1422 if (pbs) {
1423 *pbs = bs;
1424 }
1425 if (paio) {
1426 *paio = aio_context;
1427 } else {
1428 aio_context_release(aio_context);
1429 }
1430
1431 return bitmap;
1432
1433 fail:
1434 aio_context_release(aio_context);
1435 return NULL;
1436}
1437
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001438/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001439
John Snow50f43f02015-11-05 18:13:09 -05001440typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001441
John Snow50f43f02015-11-05 18:13:09 -05001442/**
1443 * BlkActionOps:
1444 * Table of operations that define an Action.
1445 *
1446 * @instance_size: Size of state struct, in bytes.
1447 * @prepare: Prepare the work, must NOT be NULL.
1448 * @commit: Commit the changes, can be NULL.
1449 * @abort: Abort the changes on fail, can be NULL.
1450 * @clean: Clean up resources after all transaction actions have called
1451 * commit() or abort(). Can be NULL.
1452 *
1453 * Only prepare() may fail. In a single transaction, only one of commit() or
1454 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001455 */
John Snow50f43f02015-11-05 18:13:09 -05001456typedef struct BlkActionOps {
1457 size_t instance_size;
1458 void (*prepare)(BlkActionState *common, Error **errp);
1459 void (*commit)(BlkActionState *common);
1460 void (*abort)(BlkActionState *common);
1461 void (*clean)(BlkActionState *common);
1462} BlkActionOps;
1463
1464/**
1465 * BlkActionState:
1466 * Describes one Action's state within a Transaction.
1467 *
1468 * @action: QAPI-defined enum identifying which Action to perform.
1469 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001470 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001471 * @entry: List membership for all Actions in this Transaction.
1472 *
1473 * This structure must be arranged as first member in a subclassed type,
1474 * assuming that the compiler will also arrange it to the same offsets as the
1475 * base class.
1476 */
1477struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001478 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001479 const BlkActionOps *ops;
John Snow94d16a62015-11-05 18:13:18 -05001480 BlockJobTxn *block_job_txn;
1481 TransactionProperties *txn_props;
John Snow50f43f02015-11-05 18:13:09 -05001482 QSIMPLEQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001483};
1484
Wenchao Xiabbe86012013-09-11 14:04:34 +08001485/* internal snapshot private data */
1486typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001487 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001488 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001489 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001490 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001491 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001492} InternalSnapshotState;
1493
John Snow94d16a62015-11-05 18:13:18 -05001494
1495static int action_check_completion_mode(BlkActionState *s, Error **errp)
1496{
1497 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1498 error_setg(errp,
1499 "Action '%s' does not support Transaction property "
1500 "completion-mode = %s",
1501 TransactionActionKind_lookup[s->action->type],
1502 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1503 return -1;
1504 }
1505 return 0;
1506}
1507
John Snow50f43f02015-11-05 18:13:09 -05001508static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001509 Error **errp)
1510{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001511 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001512 const char *device;
1513 const char *name;
1514 BlockDriverState *bs;
1515 QEMUSnapshotInfo old_sn, *sn;
1516 bool ret;
1517 qemu_timeval tv;
1518 BlockdevSnapshotInternal *internal;
1519 InternalSnapshotState *state;
1520 int ret1;
1521
Eric Blake6a8f9662015-10-26 16:34:54 -06001522 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001523 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001524 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001525 state = DO_UPCAST(InternalSnapshotState, common, common);
1526
1527 /* 1. parse input */
1528 device = internal->device;
1529 name = internal->name;
1530
1531 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001532 if (action_check_completion_mode(common, errp) < 0) {
1533 return;
1534 }
1535
Kevin Wolf75dfd402016-06-23 14:20:24 +02001536 bs = qmp_get_root_bs(device, errp);
1537 if (!bs) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001538 return;
1539 }
1540
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001541 /* AioContext is released in .clean() */
Kevin Wolf75dfd402016-06-23 14:20:24 +02001542 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001543 aio_context_acquire(state->aio_context);
1544
Fam Zheng507306c2015-10-23 11:08:13 +08001545 state->bs = bs;
1546 bdrv_drained_begin(bs);
1547
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001548 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1549 return;
1550 }
1551
Wenchao Xiabbe86012013-09-11 14:04:34 +08001552 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001553 error_setg(errp, "Device '%s' is read only", device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001554 return;
1555 }
1556
1557 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001558 error_setg(errp, "Block format '%s' used by device '%s' "
1559 "does not support internal snapshots",
1560 bs->drv->format_name, device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001561 return;
1562 }
1563
1564 if (!strlen(name)) {
1565 error_setg(errp, "Name is empty");
1566 return;
1567 }
1568
1569 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001570 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1571 &local_err);
1572 if (local_err) {
1573 error_propagate(errp, local_err);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001574 return;
1575 } else if (ret) {
1576 error_setg(errp,
1577 "Snapshot with name '%s' already exists on device '%s'",
1578 name, device);
1579 return;
1580 }
1581
1582 /* 3. take the snapshot */
1583 sn = &state->sn;
1584 pstrcpy(sn->name, sizeof(sn->name), name);
1585 qemu_gettimeofday(&tv);
1586 sn->date_sec = tv.tv_sec;
1587 sn->date_nsec = tv.tv_usec * 1000;
1588 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1589
1590 ret1 = bdrv_snapshot_create(bs, sn);
1591 if (ret1 < 0) {
1592 error_setg_errno(errp, -ret1,
1593 "Failed to create snapshot '%s' on device '%s'",
1594 name, device);
1595 return;
1596 }
1597
1598 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001599 state->created = true;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001600}
1601
John Snow50f43f02015-11-05 18:13:09 -05001602static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001603{
1604 InternalSnapshotState *state =
1605 DO_UPCAST(InternalSnapshotState, common, common);
1606 BlockDriverState *bs = state->bs;
1607 QEMUSnapshotInfo *sn = &state->sn;
1608 Error *local_error = NULL;
1609
Fam Zheng507306c2015-10-23 11:08:13 +08001610 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001611 return;
1612 }
1613
1614 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001615 error_reportf_err(local_error,
1616 "Failed to delete snapshot with id '%s' and "
1617 "name '%s' on device '%s' in abort: ",
1618 sn->id_str, sn->name,
1619 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001620 }
1621}
1622
John Snow50f43f02015-11-05 18:13:09 -05001623static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001624{
1625 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1626 common, common);
1627
1628 if (state->aio_context) {
Fam Zheng507306c2015-10-23 11:08:13 +08001629 if (state->bs) {
1630 bdrv_drained_end(state->bs);
1631 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001632 aio_context_release(state->aio_context);
1633 }
1634}
1635
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001636/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001637typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001638 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001639 BlockDriverState *old_bs;
1640 BlockDriverState *new_bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001641 AioContext *aio_context;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001642} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001643
John Snow50f43f02015-11-05 18:13:09 -05001644static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001645 Error **errp)
1646{
Max Reitz5b363932016-05-17 16:41:31 +02001647 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001648 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001649 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001650 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001651 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001652 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001653 /* Reference to the new image (for 'blockdev-snapshot') */
1654 const char *snapshot_ref;
1655 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001656 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001657 ExternalSnapshotState *state =
1658 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001659 TransactionAction *action = common->action;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001660
Alberto Garcia43de7e22015-10-26 14:27:16 +02001661 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1662 * purpose but a different set of parameters */
1663 switch (action->type) {
1664 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1665 {
Eric Blake32bafa82016-03-17 16:48:37 -06001666 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001667 device = s->node;
1668 node_name = s->node;
1669 new_image_file = NULL;
1670 snapshot_ref = s->overlay;
1671 }
1672 break;
1673 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1674 {
Eric Blake32bafa82016-03-17 16:48:37 -06001675 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001676 device = s->has_device ? s->device : NULL;
1677 node_name = s->has_node_name ? s->node_name : NULL;
1678 new_image_file = s->snapshot_file;
1679 snapshot_ref = NULL;
1680 }
1681 break;
1682 default:
1683 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001684 }
1685
1686 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001687 if (action_check_completion_mode(common, errp) < 0) {
1688 return;
1689 }
1690
Alberto Garcia43de7e22015-10-26 14:27:16 +02001691 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1692 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001693 return;
1694 }
1695
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001696 /* Acquire AioContext now so any threads operating on old_bs stop */
1697 state->aio_context = bdrv_get_aio_context(state->old_bs);
1698 aio_context_acquire(state->aio_context);
Fam Zhengda763e82015-10-23 11:08:10 +08001699 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001700
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001701 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001702 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001703 return;
1704 }
1705
Fam Zheng3718d8a2014-05-23 21:29:43 +08001706 if (bdrv_op_is_blocked(state->old_bs,
1707 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001708 return;
1709 }
1710
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001711 if (!bdrv_is_read_only(state->old_bs)) {
1712 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001713 error_setg(errp, QERR_IO_ERROR);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001714 return;
1715 }
1716 }
1717
Benoît Canet212a5a82014-01-23 21:31:36 +01001718 if (!bdrv_is_first_non_filter(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001719 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
Benoît Canetf6186f42013-10-02 14:33:48 +02001720 return;
1721 }
1722
Alberto Garcia43de7e22015-10-26 14:27:16 +02001723 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001724 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001725 const char *format = s->has_format ? s->format : "qcow2";
1726 enum NewImageMode mode;
1727 const char *snapshot_node_name =
1728 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001729
Alberto Garcia43de7e22015-10-26 14:27:16 +02001730 if (node_name && !snapshot_node_name) {
1731 error_setg(errp, "New snapshot node name missing");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001732 return;
1733 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001734
1735 if (snapshot_node_name &&
1736 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1737 error_setg(errp, "New snapshot node name already in use");
1738 return;
1739 }
1740
1741 flags = state->old_bs->open_flags;
Kevin Wolf4c844982016-02-29 13:12:26 +01001742 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001743
1744 /* create new image w/backing file */
1745 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1746 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001747 int64_t size = bdrv_getlength(state->old_bs);
1748 if (size < 0) {
1749 error_setg_errno(errp, -size, "bdrv_getlength failed");
1750 return;
1751 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001752 bdrv_img_create(new_image_file, format,
1753 state->old_bs->filename,
1754 state->old_bs->drv->format_name,
Kevin Wolff86b8b52016-03-02 12:16:44 +01001755 NULL, size, flags, &local_err, false);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001756 if (local_err) {
1757 error_propagate(errp, local_err);
1758 return;
1759 }
1760 }
1761
1762 options = qdict_new();
1763 if (s->has_snapshot_node_name) {
1764 qdict_put(options, "node-name",
1765 qstring_from_str(snapshot_node_name));
1766 }
1767 qdict_put(options, "driver", qstring_from_str(format));
1768
1769 flags |= BDRV_O_NO_BACKING;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001770 }
1771
Max Reitz5b363932016-05-17 16:41:31 +02001772 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1773 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001774 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001775 if (!state->new_bs) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001776 return;
1777 }
1778
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001779 if (bdrv_has_blk(state->new_bs)) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001780 error_setg(errp, "The snapshot is already in use by %s",
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001781 bdrv_get_parent_name(state->new_bs));
Alberto Garcia43de7e22015-10-26 14:27:16 +02001782 return;
1783 }
1784
1785 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1786 errp)) {
1787 return;
1788 }
1789
1790 if (state->new_bs->backing != NULL) {
1791 error_setg(errp, "The snapshot already has a backing image");
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001792 return;
1793 }
1794
1795 if (!state->new_bs->drv->supports_backing) {
1796 error_setg(errp, "The snapshot does not support backing images");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001797 }
1798}
1799
John Snow50f43f02015-11-05 18:13:09 -05001800static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001801{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001802 ExternalSnapshotState *state =
1803 DO_UPCAST(ExternalSnapshotState, common, common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001804
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001805 bdrv_set_aio_context(state->new_bs, state->aio_context);
1806
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001807 /* This removes our old bs and adds the new bs */
1808 bdrv_append(state->new_bs, state->old_bs);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001809 /* We don't need (or want) to use the transactional
1810 * bdrv_reopen_multiple() across all the entries at once, because we
1811 * don't want to abort all of them if one of them fails the reopen */
Kevin Wolf4c844982016-02-29 13:12:26 +01001812 if (!state->old_bs->copy_on_read) {
1813 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1814 NULL);
1815 }
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001816}
1817
John Snow50f43f02015-11-05 18:13:09 -05001818static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001819{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001820 ExternalSnapshotState *state =
1821 DO_UPCAST(ExternalSnapshotState, common, common);
1822 if (state->new_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001823 bdrv_unref(state->new_bs);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001824 }
Fam Zhengda763e82015-10-23 11:08:10 +08001825}
1826
John Snow50f43f02015-11-05 18:13:09 -05001827static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001828{
1829 ExternalSnapshotState *state =
1830 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001831 if (state->aio_context) {
Fam Zhengda763e82015-10-23 11:08:10 +08001832 bdrv_drained_end(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001833 aio_context_release(state->aio_context);
1834 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001835}
1836
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001837typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001838 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001839 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001840 AioContext *aio_context;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001841 BlockJob *job;
1842} DriveBackupState;
1843
Pavel Butsykin81206a82016-07-22 11:17:50 +03001844static void do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
1845 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001846
John Snow50f43f02015-11-05 18:13:09 -05001847static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001848{
1849 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001850 BlockDriverState *bs;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001851 DriveBackup *backup;
1852 Error *local_err = NULL;
1853
Eric Blake6a8f9662015-10-26 16:34:54 -06001854 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001855 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001856
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001857 bs = qmp_get_root_bs(backup->device, errp);
1858 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001859 return;
1860 }
1861
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001862 /* AioContext is released in .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001863 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001864 aio_context_acquire(state->aio_context);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001865 bdrv_drained_begin(bs);
1866 state->bs = bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001867
Pavel Butsykin81206a82016-07-22 11:17:50 +03001868 do_drive_backup(backup, common->block_job_txn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001869 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001870 error_propagate(errp, local_err);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001871 return;
1872 }
1873
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001874 state->job = state->bs->job;
1875}
1876
John Snow50f43f02015-11-05 18:13:09 -05001877static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001878{
1879 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1880 BlockDriverState *bs = state->bs;
1881
1882 /* Only cancel if it's the job we started */
1883 if (bs && bs->job && bs->job == state->job) {
1884 block_job_cancel_sync(bs->job);
1885 }
1886}
1887
John Snow50f43f02015-11-05 18:13:09 -05001888static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001889{
1890 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1891
1892 if (state->aio_context) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001893 bdrv_drained_end(state->bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001894 aio_context_release(state->aio_context);
1895 }
1896}
1897
Fam Zhengbd8baec2014-12-18 18:37:06 +08001898typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001899 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001900 BlockDriverState *bs;
1901 BlockJob *job;
1902 AioContext *aio_context;
1903} BlockdevBackupState;
1904
Alberto Garcia70559d42016-07-05 17:28:58 +03001905static void do_blockdev_backup(const char *job_id, const char *device,
1906 const char *target, enum MirrorSyncMode sync,
John Snow78f51fd2015-11-05 18:13:17 -05001907 bool has_speed, int64_t speed,
1908 bool has_on_source_error,
1909 BlockdevOnError on_source_error,
1910 bool has_on_target_error,
1911 BlockdevOnError on_target_error,
1912 BlockJobTxn *txn, Error **errp);
1913
John Snow50f43f02015-11-05 18:13:09 -05001914static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001915{
1916 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1917 BlockdevBackup *backup;
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001918 BlockDriverState *bs, *target;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001919 Error *local_err = NULL;
1920
Eric Blake6a8f9662015-10-26 16:34:54 -06001921 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001922 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001923
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001924 bs = qmp_get_root_bs(backup->device, errp);
1925 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001926 return;
1927 }
1928
Kevin Wolf39d990a2016-08-02 19:22:04 +02001929 target = bdrv_lookup_bs(backup->target, backup->target, errp);
Max Reitz5433c242015-10-19 17:53:29 +02001930 if (!target) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001931 return;
1932 }
1933
1934 /* AioContext is released in .clean() */
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001935 state->aio_context = bdrv_get_aio_context(bs);
Kevin Wolf39d990a2016-08-02 19:22:04 +02001936 if (state->aio_context != bdrv_get_aio_context(target)) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001937 state->aio_context = NULL;
1938 error_setg(errp, "Backup between two IO threads is not implemented");
1939 return;
1940 }
1941 aio_context_acquire(state->aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001942 state->bs = bs;
Fam Zhengff52bf32015-10-23 11:08:12 +08001943 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001944
Alberto Garcia70559d42016-07-05 17:28:58 +03001945 do_blockdev_backup(backup->has_job_id ? backup->job_id : NULL,
1946 backup->device, backup->target, backup->sync,
John Snow78f51fd2015-11-05 18:13:17 -05001947 backup->has_speed, backup->speed,
1948 backup->has_on_source_error, backup->on_source_error,
1949 backup->has_on_target_error, backup->on_target_error,
John Snow94d16a62015-11-05 18:13:18 -05001950 common->block_job_txn, &local_err);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001951 if (local_err) {
1952 error_propagate(errp, local_err);
1953 return;
1954 }
1955
Fam Zhengbd8baec2014-12-18 18:37:06 +08001956 state->job = state->bs->job;
1957}
1958
John Snow50f43f02015-11-05 18:13:09 -05001959static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001960{
1961 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1962 BlockDriverState *bs = state->bs;
1963
1964 /* Only cancel if it's the job we started */
1965 if (bs && bs->job && bs->job == state->job) {
1966 block_job_cancel_sync(bs->job);
1967 }
1968}
1969
John Snow50f43f02015-11-05 18:13:09 -05001970static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001971{
1972 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1973
1974 if (state->aio_context) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001975 bdrv_drained_end(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001976 aio_context_release(state->aio_context);
1977 }
1978}
1979
Fam Zhengdf9a6812015-11-09 18:16:54 +08001980typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05001981 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001982 BdrvDirtyBitmap *bitmap;
1983 BlockDriverState *bs;
1984 AioContext *aio_context;
1985 HBitmap *backup;
1986 bool prepared;
1987} BlockDirtyBitmapState;
1988
John Snow50f43f02015-11-05 18:13:09 -05001989static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08001990 Error **errp)
1991{
1992 Error *local_err = NULL;
1993 BlockDirtyBitmapAdd *action;
1994 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1995 common, common);
1996
John Snow94d16a62015-11-05 18:13:18 -05001997 if (action_check_completion_mode(common, errp) < 0) {
1998 return;
1999 }
2000
Eric Blake32bafa82016-03-17 16:48:37 -06002001 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002002 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2003 qmp_block_dirty_bitmap_add(action->node, action->name,
2004 action->has_granularity, action->granularity,
2005 &local_err);
2006
2007 if (!local_err) {
2008 state->prepared = true;
2009 } else {
2010 error_propagate(errp, local_err);
2011 }
2012}
2013
John Snow50f43f02015-11-05 18:13:09 -05002014static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002015{
2016 BlockDirtyBitmapAdd *action;
2017 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2018 common, common);
2019
Eric Blake32bafa82016-03-17 16:48:37 -06002020 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002021 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2022 * then the node reference and bitmap name must have been valid.
2023 */
2024 if (state->prepared) {
2025 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2026 }
2027}
2028
John Snow50f43f02015-11-05 18:13:09 -05002029static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002030 Error **errp)
2031{
2032 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2033 common, common);
2034 BlockDirtyBitmap *action;
2035
John Snow94d16a62015-11-05 18:13:18 -05002036 if (action_check_completion_mode(common, errp) < 0) {
2037 return;
2038 }
2039
Eric Blake32bafa82016-03-17 16:48:37 -06002040 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002041 state->bitmap = block_dirty_bitmap_lookup(action->node,
2042 action->name,
2043 &state->bs,
2044 &state->aio_context,
2045 errp);
2046 if (!state->bitmap) {
2047 return;
2048 }
2049
2050 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2051 error_setg(errp, "Cannot modify a frozen bitmap");
2052 return;
2053 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2054 error_setg(errp, "Cannot clear a disabled bitmap");
2055 return;
2056 }
2057
2058 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2059 /* AioContext is released in .clean() */
2060}
2061
John Snow50f43f02015-11-05 18:13:09 -05002062static void block_dirty_bitmap_clear_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002063{
2064 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2065 common, common);
2066
2067 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2068}
2069
John Snow50f43f02015-11-05 18:13:09 -05002070static void block_dirty_bitmap_clear_commit(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002071{
2072 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2073 common, common);
2074
2075 hbitmap_free(state->backup);
2076}
2077
John Snow50f43f02015-11-05 18:13:09 -05002078static void block_dirty_bitmap_clear_clean(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002079{
2080 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2081 common, common);
2082
2083 if (state->aio_context) {
2084 aio_context_release(state->aio_context);
2085 }
2086}
2087
John Snow50f43f02015-11-05 18:13:09 -05002088static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002089{
2090 error_setg(errp, "Transaction aborted using Abort action");
2091}
2092
John Snow50f43f02015-11-05 18:13:09 -05002093static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002094{
Stefan Weildfc6f862013-07-25 18:21:28 +02002095 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002096}
2097
John Snow50f43f02015-11-05 18:13:09 -05002098static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002099 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2100 .instance_size = sizeof(ExternalSnapshotState),
2101 .prepare = external_snapshot_prepare,
2102 .commit = external_snapshot_commit,
2103 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002104 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002105 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002106 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002107 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002108 .prepare = external_snapshot_prepare,
2109 .commit = external_snapshot_commit,
2110 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002111 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002112 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002113 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2114 .instance_size = sizeof(DriveBackupState),
2115 .prepare = drive_backup_prepare,
2116 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002117 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002118 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002119 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2120 .instance_size = sizeof(BlockdevBackupState),
2121 .prepare = blockdev_backup_prepare,
2122 .abort = blockdev_backup_abort,
2123 .clean = blockdev_backup_clean,
2124 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002125 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002126 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002127 .prepare = abort_prepare,
2128 .commit = abort_commit,
2129 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002130 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2131 .instance_size = sizeof(InternalSnapshotState),
2132 .prepare = internal_snapshot_prepare,
2133 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002134 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002135 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002136 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2137 .instance_size = sizeof(BlockDirtyBitmapState),
2138 .prepare = block_dirty_bitmap_add_prepare,
2139 .abort = block_dirty_bitmap_add_abort,
2140 },
2141 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2142 .instance_size = sizeof(BlockDirtyBitmapState),
2143 .prepare = block_dirty_bitmap_clear_prepare,
2144 .commit = block_dirty_bitmap_clear_commit,
2145 .abort = block_dirty_bitmap_clear_abort,
2146 .clean = block_dirty_bitmap_clear_clean,
2147 }
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002148};
2149
John Snow94d16a62015-11-05 18:13:18 -05002150/**
2151 * Allocate a TransactionProperties structure if necessary, and fill
2152 * that structure with desired defaults if they are unset.
2153 */
2154static TransactionProperties *get_transaction_properties(
2155 TransactionProperties *props)
2156{
2157 if (!props) {
2158 props = g_new0(TransactionProperties, 1);
2159 }
2160
2161 if (!props->has_completion_mode) {
2162 props->has_completion_mode = true;
2163 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2164 }
2165
2166 return props;
2167}
2168
Jeff Cody8802d1f2012-02-28 15:54:06 -05002169/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002170 * 'Atomic' group operations. The operations are performed as a set, and if
2171 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002172 */
John Snow94d16a62015-11-05 18:13:18 -05002173void qmp_transaction(TransactionActionList *dev_list,
2174 bool has_props,
2175 struct TransactionProperties *props,
2176 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002177{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002178 TransactionActionList *dev_entry = dev_list;
John Snow94d16a62015-11-05 18:13:18 -05002179 BlockJobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002180 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002181 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002182
John Snow50f43f02015-11-05 18:13:09 -05002183 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002184 QSIMPLEQ_INIT(&snap_bdrv_states);
2185
John Snow94d16a62015-11-05 18:13:18 -05002186 /* Does this transaction get canceled as a group on failure?
2187 * If not, we don't really need to make a BlockJobTxn.
2188 */
2189 props = get_transaction_properties(props);
2190 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2191 block_job_txn = block_job_txn_new();
2192 }
2193
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002194 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002195 bdrv_drain_all();
2196
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002197 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002198 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002199 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002200 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002201
Jeff Cody8802d1f2012-02-28 15:54:06 -05002202 dev_info = dev_entry->value;
2203 dev_entry = dev_entry->next;
2204
Eric Blake6a8f9662015-10-26 16:34:54 -06002205 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002206
Eric Blake6a8f9662015-10-26 16:34:54 -06002207 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002208 assert(ops->instance_size > 0);
2209
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002210 state = g_malloc0(ops->instance_size);
2211 state->ops = ops;
2212 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002213 state->block_job_txn = block_job_txn;
2214 state->txn_props = props;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002215 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002216
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002217 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002218 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002219 error_propagate(errp, local_err);
2220 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002221 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002222 }
2223
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002224 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002225 if (state->ops->commit) {
2226 state->ops->commit(state);
2227 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002228 }
2229
2230 /* success */
2231 goto exit;
2232
2233delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002234 /* failure, and it is all-or-none; roll back all operations */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002235 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2236 if (state->ops->abort) {
2237 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002238 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002239 }
2240exit:
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002241 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2242 if (state->ops->clean) {
2243 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002244 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002245 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002246 }
John Snow94d16a62015-11-05 18:13:18 -05002247 if (!has_props) {
2248 qapi_free_TransactionProperties(props);
2249 }
2250 block_job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002251}
2252
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002253void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002254{
Max Reitz38f54bd2015-10-26 21:39:12 +01002255 Error *local_err = NULL;
John Snow3a3086b2016-05-18 17:53:40 -04002256 int rc;
Markus Armbruster666daa62010-06-02 18:48:27 +02002257
John Snow3a3086b2016-05-18 17:53:40 -04002258 if (!has_force) {
2259 force = false;
2260 }
2261
2262 rc = do_open_tray(device, force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002263 if (rc && rc != -ENOSYS) {
Max Reitz38f54bd2015-10-26 21:39:12 +01002264 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002265 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002266 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002267 error_free(local_err);
John Snow3a3086b2016-05-18 17:53:40 -04002268
Max Reitz6e0abc22015-12-11 16:23:05 +01002269 qmp_x_blockdev_remove_medium(device, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002270}
2271
Benoît Canet12d3ba82014-01-23 21:31:35 +01002272void qmp_block_passwd(bool has_device, const char *device,
2273 bool has_node_name, const char *node_name,
2274 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002275{
Benoît Canet12d3ba82014-01-23 21:31:35 +01002276 Error *local_err = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +02002277 BlockDriverState *bs;
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002278 AioContext *aio_context;
Markus Armbruster666daa62010-06-02 18:48:27 +02002279
Benoît Canet12d3ba82014-01-23 21:31:35 +01002280 bs = bdrv_lookup_bs(has_device ? device : NULL,
2281 has_node_name ? node_name : NULL,
2282 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002283 if (local_err) {
Benoît Canet12d3ba82014-01-23 21:31:35 +01002284 error_propagate(errp, local_err);
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02002285 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002286 }
2287
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002288 aio_context = bdrv_get_aio_context(bs);
2289 aio_context_acquire(aio_context);
2290
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002291 bdrv_add_key(bs, password, errp);
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002292
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002293 aio_context_release(aio_context);
Markus Armbruster666daa62010-06-02 18:48:27 +02002294}
2295
Colin Lordbf18bee2016-06-06 14:15:22 -04002296/*
2297 * Attempt to open the tray of @device.
2298 * If @force, ignore its tray lock.
2299 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2300 * On error, store an error through @errp and return -errno.
2301 * If @device does not exist, return -ENODEV.
2302 * If it has no removable media, return -ENOTSUP.
2303 * If it has no tray, return -ENOSYS.
2304 * If the guest was asked to open the tray, return -EINPROGRESS.
2305 * Else, return 0.
John Snow3a3086b2016-05-18 17:53:40 -04002306 */
2307static int do_open_tray(const char *device, bool force, Error **errp)
Max Reitz7d8a9f72015-10-26 21:39:08 +01002308{
2309 BlockBackend *blk;
2310 bool locked;
2311
Max Reitz7d8a9f72015-10-26 21:39:08 +01002312 blk = blk_by_name(device);
2313 if (!blk) {
2314 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2315 "Device '%s' not found", device);
John Snow3a3086b2016-05-18 17:53:40 -04002316 return -ENODEV;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002317 }
2318
2319 if (!blk_dev_has_removable_media(blk)) {
2320 error_setg(errp, "Device '%s' is not removable", device);
John Snow3a3086b2016-05-18 17:53:40 -04002321 return -ENOTSUP;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002322 }
2323
Max Reitz12c7ec82016-01-29 20:49:11 +01002324 if (!blk_dev_has_tray(blk)) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002325 error_setg(errp, "Device '%s' does not have a tray", device);
2326 return -ENOSYS;
Max Reitz12c7ec82016-01-29 20:49:11 +01002327 }
2328
Max Reitz7d8a9f72015-10-26 21:39:08 +01002329 if (blk_dev_is_tray_open(blk)) {
John Snow3a3086b2016-05-18 17:53:40 -04002330 return 0;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002331 }
2332
2333 locked = blk_dev_is_medium_locked(blk);
2334 if (locked) {
2335 blk_dev_eject_request(blk, force);
2336 }
2337
2338 if (!locked || force) {
2339 blk_dev_change_media_cb(blk, false);
2340 }
John Snow3a3086b2016-05-18 17:53:40 -04002341
2342 if (locked && !force) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002343 error_setg(errp, "Device '%s' is locked and force was not specified, "
2344 "wait for tray to open and try again", device);
2345 return -EINPROGRESS;
John Snow3a3086b2016-05-18 17:53:40 -04002346 }
2347
2348 return 0;
2349}
2350
2351void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
2352 Error **errp)
2353{
Colin Lordbf18bee2016-06-06 14:15:22 -04002354 Error *local_err = NULL;
2355 int rc;
2356
John Snow3a3086b2016-05-18 17:53:40 -04002357 if (!has_force) {
2358 force = false;
2359 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002360 rc = do_open_tray(device, force, &local_err);
2361 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2362 error_propagate(errp, local_err);
2363 return;
2364 }
2365 error_free(local_err);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002366}
2367
Max Reitzabaaf592015-10-26 21:39:09 +01002368void qmp_blockdev_close_tray(const char *device, Error **errp)
2369{
2370 BlockBackend *blk;
2371
2372 blk = blk_by_name(device);
2373 if (!blk) {
2374 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2375 "Device '%s' not found", device);
2376 return;
2377 }
2378
2379 if (!blk_dev_has_removable_media(blk)) {
2380 error_setg(errp, "Device '%s' is not removable", device);
2381 return;
2382 }
2383
Max Reitz12c7ec82016-01-29 20:49:11 +01002384 if (!blk_dev_has_tray(blk)) {
2385 /* Ignore this command on tray-less devices */
2386 return;
2387 }
2388
Max Reitzabaaf592015-10-26 21:39:09 +01002389 if (!blk_dev_is_tray_open(blk)) {
2390 return;
2391 }
2392
2393 blk_dev_change_media_cb(blk, true);
2394}
2395
Max Reitz6e0abc22015-12-11 16:23:05 +01002396void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
Max Reitz2814f672015-10-26 21:39:10 +01002397{
2398 BlockBackend *blk;
2399 BlockDriverState *bs;
2400 AioContext *aio_context;
2401 bool has_device;
2402
2403 blk = blk_by_name(device);
2404 if (!blk) {
2405 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2406 "Device '%s' not found", device);
2407 return;
2408 }
2409
2410 /* For BBs without a device, we can exchange the BDS tree at will */
2411 has_device = blk_get_attached_dev(blk);
2412
2413 if (has_device && !blk_dev_has_removable_media(blk)) {
2414 error_setg(errp, "Device '%s' is not removable", device);
2415 return;
2416 }
2417
Max Reitz12c7ec82016-01-29 20:49:11 +01002418 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Max Reitz2814f672015-10-26 21:39:10 +01002419 error_setg(errp, "Tray of device '%s' is not open", device);
2420 return;
2421 }
2422
2423 bs = blk_bs(blk);
2424 if (!bs) {
2425 return;
2426 }
2427
2428 aio_context = bdrv_get_aio_context(bs);
2429 aio_context_acquire(aio_context);
2430
2431 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2432 goto out;
2433 }
2434
Max Reitz2814f672015-10-26 21:39:10 +01002435 blk_remove_bs(blk);
2436
Max Reitz12c7ec82016-01-29 20:49:11 +01002437 if (!blk_dev_has_tray(blk)) {
2438 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2439 * called at all); therefore, the medium needs to be ejected here.
2440 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2441 * value passed here (i.e. false). */
2442 blk_dev_change_media_cb(blk, false);
2443 }
2444
Max Reitz2814f672015-10-26 21:39:10 +01002445out:
2446 aio_context_release(aio_context);
2447}
2448
Max Reitzd1299882015-10-26 21:39:11 +01002449static void qmp_blockdev_insert_anon_medium(const char *device,
2450 BlockDriverState *bs, Error **errp)
2451{
2452 BlockBackend *blk;
2453 bool has_device;
2454
2455 blk = blk_by_name(device);
2456 if (!blk) {
2457 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2458 "Device '%s' not found", device);
2459 return;
2460 }
2461
2462 /* For BBs without a device, we can exchange the BDS tree at will */
2463 has_device = blk_get_attached_dev(blk);
2464
2465 if (has_device && !blk_dev_has_removable_media(blk)) {
2466 error_setg(errp, "Device '%s' is not removable", device);
2467 return;
2468 }
2469
Max Reitz12c7ec82016-01-29 20:49:11 +01002470 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Max Reitzd1299882015-10-26 21:39:11 +01002471 error_setg(errp, "Tray of device '%s' is not open", device);
2472 return;
2473 }
2474
2475 if (blk_bs(blk)) {
2476 error_setg(errp, "There already is a medium in device '%s'", device);
2477 return;
2478 }
2479
2480 blk_insert_bs(blk, bs);
2481
Max Reitz12c7ec82016-01-29 20:49:11 +01002482 if (!blk_dev_has_tray(blk)) {
2483 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2484 * called at all); therefore, the medium needs to be pushed into the
2485 * slot here.
2486 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2487 * value passed here (i.e. true). */
2488 blk_dev_change_media_cb(blk, true);
2489 }
Max Reitzd1299882015-10-26 21:39:11 +01002490}
2491
Max Reitz6e0abc22015-12-11 16:23:05 +01002492void qmp_x_blockdev_insert_medium(const char *device, const char *node_name,
2493 Error **errp)
Max Reitzd1299882015-10-26 21:39:11 +01002494{
2495 BlockDriverState *bs;
2496
2497 bs = bdrv_find_node(node_name);
2498 if (!bs) {
2499 error_setg(errp, "Node '%s' not found", node_name);
2500 return;
2501 }
2502
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002503 if (bdrv_has_blk(bs)) {
Max Reitzd1299882015-10-26 21:39:11 +01002504 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002505 bdrv_get_parent_name(bs));
Max Reitzd1299882015-10-26 21:39:11 +01002506 return;
2507 }
2508
2509 qmp_blockdev_insert_anon_medium(device, bs, errp);
2510}
2511
Max Reitz24fb4132015-11-06 16:27:06 +01002512void qmp_blockdev_change_medium(const char *device, const char *filename,
2513 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002514 bool has_read_only,
2515 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002516 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002517{
2518 BlockBackend *blk;
2519 BlockDriverState *medium_bs = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002520 int bdrv_flags;
Colin Lord38a53d52016-06-08 13:56:28 -04002521 int rc;
Max Reitzde2c6c02015-10-26 21:39:13 +01002522 QDict *options = NULL;
2523 Error *err = NULL;
2524
2525 blk = blk_by_name(device);
2526 if (!blk) {
2527 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2528 "Device '%s' not found", device);
2529 goto fail;
2530 }
2531
2532 if (blk_bs(blk)) {
2533 blk_update_root_state(blk);
2534 }
2535
2536 bdrv_flags = blk_get_open_flags_from_root_state(blk);
Alyssa Milburn156abc22016-02-06 13:36:18 +00002537 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2538 BDRV_O_PROTOCOL);
Max Reitzde2c6c02015-10-26 21:39:13 +01002539
Max Reitz39ff43d2015-11-11 04:49:44 +01002540 if (!has_read_only) {
2541 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2542 }
2543
2544 switch (read_only) {
2545 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2546 break;
2547
2548 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2549 bdrv_flags &= ~BDRV_O_RDWR;
2550 break;
2551
2552 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2553 bdrv_flags |= BDRV_O_RDWR;
2554 break;
2555
2556 default:
2557 abort();
2558 }
2559
Max Reitz24fb4132015-11-06 16:27:06 +01002560 if (has_format) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002561 options = qdict_new();
2562 qdict_put(options, "driver", qstring_from_str(format));
2563 }
2564
Max Reitz5b363932016-05-17 16:41:31 +02002565 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2566 if (!medium_bs) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002567 goto fail;
2568 }
2569
Max Reitzde2c6c02015-10-26 21:39:13 +01002570 bdrv_add_key(medium_bs, NULL, &err);
2571 if (err) {
2572 error_propagate(errp, err);
2573 goto fail;
2574 }
2575
Colin Lord38a53d52016-06-08 13:56:28 -04002576 rc = do_open_tray(device, false, &err);
2577 if (rc && rc != -ENOSYS) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002578 error_propagate(errp, err);
2579 goto fail;
2580 }
Colin Lord38a53d52016-06-08 13:56:28 -04002581 error_free(err);
2582 err = NULL;
Max Reitzde2c6c02015-10-26 21:39:13 +01002583
Max Reitz6e0abc22015-12-11 16:23:05 +01002584 qmp_x_blockdev_remove_medium(device, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002585 if (err) {
2586 error_propagate(errp, err);
2587 goto fail;
2588 }
2589
2590 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2591 if (err) {
2592 error_propagate(errp, err);
2593 goto fail;
2594 }
2595
Kevin Wolfa5614992016-03-21 10:49:51 +01002596 blk_apply_root_state(blk, medium_bs);
2597
Max Reitzde2c6c02015-10-26 21:39:13 +01002598 qmp_blockdev_close_tray(device, errp);
2599
2600fail:
2601 /* If the medium has been inserted, the device has its own reference, so
2602 * ours must be relinquished; and if it has not been inserted successfully,
2603 * the reference must be relinquished anyway */
2604 bdrv_unref(medium_bs);
2605}
2606
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002607/* throttling disk I/O limits */
Eric Blake4dc93972016-07-13 21:50:21 -06002608void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002609{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002610 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002611 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002612 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002613 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002614
Eric Blake4dc93972016-07-13 21:50:21 -06002615 blk = blk_by_name(arg->device);
Fam Zhenga0e85442015-03-02 19:36:48 +08002616 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01002617 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Eric Blake4dc93972016-07-13 21:50:21 -06002618 "Device '%s' not found", arg->device);
Luiz Capitulino80047da2011-12-14 16:49:14 -02002619 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002620 }
Max Reitz5433c242015-10-19 17:53:29 +02002621
2622 aio_context = blk_get_aio_context(blk);
2623 aio_context_acquire(aio_context);
2624
Fam Zhenga0e85442015-03-02 19:36:48 +08002625 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002626 if (!bs) {
Eric Blake4dc93972016-07-13 21:50:21 -06002627 error_setg(errp, "Device '%s' has no medium", arg->device);
Max Reitz5433c242015-10-19 17:53:29 +02002628 goto out;
2629 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002630
Alberto Garcia1588ab52016-02-18 12:27:00 +02002631 throttle_config_init(&cfg);
Eric Blake4dc93972016-07-13 21:50:21 -06002632 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2633 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2634 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002635
Eric Blake4dc93972016-07-13 21:50:21 -06002636 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2637 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2638 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
Benoît Canetcc0681c2013-09-02 14:14:39 +02002639
Eric Blake4dc93972016-07-13 21:50:21 -06002640 if (arg->has_bps_max) {
2641 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002642 }
Eric Blake4dc93972016-07-13 21:50:21 -06002643 if (arg->has_bps_rd_max) {
2644 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002645 }
Eric Blake4dc93972016-07-13 21:50:21 -06002646 if (arg->has_bps_wr_max) {
2647 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002648 }
Eric Blake4dc93972016-07-13 21:50:21 -06002649 if (arg->has_iops_max) {
2650 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002651 }
Eric Blake4dc93972016-07-13 21:50:21 -06002652 if (arg->has_iops_rd_max) {
2653 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002654 }
Eric Blake4dc93972016-07-13 21:50:21 -06002655 if (arg->has_iops_wr_max) {
2656 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002657 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002658
Eric Blake4dc93972016-07-13 21:50:21 -06002659 if (arg->has_bps_max_length) {
2660 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002661 }
Eric Blake4dc93972016-07-13 21:50:21 -06002662 if (arg->has_bps_rd_max_length) {
2663 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002664 }
Eric Blake4dc93972016-07-13 21:50:21 -06002665 if (arg->has_bps_wr_max_length) {
2666 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002667 }
Eric Blake4dc93972016-07-13 21:50:21 -06002668 if (arg->has_iops_max_length) {
2669 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002670 }
Eric Blake4dc93972016-07-13 21:50:21 -06002671 if (arg->has_iops_rd_max_length) {
2672 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002673 }
Eric Blake4dc93972016-07-13 21:50:21 -06002674 if (arg->has_iops_wr_max_length) {
2675 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002676 }
2677
Eric Blake4dc93972016-07-13 21:50:21 -06002678 if (arg->has_iops_size) {
2679 cfg.op_size = arg->iops_size;
Benoît Canet2024c1d2013-09-02 14:14:41 +02002680 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002681
Alberto Garciad5851082016-02-18 12:26:59 +02002682 if (!throttle_is_valid(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002683 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002684 }
2685
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002686 if (throttle_enabled(&cfg)) {
2687 /* Enable I/O limits if they're not enabled yet, otherwise
2688 * just update the throttling group. */
Kevin Wolf97148072016-03-21 13:53:52 +01002689 if (!blk_get_public(blk)->throttle_state) {
Eric Blake4dc93972016-07-13 21:50:21 -06002690 blk_io_limits_enable(blk,
2691 arg->has_group ? arg->group : arg->device);
2692 } else if (arg->has_group) {
2693 blk_io_limits_update_group(blk, arg->group);
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002694 }
2695 /* Set the new throttling configuration */
Kevin Wolf97148072016-03-21 13:53:52 +01002696 blk_set_io_limits(blk, &cfg);
2697 } else if (blk_get_public(blk)->throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002698 /* If all throttling settings are set to 0, disable I/O limits */
Kevin Wolf97148072016-03-21 13:53:52 +01002699 blk_io_limits_disable(blk);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002700 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002701
Max Reitz5433c242015-10-19 17:53:29 +02002702out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002703 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002704}
2705
John Snow341ebc22015-04-17 19:49:52 -04002706void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2707 bool has_granularity, uint32_t granularity,
2708 Error **errp)
2709{
2710 AioContext *aio_context;
2711 BlockDriverState *bs;
2712
2713 if (!name || name[0] == '\0') {
2714 error_setg(errp, "Bitmap name cannot be empty");
2715 return;
2716 }
2717
2718 bs = bdrv_lookup_bs(node, node, errp);
2719 if (!bs) {
2720 return;
2721 }
2722
2723 aio_context = bdrv_get_aio_context(bs);
2724 aio_context_acquire(aio_context);
2725
2726 if (has_granularity) {
2727 if (granularity < 512 || !is_power_of_2(granularity)) {
2728 error_setg(errp, "Granularity must be power of 2 "
2729 "and at least 512");
2730 goto out;
2731 }
2732 } else {
2733 /* Default to cluster size, if available: */
2734 granularity = bdrv_get_default_bitmap_granularity(bs);
2735 }
2736
2737 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2738
2739 out:
2740 aio_context_release(aio_context);
2741}
2742
2743void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2744 Error **errp)
2745{
2746 AioContext *aio_context;
2747 BlockDriverState *bs;
2748 BdrvDirtyBitmap *bitmap;
2749
2750 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2751 if (!bitmap || !bs) {
2752 return;
2753 }
2754
John Snow9bd2b082015-04-17 19:49:57 -04002755 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2756 error_setg(errp,
2757 "Bitmap '%s' is currently frozen and cannot be removed",
2758 name);
2759 goto out;
2760 }
John Snow20dca812015-04-17 19:50:02 -04002761 bdrv_dirty_bitmap_make_anon(bitmap);
John Snow341ebc22015-04-17 19:49:52 -04002762 bdrv_release_dirty_bitmap(bs, bitmap);
2763
John Snow9bd2b082015-04-17 19:49:57 -04002764 out:
John Snow341ebc22015-04-17 19:49:52 -04002765 aio_context_release(aio_context);
2766}
2767
John Snowe74e6b72015-04-17 19:49:59 -04002768/**
2769 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2770 * immediately after a full backup operation.
2771 */
2772void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2773 Error **errp)
2774{
2775 AioContext *aio_context;
2776 BdrvDirtyBitmap *bitmap;
2777 BlockDriverState *bs;
2778
2779 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2780 if (!bitmap || !bs) {
2781 return;
2782 }
2783
2784 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2785 error_setg(errp,
2786 "Bitmap '%s' is currently frozen and cannot be modified",
2787 name);
2788 goto out;
2789 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2790 error_setg(errp,
2791 "Bitmap '%s' is currently disabled and cannot be cleared",
2792 name);
2793 goto out;
2794 }
2795
Fam Zhengdf9a6812015-11-09 18:16:54 +08002796 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04002797
2798 out:
2799 aio_context_release(aio_context);
2800}
2801
Markus Armbruster072ebe62015-03-05 17:00:56 +01002802void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06002803{
2804 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002805 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06002806 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002807 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08002808 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06002809
Kevin Wolf2073d412016-02-23 17:50:37 +01002810 bs = bdrv_find_node(id);
2811 if (bs) {
2812 qmp_x_blockdev_del(false, NULL, true, id, &local_err);
2813 if (local_err) {
2814 error_report_err(local_err);
2815 }
2816 return;
2817 }
2818
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002819 blk = blk_by_name(id);
2820 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02002821 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01002822 return;
Ryan Harper9063f812010-11-12 11:07:13 -06002823 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002824
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02002825 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02002826 error_report("Deleting device added with blockdev-add"
2827 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01002828 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02002829 }
2830
Max Reitz5433c242015-10-19 17:53:29 +02002831 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002832 aio_context_acquire(aio_context);
2833
Max Reitz5433c242015-10-19 17:53:29 +02002834 bs = blk_bs(blk);
2835 if (bs) {
2836 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2837 error_report_err(local_err);
2838 aio_context_release(aio_context);
2839 return;
2840 }
Ryan Harper9063f812010-11-12 11:07:13 -06002841
Max Reitz938abd42016-01-29 16:36:09 +01002842 blk_remove_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002843 }
Ryan Harper9063f812010-11-12 11:07:13 -06002844
Max Reitz7c735872016-03-16 19:54:39 +01002845 /* Make the BlockBackend and the attached BlockDriverState anonymous */
Max Reitzefaa7c42016-03-16 19:54:38 +01002846 monitor_remove_blk(blk);
2847
Max Reitz7c735872016-03-16 19:54:39 +01002848 /* If this BlockBackend has a device attached to it, its refcount will be
2849 * decremented when the device is removed; otherwise we have to do so here.
Ryan Harperd22b2f42011-03-29 20:51:47 -05002850 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02002851 if (blk_get_attached_dev(blk)) {
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02002852 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02002853 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2854 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05002855 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02002856 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06002857 }
2858
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002859 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06002860}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002861
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002862void qmp_block_resize(bool has_device, const char *device,
2863 bool has_node_name, const char *node_name,
2864 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002865{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002866 Error *local_err = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002867 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002868 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02002869 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002870
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002871 bs = bdrv_lookup_bs(has_device ? device : NULL,
2872 has_node_name ? node_name : NULL,
2873 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002874 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002875 error_propagate(errp, local_err);
2876 return;
2877 }
2878
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002879 aio_context = bdrv_get_aio_context(bs);
2880 aio_context_acquire(aio_context);
2881
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002882 if (!bdrv_is_first_non_filter(bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002883 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002884 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002885 }
2886
2887 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002888 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002889 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002890 }
2891
Jeff Cody9c75e162014-06-25 16:55:30 -04002892 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002893 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002894 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04002895 }
2896
Peter Lieven92b7a082013-03-11 11:04:24 +01002897 /* complete all in-flight operations before resizing the device */
2898 bdrv_drain_all();
2899
Kevin Wolf87329012013-05-02 15:32:55 +02002900 ret = bdrv_truncate(bs, size);
2901 switch (ret) {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002902 case 0:
2903 break;
2904 case -ENOMEDIUM:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002905 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002906 break;
2907 case -ENOTSUP:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002908 error_setg(errp, QERR_UNSUPPORTED);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002909 break;
2910 case -EACCES:
Alberto Garcia81e5f782015-04-08 12:29:19 +03002911 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002912 break;
2913 case -EBUSY:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002914 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002915 break;
2916 default:
Kevin Wolf87329012013-05-02 15:32:55 +02002917 error_setg_errno(errp, -ret, "Could not resize");
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002918 break;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002919 }
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002920
2921out:
2922 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002923}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002924
Jeff Cody9abf2db2012-09-27 13:29:14 -04002925static void block_job_cb(void *opaque, int ret)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002926{
Stefan Hajnoczi723c5d92014-10-21 12:03:53 +01002927 /* Note that this function may be executed from another AioContext besides
2928 * the QEMU main loop. If you need to access anything that assumes the
2929 * QEMU global mutex, use a BH or introduce a mutex.
2930 */
2931
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002932 BlockDriverState *bs = opaque;
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002933 const char *msg = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002934
Jeff Cody9abf2db2012-09-27 13:29:14 -04002935 trace_block_job_cb(bs, bs->job, ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002936
2937 assert(bs->job);
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002938
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002939 if (ret < 0) {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002940 msg = strerror(-ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002941 }
2942
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002943 if (block_job_is_cancelled(bs->job)) {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002944 block_job_event_cancelled(bs->job);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002945 } else {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002946 block_job_event_completed(bs->job, msg);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002947 }
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002948}
2949
Alberto Garcia23233222016-07-05 17:28:59 +03002950void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002951 bool has_base, const char *base,
2952 bool has_backing_file, const char *backing_file,
2953 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002954 bool has_on_error, BlockdevOnError on_error,
2955 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002956{
2957 BlockDriverState *bs;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002958 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002959 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002960 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002961 const char *base_name = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002962
Paolo Bonzini1d809092012-09-28 17:22:59 +02002963 if (!has_on_error) {
2964 on_error = BLOCKDEV_ON_ERROR_REPORT;
2965 }
2966
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002967 bs = qmp_get_root_bs(device, errp);
2968 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002969 return;
2970 }
2971
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002972 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002973 aio_context_acquire(aio_context);
2974
Fam Zheng628ff682014-05-23 21:29:44 +08002975 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002976 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08002977 }
2978
Jeff Cody13d8cc52014-06-25 15:40:11 -04002979 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002980 base_bs = bdrv_find_backing_image(bs, base);
2981 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002982 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002983 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002984 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002985 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04002986 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002987 }
2988
Jeff Cody13d8cc52014-06-25 15:40:11 -04002989 /* if we are streaming the entire chain, the result will have no backing
2990 * file, and specifying one is therefore an error */
2991 if (base_bs == NULL && has_backing_file) {
2992 error_setg(errp, "backing file specified, but streaming the "
2993 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002994 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002995 }
2996
2997 /* backing_file string overrides base bs filename */
2998 base_name = has_backing_file ? backing_file : base_name;
2999
Alberto Garcia23233222016-07-05 17:28:59 +03003000 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
3001 has_speed ? speed : 0, on_error, block_job_cb, bs, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003002 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003003 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003004 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003005 }
3006
3007 trace_qmp_block_stream(bs, bs->job);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003008
3009out:
3010 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003011}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003012
Alberto Garciafd62c602016-07-05 17:29:00 +03003013void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003014 bool has_base, const char *base,
3015 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003016 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04003017 bool has_speed, int64_t speed,
3018 Error **errp)
3019{
3020 BlockDriverState *bs;
3021 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003022 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04003023 Error *local_err = NULL;
3024 /* This will be part of the QMP command, if/when the
3025 * BlockdevOnError change for blkmirror makes it in
3026 */
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02003027 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
Jeff Codyed61fc12012-09-27 13:29:16 -04003028
Max Reitz54504662014-04-10 19:36:25 +02003029 if (!has_speed) {
3030 speed = 0;
3031 }
3032
Jeff Cody7676e2c2014-06-30 15:14:15 +02003033 /* Important Note:
3034 * libvirt relies on the DeviceNotFound error class in order to probe for
3035 * live commit feature versions; for this to work, we must make sure to
3036 * perform the device lookup before any generic errors that may occur in a
3037 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02003038 bs = qmp_get_root_bs(device, &local_err);
3039 if (!bs) {
3040 bs = bdrv_lookup_bs(device, device, NULL);
3041 if (!bs) {
3042 error_free(local_err);
3043 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3044 "Device '%s' not found", device);
3045 } else {
3046 error_propagate(errp, local_err);
3047 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003048 return;
3049 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003050
Kevin Wolf1d13b162016-06-23 14:20:24 +02003051 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003052 aio_context_acquire(aio_context);
3053
Fam Zhengbb000212014-09-11 13:14:00 +08003054 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003055 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003056 }
3057
Jeff Codyed61fc12012-09-27 13:29:16 -04003058 /* default top_bs is the active layer */
3059 top_bs = bs;
3060
Jeff Cody7676e2c2014-06-30 15:14:15 +02003061 if (has_top && top) {
Jeff Codyed61fc12012-09-27 13:29:16 -04003062 if (strcmp(bs->filename, top) != 0) {
3063 top_bs = bdrv_find_backing_image(bs, top);
3064 }
3065 }
3066
3067 if (top_bs == NULL) {
3068 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003069 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003070 }
3071
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003072 assert(bdrv_get_aio_context(top_bs) == aio_context);
3073
Jeff Codyd5208c42012-10-16 15:49:10 -04003074 if (has_base && base) {
3075 base_bs = bdrv_find_backing_image(top_bs, base);
3076 } else {
3077 base_bs = bdrv_find_base(top_bs);
3078 }
3079
3080 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003081 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003082 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04003083 }
3084
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003085 assert(bdrv_get_aio_context(base_bs) == aio_context);
3086
Fam Zhengbb000212014-09-11 13:14:00 +08003087 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3088 goto out;
3089 }
3090
Jeff Cody7676e2c2014-06-30 15:14:15 +02003091 /* Do not allow attempts to commit an image into itself */
3092 if (top_bs == base_bs) {
3093 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003094 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02003095 }
3096
Fam Zheng20a63d22013-12-16 14:45:31 +08003097 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04003098 if (has_backing_file) {
3099 error_setg(errp, "'backing-file' specified,"
3100 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003101 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003102 }
Alberto Garciafd62c602016-07-05 17:29:00 +03003103 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, speed,
3104 on_error, block_job_cb, bs, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003105 } else {
Alberto Garciafd62c602016-07-05 17:29:00 +03003106 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed,
3107 on_error, block_job_cb, bs,
Jeff Cody54e26902014-06-25 15:40:10 -04003108 has_backing_file ? backing_file : NULL, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003109 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003110 if (local_err != NULL) {
3111 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003112 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003113 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003114
3115out:
3116 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003117}
3118
Pavel Butsykin81206a82016-07-22 11:17:50 +03003119static void do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003120{
3121 BlockDriverState *bs;
3122 BlockDriverState *target_bs;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003123 BlockDriverState *source = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003124 BdrvDirtyBitmap *bmap = NULL;
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003125 AioContext *aio_context;
Max Reitze6641712015-08-26 19:47:48 +02003126 QDict *options = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003127 Error *local_err = NULL;
3128 int flags;
3129 int64_t size;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003130
Pavel Butsykin81206a82016-07-22 11:17:50 +03003131 if (!backup->has_speed) {
3132 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003133 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003134 if (!backup->has_on_source_error) {
3135 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003136 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003137 if (!backup->has_on_target_error) {
3138 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003139 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003140 if (!backup->has_mode) {
3141 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3142 }
3143 if (!backup->has_job_id) {
3144 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003145 }
3146
Pavel Butsykin81206a82016-07-22 11:17:50 +03003147 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003148 if (!bs) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003149 return;
3150 }
3151
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003152 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003153 aio_context_acquire(aio_context);
3154
Pavel Butsykin81206a82016-07-22 11:17:50 +03003155 if (!backup->has_format) {
3156 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
3157 NULL : (char*) bs->drv->format_name;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003158 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003159
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003160 /* Early check to avoid creating target */
Fam Zheng3718d8a2014-05-23 21:29:43 +08003161 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003162 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003163 }
3164
Kevin Wolf61de4c62016-03-18 17:46:45 +01003165 flags = bs->open_flags | BDRV_O_RDWR;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003166
Ian Mainfc5d3f82013-07-26 11:39:04 -07003167 /* See if we have a backing HD we can use to create our new image
3168 * on top of. */
Pavel Butsykin81206a82016-07-22 11:17:50 +03003169 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003170 source = backing_bs(bs);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003171 if (!source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003172 backup->sync = MIRROR_SYNC_MODE_FULL;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003173 }
3174 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003175 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
Ian Mainfc5d3f82013-07-26 11:39:04 -07003176 source = bs;
3177 }
3178
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003179 size = bdrv_getlength(bs);
3180 if (size < 0) {
3181 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003182 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003183 }
3184
Pavel Butsykin81206a82016-07-22 11:17:50 +03003185 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
3186 assert(backup->format);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003187 if (source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003188 bdrv_img_create(backup->target, backup->format, source->filename,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003189 source->drv->format_name, NULL,
3190 size, flags, &local_err, false);
3191 } else {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003192 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003193 size, flags, &local_err, false);
3194 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003195 }
3196
Markus Armbruster84d18f02014-01-30 15:07:28 +01003197 if (local_err) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003198 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003199 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003200 }
3201
Pavel Butsykin81206a82016-07-22 11:17:50 +03003202 if (backup->format) {
Max Reitze6641712015-08-26 19:47:48 +02003203 options = qdict_new();
Pavel Butsykin81206a82016-07-22 11:17:50 +03003204 qdict_put(options, "driver", qstring_from_str(backup->format));
Max Reitze6641712015-08-26 19:47:48 +02003205 }
3206
Pavel Butsykin81206a82016-07-22 11:17:50 +03003207 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003208 if (!target_bs) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003209 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003210 }
3211
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003212 bdrv_set_aio_context(target_bs, aio_context);
3213
Pavel Butsykin81206a82016-07-22 11:17:50 +03003214 if (backup->has_bitmap) {
3215 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
John Snowd58d8452015-04-17 19:49:58 -04003216 if (!bmap) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003217 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
Max Reitz0702d3d2015-11-09 23:39:10 +01003218 bdrv_unref(target_bs);
John Snowd58d8452015-04-17 19:49:58 -04003219 goto out;
3220 }
3221 }
3222
Pavel Butsykin81206a82016-07-22 11:17:50 +03003223 backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync,
3224 bmap, backup->on_source_error, backup->on_target_error,
John Snow78f51fd2015-11-05 18:13:17 -05003225 block_job_cb, bs, txn, &local_err);
Kevin Wolf5c438bc2016-04-14 13:09:53 +02003226 bdrv_unref(target_bs);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003227 if (local_err != NULL) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003228 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003229 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003230 }
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003231
3232out:
3233 aio_context_release(aio_context);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003234}
3235
Pavel Butsykin81206a82016-07-22 11:17:50 +03003236void qmp_drive_backup(DriveBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003237{
Pavel Butsykin81206a82016-07-22 11:17:50 +03003238 return do_drive_backup(arg, NULL, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003239}
3240
Benoît Canetc13163f2014-01-23 21:31:34 +01003241BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3242{
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003243 return bdrv_named_nodes_list(errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003244}
3245
Alberto Garcia70559d42016-07-05 17:28:58 +03003246void do_blockdev_backup(const char *job_id, const char *device,
3247 const char *target, enum MirrorSyncMode sync,
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003248 bool has_speed, int64_t speed,
3249 bool has_on_source_error,
3250 BlockdevOnError on_source_error,
3251 bool has_on_target_error,
3252 BlockdevOnError on_target_error,
John Snow78f51fd2015-11-05 18:13:17 -05003253 BlockJobTxn *txn, Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003254{
3255 BlockDriverState *bs;
3256 BlockDriverState *target_bs;
3257 Error *local_err = NULL;
3258 AioContext *aio_context;
3259
3260 if (!has_speed) {
3261 speed = 0;
3262 }
3263 if (!has_on_source_error) {
3264 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3265 }
3266 if (!has_on_target_error) {
3267 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3268 }
3269
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003270 bs = qmp_get_root_bs(device, errp);
3271 if (!bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003272 return;
3273 }
3274
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003275 aio_context = bdrv_get_aio_context(bs);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003276 aio_context_acquire(aio_context);
3277
Fam Zheng0d978912016-05-23 10:19:35 +08003278 target_bs = bdrv_lookup_bs(target, target, errp);
3279 if (!target_bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003280 goto out;
3281 }
Max Reitz5433c242015-10-19 17:53:29 +02003282
Fam Zhengefd75562016-05-23 10:19:36 +08003283 if (bdrv_get_aio_context(target_bs) != aio_context) {
3284 if (!bdrv_has_blk(target_bs)) {
3285 /* The target BDS is not attached, we can safely move it to another
3286 * AioContext. */
3287 bdrv_set_aio_context(target_bs, aio_context);
3288 } else {
3289 error_setg(errp, "Target is attached to a different thread from "
3290 "source.");
3291 goto out;
3292 }
3293 }
Alberto Garcia70559d42016-07-05 17:28:58 +03003294 backup_start(job_id, bs, target_bs, speed, sync, NULL, on_source_error,
John Snow78f51fd2015-11-05 18:13:17 -05003295 on_target_error, block_job_cb, bs, txn, &local_err);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003296 if (local_err != NULL) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003297 error_propagate(errp, local_err);
3298 }
3299out:
3300 aio_context_release(aio_context);
3301}
3302
Alberto Garcia70559d42016-07-05 17:28:58 +03003303void qmp_blockdev_backup(bool has_job_id, const char *job_id,
3304 const char *device, const char *target,
John Snow78f51fd2015-11-05 18:13:17 -05003305 enum MirrorSyncMode sync,
3306 bool has_speed, int64_t speed,
3307 bool has_on_source_error,
3308 BlockdevOnError on_source_error,
3309 bool has_on_target_error,
3310 BlockdevOnError on_target_error,
3311 Error **errp)
3312{
Alberto Garcia70559d42016-07-05 17:28:58 +03003313 do_blockdev_backup(has_job_id ? job_id : NULL, device, target,
3314 sync, has_speed, speed,
John Snow78f51fd2015-11-05 18:13:17 -05003315 has_on_source_error, on_source_error,
3316 has_on_target_error, on_target_error,
3317 NULL, errp);
3318}
3319
Fam Zheng4193cdd2015-12-24 12:45:03 +08003320/* Parameter check and block job starting for drive mirroring.
3321 * Caller should hold @device and @target's aio context (must be the same).
3322 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003323static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003324 BlockDriverState *target,
3325 bool has_replaces, const char *replaces,
3326 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003327 BlockMirrorBackingMode backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003328 bool has_speed, int64_t speed,
3329 bool has_granularity, uint32_t granularity,
3330 bool has_buf_size, int64_t buf_size,
3331 bool has_on_source_error,
3332 BlockdevOnError on_source_error,
3333 bool has_on_target_error,
3334 BlockdevOnError on_target_error,
3335 bool has_unmap, bool unmap,
3336 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003337{
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003338
3339 if (!has_speed) {
3340 speed = 0;
3341 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003342 if (!has_on_source_error) {
3343 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3344 }
3345 if (!has_on_target_error) {
3346 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3347 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003348 if (!has_granularity) {
3349 granularity = 0;
3350 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003351 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003352 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003353 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003354 if (!has_unmap) {
3355 unmap = true;
3356 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003357
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003358 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003359 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3360 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003361 return;
3362 }
3363 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003364 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3365 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003366 return;
3367 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003368
Fam Zheng4193cdd2015-12-24 12:45:03 +08003369 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3370 return;
3371 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003372 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3373 return;
3374 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003375
3376 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3377 sync = MIRROR_SYNC_MODE_FULL;
3378 }
3379
3380 /* pass the node name to replace to mirror start since it's loose coupling
3381 * and will allow to check whether the node still exist at mirror completion
3382 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003383 mirror_start(job_id, bs, target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003384 has_replaces ? replaces : NULL,
Max Reitz274fcce2016-06-10 20:57:47 +02003385 speed, granularity, buf_size, sync, backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003386 on_source_error, on_target_error, unmap,
3387 block_job_cb, bs, errp);
3388}
3389
Eric Blakefaecd402016-07-14 16:37:58 -06003390void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003391{
3392 BlockDriverState *bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003393 BlockDriverState *source, *target_bs;
3394 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003395 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003396 Error *local_err = NULL;
3397 QDict *options = NULL;
3398 int flags;
3399 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003400 const char *format = arg->format;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003401
Kevin Wolf0524e932016-06-23 14:20:24 +02003402 bs = qmp_get_root_bs(arg->device, errp);
3403 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003404 return;
3405 }
3406
Kevin Wolf0524e932016-06-23 14:20:24 +02003407 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003408 aio_context_acquire(aio_context);
3409
Eric Blakefaecd402016-07-14 16:37:58 -06003410 if (!arg->has_mode) {
3411 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003412 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003413
Eric Blakefaecd402016-07-14 16:37:58 -06003414 if (!arg->has_format) {
3415 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3416 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003417 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003418
Kevin Wolf61de4c62016-03-18 17:46:45 +01003419 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003420 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003421 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3422 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003423 }
Eric Blakefaecd402016-07-14 16:37:58 -06003424 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003425 source = bs;
3426 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003427
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003428 size = bdrv_getlength(bs);
3429 if (size < 0) {
3430 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003431 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003432 }
3433
Eric Blakefaecd402016-07-14 16:37:58 -06003434 if (arg->has_replaces) {
Benoît Canet09158f02014-06-27 18:25:25 +02003435 BlockDriverState *to_replace_bs;
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003436 AioContext *replace_aio_context;
3437 int64_t replace_size;
Benoît Canet09158f02014-06-27 18:25:25 +02003438
Eric Blakefaecd402016-07-14 16:37:58 -06003439 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003440 error_setg(errp, "a node-name must be provided when replacing a"
3441 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003442 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003443 }
3444
Eric Blakefaecd402016-07-14 16:37:58 -06003445 to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err);
Benoît Canet09158f02014-06-27 18:25:25 +02003446
3447 if (!to_replace_bs) {
3448 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003449 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003450 }
3451
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003452 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3453 aio_context_acquire(replace_aio_context);
3454 replace_size = bdrv_getlength(to_replace_bs);
3455 aio_context_release(replace_aio_context);
3456
3457 if (size != replace_size) {
Benoît Canet09158f02014-06-27 18:25:25 +02003458 error_setg(errp, "cannot replace image with a mirror image of "
3459 "different size");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003460 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003461 }
3462 }
3463
Eric Blakefaecd402016-07-14 16:37:58 -06003464 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003465 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3466 } else {
3467 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3468 }
3469
Eric Blakefaecd402016-07-14 16:37:58 -06003470 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3471 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003472 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003473 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003474 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003475 bdrv_img_create(arg->target, format,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003476 NULL, NULL, NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003477 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003478 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003479 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003480 break;
3481 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3482 /* create new image with backing file */
Eric Blakefaecd402016-07-14 16:37:58 -06003483 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003484 source->filename,
3485 source->drv->format_name,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003486 NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003487 break;
3488 default:
3489 abort();
3490 }
3491 }
3492
Markus Armbruster84d18f02014-01-30 15:07:28 +01003493 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003494 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003495 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003496 }
3497
Max Reitze6641712015-08-26 19:47:48 +02003498 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003499 if (arg->has_node_name) {
3500 qdict_put(options, "node-name", qstring_from_str(arg->node_name));
Benoît Canet4c828dc2014-06-16 12:00:55 +02003501 }
Max Reitze6641712015-08-26 19:47:48 +02003502 if (format) {
3503 qdict_put(options, "driver", qstring_from_str(format));
3504 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003505
Paolo Bonzinib812f672013-01-21 17:09:43 +01003506 /* Mirroring takes care of copy-on-write using the source's backing
3507 * file.
3508 */
Eric Blakefaecd402016-07-14 16:37:58 -06003509 target_bs = bdrv_open(arg->target, NULL, options,
3510 flags | BDRV_O_NO_BACKING, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003511 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003512 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003513 }
3514
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003515 bdrv_set_aio_context(target_bs, aio_context);
3516
Eric Blakefaecd402016-07-14 16:37:58 -06003517 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3518 arg->has_replaces, arg->replaces, arg->sync,
3519 backing_mode, arg->has_speed, arg->speed,
3520 arg->has_granularity, arg->granularity,
3521 arg->has_buf_size, arg->buf_size,
3522 arg->has_on_source_error, arg->on_source_error,
3523 arg->has_on_target_error, arg->on_target_error,
3524 arg->has_unmap, arg->unmap,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003525 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02003526 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003527 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003528out:
3529 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003530}
3531
Alberto Garcia71aa9862016-07-05 17:28:57 +03003532void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3533 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08003534 bool has_replaces, const char *replaces,
3535 MirrorSyncMode sync,
3536 bool has_speed, int64_t speed,
3537 bool has_granularity, uint32_t granularity,
3538 bool has_buf_size, int64_t buf_size,
3539 bool has_on_source_error,
3540 BlockdevOnError on_source_error,
3541 bool has_on_target_error,
3542 BlockdevOnError on_target_error,
3543 Error **errp)
3544{
3545 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08003546 BlockDriverState *target_bs;
3547 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003548 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08003549 Error *local_err = NULL;
3550
Kevin Wolf07eec652016-06-23 14:20:24 +02003551 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08003552 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08003553 return;
3554 }
3555
3556 target_bs = bdrv_lookup_bs(target, target, errp);
3557 if (!target_bs) {
3558 return;
3559 }
3560
3561 aio_context = bdrv_get_aio_context(bs);
3562 aio_context_acquire(aio_context);
3563
Fam Zhengdf925622015-12-24 12:45:05 +08003564 bdrv_set_aio_context(target_bs, aio_context);
3565
Alberto Garcia71aa9862016-07-05 17:28:57 +03003566 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02003567 has_replaces, replaces, sync, backing_mode,
Fam Zhengdf925622015-12-24 12:45:05 +08003568 has_speed, speed,
3569 has_granularity, granularity,
3570 has_buf_size, buf_size,
3571 has_on_source_error, on_source_error,
3572 has_on_target_error, on_target_error,
3573 true, true,
3574 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003575 error_propagate(errp, local_err);
Fam Zhengdf925622015-12-24 12:45:05 +08003576
3577 aio_context_release(aio_context);
3578}
3579
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003580/* Get a block job using its ID and acquire its AioContext */
3581static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003582 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003583{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003584 BlockJob *job;
3585
3586 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003587
Max Reitz5433c242015-10-19 17:53:29 +02003588 *aio_context = NULL;
3589
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003590 job = block_job_get(id);
3591
3592 if (!job) {
3593 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3594 "Block job '%s' not found", id);
3595 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003596 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003597
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003598 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003599 aio_context_acquire(*aio_context);
3600
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003601 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003602}
3603
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003604void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003605{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003606 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003607 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003608
3609 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003610 return;
3611 }
3612
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003613 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003614 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003615}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003616
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003617void qmp_block_job_cancel(const char *device,
3618 bool has_force, bool force, Error **errp)
3619{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003620 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003621 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003622
3623 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003624 return;
3625 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003626
3627 if (!has_force) {
3628 force = false;
3629 }
3630
Fam Zheng751ebd72015-04-03 22:05:18 +08003631 if (job->user_paused && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003632 error_setg(errp, "The block job for device '%s' is currently paused",
3633 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003634 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003635 }
3636
3637 trace_qmp_block_job_cancel(job);
3638 block_job_cancel(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003639out:
3640 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003641}
3642
3643void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003644{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003645 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003646 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003647
Fam Zheng751ebd72015-04-03 22:05:18 +08003648 if (!job || job->user_paused) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003649 return;
3650 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003651
Fam Zheng751ebd72015-04-03 22:05:18 +08003652 job->user_paused = true;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003653 trace_qmp_block_job_pause(job);
3654 block_job_pause(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003655 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003656}
3657
3658void qmp_block_job_resume(const char *device, Error **errp)
3659{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003660 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003661 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003662
Fam Zheng751ebd72015-04-03 22:05:18 +08003663 if (!job || !job->user_paused) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02003664 return;
3665 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003666
Fam Zheng751ebd72015-04-03 22:05:18 +08003667 job->user_paused = false;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003668 trace_qmp_block_job_resume(job);
Stefan Hajnoczi17bd51f2016-06-16 17:56:22 +01003669 block_job_iostatus_reset(job);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003670 block_job_resume(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003671 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003672}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003673
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003674void qmp_block_job_complete(const char *device, Error **errp)
3675{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003676 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003677 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003678
3679 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003680 return;
3681 }
3682
3683 trace_qmp_block_job_complete(job);
3684 block_job_complete(job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003685 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003686}
3687
Jeff Codyfa40e652014-07-01 09:52:16 +02003688void qmp_change_backing_file(const char *device,
3689 const char *image_node_name,
3690 const char *backing_file,
3691 Error **errp)
3692{
3693 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003694 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02003695 BlockDriverState *image_bs = NULL;
3696 Error *local_err = NULL;
3697 bool ro;
3698 int open_flags;
3699 int ret;
3700
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003701 bs = qmp_get_root_bs(device, errp);
3702 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02003703 return;
3704 }
3705
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003706 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003707 aio_context_acquire(aio_context);
3708
Jeff Codyfa40e652014-07-01 09:52:16 +02003709 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3710 if (local_err) {
3711 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003712 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003713 }
3714
3715 if (!image_bs) {
3716 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003717 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003718 }
3719
3720 if (bdrv_find_base(image_bs) == image_bs) {
3721 error_setg(errp, "not allowing backing file change on an image "
3722 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003723 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003724 }
3725
3726 /* even though we are not necessarily operating on bs, we need it to
3727 * determine if block ops are currently prohibited on the chain */
3728 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003729 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003730 }
3731
3732 /* final sanity check */
3733 if (!bdrv_chain_contains(bs, image_bs)) {
3734 error_setg(errp, "'%s' and image file are not in the same chain",
3735 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003736 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003737 }
3738
3739 /* if not r/w, reopen to make r/w */
3740 open_flags = image_bs->open_flags;
3741 ro = bdrv_is_read_only(image_bs);
3742
3743 if (ro) {
3744 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3745 if (local_err) {
3746 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003747 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003748 }
3749 }
3750
3751 ret = bdrv_change_backing_file(image_bs, backing_file,
3752 image_bs->drv ? image_bs->drv->format_name : "");
3753
3754 if (ret < 0) {
3755 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3756 backing_file);
3757 /* don't exit here, so we can try to restore open flags if
3758 * appropriate */
3759 }
3760
3761 if (ro) {
3762 bdrv_reopen(image_bs, open_flags, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003763 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02003764 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003765
3766out:
3767 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02003768}
3769
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003770void hmp_drive_add_node(Monitor *mon, const char *optstr)
3771{
3772 QemuOpts *opts;
3773 QDict *qdict;
3774 Error *local_err = NULL;
3775
3776 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
3777 if (!opts) {
3778 return;
3779 }
3780
3781 qdict = qemu_opts_to_qdict(opts, NULL);
3782
3783 if (!qdict_get_try_str(qdict, "node-name")) {
Kevin Wolff8746fb2016-03-16 11:14:31 +01003784 QDECREF(qdict);
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003785 error_report("'node-name' needs to be specified");
3786 goto out;
3787 }
3788
3789 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
3790 if (!bs) {
3791 error_report_err(local_err);
3792 goto out;
3793 }
3794
3795 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3796
3797out:
3798 qemu_opts_del(opts);
3799}
3800
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003801void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3802{
Max Reitzbe4b67b2015-10-19 17:53:09 +02003803 BlockDriverState *bs;
3804 BlockBackend *blk = NULL;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003805 QObject *obj;
Eric Blake3b098d52016-06-09 10:48:43 -06003806 Visitor *v = qmp_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003807 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003808 Error *local_err = NULL;
3809
Markus Armbruster60e19e02014-06-06 14:50:58 +02003810 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003811 * cache.direct=false instead of silently switching to aio=threads, except
Markus Armbruster60e19e02014-06-06 14:50:58 +02003812 * when called from drive_new().
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003813 *
3814 * For now, simply forbidding the combination for all drivers will do. */
3815 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
Kevin Wolfc6e0bd92014-03-06 15:47:32 +01003816 bool direct = options->has_cache &&
3817 options->cache->has_direct &&
3818 options->cache->direct;
3819 if (!direct) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003820 error_setg(errp, "aio=native requires cache.direct=true");
3821 goto fail;
3822 }
3823 }
3824
Eric Blake3b098d52016-06-09 10:48:43 -06003825 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003826 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003827 error_propagate(errp, local_err);
3828 goto fail;
3829 }
3830
Eric Blake3b098d52016-06-09 10:48:43 -06003831 visit_complete(v, &obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003832 qdict = qobject_to_qdict(obj);
3833
3834 qdict_flatten(qdict);
3835
Max Reitzbe4b67b2015-10-19 17:53:09 +02003836 if (options->has_id) {
3837 blk = blockdev_init(NULL, qdict, &local_err);
3838 if (local_err) {
3839 error_propagate(errp, local_err);
3840 goto fail;
3841 }
3842
3843 bs = blk_bs(blk);
3844 } else {
Max Reitzbe4b67b2015-10-19 17:53:09 +02003845 if (!qdict_get_try_str(qdict, "node-name")) {
3846 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3847 "the root node");
3848 goto fail;
3849 }
3850
Max Reitzbd745e22015-10-19 17:53:32 +02003851 bs = bds_tree_init(qdict, errp);
3852 if (!bs) {
Max Reitzbe4b67b2015-10-19 17:53:09 +02003853 goto fail;
3854 }
Max Reitz9c4218e2016-01-29 16:36:12 +01003855
3856 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003857 }
3858
Max Reitzbe4b67b2015-10-19 17:53:09 +02003859 if (bs && bdrv_key_required(bs)) {
3860 if (blk) {
Max Reitzefaa7c42016-03-16 19:54:38 +01003861 monitor_remove_blk(blk);
Max Reitzbe4b67b2015-10-19 17:53:09 +02003862 blk_unref(blk);
3863 } else {
Max Reitz9c4218e2016-01-29 16:36:12 +01003864 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
Max Reitzbe4b67b2015-10-19 17:53:09 +02003865 bdrv_unref(bs);
3866 }
Kevin Wolf8ae8e902014-03-06 15:43:42 +01003867 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3868 goto fail;
3869 }
3870
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003871fail:
Eric Blake3b098d52016-06-09 10:48:43 -06003872 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003873}
3874
Alberto Garcia81b936a2015-11-02 16:51:55 +02003875void qmp_x_blockdev_del(bool has_id, const char *id,
3876 bool has_node_name, const char *node_name, Error **errp)
3877{
3878 AioContext *aio_context;
3879 BlockBackend *blk;
3880 BlockDriverState *bs;
3881
3882 if (has_id && has_node_name) {
3883 error_setg(errp, "Only one of id and node-name must be specified");
3884 return;
3885 } else if (!has_id && !has_node_name) {
3886 error_setg(errp, "No block device specified");
3887 return;
3888 }
3889
3890 if (has_id) {
Max Reitzefaa7c42016-03-16 19:54:38 +01003891 /* blk_by_name() never returns a BB that is not owned by the monitor */
Alberto Garcia81b936a2015-11-02 16:51:55 +02003892 blk = blk_by_name(id);
3893 if (!blk) {
3894 error_setg(errp, "Cannot find block backend %s", id);
3895 return;
3896 }
Paolo Bonzini5cf87fd2016-03-31 15:07:43 +02003897 if (blk_legacy_dinfo(blk)) {
3898 error_setg(errp, "Deleting block backend added with drive-add"
3899 " is not supported");
3900 return;
3901 }
Alberto Garcia81b936a2015-11-02 16:51:55 +02003902 if (blk_get_refcnt(blk) > 1) {
3903 error_setg(errp, "Block backend %s is in use", id);
3904 return;
3905 }
3906 bs = blk_bs(blk);
3907 aio_context = blk_get_aio_context(blk);
3908 } else {
Kevin Wolf1f0c4612016-03-22 18:38:44 +01003909 blk = NULL;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003910 bs = bdrv_find_node(node_name);
3911 if (!bs) {
3912 error_setg(errp, "Cannot find node %s", node_name);
3913 return;
3914 }
Kevin Wolf1f0c4612016-03-22 18:38:44 +01003915 if (bdrv_has_blk(bs)) {
Alberto Garcia81b936a2015-11-02 16:51:55 +02003916 error_setg(errp, "Node %s is in use by %s",
Kevin Wolf1f0c4612016-03-22 18:38:44 +01003917 node_name, bdrv_get_parent_name(bs));
Alberto Garcia81b936a2015-11-02 16:51:55 +02003918 return;
3919 }
3920 aio_context = bdrv_get_aio_context(bs);
3921 }
3922
3923 aio_context_acquire(aio_context);
3924
3925 if (bs) {
3926 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3927 goto out;
3928 }
3929
Max Reitz9c4218e2016-01-29 16:36:12 +01003930 if (!blk && !bs->monitor_list.tqe_prev) {
3931 error_setg(errp, "Node %s is not owned by the monitor",
3932 bs->node_name);
3933 goto out;
3934 }
3935
3936 if (bs->refcnt > 1) {
Alberto Garcia81b936a2015-11-02 16:51:55 +02003937 error_setg(errp, "Block device %s is in use",
3938 bdrv_get_device_or_node_name(bs));
3939 goto out;
3940 }
3941 }
3942
3943 if (blk) {
Max Reitzefaa7c42016-03-16 19:54:38 +01003944 monitor_remove_blk(blk);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003945 blk_unref(blk);
3946 } else {
Max Reitz9c4218e2016-01-29 16:36:12 +01003947 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003948 bdrv_unref(bs);
3949 }
3950
3951out:
3952 aio_context_release(aio_context);
3953}
3954
Wen Congyang7f821592016-05-10 15:36:39 +08003955static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
3956 const char *child_name)
3957{
3958 BdrvChild *child;
3959
3960 QLIST_FOREACH(child, &parent_bs->children, next) {
3961 if (strcmp(child->name, child_name) == 0) {
3962 return child;
3963 }
3964 }
3965
3966 return NULL;
3967}
3968
3969void qmp_x_blockdev_change(const char *parent, bool has_child,
3970 const char *child, bool has_node,
3971 const char *node, Error **errp)
3972{
3973 BlockDriverState *parent_bs, *new_bs = NULL;
3974 BdrvChild *p_child;
3975
3976 parent_bs = bdrv_lookup_bs(parent, parent, errp);
3977 if (!parent_bs) {
3978 return;
3979 }
3980
3981 if (has_child == has_node) {
3982 if (has_child) {
3983 error_setg(errp, "The parameters child and node are in conflict");
3984 } else {
3985 error_setg(errp, "Either child or node must be specified");
3986 }
3987 return;
3988 }
3989
3990 if (has_child) {
3991 p_child = bdrv_find_child(parent_bs, child);
3992 if (!p_child) {
3993 error_setg(errp, "Node '%s' does not have child '%s'",
3994 parent, child);
3995 return;
3996 }
3997 bdrv_del_child(parent_bs, p_child, errp);
3998 }
3999
4000 if (has_node) {
4001 new_bs = bdrv_find_node(node);
4002 if (!new_bs) {
4003 error_setg(errp, "Node '%s' not found", node);
4004 return;
4005 }
4006 bdrv_add_child(parent_bs, new_bs, errp);
4007 }
4008}
4009
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004010BlockJobInfoList *qmp_query_block_jobs(Error **errp)
4011{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004012 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004013 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004014
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004015 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
4016 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
4017 AioContext *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004018
4019 aio_context_acquire(aio_context);
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004020 elem->value = block_job_query(job);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004021 aio_context_release(aio_context);
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004022
4023 *p_next = elem;
4024 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004025 }
4026
4027 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004028}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004029
Kevin Wolf00063832013-03-15 10:35:07 +01004030QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004031 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01004032 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01004033 .desc = {
4034 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004035 .name = "snapshot",
4036 .type = QEMU_OPT_BOOL,
4037 .help = "enable/disable snapshot mode",
4038 },{
Paolo Bonzinia9384af2013-02-08 14:06:12 +01004039 .name = "discard",
4040 .type = QEMU_OPT_STRING,
4041 .help = "discard operation (ignore/off, unmap/on)",
4042 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004043 .name = "aio",
4044 .type = QEMU_OPT_STRING,
4045 .help = "host AIO implementation (threads, native)",
4046 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01004047 .name = BDRV_OPT_CACHE_WB,
4048 .type = QEMU_OPT_BOOL,
4049 .help = "Enable writeback mode",
4050 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004051 .name = "format",
4052 .type = QEMU_OPT_STRING,
4053 .help = "disk format (raw, qcow2, ...)",
4054 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004055 .name = "rerror",
4056 .type = QEMU_OPT_STRING,
4057 .help = "read error action",
4058 },{
4059 .name = "werror",
4060 .type = QEMU_OPT_STRING,
4061 .help = "write error action",
4062 },{
Kevin Wolf0f227a92013-07-19 20:07:29 +02004063 .name = "read-only",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004064 .type = QEMU_OPT_BOOL,
4065 .help = "open drive file as read-only",
4066 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004067 .name = "throttling.iops-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004068 .type = QEMU_OPT_NUMBER,
4069 .help = "limit total I/O operations per second",
4070 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004071 .name = "throttling.iops-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004072 .type = QEMU_OPT_NUMBER,
4073 .help = "limit read operations per second",
4074 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004075 .name = "throttling.iops-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004076 .type = QEMU_OPT_NUMBER,
4077 .help = "limit write operations per second",
4078 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004079 .name = "throttling.bps-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004080 .type = QEMU_OPT_NUMBER,
4081 .help = "limit total bytes per second",
4082 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004083 .name = "throttling.bps-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004084 .type = QEMU_OPT_NUMBER,
4085 .help = "limit read bytes per second",
4086 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004087 .name = "throttling.bps-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004088 .type = QEMU_OPT_NUMBER,
4089 .help = "limit write bytes per second",
4090 },{
Benoît Canet3e9fab62013-09-02 14:14:40 +02004091 .name = "throttling.iops-total-max",
4092 .type = QEMU_OPT_NUMBER,
4093 .help = "I/O operations burst",
4094 },{
4095 .name = "throttling.iops-read-max",
4096 .type = QEMU_OPT_NUMBER,
4097 .help = "I/O operations read burst",
4098 },{
4099 .name = "throttling.iops-write-max",
4100 .type = QEMU_OPT_NUMBER,
4101 .help = "I/O operations write burst",
4102 },{
4103 .name = "throttling.bps-total-max",
4104 .type = QEMU_OPT_NUMBER,
4105 .help = "total bytes burst",
4106 },{
4107 .name = "throttling.bps-read-max",
4108 .type = QEMU_OPT_NUMBER,
4109 .help = "total bytes read burst",
4110 },{
4111 .name = "throttling.bps-write-max",
4112 .type = QEMU_OPT_NUMBER,
4113 .help = "total bytes write burst",
4114 },{
Alberto Garcia8a0fc182016-02-18 12:27:02 +02004115 .name = "throttling.iops-total-max-length",
4116 .type = QEMU_OPT_NUMBER,
4117 .help = "length of the iops-total-max burst period, in seconds",
4118 },{
4119 .name = "throttling.iops-read-max-length",
4120 .type = QEMU_OPT_NUMBER,
4121 .help = "length of the iops-read-max burst period, in seconds",
4122 },{
4123 .name = "throttling.iops-write-max-length",
4124 .type = QEMU_OPT_NUMBER,
4125 .help = "length of the iops-write-max burst period, in seconds",
4126 },{
4127 .name = "throttling.bps-total-max-length",
4128 .type = QEMU_OPT_NUMBER,
4129 .help = "length of the bps-total-max burst period, in seconds",
4130 },{
4131 .name = "throttling.bps-read-max-length",
4132 .type = QEMU_OPT_NUMBER,
4133 .help = "length of the bps-read-max burst period, in seconds",
4134 },{
4135 .name = "throttling.bps-write-max-length",
4136 .type = QEMU_OPT_NUMBER,
4137 .help = "length of the bps-write-max burst period, in seconds",
4138 },{
Benoît Canet2024c1d2013-09-02 14:14:41 +02004139 .name = "throttling.iops-size",
4140 .type = QEMU_OPT_NUMBER,
4141 .help = "when limiting by iops max size of an I/O in bytes",
4142 },{
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004143 .name = "throttling.group",
4144 .type = QEMU_OPT_STRING,
4145 .help = "name of the block throttling group",
4146 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004147 .name = "copy-on-read",
4148 .type = QEMU_OPT_BOOL,
4149 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004150 },{
4151 .name = "detect-zeroes",
4152 .type = QEMU_OPT_STRING,
4153 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004154 },{
4155 .name = "stats-account-invalid",
4156 .type = QEMU_OPT_BOOL,
4157 .help = "whether to account for invalid I/O operations "
4158 "in the statistics",
4159 },{
4160 .name = "stats-account-failed",
4161 .type = QEMU_OPT_BOOL,
4162 .help = "whether to account for failed I/O operations "
4163 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004164 },
4165 { /* end of list */ }
4166 },
4167};
Kevin Wolf00063832013-03-15 10:35:07 +01004168
Max Reitzbd745e22015-10-19 17:53:32 +02004169static QemuOptsList qemu_root_bds_opts = {
4170 .name = "root-bds",
Kevin Wolf23f7fcb2016-03-15 14:43:14 +01004171 .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head),
Max Reitzbd745e22015-10-19 17:53:32 +02004172 .desc = {
4173 {
4174 .name = "discard",
4175 .type = QEMU_OPT_STRING,
4176 .help = "discard operation (ignore/off, unmap/on)",
4177 },{
Max Reitzbd745e22015-10-19 17:53:32 +02004178 .name = "aio",
4179 .type = QEMU_OPT_STRING,
4180 .help = "host AIO implementation (threads, native)",
4181 },{
4182 .name = "read-only",
4183 .type = QEMU_OPT_BOOL,
4184 .help = "open drive file as read-only",
4185 },{
4186 .name = "copy-on-read",
4187 .type = QEMU_OPT_BOOL,
4188 .help = "copy read data from backing file into image file",
4189 },{
4190 .name = "detect-zeroes",
4191 .type = QEMU_OPT_STRING,
4192 .help = "try to optimize zero writes (off, on, unmap)",
4193 },
4194 { /* end of list */ }
4195 },
4196};
4197
Kevin Wolf00063832013-03-15 10:35:07 +01004198QemuOptsList qemu_drive_opts = {
4199 .name = "drive",
4200 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4201 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004202 /*
4203 * no elements => accept any params
4204 * validation will happen later
4205 */
Kevin Wolf00063832013-03-15 10:35:07 +01004206 { /* end of list */ }
4207 },
4208};