blob: 245e1e1d177ad879a134c1717858d060315d394e [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"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010046#include "qapi/qobject-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{
Max Reitzfbf81752015-10-19 17:53:31 +0200359 Error *local_error = NULL;
360 const char *aio;
361
362 if (bdrv_flags) {
Max Reitzfbf81752015-10-19 17:53:31 +0200363 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
364 *bdrv_flags |= BDRV_O_COPY_ON_READ;
365 }
366
Max Reitzfbf81752015-10-19 17:53:31 +0200367 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
368 if (!strcmp(aio, "native")) {
369 *bdrv_flags |= BDRV_O_NATIVE_AIO;
370 } else if (!strcmp(aio, "threads")) {
371 /* this is the default */
372 } else {
373 error_setg(errp, "invalid aio option");
374 return;
375 }
376 }
377 }
378
379 /* disk I/O throttling */
380 if (throttling_group) {
381 *throttling_group = qemu_opt_get(opts, "throttling.group");
382 }
383
384 if (throttle_cfg) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200385 throttle_config_init(throttle_cfg);
Max Reitzfbf81752015-10-19 17:53:31 +0200386 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
387 qemu_opt_get_number(opts, "throttling.bps-total", 0);
388 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
389 qemu_opt_get_number(opts, "throttling.bps-read", 0);
390 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
391 qemu_opt_get_number(opts, "throttling.bps-write", 0);
392 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
393 qemu_opt_get_number(opts, "throttling.iops-total", 0);
394 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
395 qemu_opt_get_number(opts, "throttling.iops-read", 0);
396 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
397 qemu_opt_get_number(opts, "throttling.iops-write", 0);
398
399 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
400 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
401 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
402 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
403 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
404 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
405 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
406 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
407 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
408 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
409 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
410 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
411
Alberto Garcia8a0fc182016-02-18 12:27:02 +0200412 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
413 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
414 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
415 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
416 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
417 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
418 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
419 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
420 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
421 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
422 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
423 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
424
Max Reitzfbf81752015-10-19 17:53:31 +0200425 throttle_cfg->op_size =
426 qemu_opt_get_number(opts, "throttling.iops-size", 0);
427
Alberto Garciad5851082016-02-18 12:26:59 +0200428 if (!throttle_is_valid(throttle_cfg, errp)) {
Max Reitzfbf81752015-10-19 17:53:31 +0200429 return;
430 }
431 }
432
433 if (detect_zeroes) {
434 *detect_zeroes =
435 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
436 qemu_opt_get(opts, "detect-zeroes"),
Eric Blake7fb1cf12015-11-18 01:52:57 -0700437 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Max Reitzfbf81752015-10-19 17:53:31 +0200438 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
439 &local_error);
440 if (local_error) {
441 error_propagate(errp, local_error);
442 return;
443 }
Max Reitzfbf81752015-10-19 17:53:31 +0200444 }
445}
446
Kevin Wolff298d072013-09-10 12:01:20 +0200447/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200448static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
449 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200450{
451 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200452 int bdrv_flags = 0;
453 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200454 bool account_invalid, account_failed;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300455 bool writethrough, read_only;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200456 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200457 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200458 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200459 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100460 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100461 QemuOpts *opts;
Alberto Garcia40119ef2015-11-16 11:28:38 +0200462 QDict *interval_dict = NULL;
463 QList *interval_list = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100464 const char *id;
Max Reitzfbf81752015-10-19 17:53:31 +0200465 BlockdevDetectZeroesOptions detect_zeroes =
466 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
467 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200468
Kevin Wolff298d072013-09-10 12:01:20 +0200469 /* Check common options by copying from bs_opts to opts, all other options
470 * stay in bs_opts for processing by bdrv_open(). */
471 id = qdict_get_try_str(bs_opts, "id");
Kevin Wolf00063832013-03-15 10:35:07 +0100472 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100473 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200474 error_propagate(errp, error);
Markus Armbruster6376f952014-05-28 11:17:01 +0200475 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100476 }
477
Kevin Wolf00063832013-03-15 10:35:07 +0100478 qemu_opts_absorb_qdict(opts, bs_opts, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100479 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200480 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100481 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100482 }
483
484 if (id) {
485 qdict_del(bs_opts, "id");
486 }
487
Markus Armbruster666daa62010-06-02 18:48:27 +0200488 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200489 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200490
Alberto Garcia362e9292015-10-28 17:33:04 +0200491 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
492 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
493
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100494 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
495
Alberto Garciaff356ee2016-07-08 17:03:00 +0300496 id = qemu_opts_id(opts);
497
Alberto Garcia40119ef2015-11-16 11:28:38 +0200498 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
499 qdict_array_split(interval_dict, &interval_list);
500
501 if (qdict_size(interval_dict) != 0) {
502 error_setg(errp, "Invalid option stats-intervals.%s",
503 qdict_first(interval_dict)->key);
504 goto early_err;
505 }
Alberto Garcia2be55062015-10-28 17:33:07 +0200506
Max Reitzfbf81752015-10-19 17:53:31 +0200507 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
508 &detect_zeroes, &error);
509 if (error) {
510 error_propagate(errp, error);
511 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200512 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200513
514 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100515 if (is_help_option(buf)) {
516 error_printf("Supported formats:");
517 bdrv_iterate_format(bdrv_format_print, NULL);
518 error_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100519 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200520 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200521
Max Reitze4342ce2015-02-05 13:58:14 -0500522 if (qdict_haskey(bs_opts, "driver")) {
523 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100524 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400525 }
Max Reitze4342ce2015-02-05 13:58:14 -0500526 qdict_put(bs_opts, "driver", qstring_from_str(buf));
Markus Armbruster666daa62010-06-02 18:48:27 +0200527 }
528
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200529 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200530 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200531 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100532 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200533 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100534 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200535 }
536 }
537
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200538 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200539 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200540 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100541 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200542 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100543 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200544 }
545 }
546
Max Reitz5ec18f82015-10-19 17:53:30 +0200547 if (snapshot) {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200548 bdrv_flags |= BDRV_O_SNAPSHOT;
Max Reitz5ec18f82015-10-19 17:53:30 +0200549 }
550
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300551 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
552
Kevin Wolf326642b2013-07-11 12:52:34 +0200553 /* init */
Kevin Wolf39c4ae92015-11-13 14:45:42 +0100554 if ((!file || !*file) && !qdict_size(bs_opts)) {
Max Reitz5ec18f82015-10-19 17:53:30 +0200555 BlockBackendRootState *blk_rs;
556
Max Reitz109525a2016-05-17 16:41:34 +0200557 blk = blk_new();
Max Reitz5ec18f82015-10-19 17:53:30 +0200558 blk_rs = blk_get_root_state(blk);
559 blk_rs->open_flags = bdrv_flags;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300560 blk_rs->read_only = read_only;
Max Reitz5ec18f82015-10-19 17:53:30 +0200561 blk_rs->detect_zeroes = detect_zeroes;
562
Max Reitze4342ce2015-02-05 13:58:14 -0500563 QDECREF(bs_opts);
564 } else {
565 if (file && !*file) {
566 file = NULL;
567 }
568
Kevin Wolf91a097e2015-05-08 17:49:53 +0200569 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
570 * with other callers) rather than what we want as the real defaults.
571 * Apply the defaults here instead. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200572 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
573 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300574 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
575 read_only ? "on" : "off");
Kevin Wolf72e775c2016-03-15 14:34:37 +0100576 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200577
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100578 if (runstate_check(RUN_STATE_INMIGRATE)) {
579 bdrv_flags |= BDRV_O_INACTIVE;
580 }
581
Max Reitzefaa7c42016-03-16 19:54:38 +0100582 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500583 if (!blk) {
584 goto err_no_bs_opts;
585 }
586 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500587
Max Reitz5ec18f82015-10-19 17:53:30 +0200588 bs->detect_zeroes = detect_zeroes;
589
Max Reitz5ec18f82015-10-19 17:53:30 +0200590 if (bdrv_key_required(bs)) {
591 autostart = 0;
592 }
Alberto Garcia362e9292015-10-28 17:33:04 +0200593
594 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
Alberto Garcia2be55062015-10-28 17:33:07 +0200595
Alberto Garcia40119ef2015-11-16 11:28:38 +0200596 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
597 blk_unref(blk);
598 blk = NULL;
599 goto err_no_bs_opts;
Alberto Garcia2be55062015-10-28 17:33:07 +0200600 }
Max Reitz5ec18f82015-10-19 17:53:30 +0200601 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200602
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100603 /* disk I/O throttling */
604 if (throttle_enabled(&cfg)) {
605 if (!throttling_group) {
Alberto Garciaff356ee2016-07-08 17:03:00 +0300606 throttling_group = id;
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100607 }
608 blk_io_limits_enable(blk, throttling_group);
609 blk_set_io_limits(blk, &cfg);
610 }
611
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100612 blk_set_enable_write_cache(blk, !writethrough);
Max Reitz373340b2015-10-19 17:53:22 +0200613 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200614
Alberto Garciaff356ee2016-07-08 17:03:00 +0300615 if (!monitor_add_blk(blk, id, errp)) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100616 blk_unref(blk);
617 blk = NULL;
618 goto err_no_bs_opts;
619 }
620
Max Reitze4342ce2015-02-05 13:58:14 -0500621err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100622 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200623 QDECREF(interval_dict);
624 QDECREF(interval_list);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200625 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100626
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100627early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100628 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200629 QDECREF(interval_dict);
630 QDECREF(interval_list);
Markus Armbruster6376f952014-05-28 11:17:01 +0200631err_no_opts:
632 QDECREF(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100633 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200634}
635
Max Reitzbd745e22015-10-19 17:53:32 +0200636/* Takes the ownership of bs_opts */
637static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
638{
Max Reitzbd745e22015-10-19 17:53:32 +0200639 int bdrv_flags = 0;
640
Kevin Wolfa81d6162016-03-07 14:23:04 +0100641 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
642 * with other callers) rather than what we want as the real defaults.
643 * Apply the defaults here instead. */
Kevin Wolfa81d6162016-03-07 14:23:04 +0100644 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
645 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300646 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
Kevin Wolfa81d6162016-03-07 14:23:04 +0100647
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100648 if (runstate_check(RUN_STATE_INMIGRATE)) {
649 bdrv_flags |= BDRV_O_INACTIVE;
650 }
651
Kevin Wolf74e1ae72016-09-20 20:48:52 +0200652 return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
Max Reitzbd745e22015-10-19 17:53:32 +0200653}
654
Max Reitz9c4218e2016-01-29 16:36:12 +0100655void blockdev_close_all_bdrv_states(void)
656{
657 BlockDriverState *bs, *next_bs;
658
659 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
660 AioContext *ctx = bdrv_get_aio_context(bs);
661
662 aio_context_acquire(ctx);
663 bdrv_unref(bs);
664 aio_context_release(ctx);
665 }
666}
667
Max Reitz262b4e82016-03-16 19:54:41 +0100668/* Iterates over the list of monitor-owned BlockDriverStates */
669BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
670{
671 return bs ? QTAILQ_NEXT(bs, monitor_list)
672 : QTAILQ_FIRST(&monitor_bdrv_states);
673}
674
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200675static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
676 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200677{
678 const char *value;
679
680 value = qemu_opt_get(opts, from);
681 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200682 if (qemu_opt_find(opts, to)) {
683 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
684 "same time", to, from);
685 return;
686 }
Jun Li20d6cd42014-09-24 13:45:27 +0800687 }
688
689 /* rename all items in opts */
690 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100691 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200692 qemu_opt_unset(opts, from);
693 }
694}
695
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200696QemuOptsList qemu_legacy_drive_opts = {
697 .name = "drive",
698 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
699 .desc = {
700 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200701 .name = "bus",
702 .type = QEMU_OPT_NUMBER,
703 .help = "bus number",
704 },{
705 .name = "unit",
706 .type = QEMU_OPT_NUMBER,
707 .help = "unit number (i.e. lun for scsi)",
708 },{
709 .name = "index",
710 .type = QEMU_OPT_NUMBER,
711 .help = "index number",
712 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200713 .name = "media",
714 .type = QEMU_OPT_STRING,
715 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200716 },{
717 .name = "if",
718 .type = QEMU_OPT_STRING,
719 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200720 },{
721 .name = "cyls",
722 .type = QEMU_OPT_NUMBER,
723 .help = "number of cylinders (ide disk geometry)",
724 },{
725 .name = "heads",
726 .type = QEMU_OPT_NUMBER,
727 .help = "number of heads (ide disk geometry)",
728 },{
729 .name = "secs",
730 .type = QEMU_OPT_NUMBER,
731 .help = "number of sectors (ide disk geometry)",
732 },{
733 .name = "trans",
734 .type = QEMU_OPT_STRING,
735 .help = "chs translation (auto, lba, none)",
Kevin Wolf26929292013-09-09 17:01:03 +0200736 },{
737 .name = "boot",
738 .type = QEMU_OPT_BOOL,
739 .help = "(deprecated, ignored)",
Kevin Wolf394c7d42013-09-13 14:09:17 +0200740 },{
741 .name = "addr",
742 .type = QEMU_OPT_STRING,
743 .help = "pci address (virtio only)",
Max Reitzd095b462013-12-20 19:28:14 +0100744 },{
Kevin Wolfbcf83152014-06-06 15:21:48 +0200745 .name = "serial",
746 .type = QEMU_OPT_STRING,
747 .help = "disk serial number",
748 },{
Max Reitzd095b462013-12-20 19:28:14 +0100749 .name = "file",
750 .type = QEMU_OPT_STRING,
751 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200752 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200753
754 /* Options that are passed on, but have special semantics with -drive */
755 {
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300756 .name = BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200757 .type = QEMU_OPT_BOOL,
758 .help = "open drive file as read-only",
759 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100760 .name = "rerror",
761 .type = QEMU_OPT_STRING,
762 .help = "read error action",
763 },{
764 .name = "werror",
765 .type = QEMU_OPT_STRING,
766 .help = "write error action",
767 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200768 .name = "copy-on-read",
769 .type = QEMU_OPT_BOOL,
770 .help = "copy read data from backing file into image file",
771 },
772
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200773 { /* end of list */ }
774 },
775};
776
Markus Armbruster60e19e02014-06-06 14:50:58 +0200777DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
Kevin Wolf57975222013-07-17 14:41:54 +0200778{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200779 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200780 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200781 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200782 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200783 QemuOpts *legacy_opts;
784 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200785 BlockInterfaceType type;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200786 int cyls, heads, secs, translation;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200787 int max_devs, bus_id, unit_id, index;
Kevin Wolf394c7d42013-09-13 14:09:17 +0200788 const char *devaddr;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100789 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800790 bool read_only = false;
791 bool copy_on_read;
Kevin Wolfbcf83152014-06-06 15:21:48 +0200792 const char *serial;
Max Reitzd095b462013-12-20 19:28:14 +0100793 const char *filename;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200794 Error *local_err = NULL;
Kevin Wolf247147f2014-09-24 16:37:14 +0200795 int i;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200796
Kevin Wolf57975222013-07-17 14:41:54 +0200797 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200798 static const struct {
799 const char *from;
800 const char *to;
801 } opt_renames[] = {
802 { "iops", "throttling.iops-total" },
803 { "iops_rd", "throttling.iops-read" },
804 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200805
Kevin Wolf247147f2014-09-24 16:37:14 +0200806 { "bps", "throttling.bps-total" },
807 { "bps_rd", "throttling.bps-read" },
808 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200809
Kevin Wolf247147f2014-09-24 16:37:14 +0200810 { "iops_max", "throttling.iops-total-max" },
811 { "iops_rd_max", "throttling.iops-read-max" },
812 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200813
Kevin Wolf247147f2014-09-24 16:37:14 +0200814 { "bps_max", "throttling.bps-total-max" },
815 { "bps_rd_max", "throttling.bps-read-max" },
816 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200817
Kevin Wolf247147f2014-09-24 16:37:14 +0200818 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200819
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200820 { "group", "throttling.group" },
821
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300822 { "readonly", BDRV_OPT_READ_ONLY },
Kevin Wolf247147f2014-09-24 16:37:14 +0200823 };
824
825 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200826 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
827 &local_err);
828 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100829 error_report_err(local_err);
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200830 return NULL;
831 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200832 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200833
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200834 value = qemu_opt_get(all_opts, "cache");
835 if (value) {
836 int flags = 0;
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100837 bool writethrough;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200838
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100839 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200840 error_report("invalid cache option");
841 return NULL;
842 }
843
844 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200845 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
846 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100847 !writethrough, &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200848 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200849 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
850 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100851 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200852 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200853 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
854 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100855 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200856 }
857 qemu_opt_unset(all_opts, "cache");
858 }
859
Kevin Wolff298d072013-09-10 12:01:20 +0200860 /* Get a QDict for processing the options */
861 bs_opts = qdict_new();
862 qemu_opts_to_qdict(all_opts, bs_opts);
863
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800864 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
865 &error_abort);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200866 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100867 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100868 error_report_err(local_err);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200869 goto fail;
870 }
871
Kevin Wolf26929292013-09-09 17:01:03 +0200872 /* Deprecated option boot=[on|off] */
873 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
874 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
875 "ignored. Future versions will reject this parameter. Please "
876 "update your scripts.\n");
877 }
878
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200879 /* Media type */
880 value = qemu_opt_get(legacy_opts, "media");
881 if (value) {
882 if (!strcmp(value, "disk")) {
883 media = MEDIA_DISK;
884 } else if (!strcmp(value, "cdrom")) {
885 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800886 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200887 } else {
888 error_report("'%s' invalid media", value);
889 goto fail;
890 }
891 }
892
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200893 /* copy-on-read is disabled with a warning for read-only devices */
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300894 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200895 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
896
897 if (read_only && copy_on_read) {
898 error_report("warning: disabling copy-on-read on read-only drive");
899 copy_on_read = false;
900 }
901
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300902 qdict_put(bs_opts, BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200903 qstring_from_str(read_only ? "on" : "off"));
904 qdict_put(bs_opts, "copy-on-read",
905 qstring_from_str(copy_on_read ? "on" :"off"));
906
Kevin Wolf593d4642013-08-28 17:24:51 +0200907 /* Controller type */
908 value = qemu_opt_get(legacy_opts, "if");
909 if (value) {
910 for (type = 0;
911 type < IF_COUNT && strcmp(value, if_name[type]);
912 type++) {
913 }
914 if (type == IF_COUNT) {
915 error_report("unsupported bus type '%s'", value);
916 goto fail;
917 }
918 } else {
919 type = block_default_type;
920 }
921
Kevin Wolfb41a7332013-09-09 16:49:49 +0200922 /* Geometry */
923 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
924 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
925 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
926
927 if (cyls || heads || secs) {
928 if (cyls < 1) {
929 error_report("invalid physical cyls number");
930 goto fail;
931 }
932 if (heads < 1) {
933 error_report("invalid physical heads number");
934 goto fail;
935 }
936 if (secs < 1) {
937 error_report("invalid physical secs number");
938 goto fail;
939 }
940 }
941
942 translation = BIOS_ATA_TRANSLATION_AUTO;
943 value = qemu_opt_get(legacy_opts, "trans");
944 if (value != NULL) {
945 if (!cyls) {
946 error_report("'%s' trans must be used with cyls, heads and secs",
947 value);
948 goto fail;
949 }
950 if (!strcmp(value, "none")) {
951 translation = BIOS_ATA_TRANSLATION_NONE;
952 } else if (!strcmp(value, "lba")) {
953 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +0100954 } else if (!strcmp(value, "large")) {
955 translation = BIOS_ATA_TRANSLATION_LARGE;
956 } else if (!strcmp(value, "rechs")) {
957 translation = BIOS_ATA_TRANSLATION_RECHS;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200958 } else if (!strcmp(value, "auto")) {
959 translation = BIOS_ATA_TRANSLATION_AUTO;
960 } else {
961 error_report("'%s' invalid translation type", value);
962 goto fail;
963 }
964 }
965
966 if (media == MEDIA_CDROM) {
967 if (cyls || secs || heads) {
968 error_report("CHS can't be set with media=cdrom");
969 goto fail;
970 }
971 }
972
Kevin Wolf87a899c2013-09-10 15:48:13 +0200973 /* Device address specified by bus/unit or index.
974 * If none was specified, try to find the first free one. */
975 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
976 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
977 index = qemu_opt_get_number(legacy_opts, "index", -1);
978
979 max_devs = if_max_devs[type];
980
981 if (index != -1) {
982 if (bus_id != 0 || unit_id != -1) {
983 error_report("index cannot be used with bus and unit");
984 goto fail;
985 }
986 bus_id = drive_index_to_bus_id(type, index);
987 unit_id = drive_index_to_unit_id(type, index);
988 }
989
990 if (unit_id == -1) {
991 unit_id = 0;
992 while (drive_get(type, bus_id, unit_id) != NULL) {
993 unit_id++;
994 if (max_devs && unit_id >= max_devs) {
995 unit_id -= max_devs;
996 bus_id++;
997 }
998 }
999 }
1000
1001 if (max_devs && unit_id >= max_devs) {
1002 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1003 goto fail;
1004 }
1005
1006 if (drive_get(type, bus_id, unit_id) != NULL) {
1007 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1008 bus_id, unit_id, index);
1009 goto fail;
1010 }
1011
Kevin Wolfbcf83152014-06-06 15:21:48 +02001012 /* Serial number */
1013 serial = qemu_opt_get(legacy_opts, "serial");
1014
Kevin Wolf87a899c2013-09-10 15:48:13 +02001015 /* no id supplied -> create one */
1016 if (qemu_opts_id(all_opts) == NULL) {
1017 char *new_id;
1018 const char *mediastr = "";
1019 if (type == IF_IDE || type == IF_SCSI) {
1020 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1021 }
1022 if (max_devs) {
1023 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1024 mediastr, unit_id);
1025 } else {
1026 new_id = g_strdup_printf("%s%s%i", if_name[type],
1027 mediastr, unit_id);
1028 }
1029 qdict_put(bs_opts, "id", qstring_from_str(new_id));
1030 g_free(new_id);
1031 }
1032
Kevin Wolf394c7d42013-09-13 14:09:17 +02001033 /* Add virtio block device */
1034 devaddr = qemu_opt_get(legacy_opts, "addr");
1035 if (devaddr && type != IF_VIRTIO) {
1036 error_report("addr is not supported by this bus type");
1037 goto fail;
1038 }
1039
1040 if (type == IF_VIRTIO) {
1041 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001042 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1043 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001044 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +02001045 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001046 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001047 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001048 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001049 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1050 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001051 if (devaddr) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001052 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001053 }
1054 }
1055
Max Reitzd095b462013-12-20 19:28:14 +01001056 filename = qemu_opt_get(legacy_opts, "file");
1057
Kevin Wolfee13ed12014-02-09 09:52:32 +01001058 /* Check werror/rerror compatibility with if=... */
1059 werror = qemu_opt_get(legacy_opts, "werror");
1060 if (werror != NULL) {
1061 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1062 type != IF_NONE) {
1063 error_report("werror is not supported by this bus type");
1064 goto fail;
1065 }
1066 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1067 }
1068
1069 rerror = qemu_opt_get(legacy_opts, "rerror");
1070 if (rerror != NULL) {
1071 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1072 type != IF_NONE) {
1073 error_report("rerror is not supported by this bus type");
1074 goto fail;
1075 }
1076 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1077 }
1078
Kevin Wolf2d246f02013-09-18 15:14:47 +02001079 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster18e46a02014-10-07 13:59:06 +02001080 blk = blockdev_init(filename, bs_opts, &local_err);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001081 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +02001082 if (!blk) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001083 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001084 error_report_err(local_err);
Kevin Wolfb6810722013-09-20 11:33:11 +02001085 }
Kevin Wolf2d246f02013-09-18 15:14:47 +02001086 goto fail;
Kevin Wolfb6810722013-09-20 11:33:11 +02001087 } else {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001088 assert(!local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001089 }
1090
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001091 /* Create legacy DriveInfo */
1092 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001093 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001094
Kevin Wolfb41a7332013-09-09 16:49:49 +02001095 dinfo->cyls = cyls;
1096 dinfo->heads = heads;
1097 dinfo->secs = secs;
1098 dinfo->trans = translation;
1099
Kevin Wolfee13ed12014-02-09 09:52:32 +01001100 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001101 dinfo->bus = bus_id;
1102 dinfo->unit = unit_id;
Kevin Wolf394c7d42013-09-13 14:09:17 +02001103 dinfo->devaddr = devaddr;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001104 dinfo->serial = g_strdup(serial);
1105
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001106 blk_set_legacy_dinfo(blk, dinfo);
1107
Kevin Wolfe34ef042013-09-19 14:24:10 +02001108 switch(type) {
1109 case IF_IDE:
1110 case IF_SCSI:
1111 case IF_XEN:
1112 case IF_NONE:
1113 dinfo->media_cd = media == MEDIA_CDROM;
1114 break;
1115 default:
1116 break;
1117 }
1118
Kevin Wolf2d246f02013-09-18 15:14:47 +02001119fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001120 qemu_opts_del(legacy_opts);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001121 QDECREF(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001122 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001123}
1124
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02001125static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1126{
1127 BlockDriverState *bs;
1128
1129 bs = bdrv_lookup_bs(name, name, errp);
1130 if (bs == NULL) {
1131 return NULL;
1132 }
1133
1134 if (!bdrv_is_root_node(bs)) {
1135 error_setg(errp, "Need a root block node");
1136 return NULL;
1137 }
1138
1139 if (!bdrv_is_inserted(bs)) {
1140 error_setg(errp, "Device has no medium");
1141 return NULL;
1142 }
1143
1144 return bs;
1145}
1146
Kevin Wolfb33945c2016-09-20 13:38:43 +02001147static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id,
1148 Error **errp)
1149{
1150 BlockBackend *blk;
1151
1152 if (!blk_name == !qdev_id) {
1153 error_setg(errp, "Need exactly one of 'device' and 'id'");
1154 return NULL;
1155 }
1156
1157 if (qdev_id) {
1158 blk = blk_by_qdev_id(qdev_id, errp);
1159 } else {
1160 blk = blk_by_name(blk_name);
1161 if (blk == NULL) {
1162 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1163 "Device '%s' not found", blk_name);
1164 }
1165 }
1166
1167 return blk;
1168}
1169
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001170void hmp_commit(Monitor *mon, const QDict *qdict)
Markus Armbruster666daa62010-06-02 18:48:27 +02001171{
Markus Armbruster666daa62010-06-02 18:48:27 +02001172 const char *device = qdict_get_str(qdict, "device");
Fam Zhenga0e85442015-03-02 19:36:48 +08001173 BlockBackend *blk;
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001174 int ret;
Markus Armbruster666daa62010-06-02 18:48:27 +02001175
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001176 if (!strcmp(device, "all")) {
Max Reitzda31d592016-03-16 19:54:32 +01001177 ret = blk_commit_all();
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001178 } else {
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001179 BlockDriverState *bs;
1180 AioContext *aio_context;
1181
Fam Zhenga0e85442015-03-02 19:36:48 +08001182 blk = blk_by_name(device);
1183 if (!blk) {
Jeff Cody58513bd2013-01-18 12:45:35 -05001184 monitor_printf(mon, "Device '%s' not found\n", device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +02001185 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001186 }
Max Reitz5433c242015-10-19 17:53:29 +02001187 if (!blk_is_available(blk)) {
1188 monitor_printf(mon, "Device '%s' has no medium\n", device);
1189 return;
1190 }
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001191
1192 bs = blk_bs(blk);
1193 aio_context = bdrv_get_aio_context(bs);
1194 aio_context_acquire(aio_context);
1195
1196 ret = bdrv_commit(bs);
1197
1198 aio_context_release(aio_context);
Jeff Cody58513bd2013-01-18 12:45:35 -05001199 }
1200 if (ret < 0) {
1201 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1202 strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +02001203 }
1204}
1205
Eric Blake10f75902016-03-03 09:16:50 -07001206static void blockdev_do_action(TransactionAction *action, Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001207{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001208 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001209
Eric Blake10f75902016-03-03 09:16:50 -07001210 list.value = action;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001211 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001212 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001213}
1214
Benoît Canet0901f672014-01-23 21:31:38 +01001215void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1216 bool has_node_name, const char *node_name,
1217 const char *snapshot_file,
1218 bool has_snapshot_node_name,
1219 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001220 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001221 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001222{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001223 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001224 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001225 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001226 .has_node_name = has_node_name,
1227 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001228 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001229 .has_snapshot_node_name = has_snapshot_node_name,
1230 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001231 .has_format = has_format,
1232 .format = (char *) format,
1233 .has_mode = has_mode,
1234 .mode = mode,
1235 };
Eric Blake10f75902016-03-03 09:16:50 -07001236 TransactionAction action = {
1237 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001238 .u.blockdev_snapshot_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001239 };
1240 blockdev_do_action(&action, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001241}
1242
Alberto Garcia43de7e22015-10-26 14:27:16 +02001243void qmp_blockdev_snapshot(const char *node, const char *overlay,
1244 Error **errp)
1245{
1246 BlockdevSnapshot snapshot_data = {
1247 .node = (char *) node,
1248 .overlay = (char *) overlay
1249 };
Eric Blake10f75902016-03-03 09:16:50 -07001250 TransactionAction action = {
1251 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
Eric Blake32bafa82016-03-17 16:48:37 -06001252 .u.blockdev_snapshot.data = &snapshot_data,
Eric Blake10f75902016-03-03 09:16:50 -07001253 };
1254 blockdev_do_action(&action, errp);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001255}
1256
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001257void qmp_blockdev_snapshot_internal_sync(const char *device,
1258 const char *name,
1259 Error **errp)
1260{
1261 BlockdevSnapshotInternal snapshot = {
1262 .device = (char *) device,
1263 .name = (char *) name
1264 };
Eric Blake10f75902016-03-03 09:16:50 -07001265 TransactionAction action = {
1266 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001267 .u.blockdev_snapshot_internal_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001268 };
1269 blockdev_do_action(&action, errp);
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001270}
1271
Wenchao Xia44e3e052013-09-11 14:04:36 +08001272SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1273 bool has_id,
1274 const char *id,
1275 bool has_name,
1276 const char *name,
1277 Error **errp)
1278{
Fam Zhenga0e85442015-03-02 19:36:48 +08001279 BlockDriverState *bs;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001280 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001281 QEMUSnapshotInfo sn;
1282 Error *local_err = NULL;
1283 SnapshotInfo *info = NULL;
1284 int ret;
1285
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001286 bs = qmp_get_root_bs(device, errp);
1287 if (!bs) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001288 return NULL;
1289 }
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001290 aio_context = bdrv_get_aio_context(bs);
Max Reitz5433c242015-10-19 17:53:29 +02001291 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001292
1293 if (!has_id) {
1294 id = NULL;
1295 }
1296
1297 if (!has_name) {
1298 name = NULL;
1299 }
1300
1301 if (!id && !name) {
1302 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001303 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001304 }
1305
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001306 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1307 goto out_aio_context;
1308 }
1309
Wenchao Xia44e3e052013-09-11 14:04:36 +08001310 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001311 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001312 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001313 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001314 }
1315 if (!ret) {
1316 error_setg(errp,
1317 "Snapshot with id '%s' and name '%s' does not exist on "
1318 "device '%s'",
1319 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001320 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001321 }
1322
1323 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001324 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001325 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001326 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001327 }
1328
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001329 aio_context_release(aio_context);
1330
Markus Armbruster5839e532014-08-19 10:31:08 +02001331 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001332 info->id = g_strdup(sn.id_str);
1333 info->name = g_strdup(sn.name);
1334 info->date_nsec = sn.date_nsec;
1335 info->date_sec = sn.date_sec;
1336 info->vm_state_size = sn.vm_state_size;
1337 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1338 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1339
1340 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001341
1342out_aio_context:
1343 aio_context_release(aio_context);
1344 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001345}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001346
John Snow341ebc22015-04-17 19:49:52 -04001347/**
1348 * block_dirty_bitmap_lookup:
1349 * Return a dirty bitmap (if present), after validating
1350 * the node reference and bitmap names.
1351 *
1352 * @node: The name of the BDS node to search for bitmaps
1353 * @name: The name of the bitmap to search for
1354 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1355 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1356 * @errp: Output pointer for error information. Can be NULL.
1357 *
1358 * @return: A bitmap object on success, or NULL on failure.
1359 */
1360static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1361 const char *name,
1362 BlockDriverState **pbs,
1363 AioContext **paio,
1364 Error **errp)
1365{
1366 BlockDriverState *bs;
1367 BdrvDirtyBitmap *bitmap;
1368 AioContext *aio_context;
1369
1370 if (!node) {
1371 error_setg(errp, "Node cannot be NULL");
1372 return NULL;
1373 }
1374 if (!name) {
1375 error_setg(errp, "Bitmap name cannot be NULL");
1376 return NULL;
1377 }
1378 bs = bdrv_lookup_bs(node, node, NULL);
1379 if (!bs) {
1380 error_setg(errp, "Node '%s' not found", node);
1381 return NULL;
1382 }
1383
1384 aio_context = bdrv_get_aio_context(bs);
1385 aio_context_acquire(aio_context);
1386
1387 bitmap = bdrv_find_dirty_bitmap(bs, name);
1388 if (!bitmap) {
1389 error_setg(errp, "Dirty bitmap '%s' not found", name);
1390 goto fail;
1391 }
1392
1393 if (pbs) {
1394 *pbs = bs;
1395 }
1396 if (paio) {
1397 *paio = aio_context;
1398 } else {
1399 aio_context_release(aio_context);
1400 }
1401
1402 return bitmap;
1403
1404 fail:
1405 aio_context_release(aio_context);
1406 return NULL;
1407}
1408
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001409/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001410
John Snow50f43f02015-11-05 18:13:09 -05001411typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001412
John Snow50f43f02015-11-05 18:13:09 -05001413/**
1414 * BlkActionOps:
1415 * Table of operations that define an Action.
1416 *
1417 * @instance_size: Size of state struct, in bytes.
1418 * @prepare: Prepare the work, must NOT be NULL.
1419 * @commit: Commit the changes, can be NULL.
1420 * @abort: Abort the changes on fail, can be NULL.
1421 * @clean: Clean up resources after all transaction actions have called
1422 * commit() or abort(). Can be NULL.
1423 *
1424 * Only prepare() may fail. In a single transaction, only one of commit() or
1425 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001426 */
John Snow50f43f02015-11-05 18:13:09 -05001427typedef struct BlkActionOps {
1428 size_t instance_size;
1429 void (*prepare)(BlkActionState *common, Error **errp);
1430 void (*commit)(BlkActionState *common);
1431 void (*abort)(BlkActionState *common);
1432 void (*clean)(BlkActionState *common);
1433} BlkActionOps;
1434
1435/**
1436 * BlkActionState:
1437 * Describes one Action's state within a Transaction.
1438 *
1439 * @action: QAPI-defined enum identifying which Action to perform.
1440 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001441 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001442 * @entry: List membership for all Actions in this Transaction.
1443 *
1444 * This structure must be arranged as first member in a subclassed type,
1445 * assuming that the compiler will also arrange it to the same offsets as the
1446 * base class.
1447 */
1448struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001449 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001450 const BlkActionOps *ops;
John Snow94d16a62015-11-05 18:13:18 -05001451 BlockJobTxn *block_job_txn;
1452 TransactionProperties *txn_props;
John Snow50f43f02015-11-05 18:13:09 -05001453 QSIMPLEQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001454};
1455
Wenchao Xiabbe86012013-09-11 14:04:34 +08001456/* internal snapshot private data */
1457typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001458 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001459 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001460 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001461 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001462 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001463} InternalSnapshotState;
1464
John Snow94d16a62015-11-05 18:13:18 -05001465
1466static int action_check_completion_mode(BlkActionState *s, Error **errp)
1467{
1468 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1469 error_setg(errp,
1470 "Action '%s' does not support Transaction property "
1471 "completion-mode = %s",
1472 TransactionActionKind_lookup[s->action->type],
1473 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1474 return -1;
1475 }
1476 return 0;
1477}
1478
John Snow50f43f02015-11-05 18:13:09 -05001479static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001480 Error **errp)
1481{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001482 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001483 const char *device;
1484 const char *name;
1485 BlockDriverState *bs;
1486 QEMUSnapshotInfo old_sn, *sn;
1487 bool ret;
1488 qemu_timeval tv;
1489 BlockdevSnapshotInternal *internal;
1490 InternalSnapshotState *state;
1491 int ret1;
1492
Eric Blake6a8f9662015-10-26 16:34:54 -06001493 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001494 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001495 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001496 state = DO_UPCAST(InternalSnapshotState, common, common);
1497
1498 /* 1. parse input */
1499 device = internal->device;
1500 name = internal->name;
1501
1502 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001503 if (action_check_completion_mode(common, errp) < 0) {
1504 return;
1505 }
1506
Kevin Wolf75dfd402016-06-23 14:20:24 +02001507 bs = qmp_get_root_bs(device, errp);
1508 if (!bs) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001509 return;
1510 }
1511
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001512 /* AioContext is released in .clean() */
Kevin Wolf75dfd402016-06-23 14:20:24 +02001513 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001514 aio_context_acquire(state->aio_context);
1515
Fam Zheng507306c2015-10-23 11:08:13 +08001516 state->bs = bs;
1517 bdrv_drained_begin(bs);
1518
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001519 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1520 return;
1521 }
1522
Wenchao Xiabbe86012013-09-11 14:04:34 +08001523 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001524 error_setg(errp, "Device '%s' is read only", device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001525 return;
1526 }
1527
1528 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001529 error_setg(errp, "Block format '%s' used by device '%s' "
1530 "does not support internal snapshots",
1531 bs->drv->format_name, device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001532 return;
1533 }
1534
1535 if (!strlen(name)) {
1536 error_setg(errp, "Name is empty");
1537 return;
1538 }
1539
1540 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001541 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1542 &local_err);
1543 if (local_err) {
1544 error_propagate(errp, local_err);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001545 return;
1546 } else if (ret) {
1547 error_setg(errp,
1548 "Snapshot with name '%s' already exists on device '%s'",
1549 name, device);
1550 return;
1551 }
1552
1553 /* 3. take the snapshot */
1554 sn = &state->sn;
1555 pstrcpy(sn->name, sizeof(sn->name), name);
1556 qemu_gettimeofday(&tv);
1557 sn->date_sec = tv.tv_sec;
1558 sn->date_nsec = tv.tv_usec * 1000;
1559 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1560
1561 ret1 = bdrv_snapshot_create(bs, sn);
1562 if (ret1 < 0) {
1563 error_setg_errno(errp, -ret1,
1564 "Failed to create snapshot '%s' on device '%s'",
1565 name, device);
1566 return;
1567 }
1568
1569 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001570 state->created = true;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001571}
1572
John Snow50f43f02015-11-05 18:13:09 -05001573static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001574{
1575 InternalSnapshotState *state =
1576 DO_UPCAST(InternalSnapshotState, common, common);
1577 BlockDriverState *bs = state->bs;
1578 QEMUSnapshotInfo *sn = &state->sn;
1579 Error *local_error = NULL;
1580
Fam Zheng507306c2015-10-23 11:08:13 +08001581 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001582 return;
1583 }
1584
1585 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001586 error_reportf_err(local_error,
1587 "Failed to delete snapshot with id '%s' and "
1588 "name '%s' on device '%s' in abort: ",
1589 sn->id_str, sn->name,
1590 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001591 }
1592}
1593
John Snow50f43f02015-11-05 18:13:09 -05001594static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001595{
1596 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1597 common, common);
1598
1599 if (state->aio_context) {
Fam Zheng507306c2015-10-23 11:08:13 +08001600 if (state->bs) {
1601 bdrv_drained_end(state->bs);
1602 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001603 aio_context_release(state->aio_context);
1604 }
1605}
1606
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001607/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001608typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001609 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001610 BlockDriverState *old_bs;
1611 BlockDriverState *new_bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001612 AioContext *aio_context;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001613} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001614
John Snow50f43f02015-11-05 18:13:09 -05001615static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001616 Error **errp)
1617{
Max Reitz5b363932016-05-17 16:41:31 +02001618 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001619 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001620 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001621 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001622 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001623 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001624 /* Reference to the new image (for 'blockdev-snapshot') */
1625 const char *snapshot_ref;
1626 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001627 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001628 ExternalSnapshotState *state =
1629 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001630 TransactionAction *action = common->action;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001631
Alberto Garcia43de7e22015-10-26 14:27:16 +02001632 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1633 * purpose but a different set of parameters */
1634 switch (action->type) {
1635 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1636 {
Eric Blake32bafa82016-03-17 16:48:37 -06001637 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001638 device = s->node;
1639 node_name = s->node;
1640 new_image_file = NULL;
1641 snapshot_ref = s->overlay;
1642 }
1643 break;
1644 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1645 {
Eric Blake32bafa82016-03-17 16:48:37 -06001646 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001647 device = s->has_device ? s->device : NULL;
1648 node_name = s->has_node_name ? s->node_name : NULL;
1649 new_image_file = s->snapshot_file;
1650 snapshot_ref = NULL;
1651 }
1652 break;
1653 default:
1654 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001655 }
1656
1657 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001658 if (action_check_completion_mode(common, errp) < 0) {
1659 return;
1660 }
1661
Alberto Garcia43de7e22015-10-26 14:27:16 +02001662 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1663 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001664 return;
1665 }
1666
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001667 /* Acquire AioContext now so any threads operating on old_bs stop */
1668 state->aio_context = bdrv_get_aio_context(state->old_bs);
1669 aio_context_acquire(state->aio_context);
Fam Zhengda763e82015-10-23 11:08:10 +08001670 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001671
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001672 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001673 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001674 return;
1675 }
1676
Fam Zheng3718d8a2014-05-23 21:29:43 +08001677 if (bdrv_op_is_blocked(state->old_bs,
1678 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001679 return;
1680 }
1681
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001682 if (!bdrv_is_read_only(state->old_bs)) {
1683 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001684 error_setg(errp, QERR_IO_ERROR);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001685 return;
1686 }
1687 }
1688
Benoît Canet212a5a82014-01-23 21:31:36 +01001689 if (!bdrv_is_first_non_filter(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001690 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
Benoît Canetf6186f42013-10-02 14:33:48 +02001691 return;
1692 }
1693
Alberto Garcia43de7e22015-10-26 14:27:16 +02001694 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001695 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001696 const char *format = s->has_format ? s->format : "qcow2";
1697 enum NewImageMode mode;
1698 const char *snapshot_node_name =
1699 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001700
Alberto Garcia43de7e22015-10-26 14:27:16 +02001701 if (node_name && !snapshot_node_name) {
1702 error_setg(errp, "New snapshot node name missing");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001703 return;
1704 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001705
1706 if (snapshot_node_name &&
1707 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1708 error_setg(errp, "New snapshot node name already in use");
1709 return;
1710 }
1711
1712 flags = state->old_bs->open_flags;
Kevin Wolf4c844982016-02-29 13:12:26 +01001713 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001714
1715 /* create new image w/backing file */
1716 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1717 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001718 int64_t size = bdrv_getlength(state->old_bs);
1719 if (size < 0) {
1720 error_setg_errno(errp, -size, "bdrv_getlength failed");
1721 return;
1722 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001723 bdrv_img_create(new_image_file, format,
1724 state->old_bs->filename,
1725 state->old_bs->drv->format_name,
Kevin Wolff86b8b52016-03-02 12:16:44 +01001726 NULL, size, flags, &local_err, false);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001727 if (local_err) {
1728 error_propagate(errp, local_err);
1729 return;
1730 }
1731 }
1732
1733 options = qdict_new();
1734 if (s->has_snapshot_node_name) {
1735 qdict_put(options, "node-name",
1736 qstring_from_str(snapshot_node_name));
1737 }
1738 qdict_put(options, "driver", qstring_from_str(format));
1739
1740 flags |= BDRV_O_NO_BACKING;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001741 }
1742
Max Reitz5b363932016-05-17 16:41:31 +02001743 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1744 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001745 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001746 if (!state->new_bs) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001747 return;
1748 }
1749
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001750 if (bdrv_has_blk(state->new_bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02001751 error_setg(errp, "The snapshot is already in use");
Alberto Garcia43de7e22015-10-26 14:27:16 +02001752 return;
1753 }
1754
1755 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1756 errp)) {
1757 return;
1758 }
1759
1760 if (state->new_bs->backing != NULL) {
1761 error_setg(errp, "The snapshot already has a backing image");
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001762 return;
1763 }
1764
1765 if (!state->new_bs->drv->supports_backing) {
1766 error_setg(errp, "The snapshot does not support backing images");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001767 }
1768}
1769
John Snow50f43f02015-11-05 18:13:09 -05001770static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001771{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001772 ExternalSnapshotState *state =
1773 DO_UPCAST(ExternalSnapshotState, common, common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001774
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001775 bdrv_set_aio_context(state->new_bs, state->aio_context);
1776
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001777 /* This removes our old bs and adds the new bs */
1778 bdrv_append(state->new_bs, state->old_bs);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001779 /* We don't need (or want) to use the transactional
1780 * bdrv_reopen_multiple() across all the entries at once, because we
1781 * don't want to abort all of them if one of them fails the reopen */
Kevin Wolf4c844982016-02-29 13:12:26 +01001782 if (!state->old_bs->copy_on_read) {
1783 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1784 NULL);
1785 }
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001786}
1787
John Snow50f43f02015-11-05 18:13:09 -05001788static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001789{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001790 ExternalSnapshotState *state =
1791 DO_UPCAST(ExternalSnapshotState, common, common);
1792 if (state->new_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001793 bdrv_unref(state->new_bs);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001794 }
Fam Zhengda763e82015-10-23 11:08:10 +08001795}
1796
John Snow50f43f02015-11-05 18:13:09 -05001797static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001798{
1799 ExternalSnapshotState *state =
1800 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001801 if (state->aio_context) {
Fam Zhengda763e82015-10-23 11:08:10 +08001802 bdrv_drained_end(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001803 aio_context_release(state->aio_context);
1804 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001805}
1806
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001807typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001808 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001809 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001810 AioContext *aio_context;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001811 BlockJob *job;
1812} DriveBackupState;
1813
John Snow111049a2016-11-08 01:50:38 -05001814static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
Pavel Butsykin81206a82016-07-22 11:17:50 +03001815 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001816
John Snow50f43f02015-11-05 18:13:09 -05001817static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001818{
1819 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001820 BlockDriverState *bs;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001821 DriveBackup *backup;
1822 Error *local_err = NULL;
1823
Eric Blake6a8f9662015-10-26 16:34:54 -06001824 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001825 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001826
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001827 bs = qmp_get_root_bs(backup->device, errp);
1828 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001829 return;
1830 }
1831
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001832 /* AioContext is released in .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001833 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001834 aio_context_acquire(state->aio_context);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001835 bdrv_drained_begin(bs);
1836 state->bs = bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001837
John Snow111049a2016-11-08 01:50:38 -05001838 state->job = do_drive_backup(backup, common->block_job_txn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001839 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001840 error_propagate(errp, local_err);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001841 return;
1842 }
John Snow111049a2016-11-08 01:50:38 -05001843}
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001844
John Snow111049a2016-11-08 01:50:38 -05001845static void drive_backup_commit(BlkActionState *common)
1846{
1847 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1848 assert(state->job);
1849 block_job_start(state->job);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001850}
1851
John Snow50f43f02015-11-05 18:13:09 -05001852static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001853{
1854 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001855
John Snow111049a2016-11-08 01:50:38 -05001856 if (state->job) {
1857 block_job_cancel_sync(state->job);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001858 }
1859}
1860
John Snow50f43f02015-11-05 18:13:09 -05001861static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001862{
1863 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1864
1865 if (state->aio_context) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001866 bdrv_drained_end(state->bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001867 aio_context_release(state->aio_context);
1868 }
1869}
1870
Fam Zhengbd8baec2014-12-18 18:37:06 +08001871typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001872 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001873 BlockDriverState *bs;
1874 BlockJob *job;
1875 AioContext *aio_context;
1876} BlockdevBackupState;
1877
John Snow111049a2016-11-08 01:50:38 -05001878static BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
1879 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001880
John Snow50f43f02015-11-05 18:13:09 -05001881static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001882{
1883 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1884 BlockdevBackup *backup;
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001885 BlockDriverState *bs, *target;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001886 Error *local_err = NULL;
1887
Eric Blake6a8f9662015-10-26 16:34:54 -06001888 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001889 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001890
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001891 bs = qmp_get_root_bs(backup->device, errp);
1892 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001893 return;
1894 }
1895
Kevin Wolf39d990a2016-08-02 19:22:04 +02001896 target = bdrv_lookup_bs(backup->target, backup->target, errp);
Max Reitz5433c242015-10-19 17:53:29 +02001897 if (!target) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001898 return;
1899 }
1900
1901 /* AioContext is released in .clean() */
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001902 state->aio_context = bdrv_get_aio_context(bs);
Kevin Wolf39d990a2016-08-02 19:22:04 +02001903 if (state->aio_context != bdrv_get_aio_context(target)) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001904 state->aio_context = NULL;
1905 error_setg(errp, "Backup between two IO threads is not implemented");
1906 return;
1907 }
1908 aio_context_acquire(state->aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001909 state->bs = bs;
Fam Zhengff52bf32015-10-23 11:08:12 +08001910 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001911
John Snow111049a2016-11-08 01:50:38 -05001912 state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001913 if (local_err) {
1914 error_propagate(errp, local_err);
1915 return;
1916 }
John Snow111049a2016-11-08 01:50:38 -05001917}
Fam Zhengbd8baec2014-12-18 18:37:06 +08001918
John Snow111049a2016-11-08 01:50:38 -05001919static void blockdev_backup_commit(BlkActionState *common)
1920{
1921 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1922 assert(state->job);
1923 block_job_start(state->job);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001924}
1925
John Snow50f43f02015-11-05 18:13:09 -05001926static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001927{
1928 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001929
John Snow111049a2016-11-08 01:50:38 -05001930 if (state->job) {
1931 block_job_cancel_sync(state->job);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001932 }
1933}
1934
John Snow50f43f02015-11-05 18:13:09 -05001935static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001936{
1937 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1938
1939 if (state->aio_context) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001940 bdrv_drained_end(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001941 aio_context_release(state->aio_context);
1942 }
1943}
1944
Fam Zhengdf9a6812015-11-09 18:16:54 +08001945typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05001946 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001947 BdrvDirtyBitmap *bitmap;
1948 BlockDriverState *bs;
1949 AioContext *aio_context;
1950 HBitmap *backup;
1951 bool prepared;
1952} BlockDirtyBitmapState;
1953
John Snow50f43f02015-11-05 18:13:09 -05001954static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08001955 Error **errp)
1956{
1957 Error *local_err = NULL;
1958 BlockDirtyBitmapAdd *action;
1959 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1960 common, common);
1961
John Snow94d16a62015-11-05 18:13:18 -05001962 if (action_check_completion_mode(common, errp) < 0) {
1963 return;
1964 }
1965
Eric Blake32bafa82016-03-17 16:48:37 -06001966 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001967 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1968 qmp_block_dirty_bitmap_add(action->node, action->name,
1969 action->has_granularity, action->granularity,
1970 &local_err);
1971
1972 if (!local_err) {
1973 state->prepared = true;
1974 } else {
1975 error_propagate(errp, local_err);
1976 }
1977}
1978
John Snow50f43f02015-11-05 18:13:09 -05001979static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08001980{
1981 BlockDirtyBitmapAdd *action;
1982 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1983 common, common);
1984
Eric Blake32bafa82016-03-17 16:48:37 -06001985 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001986 /* Should not be able to fail: IF the bitmap was added via .prepare(),
1987 * then the node reference and bitmap name must have been valid.
1988 */
1989 if (state->prepared) {
1990 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
1991 }
1992}
1993
John Snow50f43f02015-11-05 18:13:09 -05001994static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08001995 Error **errp)
1996{
1997 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1998 common, common);
1999 BlockDirtyBitmap *action;
2000
John Snow94d16a62015-11-05 18:13:18 -05002001 if (action_check_completion_mode(common, errp) < 0) {
2002 return;
2003 }
2004
Eric Blake32bafa82016-03-17 16:48:37 -06002005 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002006 state->bitmap = block_dirty_bitmap_lookup(action->node,
2007 action->name,
2008 &state->bs,
2009 &state->aio_context,
2010 errp);
2011 if (!state->bitmap) {
2012 return;
2013 }
2014
2015 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2016 error_setg(errp, "Cannot modify a frozen bitmap");
2017 return;
2018 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2019 error_setg(errp, "Cannot clear a disabled bitmap");
2020 return;
2021 }
2022
2023 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2024 /* AioContext is released in .clean() */
2025}
2026
John Snow50f43f02015-11-05 18:13:09 -05002027static void block_dirty_bitmap_clear_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002028{
2029 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2030 common, common);
2031
2032 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2033}
2034
John Snow50f43f02015-11-05 18:13:09 -05002035static void block_dirty_bitmap_clear_commit(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002036{
2037 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2038 common, common);
2039
2040 hbitmap_free(state->backup);
2041}
2042
John Snow50f43f02015-11-05 18:13:09 -05002043static void block_dirty_bitmap_clear_clean(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002044{
2045 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2046 common, common);
2047
2048 if (state->aio_context) {
2049 aio_context_release(state->aio_context);
2050 }
2051}
2052
John Snow50f43f02015-11-05 18:13:09 -05002053static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002054{
2055 error_setg(errp, "Transaction aborted using Abort action");
2056}
2057
John Snow50f43f02015-11-05 18:13:09 -05002058static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002059{
Stefan Weildfc6f862013-07-25 18:21:28 +02002060 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002061}
2062
John Snow50f43f02015-11-05 18:13:09 -05002063static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002064 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2065 .instance_size = sizeof(ExternalSnapshotState),
2066 .prepare = external_snapshot_prepare,
2067 .commit = external_snapshot_commit,
2068 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002069 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002070 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002071 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002072 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002073 .prepare = external_snapshot_prepare,
2074 .commit = external_snapshot_commit,
2075 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002076 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002077 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002078 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2079 .instance_size = sizeof(DriveBackupState),
2080 .prepare = drive_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002081 .commit = drive_backup_commit,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002082 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002083 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002084 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002085 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2086 .instance_size = sizeof(BlockdevBackupState),
2087 .prepare = blockdev_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002088 .commit = blockdev_backup_commit,
Fam Zhengbd8baec2014-12-18 18:37:06 +08002089 .abort = blockdev_backup_abort,
2090 .clean = blockdev_backup_clean,
2091 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002092 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002093 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002094 .prepare = abort_prepare,
2095 .commit = abort_commit,
2096 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002097 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2098 .instance_size = sizeof(InternalSnapshotState),
2099 .prepare = internal_snapshot_prepare,
2100 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002101 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002102 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002103 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2104 .instance_size = sizeof(BlockDirtyBitmapState),
2105 .prepare = block_dirty_bitmap_add_prepare,
2106 .abort = block_dirty_bitmap_add_abort,
2107 },
2108 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2109 .instance_size = sizeof(BlockDirtyBitmapState),
2110 .prepare = block_dirty_bitmap_clear_prepare,
2111 .commit = block_dirty_bitmap_clear_commit,
2112 .abort = block_dirty_bitmap_clear_abort,
2113 .clean = block_dirty_bitmap_clear_clean,
2114 }
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002115};
2116
John Snow94d16a62015-11-05 18:13:18 -05002117/**
2118 * Allocate a TransactionProperties structure if necessary, and fill
2119 * that structure with desired defaults if they are unset.
2120 */
2121static TransactionProperties *get_transaction_properties(
2122 TransactionProperties *props)
2123{
2124 if (!props) {
2125 props = g_new0(TransactionProperties, 1);
2126 }
2127
2128 if (!props->has_completion_mode) {
2129 props->has_completion_mode = true;
2130 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2131 }
2132
2133 return props;
2134}
2135
Jeff Cody8802d1f2012-02-28 15:54:06 -05002136/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002137 * 'Atomic' group operations. The operations are performed as a set, and if
2138 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002139 */
John Snow94d16a62015-11-05 18:13:18 -05002140void qmp_transaction(TransactionActionList *dev_list,
2141 bool has_props,
2142 struct TransactionProperties *props,
2143 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002144{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002145 TransactionActionList *dev_entry = dev_list;
John Snow94d16a62015-11-05 18:13:18 -05002146 BlockJobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002147 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002148 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002149
John Snow50f43f02015-11-05 18:13:09 -05002150 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002151 QSIMPLEQ_INIT(&snap_bdrv_states);
2152
John Snow94d16a62015-11-05 18:13:18 -05002153 /* Does this transaction get canceled as a group on failure?
2154 * If not, we don't really need to make a BlockJobTxn.
2155 */
2156 props = get_transaction_properties(props);
2157 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2158 block_job_txn = block_job_txn_new();
2159 }
2160
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002161 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002162 bdrv_drain_all();
2163
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002164 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002165 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002166 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002167 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002168
Jeff Cody8802d1f2012-02-28 15:54:06 -05002169 dev_info = dev_entry->value;
2170 dev_entry = dev_entry->next;
2171
Eric Blake6a8f9662015-10-26 16:34:54 -06002172 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002173
Eric Blake6a8f9662015-10-26 16:34:54 -06002174 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002175 assert(ops->instance_size > 0);
2176
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002177 state = g_malloc0(ops->instance_size);
2178 state->ops = ops;
2179 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002180 state->block_job_txn = block_job_txn;
2181 state->txn_props = props;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002182 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002183
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002184 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002185 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002186 error_propagate(errp, local_err);
2187 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002188 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002189 }
2190
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002191 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002192 if (state->ops->commit) {
2193 state->ops->commit(state);
2194 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002195 }
2196
2197 /* success */
2198 goto exit;
2199
2200delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002201 /* failure, and it is all-or-none; roll back all operations */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002202 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2203 if (state->ops->abort) {
2204 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002205 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002206 }
2207exit:
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002208 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2209 if (state->ops->clean) {
2210 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002211 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002212 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002213 }
John Snow94d16a62015-11-05 18:13:18 -05002214 if (!has_props) {
2215 qapi_free_TransactionProperties(props);
2216 }
2217 block_job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002218}
2219
Kevin Wolffbe2d812016-09-20 13:38:46 +02002220void qmp_eject(bool has_device, const char *device,
2221 bool has_id, const char *id,
2222 bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002223{
Max Reitz38f54bd2015-10-26 21:39:12 +01002224 Error *local_err = NULL;
John Snow3a3086b2016-05-18 17:53:40 -04002225 int rc;
Markus Armbruster666daa62010-06-02 18:48:27 +02002226
John Snow3a3086b2016-05-18 17:53:40 -04002227 if (!has_force) {
2228 force = false;
2229 }
2230
Kevin Wolffbe2d812016-09-20 13:38:46 +02002231 rc = do_open_tray(has_device ? device : NULL,
2232 has_id ? id : NULL,
2233 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002234 if (rc && rc != -ENOSYS) {
Max Reitz38f54bd2015-10-26 21:39:12 +01002235 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002236 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002237 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002238 error_free(local_err);
John Snow3a3086b2016-05-18 17:53:40 -04002239
Kevin Wolffbe2d812016-09-20 13:38:46 +02002240 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002241}
2242
Benoît Canet12d3ba82014-01-23 21:31:35 +01002243void qmp_block_passwd(bool has_device, const char *device,
2244 bool has_node_name, const char *node_name,
2245 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002246{
Benoît Canet12d3ba82014-01-23 21:31:35 +01002247 Error *local_err = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +02002248 BlockDriverState *bs;
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002249 AioContext *aio_context;
Markus Armbruster666daa62010-06-02 18:48:27 +02002250
Benoît Canet12d3ba82014-01-23 21:31:35 +01002251 bs = bdrv_lookup_bs(has_device ? device : NULL,
2252 has_node_name ? node_name : NULL,
2253 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002254 if (local_err) {
Benoît Canet12d3ba82014-01-23 21:31:35 +01002255 error_propagate(errp, local_err);
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02002256 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002257 }
2258
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002259 aio_context = bdrv_get_aio_context(bs);
2260 aio_context_acquire(aio_context);
2261
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002262 bdrv_add_key(bs, password, errp);
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002263
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002264 aio_context_release(aio_context);
Markus Armbruster666daa62010-06-02 18:48:27 +02002265}
2266
Colin Lordbf18bee2016-06-06 14:15:22 -04002267/*
2268 * Attempt to open the tray of @device.
2269 * If @force, ignore its tray lock.
2270 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2271 * On error, store an error through @errp and return -errno.
2272 * If @device does not exist, return -ENODEV.
2273 * If it has no removable media, return -ENOTSUP.
2274 * If it has no tray, return -ENOSYS.
2275 * If the guest was asked to open the tray, return -EINPROGRESS.
2276 * Else, return 0.
John Snow3a3086b2016-05-18 17:53:40 -04002277 */
Kevin Wolfb33945c2016-09-20 13:38:43 +02002278static int do_open_tray(const char *blk_name, const char *qdev_id,
2279 bool force, Error **errp)
Max Reitz7d8a9f72015-10-26 21:39:08 +01002280{
2281 BlockBackend *blk;
Kevin Wolfb33945c2016-09-20 13:38:43 +02002282 const char *device = qdev_id ?: blk_name;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002283 bool locked;
2284
Kevin Wolfb33945c2016-09-20 13:38:43 +02002285 blk = qmp_get_blk(blk_name, qdev_id, errp);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002286 if (!blk) {
John Snow3a3086b2016-05-18 17:53:40 -04002287 return -ENODEV;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002288 }
2289
2290 if (!blk_dev_has_removable_media(blk)) {
2291 error_setg(errp, "Device '%s' is not removable", device);
John Snow3a3086b2016-05-18 17:53:40 -04002292 return -ENOTSUP;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002293 }
2294
Max Reitz12c7ec82016-01-29 20:49:11 +01002295 if (!blk_dev_has_tray(blk)) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002296 error_setg(errp, "Device '%s' does not have a tray", device);
2297 return -ENOSYS;
Max Reitz12c7ec82016-01-29 20:49:11 +01002298 }
2299
Max Reitz7d8a9f72015-10-26 21:39:08 +01002300 if (blk_dev_is_tray_open(blk)) {
John Snow3a3086b2016-05-18 17:53:40 -04002301 return 0;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002302 }
2303
2304 locked = blk_dev_is_medium_locked(blk);
2305 if (locked) {
2306 blk_dev_eject_request(blk, force);
2307 }
2308
2309 if (!locked || force) {
2310 blk_dev_change_media_cb(blk, false);
2311 }
John Snow3a3086b2016-05-18 17:53:40 -04002312
2313 if (locked && !force) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002314 error_setg(errp, "Device '%s' is locked and force was not specified, "
2315 "wait for tray to open and try again", device);
2316 return -EINPROGRESS;
John Snow3a3086b2016-05-18 17:53:40 -04002317 }
2318
2319 return 0;
2320}
2321
Kevin Wolfb33945c2016-09-20 13:38:43 +02002322void qmp_blockdev_open_tray(bool has_device, const char *device,
2323 bool has_id, const char *id,
2324 bool has_force, bool force,
John Snow3a3086b2016-05-18 17:53:40 -04002325 Error **errp)
2326{
Colin Lordbf18bee2016-06-06 14:15:22 -04002327 Error *local_err = NULL;
2328 int rc;
2329
John Snow3a3086b2016-05-18 17:53:40 -04002330 if (!has_force) {
2331 force = false;
2332 }
Kevin Wolfb33945c2016-09-20 13:38:43 +02002333 rc = do_open_tray(has_device ? device : NULL,
2334 has_id ? id : NULL,
2335 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002336 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2337 error_propagate(errp, local_err);
2338 return;
2339 }
2340 error_free(local_err);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002341}
2342
Kevin Wolfb33945c2016-09-20 13:38:43 +02002343void qmp_blockdev_close_tray(bool has_device, const char *device,
2344 bool has_id, const char *id,
2345 Error **errp)
Max Reitzabaaf592015-10-26 21:39:09 +01002346{
2347 BlockBackend *blk;
2348
Kevin Wolfb33945c2016-09-20 13:38:43 +02002349 device = has_device ? device : NULL;
2350 id = has_id ? id : NULL;
2351
2352 blk = qmp_get_blk(device, id, errp);
Max Reitzabaaf592015-10-26 21:39:09 +01002353 if (!blk) {
Max Reitzabaaf592015-10-26 21:39:09 +01002354 return;
2355 }
2356
2357 if (!blk_dev_has_removable_media(blk)) {
Kevin Wolfb33945c2016-09-20 13:38:43 +02002358 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitzabaaf592015-10-26 21:39:09 +01002359 return;
2360 }
2361
Max Reitz12c7ec82016-01-29 20:49:11 +01002362 if (!blk_dev_has_tray(blk)) {
2363 /* Ignore this command on tray-less devices */
2364 return;
2365 }
2366
Max Reitzabaaf592015-10-26 21:39:09 +01002367 if (!blk_dev_is_tray_open(blk)) {
2368 return;
2369 }
2370
2371 blk_dev_change_media_cb(blk, true);
2372}
2373
Kevin Wolf00949ba2016-09-20 13:38:45 +02002374void qmp_x_blockdev_remove_medium(bool has_device, const char *device,
2375 bool has_id, const char *id, Error **errp)
Max Reitz2814f672015-10-26 21:39:10 +01002376{
2377 BlockBackend *blk;
2378 BlockDriverState *bs;
2379 AioContext *aio_context;
Kevin Wolf00949ba2016-09-20 13:38:45 +02002380 bool has_attached_device;
Max Reitz2814f672015-10-26 21:39:10 +01002381
Kevin Wolf00949ba2016-09-20 13:38:45 +02002382 device = has_device ? device : NULL;
2383 id = has_id ? id : NULL;
2384
2385 blk = qmp_get_blk(device, id, errp);
Max Reitz2814f672015-10-26 21:39:10 +01002386 if (!blk) {
Max Reitz2814f672015-10-26 21:39:10 +01002387 return;
2388 }
2389
2390 /* For BBs without a device, we can exchange the BDS tree at will */
Kevin Wolf00949ba2016-09-20 13:38:45 +02002391 has_attached_device = blk_get_attached_dev(blk);
Max Reitz2814f672015-10-26 21:39:10 +01002392
Kevin Wolf00949ba2016-09-20 13:38:45 +02002393 if (has_attached_device && !blk_dev_has_removable_media(blk)) {
2394 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002395 return;
2396 }
2397
Kevin Wolf00949ba2016-09-20 13:38:45 +02002398 if (has_attached_device && blk_dev_has_tray(blk) &&
2399 !blk_dev_is_tray_open(blk))
2400 {
2401 error_setg(errp, "Tray of device '%s' is not open", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002402 return;
2403 }
2404
2405 bs = blk_bs(blk);
2406 if (!bs) {
2407 return;
2408 }
2409
2410 aio_context = bdrv_get_aio_context(bs);
2411 aio_context_acquire(aio_context);
2412
2413 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2414 goto out;
2415 }
2416
Max Reitz2814f672015-10-26 21:39:10 +01002417 blk_remove_bs(blk);
2418
Max Reitz12c7ec82016-01-29 20:49:11 +01002419 if (!blk_dev_has_tray(blk)) {
2420 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2421 * called at all); therefore, the medium needs to be ejected here.
2422 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2423 * value passed here (i.e. false). */
2424 blk_dev_change_media_cb(blk, false);
2425 }
2426
Max Reitz2814f672015-10-26 21:39:10 +01002427out:
2428 aio_context_release(aio_context);
2429}
2430
Kevin Wolf716df212016-09-20 13:38:44 +02002431static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
Max Reitzd1299882015-10-26 21:39:11 +01002432 BlockDriverState *bs, Error **errp)
2433{
Max Reitzd1299882015-10-26 21:39:11 +01002434 bool has_device;
2435
Max Reitzd1299882015-10-26 21:39:11 +01002436 /* For BBs without a device, we can exchange the BDS tree at will */
2437 has_device = blk_get_attached_dev(blk);
2438
2439 if (has_device && !blk_dev_has_removable_media(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002440 error_setg(errp, "Device is not removable");
Max Reitzd1299882015-10-26 21:39:11 +01002441 return;
2442 }
2443
Max Reitz12c7ec82016-01-29 20:49:11 +01002444 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002445 error_setg(errp, "Tray of the device is not open");
Max Reitzd1299882015-10-26 21:39:11 +01002446 return;
2447 }
2448
2449 if (blk_bs(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002450 error_setg(errp, "There already is a medium in the device");
Max Reitzd1299882015-10-26 21:39:11 +01002451 return;
2452 }
2453
2454 blk_insert_bs(blk, bs);
2455
Max Reitz12c7ec82016-01-29 20:49:11 +01002456 if (!blk_dev_has_tray(blk)) {
2457 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2458 * called at all); therefore, the medium needs to be pushed into the
2459 * slot here.
2460 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2461 * value passed here (i.e. true). */
2462 blk_dev_change_media_cb(blk, true);
2463 }
Max Reitzd1299882015-10-26 21:39:11 +01002464}
2465
Kevin Wolf716df212016-09-20 13:38:44 +02002466void qmp_x_blockdev_insert_medium(bool has_device, const char *device,
2467 bool has_id, const char *id,
2468 const char *node_name, Error **errp)
Max Reitzd1299882015-10-26 21:39:11 +01002469{
Kevin Wolf716df212016-09-20 13:38:44 +02002470 BlockBackend *blk;
Max Reitzd1299882015-10-26 21:39:11 +01002471 BlockDriverState *bs;
2472
Kevin Wolf716df212016-09-20 13:38:44 +02002473 blk = qmp_get_blk(has_device ? device : NULL,
2474 has_id ? id : NULL,
2475 errp);
2476 if (!blk) {
2477 return;
2478 }
2479
Max Reitzd1299882015-10-26 21:39:11 +01002480 bs = bdrv_find_node(node_name);
2481 if (!bs) {
2482 error_setg(errp, "Node '%s' not found", node_name);
2483 return;
2484 }
2485
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002486 if (bdrv_has_blk(bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02002487 error_setg(errp, "Node '%s' is already in use", node_name);
Max Reitzd1299882015-10-26 21:39:11 +01002488 return;
2489 }
2490
Kevin Wolf716df212016-09-20 13:38:44 +02002491 qmp_blockdev_insert_anon_medium(blk, bs, errp);
Max Reitzd1299882015-10-26 21:39:11 +01002492}
2493
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002494void qmp_blockdev_change_medium(bool has_device, const char *device,
2495 bool has_id, const char *id,
2496 const char *filename,
Max Reitz24fb4132015-11-06 16:27:06 +01002497 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002498 bool has_read_only,
2499 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002500 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002501{
2502 BlockBackend *blk;
2503 BlockDriverState *medium_bs = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002504 int bdrv_flags;
Kevin Wolfb85114f2016-09-12 19:08:31 +02002505 bool detect_zeroes;
Colin Lord38a53d52016-06-08 13:56:28 -04002506 int rc;
Max Reitzde2c6c02015-10-26 21:39:13 +01002507 QDict *options = NULL;
2508 Error *err = NULL;
2509
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002510 blk = qmp_get_blk(has_device ? device : NULL,
2511 has_id ? id : NULL,
2512 errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002513 if (!blk) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002514 goto fail;
2515 }
2516
2517 if (blk_bs(blk)) {
2518 blk_update_root_state(blk);
2519 }
2520
2521 bdrv_flags = blk_get_open_flags_from_root_state(blk);
Alyssa Milburn156abc22016-02-06 13:36:18 +00002522 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2523 BDRV_O_PROTOCOL);
Max Reitzde2c6c02015-10-26 21:39:13 +01002524
Max Reitz39ff43d2015-11-11 04:49:44 +01002525 if (!has_read_only) {
2526 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2527 }
2528
2529 switch (read_only) {
2530 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2531 break;
2532
2533 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2534 bdrv_flags &= ~BDRV_O_RDWR;
2535 break;
2536
2537 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2538 bdrv_flags |= BDRV_O_RDWR;
2539 break;
2540
2541 default:
2542 abort();
2543 }
2544
Kevin Wolfb85114f2016-09-12 19:08:31 +02002545 options = qdict_new();
2546 detect_zeroes = blk_get_detect_zeroes_from_root_state(blk);
2547 qdict_put(options, "detect-zeroes",
2548 qstring_from_str(detect_zeroes ? "on" : "off"));
2549
Max Reitz24fb4132015-11-06 16:27:06 +01002550 if (has_format) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002551 qdict_put(options, "driver", qstring_from_str(format));
2552 }
2553
Max Reitz5b363932016-05-17 16:41:31 +02002554 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2555 if (!medium_bs) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002556 goto fail;
2557 }
2558
Max Reitzde2c6c02015-10-26 21:39:13 +01002559 bdrv_add_key(medium_bs, NULL, &err);
2560 if (err) {
2561 error_propagate(errp, err);
2562 goto fail;
2563 }
2564
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002565 rc = do_open_tray(has_device ? device : NULL,
2566 has_id ? id : NULL,
2567 false, &err);
Colin Lord38a53d52016-06-08 13:56:28 -04002568 if (rc && rc != -ENOSYS) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002569 error_propagate(errp, err);
2570 goto fail;
2571 }
Colin Lord38a53d52016-06-08 13:56:28 -04002572 error_free(err);
2573 err = NULL;
Max Reitzde2c6c02015-10-26 21:39:13 +01002574
Kevin Wolf24df38b2016-09-26 15:03:00 +02002575 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002576 if (err) {
2577 error_propagate(errp, err);
2578 goto fail;
2579 }
2580
Kevin Wolf716df212016-09-20 13:38:44 +02002581 qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002582 if (err) {
2583 error_propagate(errp, err);
2584 goto fail;
2585 }
2586
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002587 qmp_blockdev_close_tray(has_device, device, has_id, id, errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002588
2589fail:
2590 /* If the medium has been inserted, the device has its own reference, so
2591 * ours must be relinquished; and if it has not been inserted successfully,
2592 * the reference must be relinquished anyway */
2593 bdrv_unref(medium_bs);
2594}
2595
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002596/* throttling disk I/O limits */
Eric Blake4dc93972016-07-13 21:50:21 -06002597void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002598{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002599 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002600 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002601 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002602 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002603
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002604 blk = qmp_get_blk(arg->has_device ? arg->device : NULL,
2605 arg->has_id ? arg->id : NULL,
2606 errp);
Fam Zhenga0e85442015-03-02 19:36:48 +08002607 if (!blk) {
Luiz Capitulino80047da2011-12-14 16:49:14 -02002608 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002609 }
Max Reitz5433c242015-10-19 17:53:29 +02002610
2611 aio_context = blk_get_aio_context(blk);
2612 aio_context_acquire(aio_context);
2613
Fam Zhenga0e85442015-03-02 19:36:48 +08002614 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002615 if (!bs) {
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002616 error_setg(errp, "Device has no medium");
Max Reitz5433c242015-10-19 17:53:29 +02002617 goto out;
2618 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002619
Alberto Garcia1588ab52016-02-18 12:27:00 +02002620 throttle_config_init(&cfg);
Eric Blake4dc93972016-07-13 21:50:21 -06002621 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2622 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2623 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002624
Eric Blake4dc93972016-07-13 21:50:21 -06002625 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2626 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2627 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
Benoît Canetcc0681c2013-09-02 14:14:39 +02002628
Eric Blake4dc93972016-07-13 21:50:21 -06002629 if (arg->has_bps_max) {
2630 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002631 }
Eric Blake4dc93972016-07-13 21:50:21 -06002632 if (arg->has_bps_rd_max) {
2633 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002634 }
Eric Blake4dc93972016-07-13 21:50:21 -06002635 if (arg->has_bps_wr_max) {
2636 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002637 }
Eric Blake4dc93972016-07-13 21:50:21 -06002638 if (arg->has_iops_max) {
2639 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002640 }
Eric Blake4dc93972016-07-13 21:50:21 -06002641 if (arg->has_iops_rd_max) {
2642 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002643 }
Eric Blake4dc93972016-07-13 21:50:21 -06002644 if (arg->has_iops_wr_max) {
2645 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002646 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002647
Eric Blake4dc93972016-07-13 21:50:21 -06002648 if (arg->has_bps_max_length) {
2649 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002650 }
Eric Blake4dc93972016-07-13 21:50:21 -06002651 if (arg->has_bps_rd_max_length) {
2652 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002653 }
Eric Blake4dc93972016-07-13 21:50:21 -06002654 if (arg->has_bps_wr_max_length) {
2655 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002656 }
Eric Blake4dc93972016-07-13 21:50:21 -06002657 if (arg->has_iops_max_length) {
2658 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002659 }
Eric Blake4dc93972016-07-13 21:50:21 -06002660 if (arg->has_iops_rd_max_length) {
2661 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002662 }
Eric Blake4dc93972016-07-13 21:50:21 -06002663 if (arg->has_iops_wr_max_length) {
2664 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002665 }
2666
Eric Blake4dc93972016-07-13 21:50:21 -06002667 if (arg->has_iops_size) {
2668 cfg.op_size = arg->iops_size;
Benoît Canet2024c1d2013-09-02 14:14:41 +02002669 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002670
Alberto Garciad5851082016-02-18 12:26:59 +02002671 if (!throttle_is_valid(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002672 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002673 }
2674
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002675 if (throttle_enabled(&cfg)) {
2676 /* Enable I/O limits if they're not enabled yet, otherwise
2677 * just update the throttling group. */
Kevin Wolf97148072016-03-21 13:53:52 +01002678 if (!blk_get_public(blk)->throttle_state) {
Eric Blake4dc93972016-07-13 21:50:21 -06002679 blk_io_limits_enable(blk,
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002680 arg->has_group ? arg->group :
2681 arg->has_device ? arg->device :
2682 arg->id);
Eric Blake4dc93972016-07-13 21:50:21 -06002683 } else if (arg->has_group) {
2684 blk_io_limits_update_group(blk, arg->group);
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002685 }
2686 /* Set the new throttling configuration */
Kevin Wolf97148072016-03-21 13:53:52 +01002687 blk_set_io_limits(blk, &cfg);
2688 } else if (blk_get_public(blk)->throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002689 /* If all throttling settings are set to 0, disable I/O limits */
Kevin Wolf97148072016-03-21 13:53:52 +01002690 blk_io_limits_disable(blk);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002691 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002692
Max Reitz5433c242015-10-19 17:53:29 +02002693out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002694 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002695}
2696
John Snow341ebc22015-04-17 19:49:52 -04002697void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2698 bool has_granularity, uint32_t granularity,
2699 Error **errp)
2700{
2701 AioContext *aio_context;
2702 BlockDriverState *bs;
2703
2704 if (!name || name[0] == '\0') {
2705 error_setg(errp, "Bitmap name cannot be empty");
2706 return;
2707 }
2708
2709 bs = bdrv_lookup_bs(node, node, errp);
2710 if (!bs) {
2711 return;
2712 }
2713
2714 aio_context = bdrv_get_aio_context(bs);
2715 aio_context_acquire(aio_context);
2716
2717 if (has_granularity) {
2718 if (granularity < 512 || !is_power_of_2(granularity)) {
2719 error_setg(errp, "Granularity must be power of 2 "
2720 "and at least 512");
2721 goto out;
2722 }
2723 } else {
2724 /* Default to cluster size, if available: */
2725 granularity = bdrv_get_default_bitmap_granularity(bs);
2726 }
2727
2728 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2729
2730 out:
2731 aio_context_release(aio_context);
2732}
2733
2734void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2735 Error **errp)
2736{
2737 AioContext *aio_context;
2738 BlockDriverState *bs;
2739 BdrvDirtyBitmap *bitmap;
2740
2741 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2742 if (!bitmap || !bs) {
2743 return;
2744 }
2745
John Snow9bd2b082015-04-17 19:49:57 -04002746 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2747 error_setg(errp,
2748 "Bitmap '%s' is currently frozen and cannot be removed",
2749 name);
2750 goto out;
2751 }
John Snow20dca812015-04-17 19:50:02 -04002752 bdrv_dirty_bitmap_make_anon(bitmap);
John Snow341ebc22015-04-17 19:49:52 -04002753 bdrv_release_dirty_bitmap(bs, bitmap);
2754
John Snow9bd2b082015-04-17 19:49:57 -04002755 out:
John Snow341ebc22015-04-17 19:49:52 -04002756 aio_context_release(aio_context);
2757}
2758
John Snowe74e6b72015-04-17 19:49:59 -04002759/**
2760 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2761 * immediately after a full backup operation.
2762 */
2763void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2764 Error **errp)
2765{
2766 AioContext *aio_context;
2767 BdrvDirtyBitmap *bitmap;
2768 BlockDriverState *bs;
2769
2770 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2771 if (!bitmap || !bs) {
2772 return;
2773 }
2774
2775 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2776 error_setg(errp,
2777 "Bitmap '%s' is currently frozen and cannot be modified",
2778 name);
2779 goto out;
2780 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2781 error_setg(errp,
2782 "Bitmap '%s' is currently disabled and cannot be cleared",
2783 name);
2784 goto out;
2785 }
2786
Fam Zhengdf9a6812015-11-09 18:16:54 +08002787 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04002788
2789 out:
2790 aio_context_release(aio_context);
2791}
2792
Markus Armbruster072ebe62015-03-05 17:00:56 +01002793void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06002794{
2795 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002796 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06002797 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002798 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08002799 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06002800
Kevin Wolf2073d412016-02-23 17:50:37 +01002801 bs = bdrv_find_node(id);
2802 if (bs) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02002803 qmp_x_blockdev_del(id, &local_err);
Kevin Wolf2073d412016-02-23 17:50:37 +01002804 if (local_err) {
2805 error_report_err(local_err);
2806 }
2807 return;
2808 }
2809
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002810 blk = blk_by_name(id);
2811 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02002812 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01002813 return;
Ryan Harper9063f812010-11-12 11:07:13 -06002814 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002815
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02002816 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02002817 error_report("Deleting device added with blockdev-add"
2818 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01002819 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02002820 }
2821
Max Reitz5433c242015-10-19 17:53:29 +02002822 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002823 aio_context_acquire(aio_context);
2824
Max Reitz5433c242015-10-19 17:53:29 +02002825 bs = blk_bs(blk);
2826 if (bs) {
2827 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2828 error_report_err(local_err);
2829 aio_context_release(aio_context);
2830 return;
2831 }
Ryan Harper9063f812010-11-12 11:07:13 -06002832
Max Reitz938abd42016-01-29 16:36:09 +01002833 blk_remove_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002834 }
Ryan Harper9063f812010-11-12 11:07:13 -06002835
Max Reitz7c735872016-03-16 19:54:39 +01002836 /* Make the BlockBackend and the attached BlockDriverState anonymous */
Max Reitzefaa7c42016-03-16 19:54:38 +01002837 monitor_remove_blk(blk);
2838
Max Reitz7c735872016-03-16 19:54:39 +01002839 /* If this BlockBackend has a device attached to it, its refcount will be
2840 * decremented when the device is removed; otherwise we have to do so here.
Ryan Harperd22b2f42011-03-29 20:51:47 -05002841 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02002842 if (blk_get_attached_dev(blk)) {
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02002843 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02002844 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2845 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05002846 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02002847 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06002848 }
2849
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002850 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06002851}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002852
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002853void qmp_block_resize(bool has_device, const char *device,
2854 bool has_node_name, const char *node_name,
2855 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002856{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002857 Error *local_err = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002858 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002859 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02002860 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002861
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002862 bs = bdrv_lookup_bs(has_device ? device : NULL,
2863 has_node_name ? node_name : NULL,
2864 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002865 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002866 error_propagate(errp, local_err);
2867 return;
2868 }
2869
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002870 aio_context = bdrv_get_aio_context(bs);
2871 aio_context_acquire(aio_context);
2872
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002873 if (!bdrv_is_first_non_filter(bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002874 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002875 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002876 }
2877
2878 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002879 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002880 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002881 }
2882
Jeff Cody9c75e162014-06-25 16:55:30 -04002883 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002884 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002885 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04002886 }
2887
Peter Lieven92b7a082013-03-11 11:04:24 +01002888 /* complete all in-flight operations before resizing the device */
2889 bdrv_drain_all();
2890
Kevin Wolf87329012013-05-02 15:32:55 +02002891 ret = bdrv_truncate(bs, size);
2892 switch (ret) {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002893 case 0:
2894 break;
2895 case -ENOMEDIUM:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002896 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002897 break;
2898 case -ENOTSUP:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002899 error_setg(errp, QERR_UNSUPPORTED);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002900 break;
2901 case -EACCES:
Alberto Garcia81e5f782015-04-08 12:29:19 +03002902 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002903 break;
2904 case -EBUSY:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002905 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002906 break;
2907 default:
Kevin Wolf87329012013-05-02 15:32:55 +02002908 error_setg_errno(errp, -ret, "Could not resize");
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002909 break;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002910 }
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002911
2912out:
2913 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002914}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002915
Alberto Garcia23233222016-07-05 17:28:59 +03002916void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002917 bool has_base, const char *base,
Alberto Garcia312fe092016-10-28 10:08:19 +03002918 bool has_base_node, const char *base_node,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002919 bool has_backing_file, const char *backing_file,
2920 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002921 bool has_on_error, BlockdevOnError on_error,
2922 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002923{
Alberto Garcia554b6142016-10-28 10:08:11 +03002924 BlockDriverState *bs, *iter;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002925 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002926 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002927 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002928 const char *base_name = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002929
Paolo Bonzini1d809092012-09-28 17:22:59 +02002930 if (!has_on_error) {
2931 on_error = BLOCKDEV_ON_ERROR_REPORT;
2932 }
2933
Alberto Garcia554b6142016-10-28 10:08:11 +03002934 bs = bdrv_lookup_bs(device, device, errp);
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002935 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002936 return;
2937 }
2938
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002939 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002940 aio_context_acquire(aio_context);
2941
Alberto Garcia312fe092016-10-28 10:08:19 +03002942 if (has_base && has_base_node) {
2943 error_setg(errp, "'base' and 'base-node' cannot be specified "
2944 "at the same time");
2945 goto out;
2946 }
2947
Jeff Cody13d8cc52014-06-25 15:40:11 -04002948 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002949 base_bs = bdrv_find_backing_image(bs, base);
2950 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002951 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002952 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002953 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002954 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04002955 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002956 }
2957
Alberto Garcia312fe092016-10-28 10:08:19 +03002958 if (has_base_node) {
2959 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2960 if (!base_bs) {
2961 goto out;
2962 }
2963 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
2964 error_setg(errp, "Node '%s' is not a backing image of '%s'",
2965 base_node, device);
2966 goto out;
2967 }
2968 assert(bdrv_get_aio_context(base_bs) == aio_context);
2969 base_name = base_bs->filename;
2970 }
2971
Alberto Garcia554b6142016-10-28 10:08:11 +03002972 /* Check for op blockers in the whole chain between bs and base */
2973 for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) {
2974 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
2975 goto out;
2976 }
2977 }
2978
Jeff Cody13d8cc52014-06-25 15:40:11 -04002979 /* if we are streaming the entire chain, the result will have no backing
2980 * file, and specifying one is therefore an error */
2981 if (base_bs == NULL && has_backing_file) {
2982 error_setg(errp, "backing file specified, but streaming the "
2983 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002984 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002985 }
2986
2987 /* backing_file string overrides base bs filename */
2988 base_name = has_backing_file ? backing_file : base_name;
2989
Alberto Garcia23233222016-07-05 17:28:59 +03002990 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
John Snow8254b6d2016-10-27 12:06:58 -04002991 has_speed ? speed : 0, on_error, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002992 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002993 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002994 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002995 }
2996
2997 trace_qmp_block_stream(bs, bs->job);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002998
2999out:
3000 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003001}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003002
Alberto Garciafd62c602016-07-05 17:29:00 +03003003void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003004 bool has_base, const char *base,
3005 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003006 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04003007 bool has_speed, int64_t speed,
3008 Error **errp)
3009{
3010 BlockDriverState *bs;
Alberto Garcia058223a2016-10-28 10:08:07 +03003011 BlockDriverState *iter;
Jeff Codyed61fc12012-09-27 13:29:16 -04003012 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003013 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04003014 Error *local_err = NULL;
3015 /* This will be part of the QMP command, if/when the
3016 * BlockdevOnError change for blkmirror makes it in
3017 */
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02003018 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
Jeff Codyed61fc12012-09-27 13:29:16 -04003019
Max Reitz54504662014-04-10 19:36:25 +02003020 if (!has_speed) {
3021 speed = 0;
3022 }
3023
Jeff Cody7676e2c2014-06-30 15:14:15 +02003024 /* Important Note:
3025 * libvirt relies on the DeviceNotFound error class in order to probe for
3026 * live commit feature versions; for this to work, we must make sure to
3027 * perform the device lookup before any generic errors that may occur in a
3028 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02003029 bs = qmp_get_root_bs(device, &local_err);
3030 if (!bs) {
3031 bs = bdrv_lookup_bs(device, device, NULL);
3032 if (!bs) {
3033 error_free(local_err);
3034 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3035 "Device '%s' not found", device);
3036 } else {
3037 error_propagate(errp, local_err);
3038 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003039 return;
3040 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003041
Kevin Wolf1d13b162016-06-23 14:20:24 +02003042 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003043 aio_context_acquire(aio_context);
3044
Fam Zhengbb000212014-09-11 13:14:00 +08003045 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003046 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003047 }
3048
Jeff Codyed61fc12012-09-27 13:29:16 -04003049 /* default top_bs is the active layer */
3050 top_bs = bs;
3051
Jeff Cody7676e2c2014-06-30 15:14:15 +02003052 if (has_top && top) {
Jeff Codyed61fc12012-09-27 13:29:16 -04003053 if (strcmp(bs->filename, top) != 0) {
3054 top_bs = bdrv_find_backing_image(bs, top);
3055 }
3056 }
3057
3058 if (top_bs == NULL) {
3059 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003060 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003061 }
3062
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003063 assert(bdrv_get_aio_context(top_bs) == aio_context);
3064
Jeff Codyd5208c42012-10-16 15:49:10 -04003065 if (has_base && base) {
3066 base_bs = bdrv_find_backing_image(top_bs, base);
3067 } else {
3068 base_bs = bdrv_find_base(top_bs);
3069 }
3070
3071 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003072 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003073 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04003074 }
3075
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003076 assert(bdrv_get_aio_context(base_bs) == aio_context);
3077
Alberto Garcia058223a2016-10-28 10:08:07 +03003078 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
3079 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3080 goto out;
3081 }
Fam Zhengbb000212014-09-11 13:14:00 +08003082 }
3083
Jeff Cody7676e2c2014-06-30 15:14:15 +02003084 /* Do not allow attempts to commit an image into itself */
3085 if (top_bs == base_bs) {
3086 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003087 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02003088 }
3089
Fam Zheng20a63d22013-12-16 14:45:31 +08003090 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04003091 if (has_backing_file) {
3092 error_setg(errp, "'backing-file' specified,"
3093 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003094 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003095 }
John Snow47970df2016-10-27 12:06:57 -04003096 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
John Snow8254b6d2016-10-27 12:06:58 -04003097 BLOCK_JOB_DEFAULT, speed, on_error, NULL, NULL,
3098 &local_err, false);
Fam Zheng20a63d22013-12-16 14:45:31 +08003099 } else {
Alberto Garcia058223a2016-10-28 10:08:07 +03003100 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
3101 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3102 goto out;
3103 }
Alberto Garciafd62c602016-07-05 17:29:00 +03003104 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed,
John Snow8254b6d2016-10-27 12:06:58 -04003105 on_error, has_backing_file ? backing_file : NULL,
3106 &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003107 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003108 if (local_err != NULL) {
3109 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003110 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003111 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003112
3113out:
3114 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003115}
3116
John Snow111049a2016-11-08 01:50:38 -05003117static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
3118 Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003119{
3120 BlockDriverState *bs;
3121 BlockDriverState *target_bs;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003122 BlockDriverState *source = NULL;
John Snow111049a2016-11-08 01:50:38 -05003123 BlockJob *job = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003124 BdrvDirtyBitmap *bmap = NULL;
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003125 AioContext *aio_context;
Max Reitze6641712015-08-26 19:47:48 +02003126 QDict *options = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003127 Error *local_err = NULL;
3128 int flags;
3129 int64_t size;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003130
Pavel Butsykin81206a82016-07-22 11:17:50 +03003131 if (!backup->has_speed) {
3132 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003133 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003134 if (!backup->has_on_source_error) {
3135 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003136 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003137 if (!backup->has_on_target_error) {
3138 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003139 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003140 if (!backup->has_mode) {
3141 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3142 }
3143 if (!backup->has_job_id) {
3144 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003145 }
Pavel Butsykin13b94142016-07-22 11:17:52 +03003146 if (!backup->has_compress) {
3147 backup->compress = false;
3148 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003149
Pavel Butsykin81206a82016-07-22 11:17:50 +03003150 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003151 if (!bs) {
John Snow111049a2016-11-08 01:50:38 -05003152 return NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003153 }
3154
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003155 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003156 aio_context_acquire(aio_context);
3157
Pavel Butsykin81206a82016-07-22 11:17:50 +03003158 if (!backup->has_format) {
3159 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
3160 NULL : (char*) bs->drv->format_name;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003161 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003162
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003163 /* Early check to avoid creating target */
Fam Zheng3718d8a2014-05-23 21:29:43 +08003164 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003165 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003166 }
3167
Kevin Wolf61de4c62016-03-18 17:46:45 +01003168 flags = bs->open_flags | BDRV_O_RDWR;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003169
Ian Mainfc5d3f82013-07-26 11:39:04 -07003170 /* See if we have a backing HD we can use to create our new image
3171 * on top of. */
Pavel Butsykin81206a82016-07-22 11:17:50 +03003172 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003173 source = backing_bs(bs);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003174 if (!source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003175 backup->sync = MIRROR_SYNC_MODE_FULL;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003176 }
3177 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003178 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
Ian Mainfc5d3f82013-07-26 11:39:04 -07003179 source = bs;
3180 }
3181
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003182 size = bdrv_getlength(bs);
3183 if (size < 0) {
3184 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003185 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003186 }
3187
Pavel Butsykin81206a82016-07-22 11:17:50 +03003188 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
3189 assert(backup->format);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003190 if (source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003191 bdrv_img_create(backup->target, backup->format, source->filename,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003192 source->drv->format_name, NULL,
3193 size, flags, &local_err, false);
3194 } else {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003195 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003196 size, flags, &local_err, false);
3197 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003198 }
3199
Markus Armbruster84d18f02014-01-30 15:07:28 +01003200 if (local_err) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003201 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003202 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003203 }
3204
Pavel Butsykin81206a82016-07-22 11:17:50 +03003205 if (backup->format) {
Max Reitze6641712015-08-26 19:47:48 +02003206 options = qdict_new();
Pavel Butsykin81206a82016-07-22 11:17:50 +03003207 qdict_put(options, "driver", qstring_from_str(backup->format));
Max Reitze6641712015-08-26 19:47:48 +02003208 }
3209
Pavel Butsykin81206a82016-07-22 11:17:50 +03003210 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003211 if (!target_bs) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003212 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003213 }
3214
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003215 bdrv_set_aio_context(target_bs, aio_context);
3216
Pavel Butsykin81206a82016-07-22 11:17:50 +03003217 if (backup->has_bitmap) {
3218 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
John Snowd58d8452015-04-17 19:49:58 -04003219 if (!bmap) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003220 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
Max Reitz0702d3d2015-11-09 23:39:10 +01003221 bdrv_unref(target_bs);
John Snowd58d8452015-04-17 19:49:58 -04003222 goto out;
3223 }
3224 }
3225
John Snow111049a2016-11-08 01:50:38 -05003226 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
3227 backup->sync, bmap, backup->compress,
3228 backup->on_source_error, backup->on_target_error,
3229 BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err);
Kevin Wolf5c438bc2016-04-14 13:09:53 +02003230 bdrv_unref(target_bs);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003231 if (local_err != NULL) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003232 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003233 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003234 }
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003235
3236out:
3237 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05003238 return job;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003239}
3240
Pavel Butsykin81206a82016-07-22 11:17:50 +03003241void qmp_drive_backup(DriveBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003242{
John Snow111049a2016-11-08 01:50:38 -05003243
3244 BlockJob *job;
3245 job = do_drive_backup(arg, NULL, errp);
3246 if (job) {
3247 block_job_start(job);
3248 }
John Snow78f51fd2015-11-05 18:13:17 -05003249}
3250
Benoît Canetc13163f2014-01-23 21:31:34 +01003251BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3252{
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003253 return bdrv_named_nodes_list(errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003254}
3255
John Snow111049a2016-11-08 01:50:38 -05003256BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
3257 Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003258{
3259 BlockDriverState *bs;
3260 BlockDriverState *target_bs;
3261 Error *local_err = NULL;
3262 AioContext *aio_context;
John Snow111049a2016-11-08 01:50:38 -05003263 BlockJob *job = NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003264
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003265 if (!backup->has_speed) {
3266 backup->speed = 0;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003267 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003268 if (!backup->has_on_source_error) {
3269 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003270 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003271 if (!backup->has_on_target_error) {
3272 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3273 }
3274 if (!backup->has_job_id) {
3275 backup->job_id = NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003276 }
Pavel Butsykin3b7b1232016-07-22 11:17:53 +03003277 if (!backup->has_compress) {
3278 backup->compress = false;
3279 }
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003280
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003281 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003282 if (!bs) {
John Snow111049a2016-11-08 01:50:38 -05003283 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003284 }
3285
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003286 aio_context = bdrv_get_aio_context(bs);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003287 aio_context_acquire(aio_context);
3288
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003289 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
Fam Zheng0d978912016-05-23 10:19:35 +08003290 if (!target_bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003291 goto out;
3292 }
Max Reitz5433c242015-10-19 17:53:29 +02003293
Fam Zhengefd75562016-05-23 10:19:36 +08003294 if (bdrv_get_aio_context(target_bs) != aio_context) {
3295 if (!bdrv_has_blk(target_bs)) {
3296 /* The target BDS is not attached, we can safely move it to another
3297 * AioContext. */
3298 bdrv_set_aio_context(target_bs, aio_context);
3299 } else {
3300 error_setg(errp, "Target is attached to a different thread from "
3301 "source.");
3302 goto out;
3303 }
3304 }
John Snow111049a2016-11-08 01:50:38 -05003305 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
3306 backup->sync, NULL, backup->compress,
3307 backup->on_source_error, backup->on_target_error,
3308 BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003309 if (local_err != NULL) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003310 error_propagate(errp, local_err);
3311 }
3312out:
3313 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05003314 return job;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003315}
3316
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003317void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003318{
John Snow111049a2016-11-08 01:50:38 -05003319 BlockJob *job;
3320 job = do_blockdev_backup(arg, NULL, errp);
3321 if (job) {
3322 block_job_start(job);
3323 }
John Snow78f51fd2015-11-05 18:13:17 -05003324}
3325
Fam Zheng4193cdd2015-12-24 12:45:03 +08003326/* Parameter check and block job starting for drive mirroring.
3327 * Caller should hold @device and @target's aio context (must be the same).
3328 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003329static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003330 BlockDriverState *target,
3331 bool has_replaces, const char *replaces,
3332 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003333 BlockMirrorBackingMode backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003334 bool has_speed, int64_t speed,
3335 bool has_granularity, uint32_t granularity,
3336 bool has_buf_size, int64_t buf_size,
3337 bool has_on_source_error,
3338 BlockdevOnError on_source_error,
3339 bool has_on_target_error,
3340 BlockdevOnError on_target_error,
3341 bool has_unmap, bool unmap,
3342 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003343{
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003344
3345 if (!has_speed) {
3346 speed = 0;
3347 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003348 if (!has_on_source_error) {
3349 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3350 }
3351 if (!has_on_target_error) {
3352 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3353 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003354 if (!has_granularity) {
3355 granularity = 0;
3356 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003357 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003358 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003359 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003360 if (!has_unmap) {
3361 unmap = true;
3362 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003363
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003364 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003365 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3366 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003367 return;
3368 }
3369 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003370 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3371 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003372 return;
3373 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003374
Fam Zheng4193cdd2015-12-24 12:45:03 +08003375 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3376 return;
3377 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003378 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3379 return;
3380 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003381
3382 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3383 sync = MIRROR_SYNC_MODE_FULL;
3384 }
3385
3386 /* pass the node name to replace to mirror start since it's loose coupling
3387 * and will allow to check whether the node still exist at mirror completion
3388 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003389 mirror_start(job_id, bs, target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003390 has_replaces ? replaces : NULL,
Max Reitz274fcce2016-06-10 20:57:47 +02003391 speed, granularity, buf_size, sync, backing_mode,
John Snow8254b6d2016-10-27 12:06:58 -04003392 on_source_error, on_target_error, unmap, errp);
Fam Zheng4193cdd2015-12-24 12:45:03 +08003393}
3394
Eric Blakefaecd402016-07-14 16:37:58 -06003395void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003396{
3397 BlockDriverState *bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003398 BlockDriverState *source, *target_bs;
3399 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003400 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003401 Error *local_err = NULL;
3402 QDict *options = NULL;
3403 int flags;
3404 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003405 const char *format = arg->format;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003406
Kevin Wolf0524e932016-06-23 14:20:24 +02003407 bs = qmp_get_root_bs(arg->device, errp);
3408 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003409 return;
3410 }
3411
Kevin Wolf0524e932016-06-23 14:20:24 +02003412 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003413 aio_context_acquire(aio_context);
3414
Eric Blakefaecd402016-07-14 16:37:58 -06003415 if (!arg->has_mode) {
3416 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003417 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003418
Eric Blakefaecd402016-07-14 16:37:58 -06003419 if (!arg->has_format) {
3420 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3421 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003422 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003423
Kevin Wolf61de4c62016-03-18 17:46:45 +01003424 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003425 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003426 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3427 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003428 }
Eric Blakefaecd402016-07-14 16:37:58 -06003429 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003430 source = bs;
3431 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003432
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003433 size = bdrv_getlength(bs);
3434 if (size < 0) {
3435 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003436 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003437 }
3438
Eric Blakefaecd402016-07-14 16:37:58 -06003439 if (arg->has_replaces) {
Benoît Canet09158f02014-06-27 18:25:25 +02003440 BlockDriverState *to_replace_bs;
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003441 AioContext *replace_aio_context;
3442 int64_t replace_size;
Benoît Canet09158f02014-06-27 18:25:25 +02003443
Eric Blakefaecd402016-07-14 16:37:58 -06003444 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003445 error_setg(errp, "a node-name must be provided when replacing a"
3446 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003447 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003448 }
3449
Eric Blakefaecd402016-07-14 16:37:58 -06003450 to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err);
Benoît Canet09158f02014-06-27 18:25:25 +02003451
3452 if (!to_replace_bs) {
3453 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003454 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003455 }
3456
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003457 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3458 aio_context_acquire(replace_aio_context);
3459 replace_size = bdrv_getlength(to_replace_bs);
3460 aio_context_release(replace_aio_context);
3461
3462 if (size != replace_size) {
Benoît Canet09158f02014-06-27 18:25:25 +02003463 error_setg(errp, "cannot replace image with a mirror image of "
3464 "different size");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003465 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003466 }
3467 }
3468
Eric Blakefaecd402016-07-14 16:37:58 -06003469 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003470 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3471 } else {
3472 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3473 }
3474
Eric Blakefaecd402016-07-14 16:37:58 -06003475 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3476 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003477 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003478 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003479 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003480 bdrv_img_create(arg->target, format,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003481 NULL, NULL, NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003482 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003483 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003484 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003485 break;
3486 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3487 /* create new image with backing file */
Eric Blakefaecd402016-07-14 16:37:58 -06003488 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003489 source->filename,
3490 source->drv->format_name,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003491 NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003492 break;
3493 default:
3494 abort();
3495 }
3496 }
3497
Markus Armbruster84d18f02014-01-30 15:07:28 +01003498 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003499 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003500 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003501 }
3502
Max Reitze6641712015-08-26 19:47:48 +02003503 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003504 if (arg->has_node_name) {
3505 qdict_put(options, "node-name", qstring_from_str(arg->node_name));
Benoît Canet4c828dc2014-06-16 12:00:55 +02003506 }
Max Reitze6641712015-08-26 19:47:48 +02003507 if (format) {
3508 qdict_put(options, "driver", qstring_from_str(format));
3509 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003510
Paolo Bonzinib812f672013-01-21 17:09:43 +01003511 /* Mirroring takes care of copy-on-write using the source's backing
3512 * file.
3513 */
Eric Blakefaecd402016-07-14 16:37:58 -06003514 target_bs = bdrv_open(arg->target, NULL, options,
3515 flags | BDRV_O_NO_BACKING, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003516 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003517 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003518 }
3519
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003520 bdrv_set_aio_context(target_bs, aio_context);
3521
Eric Blakefaecd402016-07-14 16:37:58 -06003522 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3523 arg->has_replaces, arg->replaces, arg->sync,
3524 backing_mode, arg->has_speed, arg->speed,
3525 arg->has_granularity, arg->granularity,
3526 arg->has_buf_size, arg->buf_size,
3527 arg->has_on_source_error, arg->on_source_error,
3528 arg->has_on_target_error, arg->on_target_error,
3529 arg->has_unmap, arg->unmap,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003530 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02003531 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003532 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003533out:
3534 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003535}
3536
Alberto Garcia71aa9862016-07-05 17:28:57 +03003537void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3538 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08003539 bool has_replaces, const char *replaces,
3540 MirrorSyncMode sync,
3541 bool has_speed, int64_t speed,
3542 bool has_granularity, uint32_t granularity,
3543 bool has_buf_size, int64_t buf_size,
3544 bool has_on_source_error,
3545 BlockdevOnError on_source_error,
3546 bool has_on_target_error,
3547 BlockdevOnError on_target_error,
3548 Error **errp)
3549{
3550 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08003551 BlockDriverState *target_bs;
3552 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003553 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08003554 Error *local_err = NULL;
3555
Kevin Wolf07eec652016-06-23 14:20:24 +02003556 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08003557 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08003558 return;
3559 }
3560
3561 target_bs = bdrv_lookup_bs(target, target, errp);
3562 if (!target_bs) {
3563 return;
3564 }
3565
3566 aio_context = bdrv_get_aio_context(bs);
3567 aio_context_acquire(aio_context);
3568
Fam Zhengdf925622015-12-24 12:45:05 +08003569 bdrv_set_aio_context(target_bs, aio_context);
3570
Alberto Garcia71aa9862016-07-05 17:28:57 +03003571 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02003572 has_replaces, replaces, sync, backing_mode,
Fam Zhengdf925622015-12-24 12:45:05 +08003573 has_speed, speed,
3574 has_granularity, granularity,
3575 has_buf_size, buf_size,
3576 has_on_source_error, on_source_error,
3577 has_on_target_error, on_target_error,
3578 true, true,
3579 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003580 error_propagate(errp, local_err);
Fam Zhengdf925622015-12-24 12:45:05 +08003581
3582 aio_context_release(aio_context);
3583}
3584
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003585/* Get a block job using its ID and acquire its AioContext */
3586static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003587 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003588{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003589 BlockJob *job;
3590
3591 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003592
Max Reitz5433c242015-10-19 17:53:29 +02003593 *aio_context = NULL;
3594
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003595 job = block_job_get(id);
3596
3597 if (!job) {
3598 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3599 "Block job '%s' not found", id);
3600 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003601 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003602
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003603 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003604 aio_context_acquire(*aio_context);
3605
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003606 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003607}
3608
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003609void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003610{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003611 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003612 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003613
3614 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003615 return;
3616 }
3617
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003618 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003619 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003620}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003621
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003622void qmp_block_job_cancel(const char *device,
3623 bool has_force, bool force, Error **errp)
3624{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003625 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003626 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003627
3628 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003629 return;
3630 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003631
3632 if (!has_force) {
3633 force = false;
3634 }
3635
John Snow0df4ba52016-10-27 12:06:59 -04003636 if (block_job_user_paused(job) && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003637 error_setg(errp, "The block job for device '%s' is currently paused",
3638 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003639 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003640 }
3641
3642 trace_qmp_block_job_cancel(job);
3643 block_job_cancel(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003644out:
3645 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003646}
3647
3648void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003649{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003650 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003651 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003652
John Snow0df4ba52016-10-27 12:06:59 -04003653 if (!job || block_job_user_paused(job)) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003654 return;
3655 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003656
3657 trace_qmp_block_job_pause(job);
John Snow0df4ba52016-10-27 12:06:59 -04003658 block_job_user_pause(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003659 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003660}
3661
3662void qmp_block_job_resume(const char *device, Error **errp)
3663{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003664 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003665 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003666
John Snow0df4ba52016-10-27 12:06:59 -04003667 if (!job || !block_job_user_paused(job)) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02003668 return;
3669 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003670
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003671 trace_qmp_block_job_resume(job);
Stefan Hajnoczi17bd51f2016-06-16 17:56:22 +01003672 block_job_iostatus_reset(job);
John Snow0df4ba52016-10-27 12:06:59 -04003673 block_job_user_resume(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003674 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003675}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003676
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003677void qmp_block_job_complete(const char *device, Error **errp)
3678{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003679 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003680 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003681
3682 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003683 return;
3684 }
3685
3686 trace_qmp_block_job_complete(job);
3687 block_job_complete(job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003688 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003689}
3690
Jeff Codyfa40e652014-07-01 09:52:16 +02003691void qmp_change_backing_file(const char *device,
3692 const char *image_node_name,
3693 const char *backing_file,
3694 Error **errp)
3695{
3696 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003697 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02003698 BlockDriverState *image_bs = NULL;
3699 Error *local_err = NULL;
3700 bool ro;
3701 int open_flags;
3702 int ret;
3703
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003704 bs = qmp_get_root_bs(device, errp);
3705 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02003706 return;
3707 }
3708
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003709 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003710 aio_context_acquire(aio_context);
3711
Jeff Codyfa40e652014-07-01 09:52:16 +02003712 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3713 if (local_err) {
3714 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003715 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003716 }
3717
3718 if (!image_bs) {
3719 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003720 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003721 }
3722
3723 if (bdrv_find_base(image_bs) == image_bs) {
3724 error_setg(errp, "not allowing backing file change on an image "
3725 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003726 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003727 }
3728
3729 /* even though we are not necessarily operating on bs, we need it to
3730 * determine if block ops are currently prohibited on the chain */
3731 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003732 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003733 }
3734
3735 /* final sanity check */
3736 if (!bdrv_chain_contains(bs, image_bs)) {
3737 error_setg(errp, "'%s' and image file are not in the same chain",
3738 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003739 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003740 }
3741
3742 /* if not r/w, reopen to make r/w */
3743 open_flags = image_bs->open_flags;
3744 ro = bdrv_is_read_only(image_bs);
3745
3746 if (ro) {
3747 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3748 if (local_err) {
3749 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003750 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003751 }
3752 }
3753
3754 ret = bdrv_change_backing_file(image_bs, backing_file,
3755 image_bs->drv ? image_bs->drv->format_name : "");
3756
3757 if (ret < 0) {
3758 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3759 backing_file);
3760 /* don't exit here, so we can try to restore open flags if
3761 * appropriate */
3762 }
3763
3764 if (ro) {
3765 bdrv_reopen(image_bs, open_flags, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003766 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02003767 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003768
3769out:
3770 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02003771}
3772
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003773void hmp_drive_add_node(Monitor *mon, const char *optstr)
3774{
3775 QemuOpts *opts;
3776 QDict *qdict;
3777 Error *local_err = NULL;
3778
3779 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
3780 if (!opts) {
3781 return;
3782 }
3783
3784 qdict = qemu_opts_to_qdict(opts, NULL);
3785
3786 if (!qdict_get_try_str(qdict, "node-name")) {
Kevin Wolff8746fb2016-03-16 11:14:31 +01003787 QDECREF(qdict);
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003788 error_report("'node-name' needs to be specified");
3789 goto out;
3790 }
3791
3792 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
3793 if (!bs) {
3794 error_report_err(local_err);
3795 goto out;
3796 }
3797
3798 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3799
3800out:
3801 qemu_opts_del(opts);
3802}
3803
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003804void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3805{
Max Reitzbe4b67b2015-10-19 17:53:09 +02003806 BlockDriverState *bs;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003807 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01003808 Visitor *v = qobject_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003809 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003810 Error *local_err = NULL;
3811
Eric Blake3b098d52016-06-09 10:48:43 -06003812 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003813 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003814 error_propagate(errp, local_err);
3815 goto fail;
3816 }
3817
Eric Blake3b098d52016-06-09 10:48:43 -06003818 visit_complete(v, &obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003819 qdict = qobject_to_qdict(obj);
3820
3821 qdict_flatten(qdict);
3822
Kevin Wolf9ec88732016-09-21 14:56:11 +02003823 if (!qdict_get_try_str(qdict, "node-name")) {
3824 error_setg(errp, "'node-name' must be specified for the root node");
3825 goto fail;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003826 }
3827
Kevin Wolf9ec88732016-09-21 14:56:11 +02003828 bs = bds_tree_init(qdict, errp);
3829 if (!bs) {
3830 goto fail;
3831 }
3832
3833 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3834
Max Reitzbe4b67b2015-10-19 17:53:09 +02003835 if (bs && bdrv_key_required(bs)) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02003836 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3837 bdrv_unref(bs);
Kevin Wolf8ae8e902014-03-06 15:43:42 +01003838 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3839 goto fail;
3840 }
3841
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003842fail:
Eric Blake3b098d52016-06-09 10:48:43 -06003843 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003844}
3845
Kevin Wolf9ec88732016-09-21 14:56:11 +02003846void qmp_x_blockdev_del(const char *node_name, Error **errp)
Alberto Garcia81b936a2015-11-02 16:51:55 +02003847{
3848 AioContext *aio_context;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003849 BlockDriverState *bs;
3850
Kevin Wolf9ec88732016-09-21 14:56:11 +02003851 bs = bdrv_find_node(node_name);
3852 if (!bs) {
3853 error_setg(errp, "Cannot find node %s", node_name);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003854 return;
3855 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003856 if (bdrv_has_blk(bs)) {
3857 error_setg(errp, "Node %s is in use", node_name);
3858 return;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003859 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003860 aio_context = bdrv_get_aio_context(bs);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003861 aio_context_acquire(aio_context);
3862
Kevin Wolf9ec88732016-09-21 14:56:11 +02003863 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3864 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003865 }
3866
Kevin Wolf9ec88732016-09-21 14:56:11 +02003867 if (!bs->monitor_list.tqe_prev) {
3868 error_setg(errp, "Node %s is not owned by the monitor",
3869 bs->node_name);
3870 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003871 }
3872
Kevin Wolf9ec88732016-09-21 14:56:11 +02003873 if (bs->refcnt > 1) {
3874 error_setg(errp, "Block device %s is in use",
3875 bdrv_get_device_or_node_name(bs));
3876 goto out;
3877 }
3878
3879 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3880 bdrv_unref(bs);
3881
Alberto Garcia81b936a2015-11-02 16:51:55 +02003882out:
3883 aio_context_release(aio_context);
3884}
3885
Wen Congyang7f821592016-05-10 15:36:39 +08003886static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
3887 const char *child_name)
3888{
3889 BdrvChild *child;
3890
3891 QLIST_FOREACH(child, &parent_bs->children, next) {
3892 if (strcmp(child->name, child_name) == 0) {
3893 return child;
3894 }
3895 }
3896
3897 return NULL;
3898}
3899
3900void qmp_x_blockdev_change(const char *parent, bool has_child,
3901 const char *child, bool has_node,
3902 const char *node, Error **errp)
3903{
3904 BlockDriverState *parent_bs, *new_bs = NULL;
3905 BdrvChild *p_child;
3906
3907 parent_bs = bdrv_lookup_bs(parent, parent, errp);
3908 if (!parent_bs) {
3909 return;
3910 }
3911
3912 if (has_child == has_node) {
3913 if (has_child) {
3914 error_setg(errp, "The parameters child and node are in conflict");
3915 } else {
3916 error_setg(errp, "Either child or node must be specified");
3917 }
3918 return;
3919 }
3920
3921 if (has_child) {
3922 p_child = bdrv_find_child(parent_bs, child);
3923 if (!p_child) {
3924 error_setg(errp, "Node '%s' does not have child '%s'",
3925 parent, child);
3926 return;
3927 }
3928 bdrv_del_child(parent_bs, p_child, errp);
3929 }
3930
3931 if (has_node) {
3932 new_bs = bdrv_find_node(node);
3933 if (!new_bs) {
3934 error_setg(errp, "Node '%s' not found", node);
3935 return;
3936 }
3937 bdrv_add_child(parent_bs, new_bs, errp);
3938 }
3939}
3940
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003941BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3942{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003943 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003944 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003945
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003946 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
John Snow559b9352016-10-27 12:06:55 -04003947 BlockJobInfoList *elem;
3948 AioContext *aio_context;
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003949
John Snow559b9352016-10-27 12:06:55 -04003950 if (block_job_is_internal(job)) {
3951 continue;
3952 }
3953 elem = g_new0(BlockJobInfoList, 1);
3954 aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003955 aio_context_acquire(aio_context);
John Snow559b9352016-10-27 12:06:55 -04003956 elem->value = block_job_query(job, errp);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003957 aio_context_release(aio_context);
John Snow559b9352016-10-27 12:06:55 -04003958 if (!elem->value) {
3959 g_free(elem);
3960 qapi_free_BlockJobInfoList(head);
3961 return NULL;
3962 }
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003963 *p_next = elem;
3964 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003965 }
3966
3967 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003968}
Paolo Bonzini4d454572012-11-26 16:03:42 +01003969
Kevin Wolf00063832013-03-15 10:35:07 +01003970QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01003971 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01003972 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01003973 .desc = {
3974 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01003975 .name = "snapshot",
3976 .type = QEMU_OPT_BOOL,
3977 .help = "enable/disable snapshot mode",
3978 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003979 .name = "aio",
3980 .type = QEMU_OPT_STRING,
3981 .help = "host AIO implementation (threads, native)",
3982 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01003983 .name = BDRV_OPT_CACHE_WB,
3984 .type = QEMU_OPT_BOOL,
3985 .help = "Enable writeback mode",
3986 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003987 .name = "format",
3988 .type = QEMU_OPT_STRING,
3989 .help = "disk format (raw, qcow2, ...)",
3990 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003991 .name = "rerror",
3992 .type = QEMU_OPT_STRING,
3993 .help = "read error action",
3994 },{
3995 .name = "werror",
3996 .type = QEMU_OPT_STRING,
3997 .help = "write error action",
3998 },{
Alberto Garcia4e200cf2016-09-15 17:53:05 +03003999 .name = BDRV_OPT_READ_ONLY,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004000 .type = QEMU_OPT_BOOL,
4001 .help = "open drive file as read-only",
4002 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004003 .name = "throttling.iops-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004004 .type = QEMU_OPT_NUMBER,
4005 .help = "limit total I/O operations per second",
4006 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004007 .name = "throttling.iops-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004008 .type = QEMU_OPT_NUMBER,
4009 .help = "limit read operations per second",
4010 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004011 .name = "throttling.iops-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004012 .type = QEMU_OPT_NUMBER,
4013 .help = "limit write operations per second",
4014 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004015 .name = "throttling.bps-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004016 .type = QEMU_OPT_NUMBER,
4017 .help = "limit total bytes per second",
4018 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004019 .name = "throttling.bps-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004020 .type = QEMU_OPT_NUMBER,
4021 .help = "limit read bytes per second",
4022 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004023 .name = "throttling.bps-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004024 .type = QEMU_OPT_NUMBER,
4025 .help = "limit write bytes per second",
4026 },{
Benoît Canet3e9fab62013-09-02 14:14:40 +02004027 .name = "throttling.iops-total-max",
4028 .type = QEMU_OPT_NUMBER,
4029 .help = "I/O operations burst",
4030 },{
4031 .name = "throttling.iops-read-max",
4032 .type = QEMU_OPT_NUMBER,
4033 .help = "I/O operations read burst",
4034 },{
4035 .name = "throttling.iops-write-max",
4036 .type = QEMU_OPT_NUMBER,
4037 .help = "I/O operations write burst",
4038 },{
4039 .name = "throttling.bps-total-max",
4040 .type = QEMU_OPT_NUMBER,
4041 .help = "total bytes burst",
4042 },{
4043 .name = "throttling.bps-read-max",
4044 .type = QEMU_OPT_NUMBER,
4045 .help = "total bytes read burst",
4046 },{
4047 .name = "throttling.bps-write-max",
4048 .type = QEMU_OPT_NUMBER,
4049 .help = "total bytes write burst",
4050 },{
Alberto Garcia8a0fc182016-02-18 12:27:02 +02004051 .name = "throttling.iops-total-max-length",
4052 .type = QEMU_OPT_NUMBER,
4053 .help = "length of the iops-total-max burst period, in seconds",
4054 },{
4055 .name = "throttling.iops-read-max-length",
4056 .type = QEMU_OPT_NUMBER,
4057 .help = "length of the iops-read-max burst period, in seconds",
4058 },{
4059 .name = "throttling.iops-write-max-length",
4060 .type = QEMU_OPT_NUMBER,
4061 .help = "length of the iops-write-max burst period, in seconds",
4062 },{
4063 .name = "throttling.bps-total-max-length",
4064 .type = QEMU_OPT_NUMBER,
4065 .help = "length of the bps-total-max burst period, in seconds",
4066 },{
4067 .name = "throttling.bps-read-max-length",
4068 .type = QEMU_OPT_NUMBER,
4069 .help = "length of the bps-read-max burst period, in seconds",
4070 },{
4071 .name = "throttling.bps-write-max-length",
4072 .type = QEMU_OPT_NUMBER,
4073 .help = "length of the bps-write-max burst period, in seconds",
4074 },{
Benoît Canet2024c1d2013-09-02 14:14:41 +02004075 .name = "throttling.iops-size",
4076 .type = QEMU_OPT_NUMBER,
4077 .help = "when limiting by iops max size of an I/O in bytes",
4078 },{
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004079 .name = "throttling.group",
4080 .type = QEMU_OPT_STRING,
4081 .help = "name of the block throttling group",
4082 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004083 .name = "copy-on-read",
4084 .type = QEMU_OPT_BOOL,
4085 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004086 },{
4087 .name = "detect-zeroes",
4088 .type = QEMU_OPT_STRING,
4089 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004090 },{
4091 .name = "stats-account-invalid",
4092 .type = QEMU_OPT_BOOL,
4093 .help = "whether to account for invalid I/O operations "
4094 "in the statistics",
4095 },{
4096 .name = "stats-account-failed",
4097 .type = QEMU_OPT_BOOL,
4098 .help = "whether to account for failed I/O operations "
4099 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004100 },
4101 { /* end of list */ }
4102 },
4103};
Kevin Wolf00063832013-03-15 10:35:07 +01004104
4105QemuOptsList qemu_drive_opts = {
4106 .name = "drive",
4107 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4108 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004109 /*
4110 * no elements => accept any params
4111 * validation will happen later
4112 */
Kevin Wolf00063832013-03-15 10:35:07 +01004113 { /* end of list */ }
4114 },
4115};