blob: 3d92724d0f50924b73e19a6b854af8196d170e09 [file] [log] [blame]
Markus Armbruster666daa62010-06-02 18:48:27 +02001/*
2 * QEMU host block devices
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
Markus Armbruster3618a092013-03-14 13:59:53 +01008 *
9 * This file incorporates work covered by the following copyright and
10 * permission notice:
11 *
12 * Copyright (c) 2003-2008 Fabrice Bellard
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * THE SOFTWARE.
Markus Armbruster666daa62010-06-02 18:48:27 +020031 */
32
Peter Maydelld38ea872016-01-29 17:50:05 +000033#include "qemu/osdep.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020034#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/blockdev.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010036#include "hw/block/block.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010037#include "block/blockjob.h"
Alberto Garcia76f4afb2015-06-08 18:17:44 +020038#include "block/throttle-groups.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010040#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/option.h"
42#include "qemu/config-file.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010043#include "qapi/qmp/types.h"
Kevin Wolfd26c9a12013-09-23 15:26:03 +020044#include "qapi-visit.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010045#include "qapi/qmp/qerror.h"
Kevin Wolfd26c9a12013-09-23 15:26:03 +020046#include "qapi/qmp-output-visitor.h"
Peter Lieven9e7dac72014-08-13 19:20:17 +020047#include "qapi/util.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010048#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010049#include "block/block_int.h"
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -020050#include "qmp-commands.h"
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +000051#include "trace.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010052#include "sysemu/arch_init.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020053#include "qemu/cutils.h"
54#include "qemu/help_option.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020055
Max Reitz9c4218e2016-01-29 16:36:12 +010056static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
57 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
58
Kevin Wolfb33945c2016-09-20 13:38:43 +020059static int do_open_tray(const char *blk_name, const char *qdev_id,
60 bool force, Error **errp);
Colin Lordbf18bee2016-06-06 14:15:22 -040061
Markus Armbruster19609662011-01-28 11:21:39 +010062static const char *const if_name[IF_COUNT] = {
63 [IF_NONE] = "none",
64 [IF_IDE] = "ide",
65 [IF_SCSI] = "scsi",
66 [IF_FLOPPY] = "floppy",
67 [IF_PFLASH] = "pflash",
68 [IF_MTD] = "mtd",
69 [IF_SD] = "sd",
70 [IF_VIRTIO] = "virtio",
71 [IF_XEN] = "xen",
72};
73
John Snow21dff8c2014-10-01 14:19:25 -040074static int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010075 /*
76 * Do not change these numbers! They govern how drive option
77 * index maps to unit and bus. That mapping is ABI.
78 *
Wei Jiangang547cb152016-04-26 18:12:43 +080079 * All controllers used to implement if=T drives need to support
Markus Armbruster27d6bf42011-01-28 11:21:40 +010080 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
81 * Otherwise, some index values map to "impossible" bus, unit
82 * values.
83 *
84 * For instance, if you change [IF_SCSI] to 255, -drive
85 * if=scsi,index=12 no longer means bus=1,unit=5, but
86 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
87 * the drive can't be set up. Regression.
88 */
89 [IF_IDE] = 2,
90 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +010091};
92
John Snow21dff8c2014-10-01 14:19:25 -040093/**
94 * Boards may call this to offer board-by-board overrides
95 * of the default, global values.
96 */
97void override_max_devs(BlockInterfaceType type, int max_devs)
98{
Markus Armbruster18e46a02014-10-07 13:59:06 +020099 BlockBackend *blk;
John Snow21dff8c2014-10-01 14:19:25 -0400100 DriveInfo *dinfo;
101
102 if (max_devs <= 0) {
103 return;
104 }
105
Markus Armbruster18e46a02014-10-07 13:59:06 +0200106 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
107 dinfo = blk_legacy_dinfo(blk);
John Snow21dff8c2014-10-01 14:19:25 -0400108 if (dinfo->type == type) {
109 fprintf(stderr, "Cannot override units-per-bus property of"
110 " the %s interface, because a drive of that type has"
111 " already been added.\n", if_name[type]);
112 g_assert_not_reached();
113 }
114 }
115
116 if_max_devs[type] = max_devs;
117}
118
Markus Armbruster14bafc52010-06-25 08:09:10 +0200119/*
120 * We automatically delete the drive when a device using it gets
121 * unplugged. Questionable feature, but we can't just drop it.
122 * Device models call blockdev_mark_auto_del() to schedule the
123 * automatic deletion, and generic qdev code calls blockdev_auto_del()
124 * when deletion is actually safe.
125 */
Markus Armbruster4be74632014-10-07 13:59:18 +0200126void blockdev_mark_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200127{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200128 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster4be74632014-10-07 13:59:18 +0200129 BlockDriverState *bs = blk_bs(blk);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100130 AioContext *aio_context;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200131
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200132 if (!dinfo) {
Kevin Wolf2d246f02013-09-18 15:14:47 +0200133 return;
134 }
135
Max Reitz5433c242015-10-19 17:53:29 +0200136 if (bs) {
137 aio_context = bdrv_get_aio_context(bs);
138 aio_context_acquire(aio_context);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100139
Max Reitz5433c242015-10-19 17:53:29 +0200140 if (bs->job) {
141 block_job_cancel(bs->job);
142 }
143
144 aio_context_release(aio_context);
Paolo Bonzini12bde0e2012-03-30 13:17:10 +0200145 }
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100146
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200147 dinfo->auto_del = 1;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200148}
149
Markus Armbruster4be74632014-10-07 13:59:18 +0200150void blockdev_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200151{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200152 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200153
Ryan Harper0fc0f1f2010-12-08 10:05:00 -0600154 if (dinfo && dinfo->auto_del) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100155 monitor_remove_blk(blk);
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +0200156 blk_unref(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200157 }
158}
159
John Snowd8f94e12014-10-01 14:19:27 -0400160/**
161 * Returns the current mapping of how many units per bus
162 * a particular interface can support.
163 *
164 * A positive integer indicates n units per bus.
165 * 0 implies the mapping has not been established.
166 * -1 indicates an invalid BlockInterfaceType was given.
167 */
168int drive_get_max_devs(BlockInterfaceType type)
169{
170 if (type >= IF_IDE && type < IF_COUNT) {
171 return if_max_devs[type];
172 }
173
174 return -1;
175}
176
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100177static int drive_index_to_bus_id(BlockInterfaceType type, int index)
178{
179 int max_devs = if_max_devs[type];
180 return max_devs ? index / max_devs : 0;
181}
182
183static int drive_index_to_unit_id(BlockInterfaceType type, int index)
184{
185 int max_devs = if_max_devs[type];
186 return max_devs ? index % max_devs : index;
187}
188
Markus Armbruster2292dda2011-01-28 11:21:41 +0100189QemuOpts *drive_def(const char *optstr)
190{
Markus Armbruster70b94332015-02-13 12:50:26 +0100191 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100192}
193
194QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +0100195 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +0200196{
Markus Armbruster666daa62010-06-02 18:48:27 +0200197 QemuOpts *opts;
198
Markus Armbruster2292dda2011-01-28 11:21:41 +0100199 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200200 if (!opts) {
201 return NULL;
202 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100203 if (type != IF_DEFAULT) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100204 qemu_opt_set(opts, "if", if_name[type], &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100205 }
206 if (index >= 0) {
Markus Armbrustera8b18f82015-02-13 15:50:43 +0100207 qemu_opt_set_number(opts, "index", index, &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100208 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200209 if (file)
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100210 qemu_opt_set(opts, "file", file, &error_abort);
Markus Armbruster666daa62010-06-02 18:48:27 +0200211 return opts;
212}
213
214DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
215{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200216 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200217 DriveInfo *dinfo;
218
Markus Armbruster18e46a02014-10-07 13:59:06 +0200219 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
220 dinfo = blk_legacy_dinfo(blk);
221 if (dinfo && dinfo->type == type
222 && dinfo->bus == bus && dinfo->unit == unit) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200223 return dinfo;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200224 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200225 }
226
227 return NULL;
228}
229
John Snowa66c9dc2014-10-01 14:19:24 -0400230bool drive_check_orphaned(void)
231{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200232 BlockBackend *blk;
John Snowa66c9dc2014-10-01 14:19:24 -0400233 DriveInfo *dinfo;
234 bool rs = false;
235
Markus Armbruster18e46a02014-10-07 13:59:06 +0200236 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
237 dinfo = blk_legacy_dinfo(blk);
John Snowa66c9dc2014-10-01 14:19:24 -0400238 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
239 /* Unless this is a default drive, this may be an oversight. */
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200240 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
John Snowa66c9dc2014-10-01 14:19:24 -0400241 dinfo->type != IF_NONE) {
242 fprintf(stderr, "Warning: Orphaned drive without device: "
243 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
Max Reitz5433c242015-10-19 17:53:29 +0200244 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
245 if_name[dinfo->type], dinfo->bus, dinfo->unit);
John Snowa66c9dc2014-10-01 14:19:24 -0400246 rs = true;
247 }
248 }
249
250 return rs;
251}
252
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100253DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
254{
255 return drive_get(type,
256 drive_index_to_bus_id(type, index),
257 drive_index_to_unit_id(type, index));
258}
259
Markus Armbruster666daa62010-06-02 18:48:27 +0200260int drive_get_max_bus(BlockInterfaceType type)
261{
262 int max_bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200263 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200264 DriveInfo *dinfo;
265
266 max_bus = -1;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200267 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
268 dinfo = blk_legacy_dinfo(blk);
269 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200270 max_bus = dinfo->bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200271 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200272 }
273 return max_bus;
274}
275
Markus Armbruster13839972011-01-28 11:21:37 +0100276/* Get a block device. This should only be used for single-drive devices
277 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
278 appropriate bus. */
279DriveInfo *drive_get_next(BlockInterfaceType type)
280{
281 static int next_block_unit[IF_COUNT];
282
283 return drive_get(type, 0, next_block_unit[type]++);
284}
285
Markus Armbruster666daa62010-06-02 18:48:27 +0200286static void bdrv_format_print(void *opaque, const char *name)
287{
Markus Armbruster807105a2011-01-17 19:31:27 +0100288 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200289}
290
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000291typedef struct {
292 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800293 BlockDriverState *bs;
294} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000295
Kevin Wolfb6810722013-09-20 11:33:11 +0200296static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200297{
298 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200299 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200300 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200301 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200302 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200303 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200304 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200305 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200306 } else {
Kevin Wolfb6810722013-09-20 11:33:11 +0200307 error_setg(errp, "'%s' invalid %s error action",
308 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200309 return -1;
310 }
311}
312
Alberto Garcia40119ef2015-11-16 11:28:38 +0200313static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
314 Error **errp)
315{
316 const QListEntry *entry;
317 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
318 switch (qobject_type(entry->value)) {
319
320 case QTYPE_QSTRING: {
321 unsigned long long length;
322 const char *str = qstring_get_str(qobject_to_qstring(entry->value));
323 if (parse_uint_full(str, &length, 10) == 0 &&
324 length > 0 && length <= UINT_MAX) {
325 block_acct_add_interval(stats, (unsigned) length);
326 } else {
327 error_setg(errp, "Invalid interval length: %s", str);
328 return false;
329 }
330 break;
331 }
332
333 case QTYPE_QINT: {
334 int64_t length = qint_get_int(qobject_to_qint(entry->value));
335 if (length > 0 && length <= UINT_MAX) {
336 block_acct_add_interval(stats, (unsigned) length);
337 } else {
338 error_setg(errp, "Invalid interval length: %" PRId64, length);
339 return false;
340 }
341 break;
342 }
343
344 default:
345 error_setg(errp, "The specification of stats-intervals is invalid");
346 return false;
347 }
348 }
349 return true;
350}
351
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200352typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
353
Max Reitzfbf81752015-10-19 17:53:31 +0200354/* All parameters but @opts are optional and may be set to NULL. */
355static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
356 const char **throttling_group, ThrottleConfig *throttle_cfg,
357 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
358{
359 const char *discard;
360 Error *local_error = NULL;
361 const char *aio;
362
363 if (bdrv_flags) {
Max Reitzfbf81752015-10-19 17:53:31 +0200364 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
365 *bdrv_flags |= BDRV_O_COPY_ON_READ;
366 }
367
368 if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
369 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
370 error_setg(errp, "Invalid discard option");
371 return;
372 }
373 }
374
Max Reitzfbf81752015-10-19 17:53:31 +0200375 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
376 if (!strcmp(aio, "native")) {
377 *bdrv_flags |= BDRV_O_NATIVE_AIO;
378 } else if (!strcmp(aio, "threads")) {
379 /* this is the default */
380 } else {
381 error_setg(errp, "invalid aio option");
382 return;
383 }
384 }
385 }
386
387 /* disk I/O throttling */
388 if (throttling_group) {
389 *throttling_group = qemu_opt_get(opts, "throttling.group");
390 }
391
392 if (throttle_cfg) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200393 throttle_config_init(throttle_cfg);
Max Reitzfbf81752015-10-19 17:53:31 +0200394 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
395 qemu_opt_get_number(opts, "throttling.bps-total", 0);
396 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
397 qemu_opt_get_number(opts, "throttling.bps-read", 0);
398 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
399 qemu_opt_get_number(opts, "throttling.bps-write", 0);
400 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
401 qemu_opt_get_number(opts, "throttling.iops-total", 0);
402 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
403 qemu_opt_get_number(opts, "throttling.iops-read", 0);
404 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
405 qemu_opt_get_number(opts, "throttling.iops-write", 0);
406
407 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
408 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
409 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
410 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
411 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
412 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
413 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
414 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
415 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
416 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
417 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
418 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
419
Alberto Garcia8a0fc182016-02-18 12:27:02 +0200420 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
421 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
422 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
423 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
424 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
425 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
426 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
427 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
428 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
429 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
430 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
431 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
432
Max Reitzfbf81752015-10-19 17:53:31 +0200433 throttle_cfg->op_size =
434 qemu_opt_get_number(opts, "throttling.iops-size", 0);
435
Alberto Garciad5851082016-02-18 12:26:59 +0200436 if (!throttle_is_valid(throttle_cfg, errp)) {
Max Reitzfbf81752015-10-19 17:53:31 +0200437 return;
438 }
439 }
440
441 if (detect_zeroes) {
442 *detect_zeroes =
443 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
444 qemu_opt_get(opts, "detect-zeroes"),
Eric Blake7fb1cf12015-11-18 01:52:57 -0700445 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Max Reitzfbf81752015-10-19 17:53:31 +0200446 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
447 &local_error);
448 if (local_error) {
449 error_propagate(errp, local_error);
450 return;
451 }
452
453 if (bdrv_flags &&
454 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
455 !(*bdrv_flags & BDRV_O_UNMAP))
456 {
457 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
458 "without setting discard operation to unmap");
459 return;
460 }
461 }
462}
463
Kevin Wolff298d072013-09-10 12:01:20 +0200464/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200465static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
466 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200467{
468 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200469 int bdrv_flags = 0;
470 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200471 bool account_invalid, account_failed;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300472 bool writethrough, read_only;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200473 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200474 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200475 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200476 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100477 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100478 QemuOpts *opts;
Alberto Garcia40119ef2015-11-16 11:28:38 +0200479 QDict *interval_dict = NULL;
480 QList *interval_list = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100481 const char *id;
Max Reitzfbf81752015-10-19 17:53:31 +0200482 BlockdevDetectZeroesOptions detect_zeroes =
483 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
484 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200485
Kevin Wolff298d072013-09-10 12:01:20 +0200486 /* Check common options by copying from bs_opts to opts, all other options
487 * stay in bs_opts for processing by bdrv_open(). */
488 id = qdict_get_try_str(bs_opts, "id");
Kevin Wolf00063832013-03-15 10:35:07 +0100489 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100490 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200491 error_propagate(errp, error);
Markus Armbruster6376f952014-05-28 11:17:01 +0200492 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100493 }
494
Kevin Wolf00063832013-03-15 10:35:07 +0100495 qemu_opts_absorb_qdict(opts, bs_opts, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100496 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200497 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100498 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100499 }
500
501 if (id) {
502 qdict_del(bs_opts, "id");
503 }
504
Markus Armbruster666daa62010-06-02 18:48:27 +0200505 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200506 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200507
Alberto Garcia362e9292015-10-28 17:33:04 +0200508 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
509 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
510
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100511 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
512
Alberto Garciaff356ee2016-07-08 17:03:00 +0300513 id = qemu_opts_id(opts);
514
Alberto Garcia40119ef2015-11-16 11:28:38 +0200515 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
516 qdict_array_split(interval_dict, &interval_list);
517
518 if (qdict_size(interval_dict) != 0) {
519 error_setg(errp, "Invalid option stats-intervals.%s",
520 qdict_first(interval_dict)->key);
521 goto early_err;
522 }
Alberto Garcia2be55062015-10-28 17:33:07 +0200523
Max Reitzfbf81752015-10-19 17:53:31 +0200524 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
525 &detect_zeroes, &error);
526 if (error) {
527 error_propagate(errp, error);
528 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200529 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200530
531 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100532 if (is_help_option(buf)) {
533 error_printf("Supported formats:");
534 bdrv_iterate_format(bdrv_format_print, NULL);
535 error_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100536 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200537 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200538
Max Reitze4342ce2015-02-05 13:58:14 -0500539 if (qdict_haskey(bs_opts, "driver")) {
540 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100541 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400542 }
Max Reitze4342ce2015-02-05 13:58:14 -0500543 qdict_put(bs_opts, "driver", qstring_from_str(buf));
Markus Armbruster666daa62010-06-02 18:48:27 +0200544 }
545
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200546 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200547 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200548 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100549 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200550 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100551 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200552 }
553 }
554
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200555 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200556 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200557 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100558 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200559 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100560 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200561 }
562 }
563
Max Reitz5ec18f82015-10-19 17:53:30 +0200564 if (snapshot) {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200565 bdrv_flags |= BDRV_O_SNAPSHOT;
Max Reitz5ec18f82015-10-19 17:53:30 +0200566 }
567
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300568 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
569
Kevin Wolf326642b2013-07-11 12:52:34 +0200570 /* init */
Kevin Wolf39c4ae92015-11-13 14:45:42 +0100571 if ((!file || !*file) && !qdict_size(bs_opts)) {
Max Reitz5ec18f82015-10-19 17:53:30 +0200572 BlockBackendRootState *blk_rs;
573
Max Reitz109525a2016-05-17 16:41:34 +0200574 blk = blk_new();
Max Reitz5ec18f82015-10-19 17:53:30 +0200575 blk_rs = blk_get_root_state(blk);
576 blk_rs->open_flags = bdrv_flags;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300577 blk_rs->read_only = read_only;
Max Reitz5ec18f82015-10-19 17:53:30 +0200578 blk_rs->detect_zeroes = detect_zeroes;
579
Max Reitze4342ce2015-02-05 13:58:14 -0500580 QDECREF(bs_opts);
581 } else {
582 if (file && !*file) {
583 file = NULL;
584 }
585
Kevin Wolf91a097e2015-05-08 17:49:53 +0200586 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
587 * with other callers) rather than what we want as the real defaults.
588 * Apply the defaults here instead. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200589 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
590 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300591 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
592 read_only ? "on" : "off");
Kevin Wolf72e775c2016-03-15 14:34:37 +0100593 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200594
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100595 if (runstate_check(RUN_STATE_INMIGRATE)) {
596 bdrv_flags |= BDRV_O_INACTIVE;
597 }
598
Max Reitzefaa7c42016-03-16 19:54:38 +0100599 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500600 if (!blk) {
601 goto err_no_bs_opts;
602 }
603 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500604
Max Reitz5ec18f82015-10-19 17:53:30 +0200605 bs->detect_zeroes = detect_zeroes;
606
Max Reitz5ec18f82015-10-19 17:53:30 +0200607 if (bdrv_key_required(bs)) {
608 autostart = 0;
609 }
Alberto Garcia362e9292015-10-28 17:33:04 +0200610
611 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
Alberto Garcia2be55062015-10-28 17:33:07 +0200612
Alberto Garcia40119ef2015-11-16 11:28:38 +0200613 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
614 blk_unref(blk);
615 blk = NULL;
616 goto err_no_bs_opts;
Alberto Garcia2be55062015-10-28 17:33:07 +0200617 }
Max Reitz5ec18f82015-10-19 17:53:30 +0200618 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200619
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100620 /* disk I/O throttling */
621 if (throttle_enabled(&cfg)) {
622 if (!throttling_group) {
Alberto Garciaff356ee2016-07-08 17:03:00 +0300623 throttling_group = id;
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100624 }
625 blk_io_limits_enable(blk, throttling_group);
626 blk_set_io_limits(blk, &cfg);
627 }
628
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100629 blk_set_enable_write_cache(blk, !writethrough);
Max Reitz373340b2015-10-19 17:53:22 +0200630 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200631
Alberto Garciaff356ee2016-07-08 17:03:00 +0300632 if (!monitor_add_blk(blk, id, errp)) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100633 blk_unref(blk);
634 blk = NULL;
635 goto err_no_bs_opts;
636 }
637
Max Reitze4342ce2015-02-05 13:58:14 -0500638err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100639 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200640 QDECREF(interval_dict);
641 QDECREF(interval_list);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200642 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100643
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100644early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100645 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200646 QDECREF(interval_dict);
647 QDECREF(interval_list);
Markus Armbruster6376f952014-05-28 11:17:01 +0200648err_no_opts:
649 QDECREF(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100650 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200651}
652
Max Reitzbd745e22015-10-19 17:53:32 +0200653static QemuOptsList qemu_root_bds_opts;
654
655/* Takes the ownership of bs_opts */
656static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
657{
658 BlockDriverState *bs;
659 QemuOpts *opts;
660 Error *local_error = NULL;
661 BlockdevDetectZeroesOptions detect_zeroes;
Max Reitzbd745e22015-10-19 17:53:32 +0200662 int bdrv_flags = 0;
663
664 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
665 if (!opts) {
666 goto fail;
667 }
668
669 qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
670 if (local_error) {
671 error_propagate(errp, local_error);
672 goto fail;
673 }
674
675 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
676 &detect_zeroes, &local_error);
677 if (local_error) {
678 error_propagate(errp, local_error);
679 goto fail;
680 }
681
Kevin Wolfa81d6162016-03-07 14:23:04 +0100682 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
683 * with other callers) rather than what we want as the real defaults.
684 * Apply the defaults here instead. */
Kevin Wolfa81d6162016-03-07 14:23:04 +0100685 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
686 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300687 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
Kevin Wolfa81d6162016-03-07 14:23:04 +0100688
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100689 if (runstate_check(RUN_STATE_INMIGRATE)) {
690 bdrv_flags |= BDRV_O_INACTIVE;
691 }
692
Max Reitz5b363932016-05-17 16:41:31 +0200693 bs = bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
694 if (!bs) {
Max Reitzbd745e22015-10-19 17:53:32 +0200695 goto fail_no_bs_opts;
696 }
697
698 bs->detect_zeroes = detect_zeroes;
699
700fail_no_bs_opts:
701 qemu_opts_del(opts);
702 return bs;
703
704fail:
705 qemu_opts_del(opts);
706 QDECREF(bs_opts);
707 return NULL;
708}
709
Max Reitz9c4218e2016-01-29 16:36:12 +0100710void blockdev_close_all_bdrv_states(void)
711{
712 BlockDriverState *bs, *next_bs;
713
714 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
715 AioContext *ctx = bdrv_get_aio_context(bs);
716
717 aio_context_acquire(ctx);
718 bdrv_unref(bs);
719 aio_context_release(ctx);
720 }
721}
722
Max Reitz262b4e82016-03-16 19:54:41 +0100723/* Iterates over the list of monitor-owned BlockDriverStates */
724BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
725{
726 return bs ? QTAILQ_NEXT(bs, monitor_list)
727 : QTAILQ_FIRST(&monitor_bdrv_states);
728}
729
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200730static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
731 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200732{
733 const char *value;
734
735 value = qemu_opt_get(opts, from);
736 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200737 if (qemu_opt_find(opts, to)) {
738 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
739 "same time", to, from);
740 return;
741 }
Jun Li20d6cd42014-09-24 13:45:27 +0800742 }
743
744 /* rename all items in opts */
745 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100746 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200747 qemu_opt_unset(opts, from);
748 }
749}
750
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200751QemuOptsList qemu_legacy_drive_opts = {
752 .name = "drive",
753 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
754 .desc = {
755 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200756 .name = "bus",
757 .type = QEMU_OPT_NUMBER,
758 .help = "bus number",
759 },{
760 .name = "unit",
761 .type = QEMU_OPT_NUMBER,
762 .help = "unit number (i.e. lun for scsi)",
763 },{
764 .name = "index",
765 .type = QEMU_OPT_NUMBER,
766 .help = "index number",
767 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200768 .name = "media",
769 .type = QEMU_OPT_STRING,
770 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200771 },{
772 .name = "if",
773 .type = QEMU_OPT_STRING,
774 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200775 },{
776 .name = "cyls",
777 .type = QEMU_OPT_NUMBER,
778 .help = "number of cylinders (ide disk geometry)",
779 },{
780 .name = "heads",
781 .type = QEMU_OPT_NUMBER,
782 .help = "number of heads (ide disk geometry)",
783 },{
784 .name = "secs",
785 .type = QEMU_OPT_NUMBER,
786 .help = "number of sectors (ide disk geometry)",
787 },{
788 .name = "trans",
789 .type = QEMU_OPT_STRING,
790 .help = "chs translation (auto, lba, none)",
Kevin Wolf26929292013-09-09 17:01:03 +0200791 },{
792 .name = "boot",
793 .type = QEMU_OPT_BOOL,
794 .help = "(deprecated, ignored)",
Kevin Wolf394c7d42013-09-13 14:09:17 +0200795 },{
796 .name = "addr",
797 .type = QEMU_OPT_STRING,
798 .help = "pci address (virtio only)",
Max Reitzd095b462013-12-20 19:28:14 +0100799 },{
Kevin Wolfbcf83152014-06-06 15:21:48 +0200800 .name = "serial",
801 .type = QEMU_OPT_STRING,
802 .help = "disk serial number",
803 },{
Max Reitzd095b462013-12-20 19:28:14 +0100804 .name = "file",
805 .type = QEMU_OPT_STRING,
806 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200807 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200808
809 /* Options that are passed on, but have special semantics with -drive */
810 {
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300811 .name = BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200812 .type = QEMU_OPT_BOOL,
813 .help = "open drive file as read-only",
814 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100815 .name = "rerror",
816 .type = QEMU_OPT_STRING,
817 .help = "read error action",
818 },{
819 .name = "werror",
820 .type = QEMU_OPT_STRING,
821 .help = "write error action",
822 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200823 .name = "copy-on-read",
824 .type = QEMU_OPT_BOOL,
825 .help = "copy read data from backing file into image file",
826 },
827
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200828 { /* end of list */ }
829 },
830};
831
Markus Armbruster60e19e02014-06-06 14:50:58 +0200832DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
Kevin Wolf57975222013-07-17 14:41:54 +0200833{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200834 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200835 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200836 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200837 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200838 QemuOpts *legacy_opts;
839 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200840 BlockInterfaceType type;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200841 int cyls, heads, secs, translation;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200842 int max_devs, bus_id, unit_id, index;
Kevin Wolf394c7d42013-09-13 14:09:17 +0200843 const char *devaddr;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100844 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800845 bool read_only = false;
846 bool copy_on_read;
Kevin Wolfbcf83152014-06-06 15:21:48 +0200847 const char *serial;
Max Reitzd095b462013-12-20 19:28:14 +0100848 const char *filename;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200849 Error *local_err = NULL;
Kevin Wolf247147f2014-09-24 16:37:14 +0200850 int i;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200851
Kevin Wolf57975222013-07-17 14:41:54 +0200852 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200853 static const struct {
854 const char *from;
855 const char *to;
856 } opt_renames[] = {
857 { "iops", "throttling.iops-total" },
858 { "iops_rd", "throttling.iops-read" },
859 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200860
Kevin Wolf247147f2014-09-24 16:37:14 +0200861 { "bps", "throttling.bps-total" },
862 { "bps_rd", "throttling.bps-read" },
863 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200864
Kevin Wolf247147f2014-09-24 16:37:14 +0200865 { "iops_max", "throttling.iops-total-max" },
866 { "iops_rd_max", "throttling.iops-read-max" },
867 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200868
Kevin Wolf247147f2014-09-24 16:37:14 +0200869 { "bps_max", "throttling.bps-total-max" },
870 { "bps_rd_max", "throttling.bps-read-max" },
871 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200872
Kevin Wolf247147f2014-09-24 16:37:14 +0200873 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200874
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200875 { "group", "throttling.group" },
876
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300877 { "readonly", BDRV_OPT_READ_ONLY },
Kevin Wolf247147f2014-09-24 16:37:14 +0200878 };
879
880 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200881 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
882 &local_err);
883 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100884 error_report_err(local_err);
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200885 return NULL;
886 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200887 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200888
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200889 value = qemu_opt_get(all_opts, "cache");
890 if (value) {
891 int flags = 0;
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100892 bool writethrough;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200893
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100894 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200895 error_report("invalid cache option");
896 return NULL;
897 }
898
899 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200900 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
901 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100902 !writethrough, &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200903 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200904 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
905 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100906 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200907 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200908 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
909 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100910 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200911 }
912 qemu_opt_unset(all_opts, "cache");
913 }
914
Kevin Wolff298d072013-09-10 12:01:20 +0200915 /* Get a QDict for processing the options */
916 bs_opts = qdict_new();
917 qemu_opts_to_qdict(all_opts, bs_opts);
918
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800919 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
920 &error_abort);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200921 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100922 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100923 error_report_err(local_err);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200924 goto fail;
925 }
926
Kevin Wolf26929292013-09-09 17:01:03 +0200927 /* Deprecated option boot=[on|off] */
928 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
929 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
930 "ignored. Future versions will reject this parameter. Please "
931 "update your scripts.\n");
932 }
933
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200934 /* Media type */
935 value = qemu_opt_get(legacy_opts, "media");
936 if (value) {
937 if (!strcmp(value, "disk")) {
938 media = MEDIA_DISK;
939 } else if (!strcmp(value, "cdrom")) {
940 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800941 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200942 } else {
943 error_report("'%s' invalid media", value);
944 goto fail;
945 }
946 }
947
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200948 /* copy-on-read is disabled with a warning for read-only devices */
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300949 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200950 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
951
952 if (read_only && copy_on_read) {
953 error_report("warning: disabling copy-on-read on read-only drive");
954 copy_on_read = false;
955 }
956
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300957 qdict_put(bs_opts, BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200958 qstring_from_str(read_only ? "on" : "off"));
959 qdict_put(bs_opts, "copy-on-read",
960 qstring_from_str(copy_on_read ? "on" :"off"));
961
Kevin Wolf593d4642013-08-28 17:24:51 +0200962 /* Controller type */
963 value = qemu_opt_get(legacy_opts, "if");
964 if (value) {
965 for (type = 0;
966 type < IF_COUNT && strcmp(value, if_name[type]);
967 type++) {
968 }
969 if (type == IF_COUNT) {
970 error_report("unsupported bus type '%s'", value);
971 goto fail;
972 }
973 } else {
974 type = block_default_type;
975 }
976
Kevin Wolfb41a7332013-09-09 16:49:49 +0200977 /* Geometry */
978 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
979 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
980 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
981
982 if (cyls || heads || secs) {
983 if (cyls < 1) {
984 error_report("invalid physical cyls number");
985 goto fail;
986 }
987 if (heads < 1) {
988 error_report("invalid physical heads number");
989 goto fail;
990 }
991 if (secs < 1) {
992 error_report("invalid physical secs number");
993 goto fail;
994 }
995 }
996
997 translation = BIOS_ATA_TRANSLATION_AUTO;
998 value = qemu_opt_get(legacy_opts, "trans");
999 if (value != NULL) {
1000 if (!cyls) {
1001 error_report("'%s' trans must be used with cyls, heads and secs",
1002 value);
1003 goto fail;
1004 }
1005 if (!strcmp(value, "none")) {
1006 translation = BIOS_ATA_TRANSLATION_NONE;
1007 } else if (!strcmp(value, "lba")) {
1008 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +01001009 } else if (!strcmp(value, "large")) {
1010 translation = BIOS_ATA_TRANSLATION_LARGE;
1011 } else if (!strcmp(value, "rechs")) {
1012 translation = BIOS_ATA_TRANSLATION_RECHS;
Kevin Wolfb41a7332013-09-09 16:49:49 +02001013 } else if (!strcmp(value, "auto")) {
1014 translation = BIOS_ATA_TRANSLATION_AUTO;
1015 } else {
1016 error_report("'%s' invalid translation type", value);
1017 goto fail;
1018 }
1019 }
1020
1021 if (media == MEDIA_CDROM) {
1022 if (cyls || secs || heads) {
1023 error_report("CHS can't be set with media=cdrom");
1024 goto fail;
1025 }
1026 }
1027
Kevin Wolf87a899c2013-09-10 15:48:13 +02001028 /* Device address specified by bus/unit or index.
1029 * If none was specified, try to find the first free one. */
1030 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
1031 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
1032 index = qemu_opt_get_number(legacy_opts, "index", -1);
1033
1034 max_devs = if_max_devs[type];
1035
1036 if (index != -1) {
1037 if (bus_id != 0 || unit_id != -1) {
1038 error_report("index cannot be used with bus and unit");
1039 goto fail;
1040 }
1041 bus_id = drive_index_to_bus_id(type, index);
1042 unit_id = drive_index_to_unit_id(type, index);
1043 }
1044
1045 if (unit_id == -1) {
1046 unit_id = 0;
1047 while (drive_get(type, bus_id, unit_id) != NULL) {
1048 unit_id++;
1049 if (max_devs && unit_id >= max_devs) {
1050 unit_id -= max_devs;
1051 bus_id++;
1052 }
1053 }
1054 }
1055
1056 if (max_devs && unit_id >= max_devs) {
1057 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1058 goto fail;
1059 }
1060
1061 if (drive_get(type, bus_id, unit_id) != NULL) {
1062 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1063 bus_id, unit_id, index);
1064 goto fail;
1065 }
1066
Kevin Wolfbcf83152014-06-06 15:21:48 +02001067 /* Serial number */
1068 serial = qemu_opt_get(legacy_opts, "serial");
1069
Kevin Wolf87a899c2013-09-10 15:48:13 +02001070 /* no id supplied -> create one */
1071 if (qemu_opts_id(all_opts) == NULL) {
1072 char *new_id;
1073 const char *mediastr = "";
1074 if (type == IF_IDE || type == IF_SCSI) {
1075 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1076 }
1077 if (max_devs) {
1078 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1079 mediastr, unit_id);
1080 } else {
1081 new_id = g_strdup_printf("%s%s%i", if_name[type],
1082 mediastr, unit_id);
1083 }
1084 qdict_put(bs_opts, "id", qstring_from_str(new_id));
1085 g_free(new_id);
1086 }
1087
Kevin Wolf394c7d42013-09-13 14:09:17 +02001088 /* Add virtio block device */
1089 devaddr = qemu_opt_get(legacy_opts, "addr");
1090 if (devaddr && type != IF_VIRTIO) {
1091 error_report("addr is not supported by this bus type");
1092 goto fail;
1093 }
1094
1095 if (type == IF_VIRTIO) {
1096 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001097 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1098 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001099 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +02001100 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001101 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001102 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001103 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001104 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1105 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001106 if (devaddr) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001107 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001108 }
1109 }
1110
Max Reitzd095b462013-12-20 19:28:14 +01001111 filename = qemu_opt_get(legacy_opts, "file");
1112
Kevin Wolfee13ed12014-02-09 09:52:32 +01001113 /* Check werror/rerror compatibility with if=... */
1114 werror = qemu_opt_get(legacy_opts, "werror");
1115 if (werror != NULL) {
1116 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1117 type != IF_NONE) {
1118 error_report("werror is not supported by this bus type");
1119 goto fail;
1120 }
1121 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1122 }
1123
1124 rerror = qemu_opt_get(legacy_opts, "rerror");
1125 if (rerror != NULL) {
1126 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1127 type != IF_NONE) {
1128 error_report("rerror is not supported by this bus type");
1129 goto fail;
1130 }
1131 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1132 }
1133
Kevin Wolf2d246f02013-09-18 15:14:47 +02001134 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster18e46a02014-10-07 13:59:06 +02001135 blk = blockdev_init(filename, bs_opts, &local_err);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001136 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +02001137 if (!blk) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001138 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001139 error_report_err(local_err);
Kevin Wolfb6810722013-09-20 11:33:11 +02001140 }
Kevin Wolf2d246f02013-09-18 15:14:47 +02001141 goto fail;
Kevin Wolfb6810722013-09-20 11:33:11 +02001142 } else {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001143 assert(!local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001144 }
1145
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001146 /* Create legacy DriveInfo */
1147 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001148 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001149
Kevin Wolfb41a7332013-09-09 16:49:49 +02001150 dinfo->cyls = cyls;
1151 dinfo->heads = heads;
1152 dinfo->secs = secs;
1153 dinfo->trans = translation;
1154
Kevin Wolfee13ed12014-02-09 09:52:32 +01001155 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001156 dinfo->bus = bus_id;
1157 dinfo->unit = unit_id;
Kevin Wolf394c7d42013-09-13 14:09:17 +02001158 dinfo->devaddr = devaddr;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001159 dinfo->serial = g_strdup(serial);
1160
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001161 blk_set_legacy_dinfo(blk, dinfo);
1162
Kevin Wolfe34ef042013-09-19 14:24:10 +02001163 switch(type) {
1164 case IF_IDE:
1165 case IF_SCSI:
1166 case IF_XEN:
1167 case IF_NONE:
1168 dinfo->media_cd = media == MEDIA_CDROM;
1169 break;
1170 default:
1171 break;
1172 }
1173
Kevin Wolf2d246f02013-09-18 15:14:47 +02001174fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001175 qemu_opts_del(legacy_opts);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001176 QDECREF(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001177 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001178}
1179
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02001180static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1181{
1182 BlockDriverState *bs;
1183
1184 bs = bdrv_lookup_bs(name, name, errp);
1185 if (bs == NULL) {
1186 return NULL;
1187 }
1188
1189 if (!bdrv_is_root_node(bs)) {
1190 error_setg(errp, "Need a root block node");
1191 return NULL;
1192 }
1193
1194 if (!bdrv_is_inserted(bs)) {
1195 error_setg(errp, "Device has no medium");
1196 return NULL;
1197 }
1198
1199 return bs;
1200}
1201
Kevin Wolfb33945c2016-09-20 13:38:43 +02001202static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id,
1203 Error **errp)
1204{
1205 BlockBackend *blk;
1206
1207 if (!blk_name == !qdev_id) {
1208 error_setg(errp, "Need exactly one of 'device' and 'id'");
1209 return NULL;
1210 }
1211
1212 if (qdev_id) {
1213 blk = blk_by_qdev_id(qdev_id, errp);
1214 } else {
1215 blk = blk_by_name(blk_name);
1216 if (blk == NULL) {
1217 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1218 "Device '%s' not found", blk_name);
1219 }
1220 }
1221
1222 return blk;
1223}
1224
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001225void hmp_commit(Monitor *mon, const QDict *qdict)
Markus Armbruster666daa62010-06-02 18:48:27 +02001226{
Markus Armbruster666daa62010-06-02 18:48:27 +02001227 const char *device = qdict_get_str(qdict, "device");
Fam Zhenga0e85442015-03-02 19:36:48 +08001228 BlockBackend *blk;
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001229 int ret;
Markus Armbruster666daa62010-06-02 18:48:27 +02001230
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001231 if (!strcmp(device, "all")) {
Max Reitzda31d592016-03-16 19:54:32 +01001232 ret = blk_commit_all();
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001233 } else {
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001234 BlockDriverState *bs;
1235 AioContext *aio_context;
1236
Fam Zhenga0e85442015-03-02 19:36:48 +08001237 blk = blk_by_name(device);
1238 if (!blk) {
Jeff Cody58513bd2013-01-18 12:45:35 -05001239 monitor_printf(mon, "Device '%s' not found\n", device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +02001240 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001241 }
Max Reitz5433c242015-10-19 17:53:29 +02001242 if (!blk_is_available(blk)) {
1243 monitor_printf(mon, "Device '%s' has no medium\n", device);
1244 return;
1245 }
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001246
1247 bs = blk_bs(blk);
1248 aio_context = bdrv_get_aio_context(bs);
1249 aio_context_acquire(aio_context);
1250
1251 ret = bdrv_commit(bs);
1252
1253 aio_context_release(aio_context);
Jeff Cody58513bd2013-01-18 12:45:35 -05001254 }
1255 if (ret < 0) {
1256 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1257 strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +02001258 }
1259}
1260
Eric Blake10f75902016-03-03 09:16:50 -07001261static void blockdev_do_action(TransactionAction *action, Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001262{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001263 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001264
Eric Blake10f75902016-03-03 09:16:50 -07001265 list.value = action;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001266 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001267 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001268}
1269
Benoît Canet0901f672014-01-23 21:31:38 +01001270void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1271 bool has_node_name, const char *node_name,
1272 const char *snapshot_file,
1273 bool has_snapshot_node_name,
1274 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001275 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001276 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001277{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001278 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001279 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001280 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001281 .has_node_name = has_node_name,
1282 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001283 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001284 .has_snapshot_node_name = has_snapshot_node_name,
1285 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001286 .has_format = has_format,
1287 .format = (char *) format,
1288 .has_mode = has_mode,
1289 .mode = mode,
1290 };
Eric Blake10f75902016-03-03 09:16:50 -07001291 TransactionAction action = {
1292 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001293 .u.blockdev_snapshot_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001294 };
1295 blockdev_do_action(&action, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001296}
1297
Alberto Garcia43de7e22015-10-26 14:27:16 +02001298void qmp_blockdev_snapshot(const char *node, const char *overlay,
1299 Error **errp)
1300{
1301 BlockdevSnapshot snapshot_data = {
1302 .node = (char *) node,
1303 .overlay = (char *) overlay
1304 };
Eric Blake10f75902016-03-03 09:16:50 -07001305 TransactionAction action = {
1306 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
Eric Blake32bafa82016-03-17 16:48:37 -06001307 .u.blockdev_snapshot.data = &snapshot_data,
Eric Blake10f75902016-03-03 09:16:50 -07001308 };
1309 blockdev_do_action(&action, errp);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001310}
1311
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001312void qmp_blockdev_snapshot_internal_sync(const char *device,
1313 const char *name,
1314 Error **errp)
1315{
1316 BlockdevSnapshotInternal snapshot = {
1317 .device = (char *) device,
1318 .name = (char *) name
1319 };
Eric Blake10f75902016-03-03 09:16:50 -07001320 TransactionAction action = {
1321 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001322 .u.blockdev_snapshot_internal_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001323 };
1324 blockdev_do_action(&action, errp);
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001325}
1326
Wenchao Xia44e3e052013-09-11 14:04:36 +08001327SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1328 bool has_id,
1329 const char *id,
1330 bool has_name,
1331 const char *name,
1332 Error **errp)
1333{
Fam Zhenga0e85442015-03-02 19:36:48 +08001334 BlockDriverState *bs;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001335 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001336 QEMUSnapshotInfo sn;
1337 Error *local_err = NULL;
1338 SnapshotInfo *info = NULL;
1339 int ret;
1340
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001341 bs = qmp_get_root_bs(device, errp);
1342 if (!bs) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001343 return NULL;
1344 }
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001345 aio_context = bdrv_get_aio_context(bs);
Max Reitz5433c242015-10-19 17:53:29 +02001346 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001347
1348 if (!has_id) {
1349 id = NULL;
1350 }
1351
1352 if (!has_name) {
1353 name = NULL;
1354 }
1355
1356 if (!id && !name) {
1357 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001358 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001359 }
1360
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001361 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1362 goto out_aio_context;
1363 }
1364
Wenchao Xia44e3e052013-09-11 14:04:36 +08001365 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001366 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001367 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001368 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001369 }
1370 if (!ret) {
1371 error_setg(errp,
1372 "Snapshot with id '%s' and name '%s' does not exist on "
1373 "device '%s'",
1374 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001375 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001376 }
1377
1378 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001379 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001380 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001381 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001382 }
1383
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001384 aio_context_release(aio_context);
1385
Markus Armbruster5839e532014-08-19 10:31:08 +02001386 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001387 info->id = g_strdup(sn.id_str);
1388 info->name = g_strdup(sn.name);
1389 info->date_nsec = sn.date_nsec;
1390 info->date_sec = sn.date_sec;
1391 info->vm_state_size = sn.vm_state_size;
1392 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1393 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1394
1395 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001396
1397out_aio_context:
1398 aio_context_release(aio_context);
1399 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001400}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001401
John Snow341ebc22015-04-17 19:49:52 -04001402/**
1403 * block_dirty_bitmap_lookup:
1404 * Return a dirty bitmap (if present), after validating
1405 * the node reference and bitmap names.
1406 *
1407 * @node: The name of the BDS node to search for bitmaps
1408 * @name: The name of the bitmap to search for
1409 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1410 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1411 * @errp: Output pointer for error information. Can be NULL.
1412 *
1413 * @return: A bitmap object on success, or NULL on failure.
1414 */
1415static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1416 const char *name,
1417 BlockDriverState **pbs,
1418 AioContext **paio,
1419 Error **errp)
1420{
1421 BlockDriverState *bs;
1422 BdrvDirtyBitmap *bitmap;
1423 AioContext *aio_context;
1424
1425 if (!node) {
1426 error_setg(errp, "Node cannot be NULL");
1427 return NULL;
1428 }
1429 if (!name) {
1430 error_setg(errp, "Bitmap name cannot be NULL");
1431 return NULL;
1432 }
1433 bs = bdrv_lookup_bs(node, node, NULL);
1434 if (!bs) {
1435 error_setg(errp, "Node '%s' not found", node);
1436 return NULL;
1437 }
1438
1439 aio_context = bdrv_get_aio_context(bs);
1440 aio_context_acquire(aio_context);
1441
1442 bitmap = bdrv_find_dirty_bitmap(bs, name);
1443 if (!bitmap) {
1444 error_setg(errp, "Dirty bitmap '%s' not found", name);
1445 goto fail;
1446 }
1447
1448 if (pbs) {
1449 *pbs = bs;
1450 }
1451 if (paio) {
1452 *paio = aio_context;
1453 } else {
1454 aio_context_release(aio_context);
1455 }
1456
1457 return bitmap;
1458
1459 fail:
1460 aio_context_release(aio_context);
1461 return NULL;
1462}
1463
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001464/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001465
John Snow50f43f02015-11-05 18:13:09 -05001466typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001467
John Snow50f43f02015-11-05 18:13:09 -05001468/**
1469 * BlkActionOps:
1470 * Table of operations that define an Action.
1471 *
1472 * @instance_size: Size of state struct, in bytes.
1473 * @prepare: Prepare the work, must NOT be NULL.
1474 * @commit: Commit the changes, can be NULL.
1475 * @abort: Abort the changes on fail, can be NULL.
1476 * @clean: Clean up resources after all transaction actions have called
1477 * commit() or abort(). Can be NULL.
1478 *
1479 * Only prepare() may fail. In a single transaction, only one of commit() or
1480 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001481 */
John Snow50f43f02015-11-05 18:13:09 -05001482typedef struct BlkActionOps {
1483 size_t instance_size;
1484 void (*prepare)(BlkActionState *common, Error **errp);
1485 void (*commit)(BlkActionState *common);
1486 void (*abort)(BlkActionState *common);
1487 void (*clean)(BlkActionState *common);
1488} BlkActionOps;
1489
1490/**
1491 * BlkActionState:
1492 * Describes one Action's state within a Transaction.
1493 *
1494 * @action: QAPI-defined enum identifying which Action to perform.
1495 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001496 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001497 * @entry: List membership for all Actions in this Transaction.
1498 *
1499 * This structure must be arranged as first member in a subclassed type,
1500 * assuming that the compiler will also arrange it to the same offsets as the
1501 * base class.
1502 */
1503struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001504 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001505 const BlkActionOps *ops;
John Snow94d16a62015-11-05 18:13:18 -05001506 BlockJobTxn *block_job_txn;
1507 TransactionProperties *txn_props;
John Snow50f43f02015-11-05 18:13:09 -05001508 QSIMPLEQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001509};
1510
Wenchao Xiabbe86012013-09-11 14:04:34 +08001511/* internal snapshot private data */
1512typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001513 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001514 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001515 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001516 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001517 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001518} InternalSnapshotState;
1519
John Snow94d16a62015-11-05 18:13:18 -05001520
1521static int action_check_completion_mode(BlkActionState *s, Error **errp)
1522{
1523 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1524 error_setg(errp,
1525 "Action '%s' does not support Transaction property "
1526 "completion-mode = %s",
1527 TransactionActionKind_lookup[s->action->type],
1528 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1529 return -1;
1530 }
1531 return 0;
1532}
1533
John Snow50f43f02015-11-05 18:13:09 -05001534static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001535 Error **errp)
1536{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001537 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001538 const char *device;
1539 const char *name;
1540 BlockDriverState *bs;
1541 QEMUSnapshotInfo old_sn, *sn;
1542 bool ret;
1543 qemu_timeval tv;
1544 BlockdevSnapshotInternal *internal;
1545 InternalSnapshotState *state;
1546 int ret1;
1547
Eric Blake6a8f9662015-10-26 16:34:54 -06001548 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001549 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001550 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001551 state = DO_UPCAST(InternalSnapshotState, common, common);
1552
1553 /* 1. parse input */
1554 device = internal->device;
1555 name = internal->name;
1556
1557 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001558 if (action_check_completion_mode(common, errp) < 0) {
1559 return;
1560 }
1561
Kevin Wolf75dfd402016-06-23 14:20:24 +02001562 bs = qmp_get_root_bs(device, errp);
1563 if (!bs) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001564 return;
1565 }
1566
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001567 /* AioContext is released in .clean() */
Kevin Wolf75dfd402016-06-23 14:20:24 +02001568 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001569 aio_context_acquire(state->aio_context);
1570
Fam Zheng507306c2015-10-23 11:08:13 +08001571 state->bs = bs;
1572 bdrv_drained_begin(bs);
1573
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001574 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1575 return;
1576 }
1577
Wenchao Xiabbe86012013-09-11 14:04:34 +08001578 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001579 error_setg(errp, "Device '%s' is read only", device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001580 return;
1581 }
1582
1583 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001584 error_setg(errp, "Block format '%s' used by device '%s' "
1585 "does not support internal snapshots",
1586 bs->drv->format_name, device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001587 return;
1588 }
1589
1590 if (!strlen(name)) {
1591 error_setg(errp, "Name is empty");
1592 return;
1593 }
1594
1595 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001596 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1597 &local_err);
1598 if (local_err) {
1599 error_propagate(errp, local_err);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001600 return;
1601 } else if (ret) {
1602 error_setg(errp,
1603 "Snapshot with name '%s' already exists on device '%s'",
1604 name, device);
1605 return;
1606 }
1607
1608 /* 3. take the snapshot */
1609 sn = &state->sn;
1610 pstrcpy(sn->name, sizeof(sn->name), name);
1611 qemu_gettimeofday(&tv);
1612 sn->date_sec = tv.tv_sec;
1613 sn->date_nsec = tv.tv_usec * 1000;
1614 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1615
1616 ret1 = bdrv_snapshot_create(bs, sn);
1617 if (ret1 < 0) {
1618 error_setg_errno(errp, -ret1,
1619 "Failed to create snapshot '%s' on device '%s'",
1620 name, device);
1621 return;
1622 }
1623
1624 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001625 state->created = true;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001626}
1627
John Snow50f43f02015-11-05 18:13:09 -05001628static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001629{
1630 InternalSnapshotState *state =
1631 DO_UPCAST(InternalSnapshotState, common, common);
1632 BlockDriverState *bs = state->bs;
1633 QEMUSnapshotInfo *sn = &state->sn;
1634 Error *local_error = NULL;
1635
Fam Zheng507306c2015-10-23 11:08:13 +08001636 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001637 return;
1638 }
1639
1640 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001641 error_reportf_err(local_error,
1642 "Failed to delete snapshot with id '%s' and "
1643 "name '%s' on device '%s' in abort: ",
1644 sn->id_str, sn->name,
1645 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001646 }
1647}
1648
John Snow50f43f02015-11-05 18:13:09 -05001649static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001650{
1651 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1652 common, common);
1653
1654 if (state->aio_context) {
Fam Zheng507306c2015-10-23 11:08:13 +08001655 if (state->bs) {
1656 bdrv_drained_end(state->bs);
1657 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001658 aio_context_release(state->aio_context);
1659 }
1660}
1661
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001662/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001663typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001664 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001665 BlockDriverState *old_bs;
1666 BlockDriverState *new_bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001667 AioContext *aio_context;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001668} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001669
John Snow50f43f02015-11-05 18:13:09 -05001670static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001671 Error **errp)
1672{
Max Reitz5b363932016-05-17 16:41:31 +02001673 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001674 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001675 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001676 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001677 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001678 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001679 /* Reference to the new image (for 'blockdev-snapshot') */
1680 const char *snapshot_ref;
1681 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001682 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001683 ExternalSnapshotState *state =
1684 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001685 TransactionAction *action = common->action;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001686
Alberto Garcia43de7e22015-10-26 14:27:16 +02001687 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1688 * purpose but a different set of parameters */
1689 switch (action->type) {
1690 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1691 {
Eric Blake32bafa82016-03-17 16:48:37 -06001692 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001693 device = s->node;
1694 node_name = s->node;
1695 new_image_file = NULL;
1696 snapshot_ref = s->overlay;
1697 }
1698 break;
1699 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1700 {
Eric Blake32bafa82016-03-17 16:48:37 -06001701 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001702 device = s->has_device ? s->device : NULL;
1703 node_name = s->has_node_name ? s->node_name : NULL;
1704 new_image_file = s->snapshot_file;
1705 snapshot_ref = NULL;
1706 }
1707 break;
1708 default:
1709 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001710 }
1711
1712 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001713 if (action_check_completion_mode(common, errp) < 0) {
1714 return;
1715 }
1716
Alberto Garcia43de7e22015-10-26 14:27:16 +02001717 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1718 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001719 return;
1720 }
1721
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001722 /* Acquire AioContext now so any threads operating on old_bs stop */
1723 state->aio_context = bdrv_get_aio_context(state->old_bs);
1724 aio_context_acquire(state->aio_context);
Fam Zhengda763e82015-10-23 11:08:10 +08001725 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001726
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001727 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001728 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001729 return;
1730 }
1731
Fam Zheng3718d8a2014-05-23 21:29:43 +08001732 if (bdrv_op_is_blocked(state->old_bs,
1733 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001734 return;
1735 }
1736
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001737 if (!bdrv_is_read_only(state->old_bs)) {
1738 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001739 error_setg(errp, QERR_IO_ERROR);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001740 return;
1741 }
1742 }
1743
Benoît Canet212a5a82014-01-23 21:31:36 +01001744 if (!bdrv_is_first_non_filter(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001745 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
Benoît Canetf6186f42013-10-02 14:33:48 +02001746 return;
1747 }
1748
Alberto Garcia43de7e22015-10-26 14:27:16 +02001749 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001750 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001751 const char *format = s->has_format ? s->format : "qcow2";
1752 enum NewImageMode mode;
1753 const char *snapshot_node_name =
1754 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001755
Alberto Garcia43de7e22015-10-26 14:27:16 +02001756 if (node_name && !snapshot_node_name) {
1757 error_setg(errp, "New snapshot node name missing");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001758 return;
1759 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001760
1761 if (snapshot_node_name &&
1762 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1763 error_setg(errp, "New snapshot node name already in use");
1764 return;
1765 }
1766
1767 flags = state->old_bs->open_flags;
Kevin Wolf4c844982016-02-29 13:12:26 +01001768 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001769
1770 /* create new image w/backing file */
1771 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1772 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001773 int64_t size = bdrv_getlength(state->old_bs);
1774 if (size < 0) {
1775 error_setg_errno(errp, -size, "bdrv_getlength failed");
1776 return;
1777 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001778 bdrv_img_create(new_image_file, format,
1779 state->old_bs->filename,
1780 state->old_bs->drv->format_name,
Kevin Wolff86b8b52016-03-02 12:16:44 +01001781 NULL, size, flags, &local_err, false);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001782 if (local_err) {
1783 error_propagate(errp, local_err);
1784 return;
1785 }
1786 }
1787
1788 options = qdict_new();
1789 if (s->has_snapshot_node_name) {
1790 qdict_put(options, "node-name",
1791 qstring_from_str(snapshot_node_name));
1792 }
1793 qdict_put(options, "driver", qstring_from_str(format));
1794
1795 flags |= BDRV_O_NO_BACKING;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001796 }
1797
Max Reitz5b363932016-05-17 16:41:31 +02001798 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1799 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001800 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001801 if (!state->new_bs) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001802 return;
1803 }
1804
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001805 if (bdrv_has_blk(state->new_bs)) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001806 error_setg(errp, "The snapshot is already in use by %s",
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001807 bdrv_get_parent_name(state->new_bs));
Alberto Garcia43de7e22015-10-26 14:27:16 +02001808 return;
1809 }
1810
1811 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1812 errp)) {
1813 return;
1814 }
1815
1816 if (state->new_bs->backing != NULL) {
1817 error_setg(errp, "The snapshot already has a backing image");
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001818 return;
1819 }
1820
1821 if (!state->new_bs->drv->supports_backing) {
1822 error_setg(errp, "The snapshot does not support backing images");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001823 }
1824}
1825
John Snow50f43f02015-11-05 18:13:09 -05001826static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001827{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001828 ExternalSnapshotState *state =
1829 DO_UPCAST(ExternalSnapshotState, common, common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001830
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001831 bdrv_set_aio_context(state->new_bs, state->aio_context);
1832
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001833 /* This removes our old bs and adds the new bs */
1834 bdrv_append(state->new_bs, state->old_bs);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001835 /* We don't need (or want) to use the transactional
1836 * bdrv_reopen_multiple() across all the entries at once, because we
1837 * don't want to abort all of them if one of them fails the reopen */
Kevin Wolf4c844982016-02-29 13:12:26 +01001838 if (!state->old_bs->copy_on_read) {
1839 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1840 NULL);
1841 }
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001842}
1843
John Snow50f43f02015-11-05 18:13:09 -05001844static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001845{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001846 ExternalSnapshotState *state =
1847 DO_UPCAST(ExternalSnapshotState, common, common);
1848 if (state->new_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001849 bdrv_unref(state->new_bs);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001850 }
Fam Zhengda763e82015-10-23 11:08:10 +08001851}
1852
John Snow50f43f02015-11-05 18:13:09 -05001853static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001854{
1855 ExternalSnapshotState *state =
1856 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001857 if (state->aio_context) {
Fam Zhengda763e82015-10-23 11:08:10 +08001858 bdrv_drained_end(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001859 aio_context_release(state->aio_context);
1860 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001861}
1862
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001863typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001864 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001865 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001866 AioContext *aio_context;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001867 BlockJob *job;
1868} DriveBackupState;
1869
Pavel Butsykin81206a82016-07-22 11:17:50 +03001870static void do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
1871 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001872
John Snow50f43f02015-11-05 18:13:09 -05001873static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001874{
1875 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001876 BlockDriverState *bs;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001877 DriveBackup *backup;
1878 Error *local_err = NULL;
1879
Eric Blake6a8f9662015-10-26 16:34:54 -06001880 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001881 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001882
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001883 bs = qmp_get_root_bs(backup->device, errp);
1884 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001885 return;
1886 }
1887
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001888 /* AioContext is released in .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001889 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001890 aio_context_acquire(state->aio_context);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001891 bdrv_drained_begin(bs);
1892 state->bs = bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001893
Pavel Butsykin81206a82016-07-22 11:17:50 +03001894 do_drive_backup(backup, common->block_job_txn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001895 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001896 error_propagate(errp, local_err);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001897 return;
1898 }
1899
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001900 state->job = state->bs->job;
1901}
1902
John Snow50f43f02015-11-05 18:13:09 -05001903static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001904{
1905 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1906 BlockDriverState *bs = state->bs;
1907
1908 /* Only cancel if it's the job we started */
1909 if (bs && bs->job && bs->job == state->job) {
1910 block_job_cancel_sync(bs->job);
1911 }
1912}
1913
John Snow50f43f02015-11-05 18:13:09 -05001914static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001915{
1916 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1917
1918 if (state->aio_context) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001919 bdrv_drained_end(state->bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001920 aio_context_release(state->aio_context);
1921 }
1922}
1923
Fam Zhengbd8baec2014-12-18 18:37:06 +08001924typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001925 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001926 BlockDriverState *bs;
1927 BlockJob *job;
1928 AioContext *aio_context;
1929} BlockdevBackupState;
1930
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03001931static void do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
1932 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001933
John Snow50f43f02015-11-05 18:13:09 -05001934static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001935{
1936 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1937 BlockdevBackup *backup;
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001938 BlockDriverState *bs, *target;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001939 Error *local_err = NULL;
1940
Eric Blake6a8f9662015-10-26 16:34:54 -06001941 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001942 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001943
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001944 bs = qmp_get_root_bs(backup->device, errp);
1945 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001946 return;
1947 }
1948
Kevin Wolf39d990a2016-08-02 19:22:04 +02001949 target = bdrv_lookup_bs(backup->target, backup->target, errp);
Max Reitz5433c242015-10-19 17:53:29 +02001950 if (!target) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001951 return;
1952 }
1953
1954 /* AioContext is released in .clean() */
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001955 state->aio_context = bdrv_get_aio_context(bs);
Kevin Wolf39d990a2016-08-02 19:22:04 +02001956 if (state->aio_context != bdrv_get_aio_context(target)) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001957 state->aio_context = NULL;
1958 error_setg(errp, "Backup between two IO threads is not implemented");
1959 return;
1960 }
1961 aio_context_acquire(state->aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001962 state->bs = bs;
Fam Zhengff52bf32015-10-23 11:08:12 +08001963 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001964
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03001965 do_blockdev_backup(backup, common->block_job_txn, &local_err);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001966 if (local_err) {
1967 error_propagate(errp, local_err);
1968 return;
1969 }
1970
Fam Zhengbd8baec2014-12-18 18:37:06 +08001971 state->job = state->bs->job;
1972}
1973
John Snow50f43f02015-11-05 18:13:09 -05001974static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001975{
1976 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1977 BlockDriverState *bs = state->bs;
1978
1979 /* Only cancel if it's the job we started */
1980 if (bs && bs->job && bs->job == state->job) {
1981 block_job_cancel_sync(bs->job);
1982 }
1983}
1984
John Snow50f43f02015-11-05 18:13:09 -05001985static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001986{
1987 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1988
1989 if (state->aio_context) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001990 bdrv_drained_end(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001991 aio_context_release(state->aio_context);
1992 }
1993}
1994
Fam Zhengdf9a6812015-11-09 18:16:54 +08001995typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05001996 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001997 BdrvDirtyBitmap *bitmap;
1998 BlockDriverState *bs;
1999 AioContext *aio_context;
2000 HBitmap *backup;
2001 bool prepared;
2002} BlockDirtyBitmapState;
2003
John Snow50f43f02015-11-05 18:13:09 -05002004static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002005 Error **errp)
2006{
2007 Error *local_err = NULL;
2008 BlockDirtyBitmapAdd *action;
2009 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2010 common, common);
2011
John Snow94d16a62015-11-05 18:13:18 -05002012 if (action_check_completion_mode(common, errp) < 0) {
2013 return;
2014 }
2015
Eric Blake32bafa82016-03-17 16:48:37 -06002016 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002017 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2018 qmp_block_dirty_bitmap_add(action->node, action->name,
2019 action->has_granularity, action->granularity,
2020 &local_err);
2021
2022 if (!local_err) {
2023 state->prepared = true;
2024 } else {
2025 error_propagate(errp, local_err);
2026 }
2027}
2028
John Snow50f43f02015-11-05 18:13:09 -05002029static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002030{
2031 BlockDirtyBitmapAdd *action;
2032 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2033 common, common);
2034
Eric Blake32bafa82016-03-17 16:48:37 -06002035 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002036 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2037 * then the node reference and bitmap name must have been valid.
2038 */
2039 if (state->prepared) {
2040 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2041 }
2042}
2043
John Snow50f43f02015-11-05 18:13:09 -05002044static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002045 Error **errp)
2046{
2047 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2048 common, common);
2049 BlockDirtyBitmap *action;
2050
John Snow94d16a62015-11-05 18:13:18 -05002051 if (action_check_completion_mode(common, errp) < 0) {
2052 return;
2053 }
2054
Eric Blake32bafa82016-03-17 16:48:37 -06002055 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002056 state->bitmap = block_dirty_bitmap_lookup(action->node,
2057 action->name,
2058 &state->bs,
2059 &state->aio_context,
2060 errp);
2061 if (!state->bitmap) {
2062 return;
2063 }
2064
2065 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2066 error_setg(errp, "Cannot modify a frozen bitmap");
2067 return;
2068 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2069 error_setg(errp, "Cannot clear a disabled bitmap");
2070 return;
2071 }
2072
2073 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2074 /* AioContext is released in .clean() */
2075}
2076
John Snow50f43f02015-11-05 18:13:09 -05002077static void block_dirty_bitmap_clear_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002078{
2079 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2080 common, common);
2081
2082 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2083}
2084
John Snow50f43f02015-11-05 18:13:09 -05002085static void block_dirty_bitmap_clear_commit(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002086{
2087 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2088 common, common);
2089
2090 hbitmap_free(state->backup);
2091}
2092
John Snow50f43f02015-11-05 18:13:09 -05002093static void block_dirty_bitmap_clear_clean(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002094{
2095 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2096 common, common);
2097
2098 if (state->aio_context) {
2099 aio_context_release(state->aio_context);
2100 }
2101}
2102
John Snow50f43f02015-11-05 18:13:09 -05002103static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002104{
2105 error_setg(errp, "Transaction aborted using Abort action");
2106}
2107
John Snow50f43f02015-11-05 18:13:09 -05002108static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002109{
Stefan Weildfc6f862013-07-25 18:21:28 +02002110 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002111}
2112
John Snow50f43f02015-11-05 18:13:09 -05002113static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002114 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2115 .instance_size = sizeof(ExternalSnapshotState),
2116 .prepare = external_snapshot_prepare,
2117 .commit = external_snapshot_commit,
2118 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002119 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002120 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002121 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002122 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002123 .prepare = external_snapshot_prepare,
2124 .commit = external_snapshot_commit,
2125 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002126 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002127 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002128 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2129 .instance_size = sizeof(DriveBackupState),
2130 .prepare = drive_backup_prepare,
2131 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002132 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002133 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002134 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2135 .instance_size = sizeof(BlockdevBackupState),
2136 .prepare = blockdev_backup_prepare,
2137 .abort = blockdev_backup_abort,
2138 .clean = blockdev_backup_clean,
2139 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002140 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002141 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002142 .prepare = abort_prepare,
2143 .commit = abort_commit,
2144 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002145 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2146 .instance_size = sizeof(InternalSnapshotState),
2147 .prepare = internal_snapshot_prepare,
2148 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002149 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002150 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002151 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2152 .instance_size = sizeof(BlockDirtyBitmapState),
2153 .prepare = block_dirty_bitmap_add_prepare,
2154 .abort = block_dirty_bitmap_add_abort,
2155 },
2156 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2157 .instance_size = sizeof(BlockDirtyBitmapState),
2158 .prepare = block_dirty_bitmap_clear_prepare,
2159 .commit = block_dirty_bitmap_clear_commit,
2160 .abort = block_dirty_bitmap_clear_abort,
2161 .clean = block_dirty_bitmap_clear_clean,
2162 }
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002163};
2164
John Snow94d16a62015-11-05 18:13:18 -05002165/**
2166 * Allocate a TransactionProperties structure if necessary, and fill
2167 * that structure with desired defaults if they are unset.
2168 */
2169static TransactionProperties *get_transaction_properties(
2170 TransactionProperties *props)
2171{
2172 if (!props) {
2173 props = g_new0(TransactionProperties, 1);
2174 }
2175
2176 if (!props->has_completion_mode) {
2177 props->has_completion_mode = true;
2178 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2179 }
2180
2181 return props;
2182}
2183
Jeff Cody8802d1f2012-02-28 15:54:06 -05002184/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002185 * 'Atomic' group operations. The operations are performed as a set, and if
2186 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002187 */
John Snow94d16a62015-11-05 18:13:18 -05002188void qmp_transaction(TransactionActionList *dev_list,
2189 bool has_props,
2190 struct TransactionProperties *props,
2191 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002192{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002193 TransactionActionList *dev_entry = dev_list;
John Snow94d16a62015-11-05 18:13:18 -05002194 BlockJobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002195 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002196 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002197
John Snow50f43f02015-11-05 18:13:09 -05002198 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002199 QSIMPLEQ_INIT(&snap_bdrv_states);
2200
John Snow94d16a62015-11-05 18:13:18 -05002201 /* Does this transaction get canceled as a group on failure?
2202 * If not, we don't really need to make a BlockJobTxn.
2203 */
2204 props = get_transaction_properties(props);
2205 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2206 block_job_txn = block_job_txn_new();
2207 }
2208
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002209 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002210 bdrv_drain_all();
2211
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002212 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002213 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002214 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002215 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002216
Jeff Cody8802d1f2012-02-28 15:54:06 -05002217 dev_info = dev_entry->value;
2218 dev_entry = dev_entry->next;
2219
Eric Blake6a8f9662015-10-26 16:34:54 -06002220 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002221
Eric Blake6a8f9662015-10-26 16:34:54 -06002222 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002223 assert(ops->instance_size > 0);
2224
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002225 state = g_malloc0(ops->instance_size);
2226 state->ops = ops;
2227 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002228 state->block_job_txn = block_job_txn;
2229 state->txn_props = props;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002230 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002231
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002232 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002233 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002234 error_propagate(errp, local_err);
2235 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002236 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002237 }
2238
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002239 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002240 if (state->ops->commit) {
2241 state->ops->commit(state);
2242 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002243 }
2244
2245 /* success */
2246 goto exit;
2247
2248delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002249 /* failure, and it is all-or-none; roll back all operations */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002250 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2251 if (state->ops->abort) {
2252 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002253 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002254 }
2255exit:
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002256 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2257 if (state->ops->clean) {
2258 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002259 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002260 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002261 }
John Snow94d16a62015-11-05 18:13:18 -05002262 if (!has_props) {
2263 qapi_free_TransactionProperties(props);
2264 }
2265 block_job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002266}
2267
Kevin Wolffbe2d812016-09-20 13:38:46 +02002268void qmp_eject(bool has_device, const char *device,
2269 bool has_id, const char *id,
2270 bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002271{
Max Reitz38f54bd2015-10-26 21:39:12 +01002272 Error *local_err = NULL;
John Snow3a3086b2016-05-18 17:53:40 -04002273 int rc;
Markus Armbruster666daa62010-06-02 18:48:27 +02002274
John Snow3a3086b2016-05-18 17:53:40 -04002275 if (!has_force) {
2276 force = false;
2277 }
2278
Kevin Wolffbe2d812016-09-20 13:38:46 +02002279 rc = do_open_tray(has_device ? device : NULL,
2280 has_id ? id : NULL,
2281 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002282 if (rc && rc != -ENOSYS) {
Max Reitz38f54bd2015-10-26 21:39:12 +01002283 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002284 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002285 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002286 error_free(local_err);
John Snow3a3086b2016-05-18 17:53:40 -04002287
Kevin Wolffbe2d812016-09-20 13:38:46 +02002288 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002289}
2290
Benoît Canet12d3ba82014-01-23 21:31:35 +01002291void qmp_block_passwd(bool has_device, const char *device,
2292 bool has_node_name, const char *node_name,
2293 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002294{
Benoît Canet12d3ba82014-01-23 21:31:35 +01002295 Error *local_err = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +02002296 BlockDriverState *bs;
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002297 AioContext *aio_context;
Markus Armbruster666daa62010-06-02 18:48:27 +02002298
Benoît Canet12d3ba82014-01-23 21:31:35 +01002299 bs = bdrv_lookup_bs(has_device ? device : NULL,
2300 has_node_name ? node_name : NULL,
2301 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002302 if (local_err) {
Benoît Canet12d3ba82014-01-23 21:31:35 +01002303 error_propagate(errp, local_err);
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02002304 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002305 }
2306
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002307 aio_context = bdrv_get_aio_context(bs);
2308 aio_context_acquire(aio_context);
2309
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002310 bdrv_add_key(bs, password, errp);
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002311
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002312 aio_context_release(aio_context);
Markus Armbruster666daa62010-06-02 18:48:27 +02002313}
2314
Colin Lordbf18bee2016-06-06 14:15:22 -04002315/*
2316 * Attempt to open the tray of @device.
2317 * If @force, ignore its tray lock.
2318 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2319 * On error, store an error through @errp and return -errno.
2320 * If @device does not exist, return -ENODEV.
2321 * If it has no removable media, return -ENOTSUP.
2322 * If it has no tray, return -ENOSYS.
2323 * If the guest was asked to open the tray, return -EINPROGRESS.
2324 * Else, return 0.
John Snow3a3086b2016-05-18 17:53:40 -04002325 */
Kevin Wolfb33945c2016-09-20 13:38:43 +02002326static int do_open_tray(const char *blk_name, const char *qdev_id,
2327 bool force, Error **errp)
Max Reitz7d8a9f72015-10-26 21:39:08 +01002328{
2329 BlockBackend *blk;
Kevin Wolfb33945c2016-09-20 13:38:43 +02002330 const char *device = qdev_id ?: blk_name;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002331 bool locked;
2332
Kevin Wolfb33945c2016-09-20 13:38:43 +02002333 blk = qmp_get_blk(blk_name, qdev_id, errp);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002334 if (!blk) {
John Snow3a3086b2016-05-18 17:53:40 -04002335 return -ENODEV;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002336 }
2337
2338 if (!blk_dev_has_removable_media(blk)) {
2339 error_setg(errp, "Device '%s' is not removable", device);
John Snow3a3086b2016-05-18 17:53:40 -04002340 return -ENOTSUP;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002341 }
2342
Max Reitz12c7ec82016-01-29 20:49:11 +01002343 if (!blk_dev_has_tray(blk)) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002344 error_setg(errp, "Device '%s' does not have a tray", device);
2345 return -ENOSYS;
Max Reitz12c7ec82016-01-29 20:49:11 +01002346 }
2347
Max Reitz7d8a9f72015-10-26 21:39:08 +01002348 if (blk_dev_is_tray_open(blk)) {
John Snow3a3086b2016-05-18 17:53:40 -04002349 return 0;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002350 }
2351
2352 locked = blk_dev_is_medium_locked(blk);
2353 if (locked) {
2354 blk_dev_eject_request(blk, force);
2355 }
2356
2357 if (!locked || force) {
2358 blk_dev_change_media_cb(blk, false);
2359 }
John Snow3a3086b2016-05-18 17:53:40 -04002360
2361 if (locked && !force) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002362 error_setg(errp, "Device '%s' is locked and force was not specified, "
2363 "wait for tray to open and try again", device);
2364 return -EINPROGRESS;
John Snow3a3086b2016-05-18 17:53:40 -04002365 }
2366
2367 return 0;
2368}
2369
Kevin Wolfb33945c2016-09-20 13:38:43 +02002370void qmp_blockdev_open_tray(bool has_device, const char *device,
2371 bool has_id, const char *id,
2372 bool has_force, bool force,
John Snow3a3086b2016-05-18 17:53:40 -04002373 Error **errp)
2374{
Colin Lordbf18bee2016-06-06 14:15:22 -04002375 Error *local_err = NULL;
2376 int rc;
2377
John Snow3a3086b2016-05-18 17:53:40 -04002378 if (!has_force) {
2379 force = false;
2380 }
Kevin Wolfb33945c2016-09-20 13:38:43 +02002381 rc = do_open_tray(has_device ? device : NULL,
2382 has_id ? id : NULL,
2383 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002384 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2385 error_propagate(errp, local_err);
2386 return;
2387 }
2388 error_free(local_err);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002389}
2390
Kevin Wolfb33945c2016-09-20 13:38:43 +02002391void qmp_blockdev_close_tray(bool has_device, const char *device,
2392 bool has_id, const char *id,
2393 Error **errp)
Max Reitzabaaf592015-10-26 21:39:09 +01002394{
2395 BlockBackend *blk;
2396
Kevin Wolfb33945c2016-09-20 13:38:43 +02002397 device = has_device ? device : NULL;
2398 id = has_id ? id : NULL;
2399
2400 blk = qmp_get_blk(device, id, errp);
Max Reitzabaaf592015-10-26 21:39:09 +01002401 if (!blk) {
Max Reitzabaaf592015-10-26 21:39:09 +01002402 return;
2403 }
2404
2405 if (!blk_dev_has_removable_media(blk)) {
Kevin Wolfb33945c2016-09-20 13:38:43 +02002406 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitzabaaf592015-10-26 21:39:09 +01002407 return;
2408 }
2409
Max Reitz12c7ec82016-01-29 20:49:11 +01002410 if (!blk_dev_has_tray(blk)) {
2411 /* Ignore this command on tray-less devices */
2412 return;
2413 }
2414
Max Reitzabaaf592015-10-26 21:39:09 +01002415 if (!blk_dev_is_tray_open(blk)) {
2416 return;
2417 }
2418
2419 blk_dev_change_media_cb(blk, true);
2420}
2421
Kevin Wolf00949ba2016-09-20 13:38:45 +02002422void qmp_x_blockdev_remove_medium(bool has_device, const char *device,
2423 bool has_id, const char *id, Error **errp)
Max Reitz2814f672015-10-26 21:39:10 +01002424{
2425 BlockBackend *blk;
2426 BlockDriverState *bs;
2427 AioContext *aio_context;
Kevin Wolf00949ba2016-09-20 13:38:45 +02002428 bool has_attached_device;
Max Reitz2814f672015-10-26 21:39:10 +01002429
Kevin Wolf00949ba2016-09-20 13:38:45 +02002430 device = has_device ? device : NULL;
2431 id = has_id ? id : NULL;
2432
2433 blk = qmp_get_blk(device, id, errp);
Max Reitz2814f672015-10-26 21:39:10 +01002434 if (!blk) {
Max Reitz2814f672015-10-26 21:39:10 +01002435 return;
2436 }
2437
2438 /* For BBs without a device, we can exchange the BDS tree at will */
Kevin Wolf00949ba2016-09-20 13:38:45 +02002439 has_attached_device = blk_get_attached_dev(blk);
Max Reitz2814f672015-10-26 21:39:10 +01002440
Kevin Wolf00949ba2016-09-20 13:38:45 +02002441 if (has_attached_device && !blk_dev_has_removable_media(blk)) {
2442 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002443 return;
2444 }
2445
Kevin Wolf00949ba2016-09-20 13:38:45 +02002446 if (has_attached_device && blk_dev_has_tray(blk) &&
2447 !blk_dev_is_tray_open(blk))
2448 {
2449 error_setg(errp, "Tray of device '%s' is not open", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002450 return;
2451 }
2452
2453 bs = blk_bs(blk);
2454 if (!bs) {
2455 return;
2456 }
2457
2458 aio_context = bdrv_get_aio_context(bs);
2459 aio_context_acquire(aio_context);
2460
2461 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2462 goto out;
2463 }
2464
Max Reitz2814f672015-10-26 21:39:10 +01002465 blk_remove_bs(blk);
2466
Max Reitz12c7ec82016-01-29 20:49:11 +01002467 if (!blk_dev_has_tray(blk)) {
2468 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2469 * called at all); therefore, the medium needs to be ejected here.
2470 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2471 * value passed here (i.e. false). */
2472 blk_dev_change_media_cb(blk, false);
2473 }
2474
Max Reitz2814f672015-10-26 21:39:10 +01002475out:
2476 aio_context_release(aio_context);
2477}
2478
Kevin Wolf716df212016-09-20 13:38:44 +02002479static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
Max Reitzd1299882015-10-26 21:39:11 +01002480 BlockDriverState *bs, Error **errp)
2481{
Max Reitzd1299882015-10-26 21:39:11 +01002482 bool has_device;
2483
Max Reitzd1299882015-10-26 21:39:11 +01002484 /* For BBs without a device, we can exchange the BDS tree at will */
2485 has_device = blk_get_attached_dev(blk);
2486
2487 if (has_device && !blk_dev_has_removable_media(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002488 error_setg(errp, "Device is not removable");
Max Reitzd1299882015-10-26 21:39:11 +01002489 return;
2490 }
2491
Max Reitz12c7ec82016-01-29 20:49:11 +01002492 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002493 error_setg(errp, "Tray of the device is not open");
Max Reitzd1299882015-10-26 21:39:11 +01002494 return;
2495 }
2496
2497 if (blk_bs(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002498 error_setg(errp, "There already is a medium in the device");
Max Reitzd1299882015-10-26 21:39:11 +01002499 return;
2500 }
2501
2502 blk_insert_bs(blk, bs);
2503
Max Reitz12c7ec82016-01-29 20:49:11 +01002504 if (!blk_dev_has_tray(blk)) {
2505 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2506 * called at all); therefore, the medium needs to be pushed into the
2507 * slot here.
2508 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2509 * value passed here (i.e. true). */
2510 blk_dev_change_media_cb(blk, true);
2511 }
Max Reitzd1299882015-10-26 21:39:11 +01002512}
2513
Kevin Wolf716df212016-09-20 13:38:44 +02002514void qmp_x_blockdev_insert_medium(bool has_device, const char *device,
2515 bool has_id, const char *id,
2516 const char *node_name, Error **errp)
Max Reitzd1299882015-10-26 21:39:11 +01002517{
Kevin Wolf716df212016-09-20 13:38:44 +02002518 BlockBackend *blk;
Max Reitzd1299882015-10-26 21:39:11 +01002519 BlockDriverState *bs;
2520
Kevin Wolf716df212016-09-20 13:38:44 +02002521 blk = qmp_get_blk(has_device ? device : NULL,
2522 has_id ? id : NULL,
2523 errp);
2524 if (!blk) {
2525 return;
2526 }
2527
Max Reitzd1299882015-10-26 21:39:11 +01002528 bs = bdrv_find_node(node_name);
2529 if (!bs) {
2530 error_setg(errp, "Node '%s' not found", node_name);
2531 return;
2532 }
2533
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002534 if (bdrv_has_blk(bs)) {
Max Reitzd1299882015-10-26 21:39:11 +01002535 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002536 bdrv_get_parent_name(bs));
Max Reitzd1299882015-10-26 21:39:11 +01002537 return;
2538 }
2539
Kevin Wolf716df212016-09-20 13:38:44 +02002540 qmp_blockdev_insert_anon_medium(blk, bs, errp);
Max Reitzd1299882015-10-26 21:39:11 +01002541}
2542
Max Reitz24fb4132015-11-06 16:27:06 +01002543void qmp_blockdev_change_medium(const char *device, const char *filename,
2544 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002545 bool has_read_only,
2546 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002547 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002548{
2549 BlockBackend *blk;
2550 BlockDriverState *medium_bs = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002551 int bdrv_flags;
Colin Lord38a53d52016-06-08 13:56:28 -04002552 int rc;
Max Reitzde2c6c02015-10-26 21:39:13 +01002553 QDict *options = NULL;
2554 Error *err = NULL;
2555
2556 blk = blk_by_name(device);
2557 if (!blk) {
2558 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2559 "Device '%s' not found", device);
2560 goto fail;
2561 }
2562
2563 if (blk_bs(blk)) {
2564 blk_update_root_state(blk);
2565 }
2566
2567 bdrv_flags = blk_get_open_flags_from_root_state(blk);
Alyssa Milburn156abc22016-02-06 13:36:18 +00002568 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2569 BDRV_O_PROTOCOL);
Max Reitzde2c6c02015-10-26 21:39:13 +01002570
Max Reitz39ff43d2015-11-11 04:49:44 +01002571 if (!has_read_only) {
2572 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2573 }
2574
2575 switch (read_only) {
2576 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2577 break;
2578
2579 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2580 bdrv_flags &= ~BDRV_O_RDWR;
2581 break;
2582
2583 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2584 bdrv_flags |= BDRV_O_RDWR;
2585 break;
2586
2587 default:
2588 abort();
2589 }
2590
Max Reitz24fb4132015-11-06 16:27:06 +01002591 if (has_format) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002592 options = qdict_new();
2593 qdict_put(options, "driver", qstring_from_str(format));
2594 }
2595
Max Reitz5b363932016-05-17 16:41:31 +02002596 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2597 if (!medium_bs) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002598 goto fail;
2599 }
2600
Max Reitzde2c6c02015-10-26 21:39:13 +01002601 bdrv_add_key(medium_bs, NULL, &err);
2602 if (err) {
2603 error_propagate(errp, err);
2604 goto fail;
2605 }
2606
Kevin Wolfb33945c2016-09-20 13:38:43 +02002607 rc = do_open_tray(device, NULL, false, &err);
Colin Lord38a53d52016-06-08 13:56:28 -04002608 if (rc && rc != -ENOSYS) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002609 error_propagate(errp, err);
2610 goto fail;
2611 }
Colin Lord38a53d52016-06-08 13:56:28 -04002612 error_free(err);
2613 err = NULL;
Max Reitzde2c6c02015-10-26 21:39:13 +01002614
Kevin Wolf00949ba2016-09-20 13:38:45 +02002615 qmp_x_blockdev_remove_medium(true, device, false, NULL, errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002616 if (err) {
2617 error_propagate(errp, err);
2618 goto fail;
2619 }
2620
Kevin Wolf716df212016-09-20 13:38:44 +02002621 qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002622 if (err) {
2623 error_propagate(errp, err);
2624 goto fail;
2625 }
2626
Kevin Wolfa5614992016-03-21 10:49:51 +01002627 blk_apply_root_state(blk, medium_bs);
2628
Kevin Wolfb33945c2016-09-20 13:38:43 +02002629 qmp_blockdev_close_tray(true, device, false, NULL, errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002630
2631fail:
2632 /* If the medium has been inserted, the device has its own reference, so
2633 * ours must be relinquished; and if it has not been inserted successfully,
2634 * the reference must be relinquished anyway */
2635 bdrv_unref(medium_bs);
2636}
2637
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002638/* throttling disk I/O limits */
Eric Blake4dc93972016-07-13 21:50:21 -06002639void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002640{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002641 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002642 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002643 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002644 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002645
Eric Blake4dc93972016-07-13 21:50:21 -06002646 blk = blk_by_name(arg->device);
Fam Zhenga0e85442015-03-02 19:36:48 +08002647 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01002648 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Eric Blake4dc93972016-07-13 21:50:21 -06002649 "Device '%s' not found", arg->device);
Luiz Capitulino80047da2011-12-14 16:49:14 -02002650 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002651 }
Max Reitz5433c242015-10-19 17:53:29 +02002652
2653 aio_context = blk_get_aio_context(blk);
2654 aio_context_acquire(aio_context);
2655
Fam Zhenga0e85442015-03-02 19:36:48 +08002656 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002657 if (!bs) {
Eric Blake4dc93972016-07-13 21:50:21 -06002658 error_setg(errp, "Device '%s' has no medium", arg->device);
Max Reitz5433c242015-10-19 17:53:29 +02002659 goto out;
2660 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002661
Alberto Garcia1588ab52016-02-18 12:27:00 +02002662 throttle_config_init(&cfg);
Eric Blake4dc93972016-07-13 21:50:21 -06002663 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2664 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2665 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002666
Eric Blake4dc93972016-07-13 21:50:21 -06002667 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2668 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2669 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
Benoît Canetcc0681c2013-09-02 14:14:39 +02002670
Eric Blake4dc93972016-07-13 21:50:21 -06002671 if (arg->has_bps_max) {
2672 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002673 }
Eric Blake4dc93972016-07-13 21:50:21 -06002674 if (arg->has_bps_rd_max) {
2675 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002676 }
Eric Blake4dc93972016-07-13 21:50:21 -06002677 if (arg->has_bps_wr_max) {
2678 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002679 }
Eric Blake4dc93972016-07-13 21:50:21 -06002680 if (arg->has_iops_max) {
2681 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002682 }
Eric Blake4dc93972016-07-13 21:50:21 -06002683 if (arg->has_iops_rd_max) {
2684 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002685 }
Eric Blake4dc93972016-07-13 21:50:21 -06002686 if (arg->has_iops_wr_max) {
2687 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002688 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002689
Eric Blake4dc93972016-07-13 21:50:21 -06002690 if (arg->has_bps_max_length) {
2691 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002692 }
Eric Blake4dc93972016-07-13 21:50:21 -06002693 if (arg->has_bps_rd_max_length) {
2694 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002695 }
Eric Blake4dc93972016-07-13 21:50:21 -06002696 if (arg->has_bps_wr_max_length) {
2697 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002698 }
Eric Blake4dc93972016-07-13 21:50:21 -06002699 if (arg->has_iops_max_length) {
2700 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002701 }
Eric Blake4dc93972016-07-13 21:50:21 -06002702 if (arg->has_iops_rd_max_length) {
2703 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002704 }
Eric Blake4dc93972016-07-13 21:50:21 -06002705 if (arg->has_iops_wr_max_length) {
2706 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002707 }
2708
Eric Blake4dc93972016-07-13 21:50:21 -06002709 if (arg->has_iops_size) {
2710 cfg.op_size = arg->iops_size;
Benoît Canet2024c1d2013-09-02 14:14:41 +02002711 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002712
Alberto Garciad5851082016-02-18 12:26:59 +02002713 if (!throttle_is_valid(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002714 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002715 }
2716
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002717 if (throttle_enabled(&cfg)) {
2718 /* Enable I/O limits if they're not enabled yet, otherwise
2719 * just update the throttling group. */
Kevin Wolf97148072016-03-21 13:53:52 +01002720 if (!blk_get_public(blk)->throttle_state) {
Eric Blake4dc93972016-07-13 21:50:21 -06002721 blk_io_limits_enable(blk,
2722 arg->has_group ? arg->group : arg->device);
2723 } else if (arg->has_group) {
2724 blk_io_limits_update_group(blk, arg->group);
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002725 }
2726 /* Set the new throttling configuration */
Kevin Wolf97148072016-03-21 13:53:52 +01002727 blk_set_io_limits(blk, &cfg);
2728 } else if (blk_get_public(blk)->throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002729 /* If all throttling settings are set to 0, disable I/O limits */
Kevin Wolf97148072016-03-21 13:53:52 +01002730 blk_io_limits_disable(blk);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002731 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002732
Max Reitz5433c242015-10-19 17:53:29 +02002733out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002734 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002735}
2736
John Snow341ebc22015-04-17 19:49:52 -04002737void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2738 bool has_granularity, uint32_t granularity,
2739 Error **errp)
2740{
2741 AioContext *aio_context;
2742 BlockDriverState *bs;
2743
2744 if (!name || name[0] == '\0') {
2745 error_setg(errp, "Bitmap name cannot be empty");
2746 return;
2747 }
2748
2749 bs = bdrv_lookup_bs(node, node, errp);
2750 if (!bs) {
2751 return;
2752 }
2753
2754 aio_context = bdrv_get_aio_context(bs);
2755 aio_context_acquire(aio_context);
2756
2757 if (has_granularity) {
2758 if (granularity < 512 || !is_power_of_2(granularity)) {
2759 error_setg(errp, "Granularity must be power of 2 "
2760 "and at least 512");
2761 goto out;
2762 }
2763 } else {
2764 /* Default to cluster size, if available: */
2765 granularity = bdrv_get_default_bitmap_granularity(bs);
2766 }
2767
2768 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2769
2770 out:
2771 aio_context_release(aio_context);
2772}
2773
2774void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2775 Error **errp)
2776{
2777 AioContext *aio_context;
2778 BlockDriverState *bs;
2779 BdrvDirtyBitmap *bitmap;
2780
2781 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2782 if (!bitmap || !bs) {
2783 return;
2784 }
2785
John Snow9bd2b082015-04-17 19:49:57 -04002786 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2787 error_setg(errp,
2788 "Bitmap '%s' is currently frozen and cannot be removed",
2789 name);
2790 goto out;
2791 }
John Snow20dca812015-04-17 19:50:02 -04002792 bdrv_dirty_bitmap_make_anon(bitmap);
John Snow341ebc22015-04-17 19:49:52 -04002793 bdrv_release_dirty_bitmap(bs, bitmap);
2794
John Snow9bd2b082015-04-17 19:49:57 -04002795 out:
John Snow341ebc22015-04-17 19:49:52 -04002796 aio_context_release(aio_context);
2797}
2798
John Snowe74e6b72015-04-17 19:49:59 -04002799/**
2800 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2801 * immediately after a full backup operation.
2802 */
2803void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2804 Error **errp)
2805{
2806 AioContext *aio_context;
2807 BdrvDirtyBitmap *bitmap;
2808 BlockDriverState *bs;
2809
2810 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2811 if (!bitmap || !bs) {
2812 return;
2813 }
2814
2815 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2816 error_setg(errp,
2817 "Bitmap '%s' is currently frozen and cannot be modified",
2818 name);
2819 goto out;
2820 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2821 error_setg(errp,
2822 "Bitmap '%s' is currently disabled and cannot be cleared",
2823 name);
2824 goto out;
2825 }
2826
Fam Zhengdf9a6812015-11-09 18:16:54 +08002827 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04002828
2829 out:
2830 aio_context_release(aio_context);
2831}
2832
Markus Armbruster072ebe62015-03-05 17:00:56 +01002833void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06002834{
2835 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002836 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06002837 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002838 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08002839 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06002840
Kevin Wolf2073d412016-02-23 17:50:37 +01002841 bs = bdrv_find_node(id);
2842 if (bs) {
2843 qmp_x_blockdev_del(false, NULL, true, id, &local_err);
2844 if (local_err) {
2845 error_report_err(local_err);
2846 }
2847 return;
2848 }
2849
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002850 blk = blk_by_name(id);
2851 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02002852 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01002853 return;
Ryan Harper9063f812010-11-12 11:07:13 -06002854 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002855
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02002856 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02002857 error_report("Deleting device added with blockdev-add"
2858 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01002859 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02002860 }
2861
Max Reitz5433c242015-10-19 17:53:29 +02002862 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002863 aio_context_acquire(aio_context);
2864
Max Reitz5433c242015-10-19 17:53:29 +02002865 bs = blk_bs(blk);
2866 if (bs) {
2867 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2868 error_report_err(local_err);
2869 aio_context_release(aio_context);
2870 return;
2871 }
Ryan Harper9063f812010-11-12 11:07:13 -06002872
Max Reitz938abd42016-01-29 16:36:09 +01002873 blk_remove_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002874 }
Ryan Harper9063f812010-11-12 11:07:13 -06002875
Max Reitz7c735872016-03-16 19:54:39 +01002876 /* Make the BlockBackend and the attached BlockDriverState anonymous */
Max Reitzefaa7c42016-03-16 19:54:38 +01002877 monitor_remove_blk(blk);
2878
Max Reitz7c735872016-03-16 19:54:39 +01002879 /* If this BlockBackend has a device attached to it, its refcount will be
2880 * decremented when the device is removed; otherwise we have to do so here.
Ryan Harperd22b2f42011-03-29 20:51:47 -05002881 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02002882 if (blk_get_attached_dev(blk)) {
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02002883 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02002884 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2885 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05002886 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02002887 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06002888 }
2889
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002890 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06002891}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002892
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002893void qmp_block_resize(bool has_device, const char *device,
2894 bool has_node_name, const char *node_name,
2895 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002896{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002897 Error *local_err = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002898 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002899 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02002900 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002901
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002902 bs = bdrv_lookup_bs(has_device ? device : NULL,
2903 has_node_name ? node_name : NULL,
2904 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002905 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002906 error_propagate(errp, local_err);
2907 return;
2908 }
2909
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002910 aio_context = bdrv_get_aio_context(bs);
2911 aio_context_acquire(aio_context);
2912
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002913 if (!bdrv_is_first_non_filter(bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002914 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002915 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002916 }
2917
2918 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002919 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002920 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002921 }
2922
Jeff Cody9c75e162014-06-25 16:55:30 -04002923 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002924 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002925 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04002926 }
2927
Peter Lieven92b7a082013-03-11 11:04:24 +01002928 /* complete all in-flight operations before resizing the device */
2929 bdrv_drain_all();
2930
Kevin Wolf87329012013-05-02 15:32:55 +02002931 ret = bdrv_truncate(bs, size);
2932 switch (ret) {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002933 case 0:
2934 break;
2935 case -ENOMEDIUM:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002936 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002937 break;
2938 case -ENOTSUP:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002939 error_setg(errp, QERR_UNSUPPORTED);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002940 break;
2941 case -EACCES:
Alberto Garcia81e5f782015-04-08 12:29:19 +03002942 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002943 break;
2944 case -EBUSY:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002945 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002946 break;
2947 default:
Kevin Wolf87329012013-05-02 15:32:55 +02002948 error_setg_errno(errp, -ret, "Could not resize");
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002949 break;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002950 }
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002951
2952out:
2953 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002954}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002955
Jeff Cody9abf2db2012-09-27 13:29:14 -04002956static void block_job_cb(void *opaque, int ret)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002957{
Stefan Hajnoczi723c5d92014-10-21 12:03:53 +01002958 /* Note that this function may be executed from another AioContext besides
2959 * the QEMU main loop. If you need to access anything that assumes the
2960 * QEMU global mutex, use a BH or introduce a mutex.
2961 */
2962
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002963 BlockDriverState *bs = opaque;
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002964 const char *msg = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002965
Jeff Cody9abf2db2012-09-27 13:29:14 -04002966 trace_block_job_cb(bs, bs->job, ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002967
2968 assert(bs->job);
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002969
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002970 if (ret < 0) {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002971 msg = strerror(-ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002972 }
2973
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002974 if (block_job_is_cancelled(bs->job)) {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002975 block_job_event_cancelled(bs->job);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002976 } else {
Wenchao Xiabcada37b2014-06-18 08:43:47 +02002977 block_job_event_completed(bs->job, msg);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002978 }
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002979}
2980
Alberto Garcia23233222016-07-05 17:28:59 +03002981void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002982 bool has_base, const char *base,
2983 bool has_backing_file, const char *backing_file,
2984 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002985 bool has_on_error, BlockdevOnError on_error,
2986 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002987{
2988 BlockDriverState *bs;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002989 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002990 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002991 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002992 const char *base_name = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002993
Paolo Bonzini1d809092012-09-28 17:22:59 +02002994 if (!has_on_error) {
2995 on_error = BLOCKDEV_ON_ERROR_REPORT;
2996 }
2997
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002998 bs = qmp_get_root_bs(device, errp);
2999 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003000 return;
3001 }
3002
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003003 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003004 aio_context_acquire(aio_context);
3005
Fam Zheng628ff682014-05-23 21:29:44 +08003006 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003007 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003008 }
3009
Jeff Cody13d8cc52014-06-25 15:40:11 -04003010 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003011 base_bs = bdrv_find_backing_image(bs, base);
3012 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003013 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003014 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003015 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003016 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04003017 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003018 }
3019
Jeff Cody13d8cc52014-06-25 15:40:11 -04003020 /* if we are streaming the entire chain, the result will have no backing
3021 * file, and specifying one is therefore an error */
3022 if (base_bs == NULL && has_backing_file) {
3023 error_setg(errp, "backing file specified, but streaming the "
3024 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003025 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003026 }
3027
3028 /* backing_file string overrides base bs filename */
3029 base_name = has_backing_file ? backing_file : base_name;
3030
Alberto Garcia23233222016-07-05 17:28:59 +03003031 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
3032 has_speed ? speed : 0, on_error, block_job_cb, bs, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003033 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003034 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003035 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003036 }
3037
3038 trace_qmp_block_stream(bs, bs->job);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003039
3040out:
3041 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003042}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003043
Alberto Garciafd62c602016-07-05 17:29:00 +03003044void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003045 bool has_base, const char *base,
3046 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003047 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04003048 bool has_speed, int64_t speed,
3049 Error **errp)
3050{
3051 BlockDriverState *bs;
3052 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003053 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04003054 Error *local_err = NULL;
3055 /* This will be part of the QMP command, if/when the
3056 * BlockdevOnError change for blkmirror makes it in
3057 */
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02003058 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
Jeff Codyed61fc12012-09-27 13:29:16 -04003059
Max Reitz54504662014-04-10 19:36:25 +02003060 if (!has_speed) {
3061 speed = 0;
3062 }
3063
Jeff Cody7676e2c2014-06-30 15:14:15 +02003064 /* Important Note:
3065 * libvirt relies on the DeviceNotFound error class in order to probe for
3066 * live commit feature versions; for this to work, we must make sure to
3067 * perform the device lookup before any generic errors that may occur in a
3068 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02003069 bs = qmp_get_root_bs(device, &local_err);
3070 if (!bs) {
3071 bs = bdrv_lookup_bs(device, device, NULL);
3072 if (!bs) {
3073 error_free(local_err);
3074 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3075 "Device '%s' not found", device);
3076 } else {
3077 error_propagate(errp, local_err);
3078 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003079 return;
3080 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003081
Kevin Wolf1d13b162016-06-23 14:20:24 +02003082 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003083 aio_context_acquire(aio_context);
3084
Fam Zhengbb000212014-09-11 13:14:00 +08003085 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003086 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003087 }
3088
Jeff Codyed61fc12012-09-27 13:29:16 -04003089 /* default top_bs is the active layer */
3090 top_bs = bs;
3091
Jeff Cody7676e2c2014-06-30 15:14:15 +02003092 if (has_top && top) {
Jeff Codyed61fc12012-09-27 13:29:16 -04003093 if (strcmp(bs->filename, top) != 0) {
3094 top_bs = bdrv_find_backing_image(bs, top);
3095 }
3096 }
3097
3098 if (top_bs == NULL) {
3099 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003100 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003101 }
3102
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003103 assert(bdrv_get_aio_context(top_bs) == aio_context);
3104
Jeff Codyd5208c42012-10-16 15:49:10 -04003105 if (has_base && base) {
3106 base_bs = bdrv_find_backing_image(top_bs, base);
3107 } else {
3108 base_bs = bdrv_find_base(top_bs);
3109 }
3110
3111 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003112 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003113 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04003114 }
3115
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003116 assert(bdrv_get_aio_context(base_bs) == aio_context);
3117
Fam Zhengbb000212014-09-11 13:14:00 +08003118 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3119 goto out;
3120 }
3121
Jeff Cody7676e2c2014-06-30 15:14:15 +02003122 /* Do not allow attempts to commit an image into itself */
3123 if (top_bs == base_bs) {
3124 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003125 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02003126 }
3127
Fam Zheng20a63d22013-12-16 14:45:31 +08003128 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04003129 if (has_backing_file) {
3130 error_setg(errp, "'backing-file' specified,"
3131 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003132 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003133 }
Alberto Garciafd62c602016-07-05 17:29:00 +03003134 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, speed,
Wen Congyangb49f7ea2016-07-27 15:01:47 +08003135 on_error, block_job_cb, bs, &local_err, false);
Fam Zheng20a63d22013-12-16 14:45:31 +08003136 } else {
Alberto Garciafd62c602016-07-05 17:29:00 +03003137 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed,
3138 on_error, block_job_cb, bs,
Jeff Cody54e26902014-06-25 15:40:10 -04003139 has_backing_file ? backing_file : NULL, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003140 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003141 if (local_err != NULL) {
3142 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003143 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003144 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003145
3146out:
3147 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003148}
3149
Pavel Butsykin81206a82016-07-22 11:17:50 +03003150static void do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003151{
3152 BlockDriverState *bs;
3153 BlockDriverState *target_bs;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003154 BlockDriverState *source = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003155 BdrvDirtyBitmap *bmap = NULL;
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003156 AioContext *aio_context;
Max Reitze6641712015-08-26 19:47:48 +02003157 QDict *options = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003158 Error *local_err = NULL;
3159 int flags;
3160 int64_t size;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003161
Pavel Butsykin81206a82016-07-22 11:17:50 +03003162 if (!backup->has_speed) {
3163 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003164 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003165 if (!backup->has_on_source_error) {
3166 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003167 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003168 if (!backup->has_on_target_error) {
3169 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003170 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003171 if (!backup->has_mode) {
3172 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3173 }
3174 if (!backup->has_job_id) {
3175 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003176 }
Pavel Butsykin13b94142016-07-22 11:17:52 +03003177 if (!backup->has_compress) {
3178 backup->compress = false;
3179 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003180
Pavel Butsykin81206a82016-07-22 11:17:50 +03003181 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003182 if (!bs) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003183 return;
3184 }
3185
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003186 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003187 aio_context_acquire(aio_context);
3188
Pavel Butsykin81206a82016-07-22 11:17:50 +03003189 if (!backup->has_format) {
3190 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
3191 NULL : (char*) bs->drv->format_name;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003192 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003193
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003194 /* Early check to avoid creating target */
Fam Zheng3718d8a2014-05-23 21:29:43 +08003195 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003196 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003197 }
3198
Kevin Wolf61de4c62016-03-18 17:46:45 +01003199 flags = bs->open_flags | BDRV_O_RDWR;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003200
Ian Mainfc5d3f82013-07-26 11:39:04 -07003201 /* See if we have a backing HD we can use to create our new image
3202 * on top of. */
Pavel Butsykin81206a82016-07-22 11:17:50 +03003203 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003204 source = backing_bs(bs);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003205 if (!source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003206 backup->sync = MIRROR_SYNC_MODE_FULL;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003207 }
3208 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003209 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
Ian Mainfc5d3f82013-07-26 11:39:04 -07003210 source = bs;
3211 }
3212
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003213 size = bdrv_getlength(bs);
3214 if (size < 0) {
3215 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003216 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003217 }
3218
Pavel Butsykin81206a82016-07-22 11:17:50 +03003219 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
3220 assert(backup->format);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003221 if (source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003222 bdrv_img_create(backup->target, backup->format, source->filename,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003223 source->drv->format_name, NULL,
3224 size, flags, &local_err, false);
3225 } else {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003226 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003227 size, flags, &local_err, false);
3228 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003229 }
3230
Markus Armbruster84d18f02014-01-30 15:07:28 +01003231 if (local_err) {
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 }
3235
Pavel Butsykin81206a82016-07-22 11:17:50 +03003236 if (backup->format) {
Max Reitze6641712015-08-26 19:47:48 +02003237 options = qdict_new();
Pavel Butsykin81206a82016-07-22 11:17:50 +03003238 qdict_put(options, "driver", qstring_from_str(backup->format));
Max Reitze6641712015-08-26 19:47:48 +02003239 }
3240
Pavel Butsykin81206a82016-07-22 11:17:50 +03003241 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003242 if (!target_bs) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003243 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003244 }
3245
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003246 bdrv_set_aio_context(target_bs, aio_context);
3247
Pavel Butsykin81206a82016-07-22 11:17:50 +03003248 if (backup->has_bitmap) {
3249 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
John Snowd58d8452015-04-17 19:49:58 -04003250 if (!bmap) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003251 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
Max Reitz0702d3d2015-11-09 23:39:10 +01003252 bdrv_unref(target_bs);
John Snowd58d8452015-04-17 19:49:58 -04003253 goto out;
3254 }
3255 }
3256
Pavel Butsykin81206a82016-07-22 11:17:50 +03003257 backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync,
Pavel Butsykin13b94142016-07-22 11:17:52 +03003258 bmap, backup->compress, backup->on_source_error,
3259 backup->on_target_error, block_job_cb, bs, txn, &local_err);
Kevin Wolf5c438bc2016-04-14 13:09:53 +02003260 bdrv_unref(target_bs);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003261 if (local_err != NULL) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003262 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003263 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003264 }
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003265
3266out:
3267 aio_context_release(aio_context);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003268}
3269
Pavel Butsykin81206a82016-07-22 11:17:50 +03003270void qmp_drive_backup(DriveBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003271{
Pavel Butsykin81206a82016-07-22 11:17:50 +03003272 return do_drive_backup(arg, NULL, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003273}
3274
Benoît Canetc13163f2014-01-23 21:31:34 +01003275BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3276{
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003277 return bdrv_named_nodes_list(errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003278}
3279
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003280void do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003281{
3282 BlockDriverState *bs;
3283 BlockDriverState *target_bs;
3284 Error *local_err = NULL;
3285 AioContext *aio_context;
3286
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003287 if (!backup->has_speed) {
3288 backup->speed = 0;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003289 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003290 if (!backup->has_on_source_error) {
3291 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003292 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003293 if (!backup->has_on_target_error) {
3294 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3295 }
3296 if (!backup->has_job_id) {
3297 backup->job_id = NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003298 }
Pavel Butsykin3b7b1232016-07-22 11:17:53 +03003299 if (!backup->has_compress) {
3300 backup->compress = false;
3301 }
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003302
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003303 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003304 if (!bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003305 return;
3306 }
3307
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003308 aio_context = bdrv_get_aio_context(bs);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003309 aio_context_acquire(aio_context);
3310
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003311 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
Fam Zheng0d978912016-05-23 10:19:35 +08003312 if (!target_bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003313 goto out;
3314 }
Max Reitz5433c242015-10-19 17:53:29 +02003315
Fam Zhengefd75562016-05-23 10:19:36 +08003316 if (bdrv_get_aio_context(target_bs) != aio_context) {
3317 if (!bdrv_has_blk(target_bs)) {
3318 /* The target BDS is not attached, we can safely move it to another
3319 * AioContext. */
3320 bdrv_set_aio_context(target_bs, aio_context);
3321 } else {
3322 error_setg(errp, "Target is attached to a different thread from "
3323 "source.");
3324 goto out;
3325 }
3326 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003327 backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync,
Pavel Butsykin3b7b1232016-07-22 11:17:53 +03003328 NULL, backup->compress, backup->on_source_error,
3329 backup->on_target_error, block_job_cb, bs, txn, &local_err);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003330 if (local_err != NULL) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003331 error_propagate(errp, local_err);
3332 }
3333out:
3334 aio_context_release(aio_context);
3335}
3336
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003337void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003338{
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003339 do_blockdev_backup(arg, NULL, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003340}
3341
Fam Zheng4193cdd2015-12-24 12:45:03 +08003342/* Parameter check and block job starting for drive mirroring.
3343 * Caller should hold @device and @target's aio context (must be the same).
3344 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003345static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003346 BlockDriverState *target,
3347 bool has_replaces, const char *replaces,
3348 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003349 BlockMirrorBackingMode backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003350 bool has_speed, int64_t speed,
3351 bool has_granularity, uint32_t granularity,
3352 bool has_buf_size, int64_t buf_size,
3353 bool has_on_source_error,
3354 BlockdevOnError on_source_error,
3355 bool has_on_target_error,
3356 BlockdevOnError on_target_error,
3357 bool has_unmap, bool unmap,
3358 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003359{
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003360
3361 if (!has_speed) {
3362 speed = 0;
3363 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003364 if (!has_on_source_error) {
3365 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3366 }
3367 if (!has_on_target_error) {
3368 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3369 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003370 if (!has_granularity) {
3371 granularity = 0;
3372 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003373 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003374 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003375 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003376 if (!has_unmap) {
3377 unmap = true;
3378 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003379
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003380 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003381 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3382 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003383 return;
3384 }
3385 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003386 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3387 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003388 return;
3389 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003390
Fam Zheng4193cdd2015-12-24 12:45:03 +08003391 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3392 return;
3393 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003394 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3395 return;
3396 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003397
3398 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3399 sync = MIRROR_SYNC_MODE_FULL;
3400 }
3401
3402 /* pass the node name to replace to mirror start since it's loose coupling
3403 * and will allow to check whether the node still exist at mirror completion
3404 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003405 mirror_start(job_id, bs, target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003406 has_replaces ? replaces : NULL,
Max Reitz274fcce2016-06-10 20:57:47 +02003407 speed, granularity, buf_size, sync, backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003408 on_source_error, on_target_error, unmap,
3409 block_job_cb, bs, errp);
3410}
3411
Eric Blakefaecd402016-07-14 16:37:58 -06003412void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003413{
3414 BlockDriverState *bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003415 BlockDriverState *source, *target_bs;
3416 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003417 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003418 Error *local_err = NULL;
3419 QDict *options = NULL;
3420 int flags;
3421 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003422 const char *format = arg->format;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003423
Kevin Wolf0524e932016-06-23 14:20:24 +02003424 bs = qmp_get_root_bs(arg->device, errp);
3425 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003426 return;
3427 }
3428
Kevin Wolf0524e932016-06-23 14:20:24 +02003429 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003430 aio_context_acquire(aio_context);
3431
Eric Blakefaecd402016-07-14 16:37:58 -06003432 if (!arg->has_mode) {
3433 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003434 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003435
Eric Blakefaecd402016-07-14 16:37:58 -06003436 if (!arg->has_format) {
3437 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3438 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003439 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003440
Kevin Wolf61de4c62016-03-18 17:46:45 +01003441 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003442 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003443 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3444 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003445 }
Eric Blakefaecd402016-07-14 16:37:58 -06003446 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003447 source = bs;
3448 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003449
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003450 size = bdrv_getlength(bs);
3451 if (size < 0) {
3452 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003453 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003454 }
3455
Eric Blakefaecd402016-07-14 16:37:58 -06003456 if (arg->has_replaces) {
Benoît Canet09158f02014-06-27 18:25:25 +02003457 BlockDriverState *to_replace_bs;
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003458 AioContext *replace_aio_context;
3459 int64_t replace_size;
Benoît Canet09158f02014-06-27 18:25:25 +02003460
Eric Blakefaecd402016-07-14 16:37:58 -06003461 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003462 error_setg(errp, "a node-name must be provided when replacing a"
3463 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003464 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003465 }
3466
Eric Blakefaecd402016-07-14 16:37:58 -06003467 to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err);
Benoît Canet09158f02014-06-27 18:25:25 +02003468
3469 if (!to_replace_bs) {
3470 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003471 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003472 }
3473
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003474 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3475 aio_context_acquire(replace_aio_context);
3476 replace_size = bdrv_getlength(to_replace_bs);
3477 aio_context_release(replace_aio_context);
3478
3479 if (size != replace_size) {
Benoît Canet09158f02014-06-27 18:25:25 +02003480 error_setg(errp, "cannot replace image with a mirror image of "
3481 "different size");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003482 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003483 }
3484 }
3485
Eric Blakefaecd402016-07-14 16:37:58 -06003486 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003487 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3488 } else {
3489 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3490 }
3491
Eric Blakefaecd402016-07-14 16:37:58 -06003492 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3493 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003494 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003495 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003496 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003497 bdrv_img_create(arg->target, format,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003498 NULL, NULL, NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003499 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003500 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003501 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003502 break;
3503 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3504 /* create new image with backing file */
Eric Blakefaecd402016-07-14 16:37:58 -06003505 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003506 source->filename,
3507 source->drv->format_name,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003508 NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003509 break;
3510 default:
3511 abort();
3512 }
3513 }
3514
Markus Armbruster84d18f02014-01-30 15:07:28 +01003515 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003516 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003517 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003518 }
3519
Max Reitze6641712015-08-26 19:47:48 +02003520 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003521 if (arg->has_node_name) {
3522 qdict_put(options, "node-name", qstring_from_str(arg->node_name));
Benoît Canet4c828dc2014-06-16 12:00:55 +02003523 }
Max Reitze6641712015-08-26 19:47:48 +02003524 if (format) {
3525 qdict_put(options, "driver", qstring_from_str(format));
3526 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003527
Paolo Bonzinib812f672013-01-21 17:09:43 +01003528 /* Mirroring takes care of copy-on-write using the source's backing
3529 * file.
3530 */
Eric Blakefaecd402016-07-14 16:37:58 -06003531 target_bs = bdrv_open(arg->target, NULL, options,
3532 flags | BDRV_O_NO_BACKING, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003533 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003534 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003535 }
3536
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003537 bdrv_set_aio_context(target_bs, aio_context);
3538
Eric Blakefaecd402016-07-14 16:37:58 -06003539 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3540 arg->has_replaces, arg->replaces, arg->sync,
3541 backing_mode, arg->has_speed, arg->speed,
3542 arg->has_granularity, arg->granularity,
3543 arg->has_buf_size, arg->buf_size,
3544 arg->has_on_source_error, arg->on_source_error,
3545 arg->has_on_target_error, arg->on_target_error,
3546 arg->has_unmap, arg->unmap,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003547 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02003548 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003549 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003550out:
3551 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003552}
3553
Alberto Garcia71aa9862016-07-05 17:28:57 +03003554void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3555 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08003556 bool has_replaces, const char *replaces,
3557 MirrorSyncMode sync,
3558 bool has_speed, int64_t speed,
3559 bool has_granularity, uint32_t granularity,
3560 bool has_buf_size, int64_t buf_size,
3561 bool has_on_source_error,
3562 BlockdevOnError on_source_error,
3563 bool has_on_target_error,
3564 BlockdevOnError on_target_error,
3565 Error **errp)
3566{
3567 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08003568 BlockDriverState *target_bs;
3569 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003570 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08003571 Error *local_err = NULL;
3572
Kevin Wolf07eec652016-06-23 14:20:24 +02003573 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08003574 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08003575 return;
3576 }
3577
3578 target_bs = bdrv_lookup_bs(target, target, errp);
3579 if (!target_bs) {
3580 return;
3581 }
3582
3583 aio_context = bdrv_get_aio_context(bs);
3584 aio_context_acquire(aio_context);
3585
Fam Zhengdf925622015-12-24 12:45:05 +08003586 bdrv_set_aio_context(target_bs, aio_context);
3587
Alberto Garcia71aa9862016-07-05 17:28:57 +03003588 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02003589 has_replaces, replaces, sync, backing_mode,
Fam Zhengdf925622015-12-24 12:45:05 +08003590 has_speed, speed,
3591 has_granularity, granularity,
3592 has_buf_size, buf_size,
3593 has_on_source_error, on_source_error,
3594 has_on_target_error, on_target_error,
3595 true, true,
3596 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003597 error_propagate(errp, local_err);
Fam Zhengdf925622015-12-24 12:45:05 +08003598
3599 aio_context_release(aio_context);
3600}
3601
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003602/* Get a block job using its ID and acquire its AioContext */
3603static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003604 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003605{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003606 BlockJob *job;
3607
3608 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003609
Max Reitz5433c242015-10-19 17:53:29 +02003610 *aio_context = NULL;
3611
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003612 job = block_job_get(id);
3613
3614 if (!job) {
3615 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3616 "Block job '%s' not found", id);
3617 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003618 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003619
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003620 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003621 aio_context_acquire(*aio_context);
3622
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003623 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003624}
3625
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003626void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003627{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003628 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003629 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003630
3631 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003632 return;
3633 }
3634
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003635 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003636 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003637}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003638
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003639void qmp_block_job_cancel(const char *device,
3640 bool has_force, bool force, Error **errp)
3641{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003642 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003643 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003644
3645 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003646 return;
3647 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003648
3649 if (!has_force) {
3650 force = false;
3651 }
3652
Fam Zheng751ebd72015-04-03 22:05:18 +08003653 if (job->user_paused && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003654 error_setg(errp, "The block job for device '%s' is currently paused",
3655 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003656 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003657 }
3658
3659 trace_qmp_block_job_cancel(job);
3660 block_job_cancel(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003661out:
3662 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003663}
3664
3665void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003666{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003667 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003668 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003669
Fam Zheng751ebd72015-04-03 22:05:18 +08003670 if (!job || job->user_paused) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003671 return;
3672 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003673
Fam Zheng751ebd72015-04-03 22:05:18 +08003674 job->user_paused = true;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003675 trace_qmp_block_job_pause(job);
3676 block_job_pause(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003677 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003678}
3679
3680void qmp_block_job_resume(const char *device, Error **errp)
3681{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003682 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003683 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003684
Fam Zheng751ebd72015-04-03 22:05:18 +08003685 if (!job || !job->user_paused) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02003686 return;
3687 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003688
Fam Zheng751ebd72015-04-03 22:05:18 +08003689 job->user_paused = false;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003690 trace_qmp_block_job_resume(job);
Stefan Hajnoczi17bd51f2016-06-16 17:56:22 +01003691 block_job_iostatus_reset(job);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003692 block_job_resume(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003693 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003694}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003695
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003696void qmp_block_job_complete(const char *device, Error **errp)
3697{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003698 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003699 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003700
3701 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003702 return;
3703 }
3704
3705 trace_qmp_block_job_complete(job);
3706 block_job_complete(job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003707 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003708}
3709
Jeff Codyfa40e652014-07-01 09:52:16 +02003710void qmp_change_backing_file(const char *device,
3711 const char *image_node_name,
3712 const char *backing_file,
3713 Error **errp)
3714{
3715 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003716 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02003717 BlockDriverState *image_bs = NULL;
3718 Error *local_err = NULL;
3719 bool ro;
3720 int open_flags;
3721 int ret;
3722
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003723 bs = qmp_get_root_bs(device, errp);
3724 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02003725 return;
3726 }
3727
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003728 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003729 aio_context_acquire(aio_context);
3730
Jeff Codyfa40e652014-07-01 09:52:16 +02003731 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3732 if (local_err) {
3733 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003734 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003735 }
3736
3737 if (!image_bs) {
3738 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003739 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003740 }
3741
3742 if (bdrv_find_base(image_bs) == image_bs) {
3743 error_setg(errp, "not allowing backing file change on an image "
3744 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003745 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003746 }
3747
3748 /* even though we are not necessarily operating on bs, we need it to
3749 * determine if block ops are currently prohibited on the chain */
3750 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003751 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003752 }
3753
3754 /* final sanity check */
3755 if (!bdrv_chain_contains(bs, image_bs)) {
3756 error_setg(errp, "'%s' and image file are not in the same chain",
3757 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003758 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003759 }
3760
3761 /* if not r/w, reopen to make r/w */
3762 open_flags = image_bs->open_flags;
3763 ro = bdrv_is_read_only(image_bs);
3764
3765 if (ro) {
3766 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3767 if (local_err) {
3768 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003769 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003770 }
3771 }
3772
3773 ret = bdrv_change_backing_file(image_bs, backing_file,
3774 image_bs->drv ? image_bs->drv->format_name : "");
3775
3776 if (ret < 0) {
3777 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3778 backing_file);
3779 /* don't exit here, so we can try to restore open flags if
3780 * appropriate */
3781 }
3782
3783 if (ro) {
3784 bdrv_reopen(image_bs, open_flags, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003785 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02003786 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003787
3788out:
3789 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02003790}
3791
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003792void hmp_drive_add_node(Monitor *mon, const char *optstr)
3793{
3794 QemuOpts *opts;
3795 QDict *qdict;
3796 Error *local_err = NULL;
3797
3798 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
3799 if (!opts) {
3800 return;
3801 }
3802
3803 qdict = qemu_opts_to_qdict(opts, NULL);
3804
3805 if (!qdict_get_try_str(qdict, "node-name")) {
Kevin Wolff8746fb2016-03-16 11:14:31 +01003806 QDECREF(qdict);
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003807 error_report("'node-name' needs to be specified");
3808 goto out;
3809 }
3810
3811 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
3812 if (!bs) {
3813 error_report_err(local_err);
3814 goto out;
3815 }
3816
3817 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3818
3819out:
3820 qemu_opts_del(opts);
3821}
3822
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003823void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3824{
Max Reitzbe4b67b2015-10-19 17:53:09 +02003825 BlockDriverState *bs;
3826 BlockBackend *blk = NULL;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003827 QObject *obj;
Eric Blake3b098d52016-06-09 10:48:43 -06003828 Visitor *v = qmp_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003829 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003830 Error *local_err = NULL;
3831
Markus Armbruster60e19e02014-06-06 14:50:58 +02003832 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003833 * cache.direct=false instead of silently switching to aio=threads, except
Markus Armbruster60e19e02014-06-06 14:50:58 +02003834 * when called from drive_new().
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003835 *
3836 * For now, simply forbidding the combination for all drivers will do. */
3837 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
Kevin Wolfc6e0bd92014-03-06 15:47:32 +01003838 bool direct = options->has_cache &&
3839 options->cache->has_direct &&
3840 options->cache->direct;
3841 if (!direct) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003842 error_setg(errp, "aio=native requires cache.direct=true");
3843 goto fail;
3844 }
3845 }
3846
Eric Blake3b098d52016-06-09 10:48:43 -06003847 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003848 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003849 error_propagate(errp, local_err);
3850 goto fail;
3851 }
3852
Eric Blake3b098d52016-06-09 10:48:43 -06003853 visit_complete(v, &obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003854 qdict = qobject_to_qdict(obj);
3855
3856 qdict_flatten(qdict);
3857
Max Reitzbe4b67b2015-10-19 17:53:09 +02003858 if (options->has_id) {
3859 blk = blockdev_init(NULL, qdict, &local_err);
3860 if (local_err) {
3861 error_propagate(errp, local_err);
3862 goto fail;
3863 }
3864
3865 bs = blk_bs(blk);
3866 } else {
Max Reitzbe4b67b2015-10-19 17:53:09 +02003867 if (!qdict_get_try_str(qdict, "node-name")) {
3868 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3869 "the root node");
3870 goto fail;
3871 }
3872
Max Reitzbd745e22015-10-19 17:53:32 +02003873 bs = bds_tree_init(qdict, errp);
3874 if (!bs) {
Max Reitzbe4b67b2015-10-19 17:53:09 +02003875 goto fail;
3876 }
Max Reitz9c4218e2016-01-29 16:36:12 +01003877
3878 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003879 }
3880
Max Reitzbe4b67b2015-10-19 17:53:09 +02003881 if (bs && bdrv_key_required(bs)) {
3882 if (blk) {
Max Reitzefaa7c42016-03-16 19:54:38 +01003883 monitor_remove_blk(blk);
Max Reitzbe4b67b2015-10-19 17:53:09 +02003884 blk_unref(blk);
3885 } else {
Max Reitz9c4218e2016-01-29 16:36:12 +01003886 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
Max Reitzbe4b67b2015-10-19 17:53:09 +02003887 bdrv_unref(bs);
3888 }
Kevin Wolf8ae8e902014-03-06 15:43:42 +01003889 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3890 goto fail;
3891 }
3892
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003893fail:
Eric Blake3b098d52016-06-09 10:48:43 -06003894 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003895}
3896
Alberto Garcia81b936a2015-11-02 16:51:55 +02003897void qmp_x_blockdev_del(bool has_id, const char *id,
3898 bool has_node_name, const char *node_name, Error **errp)
3899{
3900 AioContext *aio_context;
3901 BlockBackend *blk;
3902 BlockDriverState *bs;
3903
3904 if (has_id && has_node_name) {
3905 error_setg(errp, "Only one of id and node-name must be specified");
3906 return;
3907 } else if (!has_id && !has_node_name) {
3908 error_setg(errp, "No block device specified");
3909 return;
3910 }
3911
3912 if (has_id) {
Max Reitzefaa7c42016-03-16 19:54:38 +01003913 /* blk_by_name() never returns a BB that is not owned by the monitor */
Alberto Garcia81b936a2015-11-02 16:51:55 +02003914 blk = blk_by_name(id);
3915 if (!blk) {
3916 error_setg(errp, "Cannot find block backend %s", id);
3917 return;
3918 }
Paolo Bonzini5cf87fd2016-03-31 15:07:43 +02003919 if (blk_legacy_dinfo(blk)) {
3920 error_setg(errp, "Deleting block backend added with drive-add"
3921 " is not supported");
3922 return;
3923 }
Alberto Garcia81b936a2015-11-02 16:51:55 +02003924 if (blk_get_refcnt(blk) > 1) {
3925 error_setg(errp, "Block backend %s is in use", id);
3926 return;
3927 }
3928 bs = blk_bs(blk);
3929 aio_context = blk_get_aio_context(blk);
3930 } else {
Kevin Wolf1f0c4612016-03-22 18:38:44 +01003931 blk = NULL;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003932 bs = bdrv_find_node(node_name);
3933 if (!bs) {
3934 error_setg(errp, "Cannot find node %s", node_name);
3935 return;
3936 }
Kevin Wolf1f0c4612016-03-22 18:38:44 +01003937 if (bdrv_has_blk(bs)) {
Alberto Garcia81b936a2015-11-02 16:51:55 +02003938 error_setg(errp, "Node %s is in use by %s",
Kevin Wolf1f0c4612016-03-22 18:38:44 +01003939 node_name, bdrv_get_parent_name(bs));
Alberto Garcia81b936a2015-11-02 16:51:55 +02003940 return;
3941 }
3942 aio_context = bdrv_get_aio_context(bs);
3943 }
3944
3945 aio_context_acquire(aio_context);
3946
3947 if (bs) {
3948 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3949 goto out;
3950 }
3951
Igor Mammedov3b8c1762016-07-25 14:47:12 +02003952 if (!blk && !QTAILQ_IN_USE(bs, monitor_list)) {
Max Reitz9c4218e2016-01-29 16:36:12 +01003953 error_setg(errp, "Node %s is not owned by the monitor",
3954 bs->node_name);
3955 goto out;
3956 }
3957
3958 if (bs->refcnt > 1) {
Alberto Garcia81b936a2015-11-02 16:51:55 +02003959 error_setg(errp, "Block device %s is in use",
3960 bdrv_get_device_or_node_name(bs));
3961 goto out;
3962 }
3963 }
3964
3965 if (blk) {
Max Reitzefaa7c42016-03-16 19:54:38 +01003966 monitor_remove_blk(blk);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003967 blk_unref(blk);
3968 } else {
Max Reitz9c4218e2016-01-29 16:36:12 +01003969 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003970 bdrv_unref(bs);
3971 }
3972
3973out:
3974 aio_context_release(aio_context);
3975}
3976
Wen Congyang7f821592016-05-10 15:36:39 +08003977static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
3978 const char *child_name)
3979{
3980 BdrvChild *child;
3981
3982 QLIST_FOREACH(child, &parent_bs->children, next) {
3983 if (strcmp(child->name, child_name) == 0) {
3984 return child;
3985 }
3986 }
3987
3988 return NULL;
3989}
3990
3991void qmp_x_blockdev_change(const char *parent, bool has_child,
3992 const char *child, bool has_node,
3993 const char *node, Error **errp)
3994{
3995 BlockDriverState *parent_bs, *new_bs = NULL;
3996 BdrvChild *p_child;
3997
3998 parent_bs = bdrv_lookup_bs(parent, parent, errp);
3999 if (!parent_bs) {
4000 return;
4001 }
4002
4003 if (has_child == has_node) {
4004 if (has_child) {
4005 error_setg(errp, "The parameters child and node are in conflict");
4006 } else {
4007 error_setg(errp, "Either child or node must be specified");
4008 }
4009 return;
4010 }
4011
4012 if (has_child) {
4013 p_child = bdrv_find_child(parent_bs, child);
4014 if (!p_child) {
4015 error_setg(errp, "Node '%s' does not have child '%s'",
4016 parent, child);
4017 return;
4018 }
4019 bdrv_del_child(parent_bs, p_child, errp);
4020 }
4021
4022 if (has_node) {
4023 new_bs = bdrv_find_node(node);
4024 if (!new_bs) {
4025 error_setg(errp, "Node '%s' not found", node);
4026 return;
4027 }
4028 bdrv_add_child(parent_bs, new_bs, errp);
4029 }
4030}
4031
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004032BlockJobInfoList *qmp_query_block_jobs(Error **errp)
4033{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004034 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004035 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004036
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004037 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
4038 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
4039 AioContext *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004040
4041 aio_context_acquire(aio_context);
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004042 elem->value = block_job_query(job);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004043 aio_context_release(aio_context);
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004044
4045 *p_next = elem;
4046 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004047 }
4048
4049 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004050}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004051
Kevin Wolf00063832013-03-15 10:35:07 +01004052QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004053 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01004054 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01004055 .desc = {
4056 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004057 .name = "snapshot",
4058 .type = QEMU_OPT_BOOL,
4059 .help = "enable/disable snapshot mode",
4060 },{
Paolo Bonzinia9384af2013-02-08 14:06:12 +01004061 .name = "discard",
4062 .type = QEMU_OPT_STRING,
4063 .help = "discard operation (ignore/off, unmap/on)",
4064 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004065 .name = "aio",
4066 .type = QEMU_OPT_STRING,
4067 .help = "host AIO implementation (threads, native)",
4068 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01004069 .name = BDRV_OPT_CACHE_WB,
4070 .type = QEMU_OPT_BOOL,
4071 .help = "Enable writeback mode",
4072 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004073 .name = "format",
4074 .type = QEMU_OPT_STRING,
4075 .help = "disk format (raw, qcow2, ...)",
4076 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004077 .name = "rerror",
4078 .type = QEMU_OPT_STRING,
4079 .help = "read error action",
4080 },{
4081 .name = "werror",
4082 .type = QEMU_OPT_STRING,
4083 .help = "write error action",
4084 },{
Alberto Garcia4e200cf2016-09-15 17:53:05 +03004085 .name = BDRV_OPT_READ_ONLY,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004086 .type = QEMU_OPT_BOOL,
4087 .help = "open drive file as read-only",
4088 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004089 .name = "throttling.iops-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004090 .type = QEMU_OPT_NUMBER,
4091 .help = "limit total I/O operations per second",
4092 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004093 .name = "throttling.iops-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004094 .type = QEMU_OPT_NUMBER,
4095 .help = "limit read operations per second",
4096 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004097 .name = "throttling.iops-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004098 .type = QEMU_OPT_NUMBER,
4099 .help = "limit write operations per second",
4100 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004101 .name = "throttling.bps-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004102 .type = QEMU_OPT_NUMBER,
4103 .help = "limit total bytes per second",
4104 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004105 .name = "throttling.bps-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004106 .type = QEMU_OPT_NUMBER,
4107 .help = "limit read bytes per second",
4108 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004109 .name = "throttling.bps-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004110 .type = QEMU_OPT_NUMBER,
4111 .help = "limit write bytes per second",
4112 },{
Benoît Canet3e9fab62013-09-02 14:14:40 +02004113 .name = "throttling.iops-total-max",
4114 .type = QEMU_OPT_NUMBER,
4115 .help = "I/O operations burst",
4116 },{
4117 .name = "throttling.iops-read-max",
4118 .type = QEMU_OPT_NUMBER,
4119 .help = "I/O operations read burst",
4120 },{
4121 .name = "throttling.iops-write-max",
4122 .type = QEMU_OPT_NUMBER,
4123 .help = "I/O operations write burst",
4124 },{
4125 .name = "throttling.bps-total-max",
4126 .type = QEMU_OPT_NUMBER,
4127 .help = "total bytes burst",
4128 },{
4129 .name = "throttling.bps-read-max",
4130 .type = QEMU_OPT_NUMBER,
4131 .help = "total bytes read burst",
4132 },{
4133 .name = "throttling.bps-write-max",
4134 .type = QEMU_OPT_NUMBER,
4135 .help = "total bytes write burst",
4136 },{
Alberto Garcia8a0fc182016-02-18 12:27:02 +02004137 .name = "throttling.iops-total-max-length",
4138 .type = QEMU_OPT_NUMBER,
4139 .help = "length of the iops-total-max burst period, in seconds",
4140 },{
4141 .name = "throttling.iops-read-max-length",
4142 .type = QEMU_OPT_NUMBER,
4143 .help = "length of the iops-read-max burst period, in seconds",
4144 },{
4145 .name = "throttling.iops-write-max-length",
4146 .type = QEMU_OPT_NUMBER,
4147 .help = "length of the iops-write-max burst period, in seconds",
4148 },{
4149 .name = "throttling.bps-total-max-length",
4150 .type = QEMU_OPT_NUMBER,
4151 .help = "length of the bps-total-max burst period, in seconds",
4152 },{
4153 .name = "throttling.bps-read-max-length",
4154 .type = QEMU_OPT_NUMBER,
4155 .help = "length of the bps-read-max burst period, in seconds",
4156 },{
4157 .name = "throttling.bps-write-max-length",
4158 .type = QEMU_OPT_NUMBER,
4159 .help = "length of the bps-write-max burst period, in seconds",
4160 },{
Benoît Canet2024c1d2013-09-02 14:14:41 +02004161 .name = "throttling.iops-size",
4162 .type = QEMU_OPT_NUMBER,
4163 .help = "when limiting by iops max size of an I/O in bytes",
4164 },{
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004165 .name = "throttling.group",
4166 .type = QEMU_OPT_STRING,
4167 .help = "name of the block throttling group",
4168 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004169 .name = "copy-on-read",
4170 .type = QEMU_OPT_BOOL,
4171 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004172 },{
4173 .name = "detect-zeroes",
4174 .type = QEMU_OPT_STRING,
4175 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004176 },{
4177 .name = "stats-account-invalid",
4178 .type = QEMU_OPT_BOOL,
4179 .help = "whether to account for invalid I/O operations "
4180 "in the statistics",
4181 },{
4182 .name = "stats-account-failed",
4183 .type = QEMU_OPT_BOOL,
4184 .help = "whether to account for failed I/O operations "
4185 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004186 },
4187 { /* end of list */ }
4188 },
4189};
Kevin Wolf00063832013-03-15 10:35:07 +01004190
Max Reitzbd745e22015-10-19 17:53:32 +02004191static QemuOptsList qemu_root_bds_opts = {
4192 .name = "root-bds",
Kevin Wolf23f7fcb2016-03-15 14:43:14 +01004193 .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head),
Max Reitzbd745e22015-10-19 17:53:32 +02004194 .desc = {
4195 {
4196 .name = "discard",
4197 .type = QEMU_OPT_STRING,
4198 .help = "discard operation (ignore/off, unmap/on)",
4199 },{
Max Reitzbd745e22015-10-19 17:53:32 +02004200 .name = "aio",
4201 .type = QEMU_OPT_STRING,
4202 .help = "host AIO implementation (threads, native)",
4203 },{
Max Reitzbd745e22015-10-19 17:53:32 +02004204 .name = "copy-on-read",
4205 .type = QEMU_OPT_BOOL,
4206 .help = "copy read data from backing file into image file",
4207 },{
4208 .name = "detect-zeroes",
4209 .type = QEMU_OPT_STRING,
4210 .help = "try to optimize zero writes (off, on, unmap)",
4211 },
4212 { /* end of list */ }
4213 },
4214};
4215
Kevin Wolf00063832013-03-15 10:35:07 +01004216QemuOptsList qemu_drive_opts = {
4217 .name = "drive",
4218 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4219 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004220 /*
4221 * no elements => accept any params
4222 * validation will happen later
4223 */
Kevin Wolf00063832013-03-15 10:35:07 +01004224 { /* end of list */ }
4225 },
4226};