blob: ccff1f7d05c62bb553a95bf002b5a744ddbf9654 [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;
1309 BlockBackend *blk;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001310 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001311 QEMUSnapshotInfo sn;
1312 Error *local_err = NULL;
1313 SnapshotInfo *info = NULL;
1314 int ret;
1315
Fam Zhenga0e85442015-03-02 19:36:48 +08001316 blk = blk_by_name(device);
1317 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001318 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1319 "Device '%s' not found", device);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001320 return NULL;
1321 }
Max Reitz5433c242015-10-19 17:53:29 +02001322
1323 aio_context = blk_get_aio_context(blk);
1324 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001325
1326 if (!has_id) {
1327 id = NULL;
1328 }
1329
1330 if (!has_name) {
1331 name = NULL;
1332 }
1333
1334 if (!id && !name) {
1335 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001336 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001337 }
1338
Max Reitz5433c242015-10-19 17:53:29 +02001339 if (!blk_is_available(blk)) {
1340 error_setg(errp, "Device '%s' has no medium", device);
1341 goto out_aio_context;
1342 }
1343 bs = blk_bs(blk);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001344
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001345 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1346 goto out_aio_context;
1347 }
1348
Wenchao Xia44e3e052013-09-11 14:04:36 +08001349 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001350 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001351 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001352 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001353 }
1354 if (!ret) {
1355 error_setg(errp,
1356 "Snapshot with id '%s' and name '%s' does not exist on "
1357 "device '%s'",
1358 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001359 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001360 }
1361
1362 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001363 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001364 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001365 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001366 }
1367
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001368 aio_context_release(aio_context);
1369
Markus Armbruster5839e532014-08-19 10:31:08 +02001370 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001371 info->id = g_strdup(sn.id_str);
1372 info->name = g_strdup(sn.name);
1373 info->date_nsec = sn.date_nsec;
1374 info->date_sec = sn.date_sec;
1375 info->vm_state_size = sn.vm_state_size;
1376 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1377 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1378
1379 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001380
1381out_aio_context:
1382 aio_context_release(aio_context);
1383 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001384}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001385
John Snow341ebc22015-04-17 19:49:52 -04001386/**
1387 * block_dirty_bitmap_lookup:
1388 * Return a dirty bitmap (if present), after validating
1389 * the node reference and bitmap names.
1390 *
1391 * @node: The name of the BDS node to search for bitmaps
1392 * @name: The name of the bitmap to search for
1393 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1394 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1395 * @errp: Output pointer for error information. Can be NULL.
1396 *
1397 * @return: A bitmap object on success, or NULL on failure.
1398 */
1399static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1400 const char *name,
1401 BlockDriverState **pbs,
1402 AioContext **paio,
1403 Error **errp)
1404{
1405 BlockDriverState *bs;
1406 BdrvDirtyBitmap *bitmap;
1407 AioContext *aio_context;
1408
1409 if (!node) {
1410 error_setg(errp, "Node cannot be NULL");
1411 return NULL;
1412 }
1413 if (!name) {
1414 error_setg(errp, "Bitmap name cannot be NULL");
1415 return NULL;
1416 }
1417 bs = bdrv_lookup_bs(node, node, NULL);
1418 if (!bs) {
1419 error_setg(errp, "Node '%s' not found", node);
1420 return NULL;
1421 }
1422
1423 aio_context = bdrv_get_aio_context(bs);
1424 aio_context_acquire(aio_context);
1425
1426 bitmap = bdrv_find_dirty_bitmap(bs, name);
1427 if (!bitmap) {
1428 error_setg(errp, "Dirty bitmap '%s' not found", name);
1429 goto fail;
1430 }
1431
1432 if (pbs) {
1433 *pbs = bs;
1434 }
1435 if (paio) {
1436 *paio = aio_context;
1437 } else {
1438 aio_context_release(aio_context);
1439 }
1440
1441 return bitmap;
1442
1443 fail:
1444 aio_context_release(aio_context);
1445 return NULL;
1446}
1447
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001448/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001449
John Snow50f43f02015-11-05 18:13:09 -05001450typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001451
John Snow50f43f02015-11-05 18:13:09 -05001452/**
1453 * BlkActionOps:
1454 * Table of operations that define an Action.
1455 *
1456 * @instance_size: Size of state struct, in bytes.
1457 * @prepare: Prepare the work, must NOT be NULL.
1458 * @commit: Commit the changes, can be NULL.
1459 * @abort: Abort the changes on fail, can be NULL.
1460 * @clean: Clean up resources after all transaction actions have called
1461 * commit() or abort(). Can be NULL.
1462 *
1463 * Only prepare() may fail. In a single transaction, only one of commit() or
1464 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001465 */
John Snow50f43f02015-11-05 18:13:09 -05001466typedef struct BlkActionOps {
1467 size_t instance_size;
1468 void (*prepare)(BlkActionState *common, Error **errp);
1469 void (*commit)(BlkActionState *common);
1470 void (*abort)(BlkActionState *common);
1471 void (*clean)(BlkActionState *common);
1472} BlkActionOps;
1473
1474/**
1475 * BlkActionState:
1476 * Describes one Action's state within a Transaction.
1477 *
1478 * @action: QAPI-defined enum identifying which Action to perform.
1479 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001480 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001481 * @entry: List membership for all Actions in this Transaction.
1482 *
1483 * This structure must be arranged as first member in a subclassed type,
1484 * assuming that the compiler will also arrange it to the same offsets as the
1485 * base class.
1486 */
1487struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001488 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001489 const BlkActionOps *ops;
John Snow94d16a62015-11-05 18:13:18 -05001490 BlockJobTxn *block_job_txn;
1491 TransactionProperties *txn_props;
John Snow50f43f02015-11-05 18:13:09 -05001492 QSIMPLEQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001493};
1494
Wenchao Xiabbe86012013-09-11 14:04:34 +08001495/* internal snapshot private data */
1496typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001497 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001498 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001499 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001500 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001501 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001502} InternalSnapshotState;
1503
John Snow94d16a62015-11-05 18:13:18 -05001504
1505static int action_check_completion_mode(BlkActionState *s, Error **errp)
1506{
1507 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1508 error_setg(errp,
1509 "Action '%s' does not support Transaction property "
1510 "completion-mode = %s",
1511 TransactionActionKind_lookup[s->action->type],
1512 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1513 return -1;
1514 }
1515 return 0;
1516}
1517
John Snow50f43f02015-11-05 18:13:09 -05001518static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001519 Error **errp)
1520{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001521 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001522 const char *device;
1523 const char *name;
Fam Zhenga0e85442015-03-02 19:36:48 +08001524 BlockBackend *blk;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001525 BlockDriverState *bs;
1526 QEMUSnapshotInfo old_sn, *sn;
1527 bool ret;
1528 qemu_timeval tv;
1529 BlockdevSnapshotInternal *internal;
1530 InternalSnapshotState *state;
1531 int ret1;
1532
Eric Blake6a8f9662015-10-26 16:34:54 -06001533 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001534 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001535 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001536 state = DO_UPCAST(InternalSnapshotState, common, common);
1537
1538 /* 1. parse input */
1539 device = internal->device;
1540 name = internal->name;
1541
1542 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001543 if (action_check_completion_mode(common, errp) < 0) {
1544 return;
1545 }
1546
Fam Zhenga0e85442015-03-02 19:36:48 +08001547 blk = blk_by_name(device);
1548 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001549 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1550 "Device '%s' not found", device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001551 return;
1552 }
1553
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001554 /* AioContext is released in .clean() */
Max Reitz5433c242015-10-19 17:53:29 +02001555 state->aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001556 aio_context_acquire(state->aio_context);
1557
Max Reitz5433c242015-10-19 17:53:29 +02001558 if (!blk_is_available(blk)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001559 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001560 return;
1561 }
Max Reitz5433c242015-10-19 17:53:29 +02001562 bs = blk_bs(blk);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001563
Fam Zheng507306c2015-10-23 11:08:13 +08001564 state->bs = bs;
1565 bdrv_drained_begin(bs);
1566
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001567 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1568 return;
1569 }
1570
Wenchao Xiabbe86012013-09-11 14:04:34 +08001571 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001572 error_setg(errp, "Device '%s' is read only", device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001573 return;
1574 }
1575
1576 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001577 error_setg(errp, "Block format '%s' used by device '%s' "
1578 "does not support internal snapshots",
1579 bs->drv->format_name, device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001580 return;
1581 }
1582
1583 if (!strlen(name)) {
1584 error_setg(errp, "Name is empty");
1585 return;
1586 }
1587
1588 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001589 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1590 &local_err);
1591 if (local_err) {
1592 error_propagate(errp, local_err);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001593 return;
1594 } else if (ret) {
1595 error_setg(errp,
1596 "Snapshot with name '%s' already exists on device '%s'",
1597 name, device);
1598 return;
1599 }
1600
1601 /* 3. take the snapshot */
1602 sn = &state->sn;
1603 pstrcpy(sn->name, sizeof(sn->name), name);
1604 qemu_gettimeofday(&tv);
1605 sn->date_sec = tv.tv_sec;
1606 sn->date_nsec = tv.tv_usec * 1000;
1607 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1608
1609 ret1 = bdrv_snapshot_create(bs, sn);
1610 if (ret1 < 0) {
1611 error_setg_errno(errp, -ret1,
1612 "Failed to create snapshot '%s' on device '%s'",
1613 name, device);
1614 return;
1615 }
1616
1617 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001618 state->created = true;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001619}
1620
John Snow50f43f02015-11-05 18:13:09 -05001621static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001622{
1623 InternalSnapshotState *state =
1624 DO_UPCAST(InternalSnapshotState, common, common);
1625 BlockDriverState *bs = state->bs;
1626 QEMUSnapshotInfo *sn = &state->sn;
1627 Error *local_error = NULL;
1628
Fam Zheng507306c2015-10-23 11:08:13 +08001629 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001630 return;
1631 }
1632
1633 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001634 error_reportf_err(local_error,
1635 "Failed to delete snapshot with id '%s' and "
1636 "name '%s' on device '%s' in abort: ",
1637 sn->id_str, sn->name,
1638 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001639 }
1640}
1641
John Snow50f43f02015-11-05 18:13:09 -05001642static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001643{
1644 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1645 common, common);
1646
1647 if (state->aio_context) {
Fam Zheng507306c2015-10-23 11:08:13 +08001648 if (state->bs) {
1649 bdrv_drained_end(state->bs);
1650 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001651 aio_context_release(state->aio_context);
1652 }
1653}
1654
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001655/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001656typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001657 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001658 BlockDriverState *old_bs;
1659 BlockDriverState *new_bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001660 AioContext *aio_context;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001661} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001662
John Snow50f43f02015-11-05 18:13:09 -05001663static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001664 Error **errp)
1665{
Max Reitz5b363932016-05-17 16:41:31 +02001666 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001667 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001668 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001669 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001670 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001671 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001672 /* Reference to the new image (for 'blockdev-snapshot') */
1673 const char *snapshot_ref;
1674 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001675 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001676 ExternalSnapshotState *state =
1677 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001678 TransactionAction *action = common->action;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001679
Alberto Garcia43de7e22015-10-26 14:27:16 +02001680 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1681 * purpose but a different set of parameters */
1682 switch (action->type) {
1683 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1684 {
Eric Blake32bafa82016-03-17 16:48:37 -06001685 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001686 device = s->node;
1687 node_name = s->node;
1688 new_image_file = NULL;
1689 snapshot_ref = s->overlay;
1690 }
1691 break;
1692 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1693 {
Eric Blake32bafa82016-03-17 16:48:37 -06001694 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001695 device = s->has_device ? s->device : NULL;
1696 node_name = s->has_node_name ? s->node_name : NULL;
1697 new_image_file = s->snapshot_file;
1698 snapshot_ref = NULL;
1699 }
1700 break;
1701 default:
1702 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001703 }
1704
1705 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001706 if (action_check_completion_mode(common, errp) < 0) {
1707 return;
1708 }
1709
Alberto Garcia43de7e22015-10-26 14:27:16 +02001710 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1711 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001712 return;
1713 }
1714
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001715 /* Acquire AioContext now so any threads operating on old_bs stop */
1716 state->aio_context = bdrv_get_aio_context(state->old_bs);
1717 aio_context_acquire(state->aio_context);
Fam Zhengda763e82015-10-23 11:08:10 +08001718 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001719
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001720 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001721 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001722 return;
1723 }
1724
Fam Zheng3718d8a2014-05-23 21:29:43 +08001725 if (bdrv_op_is_blocked(state->old_bs,
1726 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001727 return;
1728 }
1729
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001730 if (!bdrv_is_read_only(state->old_bs)) {
1731 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001732 error_setg(errp, QERR_IO_ERROR);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001733 return;
1734 }
1735 }
1736
Benoît Canet212a5a82014-01-23 21:31:36 +01001737 if (!bdrv_is_first_non_filter(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001738 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
Benoît Canetf6186f42013-10-02 14:33:48 +02001739 return;
1740 }
1741
Alberto Garcia43de7e22015-10-26 14:27:16 +02001742 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001743 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001744 const char *format = s->has_format ? s->format : "qcow2";
1745 enum NewImageMode mode;
1746 const char *snapshot_node_name =
1747 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001748
Alberto Garcia43de7e22015-10-26 14:27:16 +02001749 if (node_name && !snapshot_node_name) {
1750 error_setg(errp, "New snapshot node name missing");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001751 return;
1752 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001753
1754 if (snapshot_node_name &&
1755 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1756 error_setg(errp, "New snapshot node name already in use");
1757 return;
1758 }
1759
1760 flags = state->old_bs->open_flags;
Kevin Wolf4c844982016-02-29 13:12:26 +01001761 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001762
1763 /* create new image w/backing file */
1764 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1765 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001766 int64_t size = bdrv_getlength(state->old_bs);
1767 if (size < 0) {
1768 error_setg_errno(errp, -size, "bdrv_getlength failed");
1769 return;
1770 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001771 bdrv_img_create(new_image_file, format,
1772 state->old_bs->filename,
1773 state->old_bs->drv->format_name,
Kevin Wolff86b8b52016-03-02 12:16:44 +01001774 NULL, size, flags, &local_err, false);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001775 if (local_err) {
1776 error_propagate(errp, local_err);
1777 return;
1778 }
1779 }
1780
1781 options = qdict_new();
1782 if (s->has_snapshot_node_name) {
1783 qdict_put(options, "node-name",
1784 qstring_from_str(snapshot_node_name));
1785 }
1786 qdict_put(options, "driver", qstring_from_str(format));
1787
1788 flags |= BDRV_O_NO_BACKING;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001789 }
1790
Max Reitz5b363932016-05-17 16:41:31 +02001791 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1792 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001793 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001794 if (!state->new_bs) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001795 return;
1796 }
1797
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001798 if (bdrv_has_blk(state->new_bs)) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001799 error_setg(errp, "The snapshot is already in use by %s",
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001800 bdrv_get_parent_name(state->new_bs));
Alberto Garcia43de7e22015-10-26 14:27:16 +02001801 return;
1802 }
1803
1804 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1805 errp)) {
1806 return;
1807 }
1808
1809 if (state->new_bs->backing != NULL) {
1810 error_setg(errp, "The snapshot already has a backing image");
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001811 return;
1812 }
1813
1814 if (!state->new_bs->drv->supports_backing) {
1815 error_setg(errp, "The snapshot does not support backing images");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001816 }
1817}
1818
John Snow50f43f02015-11-05 18:13:09 -05001819static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001820{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001821 ExternalSnapshotState *state =
1822 DO_UPCAST(ExternalSnapshotState, common, common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001823
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001824 bdrv_set_aio_context(state->new_bs, state->aio_context);
1825
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001826 /* This removes our old bs and adds the new bs */
1827 bdrv_append(state->new_bs, state->old_bs);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001828 /* We don't need (or want) to use the transactional
1829 * bdrv_reopen_multiple() across all the entries at once, because we
1830 * don't want to abort all of them if one of them fails the reopen */
Kevin Wolf4c844982016-02-29 13:12:26 +01001831 if (!state->old_bs->copy_on_read) {
1832 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1833 NULL);
1834 }
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001835}
1836
John Snow50f43f02015-11-05 18:13:09 -05001837static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001838{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001839 ExternalSnapshotState *state =
1840 DO_UPCAST(ExternalSnapshotState, common, common);
1841 if (state->new_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001842 bdrv_unref(state->new_bs);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001843 }
Fam Zhengda763e82015-10-23 11:08:10 +08001844}
1845
John Snow50f43f02015-11-05 18:13:09 -05001846static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001847{
1848 ExternalSnapshotState *state =
1849 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001850 if (state->aio_context) {
Fam Zhengda763e82015-10-23 11:08:10 +08001851 bdrv_drained_end(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001852 aio_context_release(state->aio_context);
1853 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001854}
1855
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001856typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001857 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001858 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001859 AioContext *aio_context;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001860 BlockJob *job;
1861} DriveBackupState;
1862
Alberto Garcia70559d42016-07-05 17:28:58 +03001863static void do_drive_backup(const char *job_id, const char *device,
1864 const char *target, bool has_format,
1865 const char *format, enum MirrorSyncMode sync,
John Snow78f51fd2015-11-05 18:13:17 -05001866 bool has_mode, enum NewImageMode mode,
1867 bool has_speed, int64_t speed,
1868 bool has_bitmap, const char *bitmap,
1869 bool has_on_source_error,
1870 BlockdevOnError on_source_error,
1871 bool has_on_target_error,
1872 BlockdevOnError on_target_error,
1873 BlockJobTxn *txn, Error **errp);
1874
John Snow50f43f02015-11-05 18:13:09 -05001875static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001876{
1877 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Fam Zhenga0e85442015-03-02 19:36:48 +08001878 BlockBackend *blk;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001879 DriveBackup *backup;
1880 Error *local_err = NULL;
1881
Eric Blake6a8f9662015-10-26 16:34:54 -06001882 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001883 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001884
Fam Zhenga0e85442015-03-02 19:36:48 +08001885 blk = blk_by_name(backup->device);
1886 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001887 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1888 "Device '%s' not found", backup->device);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001889 return;
1890 }
1891
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001892 if (!blk_is_available(blk)) {
1893 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1894 return;
1895 }
1896
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001897 /* AioContext is released in .clean() */
Max Reitz5433c242015-10-19 17:53:29 +02001898 state->aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001899 aio_context_acquire(state->aio_context);
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001900 bdrv_drained_begin(blk_bs(blk));
1901 state->bs = blk_bs(blk);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001902
Alberto Garcia70559d42016-07-05 17:28:58 +03001903 do_drive_backup(backup->has_job_id ? backup->job_id : NULL,
1904 backup->device, backup->target,
John Snow78f51fd2015-11-05 18:13:17 -05001905 backup->has_format, backup->format,
1906 backup->sync,
1907 backup->has_mode, backup->mode,
1908 backup->has_speed, backup->speed,
1909 backup->has_bitmap, backup->bitmap,
1910 backup->has_on_source_error, backup->on_source_error,
1911 backup->has_on_target_error, backup->on_target_error,
John Snow94d16a62015-11-05 18:13:18 -05001912 common->block_job_txn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001913 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001914 error_propagate(errp, local_err);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001915 return;
1916 }
1917
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001918 state->job = state->bs->job;
1919}
1920
John Snow50f43f02015-11-05 18:13:09 -05001921static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001922{
1923 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1924 BlockDriverState *bs = state->bs;
1925
1926 /* Only cancel if it's the job we started */
1927 if (bs && bs->job && bs->job == state->job) {
1928 block_job_cancel_sync(bs->job);
1929 }
1930}
1931
John Snow50f43f02015-11-05 18:13:09 -05001932static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001933{
1934 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1935
1936 if (state->aio_context) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001937 bdrv_drained_end(state->bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001938 aio_context_release(state->aio_context);
1939 }
1940}
1941
Fam Zhengbd8baec2014-12-18 18:37:06 +08001942typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001943 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001944 BlockDriverState *bs;
1945 BlockJob *job;
1946 AioContext *aio_context;
1947} BlockdevBackupState;
1948
Alberto Garcia70559d42016-07-05 17:28:58 +03001949static void do_blockdev_backup(const char *job_id, const char *device,
1950 const char *target, enum MirrorSyncMode sync,
John Snow78f51fd2015-11-05 18:13:17 -05001951 bool has_speed, int64_t speed,
1952 bool has_on_source_error,
1953 BlockdevOnError on_source_error,
1954 bool has_on_target_error,
1955 BlockdevOnError on_target_error,
1956 BlockJobTxn *txn, Error **errp);
1957
John Snow50f43f02015-11-05 18:13:09 -05001958static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001959{
1960 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1961 BlockdevBackup *backup;
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001962 BlockDriverState *bs, *target;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001963 Error *local_err = NULL;
1964
Eric Blake6a8f9662015-10-26 16:34:54 -06001965 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001966 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001967
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001968 bs = qmp_get_root_bs(backup->device, errp);
1969 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001970 return;
1971 }
1972
Kevin Wolf39d990a2016-08-02 19:22:04 +02001973 target = bdrv_lookup_bs(backup->target, backup->target, errp);
Max Reitz5433c242015-10-19 17:53:29 +02001974 if (!target) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001975 return;
1976 }
1977
1978 /* AioContext is released in .clean() */
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001979 state->aio_context = bdrv_get_aio_context(bs);
Kevin Wolf39d990a2016-08-02 19:22:04 +02001980 if (state->aio_context != bdrv_get_aio_context(target)) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001981 state->aio_context = NULL;
1982 error_setg(errp, "Backup between two IO threads is not implemented");
1983 return;
1984 }
1985 aio_context_acquire(state->aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001986 state->bs = bs;
Fam Zhengff52bf32015-10-23 11:08:12 +08001987 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001988
Alberto Garcia70559d42016-07-05 17:28:58 +03001989 do_blockdev_backup(backup->has_job_id ? backup->job_id : NULL,
1990 backup->device, backup->target, backup->sync,
John Snow78f51fd2015-11-05 18:13:17 -05001991 backup->has_speed, backup->speed,
1992 backup->has_on_source_error, backup->on_source_error,
1993 backup->has_on_target_error, backup->on_target_error,
John Snow94d16a62015-11-05 18:13:18 -05001994 common->block_job_txn, &local_err);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001995 if (local_err) {
1996 error_propagate(errp, local_err);
1997 return;
1998 }
1999
Fam Zhengbd8baec2014-12-18 18:37:06 +08002000 state->job = state->bs->job;
2001}
2002
John Snow50f43f02015-11-05 18:13:09 -05002003static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08002004{
2005 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
2006 BlockDriverState *bs = state->bs;
2007
2008 /* Only cancel if it's the job we started */
2009 if (bs && bs->job && bs->job == state->job) {
2010 block_job_cancel_sync(bs->job);
2011 }
2012}
2013
John Snow50f43f02015-11-05 18:13:09 -05002014static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08002015{
2016 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
2017
2018 if (state->aio_context) {
Fam Zhengff52bf32015-10-23 11:08:12 +08002019 bdrv_drained_end(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002020 aio_context_release(state->aio_context);
2021 }
2022}
2023
Fam Zhengdf9a6812015-11-09 18:16:54 +08002024typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05002025 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002026 BdrvDirtyBitmap *bitmap;
2027 BlockDriverState *bs;
2028 AioContext *aio_context;
2029 HBitmap *backup;
2030 bool prepared;
2031} BlockDirtyBitmapState;
2032
John Snow50f43f02015-11-05 18:13:09 -05002033static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002034 Error **errp)
2035{
2036 Error *local_err = NULL;
2037 BlockDirtyBitmapAdd *action;
2038 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2039 common, common);
2040
John Snow94d16a62015-11-05 18:13:18 -05002041 if (action_check_completion_mode(common, errp) < 0) {
2042 return;
2043 }
2044
Eric Blake32bafa82016-03-17 16:48:37 -06002045 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002046 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2047 qmp_block_dirty_bitmap_add(action->node, action->name,
2048 action->has_granularity, action->granularity,
2049 &local_err);
2050
2051 if (!local_err) {
2052 state->prepared = true;
2053 } else {
2054 error_propagate(errp, local_err);
2055 }
2056}
2057
John Snow50f43f02015-11-05 18:13:09 -05002058static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002059{
2060 BlockDirtyBitmapAdd *action;
2061 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2062 common, common);
2063
Eric Blake32bafa82016-03-17 16:48:37 -06002064 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002065 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2066 * then the node reference and bitmap name must have been valid.
2067 */
2068 if (state->prepared) {
2069 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2070 }
2071}
2072
John Snow50f43f02015-11-05 18:13:09 -05002073static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002074 Error **errp)
2075{
2076 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2077 common, common);
2078 BlockDirtyBitmap *action;
2079
John Snow94d16a62015-11-05 18:13:18 -05002080 if (action_check_completion_mode(common, errp) < 0) {
2081 return;
2082 }
2083
Eric Blake32bafa82016-03-17 16:48:37 -06002084 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002085 state->bitmap = block_dirty_bitmap_lookup(action->node,
2086 action->name,
2087 &state->bs,
2088 &state->aio_context,
2089 errp);
2090 if (!state->bitmap) {
2091 return;
2092 }
2093
2094 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2095 error_setg(errp, "Cannot modify a frozen bitmap");
2096 return;
2097 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2098 error_setg(errp, "Cannot clear a disabled bitmap");
2099 return;
2100 }
2101
2102 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2103 /* AioContext is released in .clean() */
2104}
2105
John Snow50f43f02015-11-05 18:13:09 -05002106static void block_dirty_bitmap_clear_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002107{
2108 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2109 common, common);
2110
2111 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2112}
2113
John Snow50f43f02015-11-05 18:13:09 -05002114static void block_dirty_bitmap_clear_commit(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002115{
2116 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2117 common, common);
2118
2119 hbitmap_free(state->backup);
2120}
2121
John Snow50f43f02015-11-05 18:13:09 -05002122static void block_dirty_bitmap_clear_clean(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002123{
2124 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2125 common, common);
2126
2127 if (state->aio_context) {
2128 aio_context_release(state->aio_context);
2129 }
2130}
2131
John Snow50f43f02015-11-05 18:13:09 -05002132static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002133{
2134 error_setg(errp, "Transaction aborted using Abort action");
2135}
2136
John Snow50f43f02015-11-05 18:13:09 -05002137static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002138{
Stefan Weildfc6f862013-07-25 18:21:28 +02002139 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002140}
2141
John Snow50f43f02015-11-05 18:13:09 -05002142static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002143 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2144 .instance_size = sizeof(ExternalSnapshotState),
2145 .prepare = external_snapshot_prepare,
2146 .commit = external_snapshot_commit,
2147 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002148 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002149 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002150 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002151 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002152 .prepare = external_snapshot_prepare,
2153 .commit = external_snapshot_commit,
2154 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002155 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002156 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002157 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2158 .instance_size = sizeof(DriveBackupState),
2159 .prepare = drive_backup_prepare,
2160 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002161 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002162 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002163 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2164 .instance_size = sizeof(BlockdevBackupState),
2165 .prepare = blockdev_backup_prepare,
2166 .abort = blockdev_backup_abort,
2167 .clean = blockdev_backup_clean,
2168 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002169 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002170 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002171 .prepare = abort_prepare,
2172 .commit = abort_commit,
2173 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002174 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2175 .instance_size = sizeof(InternalSnapshotState),
2176 .prepare = internal_snapshot_prepare,
2177 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002178 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002179 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002180 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2181 .instance_size = sizeof(BlockDirtyBitmapState),
2182 .prepare = block_dirty_bitmap_add_prepare,
2183 .abort = block_dirty_bitmap_add_abort,
2184 },
2185 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2186 .instance_size = sizeof(BlockDirtyBitmapState),
2187 .prepare = block_dirty_bitmap_clear_prepare,
2188 .commit = block_dirty_bitmap_clear_commit,
2189 .abort = block_dirty_bitmap_clear_abort,
2190 .clean = block_dirty_bitmap_clear_clean,
2191 }
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002192};
2193
John Snow94d16a62015-11-05 18:13:18 -05002194/**
2195 * Allocate a TransactionProperties structure if necessary, and fill
2196 * that structure with desired defaults if they are unset.
2197 */
2198static TransactionProperties *get_transaction_properties(
2199 TransactionProperties *props)
2200{
2201 if (!props) {
2202 props = g_new0(TransactionProperties, 1);
2203 }
2204
2205 if (!props->has_completion_mode) {
2206 props->has_completion_mode = true;
2207 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2208 }
2209
2210 return props;
2211}
2212
Jeff Cody8802d1f2012-02-28 15:54:06 -05002213/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002214 * 'Atomic' group operations. The operations are performed as a set, and if
2215 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002216 */
John Snow94d16a62015-11-05 18:13:18 -05002217void qmp_transaction(TransactionActionList *dev_list,
2218 bool has_props,
2219 struct TransactionProperties *props,
2220 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002221{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002222 TransactionActionList *dev_entry = dev_list;
John Snow94d16a62015-11-05 18:13:18 -05002223 BlockJobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002224 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002225 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002226
John Snow50f43f02015-11-05 18:13:09 -05002227 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002228 QSIMPLEQ_INIT(&snap_bdrv_states);
2229
John Snow94d16a62015-11-05 18:13:18 -05002230 /* Does this transaction get canceled as a group on failure?
2231 * If not, we don't really need to make a BlockJobTxn.
2232 */
2233 props = get_transaction_properties(props);
2234 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2235 block_job_txn = block_job_txn_new();
2236 }
2237
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002238 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002239 bdrv_drain_all();
2240
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002241 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002242 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002243 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002244 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002245
Jeff Cody8802d1f2012-02-28 15:54:06 -05002246 dev_info = dev_entry->value;
2247 dev_entry = dev_entry->next;
2248
Eric Blake6a8f9662015-10-26 16:34:54 -06002249 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002250
Eric Blake6a8f9662015-10-26 16:34:54 -06002251 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002252 assert(ops->instance_size > 0);
2253
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002254 state = g_malloc0(ops->instance_size);
2255 state->ops = ops;
2256 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002257 state->block_job_txn = block_job_txn;
2258 state->txn_props = props;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002259 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002260
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002261 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002262 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002263 error_propagate(errp, local_err);
2264 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002265 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002266 }
2267
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002268 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002269 if (state->ops->commit) {
2270 state->ops->commit(state);
2271 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002272 }
2273
2274 /* success */
2275 goto exit;
2276
2277delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002278 /* failure, and it is all-or-none; roll back all operations */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002279 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2280 if (state->ops->abort) {
2281 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002282 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002283 }
2284exit:
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002285 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2286 if (state->ops->clean) {
2287 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002288 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002289 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002290 }
John Snow94d16a62015-11-05 18:13:18 -05002291 if (!has_props) {
2292 qapi_free_TransactionProperties(props);
2293 }
2294 block_job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002295}
2296
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002297void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002298{
Max Reitz38f54bd2015-10-26 21:39:12 +01002299 Error *local_err = NULL;
John Snow3a3086b2016-05-18 17:53:40 -04002300 int rc;
Markus Armbruster666daa62010-06-02 18:48:27 +02002301
John Snow3a3086b2016-05-18 17:53:40 -04002302 if (!has_force) {
2303 force = false;
2304 }
2305
2306 rc = do_open_tray(device, force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002307 if (rc && rc != -ENOSYS) {
Max Reitz38f54bd2015-10-26 21:39:12 +01002308 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002309 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002310 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002311 error_free(local_err);
John Snow3a3086b2016-05-18 17:53:40 -04002312
Max Reitz6e0abc22015-12-11 16:23:05 +01002313 qmp_x_blockdev_remove_medium(device, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002314}
2315
Benoît Canet12d3ba82014-01-23 21:31:35 +01002316void qmp_block_passwd(bool has_device, const char *device,
2317 bool has_node_name, const char *node_name,
2318 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002319{
Benoît Canet12d3ba82014-01-23 21:31:35 +01002320 Error *local_err = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +02002321 BlockDriverState *bs;
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002322 AioContext *aio_context;
Markus Armbruster666daa62010-06-02 18:48:27 +02002323
Benoît Canet12d3ba82014-01-23 21:31:35 +01002324 bs = bdrv_lookup_bs(has_device ? device : NULL,
2325 has_node_name ? node_name : NULL,
2326 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002327 if (local_err) {
Benoît Canet12d3ba82014-01-23 21:31:35 +01002328 error_propagate(errp, local_err);
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02002329 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002330 }
2331
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002332 aio_context = bdrv_get_aio_context(bs);
2333 aio_context_acquire(aio_context);
2334
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002335 bdrv_add_key(bs, password, errp);
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002336
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002337 aio_context_release(aio_context);
Markus Armbruster666daa62010-06-02 18:48:27 +02002338}
2339
Colin Lordbf18bee2016-06-06 14:15:22 -04002340/*
2341 * Attempt to open the tray of @device.
2342 * If @force, ignore its tray lock.
2343 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2344 * On error, store an error through @errp and return -errno.
2345 * If @device does not exist, return -ENODEV.
2346 * If it has no removable media, return -ENOTSUP.
2347 * If it has no tray, return -ENOSYS.
2348 * If the guest was asked to open the tray, return -EINPROGRESS.
2349 * Else, return 0.
John Snow3a3086b2016-05-18 17:53:40 -04002350 */
2351static int do_open_tray(const char *device, bool force, Error **errp)
Max Reitz7d8a9f72015-10-26 21:39:08 +01002352{
2353 BlockBackend *blk;
2354 bool locked;
2355
Max Reitz7d8a9f72015-10-26 21:39:08 +01002356 blk = blk_by_name(device);
2357 if (!blk) {
2358 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2359 "Device '%s' not found", device);
John Snow3a3086b2016-05-18 17:53:40 -04002360 return -ENODEV;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002361 }
2362
2363 if (!blk_dev_has_removable_media(blk)) {
2364 error_setg(errp, "Device '%s' is not removable", device);
John Snow3a3086b2016-05-18 17:53:40 -04002365 return -ENOTSUP;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002366 }
2367
Max Reitz12c7ec82016-01-29 20:49:11 +01002368 if (!blk_dev_has_tray(blk)) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002369 error_setg(errp, "Device '%s' does not have a tray", device);
2370 return -ENOSYS;
Max Reitz12c7ec82016-01-29 20:49:11 +01002371 }
2372
Max Reitz7d8a9f72015-10-26 21:39:08 +01002373 if (blk_dev_is_tray_open(blk)) {
John Snow3a3086b2016-05-18 17:53:40 -04002374 return 0;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002375 }
2376
2377 locked = blk_dev_is_medium_locked(blk);
2378 if (locked) {
2379 blk_dev_eject_request(blk, force);
2380 }
2381
2382 if (!locked || force) {
2383 blk_dev_change_media_cb(blk, false);
2384 }
John Snow3a3086b2016-05-18 17:53:40 -04002385
2386 if (locked && !force) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002387 error_setg(errp, "Device '%s' is locked and force was not specified, "
2388 "wait for tray to open and try again", device);
2389 return -EINPROGRESS;
John Snow3a3086b2016-05-18 17:53:40 -04002390 }
2391
2392 return 0;
2393}
2394
2395void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
2396 Error **errp)
2397{
Colin Lordbf18bee2016-06-06 14:15:22 -04002398 Error *local_err = NULL;
2399 int rc;
2400
John Snow3a3086b2016-05-18 17:53:40 -04002401 if (!has_force) {
2402 force = false;
2403 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002404 rc = do_open_tray(device, force, &local_err);
2405 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2406 error_propagate(errp, local_err);
2407 return;
2408 }
2409 error_free(local_err);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002410}
2411
Max Reitzabaaf592015-10-26 21:39:09 +01002412void qmp_blockdev_close_tray(const char *device, Error **errp)
2413{
2414 BlockBackend *blk;
2415
2416 blk = blk_by_name(device);
2417 if (!blk) {
2418 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2419 "Device '%s' not found", device);
2420 return;
2421 }
2422
2423 if (!blk_dev_has_removable_media(blk)) {
2424 error_setg(errp, "Device '%s' is not removable", device);
2425 return;
2426 }
2427
Max Reitz12c7ec82016-01-29 20:49:11 +01002428 if (!blk_dev_has_tray(blk)) {
2429 /* Ignore this command on tray-less devices */
2430 return;
2431 }
2432
Max Reitzabaaf592015-10-26 21:39:09 +01002433 if (!blk_dev_is_tray_open(blk)) {
2434 return;
2435 }
2436
2437 blk_dev_change_media_cb(blk, true);
2438}
2439
Max Reitz6e0abc22015-12-11 16:23:05 +01002440void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
Max Reitz2814f672015-10-26 21:39:10 +01002441{
2442 BlockBackend *blk;
2443 BlockDriverState *bs;
2444 AioContext *aio_context;
2445 bool has_device;
2446
2447 blk = blk_by_name(device);
2448 if (!blk) {
2449 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2450 "Device '%s' not found", device);
2451 return;
2452 }
2453
2454 /* For BBs without a device, we can exchange the BDS tree at will */
2455 has_device = blk_get_attached_dev(blk);
2456
2457 if (has_device && !blk_dev_has_removable_media(blk)) {
2458 error_setg(errp, "Device '%s' is not removable", device);
2459 return;
2460 }
2461
Max Reitz12c7ec82016-01-29 20:49:11 +01002462 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Max Reitz2814f672015-10-26 21:39:10 +01002463 error_setg(errp, "Tray of device '%s' is not open", device);
2464 return;
2465 }
2466
2467 bs = blk_bs(blk);
2468 if (!bs) {
2469 return;
2470 }
2471
2472 aio_context = bdrv_get_aio_context(bs);
2473 aio_context_acquire(aio_context);
2474
2475 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2476 goto out;
2477 }
2478
Max Reitz2814f672015-10-26 21:39:10 +01002479 blk_remove_bs(blk);
2480
Max Reitz12c7ec82016-01-29 20:49:11 +01002481 if (!blk_dev_has_tray(blk)) {
2482 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2483 * called at all); therefore, the medium needs to be ejected here.
2484 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2485 * value passed here (i.e. false). */
2486 blk_dev_change_media_cb(blk, false);
2487 }
2488
Max Reitz2814f672015-10-26 21:39:10 +01002489out:
2490 aio_context_release(aio_context);
2491}
2492
Max Reitzd1299882015-10-26 21:39:11 +01002493static void qmp_blockdev_insert_anon_medium(const char *device,
2494 BlockDriverState *bs, Error **errp)
2495{
2496 BlockBackend *blk;
2497 bool has_device;
2498
2499 blk = blk_by_name(device);
2500 if (!blk) {
2501 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2502 "Device '%s' not found", device);
2503 return;
2504 }
2505
2506 /* For BBs without a device, we can exchange the BDS tree at will */
2507 has_device = blk_get_attached_dev(blk);
2508
2509 if (has_device && !blk_dev_has_removable_media(blk)) {
2510 error_setg(errp, "Device '%s' is not removable", device);
2511 return;
2512 }
2513
Max Reitz12c7ec82016-01-29 20:49:11 +01002514 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Max Reitzd1299882015-10-26 21:39:11 +01002515 error_setg(errp, "Tray of device '%s' is not open", device);
2516 return;
2517 }
2518
2519 if (blk_bs(blk)) {
2520 error_setg(errp, "There already is a medium in device '%s'", device);
2521 return;
2522 }
2523
2524 blk_insert_bs(blk, bs);
2525
Max Reitz12c7ec82016-01-29 20:49:11 +01002526 if (!blk_dev_has_tray(blk)) {
2527 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2528 * called at all); therefore, the medium needs to be pushed into the
2529 * slot here.
2530 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2531 * value passed here (i.e. true). */
2532 blk_dev_change_media_cb(blk, true);
2533 }
Max Reitzd1299882015-10-26 21:39:11 +01002534}
2535
Max Reitz6e0abc22015-12-11 16:23:05 +01002536void qmp_x_blockdev_insert_medium(const char *device, const char *node_name,
2537 Error **errp)
Max Reitzd1299882015-10-26 21:39:11 +01002538{
2539 BlockDriverState *bs;
2540
2541 bs = bdrv_find_node(node_name);
2542 if (!bs) {
2543 error_setg(errp, "Node '%s' not found", node_name);
2544 return;
2545 }
2546
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002547 if (bdrv_has_blk(bs)) {
Max Reitzd1299882015-10-26 21:39:11 +01002548 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002549 bdrv_get_parent_name(bs));
Max Reitzd1299882015-10-26 21:39:11 +01002550 return;
2551 }
2552
2553 qmp_blockdev_insert_anon_medium(device, bs, errp);
2554}
2555
Max Reitz24fb4132015-11-06 16:27:06 +01002556void qmp_blockdev_change_medium(const char *device, const char *filename,
2557 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002558 bool has_read_only,
2559 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002560 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002561{
2562 BlockBackend *blk;
2563 BlockDriverState *medium_bs = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002564 int bdrv_flags;
Colin Lord38a53d52016-06-08 13:56:28 -04002565 int rc;
Max Reitzde2c6c02015-10-26 21:39:13 +01002566 QDict *options = NULL;
2567 Error *err = NULL;
2568
2569 blk = blk_by_name(device);
2570 if (!blk) {
2571 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2572 "Device '%s' not found", device);
2573 goto fail;
2574 }
2575
2576 if (blk_bs(blk)) {
2577 blk_update_root_state(blk);
2578 }
2579
2580 bdrv_flags = blk_get_open_flags_from_root_state(blk);
Alyssa Milburn156abc22016-02-06 13:36:18 +00002581 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2582 BDRV_O_PROTOCOL);
Max Reitzde2c6c02015-10-26 21:39:13 +01002583
Max Reitz39ff43d2015-11-11 04:49:44 +01002584 if (!has_read_only) {
2585 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2586 }
2587
2588 switch (read_only) {
2589 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2590 break;
2591
2592 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2593 bdrv_flags &= ~BDRV_O_RDWR;
2594 break;
2595
2596 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2597 bdrv_flags |= BDRV_O_RDWR;
2598 break;
2599
2600 default:
2601 abort();
2602 }
2603
Max Reitz24fb4132015-11-06 16:27:06 +01002604 if (has_format) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002605 options = qdict_new();
2606 qdict_put(options, "driver", qstring_from_str(format));
2607 }
2608
Max Reitz5b363932016-05-17 16:41:31 +02002609 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2610 if (!medium_bs) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002611 goto fail;
2612 }
2613
Max Reitzde2c6c02015-10-26 21:39:13 +01002614 bdrv_add_key(medium_bs, NULL, &err);
2615 if (err) {
2616 error_propagate(errp, err);
2617 goto fail;
2618 }
2619
Colin Lord38a53d52016-06-08 13:56:28 -04002620 rc = do_open_tray(device, false, &err);
2621 if (rc && rc != -ENOSYS) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002622 error_propagate(errp, err);
2623 goto fail;
2624 }
Colin Lord38a53d52016-06-08 13:56:28 -04002625 error_free(err);
2626 err = NULL;
Max Reitzde2c6c02015-10-26 21:39:13 +01002627
Max Reitz6e0abc22015-12-11 16:23:05 +01002628 qmp_x_blockdev_remove_medium(device, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002629 if (err) {
2630 error_propagate(errp, err);
2631 goto fail;
2632 }
2633
2634 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2635 if (err) {
2636 error_propagate(errp, err);
2637 goto fail;
2638 }
2639
Kevin Wolfa5614992016-03-21 10:49:51 +01002640 blk_apply_root_state(blk, medium_bs);
2641
Max Reitzde2c6c02015-10-26 21:39:13 +01002642 qmp_blockdev_close_tray(device, errp);
2643
2644fail:
2645 /* If the medium has been inserted, the device has its own reference, so
2646 * ours must be relinquished; and if it has not been inserted successfully,
2647 * the reference must be relinquished anyway */
2648 bdrv_unref(medium_bs);
2649}
2650
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002651/* throttling disk I/O limits */
Eric Blake4dc93972016-07-13 21:50:21 -06002652void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002653{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002654 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002655 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002656 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002657 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002658
Eric Blake4dc93972016-07-13 21:50:21 -06002659 blk = blk_by_name(arg->device);
Fam Zhenga0e85442015-03-02 19:36:48 +08002660 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01002661 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Eric Blake4dc93972016-07-13 21:50:21 -06002662 "Device '%s' not found", arg->device);
Luiz Capitulino80047da2011-12-14 16:49:14 -02002663 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002664 }
Max Reitz5433c242015-10-19 17:53:29 +02002665
2666 aio_context = blk_get_aio_context(blk);
2667 aio_context_acquire(aio_context);
2668
Fam Zhenga0e85442015-03-02 19:36:48 +08002669 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002670 if (!bs) {
Eric Blake4dc93972016-07-13 21:50:21 -06002671 error_setg(errp, "Device '%s' has no medium", arg->device);
Max Reitz5433c242015-10-19 17:53:29 +02002672 goto out;
2673 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002674
Alberto Garcia1588ab52016-02-18 12:27:00 +02002675 throttle_config_init(&cfg);
Eric Blake4dc93972016-07-13 21:50:21 -06002676 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2677 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2678 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002679
Eric Blake4dc93972016-07-13 21:50:21 -06002680 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2681 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2682 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
Benoît Canetcc0681c2013-09-02 14:14:39 +02002683
Eric Blake4dc93972016-07-13 21:50:21 -06002684 if (arg->has_bps_max) {
2685 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002686 }
Eric Blake4dc93972016-07-13 21:50:21 -06002687 if (arg->has_bps_rd_max) {
2688 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002689 }
Eric Blake4dc93972016-07-13 21:50:21 -06002690 if (arg->has_bps_wr_max) {
2691 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002692 }
Eric Blake4dc93972016-07-13 21:50:21 -06002693 if (arg->has_iops_max) {
2694 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002695 }
Eric Blake4dc93972016-07-13 21:50:21 -06002696 if (arg->has_iops_rd_max) {
2697 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002698 }
Eric Blake4dc93972016-07-13 21:50:21 -06002699 if (arg->has_iops_wr_max) {
2700 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002701 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002702
Eric Blake4dc93972016-07-13 21:50:21 -06002703 if (arg->has_bps_max_length) {
2704 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002705 }
Eric Blake4dc93972016-07-13 21:50:21 -06002706 if (arg->has_bps_rd_max_length) {
2707 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002708 }
Eric Blake4dc93972016-07-13 21:50:21 -06002709 if (arg->has_bps_wr_max_length) {
2710 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002711 }
Eric Blake4dc93972016-07-13 21:50:21 -06002712 if (arg->has_iops_max_length) {
2713 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002714 }
Eric Blake4dc93972016-07-13 21:50:21 -06002715 if (arg->has_iops_rd_max_length) {
2716 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002717 }
Eric Blake4dc93972016-07-13 21:50:21 -06002718 if (arg->has_iops_wr_max_length) {
2719 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002720 }
2721
Eric Blake4dc93972016-07-13 21:50:21 -06002722 if (arg->has_iops_size) {
2723 cfg.op_size = arg->iops_size;
Benoît Canet2024c1d2013-09-02 14:14:41 +02002724 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002725
Alberto Garciad5851082016-02-18 12:26:59 +02002726 if (!throttle_is_valid(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002727 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002728 }
2729
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002730 if (throttle_enabled(&cfg)) {
2731 /* Enable I/O limits if they're not enabled yet, otherwise
2732 * just update the throttling group. */
Kevin Wolf97148072016-03-21 13:53:52 +01002733 if (!blk_get_public(blk)->throttle_state) {
Eric Blake4dc93972016-07-13 21:50:21 -06002734 blk_io_limits_enable(blk,
2735 arg->has_group ? arg->group : arg->device);
2736 } else if (arg->has_group) {
2737 blk_io_limits_update_group(blk, arg->group);
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002738 }
2739 /* Set the new throttling configuration */
Kevin Wolf97148072016-03-21 13:53:52 +01002740 blk_set_io_limits(blk, &cfg);
2741 } else if (blk_get_public(blk)->throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002742 /* If all throttling settings are set to 0, disable I/O limits */
Kevin Wolf97148072016-03-21 13:53:52 +01002743 blk_io_limits_disable(blk);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002744 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002745
Max Reitz5433c242015-10-19 17:53:29 +02002746out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002747 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002748}
2749
John Snow341ebc22015-04-17 19:49:52 -04002750void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2751 bool has_granularity, uint32_t granularity,
2752 Error **errp)
2753{
2754 AioContext *aio_context;
2755 BlockDriverState *bs;
2756
2757 if (!name || name[0] == '\0') {
2758 error_setg(errp, "Bitmap name cannot be empty");
2759 return;
2760 }
2761
2762 bs = bdrv_lookup_bs(node, node, errp);
2763 if (!bs) {
2764 return;
2765 }
2766
2767 aio_context = bdrv_get_aio_context(bs);
2768 aio_context_acquire(aio_context);
2769
2770 if (has_granularity) {
2771 if (granularity < 512 || !is_power_of_2(granularity)) {
2772 error_setg(errp, "Granularity must be power of 2 "
2773 "and at least 512");
2774 goto out;
2775 }
2776 } else {
2777 /* Default to cluster size, if available: */
2778 granularity = bdrv_get_default_bitmap_granularity(bs);
2779 }
2780
2781 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2782
2783 out:
2784 aio_context_release(aio_context);
2785}
2786
2787void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2788 Error **errp)
2789{
2790 AioContext *aio_context;
2791 BlockDriverState *bs;
2792 BdrvDirtyBitmap *bitmap;
2793
2794 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2795 if (!bitmap || !bs) {
2796 return;
2797 }
2798
John Snow9bd2b082015-04-17 19:49:57 -04002799 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2800 error_setg(errp,
2801 "Bitmap '%s' is currently frozen and cannot be removed",
2802 name);
2803 goto out;
2804 }
John Snow20dca812015-04-17 19:50:02 -04002805 bdrv_dirty_bitmap_make_anon(bitmap);
John Snow341ebc22015-04-17 19:49:52 -04002806 bdrv_release_dirty_bitmap(bs, bitmap);
2807
John Snow9bd2b082015-04-17 19:49:57 -04002808 out:
John Snow341ebc22015-04-17 19:49:52 -04002809 aio_context_release(aio_context);
2810}
2811
John Snowe74e6b72015-04-17 19:49:59 -04002812/**
2813 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2814 * immediately after a full backup operation.
2815 */
2816void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2817 Error **errp)
2818{
2819 AioContext *aio_context;
2820 BdrvDirtyBitmap *bitmap;
2821 BlockDriverState *bs;
2822
2823 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2824 if (!bitmap || !bs) {
2825 return;
2826 }
2827
2828 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2829 error_setg(errp,
2830 "Bitmap '%s' is currently frozen and cannot be modified",
2831 name);
2832 goto out;
2833 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2834 error_setg(errp,
2835 "Bitmap '%s' is currently disabled and cannot be cleared",
2836 name);
2837 goto out;
2838 }
2839
Fam Zhengdf9a6812015-11-09 18:16:54 +08002840 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04002841
2842 out:
2843 aio_context_release(aio_context);
2844}
2845
Markus Armbruster072ebe62015-03-05 17:00:56 +01002846void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06002847{
2848 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002849 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06002850 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002851 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08002852 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06002853
Kevin Wolf2073d412016-02-23 17:50:37 +01002854 bs = bdrv_find_node(id);
2855 if (bs) {
2856 qmp_x_blockdev_del(false, NULL, true, id, &local_err);
2857 if (local_err) {
2858 error_report_err(local_err);
2859 }
2860 return;
2861 }
2862
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002863 blk = blk_by_name(id);
2864 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02002865 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01002866 return;
Ryan Harper9063f812010-11-12 11:07:13 -06002867 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002868
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02002869 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02002870 error_report("Deleting device added with blockdev-add"
2871 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01002872 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02002873 }
2874
Max Reitz5433c242015-10-19 17:53:29 +02002875 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002876 aio_context_acquire(aio_context);
2877
Max Reitz5433c242015-10-19 17:53:29 +02002878 bs = blk_bs(blk);
2879 if (bs) {
2880 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2881 error_report_err(local_err);
2882 aio_context_release(aio_context);
2883 return;
2884 }
Ryan Harper9063f812010-11-12 11:07:13 -06002885
Max Reitz938abd42016-01-29 16:36:09 +01002886 blk_remove_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002887 }
Ryan Harper9063f812010-11-12 11:07:13 -06002888
Max Reitz7c735872016-03-16 19:54:39 +01002889 /* Make the BlockBackend and the attached BlockDriverState anonymous */
Max Reitzefaa7c42016-03-16 19:54:38 +01002890 monitor_remove_blk(blk);
2891
Max Reitz7c735872016-03-16 19:54:39 +01002892 /* If this BlockBackend has a device attached to it, its refcount will be
2893 * decremented when the device is removed; otherwise we have to do so here.
Ryan Harperd22b2f42011-03-29 20:51:47 -05002894 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02002895 if (blk_get_attached_dev(blk)) {
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02002896 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02002897 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2898 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05002899 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02002900 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06002901 }
2902
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002903 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06002904}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002905
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002906void qmp_block_resize(bool has_device, const char *device,
2907 bool has_node_name, const char *node_name,
2908 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002909{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002910 Error *local_err = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002911 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002912 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02002913 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002914
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002915 bs = bdrv_lookup_bs(has_device ? device : NULL,
2916 has_node_name ? node_name : NULL,
2917 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002918 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002919 error_propagate(errp, local_err);
2920 return;
2921 }
2922
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002923 aio_context = bdrv_get_aio_context(bs);
2924 aio_context_acquire(aio_context);
2925
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002926 if (!bdrv_is_first_non_filter(bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002927 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002928 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002929 }
2930
2931 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002932 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002933 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002934 }
2935
Jeff Cody9c75e162014-06-25 16:55:30 -04002936 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002937 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002938 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04002939 }
2940
Peter Lieven92b7a082013-03-11 11:04:24 +01002941 /* complete all in-flight operations before resizing the device */
2942 bdrv_drain_all();
2943
Kevin Wolf87329012013-05-02 15:32:55 +02002944 ret = bdrv_truncate(bs, size);
2945 switch (ret) {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002946 case 0:
2947 break;
2948 case -ENOMEDIUM:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002949 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002950 break;
2951 case -ENOTSUP:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002952 error_setg(errp, QERR_UNSUPPORTED);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002953 break;
2954 case -EACCES:
Alberto Garcia81e5f782015-04-08 12:29:19 +03002955 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002956 break;
2957 case -EBUSY:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002958 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002959 break;
2960 default:
Kevin Wolf87329012013-05-02 15:32:55 +02002961 error_setg_errno(errp, -ret, "Could not resize");
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002962 break;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002963 }
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002964
2965out:
2966 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002967}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002968
Jeff Cody9abf2db2012-09-27 13:29:14 -04002969static void block_job_cb(void *opaque, int ret)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002970{
Stefan Hajnoczi723c5d92014-10-21 12:03:53 +01002971 /* Note that this function may be executed from another AioContext besides
2972 * the QEMU main loop. If you need to access anything that assumes the
2973 * QEMU global mutex, use a BH or introduce a mutex.
2974 */
2975
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002976 BlockDriverState *bs = opaque;
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002977 const char *msg = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002978
Jeff Cody9abf2db2012-09-27 13:29:14 -04002979 trace_block_job_cb(bs, bs->job, ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002980
2981 assert(bs->job);
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002982
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002983 if (ret < 0) {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002984 msg = strerror(-ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002985 }
2986
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002987 if (block_job_is_cancelled(bs->job)) {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002988 block_job_event_cancelled(bs->job);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002989 } else {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002990 block_job_event_completed(bs->job, msg);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002991 }
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002992}
2993
Alberto Garcia23233222016-07-05 17:28:59 +03002994void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002995 bool has_base, const char *base,
2996 bool has_backing_file, const char *backing_file,
2997 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002998 bool has_on_error, BlockdevOnError on_error,
2999 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003000{
3001 BlockDriverState *bs;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003002 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003003 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003004 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003005 const char *base_name = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003006
Paolo Bonzini1d809092012-09-28 17:22:59 +02003007 if (!has_on_error) {
3008 on_error = BLOCKDEV_ON_ERROR_REPORT;
3009 }
3010
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003011 bs = qmp_get_root_bs(device, errp);
3012 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003013 return;
3014 }
3015
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003016 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003017 aio_context_acquire(aio_context);
3018
Fam Zheng628ff682014-05-23 21:29:44 +08003019 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003020 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003021 }
3022
Jeff Cody13d8cc52014-06-25 15:40:11 -04003023 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003024 base_bs = bdrv_find_backing_image(bs, base);
3025 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003026 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003027 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003028 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003029 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04003030 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003031 }
3032
Jeff Cody13d8cc52014-06-25 15:40:11 -04003033 /* if we are streaming the entire chain, the result will have no backing
3034 * file, and specifying one is therefore an error */
3035 if (base_bs == NULL && has_backing_file) {
3036 error_setg(errp, "backing file specified, but streaming the "
3037 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003038 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003039 }
3040
3041 /* backing_file string overrides base bs filename */
3042 base_name = has_backing_file ? backing_file : base_name;
3043
Alberto Garcia23233222016-07-05 17:28:59 +03003044 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
3045 has_speed ? speed : 0, on_error, block_job_cb, bs, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003046 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003047 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003048 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003049 }
3050
3051 trace_qmp_block_stream(bs, bs->job);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003052
3053out:
3054 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003055}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003056
Alberto Garciafd62c602016-07-05 17:29:00 +03003057void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003058 bool has_base, const char *base,
3059 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003060 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04003061 bool has_speed, int64_t speed,
3062 Error **errp)
3063{
3064 BlockDriverState *bs;
3065 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003066 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04003067 Error *local_err = NULL;
3068 /* This will be part of the QMP command, if/when the
3069 * BlockdevOnError change for blkmirror makes it in
3070 */
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02003071 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
Jeff Codyed61fc12012-09-27 13:29:16 -04003072
Max Reitz54504662014-04-10 19:36:25 +02003073 if (!has_speed) {
3074 speed = 0;
3075 }
3076
Jeff Cody7676e2c2014-06-30 15:14:15 +02003077 /* Important Note:
3078 * libvirt relies on the DeviceNotFound error class in order to probe for
3079 * live commit feature versions; for this to work, we must make sure to
3080 * perform the device lookup before any generic errors that may occur in a
3081 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02003082 bs = qmp_get_root_bs(device, &local_err);
3083 if (!bs) {
3084 bs = bdrv_lookup_bs(device, device, NULL);
3085 if (!bs) {
3086 error_free(local_err);
3087 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3088 "Device '%s' not found", device);
3089 } else {
3090 error_propagate(errp, local_err);
3091 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003092 return;
3093 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003094
Kevin Wolf1d13b162016-06-23 14:20:24 +02003095 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003096 aio_context_acquire(aio_context);
3097
Fam Zhengbb000212014-09-11 13:14:00 +08003098 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003099 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003100 }
3101
Jeff Codyed61fc12012-09-27 13:29:16 -04003102 /* default top_bs is the active layer */
3103 top_bs = bs;
3104
Jeff Cody7676e2c2014-06-30 15:14:15 +02003105 if (has_top && top) {
Jeff Codyed61fc12012-09-27 13:29:16 -04003106 if (strcmp(bs->filename, top) != 0) {
3107 top_bs = bdrv_find_backing_image(bs, top);
3108 }
3109 }
3110
3111 if (top_bs == NULL) {
3112 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003113 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003114 }
3115
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003116 assert(bdrv_get_aio_context(top_bs) == aio_context);
3117
Jeff Codyd5208c42012-10-16 15:49:10 -04003118 if (has_base && base) {
3119 base_bs = bdrv_find_backing_image(top_bs, base);
3120 } else {
3121 base_bs = bdrv_find_base(top_bs);
3122 }
3123
3124 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003125 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003126 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04003127 }
3128
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003129 assert(bdrv_get_aio_context(base_bs) == aio_context);
3130
Fam Zhengbb000212014-09-11 13:14:00 +08003131 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3132 goto out;
3133 }
3134
Jeff Cody7676e2c2014-06-30 15:14:15 +02003135 /* Do not allow attempts to commit an image into itself */
3136 if (top_bs == base_bs) {
3137 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003138 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02003139 }
3140
Fam Zheng20a63d22013-12-16 14:45:31 +08003141 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04003142 if (has_backing_file) {
3143 error_setg(errp, "'backing-file' specified,"
3144 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003145 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003146 }
Alberto Garciafd62c602016-07-05 17:29:00 +03003147 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, speed,
3148 on_error, block_job_cb, bs, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003149 } else {
Alberto Garciafd62c602016-07-05 17:29:00 +03003150 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed,
3151 on_error, block_job_cb, bs,
Jeff Cody54e26902014-06-25 15:40:10 -04003152 has_backing_file ? backing_file : NULL, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003153 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003154 if (local_err != NULL) {
3155 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003156 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003157 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003158
3159out:
3160 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003161}
3162
Alberto Garcia70559d42016-07-05 17:28:58 +03003163static void do_drive_backup(const char *job_id, const char *device,
3164 const char *target, bool has_format,
3165 const char *format, enum MirrorSyncMode sync,
John Snow78f51fd2015-11-05 18:13:17 -05003166 bool has_mode, enum NewImageMode mode,
3167 bool has_speed, int64_t speed,
3168 bool has_bitmap, const char *bitmap,
3169 bool has_on_source_error,
3170 BlockdevOnError on_source_error,
3171 bool has_on_target_error,
3172 BlockdevOnError on_target_error,
3173 BlockJobTxn *txn, Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003174{
Fam Zhenga0e85442015-03-02 19:36:48 +08003175 BlockBackend *blk;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003176 BlockDriverState *bs;
3177 BlockDriverState *target_bs;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003178 BlockDriverState *source = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003179 BdrvDirtyBitmap *bmap = NULL;
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003180 AioContext *aio_context;
Max Reitze6641712015-08-26 19:47:48 +02003181 QDict *options = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003182 Error *local_err = NULL;
3183 int flags;
3184 int64_t size;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003185
3186 if (!has_speed) {
3187 speed = 0;
3188 }
3189 if (!has_on_source_error) {
3190 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3191 }
3192 if (!has_on_target_error) {
3193 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3194 }
3195 if (!has_mode) {
3196 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3197 }
3198
Fam Zhenga0e85442015-03-02 19:36:48 +08003199 blk = blk_by_name(device);
3200 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01003201 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3202 "Device '%s' not found", device);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003203 return;
3204 }
3205
Max Reitz5433c242015-10-19 17:53:29 +02003206 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003207 aio_context_acquire(aio_context);
3208
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003209 /* Although backup_run has this check too, we need to use bs->drv below, so
3210 * do an early check redundantly. */
Max Reitz5433c242015-10-19 17:53:29 +02003211 if (!blk_is_available(blk)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003212 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003213 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003214 }
Max Reitz5433c242015-10-19 17:53:29 +02003215 bs = blk_bs(blk);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003216
3217 if (!has_format) {
3218 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3219 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003220
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003221 /* Early check to avoid creating target */
Fam Zheng3718d8a2014-05-23 21:29:43 +08003222 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003223 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003224 }
3225
Kevin Wolf61de4c62016-03-18 17:46:45 +01003226 flags = bs->open_flags | BDRV_O_RDWR;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003227
Ian Mainfc5d3f82013-07-26 11:39:04 -07003228 /* See if we have a backing HD we can use to create our new image
3229 * on top of. */
3230 if (sync == MIRROR_SYNC_MODE_TOP) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003231 source = backing_bs(bs);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003232 if (!source) {
3233 sync = MIRROR_SYNC_MODE_FULL;
3234 }
3235 }
3236 if (sync == MIRROR_SYNC_MODE_NONE) {
3237 source = bs;
3238 }
3239
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003240 size = bdrv_getlength(bs);
3241 if (size < 0) {
3242 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003243 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003244 }
3245
3246 if (mode != NEW_IMAGE_MODE_EXISTING) {
Max Reitze6641712015-08-26 19:47:48 +02003247 assert(format);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003248 if (source) {
3249 bdrv_img_create(target, format, source->filename,
3250 source->drv->format_name, NULL,
3251 size, flags, &local_err, false);
3252 } else {
3253 bdrv_img_create(target, format, NULL, NULL, NULL,
3254 size, flags, &local_err, false);
3255 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003256 }
3257
Markus Armbruster84d18f02014-01-30 15:07:28 +01003258 if (local_err) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003259 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003260 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003261 }
3262
Max Reitze6641712015-08-26 19:47:48 +02003263 if (format) {
3264 options = qdict_new();
3265 qdict_put(options, "driver", qstring_from_str(format));
3266 }
3267
Max Reitz5b363932016-05-17 16:41:31 +02003268 target_bs = bdrv_open(target, NULL, options, flags, errp);
3269 if (!target_bs) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003270 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003271 }
3272
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003273 bdrv_set_aio_context(target_bs, aio_context);
3274
John Snowd58d8452015-04-17 19:49:58 -04003275 if (has_bitmap) {
3276 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3277 if (!bmap) {
3278 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
Max Reitz0702d3d2015-11-09 23:39:10 +01003279 bdrv_unref(target_bs);
John Snowd58d8452015-04-17 19:49:58 -04003280 goto out;
3281 }
3282 }
3283
Alberto Garcia70559d42016-07-05 17:28:58 +03003284 backup_start(job_id, bs, target_bs, speed, sync, bmap,
John Snowd58d8452015-04-17 19:49:58 -04003285 on_source_error, on_target_error,
John Snow78f51fd2015-11-05 18:13:17 -05003286 block_job_cb, bs, txn, &local_err);
Kevin Wolf5c438bc2016-04-14 13:09:53 +02003287 bdrv_unref(target_bs);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003288 if (local_err != NULL) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003289 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003290 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003291 }
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003292
3293out:
3294 aio_context_release(aio_context);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003295}
3296
Alberto Garcia70559d42016-07-05 17:28:58 +03003297void qmp_drive_backup(bool has_job_id, const char *job_id,
3298 const char *device, const char *target,
John Snow78f51fd2015-11-05 18:13:17 -05003299 bool has_format, const char *format,
3300 enum MirrorSyncMode sync,
3301 bool has_mode, enum NewImageMode mode,
3302 bool has_speed, int64_t speed,
3303 bool has_bitmap, const char *bitmap,
3304 bool has_on_source_error, BlockdevOnError on_source_error,
3305 bool has_on_target_error, BlockdevOnError on_target_error,
3306 Error **errp)
3307{
Alberto Garcia70559d42016-07-05 17:28:58 +03003308 return do_drive_backup(has_job_id ? job_id : NULL, device, target,
3309 has_format, format, sync,
John Snow78f51fd2015-11-05 18:13:17 -05003310 has_mode, mode, has_speed, speed,
3311 has_bitmap, bitmap,
3312 has_on_source_error, on_source_error,
3313 has_on_target_error, on_target_error,
3314 NULL, errp);
3315}
3316
Benoît Canetc13163f2014-01-23 21:31:34 +01003317BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3318{
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003319 return bdrv_named_nodes_list(errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003320}
3321
Alberto Garcia70559d42016-07-05 17:28:58 +03003322void do_blockdev_backup(const char *job_id, const char *device,
3323 const char *target, enum MirrorSyncMode sync,
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003324 bool has_speed, int64_t speed,
3325 bool has_on_source_error,
3326 BlockdevOnError on_source_error,
3327 bool has_on_target_error,
3328 BlockdevOnError on_target_error,
John Snow78f51fd2015-11-05 18:13:17 -05003329 BlockJobTxn *txn, Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003330{
3331 BlockDriverState *bs;
3332 BlockDriverState *target_bs;
3333 Error *local_err = NULL;
3334 AioContext *aio_context;
3335
3336 if (!has_speed) {
3337 speed = 0;
3338 }
3339 if (!has_on_source_error) {
3340 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3341 }
3342 if (!has_on_target_error) {
3343 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3344 }
3345
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003346 bs = qmp_get_root_bs(device, errp);
3347 if (!bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003348 return;
3349 }
3350
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003351 aio_context = bdrv_get_aio_context(bs);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003352 aio_context_acquire(aio_context);
3353
Fam Zheng0d978912016-05-23 10:19:35 +08003354 target_bs = bdrv_lookup_bs(target, target, errp);
3355 if (!target_bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003356 goto out;
3357 }
Max Reitz5433c242015-10-19 17:53:29 +02003358
Fam Zhengefd75562016-05-23 10:19:36 +08003359 if (bdrv_get_aio_context(target_bs) != aio_context) {
3360 if (!bdrv_has_blk(target_bs)) {
3361 /* The target BDS is not attached, we can safely move it to another
3362 * AioContext. */
3363 bdrv_set_aio_context(target_bs, aio_context);
3364 } else {
3365 error_setg(errp, "Target is attached to a different thread from "
3366 "source.");
3367 goto out;
3368 }
3369 }
Alberto Garcia70559d42016-07-05 17:28:58 +03003370 backup_start(job_id, bs, target_bs, speed, sync, NULL, on_source_error,
John Snow78f51fd2015-11-05 18:13:17 -05003371 on_target_error, block_job_cb, bs, txn, &local_err);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003372 if (local_err != NULL) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003373 error_propagate(errp, local_err);
3374 }
3375out:
3376 aio_context_release(aio_context);
3377}
3378
Alberto Garcia70559d42016-07-05 17:28:58 +03003379void qmp_blockdev_backup(bool has_job_id, const char *job_id,
3380 const char *device, const char *target,
John Snow78f51fd2015-11-05 18:13:17 -05003381 enum MirrorSyncMode sync,
3382 bool has_speed, int64_t speed,
3383 bool has_on_source_error,
3384 BlockdevOnError on_source_error,
3385 bool has_on_target_error,
3386 BlockdevOnError on_target_error,
3387 Error **errp)
3388{
Alberto Garcia70559d42016-07-05 17:28:58 +03003389 do_blockdev_backup(has_job_id ? job_id : NULL, device, target,
3390 sync, has_speed, speed,
John Snow78f51fd2015-11-05 18:13:17 -05003391 has_on_source_error, on_source_error,
3392 has_on_target_error, on_target_error,
3393 NULL, errp);
3394}
3395
Fam Zheng4193cdd2015-12-24 12:45:03 +08003396/* Parameter check and block job starting for drive mirroring.
3397 * Caller should hold @device and @target's aio context (must be the same).
3398 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003399static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003400 BlockDriverState *target,
3401 bool has_replaces, const char *replaces,
3402 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003403 BlockMirrorBackingMode backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003404 bool has_speed, int64_t speed,
3405 bool has_granularity, uint32_t granularity,
3406 bool has_buf_size, int64_t buf_size,
3407 bool has_on_source_error,
3408 BlockdevOnError on_source_error,
3409 bool has_on_target_error,
3410 BlockdevOnError on_target_error,
3411 bool has_unmap, bool unmap,
3412 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003413{
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003414
3415 if (!has_speed) {
3416 speed = 0;
3417 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003418 if (!has_on_source_error) {
3419 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3420 }
3421 if (!has_on_target_error) {
3422 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3423 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003424 if (!has_granularity) {
3425 granularity = 0;
3426 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003427 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003428 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003429 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003430 if (!has_unmap) {
3431 unmap = true;
3432 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003433
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003434 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003435 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3436 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003437 return;
3438 }
3439 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003440 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3441 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003442 return;
3443 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003444
Fam Zheng4193cdd2015-12-24 12:45:03 +08003445 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3446 return;
3447 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003448 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3449 return;
3450 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003451
3452 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3453 sync = MIRROR_SYNC_MODE_FULL;
3454 }
3455
3456 /* pass the node name to replace to mirror start since it's loose coupling
3457 * and will allow to check whether the node still exist at mirror completion
3458 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003459 mirror_start(job_id, bs, target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003460 has_replaces ? replaces : NULL,
Max Reitz274fcce2016-06-10 20:57:47 +02003461 speed, granularity, buf_size, sync, backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003462 on_source_error, on_target_error, unmap,
3463 block_job_cb, bs, errp);
3464}
3465
Eric Blakefaecd402016-07-14 16:37:58 -06003466void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003467{
3468 BlockDriverState *bs;
3469 BlockBackend *blk;
3470 BlockDriverState *source, *target_bs;
3471 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003472 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003473 Error *local_err = NULL;
3474 QDict *options = NULL;
3475 int flags;
3476 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003477 const char *format = arg->format;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003478
Eric Blakefaecd402016-07-14 16:37:58 -06003479 blk = blk_by_name(arg->device);
Fam Zhenga0e85442015-03-02 19:36:48 +08003480 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01003481 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Eric Blakefaecd402016-07-14 16:37:58 -06003482 "Device '%s' not found", arg->device);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003483 return;
3484 }
3485
Max Reitz5433c242015-10-19 17:53:29 +02003486 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003487 aio_context_acquire(aio_context);
3488
Max Reitz5433c242015-10-19 17:53:29 +02003489 if (!blk_is_available(blk)) {
Eric Blakefaecd402016-07-14 16:37:58 -06003490 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, arg->device);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003491 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003492 }
Max Reitz5433c242015-10-19 17:53:29 +02003493 bs = blk_bs(blk);
Eric Blakefaecd402016-07-14 16:37:58 -06003494 if (!arg->has_mode) {
3495 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003496 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003497
Eric Blakefaecd402016-07-14 16:37:58 -06003498 if (!arg->has_format) {
3499 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3500 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003501 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003502
Kevin Wolf61de4c62016-03-18 17:46:45 +01003503 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003504 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003505 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3506 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003507 }
Eric Blakefaecd402016-07-14 16:37:58 -06003508 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003509 source = bs;
3510 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003511
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003512 size = bdrv_getlength(bs);
3513 if (size < 0) {
3514 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003515 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003516 }
3517
Eric Blakefaecd402016-07-14 16:37:58 -06003518 if (arg->has_replaces) {
Benoît Canet09158f02014-06-27 18:25:25 +02003519 BlockDriverState *to_replace_bs;
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003520 AioContext *replace_aio_context;
3521 int64_t replace_size;
Benoît Canet09158f02014-06-27 18:25:25 +02003522
Eric Blakefaecd402016-07-14 16:37:58 -06003523 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003524 error_setg(errp, "a node-name must be provided when replacing a"
3525 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003526 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003527 }
3528
Eric Blakefaecd402016-07-14 16:37:58 -06003529 to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err);
Benoît Canet09158f02014-06-27 18:25:25 +02003530
3531 if (!to_replace_bs) {
3532 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003533 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003534 }
3535
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003536 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3537 aio_context_acquire(replace_aio_context);
3538 replace_size = bdrv_getlength(to_replace_bs);
3539 aio_context_release(replace_aio_context);
3540
3541 if (size != replace_size) {
Benoît Canet09158f02014-06-27 18:25:25 +02003542 error_setg(errp, "cannot replace image with a mirror image of "
3543 "different size");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003544 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003545 }
3546 }
3547
Eric Blakefaecd402016-07-14 16:37:58 -06003548 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003549 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3550 } else {
3551 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3552 }
3553
Eric Blakefaecd402016-07-14 16:37:58 -06003554 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3555 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003556 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003557 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003558 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003559 bdrv_img_create(arg->target, format,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003560 NULL, NULL, NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003561 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003562 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003563 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003564 break;
3565 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3566 /* create new image with backing file */
Eric Blakefaecd402016-07-14 16:37:58 -06003567 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003568 source->filename,
3569 source->drv->format_name,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003570 NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003571 break;
3572 default:
3573 abort();
3574 }
3575 }
3576
Markus Armbruster84d18f02014-01-30 15:07:28 +01003577 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003578 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003579 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003580 }
3581
Max Reitze6641712015-08-26 19:47:48 +02003582 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003583 if (arg->has_node_name) {
3584 qdict_put(options, "node-name", qstring_from_str(arg->node_name));
Benoît Canet4c828dc2014-06-16 12:00:55 +02003585 }
Max Reitze6641712015-08-26 19:47:48 +02003586 if (format) {
3587 qdict_put(options, "driver", qstring_from_str(format));
3588 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003589
Paolo Bonzinib812f672013-01-21 17:09:43 +01003590 /* Mirroring takes care of copy-on-write using the source's backing
3591 * file.
3592 */
Eric Blakefaecd402016-07-14 16:37:58 -06003593 target_bs = bdrv_open(arg->target, NULL, options,
3594 flags | BDRV_O_NO_BACKING, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003595 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003596 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003597 }
3598
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003599 bdrv_set_aio_context(target_bs, aio_context);
3600
Eric Blakefaecd402016-07-14 16:37:58 -06003601 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3602 arg->has_replaces, arg->replaces, arg->sync,
3603 backing_mode, arg->has_speed, arg->speed,
3604 arg->has_granularity, arg->granularity,
3605 arg->has_buf_size, arg->buf_size,
3606 arg->has_on_source_error, arg->on_source_error,
3607 arg->has_on_target_error, arg->on_target_error,
3608 arg->has_unmap, arg->unmap,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003609 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02003610 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003611 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003612out:
3613 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003614}
3615
Alberto Garcia71aa9862016-07-05 17:28:57 +03003616void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3617 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08003618 bool has_replaces, const char *replaces,
3619 MirrorSyncMode sync,
3620 bool has_speed, int64_t speed,
3621 bool has_granularity, uint32_t granularity,
3622 bool has_buf_size, int64_t buf_size,
3623 bool has_on_source_error,
3624 BlockdevOnError on_source_error,
3625 bool has_on_target_error,
3626 BlockdevOnError on_target_error,
3627 Error **errp)
3628{
3629 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08003630 BlockDriverState *target_bs;
3631 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003632 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08003633 Error *local_err = NULL;
3634
Kevin Wolf07eec652016-06-23 14:20:24 +02003635 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08003636 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08003637 return;
3638 }
3639
3640 target_bs = bdrv_lookup_bs(target, target, errp);
3641 if (!target_bs) {
3642 return;
3643 }
3644
3645 aio_context = bdrv_get_aio_context(bs);
3646 aio_context_acquire(aio_context);
3647
Fam Zhengdf925622015-12-24 12:45:05 +08003648 bdrv_set_aio_context(target_bs, aio_context);
3649
Alberto Garcia71aa9862016-07-05 17:28:57 +03003650 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02003651 has_replaces, replaces, sync, backing_mode,
Fam Zhengdf925622015-12-24 12:45:05 +08003652 has_speed, speed,
3653 has_granularity, granularity,
3654 has_buf_size, buf_size,
3655 has_on_source_error, on_source_error,
3656 has_on_target_error, on_target_error,
3657 true, true,
3658 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003659 error_propagate(errp, local_err);
Fam Zhengdf925622015-12-24 12:45:05 +08003660
3661 aio_context_release(aio_context);
3662}
3663
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003664/* Get a block job using its ID and acquire its AioContext */
3665static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003666 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003667{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003668 BlockJob *job;
3669
3670 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003671
Max Reitz5433c242015-10-19 17:53:29 +02003672 *aio_context = NULL;
3673
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003674 job = block_job_get(id);
3675
3676 if (!job) {
3677 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3678 "Block job '%s' not found", id);
3679 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003680 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003681
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003682 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003683 aio_context_acquire(*aio_context);
3684
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003685 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003686}
3687
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003688void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003689{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003690 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003691 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003692
3693 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003694 return;
3695 }
3696
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003697 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003698 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003699}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003700
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003701void qmp_block_job_cancel(const char *device,
3702 bool has_force, bool force, Error **errp)
3703{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003704 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003705 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003706
3707 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003708 return;
3709 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003710
3711 if (!has_force) {
3712 force = false;
3713 }
3714
Fam Zheng751ebd72015-04-03 22:05:18 +08003715 if (job->user_paused && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003716 error_setg(errp, "The block job for device '%s' is currently paused",
3717 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003718 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003719 }
3720
3721 trace_qmp_block_job_cancel(job);
3722 block_job_cancel(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003723out:
3724 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003725}
3726
3727void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003728{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003729 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003730 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003731
Fam Zheng751ebd72015-04-03 22:05:18 +08003732 if (!job || job->user_paused) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003733 return;
3734 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003735
Fam Zheng751ebd72015-04-03 22:05:18 +08003736 job->user_paused = true;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003737 trace_qmp_block_job_pause(job);
3738 block_job_pause(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003739 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003740}
3741
3742void qmp_block_job_resume(const char *device, Error **errp)
3743{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003744 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003745 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003746
Fam Zheng751ebd72015-04-03 22:05:18 +08003747 if (!job || !job->user_paused) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02003748 return;
3749 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003750
Fam Zheng751ebd72015-04-03 22:05:18 +08003751 job->user_paused = false;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003752 trace_qmp_block_job_resume(job);
Stefan Hajnoczi17bd51f2016-06-16 17:56:22 +01003753 block_job_iostatus_reset(job);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003754 block_job_resume(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003755 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003756}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003757
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003758void qmp_block_job_complete(const char *device, Error **errp)
3759{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003760 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003761 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003762
3763 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003764 return;
3765 }
3766
3767 trace_qmp_block_job_complete(job);
3768 block_job_complete(job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003769 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003770}
3771
Jeff Codyfa40e652014-07-01 09:52:16 +02003772void qmp_change_backing_file(const char *device,
3773 const char *image_node_name,
3774 const char *backing_file,
3775 Error **errp)
3776{
Fam Zhenga0e85442015-03-02 19:36:48 +08003777 BlockBackend *blk;
Jeff Codyfa40e652014-07-01 09:52:16 +02003778 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003779 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02003780 BlockDriverState *image_bs = NULL;
3781 Error *local_err = NULL;
3782 bool ro;
3783 int open_flags;
3784 int ret;
3785
Fam Zhenga0e85442015-03-02 19:36:48 +08003786 blk = blk_by_name(device);
3787 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01003788 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3789 "Device '%s' not found", device);
Jeff Codyfa40e652014-07-01 09:52:16 +02003790 return;
3791 }
3792
Max Reitz5433c242015-10-19 17:53:29 +02003793 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003794 aio_context_acquire(aio_context);
3795
Max Reitz5433c242015-10-19 17:53:29 +02003796 if (!blk_is_available(blk)) {
3797 error_setg(errp, "Device '%s' has no medium", device);
3798 goto out;
3799 }
3800 bs = blk_bs(blk);
3801
Jeff Codyfa40e652014-07-01 09:52:16 +02003802 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3803 if (local_err) {
3804 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003805 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003806 }
3807
3808 if (!image_bs) {
3809 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003810 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003811 }
3812
3813 if (bdrv_find_base(image_bs) == image_bs) {
3814 error_setg(errp, "not allowing backing file change on an image "
3815 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003816 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003817 }
3818
3819 /* even though we are not necessarily operating on bs, we need it to
3820 * determine if block ops are currently prohibited on the chain */
3821 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003822 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003823 }
3824
3825 /* final sanity check */
3826 if (!bdrv_chain_contains(bs, image_bs)) {
3827 error_setg(errp, "'%s' and image file are not in the same chain",
3828 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003829 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003830 }
3831
3832 /* if not r/w, reopen to make r/w */
3833 open_flags = image_bs->open_flags;
3834 ro = bdrv_is_read_only(image_bs);
3835
3836 if (ro) {
3837 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3838 if (local_err) {
3839 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003840 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003841 }
3842 }
3843
3844 ret = bdrv_change_backing_file(image_bs, backing_file,
3845 image_bs->drv ? image_bs->drv->format_name : "");
3846
3847 if (ret < 0) {
3848 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3849 backing_file);
3850 /* don't exit here, so we can try to restore open flags if
3851 * appropriate */
3852 }
3853
3854 if (ro) {
3855 bdrv_reopen(image_bs, open_flags, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003856 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02003857 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003858
3859out:
3860 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02003861}
3862
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003863void hmp_drive_add_node(Monitor *mon, const char *optstr)
3864{
3865 QemuOpts *opts;
3866 QDict *qdict;
3867 Error *local_err = NULL;
3868
3869 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
3870 if (!opts) {
3871 return;
3872 }
3873
3874 qdict = qemu_opts_to_qdict(opts, NULL);
3875
3876 if (!qdict_get_try_str(qdict, "node-name")) {
Kevin Wolff8746fb2016-03-16 11:14:31 +01003877 QDECREF(qdict);
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003878 error_report("'node-name' needs to be specified");
3879 goto out;
3880 }
3881
3882 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
3883 if (!bs) {
3884 error_report_err(local_err);
3885 goto out;
3886 }
3887
3888 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3889
3890out:
3891 qemu_opts_del(opts);
3892}
3893
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003894void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3895{
Max Reitzbe4b67b2015-10-19 17:53:09 +02003896 BlockDriverState *bs;
3897 BlockBackend *blk = NULL;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003898 QObject *obj;
Eric Blake3b098d52016-06-09 10:48:43 -06003899 Visitor *v = qmp_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003900 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003901 Error *local_err = NULL;
3902
Markus Armbruster60e19e02014-06-06 14:50:58 +02003903 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003904 * cache.direct=false instead of silently switching to aio=threads, except
Markus Armbruster60e19e02014-06-06 14:50:58 +02003905 * when called from drive_new().
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003906 *
3907 * For now, simply forbidding the combination for all drivers will do. */
3908 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
Kevin Wolfc6e0bd92014-03-06 15:47:32 +01003909 bool direct = options->has_cache &&
3910 options->cache->has_direct &&
3911 options->cache->direct;
3912 if (!direct) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003913 error_setg(errp, "aio=native requires cache.direct=true");
3914 goto fail;
3915 }
3916 }
3917
Eric Blake3b098d52016-06-09 10:48:43 -06003918 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003919 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003920 error_propagate(errp, local_err);
3921 goto fail;
3922 }
3923
Eric Blake3b098d52016-06-09 10:48:43 -06003924 visit_complete(v, &obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003925 qdict = qobject_to_qdict(obj);
3926
3927 qdict_flatten(qdict);
3928
Max Reitzbe4b67b2015-10-19 17:53:09 +02003929 if (options->has_id) {
3930 blk = blockdev_init(NULL, qdict, &local_err);
3931 if (local_err) {
3932 error_propagate(errp, local_err);
3933 goto fail;
3934 }
3935
3936 bs = blk_bs(blk);
3937 } else {
Max Reitzbe4b67b2015-10-19 17:53:09 +02003938 if (!qdict_get_try_str(qdict, "node-name")) {
3939 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3940 "the root node");
3941 goto fail;
3942 }
3943
Max Reitzbd745e22015-10-19 17:53:32 +02003944 bs = bds_tree_init(qdict, errp);
3945 if (!bs) {
Max Reitzbe4b67b2015-10-19 17:53:09 +02003946 goto fail;
3947 }
Max Reitz9c4218e2016-01-29 16:36:12 +01003948
3949 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003950 }
3951
Max Reitzbe4b67b2015-10-19 17:53:09 +02003952 if (bs && bdrv_key_required(bs)) {
3953 if (blk) {
Max Reitzefaa7c42016-03-16 19:54:38 +01003954 monitor_remove_blk(blk);
Max Reitzbe4b67b2015-10-19 17:53:09 +02003955 blk_unref(blk);
3956 } else {
Max Reitz9c4218e2016-01-29 16:36:12 +01003957 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
Max Reitzbe4b67b2015-10-19 17:53:09 +02003958 bdrv_unref(bs);
3959 }
Kevin Wolf8ae8e902014-03-06 15:43:42 +01003960 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3961 goto fail;
3962 }
3963
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003964fail:
Eric Blake3b098d52016-06-09 10:48:43 -06003965 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003966}
3967
Alberto Garcia81b936a2015-11-02 16:51:55 +02003968void qmp_x_blockdev_del(bool has_id, const char *id,
3969 bool has_node_name, const char *node_name, Error **errp)
3970{
3971 AioContext *aio_context;
3972 BlockBackend *blk;
3973 BlockDriverState *bs;
3974
3975 if (has_id && has_node_name) {
3976 error_setg(errp, "Only one of id and node-name must be specified");
3977 return;
3978 } else if (!has_id && !has_node_name) {
3979 error_setg(errp, "No block device specified");
3980 return;
3981 }
3982
3983 if (has_id) {
Max Reitzefaa7c42016-03-16 19:54:38 +01003984 /* blk_by_name() never returns a BB that is not owned by the monitor */
Alberto Garcia81b936a2015-11-02 16:51:55 +02003985 blk = blk_by_name(id);
3986 if (!blk) {
3987 error_setg(errp, "Cannot find block backend %s", id);
3988 return;
3989 }
Paolo Bonzini5cf87fd2016-03-31 15:07:43 +02003990 if (blk_legacy_dinfo(blk)) {
3991 error_setg(errp, "Deleting block backend added with drive-add"
3992 " is not supported");
3993 return;
3994 }
Alberto Garcia81b936a2015-11-02 16:51:55 +02003995 if (blk_get_refcnt(blk) > 1) {
3996 error_setg(errp, "Block backend %s is in use", id);
3997 return;
3998 }
3999 bs = blk_bs(blk);
4000 aio_context = blk_get_aio_context(blk);
4001 } else {
Kevin Wolf1f0c4612016-03-22 18:38:44 +01004002 blk = NULL;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004003 bs = bdrv_find_node(node_name);
4004 if (!bs) {
4005 error_setg(errp, "Cannot find node %s", node_name);
4006 return;
4007 }
Kevin Wolf1f0c4612016-03-22 18:38:44 +01004008 if (bdrv_has_blk(bs)) {
Alberto Garcia81b936a2015-11-02 16:51:55 +02004009 error_setg(errp, "Node %s is in use by %s",
Kevin Wolf1f0c4612016-03-22 18:38:44 +01004010 node_name, bdrv_get_parent_name(bs));
Alberto Garcia81b936a2015-11-02 16:51:55 +02004011 return;
4012 }
4013 aio_context = bdrv_get_aio_context(bs);
4014 }
4015
4016 aio_context_acquire(aio_context);
4017
4018 if (bs) {
4019 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
4020 goto out;
4021 }
4022
Max Reitz9c4218e2016-01-29 16:36:12 +01004023 if (!blk && !bs->monitor_list.tqe_prev) {
4024 error_setg(errp, "Node %s is not owned by the monitor",
4025 bs->node_name);
4026 goto out;
4027 }
4028
4029 if (bs->refcnt > 1) {
Alberto Garcia81b936a2015-11-02 16:51:55 +02004030 error_setg(errp, "Block device %s is in use",
4031 bdrv_get_device_or_node_name(bs));
4032 goto out;
4033 }
4034 }
4035
4036 if (blk) {
Max Reitzefaa7c42016-03-16 19:54:38 +01004037 monitor_remove_blk(blk);
Alberto Garcia81b936a2015-11-02 16:51:55 +02004038 blk_unref(blk);
4039 } else {
Max Reitz9c4218e2016-01-29 16:36:12 +01004040 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
Alberto Garcia81b936a2015-11-02 16:51:55 +02004041 bdrv_unref(bs);
4042 }
4043
4044out:
4045 aio_context_release(aio_context);
4046}
4047
Wen Congyang7f821592016-05-10 15:36:39 +08004048static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
4049 const char *child_name)
4050{
4051 BdrvChild *child;
4052
4053 QLIST_FOREACH(child, &parent_bs->children, next) {
4054 if (strcmp(child->name, child_name) == 0) {
4055 return child;
4056 }
4057 }
4058
4059 return NULL;
4060}
4061
4062void qmp_x_blockdev_change(const char *parent, bool has_child,
4063 const char *child, bool has_node,
4064 const char *node, Error **errp)
4065{
4066 BlockDriverState *parent_bs, *new_bs = NULL;
4067 BdrvChild *p_child;
4068
4069 parent_bs = bdrv_lookup_bs(parent, parent, errp);
4070 if (!parent_bs) {
4071 return;
4072 }
4073
4074 if (has_child == has_node) {
4075 if (has_child) {
4076 error_setg(errp, "The parameters child and node are in conflict");
4077 } else {
4078 error_setg(errp, "Either child or node must be specified");
4079 }
4080 return;
4081 }
4082
4083 if (has_child) {
4084 p_child = bdrv_find_child(parent_bs, child);
4085 if (!p_child) {
4086 error_setg(errp, "Node '%s' does not have child '%s'",
4087 parent, child);
4088 return;
4089 }
4090 bdrv_del_child(parent_bs, p_child, errp);
4091 }
4092
4093 if (has_node) {
4094 new_bs = bdrv_find_node(node);
4095 if (!new_bs) {
4096 error_setg(errp, "Node '%s' not found", node);
4097 return;
4098 }
4099 bdrv_add_child(parent_bs, new_bs, errp);
4100 }
4101}
4102
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004103BlockJobInfoList *qmp_query_block_jobs(Error **errp)
4104{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004105 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004106 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004107
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004108 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
4109 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
4110 AioContext *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004111
4112 aio_context_acquire(aio_context);
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004113 elem->value = block_job_query(job);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004114 aio_context_release(aio_context);
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004115
4116 *p_next = elem;
4117 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004118 }
4119
4120 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004121}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004122
Kevin Wolf00063832013-03-15 10:35:07 +01004123QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004124 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01004125 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01004126 .desc = {
4127 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004128 .name = "snapshot",
4129 .type = QEMU_OPT_BOOL,
4130 .help = "enable/disable snapshot mode",
4131 },{
Paolo Bonzinia9384af2013-02-08 14:06:12 +01004132 .name = "discard",
4133 .type = QEMU_OPT_STRING,
4134 .help = "discard operation (ignore/off, unmap/on)",
4135 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004136 .name = "aio",
4137 .type = QEMU_OPT_STRING,
4138 .help = "host AIO implementation (threads, native)",
4139 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01004140 .name = BDRV_OPT_CACHE_WB,
4141 .type = QEMU_OPT_BOOL,
4142 .help = "Enable writeback mode",
4143 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004144 .name = "format",
4145 .type = QEMU_OPT_STRING,
4146 .help = "disk format (raw, qcow2, ...)",
4147 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004148 .name = "rerror",
4149 .type = QEMU_OPT_STRING,
4150 .help = "read error action",
4151 },{
4152 .name = "werror",
4153 .type = QEMU_OPT_STRING,
4154 .help = "write error action",
4155 },{
Kevin Wolf0f227a92013-07-19 20:07:29 +02004156 .name = "read-only",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004157 .type = QEMU_OPT_BOOL,
4158 .help = "open drive file as read-only",
4159 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004160 .name = "throttling.iops-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004161 .type = QEMU_OPT_NUMBER,
4162 .help = "limit total I/O operations per second",
4163 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004164 .name = "throttling.iops-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004165 .type = QEMU_OPT_NUMBER,
4166 .help = "limit read operations per second",
4167 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004168 .name = "throttling.iops-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004169 .type = QEMU_OPT_NUMBER,
4170 .help = "limit write operations per second",
4171 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004172 .name = "throttling.bps-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004173 .type = QEMU_OPT_NUMBER,
4174 .help = "limit total bytes per second",
4175 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004176 .name = "throttling.bps-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004177 .type = QEMU_OPT_NUMBER,
4178 .help = "limit read bytes per second",
4179 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004180 .name = "throttling.bps-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004181 .type = QEMU_OPT_NUMBER,
4182 .help = "limit write bytes per second",
4183 },{
Benoît Canet3e9fab62013-09-02 14:14:40 +02004184 .name = "throttling.iops-total-max",
4185 .type = QEMU_OPT_NUMBER,
4186 .help = "I/O operations burst",
4187 },{
4188 .name = "throttling.iops-read-max",
4189 .type = QEMU_OPT_NUMBER,
4190 .help = "I/O operations read burst",
4191 },{
4192 .name = "throttling.iops-write-max",
4193 .type = QEMU_OPT_NUMBER,
4194 .help = "I/O operations write burst",
4195 },{
4196 .name = "throttling.bps-total-max",
4197 .type = QEMU_OPT_NUMBER,
4198 .help = "total bytes burst",
4199 },{
4200 .name = "throttling.bps-read-max",
4201 .type = QEMU_OPT_NUMBER,
4202 .help = "total bytes read burst",
4203 },{
4204 .name = "throttling.bps-write-max",
4205 .type = QEMU_OPT_NUMBER,
4206 .help = "total bytes write burst",
4207 },{
Alberto Garcia8a0fc182016-02-18 12:27:02 +02004208 .name = "throttling.iops-total-max-length",
4209 .type = QEMU_OPT_NUMBER,
4210 .help = "length of the iops-total-max burst period, in seconds",
4211 },{
4212 .name = "throttling.iops-read-max-length",
4213 .type = QEMU_OPT_NUMBER,
4214 .help = "length of the iops-read-max burst period, in seconds",
4215 },{
4216 .name = "throttling.iops-write-max-length",
4217 .type = QEMU_OPT_NUMBER,
4218 .help = "length of the iops-write-max burst period, in seconds",
4219 },{
4220 .name = "throttling.bps-total-max-length",
4221 .type = QEMU_OPT_NUMBER,
4222 .help = "length of the bps-total-max burst period, in seconds",
4223 },{
4224 .name = "throttling.bps-read-max-length",
4225 .type = QEMU_OPT_NUMBER,
4226 .help = "length of the bps-read-max burst period, in seconds",
4227 },{
4228 .name = "throttling.bps-write-max-length",
4229 .type = QEMU_OPT_NUMBER,
4230 .help = "length of the bps-write-max burst period, in seconds",
4231 },{
Benoît Canet2024c1d2013-09-02 14:14:41 +02004232 .name = "throttling.iops-size",
4233 .type = QEMU_OPT_NUMBER,
4234 .help = "when limiting by iops max size of an I/O in bytes",
4235 },{
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004236 .name = "throttling.group",
4237 .type = QEMU_OPT_STRING,
4238 .help = "name of the block throttling group",
4239 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004240 .name = "copy-on-read",
4241 .type = QEMU_OPT_BOOL,
4242 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004243 },{
4244 .name = "detect-zeroes",
4245 .type = QEMU_OPT_STRING,
4246 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004247 },{
4248 .name = "stats-account-invalid",
4249 .type = QEMU_OPT_BOOL,
4250 .help = "whether to account for invalid I/O operations "
4251 "in the statistics",
4252 },{
4253 .name = "stats-account-failed",
4254 .type = QEMU_OPT_BOOL,
4255 .help = "whether to account for failed I/O operations "
4256 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004257 },
4258 { /* end of list */ }
4259 },
4260};
Kevin Wolf00063832013-03-15 10:35:07 +01004261
Max Reitzbd745e22015-10-19 17:53:32 +02004262static QemuOptsList qemu_root_bds_opts = {
4263 .name = "root-bds",
Kevin Wolf23f7fcb2016-03-15 14:43:14 +01004264 .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head),
Max Reitzbd745e22015-10-19 17:53:32 +02004265 .desc = {
4266 {
4267 .name = "discard",
4268 .type = QEMU_OPT_STRING,
4269 .help = "discard operation (ignore/off, unmap/on)",
4270 },{
Max Reitzbd745e22015-10-19 17:53:32 +02004271 .name = "aio",
4272 .type = QEMU_OPT_STRING,
4273 .help = "host AIO implementation (threads, native)",
4274 },{
4275 .name = "read-only",
4276 .type = QEMU_OPT_BOOL,
4277 .help = "open drive file as read-only",
4278 },{
4279 .name = "copy-on-read",
4280 .type = QEMU_OPT_BOOL,
4281 .help = "copy read data from backing file into image file",
4282 },{
4283 .name = "detect-zeroes",
4284 .type = QEMU_OPT_STRING,
4285 .help = "try to optimize zero writes (off, on, unmap)",
4286 },
4287 { /* end of list */ }
4288 },
4289};
4290
Kevin Wolf00063832013-03-15 10:35:07 +01004291QemuOptsList qemu_drive_opts = {
4292 .name = "drive",
4293 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4294 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004295 /*
4296 * no elements => accept any params
4297 * validation will happen later
4298 */
Kevin Wolf00063832013-03-15 10:35:07 +01004299 { /* end of list */ }
4300 },
4301};