blob: 2b2f6ceef0365b2ccdf7df36b8ea040fdb0b29bf [file] [log] [blame]
Markus Armbruster666daa62010-06-02 18:48:27 +02001/*
2 * QEMU host block devices
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
Markus Armbruster3618a092013-03-14 13:59:53 +01008 *
9 * This file incorporates work covered by the following copyright and
10 * permission notice:
11 *
12 * Copyright (c) 2003-2008 Fabrice Bellard
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * THE SOFTWARE.
Markus Armbruster666daa62010-06-02 18:48:27 +020031 */
32
Peter Maydelld38ea872016-01-29 17:50:05 +000033#include "qemu/osdep.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020034#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/blockdev.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010036#include "hw/block/block.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010037#include "block/blockjob.h"
Alberto Garcia76f4afb2015-06-08 18:17:44 +020038#include "block/throttle-groups.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010040#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/option.h"
42#include "qemu/config-file.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010043#include "qapi/qmp/types.h"
Kevin Wolfd26c9a12013-09-23 15:26:03 +020044#include "qapi-visit.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010045#include "qapi/qmp/qerror.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010046#include "qapi/qobject-output-visitor.h"
Peter Lieven9e7dac72014-08-13 19:20:17 +020047#include "qapi/util.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010048#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010049#include "block/block_int.h"
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -020050#include "qmp-commands.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000051#include "block/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
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100230void drive_check_orphaned(void)
John Snowa66c9dc2014-10-01 14:19:24 -0400231{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200232 BlockBackend *blk;
John Snowa66c9dc2014-10-01 14:19:24 -0400233 DriveInfo *dinfo;
Markus Armbruster664cc622017-02-15 11:05:45 +0100234 Location loc;
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100235 bool orphans = false;
John Snowa66c9dc2014-10-01 14:19:24 -0400236
Markus Armbruster18e46a02014-10-07 13:59:06 +0200237 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
238 dinfo = blk_legacy_dinfo(blk);
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200239 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
John Snowa66c9dc2014-10-01 14:19:24 -0400240 dinfo->type != IF_NONE) {
Markus Armbruster664cc622017-02-15 11:05:45 +0100241 loc_push_none(&loc);
242 qemu_opts_loc_restore(dinfo->opts);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100243 error_report("machine type does not support"
Markus Armbruster664cc622017-02-15 11:05:45 +0100244 " if=%s,bus=%d,unit=%d",
245 if_name[dinfo->type], dinfo->bus, dinfo->unit);
246 loc_pop(&loc);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100247 orphans = true;
John Snowa66c9dc2014-10-01 14:19:24 -0400248 }
249 }
250
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100251 if (orphans) {
252 exit(1);
253 }
John Snowa66c9dc2014-10-01 14:19:24 -0400254}
255
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100256DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
257{
258 return drive_get(type,
259 drive_index_to_bus_id(type, index),
260 drive_index_to_unit_id(type, index));
261}
262
Markus Armbruster666daa62010-06-02 18:48:27 +0200263int drive_get_max_bus(BlockInterfaceType type)
264{
265 int max_bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200266 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200267 DriveInfo *dinfo;
268
269 max_bus = -1;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200270 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
271 dinfo = blk_legacy_dinfo(blk);
272 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200273 max_bus = dinfo->bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200274 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200275 }
276 return max_bus;
277}
278
Markus Armbruster13839972011-01-28 11:21:37 +0100279/* Get a block device. This should only be used for single-drive devices
280 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
281 appropriate bus. */
282DriveInfo *drive_get_next(BlockInterfaceType type)
283{
284 static int next_block_unit[IF_COUNT];
285
286 return drive_get(type, 0, next_block_unit[type]++);
287}
288
Markus Armbruster666daa62010-06-02 18:48:27 +0200289static void bdrv_format_print(void *opaque, const char *name)
290{
Markus Armbruster807105a2011-01-17 19:31:27 +0100291 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200292}
293
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000294typedef struct {
295 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800296 BlockDriverState *bs;
297} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000298
Kevin Wolfb6810722013-09-20 11:33:11 +0200299static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200300{
301 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200302 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200303 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200304 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200305 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200306 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200307 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200308 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200309 } else {
Kevin Wolfb6810722013-09-20 11:33:11 +0200310 error_setg(errp, "'%s' invalid %s error action",
311 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200312 return -1;
313 }
314}
315
Alberto Garcia40119ef2015-11-16 11:28:38 +0200316static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
317 Error **errp)
318{
319 const QListEntry *entry;
320 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
321 switch (qobject_type(entry->value)) {
322
323 case QTYPE_QSTRING: {
324 unsigned long long length;
325 const char *str = qstring_get_str(qobject_to_qstring(entry->value));
326 if (parse_uint_full(str, &length, 10) == 0 &&
327 length > 0 && length <= UINT_MAX) {
328 block_acct_add_interval(stats, (unsigned) length);
329 } else {
330 error_setg(errp, "Invalid interval length: %s", str);
331 return false;
332 }
333 break;
334 }
335
336 case QTYPE_QINT: {
337 int64_t length = qint_get_int(qobject_to_qint(entry->value));
338 if (length > 0 && length <= UINT_MAX) {
339 block_acct_add_interval(stats, (unsigned) length);
340 } else {
341 error_setg(errp, "Invalid interval length: %" PRId64, length);
342 return false;
343 }
344 break;
345 }
346
347 default:
348 error_setg(errp, "The specification of stats-intervals is invalid");
349 return false;
350 }
351 }
352 return true;
353}
354
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200355typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
356
Max Reitzfbf81752015-10-19 17:53:31 +0200357/* All parameters but @opts are optional and may be set to NULL. */
358static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
359 const char **throttling_group, ThrottleConfig *throttle_cfg,
360 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
361{
Max Reitzfbf81752015-10-19 17:53:31 +0200362 Error *local_error = NULL;
363 const char *aio;
364
365 if (bdrv_flags) {
Max Reitzfbf81752015-10-19 17:53:31 +0200366 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
367 *bdrv_flags |= BDRV_O_COPY_ON_READ;
368 }
369
Max Reitzfbf81752015-10-19 17:53:31 +0200370 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
371 if (!strcmp(aio, "native")) {
372 *bdrv_flags |= BDRV_O_NATIVE_AIO;
373 } else if (!strcmp(aio, "threads")) {
374 /* this is the default */
375 } else {
376 error_setg(errp, "invalid aio option");
377 return;
378 }
379 }
380 }
381
382 /* disk I/O throttling */
383 if (throttling_group) {
384 *throttling_group = qemu_opt_get(opts, "throttling.group");
385 }
386
387 if (throttle_cfg) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200388 throttle_config_init(throttle_cfg);
Max Reitzfbf81752015-10-19 17:53:31 +0200389 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
390 qemu_opt_get_number(opts, "throttling.bps-total", 0);
391 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
392 qemu_opt_get_number(opts, "throttling.bps-read", 0);
393 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
394 qemu_opt_get_number(opts, "throttling.bps-write", 0);
395 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
396 qemu_opt_get_number(opts, "throttling.iops-total", 0);
397 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
398 qemu_opt_get_number(opts, "throttling.iops-read", 0);
399 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
400 qemu_opt_get_number(opts, "throttling.iops-write", 0);
401
402 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
403 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
404 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
405 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
406 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
407 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
408 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
409 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
410 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
411 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
412 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
413 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
414
Alberto Garcia8a0fc182016-02-18 12:27:02 +0200415 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
416 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
417 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
418 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
419 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
420 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
421 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
422 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
423 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
424 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
425 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
426 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
427
Max Reitzfbf81752015-10-19 17:53:31 +0200428 throttle_cfg->op_size =
429 qemu_opt_get_number(opts, "throttling.iops-size", 0);
430
Alberto Garciad5851082016-02-18 12:26:59 +0200431 if (!throttle_is_valid(throttle_cfg, errp)) {
Max Reitzfbf81752015-10-19 17:53:31 +0200432 return;
433 }
434 }
435
436 if (detect_zeroes) {
437 *detect_zeroes =
438 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
439 qemu_opt_get(opts, "detect-zeroes"),
Eric Blake7fb1cf12015-11-18 01:52:57 -0700440 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Max Reitzfbf81752015-10-19 17:53:31 +0200441 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
442 &local_error);
443 if (local_error) {
444 error_propagate(errp, local_error);
445 return;
446 }
Max Reitzfbf81752015-10-19 17:53:31 +0200447 }
448}
449
Kevin Wolff298d072013-09-10 12:01:20 +0200450/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200451static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
452 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200453{
454 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200455 int bdrv_flags = 0;
456 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200457 bool account_invalid, account_failed;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300458 bool writethrough, read_only;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200459 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200460 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200461 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200462 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100463 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100464 QemuOpts *opts;
Alberto Garcia40119ef2015-11-16 11:28:38 +0200465 QDict *interval_dict = NULL;
466 QList *interval_list = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100467 const char *id;
Max Reitzfbf81752015-10-19 17:53:31 +0200468 BlockdevDetectZeroesOptions detect_zeroes =
469 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
470 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200471
Kevin Wolff298d072013-09-10 12:01:20 +0200472 /* Check common options by copying from bs_opts to opts, all other options
473 * stay in bs_opts for processing by bdrv_open(). */
474 id = qdict_get_try_str(bs_opts, "id");
Kevin Wolf00063832013-03-15 10:35:07 +0100475 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100476 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200477 error_propagate(errp, error);
Markus Armbruster6376f952014-05-28 11:17:01 +0200478 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100479 }
480
Kevin Wolf00063832013-03-15 10:35:07 +0100481 qemu_opts_absorb_qdict(opts, bs_opts, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100482 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200483 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100484 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100485 }
486
487 if (id) {
488 qdict_del(bs_opts, "id");
489 }
490
Markus Armbruster666daa62010-06-02 18:48:27 +0200491 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200492 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200493
Alberto Garcia362e9292015-10-28 17:33:04 +0200494 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
495 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
496
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100497 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
498
Alberto Garciaff356ee2016-07-08 17:03:00 +0300499 id = qemu_opts_id(opts);
500
Alberto Garcia40119ef2015-11-16 11:28:38 +0200501 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
502 qdict_array_split(interval_dict, &interval_list);
503
504 if (qdict_size(interval_dict) != 0) {
505 error_setg(errp, "Invalid option stats-intervals.%s",
506 qdict_first(interval_dict)->key);
507 goto early_err;
508 }
Alberto Garcia2be55062015-10-28 17:33:07 +0200509
Max Reitzfbf81752015-10-19 17:53:31 +0200510 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
511 &detect_zeroes, &error);
512 if (error) {
513 error_propagate(errp, error);
514 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200515 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200516
517 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100518 if (is_help_option(buf)) {
519 error_printf("Supported formats:");
520 bdrv_iterate_format(bdrv_format_print, NULL);
521 error_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100522 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200523 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200524
Max Reitze4342ce2015-02-05 13:58:14 -0500525 if (qdict_haskey(bs_opts, "driver")) {
526 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100527 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400528 }
Max Reitze4342ce2015-02-05 13:58:14 -0500529 qdict_put(bs_opts, "driver", qstring_from_str(buf));
Markus Armbruster666daa62010-06-02 18:48:27 +0200530 }
531
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200532 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200533 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200534 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100535 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200536 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100537 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200538 }
539 }
540
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200541 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200542 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200543 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100544 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200545 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100546 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200547 }
548 }
549
Max Reitz5ec18f82015-10-19 17:53:30 +0200550 if (snapshot) {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200551 bdrv_flags |= BDRV_O_SNAPSHOT;
Max Reitz5ec18f82015-10-19 17:53:30 +0200552 }
553
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300554 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
555
Kevin Wolf326642b2013-07-11 12:52:34 +0200556 /* init */
Kevin Wolf39c4ae92015-11-13 14:45:42 +0100557 if ((!file || !*file) && !qdict_size(bs_opts)) {
Max Reitz5ec18f82015-10-19 17:53:30 +0200558 BlockBackendRootState *blk_rs;
559
Max Reitz109525a2016-05-17 16:41:34 +0200560 blk = blk_new();
Max Reitz5ec18f82015-10-19 17:53:30 +0200561 blk_rs = blk_get_root_state(blk);
562 blk_rs->open_flags = bdrv_flags;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300563 blk_rs->read_only = read_only;
Max Reitz5ec18f82015-10-19 17:53:30 +0200564 blk_rs->detect_zeroes = detect_zeroes;
565
Max Reitze4342ce2015-02-05 13:58:14 -0500566 QDECREF(bs_opts);
567 } else {
568 if (file && !*file) {
569 file = NULL;
570 }
571
Kevin Wolf91a097e2015-05-08 17:49:53 +0200572 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
573 * with other callers) rather than what we want as the real defaults.
574 * Apply the defaults here instead. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200575 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
576 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300577 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
578 read_only ? "on" : "off");
Kevin Wolf72e775c2016-03-15 14:34:37 +0100579 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200580
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100581 if (runstate_check(RUN_STATE_INMIGRATE)) {
582 bdrv_flags |= BDRV_O_INACTIVE;
583 }
584
Max Reitzefaa7c42016-03-16 19:54:38 +0100585 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500586 if (!blk) {
587 goto err_no_bs_opts;
588 }
589 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500590
Max Reitz5ec18f82015-10-19 17:53:30 +0200591 bs->detect_zeroes = detect_zeroes;
592
Max Reitz5ec18f82015-10-19 17:53:30 +0200593 if (bdrv_key_required(bs)) {
594 autostart = 0;
595 }
Alberto Garcia362e9292015-10-28 17:33:04 +0200596
597 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
Alberto Garcia2be55062015-10-28 17:33:07 +0200598
Alberto Garcia40119ef2015-11-16 11:28:38 +0200599 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
600 blk_unref(blk);
601 blk = NULL;
602 goto err_no_bs_opts;
Alberto Garcia2be55062015-10-28 17:33:07 +0200603 }
Max Reitz5ec18f82015-10-19 17:53:30 +0200604 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200605
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100606 /* disk I/O throttling */
607 if (throttle_enabled(&cfg)) {
608 if (!throttling_group) {
Alberto Garciaff356ee2016-07-08 17:03:00 +0300609 throttling_group = id;
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100610 }
611 blk_io_limits_enable(blk, throttling_group);
612 blk_set_io_limits(blk, &cfg);
613 }
614
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100615 blk_set_enable_write_cache(blk, !writethrough);
Max Reitz373340b2015-10-19 17:53:22 +0200616 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200617
Alberto Garciaff356ee2016-07-08 17:03:00 +0300618 if (!monitor_add_blk(blk, id, errp)) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100619 blk_unref(blk);
620 blk = NULL;
621 goto err_no_bs_opts;
622 }
623
Max Reitze4342ce2015-02-05 13:58:14 -0500624err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100625 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200626 QDECREF(interval_dict);
627 QDECREF(interval_list);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200628 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100629
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100630early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100631 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200632 QDECREF(interval_dict);
633 QDECREF(interval_list);
Markus Armbruster6376f952014-05-28 11:17:01 +0200634err_no_opts:
635 QDECREF(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100636 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200637}
638
Max Reitzbd745e22015-10-19 17:53:32 +0200639/* Takes the ownership of bs_opts */
640static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
641{
Max Reitzbd745e22015-10-19 17:53:32 +0200642 int bdrv_flags = 0;
643
Kevin Wolfa81d6162016-03-07 14:23:04 +0100644 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
645 * with other callers) rather than what we want as the real defaults.
646 * Apply the defaults here instead. */
Kevin Wolfa81d6162016-03-07 14:23:04 +0100647 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
648 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300649 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
Kevin Wolfa81d6162016-03-07 14:23:04 +0100650
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100651 if (runstate_check(RUN_STATE_INMIGRATE)) {
652 bdrv_flags |= BDRV_O_INACTIVE;
653 }
654
Kevin Wolf74e1ae72016-09-20 20:48:52 +0200655 return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
Max Reitzbd745e22015-10-19 17:53:32 +0200656}
657
Max Reitz9c4218e2016-01-29 16:36:12 +0100658void blockdev_close_all_bdrv_states(void)
659{
660 BlockDriverState *bs, *next_bs;
661
662 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
663 AioContext *ctx = bdrv_get_aio_context(bs);
664
665 aio_context_acquire(ctx);
666 bdrv_unref(bs);
667 aio_context_release(ctx);
668 }
669}
670
Max Reitz262b4e82016-03-16 19:54:41 +0100671/* Iterates over the list of monitor-owned BlockDriverStates */
672BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
673{
674 return bs ? QTAILQ_NEXT(bs, monitor_list)
675 : QTAILQ_FIRST(&monitor_bdrv_states);
676}
677
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200678static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
679 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200680{
681 const char *value;
682
683 value = qemu_opt_get(opts, from);
684 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200685 if (qemu_opt_find(opts, to)) {
686 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
687 "same time", to, from);
688 return;
689 }
Jun Li20d6cd42014-09-24 13:45:27 +0800690 }
691
692 /* rename all items in opts */
693 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100694 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200695 qemu_opt_unset(opts, from);
696 }
697}
698
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200699QemuOptsList qemu_legacy_drive_opts = {
700 .name = "drive",
701 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
702 .desc = {
703 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200704 .name = "bus",
705 .type = QEMU_OPT_NUMBER,
706 .help = "bus number",
707 },{
708 .name = "unit",
709 .type = QEMU_OPT_NUMBER,
710 .help = "unit number (i.e. lun for scsi)",
711 },{
712 .name = "index",
713 .type = QEMU_OPT_NUMBER,
714 .help = "index number",
715 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200716 .name = "media",
717 .type = QEMU_OPT_STRING,
718 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200719 },{
720 .name = "if",
721 .type = QEMU_OPT_STRING,
722 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200723 },{
724 .name = "cyls",
725 .type = QEMU_OPT_NUMBER,
726 .help = "number of cylinders (ide disk geometry)",
727 },{
728 .name = "heads",
729 .type = QEMU_OPT_NUMBER,
730 .help = "number of heads (ide disk geometry)",
731 },{
732 .name = "secs",
733 .type = QEMU_OPT_NUMBER,
734 .help = "number of sectors (ide disk geometry)",
735 },{
736 .name = "trans",
737 .type = QEMU_OPT_STRING,
738 .help = "chs translation (auto, lba, none)",
Kevin Wolf26929292013-09-09 17:01:03 +0200739 },{
740 .name = "boot",
741 .type = QEMU_OPT_BOOL,
742 .help = "(deprecated, ignored)",
Kevin Wolf394c7d42013-09-13 14:09:17 +0200743 },{
744 .name = "addr",
745 .type = QEMU_OPT_STRING,
746 .help = "pci address (virtio only)",
Max Reitzd095b462013-12-20 19:28:14 +0100747 },{
Kevin Wolfbcf83152014-06-06 15:21:48 +0200748 .name = "serial",
749 .type = QEMU_OPT_STRING,
750 .help = "disk serial number",
751 },{
Max Reitzd095b462013-12-20 19:28:14 +0100752 .name = "file",
753 .type = QEMU_OPT_STRING,
754 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200755 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200756
757 /* Options that are passed on, but have special semantics with -drive */
758 {
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300759 .name = BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200760 .type = QEMU_OPT_BOOL,
761 .help = "open drive file as read-only",
762 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100763 .name = "rerror",
764 .type = QEMU_OPT_STRING,
765 .help = "read error action",
766 },{
767 .name = "werror",
768 .type = QEMU_OPT_STRING,
769 .help = "write error action",
770 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200771 .name = "copy-on-read",
772 .type = QEMU_OPT_BOOL,
773 .help = "copy read data from backing file into image file",
774 },
775
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200776 { /* end of list */ }
777 },
778};
779
Markus Armbruster60e19e02014-06-06 14:50:58 +0200780DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
Kevin Wolf57975222013-07-17 14:41:54 +0200781{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200782 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200783 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200784 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200785 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200786 QemuOpts *legacy_opts;
787 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200788 BlockInterfaceType type;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200789 int cyls, heads, secs, translation;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200790 int max_devs, bus_id, unit_id, index;
Kevin Wolf394c7d42013-09-13 14:09:17 +0200791 const char *devaddr;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100792 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800793 bool read_only = false;
794 bool copy_on_read;
Kevin Wolfbcf83152014-06-06 15:21:48 +0200795 const char *serial;
Max Reitzd095b462013-12-20 19:28:14 +0100796 const char *filename;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200797 Error *local_err = NULL;
Kevin Wolf247147f2014-09-24 16:37:14 +0200798 int i;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200799
Kevin Wolf57975222013-07-17 14:41:54 +0200800 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200801 static const struct {
802 const char *from;
803 const char *to;
804 } opt_renames[] = {
805 { "iops", "throttling.iops-total" },
806 { "iops_rd", "throttling.iops-read" },
807 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200808
Kevin Wolf247147f2014-09-24 16:37:14 +0200809 { "bps", "throttling.bps-total" },
810 { "bps_rd", "throttling.bps-read" },
811 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200812
Kevin Wolf247147f2014-09-24 16:37:14 +0200813 { "iops_max", "throttling.iops-total-max" },
814 { "iops_rd_max", "throttling.iops-read-max" },
815 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200816
Kevin Wolf247147f2014-09-24 16:37:14 +0200817 { "bps_max", "throttling.bps-total-max" },
818 { "bps_rd_max", "throttling.bps-read-max" },
819 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200820
Kevin Wolf247147f2014-09-24 16:37:14 +0200821 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200822
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200823 { "group", "throttling.group" },
824
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300825 { "readonly", BDRV_OPT_READ_ONLY },
Kevin Wolf247147f2014-09-24 16:37:14 +0200826 };
827
828 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200829 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
830 &local_err);
831 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100832 error_report_err(local_err);
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200833 return NULL;
834 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200835 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200836
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200837 value = qemu_opt_get(all_opts, "cache");
838 if (value) {
839 int flags = 0;
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100840 bool writethrough;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200841
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100842 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200843 error_report("invalid cache option");
844 return NULL;
845 }
846
847 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200848 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
849 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100850 !writethrough, &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200851 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200852 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
853 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100854 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200855 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200856 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
857 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100858 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200859 }
860 qemu_opt_unset(all_opts, "cache");
861 }
862
Kevin Wolff298d072013-09-10 12:01:20 +0200863 /* Get a QDict for processing the options */
864 bs_opts = qdict_new();
865 qemu_opts_to_qdict(all_opts, bs_opts);
866
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800867 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
868 &error_abort);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200869 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100870 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100871 error_report_err(local_err);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200872 goto fail;
873 }
874
Kevin Wolf26929292013-09-09 17:01:03 +0200875 /* Deprecated option boot=[on|off] */
876 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
877 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
878 "ignored. Future versions will reject this parameter. Please "
879 "update your scripts.\n");
880 }
881
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200882 /* Media type */
883 value = qemu_opt_get(legacy_opts, "media");
884 if (value) {
885 if (!strcmp(value, "disk")) {
886 media = MEDIA_DISK;
887 } else if (!strcmp(value, "cdrom")) {
888 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800889 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200890 } else {
891 error_report("'%s' invalid media", value);
892 goto fail;
893 }
894 }
895
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200896 /* copy-on-read is disabled with a warning for read-only devices */
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300897 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200898 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
899
900 if (read_only && copy_on_read) {
901 error_report("warning: disabling copy-on-read on read-only drive");
902 copy_on_read = false;
903 }
904
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300905 qdict_put(bs_opts, BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200906 qstring_from_str(read_only ? "on" : "off"));
907 qdict_put(bs_opts, "copy-on-read",
908 qstring_from_str(copy_on_read ? "on" :"off"));
909
Kevin Wolf593d4642013-08-28 17:24:51 +0200910 /* Controller type */
911 value = qemu_opt_get(legacy_opts, "if");
912 if (value) {
913 for (type = 0;
914 type < IF_COUNT && strcmp(value, if_name[type]);
915 type++) {
916 }
917 if (type == IF_COUNT) {
918 error_report("unsupported bus type '%s'", value);
919 goto fail;
920 }
921 } else {
922 type = block_default_type;
923 }
924
Kevin Wolfb41a7332013-09-09 16:49:49 +0200925 /* Geometry */
926 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
927 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
928 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
929
930 if (cyls || heads || secs) {
931 if (cyls < 1) {
932 error_report("invalid physical cyls number");
933 goto fail;
934 }
935 if (heads < 1) {
936 error_report("invalid physical heads number");
937 goto fail;
938 }
939 if (secs < 1) {
940 error_report("invalid physical secs number");
941 goto fail;
942 }
943 }
944
945 translation = BIOS_ATA_TRANSLATION_AUTO;
946 value = qemu_opt_get(legacy_opts, "trans");
947 if (value != NULL) {
948 if (!cyls) {
949 error_report("'%s' trans must be used with cyls, heads and secs",
950 value);
951 goto fail;
952 }
953 if (!strcmp(value, "none")) {
954 translation = BIOS_ATA_TRANSLATION_NONE;
955 } else if (!strcmp(value, "lba")) {
956 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +0100957 } else if (!strcmp(value, "large")) {
958 translation = BIOS_ATA_TRANSLATION_LARGE;
959 } else if (!strcmp(value, "rechs")) {
960 translation = BIOS_ATA_TRANSLATION_RECHS;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200961 } else if (!strcmp(value, "auto")) {
962 translation = BIOS_ATA_TRANSLATION_AUTO;
963 } else {
964 error_report("'%s' invalid translation type", value);
965 goto fail;
966 }
967 }
968
969 if (media == MEDIA_CDROM) {
970 if (cyls || secs || heads) {
971 error_report("CHS can't be set with media=cdrom");
972 goto fail;
973 }
974 }
975
Kevin Wolf87a899c2013-09-10 15:48:13 +0200976 /* Device address specified by bus/unit or index.
977 * If none was specified, try to find the first free one. */
978 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
979 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
980 index = qemu_opt_get_number(legacy_opts, "index", -1);
981
982 max_devs = if_max_devs[type];
983
984 if (index != -1) {
985 if (bus_id != 0 || unit_id != -1) {
986 error_report("index cannot be used with bus and unit");
987 goto fail;
988 }
989 bus_id = drive_index_to_bus_id(type, index);
990 unit_id = drive_index_to_unit_id(type, index);
991 }
992
993 if (unit_id == -1) {
994 unit_id = 0;
995 while (drive_get(type, bus_id, unit_id) != NULL) {
996 unit_id++;
997 if (max_devs && unit_id >= max_devs) {
998 unit_id -= max_devs;
999 bus_id++;
1000 }
1001 }
1002 }
1003
1004 if (max_devs && unit_id >= max_devs) {
1005 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1006 goto fail;
1007 }
1008
1009 if (drive_get(type, bus_id, unit_id) != NULL) {
1010 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1011 bus_id, unit_id, index);
1012 goto fail;
1013 }
1014
Kevin Wolfbcf83152014-06-06 15:21:48 +02001015 /* Serial number */
1016 serial = qemu_opt_get(legacy_opts, "serial");
1017
Kevin Wolf87a899c2013-09-10 15:48:13 +02001018 /* no id supplied -> create one */
1019 if (qemu_opts_id(all_opts) == NULL) {
1020 char *new_id;
1021 const char *mediastr = "";
1022 if (type == IF_IDE || type == IF_SCSI) {
1023 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1024 }
1025 if (max_devs) {
1026 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1027 mediastr, unit_id);
1028 } else {
1029 new_id = g_strdup_printf("%s%s%i", if_name[type],
1030 mediastr, unit_id);
1031 }
1032 qdict_put(bs_opts, "id", qstring_from_str(new_id));
1033 g_free(new_id);
1034 }
1035
Kevin Wolf394c7d42013-09-13 14:09:17 +02001036 /* Add virtio block device */
1037 devaddr = qemu_opt_get(legacy_opts, "addr");
1038 if (devaddr && type != IF_VIRTIO) {
1039 error_report("addr is not supported by this bus type");
1040 goto fail;
1041 }
1042
1043 if (type == IF_VIRTIO) {
1044 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001045 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1046 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001047 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +02001048 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001049 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001050 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001051 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001052 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1053 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001054 if (devaddr) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001055 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001056 }
1057 }
1058
Max Reitzd095b462013-12-20 19:28:14 +01001059 filename = qemu_opt_get(legacy_opts, "file");
1060
Kevin Wolfee13ed12014-02-09 09:52:32 +01001061 /* Check werror/rerror compatibility with if=... */
1062 werror = qemu_opt_get(legacy_opts, "werror");
1063 if (werror != NULL) {
1064 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1065 type != IF_NONE) {
1066 error_report("werror is not supported by this bus type");
1067 goto fail;
1068 }
1069 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1070 }
1071
1072 rerror = qemu_opt_get(legacy_opts, "rerror");
1073 if (rerror != NULL) {
1074 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1075 type != IF_NONE) {
1076 error_report("rerror is not supported by this bus type");
1077 goto fail;
1078 }
1079 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1080 }
1081
Kevin Wolf2d246f02013-09-18 15:14:47 +02001082 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster18e46a02014-10-07 13:59:06 +02001083 blk = blockdev_init(filename, bs_opts, &local_err);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001084 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +02001085 if (!blk) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001086 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001087 error_report_err(local_err);
Kevin Wolfb6810722013-09-20 11:33:11 +02001088 }
Kevin Wolf2d246f02013-09-18 15:14:47 +02001089 goto fail;
Kevin Wolfb6810722013-09-20 11:33:11 +02001090 } else {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001091 assert(!local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001092 }
1093
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001094 /* Create legacy DriveInfo */
1095 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001096 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001097
Kevin Wolfb41a7332013-09-09 16:49:49 +02001098 dinfo->cyls = cyls;
1099 dinfo->heads = heads;
1100 dinfo->secs = secs;
1101 dinfo->trans = translation;
1102
Kevin Wolfee13ed12014-02-09 09:52:32 +01001103 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001104 dinfo->bus = bus_id;
1105 dinfo->unit = unit_id;
Kevin Wolf394c7d42013-09-13 14:09:17 +02001106 dinfo->devaddr = devaddr;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001107 dinfo->serial = g_strdup(serial);
1108
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001109 blk_set_legacy_dinfo(blk, dinfo);
1110
Kevin Wolfe34ef042013-09-19 14:24:10 +02001111 switch(type) {
1112 case IF_IDE:
1113 case IF_SCSI:
1114 case IF_XEN:
1115 case IF_NONE:
1116 dinfo->media_cd = media == MEDIA_CDROM;
1117 break;
1118 default:
1119 break;
1120 }
1121
Kevin Wolf2d246f02013-09-18 15:14:47 +02001122fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001123 qemu_opts_del(legacy_opts);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001124 QDECREF(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001125 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001126}
1127
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02001128static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1129{
1130 BlockDriverState *bs;
1131
1132 bs = bdrv_lookup_bs(name, name, errp);
1133 if (bs == NULL) {
1134 return NULL;
1135 }
1136
1137 if (!bdrv_is_root_node(bs)) {
1138 error_setg(errp, "Need a root block node");
1139 return NULL;
1140 }
1141
1142 if (!bdrv_is_inserted(bs)) {
1143 error_setg(errp, "Device has no medium");
1144 return NULL;
1145 }
1146
1147 return bs;
1148}
1149
Kevin Wolfb33945c2016-09-20 13:38:43 +02001150static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id,
1151 Error **errp)
1152{
1153 BlockBackend *blk;
1154
1155 if (!blk_name == !qdev_id) {
1156 error_setg(errp, "Need exactly one of 'device' and 'id'");
1157 return NULL;
1158 }
1159
1160 if (qdev_id) {
1161 blk = blk_by_qdev_id(qdev_id, errp);
1162 } else {
1163 blk = blk_by_name(blk_name);
1164 if (blk == NULL) {
1165 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1166 "Device '%s' not found", blk_name);
1167 }
1168 }
1169
1170 return blk;
1171}
1172
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001173void hmp_commit(Monitor *mon, const QDict *qdict)
Markus Armbruster666daa62010-06-02 18:48:27 +02001174{
Markus Armbruster666daa62010-06-02 18:48:27 +02001175 const char *device = qdict_get_str(qdict, "device");
Fam Zhenga0e85442015-03-02 19:36:48 +08001176 BlockBackend *blk;
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001177 int ret;
Markus Armbruster666daa62010-06-02 18:48:27 +02001178
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001179 if (!strcmp(device, "all")) {
Max Reitzda31d592016-03-16 19:54:32 +01001180 ret = blk_commit_all();
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001181 } else {
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001182 BlockDriverState *bs;
1183 AioContext *aio_context;
1184
Fam Zhenga0e85442015-03-02 19:36:48 +08001185 blk = blk_by_name(device);
1186 if (!blk) {
Jeff Cody58513bd2013-01-18 12:45:35 -05001187 monitor_printf(mon, "Device '%s' not found\n", device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +02001188 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001189 }
Max Reitz5433c242015-10-19 17:53:29 +02001190 if (!blk_is_available(blk)) {
1191 monitor_printf(mon, "Device '%s' has no medium\n", device);
1192 return;
1193 }
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001194
1195 bs = blk_bs(blk);
1196 aio_context = bdrv_get_aio_context(bs);
1197 aio_context_acquire(aio_context);
1198
1199 ret = bdrv_commit(bs);
1200
1201 aio_context_release(aio_context);
Jeff Cody58513bd2013-01-18 12:45:35 -05001202 }
1203 if (ret < 0) {
1204 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1205 strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +02001206 }
1207}
1208
Eric Blake10f75902016-03-03 09:16:50 -07001209static void blockdev_do_action(TransactionAction *action, Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001210{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001211 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001212
Eric Blake10f75902016-03-03 09:16:50 -07001213 list.value = action;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001214 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001215 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001216}
1217
Benoît Canet0901f672014-01-23 21:31:38 +01001218void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1219 bool has_node_name, const char *node_name,
1220 const char *snapshot_file,
1221 bool has_snapshot_node_name,
1222 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001223 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001224 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001225{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001226 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001227 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001228 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001229 .has_node_name = has_node_name,
1230 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001231 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001232 .has_snapshot_node_name = has_snapshot_node_name,
1233 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001234 .has_format = has_format,
1235 .format = (char *) format,
1236 .has_mode = has_mode,
1237 .mode = mode,
1238 };
Eric Blake10f75902016-03-03 09:16:50 -07001239 TransactionAction action = {
1240 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001241 .u.blockdev_snapshot_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001242 };
1243 blockdev_do_action(&action, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001244}
1245
Alberto Garcia43de7e22015-10-26 14:27:16 +02001246void qmp_blockdev_snapshot(const char *node, const char *overlay,
1247 Error **errp)
1248{
1249 BlockdevSnapshot snapshot_data = {
1250 .node = (char *) node,
1251 .overlay = (char *) overlay
1252 };
Eric Blake10f75902016-03-03 09:16:50 -07001253 TransactionAction action = {
1254 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
Eric Blake32bafa82016-03-17 16:48:37 -06001255 .u.blockdev_snapshot.data = &snapshot_data,
Eric Blake10f75902016-03-03 09:16:50 -07001256 };
1257 blockdev_do_action(&action, errp);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001258}
1259
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001260void qmp_blockdev_snapshot_internal_sync(const char *device,
1261 const char *name,
1262 Error **errp)
1263{
1264 BlockdevSnapshotInternal snapshot = {
1265 .device = (char *) device,
1266 .name = (char *) name
1267 };
Eric Blake10f75902016-03-03 09:16:50 -07001268 TransactionAction action = {
1269 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001270 .u.blockdev_snapshot_internal_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001271 };
1272 blockdev_do_action(&action, errp);
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001273}
1274
Wenchao Xia44e3e052013-09-11 14:04:36 +08001275SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1276 bool has_id,
1277 const char *id,
1278 bool has_name,
1279 const char *name,
1280 Error **errp)
1281{
Fam Zhenga0e85442015-03-02 19:36:48 +08001282 BlockDriverState *bs;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001283 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001284 QEMUSnapshotInfo sn;
1285 Error *local_err = NULL;
1286 SnapshotInfo *info = NULL;
1287 int ret;
1288
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001289 bs = qmp_get_root_bs(device, errp);
1290 if (!bs) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001291 return NULL;
1292 }
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001293 aio_context = bdrv_get_aio_context(bs);
Max Reitz5433c242015-10-19 17:53:29 +02001294 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001295
1296 if (!has_id) {
1297 id = NULL;
1298 }
1299
1300 if (!has_name) {
1301 name = NULL;
1302 }
1303
1304 if (!id && !name) {
1305 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001306 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001307 }
1308
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001309 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1310 goto out_aio_context;
1311 }
1312
Wenchao Xia44e3e052013-09-11 14:04:36 +08001313 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001314 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001315 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001316 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001317 }
1318 if (!ret) {
1319 error_setg(errp,
1320 "Snapshot with id '%s' and name '%s' does not exist on "
1321 "device '%s'",
1322 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001323 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001324 }
1325
1326 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001327 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001328 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001329 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001330 }
1331
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001332 aio_context_release(aio_context);
1333
Markus Armbruster5839e532014-08-19 10:31:08 +02001334 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001335 info->id = g_strdup(sn.id_str);
1336 info->name = g_strdup(sn.name);
1337 info->date_nsec = sn.date_nsec;
1338 info->date_sec = sn.date_sec;
1339 info->vm_state_size = sn.vm_state_size;
1340 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1341 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1342
1343 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001344
1345out_aio_context:
1346 aio_context_release(aio_context);
1347 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001348}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001349
John Snow341ebc22015-04-17 19:49:52 -04001350/**
1351 * block_dirty_bitmap_lookup:
1352 * Return a dirty bitmap (if present), after validating
1353 * the node reference and bitmap names.
1354 *
1355 * @node: The name of the BDS node to search for bitmaps
1356 * @name: The name of the bitmap to search for
1357 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1358 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1359 * @errp: Output pointer for error information. Can be NULL.
1360 *
1361 * @return: A bitmap object on success, or NULL on failure.
1362 */
1363static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1364 const char *name,
1365 BlockDriverState **pbs,
1366 AioContext **paio,
1367 Error **errp)
1368{
1369 BlockDriverState *bs;
1370 BdrvDirtyBitmap *bitmap;
1371 AioContext *aio_context;
1372
1373 if (!node) {
1374 error_setg(errp, "Node cannot be NULL");
1375 return NULL;
1376 }
1377 if (!name) {
1378 error_setg(errp, "Bitmap name cannot be NULL");
1379 return NULL;
1380 }
1381 bs = bdrv_lookup_bs(node, node, NULL);
1382 if (!bs) {
1383 error_setg(errp, "Node '%s' not found", node);
1384 return NULL;
1385 }
1386
1387 aio_context = bdrv_get_aio_context(bs);
1388 aio_context_acquire(aio_context);
1389
1390 bitmap = bdrv_find_dirty_bitmap(bs, name);
1391 if (!bitmap) {
1392 error_setg(errp, "Dirty bitmap '%s' not found", name);
1393 goto fail;
1394 }
1395
1396 if (pbs) {
1397 *pbs = bs;
1398 }
1399 if (paio) {
1400 *paio = aio_context;
1401 } else {
1402 aio_context_release(aio_context);
1403 }
1404
1405 return bitmap;
1406
1407 fail:
1408 aio_context_release(aio_context);
1409 return NULL;
1410}
1411
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001412/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001413
John Snow50f43f02015-11-05 18:13:09 -05001414typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001415
John Snow50f43f02015-11-05 18:13:09 -05001416/**
1417 * BlkActionOps:
1418 * Table of operations that define an Action.
1419 *
1420 * @instance_size: Size of state struct, in bytes.
1421 * @prepare: Prepare the work, must NOT be NULL.
1422 * @commit: Commit the changes, can be NULL.
1423 * @abort: Abort the changes on fail, can be NULL.
1424 * @clean: Clean up resources after all transaction actions have called
1425 * commit() or abort(). Can be NULL.
1426 *
1427 * Only prepare() may fail. In a single transaction, only one of commit() or
1428 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001429 */
John Snow50f43f02015-11-05 18:13:09 -05001430typedef struct BlkActionOps {
1431 size_t instance_size;
1432 void (*prepare)(BlkActionState *common, Error **errp);
1433 void (*commit)(BlkActionState *common);
1434 void (*abort)(BlkActionState *common);
1435 void (*clean)(BlkActionState *common);
1436} BlkActionOps;
1437
1438/**
1439 * BlkActionState:
1440 * Describes one Action's state within a Transaction.
1441 *
1442 * @action: QAPI-defined enum identifying which Action to perform.
1443 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001444 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001445 * @entry: List membership for all Actions in this Transaction.
1446 *
1447 * This structure must be arranged as first member in a subclassed type,
1448 * assuming that the compiler will also arrange it to the same offsets as the
1449 * base class.
1450 */
1451struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001452 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001453 const BlkActionOps *ops;
John Snow94d16a62015-11-05 18:13:18 -05001454 BlockJobTxn *block_job_txn;
1455 TransactionProperties *txn_props;
John Snow50f43f02015-11-05 18:13:09 -05001456 QSIMPLEQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001457};
1458
Wenchao Xiabbe86012013-09-11 14:04:34 +08001459/* internal snapshot private data */
1460typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001461 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001462 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001463 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001464 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001465 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001466} InternalSnapshotState;
1467
John Snow94d16a62015-11-05 18:13:18 -05001468
1469static int action_check_completion_mode(BlkActionState *s, Error **errp)
1470{
1471 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1472 error_setg(errp,
1473 "Action '%s' does not support Transaction property "
1474 "completion-mode = %s",
1475 TransactionActionKind_lookup[s->action->type],
1476 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1477 return -1;
1478 }
1479 return 0;
1480}
1481
John Snow50f43f02015-11-05 18:13:09 -05001482static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001483 Error **errp)
1484{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001485 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001486 const char *device;
1487 const char *name;
1488 BlockDriverState *bs;
1489 QEMUSnapshotInfo old_sn, *sn;
1490 bool ret;
1491 qemu_timeval tv;
1492 BlockdevSnapshotInternal *internal;
1493 InternalSnapshotState *state;
1494 int ret1;
1495
Eric Blake6a8f9662015-10-26 16:34:54 -06001496 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001497 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001498 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001499 state = DO_UPCAST(InternalSnapshotState, common, common);
1500
1501 /* 1. parse input */
1502 device = internal->device;
1503 name = internal->name;
1504
1505 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001506 if (action_check_completion_mode(common, errp) < 0) {
1507 return;
1508 }
1509
Kevin Wolf75dfd402016-06-23 14:20:24 +02001510 bs = qmp_get_root_bs(device, errp);
1511 if (!bs) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001512 return;
1513 }
1514
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001515 /* AioContext is released in .clean() */
Kevin Wolf75dfd402016-06-23 14:20:24 +02001516 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001517 aio_context_acquire(state->aio_context);
1518
Fam Zheng507306c2015-10-23 11:08:13 +08001519 state->bs = bs;
1520 bdrv_drained_begin(bs);
1521
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001522 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1523 return;
1524 }
1525
Wenchao Xiabbe86012013-09-11 14:04:34 +08001526 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001527 error_setg(errp, "Device '%s' is read only", device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001528 return;
1529 }
1530
1531 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001532 error_setg(errp, "Block format '%s' used by device '%s' "
1533 "does not support internal snapshots",
1534 bs->drv->format_name, device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001535 return;
1536 }
1537
1538 if (!strlen(name)) {
1539 error_setg(errp, "Name is empty");
1540 return;
1541 }
1542
1543 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001544 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1545 &local_err);
1546 if (local_err) {
1547 error_propagate(errp, local_err);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001548 return;
1549 } else if (ret) {
1550 error_setg(errp,
1551 "Snapshot with name '%s' already exists on device '%s'",
1552 name, device);
1553 return;
1554 }
1555
1556 /* 3. take the snapshot */
1557 sn = &state->sn;
1558 pstrcpy(sn->name, sizeof(sn->name), name);
1559 qemu_gettimeofday(&tv);
1560 sn->date_sec = tv.tv_sec;
1561 sn->date_nsec = tv.tv_usec * 1000;
1562 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1563
1564 ret1 = bdrv_snapshot_create(bs, sn);
1565 if (ret1 < 0) {
1566 error_setg_errno(errp, -ret1,
1567 "Failed to create snapshot '%s' on device '%s'",
1568 name, device);
1569 return;
1570 }
1571
1572 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001573 state->created = true;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001574}
1575
John Snow50f43f02015-11-05 18:13:09 -05001576static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001577{
1578 InternalSnapshotState *state =
1579 DO_UPCAST(InternalSnapshotState, common, common);
1580 BlockDriverState *bs = state->bs;
1581 QEMUSnapshotInfo *sn = &state->sn;
1582 Error *local_error = NULL;
1583
Fam Zheng507306c2015-10-23 11:08:13 +08001584 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001585 return;
1586 }
1587
1588 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001589 error_reportf_err(local_error,
1590 "Failed to delete snapshot with id '%s' and "
1591 "name '%s' on device '%s' in abort: ",
1592 sn->id_str, sn->name,
1593 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001594 }
1595}
1596
John Snow50f43f02015-11-05 18:13:09 -05001597static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001598{
1599 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1600 common, common);
1601
1602 if (state->aio_context) {
Fam Zheng507306c2015-10-23 11:08:13 +08001603 if (state->bs) {
1604 bdrv_drained_end(state->bs);
1605 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001606 aio_context_release(state->aio_context);
1607 }
1608}
1609
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001610/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001611typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001612 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001613 BlockDriverState *old_bs;
1614 BlockDriverState *new_bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001615 AioContext *aio_context;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001616} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001617
John Snow50f43f02015-11-05 18:13:09 -05001618static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001619 Error **errp)
1620{
Max Reitz5b363932016-05-17 16:41:31 +02001621 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001622 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001623 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001624 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001625 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001626 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001627 /* Reference to the new image (for 'blockdev-snapshot') */
1628 const char *snapshot_ref;
1629 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001630 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001631 ExternalSnapshotState *state =
1632 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001633 TransactionAction *action = common->action;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001634
Alberto Garcia43de7e22015-10-26 14:27:16 +02001635 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1636 * purpose but a different set of parameters */
1637 switch (action->type) {
1638 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1639 {
Eric Blake32bafa82016-03-17 16:48:37 -06001640 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001641 device = s->node;
1642 node_name = s->node;
1643 new_image_file = NULL;
1644 snapshot_ref = s->overlay;
1645 }
1646 break;
1647 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1648 {
Eric Blake32bafa82016-03-17 16:48:37 -06001649 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001650 device = s->has_device ? s->device : NULL;
1651 node_name = s->has_node_name ? s->node_name : NULL;
1652 new_image_file = s->snapshot_file;
1653 snapshot_ref = NULL;
1654 }
1655 break;
1656 default:
1657 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001658 }
1659
1660 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001661 if (action_check_completion_mode(common, errp) < 0) {
1662 return;
1663 }
1664
Alberto Garcia43de7e22015-10-26 14:27:16 +02001665 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1666 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001667 return;
1668 }
1669
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001670 /* Acquire AioContext now so any threads operating on old_bs stop */
1671 state->aio_context = bdrv_get_aio_context(state->old_bs);
1672 aio_context_acquire(state->aio_context);
Fam Zhengda763e82015-10-23 11:08:10 +08001673 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001674
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001675 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001676 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001677 return;
1678 }
1679
Fam Zheng3718d8a2014-05-23 21:29:43 +08001680 if (bdrv_op_is_blocked(state->old_bs,
1681 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001682 return;
1683 }
1684
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001685 if (!bdrv_is_read_only(state->old_bs)) {
1686 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001687 error_setg(errp, QERR_IO_ERROR);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001688 return;
1689 }
1690 }
1691
Benoît Canet212a5a82014-01-23 21:31:36 +01001692 if (!bdrv_is_first_non_filter(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001693 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
Benoît Canetf6186f42013-10-02 14:33:48 +02001694 return;
1695 }
1696
Alberto Garcia43de7e22015-10-26 14:27:16 +02001697 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001698 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001699 const char *format = s->has_format ? s->format : "qcow2";
1700 enum NewImageMode mode;
1701 const char *snapshot_node_name =
1702 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001703
Alberto Garcia43de7e22015-10-26 14:27:16 +02001704 if (node_name && !snapshot_node_name) {
1705 error_setg(errp, "New snapshot node name missing");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001706 return;
1707 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001708
1709 if (snapshot_node_name &&
1710 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1711 error_setg(errp, "New snapshot node name already in use");
1712 return;
1713 }
1714
1715 flags = state->old_bs->open_flags;
Kevin Wolf4c844982016-02-29 13:12:26 +01001716 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001717
1718 /* create new image w/backing file */
1719 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1720 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001721 int64_t size = bdrv_getlength(state->old_bs);
1722 if (size < 0) {
1723 error_setg_errno(errp, -size, "bdrv_getlength failed");
1724 return;
1725 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001726 bdrv_img_create(new_image_file, format,
1727 state->old_bs->filename,
1728 state->old_bs->drv->format_name,
Kevin Wolff86b8b52016-03-02 12:16:44 +01001729 NULL, size, flags, &local_err, false);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001730 if (local_err) {
1731 error_propagate(errp, local_err);
1732 return;
1733 }
1734 }
1735
1736 options = qdict_new();
1737 if (s->has_snapshot_node_name) {
1738 qdict_put(options, "node-name",
1739 qstring_from_str(snapshot_node_name));
1740 }
1741 qdict_put(options, "driver", qstring_from_str(format));
1742
1743 flags |= BDRV_O_NO_BACKING;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001744 }
1745
Max Reitz5b363932016-05-17 16:41:31 +02001746 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1747 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001748 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001749 if (!state->new_bs) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001750 return;
1751 }
1752
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001753 if (bdrv_has_blk(state->new_bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02001754 error_setg(errp, "The snapshot is already in use");
Alberto Garcia43de7e22015-10-26 14:27:16 +02001755 return;
1756 }
1757
1758 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1759 errp)) {
1760 return;
1761 }
1762
1763 if (state->new_bs->backing != NULL) {
1764 error_setg(errp, "The snapshot already has a backing image");
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001765 return;
1766 }
1767
1768 if (!state->new_bs->drv->supports_backing) {
1769 error_setg(errp, "The snapshot does not support backing images");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001770 }
1771}
1772
John Snow50f43f02015-11-05 18:13:09 -05001773static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001774{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001775 ExternalSnapshotState *state =
1776 DO_UPCAST(ExternalSnapshotState, common, common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001777
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001778 bdrv_set_aio_context(state->new_bs, state->aio_context);
1779
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001780 /* This removes our old bs and adds the new bs */
1781 bdrv_append(state->new_bs, state->old_bs);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001782 /* We don't need (or want) to use the transactional
1783 * bdrv_reopen_multiple() across all the entries at once, because we
1784 * don't want to abort all of them if one of them fails the reopen */
Kevin Wolf4c844982016-02-29 13:12:26 +01001785 if (!state->old_bs->copy_on_read) {
1786 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1787 NULL);
1788 }
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001789}
1790
John Snow50f43f02015-11-05 18:13:09 -05001791static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001792{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001793 ExternalSnapshotState *state =
1794 DO_UPCAST(ExternalSnapshotState, common, common);
1795 if (state->new_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001796 bdrv_unref(state->new_bs);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001797 }
Fam Zhengda763e82015-10-23 11:08:10 +08001798}
1799
John Snow50f43f02015-11-05 18:13:09 -05001800static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001801{
1802 ExternalSnapshotState *state =
1803 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001804 if (state->aio_context) {
Fam Zhengda763e82015-10-23 11:08:10 +08001805 bdrv_drained_end(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001806 aio_context_release(state->aio_context);
1807 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001808}
1809
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001810typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001811 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001812 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001813 AioContext *aio_context;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001814 BlockJob *job;
1815} DriveBackupState;
1816
John Snow111049a2016-11-08 01:50:38 -05001817static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
Pavel Butsykin81206a82016-07-22 11:17:50 +03001818 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001819
John Snow50f43f02015-11-05 18:13:09 -05001820static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001821{
1822 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001823 BlockDriverState *bs;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001824 DriveBackup *backup;
1825 Error *local_err = NULL;
1826
Eric Blake6a8f9662015-10-26 16:34:54 -06001827 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001828 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001829
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001830 bs = qmp_get_root_bs(backup->device, errp);
1831 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001832 return;
1833 }
1834
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001835 /* AioContext is released in .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001836 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001837 aio_context_acquire(state->aio_context);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001838 bdrv_drained_begin(bs);
1839 state->bs = bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001840
John Snow111049a2016-11-08 01:50:38 -05001841 state->job = do_drive_backup(backup, common->block_job_txn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001842 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001843 error_propagate(errp, local_err);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001844 return;
1845 }
John Snow111049a2016-11-08 01:50:38 -05001846}
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001847
John Snow111049a2016-11-08 01:50:38 -05001848static void drive_backup_commit(BlkActionState *common)
1849{
1850 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1851 assert(state->job);
1852 block_job_start(state->job);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001853}
1854
John Snow50f43f02015-11-05 18:13:09 -05001855static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001856{
1857 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001858
John Snow111049a2016-11-08 01:50:38 -05001859 if (state->job) {
1860 block_job_cancel_sync(state->job);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001861 }
1862}
1863
John Snow50f43f02015-11-05 18:13:09 -05001864static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001865{
1866 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1867
1868 if (state->aio_context) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001869 bdrv_drained_end(state->bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001870 aio_context_release(state->aio_context);
1871 }
1872}
1873
Fam Zhengbd8baec2014-12-18 18:37:06 +08001874typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001875 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001876 BlockDriverState *bs;
1877 BlockJob *job;
1878 AioContext *aio_context;
1879} BlockdevBackupState;
1880
John Snow111049a2016-11-08 01:50:38 -05001881static BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
1882 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001883
John Snow50f43f02015-11-05 18:13:09 -05001884static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001885{
1886 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1887 BlockdevBackup *backup;
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001888 BlockDriverState *bs, *target;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001889 Error *local_err = NULL;
1890
Eric Blake6a8f9662015-10-26 16:34:54 -06001891 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001892 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001893
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001894 bs = qmp_get_root_bs(backup->device, errp);
1895 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001896 return;
1897 }
1898
Kevin Wolf39d990a2016-08-02 19:22:04 +02001899 target = bdrv_lookup_bs(backup->target, backup->target, errp);
Max Reitz5433c242015-10-19 17:53:29 +02001900 if (!target) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001901 return;
1902 }
1903
1904 /* AioContext is released in .clean() */
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001905 state->aio_context = bdrv_get_aio_context(bs);
Kevin Wolf39d990a2016-08-02 19:22:04 +02001906 if (state->aio_context != bdrv_get_aio_context(target)) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001907 state->aio_context = NULL;
1908 error_setg(errp, "Backup between two IO threads is not implemented");
1909 return;
1910 }
1911 aio_context_acquire(state->aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001912 state->bs = bs;
Fam Zhengff52bf32015-10-23 11:08:12 +08001913 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001914
John Snow111049a2016-11-08 01:50:38 -05001915 state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001916 if (local_err) {
1917 error_propagate(errp, local_err);
1918 return;
1919 }
John Snow111049a2016-11-08 01:50:38 -05001920}
Fam Zhengbd8baec2014-12-18 18:37:06 +08001921
John Snow111049a2016-11-08 01:50:38 -05001922static void blockdev_backup_commit(BlkActionState *common)
1923{
1924 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1925 assert(state->job);
1926 block_job_start(state->job);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001927}
1928
John Snow50f43f02015-11-05 18:13:09 -05001929static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001930{
1931 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001932
John Snow111049a2016-11-08 01:50:38 -05001933 if (state->job) {
1934 block_job_cancel_sync(state->job);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001935 }
1936}
1937
John Snow50f43f02015-11-05 18:13:09 -05001938static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001939{
1940 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1941
1942 if (state->aio_context) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001943 bdrv_drained_end(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001944 aio_context_release(state->aio_context);
1945 }
1946}
1947
Fam Zhengdf9a6812015-11-09 18:16:54 +08001948typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05001949 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001950 BdrvDirtyBitmap *bitmap;
1951 BlockDriverState *bs;
1952 AioContext *aio_context;
1953 HBitmap *backup;
1954 bool prepared;
1955} BlockDirtyBitmapState;
1956
John Snow50f43f02015-11-05 18:13:09 -05001957static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08001958 Error **errp)
1959{
1960 Error *local_err = NULL;
1961 BlockDirtyBitmapAdd *action;
1962 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1963 common, common);
1964
John Snow94d16a62015-11-05 18:13:18 -05001965 if (action_check_completion_mode(common, errp) < 0) {
1966 return;
1967 }
1968
Eric Blake32bafa82016-03-17 16:48:37 -06001969 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001970 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1971 qmp_block_dirty_bitmap_add(action->node, action->name,
1972 action->has_granularity, action->granularity,
1973 &local_err);
1974
1975 if (!local_err) {
1976 state->prepared = true;
1977 } else {
1978 error_propagate(errp, local_err);
1979 }
1980}
1981
John Snow50f43f02015-11-05 18:13:09 -05001982static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08001983{
1984 BlockDirtyBitmapAdd *action;
1985 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1986 common, common);
1987
Eric Blake32bafa82016-03-17 16:48:37 -06001988 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001989 /* Should not be able to fail: IF the bitmap was added via .prepare(),
1990 * then the node reference and bitmap name must have been valid.
1991 */
1992 if (state->prepared) {
1993 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
1994 }
1995}
1996
John Snow50f43f02015-11-05 18:13:09 -05001997static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08001998 Error **errp)
1999{
2000 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2001 common, common);
2002 BlockDirtyBitmap *action;
2003
John Snow94d16a62015-11-05 18:13:18 -05002004 if (action_check_completion_mode(common, errp) < 0) {
2005 return;
2006 }
2007
Eric Blake32bafa82016-03-17 16:48:37 -06002008 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002009 state->bitmap = block_dirty_bitmap_lookup(action->node,
2010 action->name,
2011 &state->bs,
2012 &state->aio_context,
2013 errp);
2014 if (!state->bitmap) {
2015 return;
2016 }
2017
2018 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2019 error_setg(errp, "Cannot modify a frozen bitmap");
2020 return;
2021 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2022 error_setg(errp, "Cannot clear a disabled bitmap");
2023 return;
2024 }
2025
2026 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2027 /* AioContext is released in .clean() */
2028}
2029
John Snow50f43f02015-11-05 18:13:09 -05002030static void block_dirty_bitmap_clear_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002031{
2032 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2033 common, common);
2034
2035 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2036}
2037
John Snow50f43f02015-11-05 18:13:09 -05002038static void block_dirty_bitmap_clear_commit(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002039{
2040 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2041 common, common);
2042
2043 hbitmap_free(state->backup);
2044}
2045
John Snow50f43f02015-11-05 18:13:09 -05002046static void block_dirty_bitmap_clear_clean(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002047{
2048 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2049 common, common);
2050
2051 if (state->aio_context) {
2052 aio_context_release(state->aio_context);
2053 }
2054}
2055
John Snow50f43f02015-11-05 18:13:09 -05002056static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002057{
2058 error_setg(errp, "Transaction aborted using Abort action");
2059}
2060
John Snow50f43f02015-11-05 18:13:09 -05002061static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002062{
Stefan Weildfc6f862013-07-25 18:21:28 +02002063 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002064}
2065
John Snow50f43f02015-11-05 18:13:09 -05002066static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002067 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2068 .instance_size = sizeof(ExternalSnapshotState),
2069 .prepare = external_snapshot_prepare,
2070 .commit = external_snapshot_commit,
2071 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002072 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002073 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002074 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002075 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002076 .prepare = external_snapshot_prepare,
2077 .commit = external_snapshot_commit,
2078 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002079 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002080 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002081 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2082 .instance_size = sizeof(DriveBackupState),
2083 .prepare = drive_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002084 .commit = drive_backup_commit,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002085 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002086 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002087 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002088 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2089 .instance_size = sizeof(BlockdevBackupState),
2090 .prepare = blockdev_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002091 .commit = blockdev_backup_commit,
Fam Zhengbd8baec2014-12-18 18:37:06 +08002092 .abort = blockdev_backup_abort,
2093 .clean = blockdev_backup_clean,
2094 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002095 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002096 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002097 .prepare = abort_prepare,
2098 .commit = abort_commit,
2099 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002100 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2101 .instance_size = sizeof(InternalSnapshotState),
2102 .prepare = internal_snapshot_prepare,
2103 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002104 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002105 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002106 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2107 .instance_size = sizeof(BlockDirtyBitmapState),
2108 .prepare = block_dirty_bitmap_add_prepare,
2109 .abort = block_dirty_bitmap_add_abort,
2110 },
2111 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2112 .instance_size = sizeof(BlockDirtyBitmapState),
2113 .prepare = block_dirty_bitmap_clear_prepare,
2114 .commit = block_dirty_bitmap_clear_commit,
2115 .abort = block_dirty_bitmap_clear_abort,
2116 .clean = block_dirty_bitmap_clear_clean,
2117 }
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002118};
2119
John Snow94d16a62015-11-05 18:13:18 -05002120/**
2121 * Allocate a TransactionProperties structure if necessary, and fill
2122 * that structure with desired defaults if they are unset.
2123 */
2124static TransactionProperties *get_transaction_properties(
2125 TransactionProperties *props)
2126{
2127 if (!props) {
2128 props = g_new0(TransactionProperties, 1);
2129 }
2130
2131 if (!props->has_completion_mode) {
2132 props->has_completion_mode = true;
2133 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2134 }
2135
2136 return props;
2137}
2138
Jeff Cody8802d1f2012-02-28 15:54:06 -05002139/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002140 * 'Atomic' group operations. The operations are performed as a set, and if
2141 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002142 */
John Snow94d16a62015-11-05 18:13:18 -05002143void qmp_transaction(TransactionActionList *dev_list,
2144 bool has_props,
2145 struct TransactionProperties *props,
2146 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002147{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002148 TransactionActionList *dev_entry = dev_list;
John Snow94d16a62015-11-05 18:13:18 -05002149 BlockJobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002150 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002151 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002152
John Snow50f43f02015-11-05 18:13:09 -05002153 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002154 QSIMPLEQ_INIT(&snap_bdrv_states);
2155
John Snow94d16a62015-11-05 18:13:18 -05002156 /* Does this transaction get canceled as a group on failure?
2157 * If not, we don't really need to make a BlockJobTxn.
2158 */
2159 props = get_transaction_properties(props);
2160 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2161 block_job_txn = block_job_txn_new();
2162 }
2163
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002164 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002165 bdrv_drain_all();
2166
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002167 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002168 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002169 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002170 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002171
Jeff Cody8802d1f2012-02-28 15:54:06 -05002172 dev_info = dev_entry->value;
2173 dev_entry = dev_entry->next;
2174
Eric Blake6a8f9662015-10-26 16:34:54 -06002175 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002176
Eric Blake6a8f9662015-10-26 16:34:54 -06002177 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002178 assert(ops->instance_size > 0);
2179
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002180 state = g_malloc0(ops->instance_size);
2181 state->ops = ops;
2182 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002183 state->block_job_txn = block_job_txn;
2184 state->txn_props = props;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002185 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002186
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002187 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002188 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002189 error_propagate(errp, local_err);
2190 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002191 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002192 }
2193
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002194 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002195 if (state->ops->commit) {
2196 state->ops->commit(state);
2197 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002198 }
2199
2200 /* success */
2201 goto exit;
2202
2203delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002204 /* failure, and it is all-or-none; roll back all operations */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002205 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2206 if (state->ops->abort) {
2207 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002208 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002209 }
2210exit:
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002211 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2212 if (state->ops->clean) {
2213 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002214 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002215 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002216 }
John Snow94d16a62015-11-05 18:13:18 -05002217 if (!has_props) {
2218 qapi_free_TransactionProperties(props);
2219 }
2220 block_job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002221}
2222
Kevin Wolffbe2d812016-09-20 13:38:46 +02002223void qmp_eject(bool has_device, const char *device,
2224 bool has_id, const char *id,
2225 bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002226{
Max Reitz38f54bd2015-10-26 21:39:12 +01002227 Error *local_err = NULL;
John Snow3a3086b2016-05-18 17:53:40 -04002228 int rc;
Markus Armbruster666daa62010-06-02 18:48:27 +02002229
John Snow3a3086b2016-05-18 17:53:40 -04002230 if (!has_force) {
2231 force = false;
2232 }
2233
Kevin Wolffbe2d812016-09-20 13:38:46 +02002234 rc = do_open_tray(has_device ? device : NULL,
2235 has_id ? id : NULL,
2236 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002237 if (rc && rc != -ENOSYS) {
Max Reitz38f54bd2015-10-26 21:39:12 +01002238 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002239 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002240 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002241 error_free(local_err);
John Snow3a3086b2016-05-18 17:53:40 -04002242
Kevin Wolffbe2d812016-09-20 13:38:46 +02002243 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002244}
2245
Benoît Canet12d3ba82014-01-23 21:31:35 +01002246void qmp_block_passwd(bool has_device, const char *device,
2247 bool has_node_name, const char *node_name,
2248 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002249{
Benoît Canet12d3ba82014-01-23 21:31:35 +01002250 Error *local_err = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +02002251 BlockDriverState *bs;
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002252 AioContext *aio_context;
Markus Armbruster666daa62010-06-02 18:48:27 +02002253
Benoît Canet12d3ba82014-01-23 21:31:35 +01002254 bs = bdrv_lookup_bs(has_device ? device : NULL,
2255 has_node_name ? node_name : NULL,
2256 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002257 if (local_err) {
Benoît Canet12d3ba82014-01-23 21:31:35 +01002258 error_propagate(errp, local_err);
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02002259 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002260 }
2261
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002262 aio_context = bdrv_get_aio_context(bs);
2263 aio_context_acquire(aio_context);
2264
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002265 bdrv_add_key(bs, password, errp);
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002266
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002267 aio_context_release(aio_context);
Markus Armbruster666daa62010-06-02 18:48:27 +02002268}
2269
Colin Lordbf18bee2016-06-06 14:15:22 -04002270/*
2271 * Attempt to open the tray of @device.
2272 * If @force, ignore its tray lock.
2273 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2274 * On error, store an error through @errp and return -errno.
2275 * If @device does not exist, return -ENODEV.
2276 * If it has no removable media, return -ENOTSUP.
2277 * If it has no tray, return -ENOSYS.
2278 * If the guest was asked to open the tray, return -EINPROGRESS.
2279 * Else, return 0.
John Snow3a3086b2016-05-18 17:53:40 -04002280 */
Kevin Wolfb33945c2016-09-20 13:38:43 +02002281static int do_open_tray(const char *blk_name, const char *qdev_id,
2282 bool force, Error **errp)
Max Reitz7d8a9f72015-10-26 21:39:08 +01002283{
2284 BlockBackend *blk;
Kevin Wolfb33945c2016-09-20 13:38:43 +02002285 const char *device = qdev_id ?: blk_name;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002286 bool locked;
2287
Kevin Wolfb33945c2016-09-20 13:38:43 +02002288 blk = qmp_get_blk(blk_name, qdev_id, errp);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002289 if (!blk) {
John Snow3a3086b2016-05-18 17:53:40 -04002290 return -ENODEV;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002291 }
2292
2293 if (!blk_dev_has_removable_media(blk)) {
2294 error_setg(errp, "Device '%s' is not removable", device);
John Snow3a3086b2016-05-18 17:53:40 -04002295 return -ENOTSUP;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002296 }
2297
Max Reitz12c7ec82016-01-29 20:49:11 +01002298 if (!blk_dev_has_tray(blk)) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002299 error_setg(errp, "Device '%s' does not have a tray", device);
2300 return -ENOSYS;
Max Reitz12c7ec82016-01-29 20:49:11 +01002301 }
2302
Max Reitz7d8a9f72015-10-26 21:39:08 +01002303 if (blk_dev_is_tray_open(blk)) {
John Snow3a3086b2016-05-18 17:53:40 -04002304 return 0;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002305 }
2306
2307 locked = blk_dev_is_medium_locked(blk);
2308 if (locked) {
2309 blk_dev_eject_request(blk, force);
2310 }
2311
2312 if (!locked || force) {
2313 blk_dev_change_media_cb(blk, false);
2314 }
John Snow3a3086b2016-05-18 17:53:40 -04002315
2316 if (locked && !force) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002317 error_setg(errp, "Device '%s' is locked and force was not specified, "
2318 "wait for tray to open and try again", device);
2319 return -EINPROGRESS;
John Snow3a3086b2016-05-18 17:53:40 -04002320 }
2321
2322 return 0;
2323}
2324
Kevin Wolfb33945c2016-09-20 13:38:43 +02002325void qmp_blockdev_open_tray(bool has_device, const char *device,
2326 bool has_id, const char *id,
2327 bool has_force, bool force,
John Snow3a3086b2016-05-18 17:53:40 -04002328 Error **errp)
2329{
Colin Lordbf18bee2016-06-06 14:15:22 -04002330 Error *local_err = NULL;
2331 int rc;
2332
John Snow3a3086b2016-05-18 17:53:40 -04002333 if (!has_force) {
2334 force = false;
2335 }
Kevin Wolfb33945c2016-09-20 13:38:43 +02002336 rc = do_open_tray(has_device ? device : NULL,
2337 has_id ? id : NULL,
2338 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002339 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2340 error_propagate(errp, local_err);
2341 return;
2342 }
2343 error_free(local_err);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002344}
2345
Kevin Wolfb33945c2016-09-20 13:38:43 +02002346void qmp_blockdev_close_tray(bool has_device, const char *device,
2347 bool has_id, const char *id,
2348 Error **errp)
Max Reitzabaaf592015-10-26 21:39:09 +01002349{
2350 BlockBackend *blk;
2351
Kevin Wolfb33945c2016-09-20 13:38:43 +02002352 device = has_device ? device : NULL;
2353 id = has_id ? id : NULL;
2354
2355 blk = qmp_get_blk(device, id, errp);
Max Reitzabaaf592015-10-26 21:39:09 +01002356 if (!blk) {
Max Reitzabaaf592015-10-26 21:39:09 +01002357 return;
2358 }
2359
2360 if (!blk_dev_has_removable_media(blk)) {
Kevin Wolfb33945c2016-09-20 13:38:43 +02002361 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitzabaaf592015-10-26 21:39:09 +01002362 return;
2363 }
2364
Max Reitz12c7ec82016-01-29 20:49:11 +01002365 if (!blk_dev_has_tray(blk)) {
2366 /* Ignore this command on tray-less devices */
2367 return;
2368 }
2369
Max Reitzabaaf592015-10-26 21:39:09 +01002370 if (!blk_dev_is_tray_open(blk)) {
2371 return;
2372 }
2373
2374 blk_dev_change_media_cb(blk, true);
2375}
2376
Kevin Wolf00949ba2016-09-20 13:38:45 +02002377void qmp_x_blockdev_remove_medium(bool has_device, const char *device,
2378 bool has_id, const char *id, Error **errp)
Max Reitz2814f672015-10-26 21:39:10 +01002379{
2380 BlockBackend *blk;
2381 BlockDriverState *bs;
2382 AioContext *aio_context;
Kevin Wolf00949ba2016-09-20 13:38:45 +02002383 bool has_attached_device;
Max Reitz2814f672015-10-26 21:39:10 +01002384
Kevin Wolf00949ba2016-09-20 13:38:45 +02002385 device = has_device ? device : NULL;
2386 id = has_id ? id : NULL;
2387
2388 blk = qmp_get_blk(device, id, errp);
Max Reitz2814f672015-10-26 21:39:10 +01002389 if (!blk) {
Max Reitz2814f672015-10-26 21:39:10 +01002390 return;
2391 }
2392
2393 /* For BBs without a device, we can exchange the BDS tree at will */
Kevin Wolf00949ba2016-09-20 13:38:45 +02002394 has_attached_device = blk_get_attached_dev(blk);
Max Reitz2814f672015-10-26 21:39:10 +01002395
Kevin Wolf00949ba2016-09-20 13:38:45 +02002396 if (has_attached_device && !blk_dev_has_removable_media(blk)) {
2397 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002398 return;
2399 }
2400
Kevin Wolf00949ba2016-09-20 13:38:45 +02002401 if (has_attached_device && blk_dev_has_tray(blk) &&
2402 !blk_dev_is_tray_open(blk))
2403 {
2404 error_setg(errp, "Tray of device '%s' is not open", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002405 return;
2406 }
2407
2408 bs = blk_bs(blk);
2409 if (!bs) {
2410 return;
2411 }
2412
2413 aio_context = bdrv_get_aio_context(bs);
2414 aio_context_acquire(aio_context);
2415
2416 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2417 goto out;
2418 }
2419
Max Reitz2814f672015-10-26 21:39:10 +01002420 blk_remove_bs(blk);
2421
Max Reitz12c7ec82016-01-29 20:49:11 +01002422 if (!blk_dev_has_tray(blk)) {
2423 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2424 * called at all); therefore, the medium needs to be ejected here.
2425 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2426 * value passed here (i.e. false). */
2427 blk_dev_change_media_cb(blk, false);
2428 }
2429
Max Reitz2814f672015-10-26 21:39:10 +01002430out:
2431 aio_context_release(aio_context);
2432}
2433
Kevin Wolf716df212016-09-20 13:38:44 +02002434static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
Max Reitzd1299882015-10-26 21:39:11 +01002435 BlockDriverState *bs, Error **errp)
2436{
Max Reitzd1299882015-10-26 21:39:11 +01002437 bool has_device;
2438
Max Reitzd1299882015-10-26 21:39:11 +01002439 /* For BBs without a device, we can exchange the BDS tree at will */
2440 has_device = blk_get_attached_dev(blk);
2441
2442 if (has_device && !blk_dev_has_removable_media(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002443 error_setg(errp, "Device is not removable");
Max Reitzd1299882015-10-26 21:39:11 +01002444 return;
2445 }
2446
Max Reitz12c7ec82016-01-29 20:49:11 +01002447 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002448 error_setg(errp, "Tray of the device is not open");
Max Reitzd1299882015-10-26 21:39:11 +01002449 return;
2450 }
2451
2452 if (blk_bs(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002453 error_setg(errp, "There already is a medium in the device");
Max Reitzd1299882015-10-26 21:39:11 +01002454 return;
2455 }
2456
2457 blk_insert_bs(blk, bs);
2458
Max Reitz12c7ec82016-01-29 20:49:11 +01002459 if (!blk_dev_has_tray(blk)) {
2460 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2461 * called at all); therefore, the medium needs to be pushed into the
2462 * slot here.
2463 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2464 * value passed here (i.e. true). */
2465 blk_dev_change_media_cb(blk, true);
2466 }
Max Reitzd1299882015-10-26 21:39:11 +01002467}
2468
Kevin Wolf716df212016-09-20 13:38:44 +02002469void qmp_x_blockdev_insert_medium(bool has_device, const char *device,
2470 bool has_id, const char *id,
2471 const char *node_name, Error **errp)
Max Reitzd1299882015-10-26 21:39:11 +01002472{
Kevin Wolf716df212016-09-20 13:38:44 +02002473 BlockBackend *blk;
Max Reitzd1299882015-10-26 21:39:11 +01002474 BlockDriverState *bs;
2475
Kevin Wolf716df212016-09-20 13:38:44 +02002476 blk = qmp_get_blk(has_device ? device : NULL,
2477 has_id ? id : NULL,
2478 errp);
2479 if (!blk) {
2480 return;
2481 }
2482
Max Reitzd1299882015-10-26 21:39:11 +01002483 bs = bdrv_find_node(node_name);
2484 if (!bs) {
2485 error_setg(errp, "Node '%s' not found", node_name);
2486 return;
2487 }
2488
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002489 if (bdrv_has_blk(bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02002490 error_setg(errp, "Node '%s' is already in use", node_name);
Max Reitzd1299882015-10-26 21:39:11 +01002491 return;
2492 }
2493
Kevin Wolf716df212016-09-20 13:38:44 +02002494 qmp_blockdev_insert_anon_medium(blk, bs, errp);
Max Reitzd1299882015-10-26 21:39:11 +01002495}
2496
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002497void qmp_blockdev_change_medium(bool has_device, const char *device,
2498 bool has_id, const char *id,
2499 const char *filename,
Max Reitz24fb4132015-11-06 16:27:06 +01002500 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002501 bool has_read_only,
2502 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002503 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002504{
2505 BlockBackend *blk;
2506 BlockDriverState *medium_bs = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002507 int bdrv_flags;
Kevin Wolfb85114f2016-09-12 19:08:31 +02002508 bool detect_zeroes;
Colin Lord38a53d52016-06-08 13:56:28 -04002509 int rc;
Max Reitzde2c6c02015-10-26 21:39:13 +01002510 QDict *options = NULL;
2511 Error *err = NULL;
2512
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002513 blk = qmp_get_blk(has_device ? device : NULL,
2514 has_id ? id : NULL,
2515 errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002516 if (!blk) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002517 goto fail;
2518 }
2519
2520 if (blk_bs(blk)) {
2521 blk_update_root_state(blk);
2522 }
2523
2524 bdrv_flags = blk_get_open_flags_from_root_state(blk);
Alyssa Milburn156abc22016-02-06 13:36:18 +00002525 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2526 BDRV_O_PROTOCOL);
Max Reitzde2c6c02015-10-26 21:39:13 +01002527
Max Reitz39ff43d2015-11-11 04:49:44 +01002528 if (!has_read_only) {
2529 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2530 }
2531
2532 switch (read_only) {
2533 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2534 break;
2535
2536 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2537 bdrv_flags &= ~BDRV_O_RDWR;
2538 break;
2539
2540 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2541 bdrv_flags |= BDRV_O_RDWR;
2542 break;
2543
2544 default:
2545 abort();
2546 }
2547
Kevin Wolfb85114f2016-09-12 19:08:31 +02002548 options = qdict_new();
2549 detect_zeroes = blk_get_detect_zeroes_from_root_state(blk);
2550 qdict_put(options, "detect-zeroes",
2551 qstring_from_str(detect_zeroes ? "on" : "off"));
2552
Max Reitz24fb4132015-11-06 16:27:06 +01002553 if (has_format) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002554 qdict_put(options, "driver", qstring_from_str(format));
2555 }
2556
Max Reitz5b363932016-05-17 16:41:31 +02002557 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2558 if (!medium_bs) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002559 goto fail;
2560 }
2561
Max Reitzde2c6c02015-10-26 21:39:13 +01002562 bdrv_add_key(medium_bs, NULL, &err);
2563 if (err) {
2564 error_propagate(errp, err);
2565 goto fail;
2566 }
2567
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002568 rc = do_open_tray(has_device ? device : NULL,
2569 has_id ? id : NULL,
2570 false, &err);
Colin Lord38a53d52016-06-08 13:56:28 -04002571 if (rc && rc != -ENOSYS) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002572 error_propagate(errp, err);
2573 goto fail;
2574 }
Colin Lord38a53d52016-06-08 13:56:28 -04002575 error_free(err);
2576 err = NULL;
Max Reitzde2c6c02015-10-26 21:39:13 +01002577
Kevin Wolf24df38b2016-09-26 15:03:00 +02002578 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002579 if (err) {
2580 error_propagate(errp, err);
2581 goto fail;
2582 }
2583
Kevin Wolf716df212016-09-20 13:38:44 +02002584 qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002585 if (err) {
2586 error_propagate(errp, err);
2587 goto fail;
2588 }
2589
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002590 qmp_blockdev_close_tray(has_device, device, has_id, id, errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002591
2592fail:
2593 /* If the medium has been inserted, the device has its own reference, so
2594 * ours must be relinquished; and if it has not been inserted successfully,
2595 * the reference must be relinquished anyway */
2596 bdrv_unref(medium_bs);
2597}
2598
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002599/* throttling disk I/O limits */
Eric Blake4dc93972016-07-13 21:50:21 -06002600void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002601{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002602 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002603 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002604 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002605 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002606
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002607 blk = qmp_get_blk(arg->has_device ? arg->device : NULL,
2608 arg->has_id ? arg->id : NULL,
2609 errp);
Fam Zhenga0e85442015-03-02 19:36:48 +08002610 if (!blk) {
Luiz Capitulino80047da2011-12-14 16:49:14 -02002611 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002612 }
Max Reitz5433c242015-10-19 17:53:29 +02002613
2614 aio_context = blk_get_aio_context(blk);
2615 aio_context_acquire(aio_context);
2616
Fam Zhenga0e85442015-03-02 19:36:48 +08002617 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002618 if (!bs) {
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002619 error_setg(errp, "Device has no medium");
Max Reitz5433c242015-10-19 17:53:29 +02002620 goto out;
2621 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002622
Alberto Garcia1588ab52016-02-18 12:27:00 +02002623 throttle_config_init(&cfg);
Eric Blake4dc93972016-07-13 21:50:21 -06002624 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2625 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2626 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002627
Eric Blake4dc93972016-07-13 21:50:21 -06002628 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2629 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2630 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
Benoît Canetcc0681c2013-09-02 14:14:39 +02002631
Eric Blake4dc93972016-07-13 21:50:21 -06002632 if (arg->has_bps_max) {
2633 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002634 }
Eric Blake4dc93972016-07-13 21:50:21 -06002635 if (arg->has_bps_rd_max) {
2636 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002637 }
Eric Blake4dc93972016-07-13 21:50:21 -06002638 if (arg->has_bps_wr_max) {
2639 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002640 }
Eric Blake4dc93972016-07-13 21:50:21 -06002641 if (arg->has_iops_max) {
2642 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002643 }
Eric Blake4dc93972016-07-13 21:50:21 -06002644 if (arg->has_iops_rd_max) {
2645 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002646 }
Eric Blake4dc93972016-07-13 21:50:21 -06002647 if (arg->has_iops_wr_max) {
2648 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002649 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002650
Eric Blake4dc93972016-07-13 21:50:21 -06002651 if (arg->has_bps_max_length) {
2652 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002653 }
Eric Blake4dc93972016-07-13 21:50:21 -06002654 if (arg->has_bps_rd_max_length) {
2655 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002656 }
Eric Blake4dc93972016-07-13 21:50:21 -06002657 if (arg->has_bps_wr_max_length) {
2658 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002659 }
Eric Blake4dc93972016-07-13 21:50:21 -06002660 if (arg->has_iops_max_length) {
2661 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002662 }
Eric Blake4dc93972016-07-13 21:50:21 -06002663 if (arg->has_iops_rd_max_length) {
2664 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002665 }
Eric Blake4dc93972016-07-13 21:50:21 -06002666 if (arg->has_iops_wr_max_length) {
2667 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002668 }
2669
Eric Blake4dc93972016-07-13 21:50:21 -06002670 if (arg->has_iops_size) {
2671 cfg.op_size = arg->iops_size;
Benoît Canet2024c1d2013-09-02 14:14:41 +02002672 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002673
Alberto Garciad5851082016-02-18 12:26:59 +02002674 if (!throttle_is_valid(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002675 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002676 }
2677
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002678 if (throttle_enabled(&cfg)) {
2679 /* Enable I/O limits if they're not enabled yet, otherwise
2680 * just update the throttling group. */
Kevin Wolf97148072016-03-21 13:53:52 +01002681 if (!blk_get_public(blk)->throttle_state) {
Eric Blake4dc93972016-07-13 21:50:21 -06002682 blk_io_limits_enable(blk,
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002683 arg->has_group ? arg->group :
2684 arg->has_device ? arg->device :
2685 arg->id);
Eric Blake4dc93972016-07-13 21:50:21 -06002686 } else if (arg->has_group) {
2687 blk_io_limits_update_group(blk, arg->group);
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002688 }
2689 /* Set the new throttling configuration */
Kevin Wolf97148072016-03-21 13:53:52 +01002690 blk_set_io_limits(blk, &cfg);
2691 } else if (blk_get_public(blk)->throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002692 /* If all throttling settings are set to 0, disable I/O limits */
Kevin Wolf97148072016-03-21 13:53:52 +01002693 blk_io_limits_disable(blk);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002694 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002695
Max Reitz5433c242015-10-19 17:53:29 +02002696out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002697 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002698}
2699
John Snow341ebc22015-04-17 19:49:52 -04002700void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2701 bool has_granularity, uint32_t granularity,
2702 Error **errp)
2703{
2704 AioContext *aio_context;
2705 BlockDriverState *bs;
2706
2707 if (!name || name[0] == '\0') {
2708 error_setg(errp, "Bitmap name cannot be empty");
2709 return;
2710 }
2711
2712 bs = bdrv_lookup_bs(node, node, errp);
2713 if (!bs) {
2714 return;
2715 }
2716
2717 aio_context = bdrv_get_aio_context(bs);
2718 aio_context_acquire(aio_context);
2719
2720 if (has_granularity) {
2721 if (granularity < 512 || !is_power_of_2(granularity)) {
2722 error_setg(errp, "Granularity must be power of 2 "
2723 "and at least 512");
2724 goto out;
2725 }
2726 } else {
2727 /* Default to cluster size, if available: */
2728 granularity = bdrv_get_default_bitmap_granularity(bs);
2729 }
2730
2731 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2732
2733 out:
2734 aio_context_release(aio_context);
2735}
2736
2737void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2738 Error **errp)
2739{
2740 AioContext *aio_context;
2741 BlockDriverState *bs;
2742 BdrvDirtyBitmap *bitmap;
2743
2744 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2745 if (!bitmap || !bs) {
2746 return;
2747 }
2748
John Snow9bd2b082015-04-17 19:49:57 -04002749 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2750 error_setg(errp,
2751 "Bitmap '%s' is currently frozen and cannot be removed",
2752 name);
2753 goto out;
2754 }
John Snow20dca812015-04-17 19:50:02 -04002755 bdrv_dirty_bitmap_make_anon(bitmap);
John Snow341ebc22015-04-17 19:49:52 -04002756 bdrv_release_dirty_bitmap(bs, bitmap);
2757
John Snow9bd2b082015-04-17 19:49:57 -04002758 out:
John Snow341ebc22015-04-17 19:49:52 -04002759 aio_context_release(aio_context);
2760}
2761
John Snowe74e6b72015-04-17 19:49:59 -04002762/**
2763 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2764 * immediately after a full backup operation.
2765 */
2766void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2767 Error **errp)
2768{
2769 AioContext *aio_context;
2770 BdrvDirtyBitmap *bitmap;
2771 BlockDriverState *bs;
2772
2773 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2774 if (!bitmap || !bs) {
2775 return;
2776 }
2777
2778 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2779 error_setg(errp,
2780 "Bitmap '%s' is currently frozen and cannot be modified",
2781 name);
2782 goto out;
2783 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2784 error_setg(errp,
2785 "Bitmap '%s' is currently disabled and cannot be cleared",
2786 name);
2787 goto out;
2788 }
2789
Fam Zhengdf9a6812015-11-09 18:16:54 +08002790 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04002791
2792 out:
2793 aio_context_release(aio_context);
2794}
2795
Markus Armbruster072ebe62015-03-05 17:00:56 +01002796void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06002797{
2798 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002799 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06002800 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002801 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08002802 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06002803
Kevin Wolf2073d412016-02-23 17:50:37 +01002804 bs = bdrv_find_node(id);
2805 if (bs) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02002806 qmp_x_blockdev_del(id, &local_err);
Kevin Wolf2073d412016-02-23 17:50:37 +01002807 if (local_err) {
2808 error_report_err(local_err);
2809 }
2810 return;
2811 }
2812
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002813 blk = blk_by_name(id);
2814 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02002815 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01002816 return;
Ryan Harper9063f812010-11-12 11:07:13 -06002817 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002818
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02002819 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02002820 error_report("Deleting device added with blockdev-add"
2821 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01002822 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02002823 }
2824
Max Reitz5433c242015-10-19 17:53:29 +02002825 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002826 aio_context_acquire(aio_context);
2827
Max Reitz5433c242015-10-19 17:53:29 +02002828 bs = blk_bs(blk);
2829 if (bs) {
2830 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2831 error_report_err(local_err);
2832 aio_context_release(aio_context);
2833 return;
2834 }
Ryan Harper9063f812010-11-12 11:07:13 -06002835
Max Reitz938abd42016-01-29 16:36:09 +01002836 blk_remove_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002837 }
Ryan Harper9063f812010-11-12 11:07:13 -06002838
Max Reitz7c735872016-03-16 19:54:39 +01002839 /* Make the BlockBackend and the attached BlockDriverState anonymous */
Max Reitzefaa7c42016-03-16 19:54:38 +01002840 monitor_remove_blk(blk);
2841
Max Reitz7c735872016-03-16 19:54:39 +01002842 /* If this BlockBackend has a device attached to it, its refcount will be
2843 * decremented when the device is removed; otherwise we have to do so here.
Ryan Harperd22b2f42011-03-29 20:51:47 -05002844 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02002845 if (blk_get_attached_dev(blk)) {
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02002846 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02002847 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2848 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05002849 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02002850 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06002851 }
2852
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002853 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06002854}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002855
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002856void qmp_block_resize(bool has_device, const char *device,
2857 bool has_node_name, const char *node_name,
2858 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002859{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002860 Error *local_err = NULL;
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002861 BlockBackend *blk = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002862 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002863 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02002864 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002865
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002866 bs = bdrv_lookup_bs(has_device ? device : NULL,
2867 has_node_name ? node_name : NULL,
2868 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002869 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002870 error_propagate(errp, local_err);
2871 return;
2872 }
2873
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002874 aio_context = bdrv_get_aio_context(bs);
2875 aio_context_acquire(aio_context);
2876
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002877 if (!bdrv_is_first_non_filter(bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002878 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002879 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002880 }
2881
2882 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002883 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002884 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002885 }
2886
Jeff Cody9c75e162014-06-25 16:55:30 -04002887 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002888 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002889 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04002890 }
2891
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002892 blk = blk_new();
2893 blk_insert_bs(blk, bs);
2894
Peter Lieven92b7a082013-03-11 11:04:24 +01002895 /* complete all in-flight operations before resizing the device */
2896 bdrv_drain_all();
2897
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002898 ret = blk_truncate(blk, size);
Kevin Wolf87329012013-05-02 15:32:55 +02002899 switch (ret) {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002900 case 0:
2901 break;
2902 case -ENOMEDIUM:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002903 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002904 break;
2905 case -ENOTSUP:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002906 error_setg(errp, QERR_UNSUPPORTED);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002907 break;
2908 case -EACCES:
Alberto Garcia81e5f782015-04-08 12:29:19 +03002909 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002910 break;
2911 case -EBUSY:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002912 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002913 break;
2914 default:
Kevin Wolf87329012013-05-02 15:32:55 +02002915 error_setg_errno(errp, -ret, "Could not resize");
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00002916 break;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002917 }
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002918
2919out:
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002920 blk_unref(blk);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002921 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002922}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002923
Alberto Garcia23233222016-07-05 17:28:59 +03002924void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002925 bool has_base, const char *base,
Alberto Garcia312fe092016-10-28 10:08:19 +03002926 bool has_base_node, const char *base_node,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002927 bool has_backing_file, const char *backing_file,
2928 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002929 bool has_on_error, BlockdevOnError on_error,
2930 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002931{
Alberto Garcia554b6142016-10-28 10:08:11 +03002932 BlockDriverState *bs, *iter;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002933 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002934 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002935 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002936 const char *base_name = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002937
Paolo Bonzini1d809092012-09-28 17:22:59 +02002938 if (!has_on_error) {
2939 on_error = BLOCKDEV_ON_ERROR_REPORT;
2940 }
2941
Alberto Garcia554b6142016-10-28 10:08:11 +03002942 bs = bdrv_lookup_bs(device, device, errp);
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002943 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002944 return;
2945 }
2946
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002947 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002948 aio_context_acquire(aio_context);
2949
Alberto Garcia312fe092016-10-28 10:08:19 +03002950 if (has_base && has_base_node) {
2951 error_setg(errp, "'base' and 'base-node' cannot be specified "
2952 "at the same time");
2953 goto out;
2954 }
2955
Jeff Cody13d8cc52014-06-25 15:40:11 -04002956 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002957 base_bs = bdrv_find_backing_image(bs, base);
2958 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002959 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002960 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002961 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002962 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04002963 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002964 }
2965
Alberto Garcia312fe092016-10-28 10:08:19 +03002966 if (has_base_node) {
2967 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2968 if (!base_bs) {
2969 goto out;
2970 }
2971 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
2972 error_setg(errp, "Node '%s' is not a backing image of '%s'",
2973 base_node, device);
2974 goto out;
2975 }
2976 assert(bdrv_get_aio_context(base_bs) == aio_context);
2977 base_name = base_bs->filename;
2978 }
2979
Alberto Garcia554b6142016-10-28 10:08:11 +03002980 /* Check for op blockers in the whole chain between bs and base */
2981 for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) {
2982 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
2983 goto out;
2984 }
2985 }
2986
Jeff Cody13d8cc52014-06-25 15:40:11 -04002987 /* if we are streaming the entire chain, the result will have no backing
2988 * file, and specifying one is therefore an error */
2989 if (base_bs == NULL && has_backing_file) {
2990 error_setg(errp, "backing file specified, but streaming the "
2991 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002992 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002993 }
2994
2995 /* backing_file string overrides base bs filename */
2996 base_name = has_backing_file ? backing_file : base_name;
2997
Alberto Garcia23233222016-07-05 17:28:59 +03002998 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
John Snow8254b6d2016-10-27 12:06:58 -04002999 has_speed ? speed : 0, on_error, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003000 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003001 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003002 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003003 }
3004
3005 trace_qmp_block_stream(bs, bs->job);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003006
3007out:
3008 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003009}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003010
Alberto Garciafd62c602016-07-05 17:29:00 +03003011void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003012 bool has_base, const char *base,
3013 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003014 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04003015 bool has_speed, int64_t speed,
3016 Error **errp)
3017{
3018 BlockDriverState *bs;
Alberto Garcia058223a2016-10-28 10:08:07 +03003019 BlockDriverState *iter;
Jeff Codyed61fc12012-09-27 13:29:16 -04003020 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003021 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04003022 Error *local_err = NULL;
3023 /* This will be part of the QMP command, if/when the
3024 * BlockdevOnError change for blkmirror makes it in
3025 */
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02003026 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
Jeff Codyed61fc12012-09-27 13:29:16 -04003027
Max Reitz54504662014-04-10 19:36:25 +02003028 if (!has_speed) {
3029 speed = 0;
3030 }
3031
Jeff Cody7676e2c2014-06-30 15:14:15 +02003032 /* Important Note:
3033 * libvirt relies on the DeviceNotFound error class in order to probe for
3034 * live commit feature versions; for this to work, we must make sure to
3035 * perform the device lookup before any generic errors that may occur in a
3036 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02003037 bs = qmp_get_root_bs(device, &local_err);
3038 if (!bs) {
3039 bs = bdrv_lookup_bs(device, device, NULL);
3040 if (!bs) {
3041 error_free(local_err);
3042 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3043 "Device '%s' not found", device);
3044 } else {
3045 error_propagate(errp, local_err);
3046 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003047 return;
3048 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003049
Kevin Wolf1d13b162016-06-23 14:20:24 +02003050 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003051 aio_context_acquire(aio_context);
3052
Fam Zhengbb000212014-09-11 13:14:00 +08003053 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003054 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003055 }
3056
Jeff Codyed61fc12012-09-27 13:29:16 -04003057 /* default top_bs is the active layer */
3058 top_bs = bs;
3059
Jeff Cody7676e2c2014-06-30 15:14:15 +02003060 if (has_top && top) {
Jeff Codyed61fc12012-09-27 13:29:16 -04003061 if (strcmp(bs->filename, top) != 0) {
3062 top_bs = bdrv_find_backing_image(bs, top);
3063 }
3064 }
3065
3066 if (top_bs == NULL) {
3067 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003068 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003069 }
3070
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003071 assert(bdrv_get_aio_context(top_bs) == aio_context);
3072
Jeff Codyd5208c42012-10-16 15:49:10 -04003073 if (has_base && base) {
3074 base_bs = bdrv_find_backing_image(top_bs, base);
3075 } else {
3076 base_bs = bdrv_find_base(top_bs);
3077 }
3078
3079 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003080 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003081 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04003082 }
3083
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003084 assert(bdrv_get_aio_context(base_bs) == aio_context);
3085
Alberto Garcia058223a2016-10-28 10:08:07 +03003086 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
3087 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3088 goto out;
3089 }
Fam Zhengbb000212014-09-11 13:14:00 +08003090 }
3091
Jeff Cody7676e2c2014-06-30 15:14:15 +02003092 /* Do not allow attempts to commit an image into itself */
3093 if (top_bs == base_bs) {
3094 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003095 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02003096 }
3097
Fam Zheng20a63d22013-12-16 14:45:31 +08003098 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04003099 if (has_backing_file) {
3100 error_setg(errp, "'backing-file' specified,"
3101 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003102 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003103 }
John Snow47970df2016-10-27 12:06:57 -04003104 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
John Snow8254b6d2016-10-27 12:06:58 -04003105 BLOCK_JOB_DEFAULT, speed, on_error, NULL, NULL,
3106 &local_err, false);
Fam Zheng20a63d22013-12-16 14:45:31 +08003107 } else {
Alberto Garcia058223a2016-10-28 10:08:07 +03003108 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
3109 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3110 goto out;
3111 }
Alberto Garciafd62c602016-07-05 17:29:00 +03003112 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed,
John Snow8254b6d2016-10-27 12:06:58 -04003113 on_error, has_backing_file ? backing_file : NULL,
3114 &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003115 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003116 if (local_err != NULL) {
3117 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003118 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003119 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003120
3121out:
3122 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003123}
3124
John Snow111049a2016-11-08 01:50:38 -05003125static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
3126 Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003127{
3128 BlockDriverState *bs;
3129 BlockDriverState *target_bs;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003130 BlockDriverState *source = NULL;
John Snow111049a2016-11-08 01:50:38 -05003131 BlockJob *job = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003132 BdrvDirtyBitmap *bmap = NULL;
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003133 AioContext *aio_context;
Max Reitze6641712015-08-26 19:47:48 +02003134 QDict *options = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003135 Error *local_err = NULL;
3136 int flags;
3137 int64_t size;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003138
Pavel Butsykin81206a82016-07-22 11:17:50 +03003139 if (!backup->has_speed) {
3140 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003141 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003142 if (!backup->has_on_source_error) {
3143 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003144 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003145 if (!backup->has_on_target_error) {
3146 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003147 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003148 if (!backup->has_mode) {
3149 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3150 }
3151 if (!backup->has_job_id) {
3152 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003153 }
Pavel Butsykin13b94142016-07-22 11:17:52 +03003154 if (!backup->has_compress) {
3155 backup->compress = false;
3156 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003157
Pavel Butsykin81206a82016-07-22 11:17:50 +03003158 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003159 if (!bs) {
John Snow111049a2016-11-08 01:50:38 -05003160 return NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003161 }
3162
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003163 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003164 aio_context_acquire(aio_context);
3165
Pavel Butsykin81206a82016-07-22 11:17:50 +03003166 if (!backup->has_format) {
3167 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
3168 NULL : (char*) bs->drv->format_name;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003169 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003170
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003171 /* Early check to avoid creating target */
Fam Zheng3718d8a2014-05-23 21:29:43 +08003172 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003173 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003174 }
3175
Kevin Wolf61de4c62016-03-18 17:46:45 +01003176 flags = bs->open_flags | BDRV_O_RDWR;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003177
Ian Mainfc5d3f82013-07-26 11:39:04 -07003178 /* See if we have a backing HD we can use to create our new image
3179 * on top of. */
Pavel Butsykin81206a82016-07-22 11:17:50 +03003180 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003181 source = backing_bs(bs);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003182 if (!source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003183 backup->sync = MIRROR_SYNC_MODE_FULL;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003184 }
3185 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003186 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
Ian Mainfc5d3f82013-07-26 11:39:04 -07003187 source = bs;
3188 }
3189
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003190 size = bdrv_getlength(bs);
3191 if (size < 0) {
3192 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003193 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003194 }
3195
Pavel Butsykin81206a82016-07-22 11:17:50 +03003196 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
3197 assert(backup->format);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003198 if (source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003199 bdrv_img_create(backup->target, backup->format, source->filename,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003200 source->drv->format_name, NULL,
3201 size, flags, &local_err, false);
3202 } else {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003203 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003204 size, flags, &local_err, false);
3205 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003206 }
3207
Markus Armbruster84d18f02014-01-30 15:07:28 +01003208 if (local_err) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003209 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003210 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003211 }
3212
Pavel Butsykin81206a82016-07-22 11:17:50 +03003213 if (backup->format) {
Max Reitze6641712015-08-26 19:47:48 +02003214 options = qdict_new();
Pavel Butsykin81206a82016-07-22 11:17:50 +03003215 qdict_put(options, "driver", qstring_from_str(backup->format));
Max Reitze6641712015-08-26 19:47:48 +02003216 }
3217
Pavel Butsykin81206a82016-07-22 11:17:50 +03003218 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003219 if (!target_bs) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003220 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003221 }
3222
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003223 bdrv_set_aio_context(target_bs, aio_context);
3224
Pavel Butsykin81206a82016-07-22 11:17:50 +03003225 if (backup->has_bitmap) {
3226 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
John Snowd58d8452015-04-17 19:49:58 -04003227 if (!bmap) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003228 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
Max Reitz0702d3d2015-11-09 23:39:10 +01003229 bdrv_unref(target_bs);
John Snowd58d8452015-04-17 19:49:58 -04003230 goto out;
3231 }
3232 }
3233
John Snow111049a2016-11-08 01:50:38 -05003234 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
3235 backup->sync, bmap, backup->compress,
3236 backup->on_source_error, backup->on_target_error,
3237 BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err);
Kevin Wolf5c438bc2016-04-14 13:09:53 +02003238 bdrv_unref(target_bs);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003239 if (local_err != NULL) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003240 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003241 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003242 }
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003243
3244out:
3245 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05003246 return job;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003247}
3248
Pavel Butsykin81206a82016-07-22 11:17:50 +03003249void qmp_drive_backup(DriveBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003250{
John Snow111049a2016-11-08 01:50:38 -05003251
3252 BlockJob *job;
3253 job = do_drive_backup(arg, NULL, errp);
3254 if (job) {
3255 block_job_start(job);
3256 }
John Snow78f51fd2015-11-05 18:13:17 -05003257}
3258
Benoît Canetc13163f2014-01-23 21:31:34 +01003259BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3260{
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003261 return bdrv_named_nodes_list(errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003262}
3263
John Snow111049a2016-11-08 01:50:38 -05003264BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
3265 Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003266{
3267 BlockDriverState *bs;
3268 BlockDriverState *target_bs;
3269 Error *local_err = NULL;
3270 AioContext *aio_context;
John Snow111049a2016-11-08 01:50:38 -05003271 BlockJob *job = NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003272
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003273 if (!backup->has_speed) {
3274 backup->speed = 0;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003275 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003276 if (!backup->has_on_source_error) {
3277 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003278 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003279 if (!backup->has_on_target_error) {
3280 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3281 }
3282 if (!backup->has_job_id) {
3283 backup->job_id = NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003284 }
Pavel Butsykin3b7b1232016-07-22 11:17:53 +03003285 if (!backup->has_compress) {
3286 backup->compress = false;
3287 }
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003288
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003289 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003290 if (!bs) {
John Snow111049a2016-11-08 01:50:38 -05003291 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003292 }
3293
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003294 aio_context = bdrv_get_aio_context(bs);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003295 aio_context_acquire(aio_context);
3296
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003297 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
Fam Zheng0d978912016-05-23 10:19:35 +08003298 if (!target_bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003299 goto out;
3300 }
Max Reitz5433c242015-10-19 17:53:29 +02003301
Fam Zhengefd75562016-05-23 10:19:36 +08003302 if (bdrv_get_aio_context(target_bs) != aio_context) {
3303 if (!bdrv_has_blk(target_bs)) {
3304 /* The target BDS is not attached, we can safely move it to another
3305 * AioContext. */
3306 bdrv_set_aio_context(target_bs, aio_context);
3307 } else {
3308 error_setg(errp, "Target is attached to a different thread from "
3309 "source.");
3310 goto out;
3311 }
3312 }
John Snow111049a2016-11-08 01:50:38 -05003313 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
3314 backup->sync, NULL, backup->compress,
3315 backup->on_source_error, backup->on_target_error,
3316 BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003317 if (local_err != NULL) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003318 error_propagate(errp, local_err);
3319 }
3320out:
3321 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05003322 return job;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003323}
3324
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003325void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003326{
John Snow111049a2016-11-08 01:50:38 -05003327 BlockJob *job;
3328 job = do_blockdev_backup(arg, NULL, errp);
3329 if (job) {
3330 block_job_start(job);
3331 }
John Snow78f51fd2015-11-05 18:13:17 -05003332}
3333
Fam Zheng4193cdd2015-12-24 12:45:03 +08003334/* Parameter check and block job starting for drive mirroring.
3335 * Caller should hold @device and @target's aio context (must be the same).
3336 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003337static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003338 BlockDriverState *target,
3339 bool has_replaces, const char *replaces,
3340 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003341 BlockMirrorBackingMode backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003342 bool has_speed, int64_t speed,
3343 bool has_granularity, uint32_t granularity,
3344 bool has_buf_size, int64_t buf_size,
3345 bool has_on_source_error,
3346 BlockdevOnError on_source_error,
3347 bool has_on_target_error,
3348 BlockdevOnError on_target_error,
3349 bool has_unmap, bool unmap,
3350 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003351{
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003352
3353 if (!has_speed) {
3354 speed = 0;
3355 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003356 if (!has_on_source_error) {
3357 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3358 }
3359 if (!has_on_target_error) {
3360 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3361 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003362 if (!has_granularity) {
3363 granularity = 0;
3364 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003365 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003366 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003367 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003368 if (!has_unmap) {
3369 unmap = true;
3370 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003371
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003372 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003373 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3374 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003375 return;
3376 }
3377 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003378 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3379 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003380 return;
3381 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003382
Fam Zheng4193cdd2015-12-24 12:45:03 +08003383 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3384 return;
3385 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003386 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3387 return;
3388 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003389
3390 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3391 sync = MIRROR_SYNC_MODE_FULL;
3392 }
3393
3394 /* pass the node name to replace to mirror start since it's loose coupling
3395 * and will allow to check whether the node still exist at mirror completion
3396 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003397 mirror_start(job_id, bs, target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003398 has_replaces ? replaces : NULL,
Max Reitz274fcce2016-06-10 20:57:47 +02003399 speed, granularity, buf_size, sync, backing_mode,
John Snow8254b6d2016-10-27 12:06:58 -04003400 on_source_error, on_target_error, unmap, errp);
Fam Zheng4193cdd2015-12-24 12:45:03 +08003401}
3402
Eric Blakefaecd402016-07-14 16:37:58 -06003403void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003404{
3405 BlockDriverState *bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003406 BlockDriverState *source, *target_bs;
3407 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003408 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003409 Error *local_err = NULL;
3410 QDict *options = NULL;
3411 int flags;
3412 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003413 const char *format = arg->format;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003414
Kevin Wolf0524e932016-06-23 14:20:24 +02003415 bs = qmp_get_root_bs(arg->device, errp);
3416 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003417 return;
3418 }
3419
Kevin Wolf0524e932016-06-23 14:20:24 +02003420 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003421 aio_context_acquire(aio_context);
3422
Eric Blakefaecd402016-07-14 16:37:58 -06003423 if (!arg->has_mode) {
3424 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003425 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003426
Eric Blakefaecd402016-07-14 16:37:58 -06003427 if (!arg->has_format) {
3428 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3429 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003430 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003431
Kevin Wolf61de4c62016-03-18 17:46:45 +01003432 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003433 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003434 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3435 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003436 }
Eric Blakefaecd402016-07-14 16:37:58 -06003437 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003438 source = bs;
3439 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003440
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003441 size = bdrv_getlength(bs);
3442 if (size < 0) {
3443 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003444 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003445 }
3446
Eric Blakefaecd402016-07-14 16:37:58 -06003447 if (arg->has_replaces) {
Benoît Canet09158f02014-06-27 18:25:25 +02003448 BlockDriverState *to_replace_bs;
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003449 AioContext *replace_aio_context;
3450 int64_t replace_size;
Benoît Canet09158f02014-06-27 18:25:25 +02003451
Eric Blakefaecd402016-07-14 16:37:58 -06003452 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003453 error_setg(errp, "a node-name must be provided when replacing a"
3454 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003455 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003456 }
3457
Eric Blakefaecd402016-07-14 16:37:58 -06003458 to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err);
Benoît Canet09158f02014-06-27 18:25:25 +02003459
3460 if (!to_replace_bs) {
3461 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003462 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003463 }
3464
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003465 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3466 aio_context_acquire(replace_aio_context);
3467 replace_size = bdrv_getlength(to_replace_bs);
3468 aio_context_release(replace_aio_context);
3469
3470 if (size != replace_size) {
Benoît Canet09158f02014-06-27 18:25:25 +02003471 error_setg(errp, "cannot replace image with a mirror image of "
3472 "different size");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003473 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003474 }
3475 }
3476
Eric Blakefaecd402016-07-14 16:37:58 -06003477 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003478 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3479 } else {
3480 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3481 }
3482
Eric Blakefaecd402016-07-14 16:37:58 -06003483 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3484 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003485 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003486 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003487 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003488 bdrv_img_create(arg->target, format,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003489 NULL, NULL, NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003490 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003491 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003492 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003493 break;
3494 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3495 /* create new image with backing file */
Eric Blakefaecd402016-07-14 16:37:58 -06003496 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003497 source->filename,
3498 source->drv->format_name,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003499 NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003500 break;
3501 default:
3502 abort();
3503 }
3504 }
3505
Markus Armbruster84d18f02014-01-30 15:07:28 +01003506 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003507 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003508 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003509 }
3510
Max Reitze6641712015-08-26 19:47:48 +02003511 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003512 if (arg->has_node_name) {
3513 qdict_put(options, "node-name", qstring_from_str(arg->node_name));
Benoît Canet4c828dc2014-06-16 12:00:55 +02003514 }
Max Reitze6641712015-08-26 19:47:48 +02003515 if (format) {
3516 qdict_put(options, "driver", qstring_from_str(format));
3517 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003518
Paolo Bonzinib812f672013-01-21 17:09:43 +01003519 /* Mirroring takes care of copy-on-write using the source's backing
3520 * file.
3521 */
Eric Blakefaecd402016-07-14 16:37:58 -06003522 target_bs = bdrv_open(arg->target, NULL, options,
3523 flags | BDRV_O_NO_BACKING, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003524 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003525 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003526 }
3527
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003528 bdrv_set_aio_context(target_bs, aio_context);
3529
Eric Blakefaecd402016-07-14 16:37:58 -06003530 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3531 arg->has_replaces, arg->replaces, arg->sync,
3532 backing_mode, arg->has_speed, arg->speed,
3533 arg->has_granularity, arg->granularity,
3534 arg->has_buf_size, arg->buf_size,
3535 arg->has_on_source_error, arg->on_source_error,
3536 arg->has_on_target_error, arg->on_target_error,
3537 arg->has_unmap, arg->unmap,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003538 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02003539 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003540 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003541out:
3542 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003543}
3544
Alberto Garcia71aa9862016-07-05 17:28:57 +03003545void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3546 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08003547 bool has_replaces, const char *replaces,
3548 MirrorSyncMode sync,
3549 bool has_speed, int64_t speed,
3550 bool has_granularity, uint32_t granularity,
3551 bool has_buf_size, int64_t buf_size,
3552 bool has_on_source_error,
3553 BlockdevOnError on_source_error,
3554 bool has_on_target_error,
3555 BlockdevOnError on_target_error,
3556 Error **errp)
3557{
3558 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08003559 BlockDriverState *target_bs;
3560 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003561 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08003562 Error *local_err = NULL;
3563
Kevin Wolf07eec652016-06-23 14:20:24 +02003564 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08003565 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08003566 return;
3567 }
3568
3569 target_bs = bdrv_lookup_bs(target, target, errp);
3570 if (!target_bs) {
3571 return;
3572 }
3573
3574 aio_context = bdrv_get_aio_context(bs);
3575 aio_context_acquire(aio_context);
3576
Fam Zhengdf925622015-12-24 12:45:05 +08003577 bdrv_set_aio_context(target_bs, aio_context);
3578
Alberto Garcia71aa9862016-07-05 17:28:57 +03003579 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02003580 has_replaces, replaces, sync, backing_mode,
Fam Zhengdf925622015-12-24 12:45:05 +08003581 has_speed, speed,
3582 has_granularity, granularity,
3583 has_buf_size, buf_size,
3584 has_on_source_error, on_source_error,
3585 has_on_target_error, on_target_error,
3586 true, true,
3587 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003588 error_propagate(errp, local_err);
Fam Zhengdf925622015-12-24 12:45:05 +08003589
3590 aio_context_release(aio_context);
3591}
3592
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003593/* Get a block job using its ID and acquire its AioContext */
3594static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003595 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003596{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003597 BlockJob *job;
3598
3599 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003600
Max Reitz5433c242015-10-19 17:53:29 +02003601 *aio_context = NULL;
3602
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003603 job = block_job_get(id);
3604
3605 if (!job) {
3606 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3607 "Block job '%s' not found", id);
3608 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003609 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003610
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003611 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003612 aio_context_acquire(*aio_context);
3613
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003614 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003615}
3616
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003617void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003618{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003619 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003620 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003621
3622 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003623 return;
3624 }
3625
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003626 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003627 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003628}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003629
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003630void qmp_block_job_cancel(const char *device,
3631 bool has_force, bool force, Error **errp)
3632{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003633 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003634 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003635
3636 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003637 return;
3638 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003639
3640 if (!has_force) {
3641 force = false;
3642 }
3643
John Snow0df4ba52016-10-27 12:06:59 -04003644 if (block_job_user_paused(job) && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003645 error_setg(errp, "The block job for device '%s' is currently paused",
3646 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003647 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003648 }
3649
3650 trace_qmp_block_job_cancel(job);
3651 block_job_cancel(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003652out:
3653 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003654}
3655
3656void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003657{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003658 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003659 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003660
John Snow0df4ba52016-10-27 12:06:59 -04003661 if (!job || block_job_user_paused(job)) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003662 return;
3663 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003664
3665 trace_qmp_block_job_pause(job);
John Snow0df4ba52016-10-27 12:06:59 -04003666 block_job_user_pause(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003667 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003668}
3669
3670void qmp_block_job_resume(const char *device, Error **errp)
3671{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003672 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003673 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003674
John Snow0df4ba52016-10-27 12:06:59 -04003675 if (!job || !block_job_user_paused(job)) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02003676 return;
3677 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003678
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003679 trace_qmp_block_job_resume(job);
Stefan Hajnoczi17bd51f2016-06-16 17:56:22 +01003680 block_job_iostatus_reset(job);
John Snow0df4ba52016-10-27 12:06:59 -04003681 block_job_user_resume(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003682 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003683}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003684
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003685void qmp_block_job_complete(const char *device, Error **errp)
3686{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003687 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003688 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003689
3690 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003691 return;
3692 }
3693
3694 trace_qmp_block_job_complete(job);
3695 block_job_complete(job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003696 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003697}
3698
Jeff Codyfa40e652014-07-01 09:52:16 +02003699void qmp_change_backing_file(const char *device,
3700 const char *image_node_name,
3701 const char *backing_file,
3702 Error **errp)
3703{
3704 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003705 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02003706 BlockDriverState *image_bs = NULL;
3707 Error *local_err = NULL;
3708 bool ro;
3709 int open_flags;
3710 int ret;
3711
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003712 bs = qmp_get_root_bs(device, errp);
3713 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02003714 return;
3715 }
3716
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003717 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003718 aio_context_acquire(aio_context);
3719
Jeff Codyfa40e652014-07-01 09:52:16 +02003720 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3721 if (local_err) {
3722 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003723 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003724 }
3725
3726 if (!image_bs) {
3727 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003728 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003729 }
3730
3731 if (bdrv_find_base(image_bs) == image_bs) {
3732 error_setg(errp, "not allowing backing file change on an image "
3733 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003734 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003735 }
3736
3737 /* even though we are not necessarily operating on bs, we need it to
3738 * determine if block ops are currently prohibited on the chain */
3739 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003740 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003741 }
3742
3743 /* final sanity check */
3744 if (!bdrv_chain_contains(bs, image_bs)) {
3745 error_setg(errp, "'%s' and image file are not in the same chain",
3746 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003747 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003748 }
3749
3750 /* if not r/w, reopen to make r/w */
3751 open_flags = image_bs->open_flags;
3752 ro = bdrv_is_read_only(image_bs);
3753
3754 if (ro) {
3755 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3756 if (local_err) {
3757 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003758 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003759 }
3760 }
3761
3762 ret = bdrv_change_backing_file(image_bs, backing_file,
3763 image_bs->drv ? image_bs->drv->format_name : "");
3764
3765 if (ret < 0) {
3766 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3767 backing_file);
3768 /* don't exit here, so we can try to restore open flags if
3769 * appropriate */
3770 }
3771
3772 if (ro) {
3773 bdrv_reopen(image_bs, open_flags, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003774 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02003775 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003776
3777out:
3778 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02003779}
3780
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003781void hmp_drive_add_node(Monitor *mon, const char *optstr)
3782{
3783 QemuOpts *opts;
3784 QDict *qdict;
3785 Error *local_err = NULL;
3786
3787 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
3788 if (!opts) {
3789 return;
3790 }
3791
3792 qdict = qemu_opts_to_qdict(opts, NULL);
3793
3794 if (!qdict_get_try_str(qdict, "node-name")) {
Kevin Wolff8746fb2016-03-16 11:14:31 +01003795 QDECREF(qdict);
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003796 error_report("'node-name' needs to be specified");
3797 goto out;
3798 }
3799
3800 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
3801 if (!bs) {
3802 error_report_err(local_err);
3803 goto out;
3804 }
3805
3806 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3807
3808out:
3809 qemu_opts_del(opts);
3810}
3811
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003812void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3813{
Max Reitzbe4b67b2015-10-19 17:53:09 +02003814 BlockDriverState *bs;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003815 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01003816 Visitor *v = qobject_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003817 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003818 Error *local_err = NULL;
3819
Eric Blake3b098d52016-06-09 10:48:43 -06003820 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003821 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003822 error_propagate(errp, local_err);
3823 goto fail;
3824 }
3825
Eric Blake3b098d52016-06-09 10:48:43 -06003826 visit_complete(v, &obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003827 qdict = qobject_to_qdict(obj);
3828
3829 qdict_flatten(qdict);
3830
Kevin Wolf9ec88732016-09-21 14:56:11 +02003831 if (!qdict_get_try_str(qdict, "node-name")) {
3832 error_setg(errp, "'node-name' must be specified for the root node");
3833 goto fail;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003834 }
3835
Kevin Wolf9ec88732016-09-21 14:56:11 +02003836 bs = bds_tree_init(qdict, errp);
3837 if (!bs) {
3838 goto fail;
3839 }
3840
3841 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3842
Max Reitzbe4b67b2015-10-19 17:53:09 +02003843 if (bs && bdrv_key_required(bs)) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02003844 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3845 bdrv_unref(bs);
Kevin Wolf8ae8e902014-03-06 15:43:42 +01003846 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3847 goto fail;
3848 }
3849
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003850fail:
Eric Blake3b098d52016-06-09 10:48:43 -06003851 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003852}
3853
Kevin Wolf9ec88732016-09-21 14:56:11 +02003854void qmp_x_blockdev_del(const char *node_name, Error **errp)
Alberto Garcia81b936a2015-11-02 16:51:55 +02003855{
3856 AioContext *aio_context;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003857 BlockDriverState *bs;
3858
Kevin Wolf9ec88732016-09-21 14:56:11 +02003859 bs = bdrv_find_node(node_name);
3860 if (!bs) {
3861 error_setg(errp, "Cannot find node %s", node_name);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003862 return;
3863 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003864 if (bdrv_has_blk(bs)) {
3865 error_setg(errp, "Node %s is in use", node_name);
3866 return;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003867 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003868 aio_context = bdrv_get_aio_context(bs);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003869 aio_context_acquire(aio_context);
3870
Kevin Wolf9ec88732016-09-21 14:56:11 +02003871 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3872 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003873 }
3874
Kevin Wolf9ec88732016-09-21 14:56:11 +02003875 if (!bs->monitor_list.tqe_prev) {
3876 error_setg(errp, "Node %s is not owned by the monitor",
3877 bs->node_name);
3878 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003879 }
3880
Kevin Wolf9ec88732016-09-21 14:56:11 +02003881 if (bs->refcnt > 1) {
3882 error_setg(errp, "Block device %s is in use",
3883 bdrv_get_device_or_node_name(bs));
3884 goto out;
3885 }
3886
3887 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3888 bdrv_unref(bs);
3889
Alberto Garcia81b936a2015-11-02 16:51:55 +02003890out:
3891 aio_context_release(aio_context);
3892}
3893
Wen Congyang7f821592016-05-10 15:36:39 +08003894static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
3895 const char *child_name)
3896{
3897 BdrvChild *child;
3898
3899 QLIST_FOREACH(child, &parent_bs->children, next) {
3900 if (strcmp(child->name, child_name) == 0) {
3901 return child;
3902 }
3903 }
3904
3905 return NULL;
3906}
3907
3908void qmp_x_blockdev_change(const char *parent, bool has_child,
3909 const char *child, bool has_node,
3910 const char *node, Error **errp)
3911{
3912 BlockDriverState *parent_bs, *new_bs = NULL;
3913 BdrvChild *p_child;
3914
3915 parent_bs = bdrv_lookup_bs(parent, parent, errp);
3916 if (!parent_bs) {
3917 return;
3918 }
3919
3920 if (has_child == has_node) {
3921 if (has_child) {
3922 error_setg(errp, "The parameters child and node are in conflict");
3923 } else {
3924 error_setg(errp, "Either child or node must be specified");
3925 }
3926 return;
3927 }
3928
3929 if (has_child) {
3930 p_child = bdrv_find_child(parent_bs, child);
3931 if (!p_child) {
3932 error_setg(errp, "Node '%s' does not have child '%s'",
3933 parent, child);
3934 return;
3935 }
3936 bdrv_del_child(parent_bs, p_child, errp);
3937 }
3938
3939 if (has_node) {
3940 new_bs = bdrv_find_node(node);
3941 if (!new_bs) {
3942 error_setg(errp, "Node '%s' not found", node);
3943 return;
3944 }
3945 bdrv_add_child(parent_bs, new_bs, errp);
3946 }
3947}
3948
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003949BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3950{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003951 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003952 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003953
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003954 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
John Snow559b9352016-10-27 12:06:55 -04003955 BlockJobInfoList *elem;
3956 AioContext *aio_context;
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003957
John Snow559b9352016-10-27 12:06:55 -04003958 if (block_job_is_internal(job)) {
3959 continue;
3960 }
3961 elem = g_new0(BlockJobInfoList, 1);
3962 aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003963 aio_context_acquire(aio_context);
John Snow559b9352016-10-27 12:06:55 -04003964 elem->value = block_job_query(job, errp);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003965 aio_context_release(aio_context);
John Snow559b9352016-10-27 12:06:55 -04003966 if (!elem->value) {
3967 g_free(elem);
3968 qapi_free_BlockJobInfoList(head);
3969 return NULL;
3970 }
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003971 *p_next = elem;
3972 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003973 }
3974
3975 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003976}
Paolo Bonzini4d454572012-11-26 16:03:42 +01003977
Kevin Wolf00063832013-03-15 10:35:07 +01003978QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01003979 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01003980 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01003981 .desc = {
3982 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01003983 .name = "snapshot",
3984 .type = QEMU_OPT_BOOL,
3985 .help = "enable/disable snapshot mode",
3986 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003987 .name = "aio",
3988 .type = QEMU_OPT_STRING,
3989 .help = "host AIO implementation (threads, native)",
3990 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01003991 .name = BDRV_OPT_CACHE_WB,
3992 .type = QEMU_OPT_BOOL,
3993 .help = "Enable writeback mode",
3994 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003995 .name = "format",
3996 .type = QEMU_OPT_STRING,
3997 .help = "disk format (raw, qcow2, ...)",
3998 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003999 .name = "rerror",
4000 .type = QEMU_OPT_STRING,
4001 .help = "read error action",
4002 },{
4003 .name = "werror",
4004 .type = QEMU_OPT_STRING,
4005 .help = "write error action",
4006 },{
Alberto Garcia4e200cf2016-09-15 17:53:05 +03004007 .name = BDRV_OPT_READ_ONLY,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004008 .type = QEMU_OPT_BOOL,
4009 .help = "open drive file as read-only",
4010 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004011 .name = "throttling.iops-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004012 .type = QEMU_OPT_NUMBER,
4013 .help = "limit total I/O operations per second",
4014 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004015 .name = "throttling.iops-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004016 .type = QEMU_OPT_NUMBER,
4017 .help = "limit read operations per second",
4018 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004019 .name = "throttling.iops-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004020 .type = QEMU_OPT_NUMBER,
4021 .help = "limit write operations per second",
4022 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004023 .name = "throttling.bps-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004024 .type = QEMU_OPT_NUMBER,
4025 .help = "limit total bytes per second",
4026 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004027 .name = "throttling.bps-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004028 .type = QEMU_OPT_NUMBER,
4029 .help = "limit read bytes per second",
4030 },{
Kevin Wolf57975222013-07-17 14:41:54 +02004031 .name = "throttling.bps-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004032 .type = QEMU_OPT_NUMBER,
4033 .help = "limit write bytes per second",
4034 },{
Benoît Canet3e9fab62013-09-02 14:14:40 +02004035 .name = "throttling.iops-total-max",
4036 .type = QEMU_OPT_NUMBER,
4037 .help = "I/O operations burst",
4038 },{
4039 .name = "throttling.iops-read-max",
4040 .type = QEMU_OPT_NUMBER,
4041 .help = "I/O operations read burst",
4042 },{
4043 .name = "throttling.iops-write-max",
4044 .type = QEMU_OPT_NUMBER,
4045 .help = "I/O operations write burst",
4046 },{
4047 .name = "throttling.bps-total-max",
4048 .type = QEMU_OPT_NUMBER,
4049 .help = "total bytes burst",
4050 },{
4051 .name = "throttling.bps-read-max",
4052 .type = QEMU_OPT_NUMBER,
4053 .help = "total bytes read burst",
4054 },{
4055 .name = "throttling.bps-write-max",
4056 .type = QEMU_OPT_NUMBER,
4057 .help = "total bytes write burst",
4058 },{
Alberto Garcia8a0fc182016-02-18 12:27:02 +02004059 .name = "throttling.iops-total-max-length",
4060 .type = QEMU_OPT_NUMBER,
4061 .help = "length of the iops-total-max burst period, in seconds",
4062 },{
4063 .name = "throttling.iops-read-max-length",
4064 .type = QEMU_OPT_NUMBER,
4065 .help = "length of the iops-read-max burst period, in seconds",
4066 },{
4067 .name = "throttling.iops-write-max-length",
4068 .type = QEMU_OPT_NUMBER,
4069 .help = "length of the iops-write-max burst period, in seconds",
4070 },{
4071 .name = "throttling.bps-total-max-length",
4072 .type = QEMU_OPT_NUMBER,
4073 .help = "length of the bps-total-max burst period, in seconds",
4074 },{
4075 .name = "throttling.bps-read-max-length",
4076 .type = QEMU_OPT_NUMBER,
4077 .help = "length of the bps-read-max burst period, in seconds",
4078 },{
4079 .name = "throttling.bps-write-max-length",
4080 .type = QEMU_OPT_NUMBER,
4081 .help = "length of the bps-write-max burst period, in seconds",
4082 },{
Benoît Canet2024c1d2013-09-02 14:14:41 +02004083 .name = "throttling.iops-size",
4084 .type = QEMU_OPT_NUMBER,
4085 .help = "when limiting by iops max size of an I/O in bytes",
4086 },{
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004087 .name = "throttling.group",
4088 .type = QEMU_OPT_STRING,
4089 .help = "name of the block throttling group",
4090 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004091 .name = "copy-on-read",
4092 .type = QEMU_OPT_BOOL,
4093 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004094 },{
4095 .name = "detect-zeroes",
4096 .type = QEMU_OPT_STRING,
4097 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004098 },{
4099 .name = "stats-account-invalid",
4100 .type = QEMU_OPT_BOOL,
4101 .help = "whether to account for invalid I/O operations "
4102 "in the statistics",
4103 },{
4104 .name = "stats-account-failed",
4105 .type = QEMU_OPT_BOOL,
4106 .help = "whether to account for failed I/O operations "
4107 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004108 },
4109 { /* end of list */ }
4110 },
4111};
Kevin Wolf00063832013-03-15 10:35:07 +01004112
4113QemuOptsList qemu_drive_opts = {
4114 .name = "drive",
4115 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4116 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004117 /*
4118 * no elements => accept any params
4119 * validation will happen later
4120 */
Kevin Wolf00063832013-03-15 10:35:07 +01004121 { /* end of list */ }
4122 },
4123};