blob: 29c6561fd89dc076fcd412ab987904c37b72208c [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
Kevin Wolfb33945c2016-09-20 13:38:43 +020059static int do_open_tray(const char *blk_name, const char *qdev_id,
60 bool force, Error **errp);
Colin Lordbf18bee2016-06-06 14:15:22 -040061
Markus Armbruster19609662011-01-28 11:21:39 +010062static const char *const if_name[IF_COUNT] = {
63 [IF_NONE] = "none",
64 [IF_IDE] = "ide",
65 [IF_SCSI] = "scsi",
66 [IF_FLOPPY] = "floppy",
67 [IF_PFLASH] = "pflash",
68 [IF_MTD] = "mtd",
69 [IF_SD] = "sd",
70 [IF_VIRTIO] = "virtio",
71 [IF_XEN] = "xen",
72};
73
John Snow21dff8c2014-10-01 14:19:25 -040074static int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010075 /*
76 * Do not change these numbers! They govern how drive option
77 * index maps to unit and bus. That mapping is ABI.
78 *
Wei Jiangang547cb152016-04-26 18:12:43 +080079 * All controllers used to implement if=T drives need to support
Markus Armbruster27d6bf42011-01-28 11:21:40 +010080 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
81 * Otherwise, some index values map to "impossible" bus, unit
82 * values.
83 *
84 * For instance, if you change [IF_SCSI] to 255, -drive
85 * if=scsi,index=12 no longer means bus=1,unit=5, but
86 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
87 * the drive can't be set up. Regression.
88 */
89 [IF_IDE] = 2,
90 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +010091};
92
John Snow21dff8c2014-10-01 14:19:25 -040093/**
94 * Boards may call this to offer board-by-board overrides
95 * of the default, global values.
96 */
97void override_max_devs(BlockInterfaceType type, int max_devs)
98{
Markus Armbruster18e46a02014-10-07 13:59:06 +020099 BlockBackend *blk;
John Snow21dff8c2014-10-01 14:19:25 -0400100 DriveInfo *dinfo;
101
102 if (max_devs <= 0) {
103 return;
104 }
105
Markus Armbruster18e46a02014-10-07 13:59:06 +0200106 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
107 dinfo = blk_legacy_dinfo(blk);
John Snow21dff8c2014-10-01 14:19:25 -0400108 if (dinfo->type == type) {
109 fprintf(stderr, "Cannot override units-per-bus property of"
110 " the %s interface, because a drive of that type has"
111 " already been added.\n", if_name[type]);
112 g_assert_not_reached();
113 }
114 }
115
116 if_max_devs[type] = max_devs;
117}
118
Markus Armbruster14bafc52010-06-25 08:09:10 +0200119/*
120 * We automatically delete the drive when a device using it gets
121 * unplugged. Questionable feature, but we can't just drop it.
122 * Device models call blockdev_mark_auto_del() to schedule the
123 * automatic deletion, and generic qdev code calls blockdev_auto_del()
124 * when deletion is actually safe.
125 */
Markus Armbruster4be74632014-10-07 13:59:18 +0200126void blockdev_mark_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200127{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200128 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster4be74632014-10-07 13:59:18 +0200129 BlockDriverState *bs = blk_bs(blk);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100130 AioContext *aio_context;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200131
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200132 if (!dinfo) {
Kevin Wolf2d246f02013-09-18 15:14:47 +0200133 return;
134 }
135
Max Reitz5433c242015-10-19 17:53:29 +0200136 if (bs) {
137 aio_context = bdrv_get_aio_context(bs);
138 aio_context_acquire(aio_context);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100139
Max Reitz5433c242015-10-19 17:53:29 +0200140 if (bs->job) {
141 block_job_cancel(bs->job);
142 }
143
144 aio_context_release(aio_context);
Paolo Bonzini12bde0e2012-03-30 13:17:10 +0200145 }
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100146
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200147 dinfo->auto_del = 1;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200148}
149
Markus Armbruster4be74632014-10-07 13:59:18 +0200150void blockdev_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200151{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200152 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200153
Ryan Harper0fc0f1f2010-12-08 10:05:00 -0600154 if (dinfo && dinfo->auto_del) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100155 monitor_remove_blk(blk);
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +0200156 blk_unref(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200157 }
158}
159
John Snowd8f94e12014-10-01 14:19:27 -0400160/**
161 * Returns the current mapping of how many units per bus
162 * a particular interface can support.
163 *
164 * A positive integer indicates n units per bus.
165 * 0 implies the mapping has not been established.
166 * -1 indicates an invalid BlockInterfaceType was given.
167 */
168int drive_get_max_devs(BlockInterfaceType type)
169{
170 if (type >= IF_IDE && type < IF_COUNT) {
171 return if_max_devs[type];
172 }
173
174 return -1;
175}
176
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100177static int drive_index_to_bus_id(BlockInterfaceType type, int index)
178{
179 int max_devs = if_max_devs[type];
180 return max_devs ? index / max_devs : 0;
181}
182
183static int drive_index_to_unit_id(BlockInterfaceType type, int index)
184{
185 int max_devs = if_max_devs[type];
186 return max_devs ? index % max_devs : index;
187}
188
Markus Armbruster2292dda2011-01-28 11:21:41 +0100189QemuOpts *drive_def(const char *optstr)
190{
Markus Armbruster70b94332015-02-13 12:50:26 +0100191 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100192}
193
194QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +0100195 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +0200196{
Markus Armbruster666daa62010-06-02 18:48:27 +0200197 QemuOpts *opts;
198
Markus Armbruster2292dda2011-01-28 11:21:41 +0100199 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200200 if (!opts) {
201 return NULL;
202 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100203 if (type != IF_DEFAULT) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100204 qemu_opt_set(opts, "if", if_name[type], &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100205 }
206 if (index >= 0) {
Markus Armbrustera8b18f82015-02-13 15:50:43 +0100207 qemu_opt_set_number(opts, "index", index, &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100208 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200209 if (file)
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100210 qemu_opt_set(opts, "file", file, &error_abort);
Markus Armbruster666daa62010-06-02 18:48:27 +0200211 return opts;
212}
213
214DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
215{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200216 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200217 DriveInfo *dinfo;
218
Markus Armbruster18e46a02014-10-07 13:59:06 +0200219 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
220 dinfo = blk_legacy_dinfo(blk);
221 if (dinfo && dinfo->type == type
222 && dinfo->bus == bus && dinfo->unit == unit) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200223 return dinfo;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200224 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200225 }
226
227 return NULL;
228}
229
John Snowa66c9dc2014-10-01 14:19:24 -0400230bool drive_check_orphaned(void)
231{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200232 BlockBackend *blk;
John Snowa66c9dc2014-10-01 14:19:24 -0400233 DriveInfo *dinfo;
234 bool rs = false;
235
Markus Armbruster18e46a02014-10-07 13:59:06 +0200236 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
237 dinfo = blk_legacy_dinfo(blk);
John Snowa66c9dc2014-10-01 14:19:24 -0400238 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
239 /* Unless this is a default drive, this may be an oversight. */
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200240 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
John Snowa66c9dc2014-10-01 14:19:24 -0400241 dinfo->type != IF_NONE) {
242 fprintf(stderr, "Warning: Orphaned drive without device: "
243 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
Max Reitz5433c242015-10-19 17:53:29 +0200244 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
245 if_name[dinfo->type], dinfo->bus, dinfo->unit);
John Snowa66c9dc2014-10-01 14:19:24 -0400246 rs = true;
247 }
248 }
249
250 return rs;
251}
252
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100253DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
254{
255 return drive_get(type,
256 drive_index_to_bus_id(type, index),
257 drive_index_to_unit_id(type, index));
258}
259
Markus Armbruster666daa62010-06-02 18:48:27 +0200260int drive_get_max_bus(BlockInterfaceType type)
261{
262 int max_bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200263 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200264 DriveInfo *dinfo;
265
266 max_bus = -1;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200267 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
268 dinfo = blk_legacy_dinfo(blk);
269 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200270 max_bus = dinfo->bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200271 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200272 }
273 return max_bus;
274}
275
Markus Armbruster13839972011-01-28 11:21:37 +0100276/* Get a block device. This should only be used for single-drive devices
277 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
278 appropriate bus. */
279DriveInfo *drive_get_next(BlockInterfaceType type)
280{
281 static int next_block_unit[IF_COUNT];
282
283 return drive_get(type, 0, next_block_unit[type]++);
284}
285
Markus Armbruster666daa62010-06-02 18:48:27 +0200286static void bdrv_format_print(void *opaque, const char *name)
287{
Markus Armbruster807105a2011-01-17 19:31:27 +0100288 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200289}
290
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000291typedef struct {
292 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800293 BlockDriverState *bs;
294} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000295
Kevin Wolfb6810722013-09-20 11:33:11 +0200296static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200297{
298 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200299 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200300 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200301 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200302 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200303 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200304 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200305 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200306 } else {
Kevin Wolfb6810722013-09-20 11:33:11 +0200307 error_setg(errp, "'%s' invalid %s error action",
308 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200309 return -1;
310 }
311}
312
Alberto Garcia40119ef2015-11-16 11:28:38 +0200313static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
314 Error **errp)
315{
316 const QListEntry *entry;
317 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
318 switch (qobject_type(entry->value)) {
319
320 case QTYPE_QSTRING: {
321 unsigned long long length;
322 const char *str = qstring_get_str(qobject_to_qstring(entry->value));
323 if (parse_uint_full(str, &length, 10) == 0 &&
324 length > 0 && length <= UINT_MAX) {
325 block_acct_add_interval(stats, (unsigned) length);
326 } else {
327 error_setg(errp, "Invalid interval length: %s", str);
328 return false;
329 }
330 break;
331 }
332
333 case QTYPE_QINT: {
334 int64_t length = qint_get_int(qobject_to_qint(entry->value));
335 if (length > 0 && length <= UINT_MAX) {
336 block_acct_add_interval(stats, (unsigned) length);
337 } else {
338 error_setg(errp, "Invalid interval length: %" PRId64, length);
339 return false;
340 }
341 break;
342 }
343
344 default:
345 error_setg(errp, "The specification of stats-intervals is invalid");
346 return false;
347 }
348 }
349 return true;
350}
351
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200352typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
353
Max Reitzfbf81752015-10-19 17:53:31 +0200354/* All parameters but @opts are optional and may be set to NULL. */
355static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
356 const char **throttling_group, ThrottleConfig *throttle_cfg,
357 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
358{
359 const char *discard;
360 Error *local_error = NULL;
361 const char *aio;
362
363 if (bdrv_flags) {
Max Reitzfbf81752015-10-19 17:53:31 +0200364 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
365 *bdrv_flags |= BDRV_O_COPY_ON_READ;
366 }
367
368 if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
369 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
370 error_setg(errp, "Invalid discard option");
371 return;
372 }
373 }
374
Max Reitzfbf81752015-10-19 17:53:31 +0200375 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
376 if (!strcmp(aio, "native")) {
377 *bdrv_flags |= BDRV_O_NATIVE_AIO;
378 } else if (!strcmp(aio, "threads")) {
379 /* this is the default */
380 } else {
381 error_setg(errp, "invalid aio option");
382 return;
383 }
384 }
385 }
386
387 /* disk I/O throttling */
388 if (throttling_group) {
389 *throttling_group = qemu_opt_get(opts, "throttling.group");
390 }
391
392 if (throttle_cfg) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200393 throttle_config_init(throttle_cfg);
Max Reitzfbf81752015-10-19 17:53:31 +0200394 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
395 qemu_opt_get_number(opts, "throttling.bps-total", 0);
396 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
397 qemu_opt_get_number(opts, "throttling.bps-read", 0);
398 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
399 qemu_opt_get_number(opts, "throttling.bps-write", 0);
400 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
401 qemu_opt_get_number(opts, "throttling.iops-total", 0);
402 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
403 qemu_opt_get_number(opts, "throttling.iops-read", 0);
404 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
405 qemu_opt_get_number(opts, "throttling.iops-write", 0);
406
407 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
408 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
409 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
410 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
411 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
412 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
413 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
414 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
415 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
416 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
417 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
418 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
419
Alberto Garcia8a0fc182016-02-18 12:27:02 +0200420 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
421 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
422 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
423 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
424 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
425 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
426 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
427 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
428 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
429 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
430 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
431 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
432
Max Reitzfbf81752015-10-19 17:53:31 +0200433 throttle_cfg->op_size =
434 qemu_opt_get_number(opts, "throttling.iops-size", 0);
435
Alberto Garciad5851082016-02-18 12:26:59 +0200436 if (!throttle_is_valid(throttle_cfg, errp)) {
Max Reitzfbf81752015-10-19 17:53:31 +0200437 return;
438 }
439 }
440
441 if (detect_zeroes) {
442 *detect_zeroes =
443 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
444 qemu_opt_get(opts, "detect-zeroes"),
Eric Blake7fb1cf12015-11-18 01:52:57 -0700445 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Max Reitzfbf81752015-10-19 17:53:31 +0200446 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
447 &local_error);
448 if (local_error) {
449 error_propagate(errp, local_error);
450 return;
451 }
452
453 if (bdrv_flags &&
454 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
455 !(*bdrv_flags & BDRV_O_UNMAP))
456 {
457 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
458 "without setting discard operation to unmap");
459 return;
460 }
461 }
462}
463
Kevin Wolff298d072013-09-10 12:01:20 +0200464/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200465static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
466 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200467{
468 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200469 int bdrv_flags = 0;
470 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200471 bool account_invalid, account_failed;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300472 bool writethrough, read_only;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200473 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200474 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200475 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200476 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100477 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100478 QemuOpts *opts;
Alberto Garcia40119ef2015-11-16 11:28:38 +0200479 QDict *interval_dict = NULL;
480 QList *interval_list = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100481 const char *id;
Max Reitzfbf81752015-10-19 17:53:31 +0200482 BlockdevDetectZeroesOptions detect_zeroes =
483 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
484 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200485
Kevin Wolff298d072013-09-10 12:01:20 +0200486 /* Check common options by copying from bs_opts to opts, all other options
487 * stay in bs_opts for processing by bdrv_open(). */
488 id = qdict_get_try_str(bs_opts, "id");
Kevin Wolf00063832013-03-15 10:35:07 +0100489 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100490 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200491 error_propagate(errp, error);
Markus Armbruster6376f952014-05-28 11:17:01 +0200492 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100493 }
494
Kevin Wolf00063832013-03-15 10:35:07 +0100495 qemu_opts_absorb_qdict(opts, bs_opts, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100496 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200497 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100498 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100499 }
500
501 if (id) {
502 qdict_del(bs_opts, "id");
503 }
504
Markus Armbruster666daa62010-06-02 18:48:27 +0200505 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200506 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200507
Alberto Garcia362e9292015-10-28 17:33:04 +0200508 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
509 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
510
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100511 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
512
Alberto Garciaff356ee2016-07-08 17:03:00 +0300513 id = qemu_opts_id(opts);
514
Alberto Garcia40119ef2015-11-16 11:28:38 +0200515 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
516 qdict_array_split(interval_dict, &interval_list);
517
518 if (qdict_size(interval_dict) != 0) {
519 error_setg(errp, "Invalid option stats-intervals.%s",
520 qdict_first(interval_dict)->key);
521 goto early_err;
522 }
Alberto Garcia2be55062015-10-28 17:33:07 +0200523
Max Reitzfbf81752015-10-19 17:53:31 +0200524 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
525 &detect_zeroes, &error);
526 if (error) {
527 error_propagate(errp, error);
528 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200529 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200530
531 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100532 if (is_help_option(buf)) {
533 error_printf("Supported formats:");
534 bdrv_iterate_format(bdrv_format_print, NULL);
535 error_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100536 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200537 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200538
Max Reitze4342ce2015-02-05 13:58:14 -0500539 if (qdict_haskey(bs_opts, "driver")) {
540 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100541 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400542 }
Max Reitze4342ce2015-02-05 13:58:14 -0500543 qdict_put(bs_opts, "driver", qstring_from_str(buf));
Markus Armbruster666daa62010-06-02 18:48:27 +0200544 }
545
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200546 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200547 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200548 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100549 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200550 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100551 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200552 }
553 }
554
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200555 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200556 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200557 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100558 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200559 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100560 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200561 }
562 }
563
Max Reitz5ec18f82015-10-19 17:53:30 +0200564 if (snapshot) {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200565 bdrv_flags |= BDRV_O_SNAPSHOT;
Max Reitz5ec18f82015-10-19 17:53:30 +0200566 }
567
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300568 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
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;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300577 blk_rs->read_only = read_only;
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");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300591 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
592 read_only ? "on" : "off");
Kevin Wolf72e775c2016-03-15 14:34:37 +0100593 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200594
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100595 if (runstate_check(RUN_STATE_INMIGRATE)) {
596 bdrv_flags |= BDRV_O_INACTIVE;
597 }
598
Max Reitzefaa7c42016-03-16 19:54:38 +0100599 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500600 if (!blk) {
601 goto err_no_bs_opts;
602 }
603 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500604
Max Reitz5ec18f82015-10-19 17:53:30 +0200605 bs->detect_zeroes = detect_zeroes;
606
Max Reitz5ec18f82015-10-19 17:53:30 +0200607 if (bdrv_key_required(bs)) {
608 autostart = 0;
609 }
Alberto Garcia362e9292015-10-28 17:33:04 +0200610
611 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
Alberto Garcia2be55062015-10-28 17:33:07 +0200612
Alberto Garcia40119ef2015-11-16 11:28:38 +0200613 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
614 blk_unref(blk);
615 blk = NULL;
616 goto err_no_bs_opts;
Alberto Garcia2be55062015-10-28 17:33:07 +0200617 }
Max Reitz5ec18f82015-10-19 17:53:30 +0200618 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200619
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100620 /* disk I/O throttling */
621 if (throttle_enabled(&cfg)) {
622 if (!throttling_group) {
Alberto Garciaff356ee2016-07-08 17:03:00 +0300623 throttling_group = id;
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100624 }
625 blk_io_limits_enable(blk, throttling_group);
626 blk_set_io_limits(blk, &cfg);
627 }
628
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100629 blk_set_enable_write_cache(blk, !writethrough);
Max Reitz373340b2015-10-19 17:53:22 +0200630 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200631
Alberto Garciaff356ee2016-07-08 17:03:00 +0300632 if (!monitor_add_blk(blk, id, errp)) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100633 blk_unref(blk);
634 blk = NULL;
635 goto err_no_bs_opts;
636 }
637
Max Reitze4342ce2015-02-05 13:58:14 -0500638err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100639 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200640 QDECREF(interval_dict);
641 QDECREF(interval_list);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200642 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100643
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100644early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100645 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200646 QDECREF(interval_dict);
647 QDECREF(interval_list);
Markus Armbruster6376f952014-05-28 11:17:01 +0200648err_no_opts:
649 QDECREF(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100650 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200651}
652
Max Reitzbd745e22015-10-19 17:53:32 +0200653static QemuOptsList qemu_root_bds_opts;
654
655/* Takes the ownership of bs_opts */
656static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
657{
658 BlockDriverState *bs;
659 QemuOpts *opts;
660 Error *local_error = NULL;
661 BlockdevDetectZeroesOptions detect_zeroes;
Max Reitzbd745e22015-10-19 17:53:32 +0200662 int bdrv_flags = 0;
663
664 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
665 if (!opts) {
666 goto fail;
667 }
668
669 qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
670 if (local_error) {
671 error_propagate(errp, local_error);
672 goto fail;
673 }
674
675 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
676 &detect_zeroes, &local_error);
677 if (local_error) {
678 error_propagate(errp, local_error);
679 goto fail;
680 }
681
Kevin Wolfa81d6162016-03-07 14:23:04 +0100682 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
683 * with other callers) rather than what we want as the real defaults.
684 * Apply the defaults here instead. */
Kevin Wolfa81d6162016-03-07 14:23:04 +0100685 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
686 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300687 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
Kevin Wolfa81d6162016-03-07 14:23:04 +0100688
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100689 if (runstate_check(RUN_STATE_INMIGRATE)) {
690 bdrv_flags |= BDRV_O_INACTIVE;
691 }
692
Max Reitz5b363932016-05-17 16:41:31 +0200693 bs = bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
694 if (!bs) {
Max Reitzbd745e22015-10-19 17:53:32 +0200695 goto fail_no_bs_opts;
696 }
697
698 bs->detect_zeroes = detect_zeroes;
699
700fail_no_bs_opts:
701 qemu_opts_del(opts);
702 return bs;
703
704fail:
705 qemu_opts_del(opts);
706 QDECREF(bs_opts);
707 return NULL;
708}
709
Max Reitz9c4218e2016-01-29 16:36:12 +0100710void blockdev_close_all_bdrv_states(void)
711{
712 BlockDriverState *bs, *next_bs;
713
714 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
715 AioContext *ctx = bdrv_get_aio_context(bs);
716
717 aio_context_acquire(ctx);
718 bdrv_unref(bs);
719 aio_context_release(ctx);
720 }
721}
722
Max Reitz262b4e82016-03-16 19:54:41 +0100723/* Iterates over the list of monitor-owned BlockDriverStates */
724BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
725{
726 return bs ? QTAILQ_NEXT(bs, monitor_list)
727 : QTAILQ_FIRST(&monitor_bdrv_states);
728}
729
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200730static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
731 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200732{
733 const char *value;
734
735 value = qemu_opt_get(opts, from);
736 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200737 if (qemu_opt_find(opts, to)) {
738 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
739 "same time", to, from);
740 return;
741 }
Jun Li20d6cd42014-09-24 13:45:27 +0800742 }
743
744 /* rename all items in opts */
745 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100746 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200747 qemu_opt_unset(opts, from);
748 }
749}
750
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200751QemuOptsList qemu_legacy_drive_opts = {
752 .name = "drive",
753 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
754 .desc = {
755 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200756 .name = "bus",
757 .type = QEMU_OPT_NUMBER,
758 .help = "bus number",
759 },{
760 .name = "unit",
761 .type = QEMU_OPT_NUMBER,
762 .help = "unit number (i.e. lun for scsi)",
763 },{
764 .name = "index",
765 .type = QEMU_OPT_NUMBER,
766 .help = "index number",
767 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200768 .name = "media",
769 .type = QEMU_OPT_STRING,
770 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200771 },{
772 .name = "if",
773 .type = QEMU_OPT_STRING,
774 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200775 },{
776 .name = "cyls",
777 .type = QEMU_OPT_NUMBER,
778 .help = "number of cylinders (ide disk geometry)",
779 },{
780 .name = "heads",
781 .type = QEMU_OPT_NUMBER,
782 .help = "number of heads (ide disk geometry)",
783 },{
784 .name = "secs",
785 .type = QEMU_OPT_NUMBER,
786 .help = "number of sectors (ide disk geometry)",
787 },{
788 .name = "trans",
789 .type = QEMU_OPT_STRING,
790 .help = "chs translation (auto, lba, none)",
Kevin Wolf26929292013-09-09 17:01:03 +0200791 },{
792 .name = "boot",
793 .type = QEMU_OPT_BOOL,
794 .help = "(deprecated, ignored)",
Kevin Wolf394c7d42013-09-13 14:09:17 +0200795 },{
796 .name = "addr",
797 .type = QEMU_OPT_STRING,
798 .help = "pci address (virtio only)",
Max Reitzd095b462013-12-20 19:28:14 +0100799 },{
Kevin Wolfbcf83152014-06-06 15:21:48 +0200800 .name = "serial",
801 .type = QEMU_OPT_STRING,
802 .help = "disk serial number",
803 },{
Max Reitzd095b462013-12-20 19:28:14 +0100804 .name = "file",
805 .type = QEMU_OPT_STRING,
806 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200807 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200808
809 /* Options that are passed on, but have special semantics with -drive */
810 {
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300811 .name = BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200812 .type = QEMU_OPT_BOOL,
813 .help = "open drive file as read-only",
814 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100815 .name = "rerror",
816 .type = QEMU_OPT_STRING,
817 .help = "read error action",
818 },{
819 .name = "werror",
820 .type = QEMU_OPT_STRING,
821 .help = "write error action",
822 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200823 .name = "copy-on-read",
824 .type = QEMU_OPT_BOOL,
825 .help = "copy read data from backing file into image file",
826 },
827
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200828 { /* end of list */ }
829 },
830};
831
Markus Armbruster60e19e02014-06-06 14:50:58 +0200832DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
Kevin Wolf57975222013-07-17 14:41:54 +0200833{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200834 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200835 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200836 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200837 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200838 QemuOpts *legacy_opts;
839 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200840 BlockInterfaceType type;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200841 int cyls, heads, secs, translation;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200842 int max_devs, bus_id, unit_id, index;
Kevin Wolf394c7d42013-09-13 14:09:17 +0200843 const char *devaddr;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100844 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800845 bool read_only = false;
846 bool copy_on_read;
Kevin Wolfbcf83152014-06-06 15:21:48 +0200847 const char *serial;
Max Reitzd095b462013-12-20 19:28:14 +0100848 const char *filename;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200849 Error *local_err = NULL;
Kevin Wolf247147f2014-09-24 16:37:14 +0200850 int i;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200851
Kevin Wolf57975222013-07-17 14:41:54 +0200852 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200853 static const struct {
854 const char *from;
855 const char *to;
856 } opt_renames[] = {
857 { "iops", "throttling.iops-total" },
858 { "iops_rd", "throttling.iops-read" },
859 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200860
Kevin Wolf247147f2014-09-24 16:37:14 +0200861 { "bps", "throttling.bps-total" },
862 { "bps_rd", "throttling.bps-read" },
863 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200864
Kevin Wolf247147f2014-09-24 16:37:14 +0200865 { "iops_max", "throttling.iops-total-max" },
866 { "iops_rd_max", "throttling.iops-read-max" },
867 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200868
Kevin Wolf247147f2014-09-24 16:37:14 +0200869 { "bps_max", "throttling.bps-total-max" },
870 { "bps_rd_max", "throttling.bps-read-max" },
871 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200872
Kevin Wolf247147f2014-09-24 16:37:14 +0200873 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200874
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200875 { "group", "throttling.group" },
876
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300877 { "readonly", BDRV_OPT_READ_ONLY },
Kevin Wolf247147f2014-09-24 16:37:14 +0200878 };
879
880 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200881 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
882 &local_err);
883 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100884 error_report_err(local_err);
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200885 return NULL;
886 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200887 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200888
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200889 value = qemu_opt_get(all_opts, "cache");
890 if (value) {
891 int flags = 0;
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100892 bool writethrough;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200893
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100894 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200895 error_report("invalid cache option");
896 return NULL;
897 }
898
899 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200900 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
901 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100902 !writethrough, &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200903 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200904 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
905 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100906 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200907 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200908 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
909 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100910 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200911 }
912 qemu_opt_unset(all_opts, "cache");
913 }
914
Kevin Wolff298d072013-09-10 12:01:20 +0200915 /* Get a QDict for processing the options */
916 bs_opts = qdict_new();
917 qemu_opts_to_qdict(all_opts, bs_opts);
918
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800919 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
920 &error_abort);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200921 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100922 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100923 error_report_err(local_err);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200924 goto fail;
925 }
926
Kevin Wolf26929292013-09-09 17:01:03 +0200927 /* Deprecated option boot=[on|off] */
928 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
929 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
930 "ignored. Future versions will reject this parameter. Please "
931 "update your scripts.\n");
932 }
933
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200934 /* Media type */
935 value = qemu_opt_get(legacy_opts, "media");
936 if (value) {
937 if (!strcmp(value, "disk")) {
938 media = MEDIA_DISK;
939 } else if (!strcmp(value, "cdrom")) {
940 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800941 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200942 } else {
943 error_report("'%s' invalid media", value);
944 goto fail;
945 }
946 }
947
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200948 /* copy-on-read is disabled with a warning for read-only devices */
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300949 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200950 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
951
952 if (read_only && copy_on_read) {
953 error_report("warning: disabling copy-on-read on read-only drive");
954 copy_on_read = false;
955 }
956
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300957 qdict_put(bs_opts, BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200958 qstring_from_str(read_only ? "on" : "off"));
959 qdict_put(bs_opts, "copy-on-read",
960 qstring_from_str(copy_on_read ? "on" :"off"));
961
Kevin Wolf593d4642013-08-28 17:24:51 +0200962 /* Controller type */
963 value = qemu_opt_get(legacy_opts, "if");
964 if (value) {
965 for (type = 0;
966 type < IF_COUNT && strcmp(value, if_name[type]);
967 type++) {
968 }
969 if (type == IF_COUNT) {
970 error_report("unsupported bus type '%s'", value);
971 goto fail;
972 }
973 } else {
974 type = block_default_type;
975 }
976
Kevin Wolfb41a7332013-09-09 16:49:49 +0200977 /* Geometry */
978 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
979 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
980 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
981
982 if (cyls || heads || secs) {
983 if (cyls < 1) {
984 error_report("invalid physical cyls number");
985 goto fail;
986 }
987 if (heads < 1) {
988 error_report("invalid physical heads number");
989 goto fail;
990 }
991 if (secs < 1) {
992 error_report("invalid physical secs number");
993 goto fail;
994 }
995 }
996
997 translation = BIOS_ATA_TRANSLATION_AUTO;
998 value = qemu_opt_get(legacy_opts, "trans");
999 if (value != NULL) {
1000 if (!cyls) {
1001 error_report("'%s' trans must be used with cyls, heads and secs",
1002 value);
1003 goto fail;
1004 }
1005 if (!strcmp(value, "none")) {
1006 translation = BIOS_ATA_TRANSLATION_NONE;
1007 } else if (!strcmp(value, "lba")) {
1008 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01001009 } else if (!strcmp(value, "large")) {
1010 translation = BIOS_ATA_TRANSLATION_LARGE;
1011 } else if (!strcmp(value, "rechs")) {
1012 translation = BIOS_ATA_TRANSLATION_RECHS;
Kevin Wolfb41a7332013-09-09 16:49:49 +02001013 } else if (!strcmp(value, "auto")) {
1014 translation = BIOS_ATA_TRANSLATION_AUTO;
1015 } else {
1016 error_report("'%s' invalid translation type", value);
1017 goto fail;
1018 }
1019 }
1020
1021 if (media == MEDIA_CDROM) {
1022 if (cyls || secs || heads) {
1023 error_report("CHS can't be set with media=cdrom");
1024 goto fail;
1025 }
1026 }
1027
Kevin Wolf87a899c2013-09-10 15:48:13 +02001028 /* Device address specified by bus/unit or index.
1029 * If none was specified, try to find the first free one. */
1030 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
1031 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
1032 index = qemu_opt_get_number(legacy_opts, "index", -1);
1033
1034 max_devs = if_max_devs[type];
1035
1036 if (index != -1) {
1037 if (bus_id != 0 || unit_id != -1) {
1038 error_report("index cannot be used with bus and unit");
1039 goto fail;
1040 }
1041 bus_id = drive_index_to_bus_id(type, index);
1042 unit_id = drive_index_to_unit_id(type, index);
1043 }
1044
1045 if (unit_id == -1) {
1046 unit_id = 0;
1047 while (drive_get(type, bus_id, unit_id) != NULL) {
1048 unit_id++;
1049 if (max_devs && unit_id >= max_devs) {
1050 unit_id -= max_devs;
1051 bus_id++;
1052 }
1053 }
1054 }
1055
1056 if (max_devs && unit_id >= max_devs) {
1057 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1058 goto fail;
1059 }
1060
1061 if (drive_get(type, bus_id, unit_id) != NULL) {
1062 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1063 bus_id, unit_id, index);
1064 goto fail;
1065 }
1066
Kevin Wolfbcf83152014-06-06 15:21:48 +02001067 /* Serial number */
1068 serial = qemu_opt_get(legacy_opts, "serial");
1069
Kevin Wolf87a899c2013-09-10 15:48:13 +02001070 /* no id supplied -> create one */
1071 if (qemu_opts_id(all_opts) == NULL) {
1072 char *new_id;
1073 const char *mediastr = "";
1074 if (type == IF_IDE || type == IF_SCSI) {
1075 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1076 }
1077 if (max_devs) {
1078 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1079 mediastr, unit_id);
1080 } else {
1081 new_id = g_strdup_printf("%s%s%i", if_name[type],
1082 mediastr, unit_id);
1083 }
1084 qdict_put(bs_opts, "id", qstring_from_str(new_id));
1085 g_free(new_id);
1086 }
1087
Kevin Wolf394c7d42013-09-13 14:09:17 +02001088 /* Add virtio block device */
1089 devaddr = qemu_opt_get(legacy_opts, "addr");
1090 if (devaddr && type != IF_VIRTIO) {
1091 error_report("addr is not supported by this bus type");
1092 goto fail;
1093 }
1094
1095 if (type == IF_VIRTIO) {
1096 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001097 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1098 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001099 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +02001100 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001101 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001102 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001103 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001104 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1105 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001106 if (devaddr) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001107 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001108 }
1109 }
1110
Max Reitzd095b462013-12-20 19:28:14 +01001111 filename = qemu_opt_get(legacy_opts, "file");
1112
Kevin Wolfee13ed12014-02-09 09:52:32 +01001113 /* Check werror/rerror compatibility with if=... */
1114 werror = qemu_opt_get(legacy_opts, "werror");
1115 if (werror != NULL) {
1116 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1117 type != IF_NONE) {
1118 error_report("werror is not supported by this bus type");
1119 goto fail;
1120 }
1121 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1122 }
1123
1124 rerror = qemu_opt_get(legacy_opts, "rerror");
1125 if (rerror != NULL) {
1126 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1127 type != IF_NONE) {
1128 error_report("rerror is not supported by this bus type");
1129 goto fail;
1130 }
1131 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1132 }
1133
Kevin Wolf2d246f02013-09-18 15:14:47 +02001134 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster18e46a02014-10-07 13:59:06 +02001135 blk = blockdev_init(filename, bs_opts, &local_err);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001136 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +02001137 if (!blk) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001138 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001139 error_report_err(local_err);
Kevin Wolfb6810722013-09-20 11:33:11 +02001140 }
Kevin Wolf2d246f02013-09-18 15:14:47 +02001141 goto fail;
Kevin Wolfb6810722013-09-20 11:33:11 +02001142 } else {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001143 assert(!local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001144 }
1145
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001146 /* Create legacy DriveInfo */
1147 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001148 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001149
Kevin Wolfb41a7332013-09-09 16:49:49 +02001150 dinfo->cyls = cyls;
1151 dinfo->heads = heads;
1152 dinfo->secs = secs;
1153 dinfo->trans = translation;
1154
Kevin Wolfee13ed12014-02-09 09:52:32 +01001155 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001156 dinfo->bus = bus_id;
1157 dinfo->unit = unit_id;
Kevin Wolf394c7d42013-09-13 14:09:17 +02001158 dinfo->devaddr = devaddr;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001159 dinfo->serial = g_strdup(serial);
1160
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001161 blk_set_legacy_dinfo(blk, dinfo);
1162
Kevin Wolfe34ef042013-09-19 14:24:10 +02001163 switch(type) {
1164 case IF_IDE:
1165 case IF_SCSI:
1166 case IF_XEN:
1167 case IF_NONE:
1168 dinfo->media_cd = media == MEDIA_CDROM;
1169 break;
1170 default:
1171 break;
1172 }
1173
Kevin Wolf2d246f02013-09-18 15:14:47 +02001174fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001175 qemu_opts_del(legacy_opts);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001176 QDECREF(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001177 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001178}
1179
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02001180static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1181{
1182 BlockDriverState *bs;
1183
1184 bs = bdrv_lookup_bs(name, name, errp);
1185 if (bs == NULL) {
1186 return NULL;
1187 }
1188
1189 if (!bdrv_is_root_node(bs)) {
1190 error_setg(errp, "Need a root block node");
1191 return NULL;
1192 }
1193
1194 if (!bdrv_is_inserted(bs)) {
1195 error_setg(errp, "Device has no medium");
1196 return NULL;
1197 }
1198
1199 return bs;
1200}
1201
Kevin Wolfb33945c2016-09-20 13:38:43 +02001202static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id,
1203 Error **errp)
1204{
1205 BlockBackend *blk;
1206
1207 if (!blk_name == !qdev_id) {
1208 error_setg(errp, "Need exactly one of 'device' and 'id'");
1209 return NULL;
1210 }
1211
1212 if (qdev_id) {
1213 blk = blk_by_qdev_id(qdev_id, errp);
1214 } else {
1215 blk = blk_by_name(blk_name);
1216 if (blk == NULL) {
1217 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1218 "Device '%s' not found", blk_name);
1219 }
1220 }
1221
1222 return blk;
1223}
1224
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001225void hmp_commit(Monitor *mon, const QDict *qdict)
Markus Armbruster666daa62010-06-02 18:48:27 +02001226{
Markus Armbruster666daa62010-06-02 18:48:27 +02001227 const char *device = qdict_get_str(qdict, "device");
Fam Zhenga0e85442015-03-02 19:36:48 +08001228 BlockBackend *blk;
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001229 int ret;
Markus Armbruster666daa62010-06-02 18:48:27 +02001230
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001231 if (!strcmp(device, "all")) {
Max Reitzda31d592016-03-16 19:54:32 +01001232 ret = blk_commit_all();
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001233 } else {
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001234 BlockDriverState *bs;
1235 AioContext *aio_context;
1236
Fam Zhenga0e85442015-03-02 19:36:48 +08001237 blk = blk_by_name(device);
1238 if (!blk) {
Jeff Cody58513bd2013-01-18 12:45:35 -05001239 monitor_printf(mon, "Device '%s' not found\n", device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +02001240 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001241 }
Max Reitz5433c242015-10-19 17:53:29 +02001242 if (!blk_is_available(blk)) {
1243 monitor_printf(mon, "Device '%s' has no medium\n", device);
1244 return;
1245 }
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001246
1247 bs = blk_bs(blk);
1248 aio_context = bdrv_get_aio_context(bs);
1249 aio_context_acquire(aio_context);
1250
1251 ret = bdrv_commit(bs);
1252
1253 aio_context_release(aio_context);
Jeff Cody58513bd2013-01-18 12:45:35 -05001254 }
1255 if (ret < 0) {
1256 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1257 strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +02001258 }
1259}
1260
Eric Blake10f75902016-03-03 09:16:50 -07001261static void blockdev_do_action(TransactionAction *action, Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001262{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001263 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001264
Eric Blake10f75902016-03-03 09:16:50 -07001265 list.value = action;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001266 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001267 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001268}
1269
Benoît Canet0901f672014-01-23 21:31:38 +01001270void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1271 bool has_node_name, const char *node_name,
1272 const char *snapshot_file,
1273 bool has_snapshot_node_name,
1274 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001275 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001276 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001277{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001278 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001279 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001280 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001281 .has_node_name = has_node_name,
1282 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001283 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001284 .has_snapshot_node_name = has_snapshot_node_name,
1285 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001286 .has_format = has_format,
1287 .format = (char *) format,
1288 .has_mode = has_mode,
1289 .mode = mode,
1290 };
Eric Blake10f75902016-03-03 09:16:50 -07001291 TransactionAction action = {
1292 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001293 .u.blockdev_snapshot_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001294 };
1295 blockdev_do_action(&action, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001296}
1297
Alberto Garcia43de7e22015-10-26 14:27:16 +02001298void qmp_blockdev_snapshot(const char *node, const char *overlay,
1299 Error **errp)
1300{
1301 BlockdevSnapshot snapshot_data = {
1302 .node = (char *) node,
1303 .overlay = (char *) overlay
1304 };
Eric Blake10f75902016-03-03 09:16:50 -07001305 TransactionAction action = {
1306 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
Eric Blake32bafa82016-03-17 16:48:37 -06001307 .u.blockdev_snapshot.data = &snapshot_data,
Eric Blake10f75902016-03-03 09:16:50 -07001308 };
1309 blockdev_do_action(&action, errp);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001310}
1311
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001312void qmp_blockdev_snapshot_internal_sync(const char *device,
1313 const char *name,
1314 Error **errp)
1315{
1316 BlockdevSnapshotInternal snapshot = {
1317 .device = (char *) device,
1318 .name = (char *) name
1319 };
Eric Blake10f75902016-03-03 09:16:50 -07001320 TransactionAction action = {
1321 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001322 .u.blockdev_snapshot_internal_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001323 };
1324 blockdev_do_action(&action, errp);
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001325}
1326
Wenchao Xia44e3e052013-09-11 14:04:36 +08001327SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1328 bool has_id,
1329 const char *id,
1330 bool has_name,
1331 const char *name,
1332 Error **errp)
1333{
Fam Zhenga0e85442015-03-02 19:36:48 +08001334 BlockDriverState *bs;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001335 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001336 QEMUSnapshotInfo sn;
1337 Error *local_err = NULL;
1338 SnapshotInfo *info = NULL;
1339 int ret;
1340
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001341 bs = qmp_get_root_bs(device, errp);
1342 if (!bs) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001343 return NULL;
1344 }
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001345 aio_context = bdrv_get_aio_context(bs);
Max Reitz5433c242015-10-19 17:53:29 +02001346 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001347
1348 if (!has_id) {
1349 id = NULL;
1350 }
1351
1352 if (!has_name) {
1353 name = NULL;
1354 }
1355
1356 if (!id && !name) {
1357 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001358 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001359 }
1360
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001361 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1362 goto out_aio_context;
1363 }
1364
Wenchao Xia44e3e052013-09-11 14:04:36 +08001365 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001366 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001367 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001368 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001369 }
1370 if (!ret) {
1371 error_setg(errp,
1372 "Snapshot with id '%s' and name '%s' does not exist on "
1373 "device '%s'",
1374 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001375 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001376 }
1377
1378 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001379 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001380 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001381 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001382 }
1383
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001384 aio_context_release(aio_context);
1385
Markus Armbruster5839e532014-08-19 10:31:08 +02001386 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001387 info->id = g_strdup(sn.id_str);
1388 info->name = g_strdup(sn.name);
1389 info->date_nsec = sn.date_nsec;
1390 info->date_sec = sn.date_sec;
1391 info->vm_state_size = sn.vm_state_size;
1392 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1393 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1394
1395 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001396
1397out_aio_context:
1398 aio_context_release(aio_context);
1399 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001400}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001401
John Snow341ebc22015-04-17 19:49:52 -04001402/**
1403 * block_dirty_bitmap_lookup:
1404 * Return a dirty bitmap (if present), after validating
1405 * the node reference and bitmap names.
1406 *
1407 * @node: The name of the BDS node to search for bitmaps
1408 * @name: The name of the bitmap to search for
1409 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1410 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1411 * @errp: Output pointer for error information. Can be NULL.
1412 *
1413 * @return: A bitmap object on success, or NULL on failure.
1414 */
1415static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1416 const char *name,
1417 BlockDriverState **pbs,
1418 AioContext **paio,
1419 Error **errp)
1420{
1421 BlockDriverState *bs;
1422 BdrvDirtyBitmap *bitmap;
1423 AioContext *aio_context;
1424
1425 if (!node) {
1426 error_setg(errp, "Node cannot be NULL");
1427 return NULL;
1428 }
1429 if (!name) {
1430 error_setg(errp, "Bitmap name cannot be NULL");
1431 return NULL;
1432 }
1433 bs = bdrv_lookup_bs(node, node, NULL);
1434 if (!bs) {
1435 error_setg(errp, "Node '%s' not found", node);
1436 return NULL;
1437 }
1438
1439 aio_context = bdrv_get_aio_context(bs);
1440 aio_context_acquire(aio_context);
1441
1442 bitmap = bdrv_find_dirty_bitmap(bs, name);
1443 if (!bitmap) {
1444 error_setg(errp, "Dirty bitmap '%s' not found", name);
1445 goto fail;
1446 }
1447
1448 if (pbs) {
1449 *pbs = bs;
1450 }
1451 if (paio) {
1452 *paio = aio_context;
1453 } else {
1454 aio_context_release(aio_context);
1455 }
1456
1457 return bitmap;
1458
1459 fail:
1460 aio_context_release(aio_context);
1461 return NULL;
1462}
1463
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001464/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001465
John Snow50f43f02015-11-05 18:13:09 -05001466typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001467
John Snow50f43f02015-11-05 18:13:09 -05001468/**
1469 * BlkActionOps:
1470 * Table of operations that define an Action.
1471 *
1472 * @instance_size: Size of state struct, in bytes.
1473 * @prepare: Prepare the work, must NOT be NULL.
1474 * @commit: Commit the changes, can be NULL.
1475 * @abort: Abort the changes on fail, can be NULL.
1476 * @clean: Clean up resources after all transaction actions have called
1477 * commit() or abort(). Can be NULL.
1478 *
1479 * Only prepare() may fail. In a single transaction, only one of commit() or
1480 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001481 */
John Snow50f43f02015-11-05 18:13:09 -05001482typedef struct BlkActionOps {
1483 size_t instance_size;
1484 void (*prepare)(BlkActionState *common, Error **errp);
1485 void (*commit)(BlkActionState *common);
1486 void (*abort)(BlkActionState *common);
1487 void (*clean)(BlkActionState *common);
1488} BlkActionOps;
1489
1490/**
1491 * BlkActionState:
1492 * Describes one Action's state within a Transaction.
1493 *
1494 * @action: QAPI-defined enum identifying which Action to perform.
1495 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001496 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001497 * @entry: List membership for all Actions in this Transaction.
1498 *
1499 * This structure must be arranged as first member in a subclassed type,
1500 * assuming that the compiler will also arrange it to the same offsets as the
1501 * base class.
1502 */
1503struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001504 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001505 const BlkActionOps *ops;
John Snow94d16a62015-11-05 18:13:18 -05001506 BlockJobTxn *block_job_txn;
1507 TransactionProperties *txn_props;
John Snow50f43f02015-11-05 18:13:09 -05001508 QSIMPLEQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001509};
1510
Wenchao Xiabbe86012013-09-11 14:04:34 +08001511/* internal snapshot private data */
1512typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001513 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001514 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001515 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001516 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001517 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001518} InternalSnapshotState;
1519
John Snow94d16a62015-11-05 18:13:18 -05001520
1521static int action_check_completion_mode(BlkActionState *s, Error **errp)
1522{
1523 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1524 error_setg(errp,
1525 "Action '%s' does not support Transaction property "
1526 "completion-mode = %s",
1527 TransactionActionKind_lookup[s->action->type],
1528 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1529 return -1;
1530 }
1531 return 0;
1532}
1533
John Snow50f43f02015-11-05 18:13:09 -05001534static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001535 Error **errp)
1536{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001537 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001538 const char *device;
1539 const char *name;
1540 BlockDriverState *bs;
1541 QEMUSnapshotInfo old_sn, *sn;
1542 bool ret;
1543 qemu_timeval tv;
1544 BlockdevSnapshotInternal *internal;
1545 InternalSnapshotState *state;
1546 int ret1;
1547
Eric Blake6a8f9662015-10-26 16:34:54 -06001548 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001549 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001550 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001551 state = DO_UPCAST(InternalSnapshotState, common, common);
1552
1553 /* 1. parse input */
1554 device = internal->device;
1555 name = internal->name;
1556
1557 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001558 if (action_check_completion_mode(common, errp) < 0) {
1559 return;
1560 }
1561
Kevin Wolf75dfd402016-06-23 14:20:24 +02001562 bs = qmp_get_root_bs(device, errp);
1563 if (!bs) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001564 return;
1565 }
1566
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001567 /* AioContext is released in .clean() */
Kevin Wolf75dfd402016-06-23 14:20:24 +02001568 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001569 aio_context_acquire(state->aio_context);
1570
Fam Zheng507306c2015-10-23 11:08:13 +08001571 state->bs = bs;
1572 bdrv_drained_begin(bs);
1573
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001574 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1575 return;
1576 }
1577
Wenchao Xiabbe86012013-09-11 14:04:34 +08001578 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001579 error_setg(errp, "Device '%s' is read only", device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001580 return;
1581 }
1582
1583 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001584 error_setg(errp, "Block format '%s' used by device '%s' "
1585 "does not support internal snapshots",
1586 bs->drv->format_name, device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001587 return;
1588 }
1589
1590 if (!strlen(name)) {
1591 error_setg(errp, "Name is empty");
1592 return;
1593 }
1594
1595 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001596 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1597 &local_err);
1598 if (local_err) {
1599 error_propagate(errp, local_err);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001600 return;
1601 } else if (ret) {
1602 error_setg(errp,
1603 "Snapshot with name '%s' already exists on device '%s'",
1604 name, device);
1605 return;
1606 }
1607
1608 /* 3. take the snapshot */
1609 sn = &state->sn;
1610 pstrcpy(sn->name, sizeof(sn->name), name);
1611 qemu_gettimeofday(&tv);
1612 sn->date_sec = tv.tv_sec;
1613 sn->date_nsec = tv.tv_usec * 1000;
1614 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1615
1616 ret1 = bdrv_snapshot_create(bs, sn);
1617 if (ret1 < 0) {
1618 error_setg_errno(errp, -ret1,
1619 "Failed to create snapshot '%s' on device '%s'",
1620 name, device);
1621 return;
1622 }
1623
1624 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001625 state->created = true;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001626}
1627
John Snow50f43f02015-11-05 18:13:09 -05001628static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001629{
1630 InternalSnapshotState *state =
1631 DO_UPCAST(InternalSnapshotState, common, common);
1632 BlockDriverState *bs = state->bs;
1633 QEMUSnapshotInfo *sn = &state->sn;
1634 Error *local_error = NULL;
1635
Fam Zheng507306c2015-10-23 11:08:13 +08001636 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001637 return;
1638 }
1639
1640 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001641 error_reportf_err(local_error,
1642 "Failed to delete snapshot with id '%s' and "
1643 "name '%s' on device '%s' in abort: ",
1644 sn->id_str, sn->name,
1645 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001646 }
1647}
1648
John Snow50f43f02015-11-05 18:13:09 -05001649static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001650{
1651 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1652 common, common);
1653
1654 if (state->aio_context) {
Fam Zheng507306c2015-10-23 11:08:13 +08001655 if (state->bs) {
1656 bdrv_drained_end(state->bs);
1657 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001658 aio_context_release(state->aio_context);
1659 }
1660}
1661
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001662/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001663typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001664 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001665 BlockDriverState *old_bs;
1666 BlockDriverState *new_bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001667 AioContext *aio_context;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001668} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001669
John Snow50f43f02015-11-05 18:13:09 -05001670static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001671 Error **errp)
1672{
Max Reitz5b363932016-05-17 16:41:31 +02001673 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001674 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001675 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001676 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001677 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001678 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001679 /* Reference to the new image (for 'blockdev-snapshot') */
1680 const char *snapshot_ref;
1681 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001682 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001683 ExternalSnapshotState *state =
1684 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001685 TransactionAction *action = common->action;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001686
Alberto Garcia43de7e22015-10-26 14:27:16 +02001687 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1688 * purpose but a different set of parameters */
1689 switch (action->type) {
1690 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1691 {
Eric Blake32bafa82016-03-17 16:48:37 -06001692 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001693 device = s->node;
1694 node_name = s->node;
1695 new_image_file = NULL;
1696 snapshot_ref = s->overlay;
1697 }
1698 break;
1699 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1700 {
Eric Blake32bafa82016-03-17 16:48:37 -06001701 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001702 device = s->has_device ? s->device : NULL;
1703 node_name = s->has_node_name ? s->node_name : NULL;
1704 new_image_file = s->snapshot_file;
1705 snapshot_ref = NULL;
1706 }
1707 break;
1708 default:
1709 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001710 }
1711
1712 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001713 if (action_check_completion_mode(common, errp) < 0) {
1714 return;
1715 }
1716
Alberto Garcia43de7e22015-10-26 14:27:16 +02001717 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1718 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001719 return;
1720 }
1721
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001722 /* Acquire AioContext now so any threads operating on old_bs stop */
1723 state->aio_context = bdrv_get_aio_context(state->old_bs);
1724 aio_context_acquire(state->aio_context);
Fam Zhengda763e82015-10-23 11:08:10 +08001725 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001726
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001727 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001728 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001729 return;
1730 }
1731
Fam Zheng3718d8a2014-05-23 21:29:43 +08001732 if (bdrv_op_is_blocked(state->old_bs,
1733 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001734 return;
1735 }
1736
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001737 if (!bdrv_is_read_only(state->old_bs)) {
1738 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001739 error_setg(errp, QERR_IO_ERROR);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001740 return;
1741 }
1742 }
1743
Benoît Canet212a5a82014-01-23 21:31:36 +01001744 if (!bdrv_is_first_non_filter(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001745 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
Benoît Canetf6186f42013-10-02 14:33:48 +02001746 return;
1747 }
1748
Alberto Garcia43de7e22015-10-26 14:27:16 +02001749 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001750 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001751 const char *format = s->has_format ? s->format : "qcow2";
1752 enum NewImageMode mode;
1753 const char *snapshot_node_name =
1754 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001755
Alberto Garcia43de7e22015-10-26 14:27:16 +02001756 if (node_name && !snapshot_node_name) {
1757 error_setg(errp, "New snapshot node name missing");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001758 return;
1759 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001760
1761 if (snapshot_node_name &&
1762 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1763 error_setg(errp, "New snapshot node name already in use");
1764 return;
1765 }
1766
1767 flags = state->old_bs->open_flags;
Kevin Wolf4c844982016-02-29 13:12:26 +01001768 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001769
1770 /* create new image w/backing file */
1771 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1772 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001773 int64_t size = bdrv_getlength(state->old_bs);
1774 if (size < 0) {
1775 error_setg_errno(errp, -size, "bdrv_getlength failed");
1776 return;
1777 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001778 bdrv_img_create(new_image_file, format,
1779 state->old_bs->filename,
1780 state->old_bs->drv->format_name,
Kevin Wolff86b8b52016-03-02 12:16:44 +01001781 NULL, size, flags, &local_err, false);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001782 if (local_err) {
1783 error_propagate(errp, local_err);
1784 return;
1785 }
1786 }
1787
1788 options = qdict_new();
1789 if (s->has_snapshot_node_name) {
1790 qdict_put(options, "node-name",
1791 qstring_from_str(snapshot_node_name));
1792 }
1793 qdict_put(options, "driver", qstring_from_str(format));
1794
1795 flags |= BDRV_O_NO_BACKING;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001796 }
1797
Max Reitz5b363932016-05-17 16:41:31 +02001798 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1799 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001800 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001801 if (!state->new_bs) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001802 return;
1803 }
1804
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001805 if (bdrv_has_blk(state->new_bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02001806 error_setg(errp, "The snapshot is already in use");
Alberto Garcia43de7e22015-10-26 14:27:16 +02001807 return;
1808 }
1809
1810 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1811 errp)) {
1812 return;
1813 }
1814
1815 if (state->new_bs->backing != NULL) {
1816 error_setg(errp, "The snapshot already has a backing image");
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001817 return;
1818 }
1819
1820 if (!state->new_bs->drv->supports_backing) {
1821 error_setg(errp, "The snapshot does not support backing images");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001822 }
1823}
1824
John Snow50f43f02015-11-05 18:13:09 -05001825static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001826{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001827 ExternalSnapshotState *state =
1828 DO_UPCAST(ExternalSnapshotState, common, common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001829
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001830 bdrv_set_aio_context(state->new_bs, state->aio_context);
1831
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001832 /* This removes our old bs and adds the new bs */
1833 bdrv_append(state->new_bs, state->old_bs);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001834 /* We don't need (or want) to use the transactional
1835 * bdrv_reopen_multiple() across all the entries at once, because we
1836 * don't want to abort all of them if one of them fails the reopen */
Kevin Wolf4c844982016-02-29 13:12:26 +01001837 if (!state->old_bs->copy_on_read) {
1838 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1839 NULL);
1840 }
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001841}
1842
John Snow50f43f02015-11-05 18:13:09 -05001843static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001844{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001845 ExternalSnapshotState *state =
1846 DO_UPCAST(ExternalSnapshotState, common, common);
1847 if (state->new_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001848 bdrv_unref(state->new_bs);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001849 }
Fam Zhengda763e82015-10-23 11:08:10 +08001850}
1851
John Snow50f43f02015-11-05 18:13:09 -05001852static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001853{
1854 ExternalSnapshotState *state =
1855 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001856 if (state->aio_context) {
Fam Zhengda763e82015-10-23 11:08:10 +08001857 bdrv_drained_end(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001858 aio_context_release(state->aio_context);
1859 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001860}
1861
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001862typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001863 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001864 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001865 AioContext *aio_context;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001866 BlockJob *job;
1867} DriveBackupState;
1868
Pavel Butsykin81206a82016-07-22 11:17:50 +03001869static void do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
1870 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001871
John Snow50f43f02015-11-05 18:13:09 -05001872static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001873{
1874 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001875 BlockDriverState *bs;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001876 DriveBackup *backup;
1877 Error *local_err = NULL;
1878
Eric Blake6a8f9662015-10-26 16:34:54 -06001879 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001880 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001881
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001882 bs = qmp_get_root_bs(backup->device, errp);
1883 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001884 return;
1885 }
1886
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001887 /* AioContext is released in .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001888 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001889 aio_context_acquire(state->aio_context);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001890 bdrv_drained_begin(bs);
1891 state->bs = bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001892
Pavel Butsykin81206a82016-07-22 11:17:50 +03001893 do_drive_backup(backup, common->block_job_txn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001894 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001895 error_propagate(errp, local_err);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001896 return;
1897 }
1898
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001899 state->job = state->bs->job;
1900}
1901
John Snow50f43f02015-11-05 18:13:09 -05001902static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001903{
1904 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1905 BlockDriverState *bs = state->bs;
1906
1907 /* Only cancel if it's the job we started */
1908 if (bs && bs->job && bs->job == state->job) {
1909 block_job_cancel_sync(bs->job);
1910 }
1911}
1912
John Snow50f43f02015-11-05 18:13:09 -05001913static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001914{
1915 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1916
1917 if (state->aio_context) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001918 bdrv_drained_end(state->bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001919 aio_context_release(state->aio_context);
1920 }
1921}
1922
Fam Zhengbd8baec2014-12-18 18:37:06 +08001923typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001924 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001925 BlockDriverState *bs;
1926 BlockJob *job;
1927 AioContext *aio_context;
1928} BlockdevBackupState;
1929
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03001930static void do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
1931 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001932
John Snow50f43f02015-11-05 18:13:09 -05001933static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001934{
1935 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1936 BlockdevBackup *backup;
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001937 BlockDriverState *bs, *target;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001938 Error *local_err = NULL;
1939
Eric Blake6a8f9662015-10-26 16:34:54 -06001940 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001941 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001942
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001943 bs = qmp_get_root_bs(backup->device, errp);
1944 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001945 return;
1946 }
1947
Kevin Wolf39d990a2016-08-02 19:22:04 +02001948 target = bdrv_lookup_bs(backup->target, backup->target, errp);
Max Reitz5433c242015-10-19 17:53:29 +02001949 if (!target) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001950 return;
1951 }
1952
1953 /* AioContext is released in .clean() */
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001954 state->aio_context = bdrv_get_aio_context(bs);
Kevin Wolf39d990a2016-08-02 19:22:04 +02001955 if (state->aio_context != bdrv_get_aio_context(target)) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001956 state->aio_context = NULL;
1957 error_setg(errp, "Backup between two IO threads is not implemented");
1958 return;
1959 }
1960 aio_context_acquire(state->aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001961 state->bs = bs;
Fam Zhengff52bf32015-10-23 11:08:12 +08001962 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001963
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03001964 do_blockdev_backup(backup, common->block_job_txn, &local_err);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001965 if (local_err) {
1966 error_propagate(errp, local_err);
1967 return;
1968 }
1969
Fam Zhengbd8baec2014-12-18 18:37:06 +08001970 state->job = state->bs->job;
1971}
1972
John Snow50f43f02015-11-05 18:13:09 -05001973static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001974{
1975 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1976 BlockDriverState *bs = state->bs;
1977
1978 /* Only cancel if it's the job we started */
1979 if (bs && bs->job && bs->job == state->job) {
1980 block_job_cancel_sync(bs->job);
1981 }
1982}
1983
John Snow50f43f02015-11-05 18:13:09 -05001984static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001985{
1986 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1987
1988 if (state->aio_context) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001989 bdrv_drained_end(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001990 aio_context_release(state->aio_context);
1991 }
1992}
1993
Fam Zhengdf9a6812015-11-09 18:16:54 +08001994typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05001995 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001996 BdrvDirtyBitmap *bitmap;
1997 BlockDriverState *bs;
1998 AioContext *aio_context;
1999 HBitmap *backup;
2000 bool prepared;
2001} BlockDirtyBitmapState;
2002
John Snow50f43f02015-11-05 18:13:09 -05002003static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002004 Error **errp)
2005{
2006 Error *local_err = NULL;
2007 BlockDirtyBitmapAdd *action;
2008 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2009 common, common);
2010
John Snow94d16a62015-11-05 18:13:18 -05002011 if (action_check_completion_mode(common, errp) < 0) {
2012 return;
2013 }
2014
Eric Blake32bafa82016-03-17 16:48:37 -06002015 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002016 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2017 qmp_block_dirty_bitmap_add(action->node, action->name,
2018 action->has_granularity, action->granularity,
2019 &local_err);
2020
2021 if (!local_err) {
2022 state->prepared = true;
2023 } else {
2024 error_propagate(errp, local_err);
2025 }
2026}
2027
John Snow50f43f02015-11-05 18:13:09 -05002028static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002029{
2030 BlockDirtyBitmapAdd *action;
2031 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2032 common, common);
2033
Eric Blake32bafa82016-03-17 16:48:37 -06002034 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002035 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2036 * then the node reference and bitmap name must have been valid.
2037 */
2038 if (state->prepared) {
2039 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2040 }
2041}
2042
John Snow50f43f02015-11-05 18:13:09 -05002043static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002044 Error **errp)
2045{
2046 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2047 common, common);
2048 BlockDirtyBitmap *action;
2049
John Snow94d16a62015-11-05 18:13:18 -05002050 if (action_check_completion_mode(common, errp) < 0) {
2051 return;
2052 }
2053
Eric Blake32bafa82016-03-17 16:48:37 -06002054 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002055 state->bitmap = block_dirty_bitmap_lookup(action->node,
2056 action->name,
2057 &state->bs,
2058 &state->aio_context,
2059 errp);
2060 if (!state->bitmap) {
2061 return;
2062 }
2063
2064 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2065 error_setg(errp, "Cannot modify a frozen bitmap");
2066 return;
2067 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2068 error_setg(errp, "Cannot clear a disabled bitmap");
2069 return;
2070 }
2071
2072 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2073 /* AioContext is released in .clean() */
2074}
2075
John Snow50f43f02015-11-05 18:13:09 -05002076static void block_dirty_bitmap_clear_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002077{
2078 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2079 common, common);
2080
2081 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2082}
2083
John Snow50f43f02015-11-05 18:13:09 -05002084static void block_dirty_bitmap_clear_commit(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002085{
2086 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2087 common, common);
2088
2089 hbitmap_free(state->backup);
2090}
2091
John Snow50f43f02015-11-05 18:13:09 -05002092static void block_dirty_bitmap_clear_clean(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002093{
2094 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2095 common, common);
2096
2097 if (state->aio_context) {
2098 aio_context_release(state->aio_context);
2099 }
2100}
2101
John Snow50f43f02015-11-05 18:13:09 -05002102static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002103{
2104 error_setg(errp, "Transaction aborted using Abort action");
2105}
2106
John Snow50f43f02015-11-05 18:13:09 -05002107static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002108{
Stefan Weildfc6f862013-07-25 18:21:28 +02002109 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002110}
2111
John Snow50f43f02015-11-05 18:13:09 -05002112static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002113 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2114 .instance_size = sizeof(ExternalSnapshotState),
2115 .prepare = external_snapshot_prepare,
2116 .commit = external_snapshot_commit,
2117 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002118 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002119 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002120 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002121 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002122 .prepare = external_snapshot_prepare,
2123 .commit = external_snapshot_commit,
2124 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002125 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002126 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002127 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2128 .instance_size = sizeof(DriveBackupState),
2129 .prepare = drive_backup_prepare,
2130 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002131 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002132 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002133 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2134 .instance_size = sizeof(BlockdevBackupState),
2135 .prepare = blockdev_backup_prepare,
2136 .abort = blockdev_backup_abort,
2137 .clean = blockdev_backup_clean,
2138 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002139 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002140 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002141 .prepare = abort_prepare,
2142 .commit = abort_commit,
2143 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002144 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2145 .instance_size = sizeof(InternalSnapshotState),
2146 .prepare = internal_snapshot_prepare,
2147 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002148 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002149 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002150 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2151 .instance_size = sizeof(BlockDirtyBitmapState),
2152 .prepare = block_dirty_bitmap_add_prepare,
2153 .abort = block_dirty_bitmap_add_abort,
2154 },
2155 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2156 .instance_size = sizeof(BlockDirtyBitmapState),
2157 .prepare = block_dirty_bitmap_clear_prepare,
2158 .commit = block_dirty_bitmap_clear_commit,
2159 .abort = block_dirty_bitmap_clear_abort,
2160 .clean = block_dirty_bitmap_clear_clean,
2161 }
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002162};
2163
John Snow94d16a62015-11-05 18:13:18 -05002164/**
2165 * Allocate a TransactionProperties structure if necessary, and fill
2166 * that structure with desired defaults if they are unset.
2167 */
2168static TransactionProperties *get_transaction_properties(
2169 TransactionProperties *props)
2170{
2171 if (!props) {
2172 props = g_new0(TransactionProperties, 1);
2173 }
2174
2175 if (!props->has_completion_mode) {
2176 props->has_completion_mode = true;
2177 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2178 }
2179
2180 return props;
2181}
2182
Jeff Cody8802d1f2012-02-28 15:54:06 -05002183/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002184 * 'Atomic' group operations. The operations are performed as a set, and if
2185 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002186 */
John Snow94d16a62015-11-05 18:13:18 -05002187void qmp_transaction(TransactionActionList *dev_list,
2188 bool has_props,
2189 struct TransactionProperties *props,
2190 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002191{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002192 TransactionActionList *dev_entry = dev_list;
John Snow94d16a62015-11-05 18:13:18 -05002193 BlockJobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002194 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002195 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002196
John Snow50f43f02015-11-05 18:13:09 -05002197 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002198 QSIMPLEQ_INIT(&snap_bdrv_states);
2199
John Snow94d16a62015-11-05 18:13:18 -05002200 /* Does this transaction get canceled as a group on failure?
2201 * If not, we don't really need to make a BlockJobTxn.
2202 */
2203 props = get_transaction_properties(props);
2204 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2205 block_job_txn = block_job_txn_new();
2206 }
2207
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002208 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002209 bdrv_drain_all();
2210
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002211 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002212 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002213 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002214 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002215
Jeff Cody8802d1f2012-02-28 15:54:06 -05002216 dev_info = dev_entry->value;
2217 dev_entry = dev_entry->next;
2218
Eric Blake6a8f9662015-10-26 16:34:54 -06002219 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002220
Eric Blake6a8f9662015-10-26 16:34:54 -06002221 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002222 assert(ops->instance_size > 0);
2223
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002224 state = g_malloc0(ops->instance_size);
2225 state->ops = ops;
2226 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002227 state->block_job_txn = block_job_txn;
2228 state->txn_props = props;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002229 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002230
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002231 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002232 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002233 error_propagate(errp, local_err);
2234 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002235 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002236 }
2237
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002238 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002239 if (state->ops->commit) {
2240 state->ops->commit(state);
2241 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002242 }
2243
2244 /* success */
2245 goto exit;
2246
2247delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002248 /* failure, and it is all-or-none; roll back all operations */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002249 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2250 if (state->ops->abort) {
2251 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002252 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002253 }
2254exit:
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002255 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2256 if (state->ops->clean) {
2257 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002258 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002259 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002260 }
John Snow94d16a62015-11-05 18:13:18 -05002261 if (!has_props) {
2262 qapi_free_TransactionProperties(props);
2263 }
2264 block_job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002265}
2266
Kevin Wolffbe2d812016-09-20 13:38:46 +02002267void qmp_eject(bool has_device, const char *device,
2268 bool has_id, const char *id,
2269 bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002270{
Max Reitz38f54bd2015-10-26 21:39:12 +01002271 Error *local_err = NULL;
John Snow3a3086b2016-05-18 17:53:40 -04002272 int rc;
Markus Armbruster666daa62010-06-02 18:48:27 +02002273
John Snow3a3086b2016-05-18 17:53:40 -04002274 if (!has_force) {
2275 force = false;
2276 }
2277
Kevin Wolffbe2d812016-09-20 13:38:46 +02002278 rc = do_open_tray(has_device ? device : NULL,
2279 has_id ? id : NULL,
2280 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002281 if (rc && rc != -ENOSYS) {
Max Reitz38f54bd2015-10-26 21:39:12 +01002282 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002283 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002284 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002285 error_free(local_err);
John Snow3a3086b2016-05-18 17:53:40 -04002286
Kevin Wolffbe2d812016-09-20 13:38:46 +02002287 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002288}
2289
Benoît Canet12d3ba82014-01-23 21:31:35 +01002290void qmp_block_passwd(bool has_device, const char *device,
2291 bool has_node_name, const char *node_name,
2292 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002293{
Benoît Canet12d3ba82014-01-23 21:31:35 +01002294 Error *local_err = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +02002295 BlockDriverState *bs;
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002296 AioContext *aio_context;
Markus Armbruster666daa62010-06-02 18:48:27 +02002297
Benoît Canet12d3ba82014-01-23 21:31:35 +01002298 bs = bdrv_lookup_bs(has_device ? device : NULL,
2299 has_node_name ? node_name : NULL,
2300 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002301 if (local_err) {
Benoît Canet12d3ba82014-01-23 21:31:35 +01002302 error_propagate(errp, local_err);
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02002303 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002304 }
2305
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002306 aio_context = bdrv_get_aio_context(bs);
2307 aio_context_acquire(aio_context);
2308
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002309 bdrv_add_key(bs, password, errp);
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002310
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002311 aio_context_release(aio_context);
Markus Armbruster666daa62010-06-02 18:48:27 +02002312}
2313
Colin Lordbf18bee2016-06-06 14:15:22 -04002314/*
2315 * Attempt to open the tray of @device.
2316 * If @force, ignore its tray lock.
2317 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2318 * On error, store an error through @errp and return -errno.
2319 * If @device does not exist, return -ENODEV.
2320 * If it has no removable media, return -ENOTSUP.
2321 * If it has no tray, return -ENOSYS.
2322 * If the guest was asked to open the tray, return -EINPROGRESS.
2323 * Else, return 0.
John Snow3a3086b2016-05-18 17:53:40 -04002324 */
Kevin Wolfb33945c2016-09-20 13:38:43 +02002325static int do_open_tray(const char *blk_name, const char *qdev_id,
2326 bool force, Error **errp)
Max Reitz7d8a9f72015-10-26 21:39:08 +01002327{
2328 BlockBackend *blk;
Kevin Wolfb33945c2016-09-20 13:38:43 +02002329 const char *device = qdev_id ?: blk_name;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002330 bool locked;
2331
Kevin Wolfb33945c2016-09-20 13:38:43 +02002332 blk = qmp_get_blk(blk_name, qdev_id, errp);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002333 if (!blk) {
John Snow3a3086b2016-05-18 17:53:40 -04002334 return -ENODEV;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002335 }
2336
2337 if (!blk_dev_has_removable_media(blk)) {
2338 error_setg(errp, "Device '%s' is not removable", device);
John Snow3a3086b2016-05-18 17:53:40 -04002339 return -ENOTSUP;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002340 }
2341
Max Reitz12c7ec82016-01-29 20:49:11 +01002342 if (!blk_dev_has_tray(blk)) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002343 error_setg(errp, "Device '%s' does not have a tray", device);
2344 return -ENOSYS;
Max Reitz12c7ec82016-01-29 20:49:11 +01002345 }
2346
Max Reitz7d8a9f72015-10-26 21:39:08 +01002347 if (blk_dev_is_tray_open(blk)) {
John Snow3a3086b2016-05-18 17:53:40 -04002348 return 0;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002349 }
2350
2351 locked = blk_dev_is_medium_locked(blk);
2352 if (locked) {
2353 blk_dev_eject_request(blk, force);
2354 }
2355
2356 if (!locked || force) {
2357 blk_dev_change_media_cb(blk, false);
2358 }
John Snow3a3086b2016-05-18 17:53:40 -04002359
2360 if (locked && !force) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002361 error_setg(errp, "Device '%s' is locked and force was not specified, "
2362 "wait for tray to open and try again", device);
2363 return -EINPROGRESS;
John Snow3a3086b2016-05-18 17:53:40 -04002364 }
2365
2366 return 0;
2367}
2368
Kevin Wolfb33945c2016-09-20 13:38:43 +02002369void qmp_blockdev_open_tray(bool has_device, const char *device,
2370 bool has_id, const char *id,
2371 bool has_force, bool force,
John Snow3a3086b2016-05-18 17:53:40 -04002372 Error **errp)
2373{
Colin Lordbf18bee2016-06-06 14:15:22 -04002374 Error *local_err = NULL;
2375 int rc;
2376
John Snow3a3086b2016-05-18 17:53:40 -04002377 if (!has_force) {
2378 force = false;
2379 }
Kevin Wolfb33945c2016-09-20 13:38:43 +02002380 rc = do_open_tray(has_device ? device : NULL,
2381 has_id ? id : NULL,
2382 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002383 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2384 error_propagate(errp, local_err);
2385 return;
2386 }
2387 error_free(local_err);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002388}
2389
Kevin Wolfb33945c2016-09-20 13:38:43 +02002390void qmp_blockdev_close_tray(bool has_device, const char *device,
2391 bool has_id, const char *id,
2392 Error **errp)
Max Reitzabaaf592015-10-26 21:39:09 +01002393{
2394 BlockBackend *blk;
2395
Kevin Wolfb33945c2016-09-20 13:38:43 +02002396 device = has_device ? device : NULL;
2397 id = has_id ? id : NULL;
2398
2399 blk = qmp_get_blk(device, id, errp);
Max Reitzabaaf592015-10-26 21:39:09 +01002400 if (!blk) {
Max Reitzabaaf592015-10-26 21:39:09 +01002401 return;
2402 }
2403
2404 if (!blk_dev_has_removable_media(blk)) {
Kevin Wolfb33945c2016-09-20 13:38:43 +02002405 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitzabaaf592015-10-26 21:39:09 +01002406 return;
2407 }
2408
Max Reitz12c7ec82016-01-29 20:49:11 +01002409 if (!blk_dev_has_tray(blk)) {
2410 /* Ignore this command on tray-less devices */
2411 return;
2412 }
2413
Max Reitzabaaf592015-10-26 21:39:09 +01002414 if (!blk_dev_is_tray_open(blk)) {
2415 return;
2416 }
2417
2418 blk_dev_change_media_cb(blk, true);
2419}
2420
Kevin Wolf00949ba2016-09-20 13:38:45 +02002421void qmp_x_blockdev_remove_medium(bool has_device, const char *device,
2422 bool has_id, const char *id, Error **errp)
Max Reitz2814f672015-10-26 21:39:10 +01002423{
2424 BlockBackend *blk;
2425 BlockDriverState *bs;
2426 AioContext *aio_context;
Kevin Wolf00949ba2016-09-20 13:38:45 +02002427 bool has_attached_device;
Max Reitz2814f672015-10-26 21:39:10 +01002428
Kevin Wolf00949ba2016-09-20 13:38:45 +02002429 device = has_device ? device : NULL;
2430 id = has_id ? id : NULL;
2431
2432 blk = qmp_get_blk(device, id, errp);
Max Reitz2814f672015-10-26 21:39:10 +01002433 if (!blk) {
Max Reitz2814f672015-10-26 21:39:10 +01002434 return;
2435 }
2436
2437 /* For BBs without a device, we can exchange the BDS tree at will */
Kevin Wolf00949ba2016-09-20 13:38:45 +02002438 has_attached_device = blk_get_attached_dev(blk);
Max Reitz2814f672015-10-26 21:39:10 +01002439
Kevin Wolf00949ba2016-09-20 13:38:45 +02002440 if (has_attached_device && !blk_dev_has_removable_media(blk)) {
2441 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002442 return;
2443 }
2444
Kevin Wolf00949ba2016-09-20 13:38:45 +02002445 if (has_attached_device && blk_dev_has_tray(blk) &&
2446 !blk_dev_is_tray_open(blk))
2447 {
2448 error_setg(errp, "Tray of device '%s' is not open", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002449 return;
2450 }
2451
2452 bs = blk_bs(blk);
2453 if (!bs) {
2454 return;
2455 }
2456
2457 aio_context = bdrv_get_aio_context(bs);
2458 aio_context_acquire(aio_context);
2459
2460 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2461 goto out;
2462 }
2463
Max Reitz2814f672015-10-26 21:39:10 +01002464 blk_remove_bs(blk);
2465
Max Reitz12c7ec82016-01-29 20:49:11 +01002466 if (!blk_dev_has_tray(blk)) {
2467 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2468 * called at all); therefore, the medium needs to be ejected here.
2469 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2470 * value passed here (i.e. false). */
2471 blk_dev_change_media_cb(blk, false);
2472 }
2473
Max Reitz2814f672015-10-26 21:39:10 +01002474out:
2475 aio_context_release(aio_context);
2476}
2477
Kevin Wolf716df212016-09-20 13:38:44 +02002478static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
Max Reitzd1299882015-10-26 21:39:11 +01002479 BlockDriverState *bs, Error **errp)
2480{
Max Reitzd1299882015-10-26 21:39:11 +01002481 bool has_device;
2482
Max Reitzd1299882015-10-26 21:39:11 +01002483 /* For BBs without a device, we can exchange the BDS tree at will */
2484 has_device = blk_get_attached_dev(blk);
2485
2486 if (has_device && !blk_dev_has_removable_media(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002487 error_setg(errp, "Device is not removable");
Max Reitzd1299882015-10-26 21:39:11 +01002488 return;
2489 }
2490
Max Reitz12c7ec82016-01-29 20:49:11 +01002491 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002492 error_setg(errp, "Tray of the device is not open");
Max Reitzd1299882015-10-26 21:39:11 +01002493 return;
2494 }
2495
2496 if (blk_bs(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002497 error_setg(errp, "There already is a medium in the device");
Max Reitzd1299882015-10-26 21:39:11 +01002498 return;
2499 }
2500
2501 blk_insert_bs(blk, bs);
2502
Max Reitz12c7ec82016-01-29 20:49:11 +01002503 if (!blk_dev_has_tray(blk)) {
2504 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2505 * called at all); therefore, the medium needs to be pushed into the
2506 * slot here.
2507 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2508 * value passed here (i.e. true). */
2509 blk_dev_change_media_cb(blk, true);
2510 }
Max Reitzd1299882015-10-26 21:39:11 +01002511}
2512
Kevin Wolf716df212016-09-20 13:38:44 +02002513void qmp_x_blockdev_insert_medium(bool has_device, const char *device,
2514 bool has_id, const char *id,
2515 const char *node_name, Error **errp)
Max Reitzd1299882015-10-26 21:39:11 +01002516{
Kevin Wolf716df212016-09-20 13:38:44 +02002517 BlockBackend *blk;
Max Reitzd1299882015-10-26 21:39:11 +01002518 BlockDriverState *bs;
2519
Kevin Wolf716df212016-09-20 13:38:44 +02002520 blk = qmp_get_blk(has_device ? device : NULL,
2521 has_id ? id : NULL,
2522 errp);
2523 if (!blk) {
2524 return;
2525 }
2526
Max Reitzd1299882015-10-26 21:39:11 +01002527 bs = bdrv_find_node(node_name);
2528 if (!bs) {
2529 error_setg(errp, "Node '%s' not found", node_name);
2530 return;
2531 }
2532
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002533 if (bdrv_has_blk(bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02002534 error_setg(errp, "Node '%s' is already in use", node_name);
Max Reitzd1299882015-10-26 21:39:11 +01002535 return;
2536 }
2537
Kevin Wolf716df212016-09-20 13:38:44 +02002538 qmp_blockdev_insert_anon_medium(blk, bs, errp);
Max Reitzd1299882015-10-26 21:39:11 +01002539}
2540
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002541void qmp_blockdev_change_medium(bool has_device, const char *device,
2542 bool has_id, const char *id,
2543 const char *filename,
Max Reitz24fb4132015-11-06 16:27:06 +01002544 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002545 bool has_read_only,
2546 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002547 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002548{
2549 BlockBackend *blk;
2550 BlockDriverState *medium_bs = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002551 int bdrv_flags;
Colin Lord38a53d52016-06-08 13:56:28 -04002552 int rc;
Max Reitzde2c6c02015-10-26 21:39:13 +01002553 QDict *options = NULL;
2554 Error *err = NULL;
2555
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002556 blk = qmp_get_blk(has_device ? device : NULL,
2557 has_id ? id : NULL,
2558 errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002559 if (!blk) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002560 goto fail;
2561 }
2562
2563 if (blk_bs(blk)) {
2564 blk_update_root_state(blk);
2565 }
2566
2567 bdrv_flags = blk_get_open_flags_from_root_state(blk);
Alyssa Milburn156abc22016-02-06 13:36:18 +00002568 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2569 BDRV_O_PROTOCOL);
Max Reitzde2c6c02015-10-26 21:39:13 +01002570
Max Reitz39ff43d2015-11-11 04:49:44 +01002571 if (!has_read_only) {
2572 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2573 }
2574
2575 switch (read_only) {
2576 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2577 break;
2578
2579 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2580 bdrv_flags &= ~BDRV_O_RDWR;
2581 break;
2582
2583 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2584 bdrv_flags |= BDRV_O_RDWR;
2585 break;
2586
2587 default:
2588 abort();
2589 }
2590
Max Reitz24fb4132015-11-06 16:27:06 +01002591 if (has_format) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002592 options = qdict_new();
2593 qdict_put(options, "driver", qstring_from_str(format));
2594 }
2595
Max Reitz5b363932016-05-17 16:41:31 +02002596 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2597 if (!medium_bs) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002598 goto fail;
2599 }
2600
Max Reitzde2c6c02015-10-26 21:39:13 +01002601 bdrv_add_key(medium_bs, NULL, &err);
2602 if (err) {
2603 error_propagate(errp, err);
2604 goto fail;
2605 }
2606
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002607 rc = do_open_tray(has_device ? device : NULL,
2608 has_id ? id : NULL,
2609 false, &err);
Colin Lord38a53d52016-06-08 13:56:28 -04002610 if (rc && rc != -ENOSYS) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002611 error_propagate(errp, err);
2612 goto fail;
2613 }
Colin Lord38a53d52016-06-08 13:56:28 -04002614 error_free(err);
2615 err = NULL;
Max Reitzde2c6c02015-10-26 21:39:13 +01002616
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002617 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002618 if (err) {
2619 error_propagate(errp, err);
2620 goto fail;
2621 }
2622
Kevin Wolf716df212016-09-20 13:38:44 +02002623 qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002624 if (err) {
2625 error_propagate(errp, err);
2626 goto fail;
2627 }
2628
Kevin Wolfa5614992016-03-21 10:49:51 +01002629 blk_apply_root_state(blk, medium_bs);
2630
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002631 qmp_blockdev_close_tray(has_device, device, has_id, id, errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002632
2633fail:
2634 /* If the medium has been inserted, the device has its own reference, so
2635 * ours must be relinquished; and if it has not been inserted successfully,
2636 * the reference must be relinquished anyway */
2637 bdrv_unref(medium_bs);
2638}
2639
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002640/* throttling disk I/O limits */
Eric Blake4dc93972016-07-13 21:50:21 -06002641void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002642{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002643 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002644 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002645 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002646 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002647
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002648 blk = qmp_get_blk(arg->has_device ? arg->device : NULL,
2649 arg->has_id ? arg->id : NULL,
2650 errp);
Fam Zhenga0e85442015-03-02 19:36:48 +08002651 if (!blk) {
Luiz Capitulino80047da2011-12-14 16:49:14 -02002652 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002653 }
Max Reitz5433c242015-10-19 17:53:29 +02002654
2655 aio_context = blk_get_aio_context(blk);
2656 aio_context_acquire(aio_context);
2657
Fam Zhenga0e85442015-03-02 19:36:48 +08002658 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002659 if (!bs) {
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002660 error_setg(errp, "Device has no medium");
Max Reitz5433c242015-10-19 17:53:29 +02002661 goto out;
2662 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002663
Alberto Garcia1588ab52016-02-18 12:27:00 +02002664 throttle_config_init(&cfg);
Eric Blake4dc93972016-07-13 21:50:21 -06002665 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2666 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2667 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002668
Eric Blake4dc93972016-07-13 21:50:21 -06002669 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2670 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2671 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
Benoît Canetcc0681c2013-09-02 14:14:39 +02002672
Eric Blake4dc93972016-07-13 21:50:21 -06002673 if (arg->has_bps_max) {
2674 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002675 }
Eric Blake4dc93972016-07-13 21:50:21 -06002676 if (arg->has_bps_rd_max) {
2677 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002678 }
Eric Blake4dc93972016-07-13 21:50:21 -06002679 if (arg->has_bps_wr_max) {
2680 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002681 }
Eric Blake4dc93972016-07-13 21:50:21 -06002682 if (arg->has_iops_max) {
2683 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002684 }
Eric Blake4dc93972016-07-13 21:50:21 -06002685 if (arg->has_iops_rd_max) {
2686 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002687 }
Eric Blake4dc93972016-07-13 21:50:21 -06002688 if (arg->has_iops_wr_max) {
2689 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002690 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002691
Eric Blake4dc93972016-07-13 21:50:21 -06002692 if (arg->has_bps_max_length) {
2693 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002694 }
Eric Blake4dc93972016-07-13 21:50:21 -06002695 if (arg->has_bps_rd_max_length) {
2696 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002697 }
Eric Blake4dc93972016-07-13 21:50:21 -06002698 if (arg->has_bps_wr_max_length) {
2699 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002700 }
Eric Blake4dc93972016-07-13 21:50:21 -06002701 if (arg->has_iops_max_length) {
2702 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002703 }
Eric Blake4dc93972016-07-13 21:50:21 -06002704 if (arg->has_iops_rd_max_length) {
2705 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002706 }
Eric Blake4dc93972016-07-13 21:50:21 -06002707 if (arg->has_iops_wr_max_length) {
2708 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002709 }
2710
Eric Blake4dc93972016-07-13 21:50:21 -06002711 if (arg->has_iops_size) {
2712 cfg.op_size = arg->iops_size;
Benoît Canet2024c1d2013-09-02 14:14:41 +02002713 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002714
Alberto Garciad5851082016-02-18 12:26:59 +02002715 if (!throttle_is_valid(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002716 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002717 }
2718
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002719 if (throttle_enabled(&cfg)) {
2720 /* Enable I/O limits if they're not enabled yet, otherwise
2721 * just update the throttling group. */
Kevin Wolf97148072016-03-21 13:53:52 +01002722 if (!blk_get_public(blk)->throttle_state) {
Eric Blake4dc93972016-07-13 21:50:21 -06002723 blk_io_limits_enable(blk,
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002724 arg->has_group ? arg->group :
2725 arg->has_device ? arg->device :
2726 arg->id);
Eric Blake4dc93972016-07-13 21:50:21 -06002727 } else if (arg->has_group) {
2728 blk_io_limits_update_group(blk, arg->group);
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002729 }
2730 /* Set the new throttling configuration */
Kevin Wolf97148072016-03-21 13:53:52 +01002731 blk_set_io_limits(blk, &cfg);
2732 } else if (blk_get_public(blk)->throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002733 /* If all throttling settings are set to 0, disable I/O limits */
Kevin Wolf97148072016-03-21 13:53:52 +01002734 blk_io_limits_disable(blk);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002735 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002736
Max Reitz5433c242015-10-19 17:53:29 +02002737out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002738 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002739}
2740
John Snow341ebc22015-04-17 19:49:52 -04002741void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2742 bool has_granularity, uint32_t granularity,
2743 Error **errp)
2744{
2745 AioContext *aio_context;
2746 BlockDriverState *bs;
2747
2748 if (!name || name[0] == '\0') {
2749 error_setg(errp, "Bitmap name cannot be empty");
2750 return;
2751 }
2752
2753 bs = bdrv_lookup_bs(node, node, errp);
2754 if (!bs) {
2755 return;
2756 }
2757
2758 aio_context = bdrv_get_aio_context(bs);
2759 aio_context_acquire(aio_context);
2760
2761 if (has_granularity) {
2762 if (granularity < 512 || !is_power_of_2(granularity)) {
2763 error_setg(errp, "Granularity must be power of 2 "
2764 "and at least 512");
2765 goto out;
2766 }
2767 } else {
2768 /* Default to cluster size, if available: */
2769 granularity = bdrv_get_default_bitmap_granularity(bs);
2770 }
2771
2772 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2773
2774 out:
2775 aio_context_release(aio_context);
2776}
2777
2778void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2779 Error **errp)
2780{
2781 AioContext *aio_context;
2782 BlockDriverState *bs;
2783 BdrvDirtyBitmap *bitmap;
2784
2785 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2786 if (!bitmap || !bs) {
2787 return;
2788 }
2789
John Snow9bd2b082015-04-17 19:49:57 -04002790 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2791 error_setg(errp,
2792 "Bitmap '%s' is currently frozen and cannot be removed",
2793 name);
2794 goto out;
2795 }
John Snow20dca812015-04-17 19:50:02 -04002796 bdrv_dirty_bitmap_make_anon(bitmap);
John Snow341ebc22015-04-17 19:49:52 -04002797 bdrv_release_dirty_bitmap(bs, bitmap);
2798
John Snow9bd2b082015-04-17 19:49:57 -04002799 out:
John Snow341ebc22015-04-17 19:49:52 -04002800 aio_context_release(aio_context);
2801}
2802
John Snowe74e6b72015-04-17 19:49:59 -04002803/**
2804 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2805 * immediately after a full backup operation.
2806 */
2807void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2808 Error **errp)
2809{
2810 AioContext *aio_context;
2811 BdrvDirtyBitmap *bitmap;
2812 BlockDriverState *bs;
2813
2814 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2815 if (!bitmap || !bs) {
2816 return;
2817 }
2818
2819 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2820 error_setg(errp,
2821 "Bitmap '%s' is currently frozen and cannot be modified",
2822 name);
2823 goto out;
2824 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2825 error_setg(errp,
2826 "Bitmap '%s' is currently disabled and cannot be cleared",
2827 name);
2828 goto out;
2829 }
2830
Fam Zhengdf9a6812015-11-09 18:16:54 +08002831 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04002832
2833 out:
2834 aio_context_release(aio_context);
2835}
2836
Markus Armbruster072ebe62015-03-05 17:00:56 +01002837void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06002838{
2839 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002840 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06002841 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002842 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08002843 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06002844
Kevin Wolf2073d412016-02-23 17:50:37 +01002845 bs = bdrv_find_node(id);
2846 if (bs) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02002847 qmp_x_blockdev_del(id, &local_err);
Kevin Wolf2073d412016-02-23 17:50:37 +01002848 if (local_err) {
2849 error_report_err(local_err);
2850 }
2851 return;
2852 }
2853
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002854 blk = blk_by_name(id);
2855 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02002856 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01002857 return;
Ryan Harper9063f812010-11-12 11:07:13 -06002858 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002859
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02002860 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02002861 error_report("Deleting device added with blockdev-add"
2862 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01002863 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02002864 }
2865
Max Reitz5433c242015-10-19 17:53:29 +02002866 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002867 aio_context_acquire(aio_context);
2868
Max Reitz5433c242015-10-19 17:53:29 +02002869 bs = blk_bs(blk);
2870 if (bs) {
2871 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2872 error_report_err(local_err);
2873 aio_context_release(aio_context);
2874 return;
2875 }
Ryan Harper9063f812010-11-12 11:07:13 -06002876
Max Reitz938abd42016-01-29 16:36:09 +01002877 blk_remove_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002878 }
Ryan Harper9063f812010-11-12 11:07:13 -06002879
Max Reitz7c735872016-03-16 19:54:39 +01002880 /* Make the BlockBackend and the attached BlockDriverState anonymous */
Max Reitzefaa7c42016-03-16 19:54:38 +01002881 monitor_remove_blk(blk);
2882
Max Reitz7c735872016-03-16 19:54:39 +01002883 /* If this BlockBackend has a device attached to it, its refcount will be
2884 * decremented when the device is removed; otherwise we have to do so here.
Ryan Harperd22b2f42011-03-29 20:51:47 -05002885 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02002886 if (blk_get_attached_dev(blk)) {
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02002887 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02002888 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2889 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05002890 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02002891 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06002892 }
2893
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002894 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06002895}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002896
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002897void qmp_block_resize(bool has_device, const char *device,
2898 bool has_node_name, const char *node_name,
2899 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002900{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002901 Error *local_err = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002902 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002903 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02002904 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002905
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002906 bs = bdrv_lookup_bs(has_device ? device : NULL,
2907 has_node_name ? node_name : NULL,
2908 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002909 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002910 error_propagate(errp, local_err);
2911 return;
2912 }
2913
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002914 aio_context = bdrv_get_aio_context(bs);
2915 aio_context_acquire(aio_context);
2916
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002917 if (!bdrv_is_first_non_filter(bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002918 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002919 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002920 }
2921
2922 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002923 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002924 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002925 }
2926
Jeff Cody9c75e162014-06-25 16:55:30 -04002927 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002928 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002929 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04002930 }
2931
Peter Lieven92b7a082013-03-11 11:04:24 +01002932 /* complete all in-flight operations before resizing the device */
2933 bdrv_drain_all();
2934
Kevin Wolf87329012013-05-02 15:32:55 +02002935 ret = bdrv_truncate(bs, size);
2936 switch (ret) {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002937 case 0:
2938 break;
2939 case -ENOMEDIUM:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002940 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002941 break;
2942 case -ENOTSUP:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002943 error_setg(errp, QERR_UNSUPPORTED);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002944 break;
2945 case -EACCES:
Alberto Garcia81e5f782015-04-08 12:29:19 +03002946 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002947 break;
2948 case -EBUSY:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002949 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002950 break;
2951 default:
Kevin Wolf87329012013-05-02 15:32:55 +02002952 error_setg_errno(errp, -ret, "Could not resize");
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002953 break;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002954 }
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002955
2956out:
2957 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002958}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002959
Jeff Cody9abf2db2012-09-27 13:29:14 -04002960static void block_job_cb(void *opaque, int ret)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002961{
Stefan Hajnoczi723c5d92014-10-21 12:03:53 +01002962 /* Note that this function may be executed from another AioContext besides
2963 * the QEMU main loop. If you need to access anything that assumes the
2964 * QEMU global mutex, use a BH or introduce a mutex.
2965 */
2966
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002967 BlockDriverState *bs = opaque;
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002968 const char *msg = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002969
Jeff Cody9abf2db2012-09-27 13:29:14 -04002970 trace_block_job_cb(bs, bs->job, ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002971
2972 assert(bs->job);
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002973
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002974 if (ret < 0) {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002975 msg = strerror(-ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002976 }
2977
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002978 if (block_job_is_cancelled(bs->job)) {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002979 block_job_event_cancelled(bs->job);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002980 } else {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002981 block_job_event_completed(bs->job, msg);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002982 }
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002983}
2984
Alberto Garcia23233222016-07-05 17:28:59 +03002985void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002986 bool has_base, const char *base,
2987 bool has_backing_file, const char *backing_file,
2988 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002989 bool has_on_error, BlockdevOnError on_error,
2990 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002991{
2992 BlockDriverState *bs;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002993 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002994 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002995 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002996 const char *base_name = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002997
Paolo Bonzini1d809092012-09-28 17:22:59 +02002998 if (!has_on_error) {
2999 on_error = BLOCKDEV_ON_ERROR_REPORT;
3000 }
3001
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003002 bs = qmp_get_root_bs(device, errp);
3003 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003004 return;
3005 }
3006
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003007 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003008 aio_context_acquire(aio_context);
3009
Fam Zheng628ff682014-05-23 21:29:44 +08003010 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003011 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003012 }
3013
Jeff Cody13d8cc52014-06-25 15:40:11 -04003014 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003015 base_bs = bdrv_find_backing_image(bs, base);
3016 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003017 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003018 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003019 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003020 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04003021 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003022 }
3023
Jeff Cody13d8cc52014-06-25 15:40:11 -04003024 /* if we are streaming the entire chain, the result will have no backing
3025 * file, and specifying one is therefore an error */
3026 if (base_bs == NULL && has_backing_file) {
3027 error_setg(errp, "backing file specified, but streaming the "
3028 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003029 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003030 }
3031
3032 /* backing_file string overrides base bs filename */
3033 base_name = has_backing_file ? backing_file : base_name;
3034
Alberto Garcia23233222016-07-05 17:28:59 +03003035 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
3036 has_speed ? speed : 0, on_error, block_job_cb, bs, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003037 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003038 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003039 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003040 }
3041
3042 trace_qmp_block_stream(bs, bs->job);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003043
3044out:
3045 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003046}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003047
Alberto Garciafd62c602016-07-05 17:29:00 +03003048void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003049 bool has_base, const char *base,
3050 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003051 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04003052 bool has_speed, int64_t speed,
3053 Error **errp)
3054{
3055 BlockDriverState *bs;
3056 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003057 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04003058 Error *local_err = NULL;
3059 /* This will be part of the QMP command, if/when the
3060 * BlockdevOnError change for blkmirror makes it in
3061 */
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02003062 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
Jeff Codyed61fc12012-09-27 13:29:16 -04003063
Max Reitz54504662014-04-10 19:36:25 +02003064 if (!has_speed) {
3065 speed = 0;
3066 }
3067
Jeff Cody7676e2c2014-06-30 15:14:15 +02003068 /* Important Note:
3069 * libvirt relies on the DeviceNotFound error class in order to probe for
3070 * live commit feature versions; for this to work, we must make sure to
3071 * perform the device lookup before any generic errors that may occur in a
3072 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02003073 bs = qmp_get_root_bs(device, &local_err);
3074 if (!bs) {
3075 bs = bdrv_lookup_bs(device, device, NULL);
3076 if (!bs) {
3077 error_free(local_err);
3078 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3079 "Device '%s' not found", device);
3080 } else {
3081 error_propagate(errp, local_err);
3082 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003083 return;
3084 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003085
Kevin Wolf1d13b162016-06-23 14:20:24 +02003086 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003087 aio_context_acquire(aio_context);
3088
Fam Zhengbb000212014-09-11 13:14:00 +08003089 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003090 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003091 }
3092
Jeff Codyed61fc12012-09-27 13:29:16 -04003093 /* default top_bs is the active layer */
3094 top_bs = bs;
3095
Jeff Cody7676e2c2014-06-30 15:14:15 +02003096 if (has_top && top) {
Jeff Codyed61fc12012-09-27 13:29:16 -04003097 if (strcmp(bs->filename, top) != 0) {
3098 top_bs = bdrv_find_backing_image(bs, top);
3099 }
3100 }
3101
3102 if (top_bs == NULL) {
3103 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003104 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003105 }
3106
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003107 assert(bdrv_get_aio_context(top_bs) == aio_context);
3108
Jeff Codyd5208c42012-10-16 15:49:10 -04003109 if (has_base && base) {
3110 base_bs = bdrv_find_backing_image(top_bs, base);
3111 } else {
3112 base_bs = bdrv_find_base(top_bs);
3113 }
3114
3115 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003116 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003117 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04003118 }
3119
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003120 assert(bdrv_get_aio_context(base_bs) == aio_context);
3121
Fam Zhengbb000212014-09-11 13:14:00 +08003122 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3123 goto out;
3124 }
3125
Jeff Cody7676e2c2014-06-30 15:14:15 +02003126 /* Do not allow attempts to commit an image into itself */
3127 if (top_bs == base_bs) {
3128 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003129 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02003130 }
3131
Fam Zheng20a63d22013-12-16 14:45:31 +08003132 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04003133 if (has_backing_file) {
3134 error_setg(errp, "'backing-file' specified,"
3135 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003136 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003137 }
Alberto Garciafd62c602016-07-05 17:29:00 +03003138 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, speed,
Wen Congyangb49f7ea2016-07-27 15:01:47 +08003139 on_error, block_job_cb, bs, &local_err, false);
Fam Zheng20a63d22013-12-16 14:45:31 +08003140 } else {
Alberto Garciafd62c602016-07-05 17:29:00 +03003141 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed,
3142 on_error, block_job_cb, bs,
Jeff Cody54e26902014-06-25 15:40:10 -04003143 has_backing_file ? backing_file : NULL, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003144 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003145 if (local_err != NULL) {
3146 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003147 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003148 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003149
3150out:
3151 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003152}
3153
Pavel Butsykin81206a82016-07-22 11:17:50 +03003154static void do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003155{
3156 BlockDriverState *bs;
3157 BlockDriverState *target_bs;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003158 BlockDriverState *source = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003159 BdrvDirtyBitmap *bmap = NULL;
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003160 AioContext *aio_context;
Max Reitze6641712015-08-26 19:47:48 +02003161 QDict *options = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003162 Error *local_err = NULL;
3163 int flags;
3164 int64_t size;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003165
Pavel Butsykin81206a82016-07-22 11:17:50 +03003166 if (!backup->has_speed) {
3167 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003168 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003169 if (!backup->has_on_source_error) {
3170 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003171 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003172 if (!backup->has_on_target_error) {
3173 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003174 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003175 if (!backup->has_mode) {
3176 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3177 }
3178 if (!backup->has_job_id) {
3179 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003180 }
Pavel Butsykin13b94142016-07-22 11:17:52 +03003181 if (!backup->has_compress) {
3182 backup->compress = false;
3183 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003184
Pavel Butsykin81206a82016-07-22 11:17:50 +03003185 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003186 if (!bs) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003187 return;
3188 }
3189
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003190 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003191 aio_context_acquire(aio_context);
3192
Pavel Butsykin81206a82016-07-22 11:17:50 +03003193 if (!backup->has_format) {
3194 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
3195 NULL : (char*) bs->drv->format_name;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003196 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003197
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003198 /* Early check to avoid creating target */
Fam Zheng3718d8a2014-05-23 21:29:43 +08003199 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003200 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003201 }
3202
Kevin Wolf61de4c62016-03-18 17:46:45 +01003203 flags = bs->open_flags | BDRV_O_RDWR;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003204
Ian Mainfc5d3f82013-07-26 11:39:04 -07003205 /* See if we have a backing HD we can use to create our new image
3206 * on top of. */
Pavel Butsykin81206a82016-07-22 11:17:50 +03003207 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003208 source = backing_bs(bs);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003209 if (!source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003210 backup->sync = MIRROR_SYNC_MODE_FULL;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003211 }
3212 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003213 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
Ian Mainfc5d3f82013-07-26 11:39:04 -07003214 source = bs;
3215 }
3216
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003217 size = bdrv_getlength(bs);
3218 if (size < 0) {
3219 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003220 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003221 }
3222
Pavel Butsykin81206a82016-07-22 11:17:50 +03003223 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
3224 assert(backup->format);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003225 if (source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003226 bdrv_img_create(backup->target, backup->format, source->filename,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003227 source->drv->format_name, NULL,
3228 size, flags, &local_err, false);
3229 } else {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003230 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003231 size, flags, &local_err, false);
3232 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003233 }
3234
Markus Armbruster84d18f02014-01-30 15:07:28 +01003235 if (local_err) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003236 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003237 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003238 }
3239
Pavel Butsykin81206a82016-07-22 11:17:50 +03003240 if (backup->format) {
Max Reitze6641712015-08-26 19:47:48 +02003241 options = qdict_new();
Pavel Butsykin81206a82016-07-22 11:17:50 +03003242 qdict_put(options, "driver", qstring_from_str(backup->format));
Max Reitze6641712015-08-26 19:47:48 +02003243 }
3244
Pavel Butsykin81206a82016-07-22 11:17:50 +03003245 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003246 if (!target_bs) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003247 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003248 }
3249
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003250 bdrv_set_aio_context(target_bs, aio_context);
3251
Pavel Butsykin81206a82016-07-22 11:17:50 +03003252 if (backup->has_bitmap) {
3253 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
John Snowd58d8452015-04-17 19:49:58 -04003254 if (!bmap) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003255 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
Max Reitz0702d3d2015-11-09 23:39:10 +01003256 bdrv_unref(target_bs);
John Snowd58d8452015-04-17 19:49:58 -04003257 goto out;
3258 }
3259 }
3260
Pavel Butsykin81206a82016-07-22 11:17:50 +03003261 backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync,
Pavel Butsykin13b94142016-07-22 11:17:52 +03003262 bmap, backup->compress, backup->on_source_error,
3263 backup->on_target_error, block_job_cb, bs, txn, &local_err);
Kevin Wolf5c438bc2016-04-14 13:09:53 +02003264 bdrv_unref(target_bs);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003265 if (local_err != NULL) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003266 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003267 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003268 }
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003269
3270out:
3271 aio_context_release(aio_context);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003272}
3273
Pavel Butsykin81206a82016-07-22 11:17:50 +03003274void qmp_drive_backup(DriveBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003275{
Pavel Butsykin81206a82016-07-22 11:17:50 +03003276 return do_drive_backup(arg, NULL, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003277}
3278
Benoît Canetc13163f2014-01-23 21:31:34 +01003279BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3280{
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003281 return bdrv_named_nodes_list(errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003282}
3283
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003284void do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003285{
3286 BlockDriverState *bs;
3287 BlockDriverState *target_bs;
3288 Error *local_err = NULL;
3289 AioContext *aio_context;
3290
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003291 if (!backup->has_speed) {
3292 backup->speed = 0;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003293 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003294 if (!backup->has_on_source_error) {
3295 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003296 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003297 if (!backup->has_on_target_error) {
3298 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3299 }
3300 if (!backup->has_job_id) {
3301 backup->job_id = NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003302 }
Pavel Butsykin3b7b1232016-07-22 11:17:53 +03003303 if (!backup->has_compress) {
3304 backup->compress = false;
3305 }
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003306
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003307 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003308 if (!bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003309 return;
3310 }
3311
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003312 aio_context = bdrv_get_aio_context(bs);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003313 aio_context_acquire(aio_context);
3314
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003315 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
Fam Zheng0d978912016-05-23 10:19:35 +08003316 if (!target_bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003317 goto out;
3318 }
Max Reitz5433c242015-10-19 17:53:29 +02003319
Fam Zhengefd75562016-05-23 10:19:36 +08003320 if (bdrv_get_aio_context(target_bs) != aio_context) {
3321 if (!bdrv_has_blk(target_bs)) {
3322 /* The target BDS is not attached, we can safely move it to another
3323 * AioContext. */
3324 bdrv_set_aio_context(target_bs, aio_context);
3325 } else {
3326 error_setg(errp, "Target is attached to a different thread from "
3327 "source.");
3328 goto out;
3329 }
3330 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003331 backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync,
Pavel Butsykin3b7b1232016-07-22 11:17:53 +03003332 NULL, backup->compress, backup->on_source_error,
3333 backup->on_target_error, block_job_cb, bs, txn, &local_err);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003334 if (local_err != NULL) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003335 error_propagate(errp, local_err);
3336 }
3337out:
3338 aio_context_release(aio_context);
3339}
3340
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003341void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003342{
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003343 do_blockdev_backup(arg, NULL, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003344}
3345
Fam Zheng4193cdd2015-12-24 12:45:03 +08003346/* Parameter check and block job starting for drive mirroring.
3347 * Caller should hold @device and @target's aio context (must be the same).
3348 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003349static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003350 BlockDriverState *target,
3351 bool has_replaces, const char *replaces,
3352 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003353 BlockMirrorBackingMode backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003354 bool has_speed, int64_t speed,
3355 bool has_granularity, uint32_t granularity,
3356 bool has_buf_size, int64_t buf_size,
3357 bool has_on_source_error,
3358 BlockdevOnError on_source_error,
3359 bool has_on_target_error,
3360 BlockdevOnError on_target_error,
3361 bool has_unmap, bool unmap,
3362 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003363{
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003364
3365 if (!has_speed) {
3366 speed = 0;
3367 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003368 if (!has_on_source_error) {
3369 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3370 }
3371 if (!has_on_target_error) {
3372 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3373 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003374 if (!has_granularity) {
3375 granularity = 0;
3376 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003377 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003378 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003379 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003380 if (!has_unmap) {
3381 unmap = true;
3382 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003383
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003384 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003385 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3386 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003387 return;
3388 }
3389 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003390 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3391 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003392 return;
3393 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003394
Fam Zheng4193cdd2015-12-24 12:45:03 +08003395 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3396 return;
3397 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003398 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3399 return;
3400 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003401
3402 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3403 sync = MIRROR_SYNC_MODE_FULL;
3404 }
3405
3406 /* pass the node name to replace to mirror start since it's loose coupling
3407 * and will allow to check whether the node still exist at mirror completion
3408 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003409 mirror_start(job_id, bs, target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003410 has_replaces ? replaces : NULL,
Max Reitz274fcce2016-06-10 20:57:47 +02003411 speed, granularity, buf_size, sync, backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003412 on_source_error, on_target_error, unmap,
3413 block_job_cb, bs, errp);
3414}
3415
Eric Blakefaecd402016-07-14 16:37:58 -06003416void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003417{
3418 BlockDriverState *bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003419 BlockDriverState *source, *target_bs;
3420 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003421 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003422 Error *local_err = NULL;
3423 QDict *options = NULL;
3424 int flags;
3425 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003426 const char *format = arg->format;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003427
Kevin Wolf0524e932016-06-23 14:20:24 +02003428 bs = qmp_get_root_bs(arg->device, errp);
3429 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003430 return;
3431 }
3432
Kevin Wolf0524e932016-06-23 14:20:24 +02003433 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003434 aio_context_acquire(aio_context);
3435
Eric Blakefaecd402016-07-14 16:37:58 -06003436 if (!arg->has_mode) {
3437 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003438 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003439
Eric Blakefaecd402016-07-14 16:37:58 -06003440 if (!arg->has_format) {
3441 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3442 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003443 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003444
Kevin Wolf61de4c62016-03-18 17:46:45 +01003445 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003446 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003447 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3448 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003449 }
Eric Blakefaecd402016-07-14 16:37:58 -06003450 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003451 source = bs;
3452 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003453
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003454 size = bdrv_getlength(bs);
3455 if (size < 0) {
3456 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003457 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003458 }
3459
Eric Blakefaecd402016-07-14 16:37:58 -06003460 if (arg->has_replaces) {
Benoît Canet09158f02014-06-27 18:25:25 +02003461 BlockDriverState *to_replace_bs;
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003462 AioContext *replace_aio_context;
3463 int64_t replace_size;
Benoît Canet09158f02014-06-27 18:25:25 +02003464
Eric Blakefaecd402016-07-14 16:37:58 -06003465 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003466 error_setg(errp, "a node-name must be provided when replacing a"
3467 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003468 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003469 }
3470
Eric Blakefaecd402016-07-14 16:37:58 -06003471 to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err);
Benoît Canet09158f02014-06-27 18:25:25 +02003472
3473 if (!to_replace_bs) {
3474 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003475 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003476 }
3477
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003478 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3479 aio_context_acquire(replace_aio_context);
3480 replace_size = bdrv_getlength(to_replace_bs);
3481 aio_context_release(replace_aio_context);
3482
3483 if (size != replace_size) {
Benoît Canet09158f02014-06-27 18:25:25 +02003484 error_setg(errp, "cannot replace image with a mirror image of "
3485 "different size");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003486 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003487 }
3488 }
3489
Eric Blakefaecd402016-07-14 16:37:58 -06003490 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003491 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3492 } else {
3493 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3494 }
3495
Eric Blakefaecd402016-07-14 16:37:58 -06003496 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3497 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003498 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003499 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003500 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003501 bdrv_img_create(arg->target, format,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003502 NULL, NULL, NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003503 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003504 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003505 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003506 break;
3507 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3508 /* create new image with backing file */
Eric Blakefaecd402016-07-14 16:37:58 -06003509 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003510 source->filename,
3511 source->drv->format_name,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003512 NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003513 break;
3514 default:
3515 abort();
3516 }
3517 }
3518
Markus Armbruster84d18f02014-01-30 15:07:28 +01003519 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003520 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003521 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003522 }
3523
Max Reitze6641712015-08-26 19:47:48 +02003524 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003525 if (arg->has_node_name) {
3526 qdict_put(options, "node-name", qstring_from_str(arg->node_name));
Benoît Canet4c828dc2014-06-16 12:00:55 +02003527 }
Max Reitze6641712015-08-26 19:47:48 +02003528 if (format) {
3529 qdict_put(options, "driver", qstring_from_str(format));
3530 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003531
Paolo Bonzinib812f672013-01-21 17:09:43 +01003532 /* Mirroring takes care of copy-on-write using the source's backing
3533 * file.
3534 */
Eric Blakefaecd402016-07-14 16:37:58 -06003535 target_bs = bdrv_open(arg->target, NULL, options,
3536 flags | BDRV_O_NO_BACKING, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003537 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003538 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003539 }
3540
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003541 bdrv_set_aio_context(target_bs, aio_context);
3542
Eric Blakefaecd402016-07-14 16:37:58 -06003543 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3544 arg->has_replaces, arg->replaces, arg->sync,
3545 backing_mode, arg->has_speed, arg->speed,
3546 arg->has_granularity, arg->granularity,
3547 arg->has_buf_size, arg->buf_size,
3548 arg->has_on_source_error, arg->on_source_error,
3549 arg->has_on_target_error, arg->on_target_error,
3550 arg->has_unmap, arg->unmap,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003551 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02003552 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003553 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003554out:
3555 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003556}
3557
Alberto Garcia71aa9862016-07-05 17:28:57 +03003558void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3559 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08003560 bool has_replaces, const char *replaces,
3561 MirrorSyncMode sync,
3562 bool has_speed, int64_t speed,
3563 bool has_granularity, uint32_t granularity,
3564 bool has_buf_size, int64_t buf_size,
3565 bool has_on_source_error,
3566 BlockdevOnError on_source_error,
3567 bool has_on_target_error,
3568 BlockdevOnError on_target_error,
3569 Error **errp)
3570{
3571 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08003572 BlockDriverState *target_bs;
3573 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003574 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08003575 Error *local_err = NULL;
3576
Kevin Wolf07eec652016-06-23 14:20:24 +02003577 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08003578 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08003579 return;
3580 }
3581
3582 target_bs = bdrv_lookup_bs(target, target, errp);
3583 if (!target_bs) {
3584 return;
3585 }
3586
3587 aio_context = bdrv_get_aio_context(bs);
3588 aio_context_acquire(aio_context);
3589
Fam Zhengdf925622015-12-24 12:45:05 +08003590 bdrv_set_aio_context(target_bs, aio_context);
3591
Alberto Garcia71aa9862016-07-05 17:28:57 +03003592 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02003593 has_replaces, replaces, sync, backing_mode,
Fam Zhengdf925622015-12-24 12:45:05 +08003594 has_speed, speed,
3595 has_granularity, granularity,
3596 has_buf_size, buf_size,
3597 has_on_source_error, on_source_error,
3598 has_on_target_error, on_target_error,
3599 true, true,
3600 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003601 error_propagate(errp, local_err);
Fam Zhengdf925622015-12-24 12:45:05 +08003602
3603 aio_context_release(aio_context);
3604}
3605
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003606/* Get a block job using its ID and acquire its AioContext */
3607static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003608 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003609{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003610 BlockJob *job;
3611
3612 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003613
Max Reitz5433c242015-10-19 17:53:29 +02003614 *aio_context = NULL;
3615
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003616 job = block_job_get(id);
3617
3618 if (!job) {
3619 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3620 "Block job '%s' not found", id);
3621 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003622 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003623
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003624 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003625 aio_context_acquire(*aio_context);
3626
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003627 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003628}
3629
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003630void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003631{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003632 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003633 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003634
3635 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003636 return;
3637 }
3638
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003639 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003640 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003641}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003642
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003643void qmp_block_job_cancel(const char *device,
3644 bool has_force, bool force, Error **errp)
3645{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003646 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003647 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003648
3649 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003650 return;
3651 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003652
3653 if (!has_force) {
3654 force = false;
3655 }
3656
Fam Zheng751ebd72015-04-03 22:05:18 +08003657 if (job->user_paused && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003658 error_setg(errp, "The block job for device '%s' is currently paused",
3659 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003660 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003661 }
3662
3663 trace_qmp_block_job_cancel(job);
3664 block_job_cancel(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003665out:
3666 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003667}
3668
3669void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003670{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003671 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003672 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003673
Fam Zheng751ebd72015-04-03 22:05:18 +08003674 if (!job || job->user_paused) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003675 return;
3676 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003677
Fam Zheng751ebd72015-04-03 22:05:18 +08003678 job->user_paused = true;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003679 trace_qmp_block_job_pause(job);
3680 block_job_pause(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003681 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003682}
3683
3684void qmp_block_job_resume(const char *device, Error **errp)
3685{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003686 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003687 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003688
Fam Zheng751ebd72015-04-03 22:05:18 +08003689 if (!job || !job->user_paused) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02003690 return;
3691 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003692
Fam Zheng751ebd72015-04-03 22:05:18 +08003693 job->user_paused = false;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003694 trace_qmp_block_job_resume(job);
Stefan Hajnoczi17bd51f2016-06-16 17:56:22 +01003695 block_job_iostatus_reset(job);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003696 block_job_resume(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003697 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003698}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003699
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003700void qmp_block_job_complete(const char *device, Error **errp)
3701{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003702 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003703 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003704
3705 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003706 return;
3707 }
3708
3709 trace_qmp_block_job_complete(job);
3710 block_job_complete(job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003711 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003712}
3713
Jeff Codyfa40e652014-07-01 09:52:16 +02003714void qmp_change_backing_file(const char *device,
3715 const char *image_node_name,
3716 const char *backing_file,
3717 Error **errp)
3718{
3719 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003720 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02003721 BlockDriverState *image_bs = NULL;
3722 Error *local_err = NULL;
3723 bool ro;
3724 int open_flags;
3725 int ret;
3726
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003727 bs = qmp_get_root_bs(device, errp);
3728 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02003729 return;
3730 }
3731
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003732 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003733 aio_context_acquire(aio_context);
3734
Jeff Codyfa40e652014-07-01 09:52:16 +02003735 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3736 if (local_err) {
3737 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003738 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003739 }
3740
3741 if (!image_bs) {
3742 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003743 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003744 }
3745
3746 if (bdrv_find_base(image_bs) == image_bs) {
3747 error_setg(errp, "not allowing backing file change on an image "
3748 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003749 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003750 }
3751
3752 /* even though we are not necessarily operating on bs, we need it to
3753 * determine if block ops are currently prohibited on the chain */
3754 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003755 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003756 }
3757
3758 /* final sanity check */
3759 if (!bdrv_chain_contains(bs, image_bs)) {
3760 error_setg(errp, "'%s' and image file are not in the same chain",
3761 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003762 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003763 }
3764
3765 /* if not r/w, reopen to make r/w */
3766 open_flags = image_bs->open_flags;
3767 ro = bdrv_is_read_only(image_bs);
3768
3769 if (ro) {
3770 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3771 if (local_err) {
3772 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003773 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003774 }
3775 }
3776
3777 ret = bdrv_change_backing_file(image_bs, backing_file,
3778 image_bs->drv ? image_bs->drv->format_name : "");
3779
3780 if (ret < 0) {
3781 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3782 backing_file);
3783 /* don't exit here, so we can try to restore open flags if
3784 * appropriate */
3785 }
3786
3787 if (ro) {
3788 bdrv_reopen(image_bs, open_flags, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003789 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02003790 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003791
3792out:
3793 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02003794}
3795
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003796void hmp_drive_add_node(Monitor *mon, const char *optstr)
3797{
3798 QemuOpts *opts;
3799 QDict *qdict;
3800 Error *local_err = NULL;
3801
3802 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
3803 if (!opts) {
3804 return;
3805 }
3806
3807 qdict = qemu_opts_to_qdict(opts, NULL);
3808
3809 if (!qdict_get_try_str(qdict, "node-name")) {
Kevin Wolff8746fb2016-03-16 11:14:31 +01003810 QDECREF(qdict);
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003811 error_report("'node-name' needs to be specified");
3812 goto out;
3813 }
3814
3815 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
3816 if (!bs) {
3817 error_report_err(local_err);
3818 goto out;
3819 }
3820
3821 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3822
3823out:
3824 qemu_opts_del(opts);
3825}
3826
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003827void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3828{
Max Reitzbe4b67b2015-10-19 17:53:09 +02003829 BlockDriverState *bs;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003830 QObject *obj;
Eric Blake3b098d52016-06-09 10:48:43 -06003831 Visitor *v = qmp_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003832 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003833 Error *local_err = NULL;
3834
Markus Armbruster60e19e02014-06-06 14:50:58 +02003835 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003836 * cache.direct=false instead of silently switching to aio=threads, except
Markus Armbruster60e19e02014-06-06 14:50:58 +02003837 * when called from drive_new().
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003838 *
3839 * For now, simply forbidding the combination for all drivers will do. */
3840 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
Kevin Wolfc6e0bd92014-03-06 15:47:32 +01003841 bool direct = options->has_cache &&
3842 options->cache->has_direct &&
3843 options->cache->direct;
3844 if (!direct) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003845 error_setg(errp, "aio=native requires cache.direct=true");
3846 goto fail;
3847 }
3848 }
3849
Eric Blake3b098d52016-06-09 10:48:43 -06003850 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003851 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003852 error_propagate(errp, local_err);
3853 goto fail;
3854 }
3855
Eric Blake3b098d52016-06-09 10:48:43 -06003856 visit_complete(v, &obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003857 qdict = qobject_to_qdict(obj);
3858
3859 qdict_flatten(qdict);
3860
Kevin Wolf9ec88732016-09-21 14:56:11 +02003861 if (!qdict_get_try_str(qdict, "node-name")) {
3862 error_setg(errp, "'node-name' must be specified for the root node");
3863 goto fail;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003864 }
3865
Kevin Wolf9ec88732016-09-21 14:56:11 +02003866 bs = bds_tree_init(qdict, errp);
3867 if (!bs) {
3868 goto fail;
3869 }
3870
3871 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3872
Max Reitzbe4b67b2015-10-19 17:53:09 +02003873 if (bs && bdrv_key_required(bs)) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02003874 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3875 bdrv_unref(bs);
Kevin Wolf8ae8e902014-03-06 15:43:42 +01003876 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3877 goto fail;
3878 }
3879
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003880fail:
Eric Blake3b098d52016-06-09 10:48:43 -06003881 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003882}
3883
Kevin Wolf9ec88732016-09-21 14:56:11 +02003884void qmp_x_blockdev_del(const char *node_name, Error **errp)
Alberto Garcia81b936a2015-11-02 16:51:55 +02003885{
3886 AioContext *aio_context;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003887 BlockDriverState *bs;
3888
Kevin Wolf9ec88732016-09-21 14:56:11 +02003889 bs = bdrv_find_node(node_name);
3890 if (!bs) {
3891 error_setg(errp, "Cannot find node %s", node_name);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003892 return;
3893 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003894 if (bdrv_has_blk(bs)) {
3895 error_setg(errp, "Node %s is in use", node_name);
3896 return;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003897 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003898 aio_context = bdrv_get_aio_context(bs);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003899 aio_context_acquire(aio_context);
3900
Kevin Wolf9ec88732016-09-21 14:56:11 +02003901 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3902 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003903 }
3904
Kevin Wolf9ec88732016-09-21 14:56:11 +02003905 if (!bs->monitor_list.tqe_prev) {
3906 error_setg(errp, "Node %s is not owned by the monitor",
3907 bs->node_name);
3908 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003909 }
3910
Kevin Wolf9ec88732016-09-21 14:56:11 +02003911 if (bs->refcnt > 1) {
3912 error_setg(errp, "Block device %s is in use",
3913 bdrv_get_device_or_node_name(bs));
3914 goto out;
3915 }
3916
3917 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3918 bdrv_unref(bs);
3919
Alberto Garcia81b936a2015-11-02 16:51:55 +02003920out:
3921 aio_context_release(aio_context);
3922}
3923
Wen Congyang7f821592016-05-10 15:36:39 +08003924static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
3925 const char *child_name)
3926{
3927 BdrvChild *child;
3928
3929 QLIST_FOREACH(child, &parent_bs->children, next) {
3930 if (strcmp(child->name, child_name) == 0) {
3931 return child;
3932 }
3933 }
3934
3935 return NULL;
3936}
3937
3938void qmp_x_blockdev_change(const char *parent, bool has_child,
3939 const char *child, bool has_node,
3940 const char *node, Error **errp)
3941{
3942 BlockDriverState *parent_bs, *new_bs = NULL;
3943 BdrvChild *p_child;
3944
3945 parent_bs = bdrv_lookup_bs(parent, parent, errp);
3946 if (!parent_bs) {
3947 return;
3948 }
3949
3950 if (has_child == has_node) {
3951 if (has_child) {
3952 error_setg(errp, "The parameters child and node are in conflict");
3953 } else {
3954 error_setg(errp, "Either child or node must be specified");
3955 }
3956 return;
3957 }
3958
3959 if (has_child) {
3960 p_child = bdrv_find_child(parent_bs, child);
3961 if (!p_child) {
3962 error_setg(errp, "Node '%s' does not have child '%s'",
3963 parent, child);
3964 return;
3965 }
3966 bdrv_del_child(parent_bs, p_child, errp);
3967 }
3968
3969 if (has_node) {
3970 new_bs = bdrv_find_node(node);
3971 if (!new_bs) {
3972 error_setg(errp, "Node '%s' not found", node);
3973 return;
3974 }
3975 bdrv_add_child(parent_bs, new_bs, errp);
3976 }
3977}
3978
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003979BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3980{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003981 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003982 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003983
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003984 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
3985 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3986 AioContext *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003987
3988 aio_context_acquire(aio_context);
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003989 elem->value = block_job_query(job);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003990 aio_context_release(aio_context);
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003991
3992 *p_next = elem;
3993 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003994 }
3995
3996 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003997}
Paolo Bonzini4d454572012-11-26 16:03:42 +01003998
Kevin Wolf00063832013-03-15 10:35:07 +01003999QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004000 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01004001 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01004002 .desc = {
4003 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004004 .name = "snapshot",
4005 .type = QEMU_OPT_BOOL,
4006 .help = "enable/disable snapshot mode",
4007 },{
Paolo Bonzinia9384af2013-02-08 14:06:12 +01004008 .name = "discard",
4009 .type = QEMU_OPT_STRING,
4010 .help = "discard operation (ignore/off, unmap/on)",
4011 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004012 .name = "aio",
4013 .type = QEMU_OPT_STRING,
4014 .help = "host AIO implementation (threads, native)",
4015 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01004016 .name = BDRV_OPT_CACHE_WB,
4017 .type = QEMU_OPT_BOOL,
4018 .help = "Enable writeback mode",
4019 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004020 .name = "format",
4021 .type = QEMU_OPT_STRING,
4022 .help = "disk format (raw, qcow2, ...)",
4023 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004024 .name = "rerror",
4025 .type = QEMU_OPT_STRING,
4026 .help = "read error action",
4027 },{
4028 .name = "werror",
4029 .type = QEMU_OPT_STRING,
4030 .help = "write error action",
4031 },{
Alberto Garcia4e200cf2016-09-15 17:53:05 +03004032 .name = BDRV_OPT_READ_ONLY,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004033 .type = QEMU_OPT_BOOL,
4034 .help = "open drive file as read-only",
4035 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004036 .name = "throttling.iops-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004037 .type = QEMU_OPT_NUMBER,
4038 .help = "limit total I/O operations per second",
4039 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004040 .name = "throttling.iops-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004041 .type = QEMU_OPT_NUMBER,
4042 .help = "limit read operations per second",
4043 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004044 .name = "throttling.iops-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004045 .type = QEMU_OPT_NUMBER,
4046 .help = "limit write operations per second",
4047 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004048 .name = "throttling.bps-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004049 .type = QEMU_OPT_NUMBER,
4050 .help = "limit total bytes per second",
4051 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004052 .name = "throttling.bps-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004053 .type = QEMU_OPT_NUMBER,
4054 .help = "limit read bytes per second",
4055 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004056 .name = "throttling.bps-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004057 .type = QEMU_OPT_NUMBER,
4058 .help = "limit write bytes per second",
4059 },{
Benoît Canet3e9fab62013-09-02 14:14:40 +02004060 .name = "throttling.iops-total-max",
4061 .type = QEMU_OPT_NUMBER,
4062 .help = "I/O operations burst",
4063 },{
4064 .name = "throttling.iops-read-max",
4065 .type = QEMU_OPT_NUMBER,
4066 .help = "I/O operations read burst",
4067 },{
4068 .name = "throttling.iops-write-max",
4069 .type = QEMU_OPT_NUMBER,
4070 .help = "I/O operations write burst",
4071 },{
4072 .name = "throttling.bps-total-max",
4073 .type = QEMU_OPT_NUMBER,
4074 .help = "total bytes burst",
4075 },{
4076 .name = "throttling.bps-read-max",
4077 .type = QEMU_OPT_NUMBER,
4078 .help = "total bytes read burst",
4079 },{
4080 .name = "throttling.bps-write-max",
4081 .type = QEMU_OPT_NUMBER,
4082 .help = "total bytes write burst",
4083 },{
Alberto Garcia8a0fc182016-02-18 12:27:02 +02004084 .name = "throttling.iops-total-max-length",
4085 .type = QEMU_OPT_NUMBER,
4086 .help = "length of the iops-total-max burst period, in seconds",
4087 },{
4088 .name = "throttling.iops-read-max-length",
4089 .type = QEMU_OPT_NUMBER,
4090 .help = "length of the iops-read-max burst period, in seconds",
4091 },{
4092 .name = "throttling.iops-write-max-length",
4093 .type = QEMU_OPT_NUMBER,
4094 .help = "length of the iops-write-max burst period, in seconds",
4095 },{
4096 .name = "throttling.bps-total-max-length",
4097 .type = QEMU_OPT_NUMBER,
4098 .help = "length of the bps-total-max burst period, in seconds",
4099 },{
4100 .name = "throttling.bps-read-max-length",
4101 .type = QEMU_OPT_NUMBER,
4102 .help = "length of the bps-read-max burst period, in seconds",
4103 },{
4104 .name = "throttling.bps-write-max-length",
4105 .type = QEMU_OPT_NUMBER,
4106 .help = "length of the bps-write-max burst period, in seconds",
4107 },{
Benoît Canet2024c1d2013-09-02 14:14:41 +02004108 .name = "throttling.iops-size",
4109 .type = QEMU_OPT_NUMBER,
4110 .help = "when limiting by iops max size of an I/O in bytes",
4111 },{
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004112 .name = "throttling.group",
4113 .type = QEMU_OPT_STRING,
4114 .help = "name of the block throttling group",
4115 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004116 .name = "copy-on-read",
4117 .type = QEMU_OPT_BOOL,
4118 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004119 },{
4120 .name = "detect-zeroes",
4121 .type = QEMU_OPT_STRING,
4122 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004123 },{
4124 .name = "stats-account-invalid",
4125 .type = QEMU_OPT_BOOL,
4126 .help = "whether to account for invalid I/O operations "
4127 "in the statistics",
4128 },{
4129 .name = "stats-account-failed",
4130 .type = QEMU_OPT_BOOL,
4131 .help = "whether to account for failed I/O operations "
4132 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004133 },
4134 { /* end of list */ }
4135 },
4136};
Kevin Wolf00063832013-03-15 10:35:07 +01004137
Max Reitzbd745e22015-10-19 17:53:32 +02004138static QemuOptsList qemu_root_bds_opts = {
4139 .name = "root-bds",
Kevin Wolf23f7fcb2016-03-15 14:43:14 +01004140 .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head),
Max Reitzbd745e22015-10-19 17:53:32 +02004141 .desc = {
4142 {
4143 .name = "discard",
4144 .type = QEMU_OPT_STRING,
4145 .help = "discard operation (ignore/off, unmap/on)",
4146 },{
Max Reitzbd745e22015-10-19 17:53:32 +02004147 .name = "aio",
4148 .type = QEMU_OPT_STRING,
4149 .help = "host AIO implementation (threads, native)",
4150 },{
Max Reitzbd745e22015-10-19 17:53:32 +02004151 .name = "copy-on-read",
4152 .type = QEMU_OPT_BOOL,
4153 .help = "copy read data from backing file into image file",
4154 },{
4155 .name = "detect-zeroes",
4156 .type = QEMU_OPT_STRING,
4157 .help = "try to optimize zero writes (off, on, unmap)",
4158 },
4159 { /* end of list */ }
4160 },
4161};
4162
Kevin Wolf00063832013-03-15 10:35:07 +01004163QemuOptsList qemu_drive_opts = {
4164 .name = "drive",
4165 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4166 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004167 /*
4168 * no elements => accept any params
4169 * validation will happen later
4170 */
Kevin Wolf00063832013-03-15 10:35:07 +01004171 { /* end of list */ }
4172 },
4173};