blob: dcb7a6d54e27007f5a3ad30a4e7ccd00dbbec037 [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"
Markus Armbruster9af23982018-02-11 10:36:01 +010043#include "qapi/qapi-commands-block.h"
44#include "qapi/qapi-commands-transaction.h"
45#include "qapi/qapi-visit-block-core.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010046#include "qapi/qmp/qdict.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010047#include "qapi/qmp/qnum.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010048#include "qapi/qmp/qstring.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010049#include "qapi/error.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010050#include "qapi/qmp/qerror.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010051#include "qapi/qmp/qlist.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010052#include "qapi/qobject-output-visitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010053#include "sysemu/sysemu.h"
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +000054#include "sysemu/iothread.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010055#include "block/block_int.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000056#include "block/trace.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010057#include "sysemu/arch_init.h"
Thomas Huthc616f162017-05-12 12:33:49 +020058#include "sysemu/qtest.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020059#include "qemu/cutils.h"
60#include "qemu/help_option.h"
Pradeep Jagadeesha2a78622017-02-28 10:31:46 +010061#include "qemu/throttle-options.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020062
Max Reitz9c4218e2016-01-29 16:36:12 +010063static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
64 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
65
Kevin Wolfb33945c2016-09-20 13:38:43 +020066static int do_open_tray(const char *blk_name, const char *qdev_id,
67 bool force, Error **errp);
Max Reitz82fcf662017-11-10 23:43:01 +010068static void blockdev_remove_medium(bool has_device, const char *device,
69 bool has_id, const char *id, Error **errp);
70static void blockdev_insert_medium(bool has_device, const char *device,
71 bool has_id, const char *id,
72 const char *node_name, Error **errp);
Colin Lordbf18bee2016-06-06 14:15:22 -040073
Markus Armbruster19609662011-01-28 11:21:39 +010074static const char *const if_name[IF_COUNT] = {
75 [IF_NONE] = "none",
76 [IF_IDE] = "ide",
77 [IF_SCSI] = "scsi",
78 [IF_FLOPPY] = "floppy",
79 [IF_PFLASH] = "pflash",
80 [IF_MTD] = "mtd",
81 [IF_SD] = "sd",
82 [IF_VIRTIO] = "virtio",
83 [IF_XEN] = "xen",
84};
85
John Snow21dff8c2014-10-01 14:19:25 -040086static int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010087 /*
88 * Do not change these numbers! They govern how drive option
89 * index maps to unit and bus. That mapping is ABI.
90 *
Wei Jiangang547cb152016-04-26 18:12:43 +080091 * All controllers used to implement if=T drives need to support
Markus Armbruster27d6bf42011-01-28 11:21:40 +010092 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
93 * Otherwise, some index values map to "impossible" bus, unit
94 * values.
95 *
96 * For instance, if you change [IF_SCSI] to 255, -drive
97 * if=scsi,index=12 no longer means bus=1,unit=5, but
98 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
99 * the drive can't be set up. Regression.
100 */
101 [IF_IDE] = 2,
102 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +0100103};
104
John Snow21dff8c2014-10-01 14:19:25 -0400105/**
106 * Boards may call this to offer board-by-board overrides
107 * of the default, global values.
108 */
109void override_max_devs(BlockInterfaceType type, int max_devs)
110{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200111 BlockBackend *blk;
John Snow21dff8c2014-10-01 14:19:25 -0400112 DriveInfo *dinfo;
113
114 if (max_devs <= 0) {
115 return;
116 }
117
Markus Armbruster18e46a02014-10-07 13:59:06 +0200118 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
119 dinfo = blk_legacy_dinfo(blk);
John Snow21dff8c2014-10-01 14:19:25 -0400120 if (dinfo->type == type) {
121 fprintf(stderr, "Cannot override units-per-bus property of"
122 " the %s interface, because a drive of that type has"
123 " already been added.\n", if_name[type]);
124 g_assert_not_reached();
125 }
126 }
127
128 if_max_devs[type] = max_devs;
129}
130
Markus Armbruster14bafc52010-06-25 08:09:10 +0200131/*
132 * We automatically delete the drive when a device using it gets
133 * unplugged. Questionable feature, but we can't just drop it.
134 * Device models call blockdev_mark_auto_del() to schedule the
135 * automatic deletion, and generic qdev code calls blockdev_auto_del()
136 * when deletion is actually safe.
137 */
Markus Armbruster4be74632014-10-07 13:59:18 +0200138void blockdev_mark_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200139{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200140 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster4be74632014-10-07 13:59:18 +0200141 BlockDriverState *bs = blk_bs(blk);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100142 AioContext *aio_context;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200143
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200144 if (!dinfo) {
Kevin Wolf2d246f02013-09-18 15:14:47 +0200145 return;
146 }
147
Max Reitz5433c242015-10-19 17:53:29 +0200148 if (bs) {
149 aio_context = bdrv_get_aio_context(bs);
150 aio_context_acquire(aio_context);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100151
Max Reitz5433c242015-10-19 17:53:29 +0200152 if (bs->job) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200153 job_cancel(&bs->job->job, false);
Max Reitz5433c242015-10-19 17:53:29 +0200154 }
155
156 aio_context_release(aio_context);
Paolo Bonzini12bde0e2012-03-30 13:17:10 +0200157 }
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100158
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200159 dinfo->auto_del = 1;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200160}
161
Markus Armbruster4be74632014-10-07 13:59:18 +0200162void blockdev_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200163{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200164 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200165
Ryan Harper0fc0f1f2010-12-08 10:05:00 -0600166 if (dinfo && dinfo->auto_del) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100167 monitor_remove_blk(blk);
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +0200168 blk_unref(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200169 }
170}
171
John Snowd8f94e12014-10-01 14:19:27 -0400172/**
173 * Returns the current mapping of how many units per bus
174 * a particular interface can support.
175 *
176 * A positive integer indicates n units per bus.
177 * 0 implies the mapping has not been established.
178 * -1 indicates an invalid BlockInterfaceType was given.
179 */
180int drive_get_max_devs(BlockInterfaceType type)
181{
182 if (type >= IF_IDE && type < IF_COUNT) {
183 return if_max_devs[type];
184 }
185
186 return -1;
187}
188
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100189static int drive_index_to_bus_id(BlockInterfaceType type, int index)
190{
191 int max_devs = if_max_devs[type];
192 return max_devs ? index / max_devs : 0;
193}
194
195static int drive_index_to_unit_id(BlockInterfaceType type, int index)
196{
197 int max_devs = if_max_devs[type];
198 return max_devs ? index % max_devs : index;
199}
200
Markus Armbruster2292dda2011-01-28 11:21:41 +0100201QemuOpts *drive_def(const char *optstr)
202{
Markus Armbruster70b94332015-02-13 12:50:26 +0100203 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100204}
205
206QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +0100207 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +0200208{
Markus Armbruster666daa62010-06-02 18:48:27 +0200209 QemuOpts *opts;
210
Markus Armbruster2292dda2011-01-28 11:21:41 +0100211 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200212 if (!opts) {
213 return NULL;
214 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100215 if (type != IF_DEFAULT) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100216 qemu_opt_set(opts, "if", if_name[type], &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100217 }
218 if (index >= 0) {
Markus Armbrustera8b18f82015-02-13 15:50:43 +0100219 qemu_opt_set_number(opts, "index", index, &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100220 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200221 if (file)
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100222 qemu_opt_set(opts, "file", file, &error_abort);
Markus Armbruster666daa62010-06-02 18:48:27 +0200223 return opts;
224}
225
226DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
227{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200228 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200229 DriveInfo *dinfo;
230
Markus Armbruster18e46a02014-10-07 13:59:06 +0200231 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
232 dinfo = blk_legacy_dinfo(blk);
233 if (dinfo && dinfo->type == type
234 && dinfo->bus == bus && dinfo->unit == unit) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200235 return dinfo;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200236 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200237 }
238
239 return NULL;
240}
241
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100242void drive_check_orphaned(void)
John Snowa66c9dc2014-10-01 14:19:24 -0400243{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200244 BlockBackend *blk;
John Snowa66c9dc2014-10-01 14:19:24 -0400245 DriveInfo *dinfo;
Markus Armbruster664cc622017-02-15 11:05:45 +0100246 Location loc;
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100247 bool orphans = false;
John Snowa66c9dc2014-10-01 14:19:24 -0400248
Markus Armbruster18e46a02014-10-07 13:59:06 +0200249 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
250 dinfo = blk_legacy_dinfo(blk);
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200251 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
John Snowa66c9dc2014-10-01 14:19:24 -0400252 dinfo->type != IF_NONE) {
Markus Armbruster664cc622017-02-15 11:05:45 +0100253 loc_push_none(&loc);
254 qemu_opts_loc_restore(dinfo->opts);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100255 error_report("machine type does not support"
Markus Armbruster664cc622017-02-15 11:05:45 +0100256 " if=%s,bus=%d,unit=%d",
257 if_name[dinfo->type], dinfo->bus, dinfo->unit);
258 loc_pop(&loc);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100259 orphans = true;
John Snowa66c9dc2014-10-01 14:19:24 -0400260 }
261 }
262
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100263 if (orphans) {
264 exit(1);
265 }
John Snowa66c9dc2014-10-01 14:19:24 -0400266}
267
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100268DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
269{
270 return drive_get(type,
271 drive_index_to_bus_id(type, index),
272 drive_index_to_unit_id(type, index));
273}
274
Markus Armbruster666daa62010-06-02 18:48:27 +0200275int drive_get_max_bus(BlockInterfaceType type)
276{
277 int max_bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200278 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200279 DriveInfo *dinfo;
280
281 max_bus = -1;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200282 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
283 dinfo = blk_legacy_dinfo(blk);
284 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200285 max_bus = dinfo->bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200286 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200287 }
288 return max_bus;
289}
290
Markus Armbruster13839972011-01-28 11:21:37 +0100291/* Get a block device. This should only be used for single-drive devices
292 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
293 appropriate bus. */
294DriveInfo *drive_get_next(BlockInterfaceType type)
295{
296 static int next_block_unit[IF_COUNT];
297
298 return drive_get(type, 0, next_block_unit[type]++);
299}
300
Markus Armbruster666daa62010-06-02 18:48:27 +0200301static void bdrv_format_print(void *opaque, const char *name)
302{
Markus Armbruster807105a2011-01-17 19:31:27 +0100303 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200304}
305
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000306typedef struct {
307 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800308 BlockDriverState *bs;
309} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000310
Kevin Wolfb6810722013-09-20 11:33:11 +0200311static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200312{
313 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200314 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200315 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200316 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200317 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200318 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200319 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200320 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200321 } else {
Kevin Wolfb6810722013-09-20 11:33:11 +0200322 error_setg(errp, "'%s' invalid %s error action",
323 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200324 return -1;
325 }
326}
327
Alberto Garcia40119ef2015-11-16 11:28:38 +0200328static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
329 Error **errp)
330{
331 const QListEntry *entry;
332 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
333 switch (qobject_type(entry->value)) {
334
335 case QTYPE_QSTRING: {
336 unsigned long long length;
Max Reitz7dc847e2018-02-24 16:40:29 +0100337 const char *str = qstring_get_str(qobject_to(QString,
338 entry->value));
Alberto Garcia40119ef2015-11-16 11:28:38 +0200339 if (parse_uint_full(str, &length, 10) == 0 &&
340 length > 0 && length <= UINT_MAX) {
341 block_acct_add_interval(stats, (unsigned) length);
342 } else {
343 error_setg(errp, "Invalid interval length: %s", str);
344 return false;
345 }
346 break;
347 }
348
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400349 case QTYPE_QNUM: {
Max Reitz7dc847e2018-02-24 16:40:29 +0100350 int64_t length = qnum_get_int(qobject_to(QNum, entry->value));
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400351
Alberto Garcia40119ef2015-11-16 11:28:38 +0200352 if (length > 0 && length <= UINT_MAX) {
353 block_acct_add_interval(stats, (unsigned) length);
354 } else {
355 error_setg(errp, "Invalid interval length: %" PRId64, length);
356 return false;
357 }
358 break;
359 }
360
361 default:
362 error_setg(errp, "The specification of stats-intervals is invalid");
363 return false;
364 }
365 }
366 return true;
367}
368
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200369typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
370
Max Reitzfbf81752015-10-19 17:53:31 +0200371/* All parameters but @opts are optional and may be set to NULL. */
372static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
373 const char **throttling_group, ThrottleConfig *throttle_cfg,
374 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
375{
Max Reitzfbf81752015-10-19 17:53:31 +0200376 Error *local_error = NULL;
377 const char *aio;
378
379 if (bdrv_flags) {
Max Reitzfbf81752015-10-19 17:53:31 +0200380 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
381 *bdrv_flags |= BDRV_O_COPY_ON_READ;
382 }
383
Max Reitzfbf81752015-10-19 17:53:31 +0200384 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
385 if (!strcmp(aio, "native")) {
386 *bdrv_flags |= BDRV_O_NATIVE_AIO;
387 } else if (!strcmp(aio, "threads")) {
388 /* this is the default */
389 } else {
390 error_setg(errp, "invalid aio option");
391 return;
392 }
393 }
394 }
395
396 /* disk I/O throttling */
397 if (throttling_group) {
398 *throttling_group = qemu_opt_get(opts, "throttling.group");
399 }
400
401 if (throttle_cfg) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200402 throttle_config_init(throttle_cfg);
Max Reitzfbf81752015-10-19 17:53:31 +0200403 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
404 qemu_opt_get_number(opts, "throttling.bps-total", 0);
405 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
406 qemu_opt_get_number(opts, "throttling.bps-read", 0);
407 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
408 qemu_opt_get_number(opts, "throttling.bps-write", 0);
409 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
410 qemu_opt_get_number(opts, "throttling.iops-total", 0);
411 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
412 qemu_opt_get_number(opts, "throttling.iops-read", 0);
413 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
414 qemu_opt_get_number(opts, "throttling.iops-write", 0);
415
416 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
417 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
418 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
419 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
420 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
421 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
422 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
423 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
424 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
425 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
426 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
427 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
428
Alberto Garcia8a0fc182016-02-18 12:27:02 +0200429 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
430 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
431 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
432 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
433 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
434 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
435 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
436 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
437 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
438 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
439 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
440 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
441
Max Reitzfbf81752015-10-19 17:53:31 +0200442 throttle_cfg->op_size =
443 qemu_opt_get_number(opts, "throttling.iops-size", 0);
444
Alberto Garciad5851082016-02-18 12:26:59 +0200445 if (!throttle_is_valid(throttle_cfg, errp)) {
Max Reitzfbf81752015-10-19 17:53:31 +0200446 return;
447 }
448 }
449
450 if (detect_zeroes) {
451 *detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200452 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Max Reitzfbf81752015-10-19 17:53:31 +0200453 qemu_opt_get(opts, "detect-zeroes"),
Max Reitzfbf81752015-10-19 17:53:31 +0200454 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
455 &local_error);
456 if (local_error) {
457 error_propagate(errp, local_error);
458 return;
459 }
Max Reitzfbf81752015-10-19 17:53:31 +0200460 }
461}
462
Kevin Wolff298d072013-09-10 12:01:20 +0200463/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200464static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
465 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200466{
467 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200468 int bdrv_flags = 0;
469 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200470 bool account_invalid, account_failed;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300471 bool writethrough, read_only;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200472 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200473 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200474 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200475 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100476 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100477 QemuOpts *opts;
Alberto Garcia40119ef2015-11-16 11:28:38 +0200478 QDict *interval_dict = NULL;
479 QList *interval_list = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100480 const char *id;
Max Reitzfbf81752015-10-19 17:53:31 +0200481 BlockdevDetectZeroesOptions detect_zeroes =
482 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
483 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200484
Kevin Wolff298d072013-09-10 12:01:20 +0200485 /* Check common options by copying from bs_opts to opts, all other options
486 * stay in bs_opts for processing by bdrv_open(). */
487 id = qdict_get_try_str(bs_opts, "id");
Kevin Wolf00063832013-03-15 10:35:07 +0100488 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100489 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200490 error_propagate(errp, error);
Markus Armbruster6376f952014-05-28 11:17:01 +0200491 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100492 }
493
Kevin Wolf00063832013-03-15 10:35:07 +0100494 qemu_opts_absorb_qdict(opts, bs_opts, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100495 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200496 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100497 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100498 }
499
500 if (id) {
501 qdict_del(bs_opts, "id");
502 }
503
Markus Armbruster666daa62010-06-02 18:48:27 +0200504 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200505 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200506
Alberto Garcia362e9292015-10-28 17:33:04 +0200507 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
508 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
509
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100510 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
511
Alberto Garciaff356ee2016-07-08 17:03:00 +0300512 id = qemu_opts_id(opts);
513
Alberto Garcia40119ef2015-11-16 11:28:38 +0200514 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
515 qdict_array_split(interval_dict, &interval_list);
516
517 if (qdict_size(interval_dict) != 0) {
518 error_setg(errp, "Invalid option stats-intervals.%s",
519 qdict_first(interval_dict)->key);
520 goto early_err;
521 }
Alberto Garcia2be55062015-10-28 17:33:07 +0200522
Max Reitzfbf81752015-10-19 17:53:31 +0200523 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
524 &detect_zeroes, &error);
525 if (error) {
526 error_propagate(errp, error);
527 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200528 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200529
530 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100531 if (is_help_option(buf)) {
532 error_printf("Supported formats:");
533 bdrv_iterate_format(bdrv_format_print, NULL);
534 error_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100535 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200536 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200537
Max Reitze4342ce2015-02-05 13:58:14 -0500538 if (qdict_haskey(bs_opts, "driver")) {
539 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100540 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400541 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500542 qdict_put_str(bs_opts, "driver", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200543 }
544
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200545 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200546 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200547 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100548 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200549 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100550 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200551 }
552 }
553
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200554 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200555 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200556 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100557 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200558 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100559 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200560 }
561 }
562
Max Reitz5ec18f82015-10-19 17:53:30 +0200563 if (snapshot) {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200564 bdrv_flags |= BDRV_O_SNAPSHOT;
Max Reitz5ec18f82015-10-19 17:53:30 +0200565 }
566
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300567 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
568
Kevin Wolf326642b2013-07-11 12:52:34 +0200569 /* init */
Kevin Wolf39c4ae92015-11-13 14:45:42 +0100570 if ((!file || !*file) && !qdict_size(bs_opts)) {
Max Reitz5ec18f82015-10-19 17:53:30 +0200571 BlockBackendRootState *blk_rs;
572
Kevin Wolf6d0eb642017-01-20 17:07:26 +0100573 blk = blk_new(0, BLK_PERM_ALL);
Max Reitz5ec18f82015-10-19 17:53:30 +0200574 blk_rs = blk_get_root_state(blk);
575 blk_rs->open_flags = bdrv_flags;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300576 blk_rs->read_only = read_only;
Max Reitz5ec18f82015-10-19 17:53:30 +0200577 blk_rs->detect_zeroes = detect_zeroes;
578
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200579 qobject_unref(bs_opts);
Max Reitze4342ce2015-02-05 13:58:14 -0500580 } else {
581 if (file && !*file) {
582 file = NULL;
583 }
584
Kevin Wolf91a097e2015-05-08 17:49:53 +0200585 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
586 * with other callers) rather than what we want as the real defaults.
587 * Apply the defaults here instead. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200588 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
589 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300590 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
591 read_only ? "on" : "off");
Kevin Wolf72e775c2016-03-15 14:34:37 +0100592 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200593
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100594 if (runstate_check(RUN_STATE_INMIGRATE)) {
595 bdrv_flags |= BDRV_O_INACTIVE;
596 }
597
Max Reitzefaa7c42016-03-16 19:54:38 +0100598 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500599 if (!blk) {
600 goto err_no_bs_opts;
601 }
602 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500603
Max Reitz5ec18f82015-10-19 17:53:30 +0200604 bs->detect_zeroes = detect_zeroes;
605
Paolo Bonzini9caa6f32017-06-05 14:39:07 +0200606 block_acct_setup(blk_get_stats(blk), account_invalid, account_failed);
Alberto Garcia2be55062015-10-28 17:33:07 +0200607
Alberto Garcia40119ef2015-11-16 11:28:38 +0200608 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
609 blk_unref(blk);
610 blk = NULL;
611 goto err_no_bs_opts;
Alberto Garcia2be55062015-10-28 17:33:07 +0200612 }
Max Reitz5ec18f82015-10-19 17:53:30 +0200613 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200614
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100615 /* disk I/O throttling */
616 if (throttle_enabled(&cfg)) {
617 if (!throttling_group) {
Alberto Garciaff356ee2016-07-08 17:03:00 +0300618 throttling_group = id;
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100619 }
620 blk_io_limits_enable(blk, throttling_group);
621 blk_set_io_limits(blk, &cfg);
622 }
623
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100624 blk_set_enable_write_cache(blk, !writethrough);
Max Reitz373340b2015-10-19 17:53:22 +0200625 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200626
Alberto Garciaff356ee2016-07-08 17:03:00 +0300627 if (!monitor_add_blk(blk, id, errp)) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100628 blk_unref(blk);
629 blk = NULL;
630 goto err_no_bs_opts;
631 }
632
Max Reitze4342ce2015-02-05 13:58:14 -0500633err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100634 qemu_opts_del(opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200635 qobject_unref(interval_dict);
636 qobject_unref(interval_list);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200637 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100638
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100639early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100640 qemu_opts_del(opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200641 qobject_unref(interval_dict);
642 qobject_unref(interval_list);
Markus Armbruster6376f952014-05-28 11:17:01 +0200643err_no_opts:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200644 qobject_unref(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100645 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200646}
647
Max Reitzbd745e22015-10-19 17:53:32 +0200648/* Takes the ownership of bs_opts */
649static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
650{
Max Reitzbd745e22015-10-19 17:53:32 +0200651 int bdrv_flags = 0;
652
Kevin Wolfa81d6162016-03-07 14:23:04 +0100653 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
654 * with other callers) rather than what we want as the real defaults.
655 * Apply the defaults here instead. */
Kevin Wolfa81d6162016-03-07 14:23:04 +0100656 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
657 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300658 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
Kevin Wolfa81d6162016-03-07 14:23:04 +0100659
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100660 if (runstate_check(RUN_STATE_INMIGRATE)) {
661 bdrv_flags |= BDRV_O_INACTIVE;
662 }
663
Kevin Wolf74e1ae72016-09-20 20:48:52 +0200664 return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
Max Reitzbd745e22015-10-19 17:53:32 +0200665}
666
Max Reitz9c4218e2016-01-29 16:36:12 +0100667void blockdev_close_all_bdrv_states(void)
668{
669 BlockDriverState *bs, *next_bs;
670
671 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
672 AioContext *ctx = bdrv_get_aio_context(bs);
673
674 aio_context_acquire(ctx);
675 bdrv_unref(bs);
676 aio_context_release(ctx);
677 }
678}
679
Max Reitz262b4e82016-03-16 19:54:41 +0100680/* Iterates over the list of monitor-owned BlockDriverStates */
681BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
682{
683 return bs ? QTAILQ_NEXT(bs, monitor_list)
684 : QTAILQ_FIRST(&monitor_bdrv_states);
685}
686
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200687static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
688 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200689{
690 const char *value;
691
692 value = qemu_opt_get(opts, from);
693 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200694 if (qemu_opt_find(opts, to)) {
695 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
696 "same time", to, from);
697 return;
698 }
Jun Li20d6cd42014-09-24 13:45:27 +0800699 }
700
701 /* rename all items in opts */
702 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100703 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200704 qemu_opt_unset(opts, from);
705 }
706}
707
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200708QemuOptsList qemu_legacy_drive_opts = {
709 .name = "drive",
710 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
711 .desc = {
712 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200713 .name = "bus",
714 .type = QEMU_OPT_NUMBER,
715 .help = "bus number",
716 },{
717 .name = "unit",
718 .type = QEMU_OPT_NUMBER,
719 .help = "unit number (i.e. lun for scsi)",
720 },{
721 .name = "index",
722 .type = QEMU_OPT_NUMBER,
723 .help = "index number",
724 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200725 .name = "media",
726 .type = QEMU_OPT_STRING,
727 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200728 },{
729 .name = "if",
730 .type = QEMU_OPT_STRING,
731 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200732 },{
733 .name = "cyls",
734 .type = QEMU_OPT_NUMBER,
735 .help = "number of cylinders (ide disk geometry)",
736 },{
737 .name = "heads",
738 .type = QEMU_OPT_NUMBER,
739 .help = "number of heads (ide disk geometry)",
740 },{
741 .name = "secs",
742 .type = QEMU_OPT_NUMBER,
743 .help = "number of sectors (ide disk geometry)",
744 },{
745 .name = "trans",
746 .type = QEMU_OPT_STRING,
747 .help = "chs translation (auto, lba, none)",
Kevin Wolf26929292013-09-09 17:01:03 +0200748 },{
Kevin Wolf394c7d42013-09-13 14:09:17 +0200749 .name = "addr",
750 .type = QEMU_OPT_STRING,
751 .help = "pci address (virtio only)",
Max Reitzd095b462013-12-20 19:28:14 +0100752 },{
Kevin Wolfbcf83152014-06-06 15:21:48 +0200753 .name = "serial",
754 .type = QEMU_OPT_STRING,
755 .help = "disk serial number",
756 },{
Max Reitzd095b462013-12-20 19:28:14 +0100757 .name = "file",
758 .type = QEMU_OPT_STRING,
759 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200760 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200761
762 /* Options that are passed on, but have special semantics with -drive */
763 {
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300764 .name = BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200765 .type = QEMU_OPT_BOOL,
766 .help = "open drive file as read-only",
767 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100768 .name = "rerror",
769 .type = QEMU_OPT_STRING,
770 .help = "read error action",
771 },{
772 .name = "werror",
773 .type = QEMU_OPT_STRING,
774 .help = "write error action",
775 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200776 .name = "copy-on-read",
777 .type = QEMU_OPT_BOOL,
778 .help = "copy read data from backing file into image file",
779 },
780
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200781 { /* end of list */ }
782 },
783};
784
Markus Armbruster60e19e02014-06-06 14:50:58 +0200785DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
Kevin Wolf57975222013-07-17 14:41:54 +0200786{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200787 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200788 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200789 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200790 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200791 QemuOpts *legacy_opts;
792 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200793 BlockInterfaceType type;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200794 int cyls, heads, secs, translation;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200795 int max_devs, bus_id, unit_id, index;
Kevin Wolf394c7d42013-09-13 14:09:17 +0200796 const char *devaddr;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100797 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800798 bool read_only = false;
799 bool copy_on_read;
Kevin Wolfbcf83152014-06-06 15:21:48 +0200800 const char *serial;
Max Reitzd095b462013-12-20 19:28:14 +0100801 const char *filename;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200802 Error *local_err = NULL;
Kevin Wolf247147f2014-09-24 16:37:14 +0200803 int i;
Thomas Huthc616f162017-05-12 12:33:49 +0200804 const char *deprecated[] = {
805 "serial", "trans", "secs", "heads", "cyls", "addr"
806 };
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200807
Kevin Wolf57975222013-07-17 14:41:54 +0200808 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200809 static const struct {
810 const char *from;
811 const char *to;
812 } opt_renames[] = {
813 { "iops", "throttling.iops-total" },
814 { "iops_rd", "throttling.iops-read" },
815 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200816
Kevin Wolf247147f2014-09-24 16:37:14 +0200817 { "bps", "throttling.bps-total" },
818 { "bps_rd", "throttling.bps-read" },
819 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200820
Kevin Wolf247147f2014-09-24 16:37:14 +0200821 { "iops_max", "throttling.iops-total-max" },
822 { "iops_rd_max", "throttling.iops-read-max" },
823 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200824
Kevin Wolf247147f2014-09-24 16:37:14 +0200825 { "bps_max", "throttling.bps-total-max" },
826 { "bps_rd_max", "throttling.bps-read-max" },
827 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200828
Kevin Wolf247147f2014-09-24 16:37:14 +0200829 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200830
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200831 { "group", "throttling.group" },
832
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300833 { "readonly", BDRV_OPT_READ_ONLY },
Kevin Wolf247147f2014-09-24 16:37:14 +0200834 };
835
836 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200837 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
838 &local_err);
839 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100840 error_report_err(local_err);
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200841 return NULL;
842 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200843 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200844
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200845 value = qemu_opt_get(all_opts, "cache");
846 if (value) {
847 int flags = 0;
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100848 bool writethrough;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200849
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100850 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200851 error_report("invalid cache option");
852 return NULL;
853 }
854
855 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200856 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
857 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100858 !writethrough, &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200859 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200860 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
861 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100862 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200863 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200864 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
865 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100866 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200867 }
868 qemu_opt_unset(all_opts, "cache");
869 }
870
Kevin Wolff298d072013-09-10 12:01:20 +0200871 /* Get a QDict for processing the options */
872 bs_opts = qdict_new();
873 qemu_opts_to_qdict(all_opts, bs_opts);
874
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800875 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
876 &error_abort);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200877 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100878 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100879 error_report_err(local_err);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200880 goto fail;
881 }
882
Thomas Huthc616f162017-05-12 12:33:49 +0200883 /* Other deprecated options */
884 if (!qtest_enabled()) {
885 for (i = 0; i < ARRAY_SIZE(deprecated); i++) {
886 if (qemu_opt_get(legacy_opts, deprecated[i]) != NULL) {
887 error_report("'%s' is deprecated, please use the corresponding "
888 "option of '-device' instead", deprecated[i]);
889 }
890 }
891 }
892
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200893 /* Media type */
894 value = qemu_opt_get(legacy_opts, "media");
895 if (value) {
896 if (!strcmp(value, "disk")) {
897 media = MEDIA_DISK;
898 } else if (!strcmp(value, "cdrom")) {
899 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800900 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200901 } else {
902 error_report("'%s' invalid media", value);
903 goto fail;
904 }
905 }
906
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200907 /* copy-on-read is disabled with a warning for read-only devices */
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300908 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200909 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
910
911 if (read_only && copy_on_read) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700912 warn_report("disabling copy-on-read on read-only drive");
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200913 copy_on_read = false;
914 }
915
Eric Blake46f5ac22017-04-27 16:58:17 -0500916 qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off");
917 qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off");
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200918
Kevin Wolf593d4642013-08-28 17:24:51 +0200919 /* Controller type */
920 value = qemu_opt_get(legacy_opts, "if");
921 if (value) {
922 for (type = 0;
923 type < IF_COUNT && strcmp(value, if_name[type]);
924 type++) {
925 }
926 if (type == IF_COUNT) {
927 error_report("unsupported bus type '%s'", value);
928 goto fail;
929 }
930 } else {
931 type = block_default_type;
932 }
933
Kevin Wolfb41a7332013-09-09 16:49:49 +0200934 /* Geometry */
935 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
936 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
937 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
938
939 if (cyls || heads || secs) {
940 if (cyls < 1) {
941 error_report("invalid physical cyls number");
942 goto fail;
943 }
944 if (heads < 1) {
945 error_report("invalid physical heads number");
946 goto fail;
947 }
948 if (secs < 1) {
949 error_report("invalid physical secs number");
950 goto fail;
951 }
952 }
953
954 translation = BIOS_ATA_TRANSLATION_AUTO;
955 value = qemu_opt_get(legacy_opts, "trans");
956 if (value != NULL) {
957 if (!cyls) {
958 error_report("'%s' trans must be used with cyls, heads and secs",
959 value);
960 goto fail;
961 }
962 if (!strcmp(value, "none")) {
963 translation = BIOS_ATA_TRANSLATION_NONE;
964 } else if (!strcmp(value, "lba")) {
965 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +0100966 } else if (!strcmp(value, "large")) {
967 translation = BIOS_ATA_TRANSLATION_LARGE;
968 } else if (!strcmp(value, "rechs")) {
969 translation = BIOS_ATA_TRANSLATION_RECHS;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200970 } else if (!strcmp(value, "auto")) {
971 translation = BIOS_ATA_TRANSLATION_AUTO;
972 } else {
973 error_report("'%s' invalid translation type", value);
974 goto fail;
975 }
976 }
977
978 if (media == MEDIA_CDROM) {
979 if (cyls || secs || heads) {
980 error_report("CHS can't be set with media=cdrom");
981 goto fail;
982 }
983 }
984
Kevin Wolf87a899c2013-09-10 15:48:13 +0200985 /* Device address specified by bus/unit or index.
986 * If none was specified, try to find the first free one. */
987 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
988 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
989 index = qemu_opt_get_number(legacy_opts, "index", -1);
990
991 max_devs = if_max_devs[type];
992
993 if (index != -1) {
994 if (bus_id != 0 || unit_id != -1) {
995 error_report("index cannot be used with bus and unit");
996 goto fail;
997 }
998 bus_id = drive_index_to_bus_id(type, index);
999 unit_id = drive_index_to_unit_id(type, index);
1000 }
1001
1002 if (unit_id == -1) {
1003 unit_id = 0;
1004 while (drive_get(type, bus_id, unit_id) != NULL) {
1005 unit_id++;
1006 if (max_devs && unit_id >= max_devs) {
1007 unit_id -= max_devs;
1008 bus_id++;
1009 }
1010 }
1011 }
1012
1013 if (max_devs && unit_id >= max_devs) {
1014 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1015 goto fail;
1016 }
1017
1018 if (drive_get(type, bus_id, unit_id) != NULL) {
1019 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1020 bus_id, unit_id, index);
1021 goto fail;
1022 }
1023
Kevin Wolfbcf83152014-06-06 15:21:48 +02001024 /* Serial number */
1025 serial = qemu_opt_get(legacy_opts, "serial");
1026
Kevin Wolf87a899c2013-09-10 15:48:13 +02001027 /* no id supplied -> create one */
1028 if (qemu_opts_id(all_opts) == NULL) {
1029 char *new_id;
1030 const char *mediastr = "";
1031 if (type == IF_IDE || type == IF_SCSI) {
1032 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1033 }
1034 if (max_devs) {
1035 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1036 mediastr, unit_id);
1037 } else {
1038 new_id = g_strdup_printf("%s%s%i", if_name[type],
1039 mediastr, unit_id);
1040 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001041 qdict_put_str(bs_opts, "id", new_id);
Kevin Wolf87a899c2013-09-10 15:48:13 +02001042 g_free(new_id);
1043 }
1044
Kevin Wolf394c7d42013-09-13 14:09:17 +02001045 /* Add virtio block device */
1046 devaddr = qemu_opt_get(legacy_opts, "addr");
1047 if (devaddr && type != IF_VIRTIO) {
1048 error_report("addr is not supported by this bus type");
1049 goto fail;
1050 }
1051
1052 if (type == IF_VIRTIO) {
1053 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001054 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1055 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001056 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +02001057 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001058 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001059 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001060 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001061 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1062 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001063 if (devaddr) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001064 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001065 }
1066 }
1067
Max Reitzd095b462013-12-20 19:28:14 +01001068 filename = qemu_opt_get(legacy_opts, "file");
1069
Kevin Wolfee13ed12014-02-09 09:52:32 +01001070 /* Check werror/rerror compatibility with if=... */
1071 werror = qemu_opt_get(legacy_opts, "werror");
1072 if (werror != NULL) {
1073 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1074 type != IF_NONE) {
1075 error_report("werror is not supported by this bus type");
1076 goto fail;
1077 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001078 qdict_put_str(bs_opts, "werror", werror);
Kevin Wolfee13ed12014-02-09 09:52:32 +01001079 }
1080
1081 rerror = qemu_opt_get(legacy_opts, "rerror");
1082 if (rerror != NULL) {
1083 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1084 type != IF_NONE) {
1085 error_report("rerror is not supported by this bus type");
1086 goto fail;
1087 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001088 qdict_put_str(bs_opts, "rerror", rerror);
Kevin Wolfee13ed12014-02-09 09:52:32 +01001089 }
1090
Kevin Wolf2d246f02013-09-18 15:14:47 +02001091 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster18e46a02014-10-07 13:59:06 +02001092 blk = blockdev_init(filename, bs_opts, &local_err);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001093 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +02001094 if (!blk) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001095 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001096 error_report_err(local_err);
Kevin Wolfb6810722013-09-20 11:33:11 +02001097 }
Kevin Wolf2d246f02013-09-18 15:14:47 +02001098 goto fail;
Kevin Wolfb6810722013-09-20 11:33:11 +02001099 } else {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001100 assert(!local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001101 }
1102
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001103 /* Create legacy DriveInfo */
1104 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001105 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001106
Kevin Wolfb41a7332013-09-09 16:49:49 +02001107 dinfo->cyls = cyls;
1108 dinfo->heads = heads;
1109 dinfo->secs = secs;
1110 dinfo->trans = translation;
1111
Kevin Wolfee13ed12014-02-09 09:52:32 +01001112 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001113 dinfo->bus = bus_id;
1114 dinfo->unit = unit_id;
Kevin Wolf394c7d42013-09-13 14:09:17 +02001115 dinfo->devaddr = devaddr;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001116 dinfo->serial = g_strdup(serial);
1117
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001118 blk_set_legacy_dinfo(blk, dinfo);
1119
Kevin Wolfe34ef042013-09-19 14:24:10 +02001120 switch(type) {
1121 case IF_IDE:
1122 case IF_SCSI:
1123 case IF_XEN:
1124 case IF_NONE:
1125 dinfo->media_cd = media == MEDIA_CDROM;
1126 break;
1127 default:
1128 break;
1129 }
1130
Kevin Wolf2d246f02013-09-18 15:14:47 +02001131fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001132 qemu_opts_del(legacy_opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001133 qobject_unref(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001134 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001135}
1136
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02001137static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1138{
1139 BlockDriverState *bs;
1140
1141 bs = bdrv_lookup_bs(name, name, errp);
1142 if (bs == NULL) {
1143 return NULL;
1144 }
1145
1146 if (!bdrv_is_root_node(bs)) {
1147 error_setg(errp, "Need a root block node");
1148 return NULL;
1149 }
1150
1151 if (!bdrv_is_inserted(bs)) {
1152 error_setg(errp, "Device has no medium");
1153 return NULL;
1154 }
1155
1156 return bs;
1157}
1158
Kevin Wolfb33945c2016-09-20 13:38:43 +02001159static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id,
1160 Error **errp)
1161{
1162 BlockBackend *blk;
1163
1164 if (!blk_name == !qdev_id) {
1165 error_setg(errp, "Need exactly one of 'device' and 'id'");
1166 return NULL;
1167 }
1168
1169 if (qdev_id) {
1170 blk = blk_by_qdev_id(qdev_id, errp);
1171 } else {
1172 blk = blk_by_name(blk_name);
1173 if (blk == NULL) {
1174 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1175 "Device '%s' not found", blk_name);
1176 }
1177 }
1178
1179 return blk;
1180}
1181
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001182void hmp_commit(Monitor *mon, const QDict *qdict)
Markus Armbruster666daa62010-06-02 18:48:27 +02001183{
Markus Armbruster666daa62010-06-02 18:48:27 +02001184 const char *device = qdict_get_str(qdict, "device");
Fam Zhenga0e85442015-03-02 19:36:48 +08001185 BlockBackend *blk;
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001186 int ret;
Markus Armbruster666daa62010-06-02 18:48:27 +02001187
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001188 if (!strcmp(device, "all")) {
Max Reitzda31d592016-03-16 19:54:32 +01001189 ret = blk_commit_all();
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001190 } else {
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001191 BlockDriverState *bs;
1192 AioContext *aio_context;
1193
Fam Zhenga0e85442015-03-02 19:36:48 +08001194 blk = blk_by_name(device);
1195 if (!blk) {
Jeff Cody58513bd2013-01-18 12:45:35 -05001196 monitor_printf(mon, "Device '%s' not found\n", device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +02001197 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001198 }
Max Reitz5433c242015-10-19 17:53:29 +02001199 if (!blk_is_available(blk)) {
1200 monitor_printf(mon, "Device '%s' has no medium\n", device);
1201 return;
1202 }
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001203
1204 bs = blk_bs(blk);
1205 aio_context = bdrv_get_aio_context(bs);
1206 aio_context_acquire(aio_context);
1207
1208 ret = bdrv_commit(bs);
1209
1210 aio_context_release(aio_context);
Jeff Cody58513bd2013-01-18 12:45:35 -05001211 }
1212 if (ret < 0) {
1213 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1214 strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +02001215 }
1216}
1217
Eric Blake10f75902016-03-03 09:16:50 -07001218static void blockdev_do_action(TransactionAction *action, Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001219{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001220 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001221
Eric Blake10f75902016-03-03 09:16:50 -07001222 list.value = action;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001223 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001224 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001225}
1226
Benoît Canet0901f672014-01-23 21:31:38 +01001227void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1228 bool has_node_name, const char *node_name,
1229 const char *snapshot_file,
1230 bool has_snapshot_node_name,
1231 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001232 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001233 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001234{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001235 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001236 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001237 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001238 .has_node_name = has_node_name,
1239 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001240 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001241 .has_snapshot_node_name = has_snapshot_node_name,
1242 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001243 .has_format = has_format,
1244 .format = (char *) format,
1245 .has_mode = has_mode,
1246 .mode = mode,
1247 };
Eric Blake10f75902016-03-03 09:16:50 -07001248 TransactionAction action = {
1249 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001250 .u.blockdev_snapshot_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001251 };
1252 blockdev_do_action(&action, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001253}
1254
Alberto Garcia43de7e22015-10-26 14:27:16 +02001255void qmp_blockdev_snapshot(const char *node, const char *overlay,
1256 Error **errp)
1257{
1258 BlockdevSnapshot snapshot_data = {
1259 .node = (char *) node,
1260 .overlay = (char *) overlay
1261 };
Eric Blake10f75902016-03-03 09:16:50 -07001262 TransactionAction action = {
1263 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
Eric Blake32bafa82016-03-17 16:48:37 -06001264 .u.blockdev_snapshot.data = &snapshot_data,
Eric Blake10f75902016-03-03 09:16:50 -07001265 };
1266 blockdev_do_action(&action, errp);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001267}
1268
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001269void qmp_blockdev_snapshot_internal_sync(const char *device,
1270 const char *name,
1271 Error **errp)
1272{
1273 BlockdevSnapshotInternal snapshot = {
1274 .device = (char *) device,
1275 .name = (char *) name
1276 };
Eric Blake10f75902016-03-03 09:16:50 -07001277 TransactionAction action = {
1278 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001279 .u.blockdev_snapshot_internal_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001280 };
1281 blockdev_do_action(&action, errp);
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001282}
1283
Wenchao Xia44e3e052013-09-11 14:04:36 +08001284SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1285 bool has_id,
1286 const char *id,
1287 bool has_name,
1288 const char *name,
1289 Error **errp)
1290{
Fam Zhenga0e85442015-03-02 19:36:48 +08001291 BlockDriverState *bs;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001292 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001293 QEMUSnapshotInfo sn;
1294 Error *local_err = NULL;
1295 SnapshotInfo *info = NULL;
1296 int ret;
1297
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001298 bs = qmp_get_root_bs(device, errp);
1299 if (!bs) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001300 return NULL;
1301 }
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001302 aio_context = bdrv_get_aio_context(bs);
Max Reitz5433c242015-10-19 17:53:29 +02001303 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001304
1305 if (!has_id) {
1306 id = NULL;
1307 }
1308
1309 if (!has_name) {
1310 name = NULL;
1311 }
1312
1313 if (!id && !name) {
1314 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001315 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001316 }
1317
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001318 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1319 goto out_aio_context;
1320 }
1321
Wenchao Xia44e3e052013-09-11 14:04:36 +08001322 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001323 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001324 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001325 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001326 }
1327 if (!ret) {
1328 error_setg(errp,
1329 "Snapshot with id '%s' and name '%s' does not exist on "
1330 "device '%s'",
1331 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001332 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001333 }
1334
1335 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001336 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001337 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001338 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001339 }
1340
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001341 aio_context_release(aio_context);
1342
Markus Armbruster5839e532014-08-19 10:31:08 +02001343 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001344 info->id = g_strdup(sn.id_str);
1345 info->name = g_strdup(sn.name);
1346 info->date_nsec = sn.date_nsec;
1347 info->date_sec = sn.date_sec;
1348 info->vm_state_size = sn.vm_state_size;
1349 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1350 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1351
1352 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001353
1354out_aio_context:
1355 aio_context_release(aio_context);
1356 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001357}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001358
John Snow341ebc22015-04-17 19:49:52 -04001359/**
1360 * block_dirty_bitmap_lookup:
1361 * Return a dirty bitmap (if present), after validating
1362 * the node reference and bitmap names.
1363 *
1364 * @node: The name of the BDS node to search for bitmaps
1365 * @name: The name of the bitmap to search for
1366 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1367 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1368 * @errp: Output pointer for error information. Can be NULL.
1369 *
1370 * @return: A bitmap object on success, or NULL on failure.
1371 */
1372static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1373 const char *name,
1374 BlockDriverState **pbs,
John Snow341ebc22015-04-17 19:49:52 -04001375 Error **errp)
1376{
1377 BlockDriverState *bs;
1378 BdrvDirtyBitmap *bitmap;
John Snow341ebc22015-04-17 19:49:52 -04001379
1380 if (!node) {
1381 error_setg(errp, "Node cannot be NULL");
1382 return NULL;
1383 }
1384 if (!name) {
1385 error_setg(errp, "Bitmap name cannot be NULL");
1386 return NULL;
1387 }
1388 bs = bdrv_lookup_bs(node, node, NULL);
1389 if (!bs) {
1390 error_setg(errp, "Node '%s' not found", node);
1391 return NULL;
1392 }
1393
John Snow341ebc22015-04-17 19:49:52 -04001394 bitmap = bdrv_find_dirty_bitmap(bs, name);
1395 if (!bitmap) {
1396 error_setg(errp, "Dirty bitmap '%s' not found", name);
Paolo Bonzini21198822017-06-05 14:39:03 +02001397 return NULL;
John Snow341ebc22015-04-17 19:49:52 -04001398 }
1399
1400 if (pbs) {
1401 *pbs = bs;
1402 }
John Snow341ebc22015-04-17 19:49:52 -04001403
1404 return bitmap;
John Snow341ebc22015-04-17 19:49:52 -04001405}
1406
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001407/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001408
John Snow50f43f02015-11-05 18:13:09 -05001409typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001410
John Snow50f43f02015-11-05 18:13:09 -05001411/**
1412 * BlkActionOps:
1413 * Table of operations that define an Action.
1414 *
1415 * @instance_size: Size of state struct, in bytes.
1416 * @prepare: Prepare the work, must NOT be NULL.
1417 * @commit: Commit the changes, can be NULL.
1418 * @abort: Abort the changes on fail, can be NULL.
1419 * @clean: Clean up resources after all transaction actions have called
1420 * commit() or abort(). Can be NULL.
1421 *
1422 * Only prepare() may fail. In a single transaction, only one of commit() or
1423 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001424 */
John Snow50f43f02015-11-05 18:13:09 -05001425typedef struct BlkActionOps {
1426 size_t instance_size;
1427 void (*prepare)(BlkActionState *common, Error **errp);
1428 void (*commit)(BlkActionState *common);
1429 void (*abort)(BlkActionState *common);
1430 void (*clean)(BlkActionState *common);
1431} BlkActionOps;
1432
1433/**
1434 * BlkActionState:
1435 * Describes one Action's state within a Transaction.
1436 *
1437 * @action: QAPI-defined enum identifying which Action to perform.
1438 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001439 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001440 * @entry: List membership for all Actions in this Transaction.
1441 *
1442 * This structure must be arranged as first member in a subclassed type,
1443 * assuming that the compiler will also arrange it to the same offsets as the
1444 * base class.
1445 */
1446struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001447 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001448 const BlkActionOps *ops;
Kevin Wolf62c9e412018-04-19 16:09:52 +02001449 JobTxn *block_job_txn;
John Snow94d16a62015-11-05 18:13:18 -05001450 TransactionProperties *txn_props;
John Snow50f43f02015-11-05 18:13:09 -05001451 QSIMPLEQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001452};
1453
Wenchao Xiabbe86012013-09-11 14:04:34 +08001454/* internal snapshot private data */
1455typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001456 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001457 BlockDriverState *bs;
1458 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001459 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001460} InternalSnapshotState;
1461
John Snow94d16a62015-11-05 18:13:18 -05001462
1463static int action_check_completion_mode(BlkActionState *s, Error **errp)
1464{
1465 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1466 error_setg(errp,
1467 "Action '%s' does not support Transaction property "
1468 "completion-mode = %s",
Markus Armbruster977c7362017-08-24 10:46:08 +02001469 TransactionActionKind_str(s->action->type),
1470 ActionCompletionMode_str(s->txn_props->completion_mode));
John Snow94d16a62015-11-05 18:13:18 -05001471 return -1;
1472 }
1473 return 0;
1474}
1475
John Snow50f43f02015-11-05 18:13:09 -05001476static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001477 Error **errp)
1478{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001479 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001480 const char *device;
1481 const char *name;
1482 BlockDriverState *bs;
1483 QEMUSnapshotInfo old_sn, *sn;
1484 bool ret;
1485 qemu_timeval tv;
1486 BlockdevSnapshotInternal *internal;
1487 InternalSnapshotState *state;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001488 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001489 int ret1;
1490
Eric Blake6a8f9662015-10-26 16:34:54 -06001491 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001492 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001493 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001494 state = DO_UPCAST(InternalSnapshotState, common, common);
1495
1496 /* 1. parse input */
1497 device = internal->device;
1498 name = internal->name;
1499
1500 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001501 if (action_check_completion_mode(common, errp) < 0) {
1502 return;
1503 }
1504
Kevin Wolf75dfd402016-06-23 14:20:24 +02001505 bs = qmp_get_root_bs(device, errp);
1506 if (!bs) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001507 return;
1508 }
1509
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001510 aio_context = bdrv_get_aio_context(bs);
1511 aio_context_acquire(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001512
Fam Zheng507306c2015-10-23 11:08:13 +08001513 state->bs = bs;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001514
1515 /* Paired with .clean() */
Fam Zheng507306c2015-10-23 11:08:13 +08001516 bdrv_drained_begin(bs);
1517
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001518 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001519 goto out;
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001520 }
1521
Wenchao Xiabbe86012013-09-11 14:04:34 +08001522 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001523 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001524 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001525 }
1526
1527 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001528 error_setg(errp, "Block format '%s' used by device '%s' "
1529 "does not support internal snapshots",
1530 bs->drv->format_name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001531 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001532 }
1533
1534 if (!strlen(name)) {
1535 error_setg(errp, "Name is empty");
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001536 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001537 }
1538
1539 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001540 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1541 &local_err);
1542 if (local_err) {
1543 error_propagate(errp, local_err);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001544 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001545 } else if (ret) {
1546 error_setg(errp,
1547 "Snapshot with name '%s' already exists on device '%s'",
1548 name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001549 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001550 }
1551
1552 /* 3. take the snapshot */
1553 sn = &state->sn;
1554 pstrcpy(sn->name, sizeof(sn->name), name);
1555 qemu_gettimeofday(&tv);
1556 sn->date_sec = tv.tv_sec;
1557 sn->date_nsec = tv.tv_usec * 1000;
1558 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1559
1560 ret1 = bdrv_snapshot_create(bs, sn);
1561 if (ret1 < 0) {
1562 error_setg_errno(errp, -ret1,
1563 "Failed to create snapshot '%s' on device '%s'",
1564 name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001565 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001566 }
1567
1568 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001569 state->created = true;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001570
1571out:
1572 aio_context_release(aio_context);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001573}
1574
John Snow50f43f02015-11-05 18:13:09 -05001575static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001576{
1577 InternalSnapshotState *state =
1578 DO_UPCAST(InternalSnapshotState, common, common);
1579 BlockDriverState *bs = state->bs;
1580 QEMUSnapshotInfo *sn = &state->sn;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001581 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001582 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
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001588 aio_context = bdrv_get_aio_context(state->bs);
1589 aio_context_acquire(aio_context);
1590
Wenchao Xiabbe86012013-09-11 14:04:34 +08001591 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001592 error_reportf_err(local_error,
1593 "Failed to delete snapshot with id '%s' and "
1594 "name '%s' on device '%s' in abort: ",
1595 sn->id_str, sn->name,
1596 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001597 }
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001598
1599 aio_context_release(aio_context);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001600}
1601
John Snow50f43f02015-11-05 18:13:09 -05001602static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001603{
1604 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1605 common, common);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001606 AioContext *aio_context;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001607
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001608 if (!state->bs) {
1609 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001610 }
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001611
1612 aio_context = bdrv_get_aio_context(state->bs);
1613 aio_context_acquire(aio_context);
1614
1615 bdrv_drained_end(state->bs);
1616
1617 aio_context_release(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001618}
1619
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001620/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001621typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001622 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001623 BlockDriverState *old_bs;
1624 BlockDriverState *new_bs;
Kevin Wolf067acf22017-03-02 15:26:18 +01001625 bool overlay_appended;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001626} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001627
John Snow50f43f02015-11-05 18:13:09 -05001628static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001629 Error **errp)
1630{
Max Reitz5b363932016-05-17 16:41:31 +02001631 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001632 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001633 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001634 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001635 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001636 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001637 /* Reference to the new image (for 'blockdev-snapshot') */
1638 const char *snapshot_ref;
1639 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001640 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001641 ExternalSnapshotState *state =
1642 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001643 TransactionAction *action = common->action;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001644 AioContext *aio_context;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001645
Alberto Garcia43de7e22015-10-26 14:27:16 +02001646 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1647 * purpose but a different set of parameters */
1648 switch (action->type) {
1649 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1650 {
Eric Blake32bafa82016-03-17 16:48:37 -06001651 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001652 device = s->node;
1653 node_name = s->node;
1654 new_image_file = NULL;
1655 snapshot_ref = s->overlay;
1656 }
1657 break;
1658 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1659 {
Eric Blake32bafa82016-03-17 16:48:37 -06001660 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001661 device = s->has_device ? s->device : NULL;
1662 node_name = s->has_node_name ? s->node_name : NULL;
1663 new_image_file = s->snapshot_file;
1664 snapshot_ref = NULL;
1665 }
1666 break;
1667 default:
1668 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001669 }
1670
1671 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001672 if (action_check_completion_mode(common, errp) < 0) {
1673 return;
1674 }
1675
Alberto Garcia43de7e22015-10-26 14:27:16 +02001676 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1677 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001678 return;
1679 }
1680
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001681 aio_context = bdrv_get_aio_context(state->old_bs);
1682 aio_context_acquire(aio_context);
1683
1684 /* Paired with .clean() */
Fam Zhengda763e82015-10-23 11:08:10 +08001685 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001686
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001687 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001688 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001689 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001690 }
1691
Fam Zheng3718d8a2014-05-23 21:29:43 +08001692 if (bdrv_op_is_blocked(state->old_bs,
1693 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001694 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001695 }
1696
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001697 if (!bdrv_is_read_only(state->old_bs)) {
1698 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001699 error_setg(errp, QERR_IO_ERROR);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001700 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001701 }
1702 }
1703
Benoît Canet212a5a82014-01-23 21:31:36 +01001704 if (!bdrv_is_first_non_filter(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001705 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001706 goto out;
Benoît Canetf6186f42013-10-02 14:33:48 +02001707 }
1708
Alberto Garcia43de7e22015-10-26 14:27:16 +02001709 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001710 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001711 const char *format = s->has_format ? s->format : "qcow2";
1712 enum NewImageMode mode;
1713 const char *snapshot_node_name =
1714 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001715
Alberto Garcia43de7e22015-10-26 14:27:16 +02001716 if (node_name && !snapshot_node_name) {
1717 error_setg(errp, "New snapshot node name missing");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001718 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001719 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001720
1721 if (snapshot_node_name &&
1722 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1723 error_setg(errp, "New snapshot node name already in use");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001724 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001725 }
1726
1727 flags = state->old_bs->open_flags;
John Snow2a32c6e2017-07-17 20:34:21 -04001728 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ);
1729 flags |= BDRV_O_NO_BACKING;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001730
1731 /* create new image w/backing file */
1732 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1733 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001734 int64_t size = bdrv_getlength(state->old_bs);
1735 if (size < 0) {
1736 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001737 goto out;
Kevin Wolff86b8b52016-03-02 12:16:44 +01001738 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001739 bdrv_img_create(new_image_file, format,
1740 state->old_bs->filename,
1741 state->old_bs->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08001742 NULL, size, flags, false, &local_err);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001743 if (local_err) {
1744 error_propagate(errp, local_err);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001745 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001746 }
1747 }
1748
1749 options = qdict_new();
1750 if (s->has_snapshot_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05001751 qdict_put_str(options, "node-name", snapshot_node_name);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001752 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001753 qdict_put_str(options, "driver", format);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001754 }
1755
Max Reitz5b363932016-05-17 16:41:31 +02001756 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1757 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001758 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001759 if (!state->new_bs) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001760 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001761 }
1762
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001763 if (bdrv_has_blk(state->new_bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02001764 error_setg(errp, "The snapshot is already in use");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001765 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001766 }
1767
1768 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1769 errp)) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001770 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001771 }
1772
1773 if (state->new_bs->backing != NULL) {
1774 error_setg(errp, "The snapshot already has a backing image");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001775 goto out;
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001776 }
1777
1778 if (!state->new_bs->drv->supports_backing) {
1779 error_setg(errp, "The snapshot does not support backing images");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001780 goto out;
Kevin Wolfb2c28322017-02-20 12:46:42 +01001781 }
1782
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001783 bdrv_set_aio_context(state->new_bs, aio_context);
Fam Zhengc26a5ab2017-04-07 14:54:09 +08001784
Kevin Wolfb2c28322017-02-20 12:46:42 +01001785 /* This removes our old bs and adds the new bs. This is an operation that
1786 * can fail, so we need to do it in .prepare; undoing it for abort is
1787 * always possible. */
1788 bdrv_ref(state->new_bs);
1789 bdrv_append(state->new_bs, state->old_bs, &local_err);
1790 if (local_err) {
1791 error_propagate(errp, local_err);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001792 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001793 }
Kevin Wolf067acf22017-03-02 15:26:18 +01001794 state->overlay_appended = true;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001795
1796out:
1797 aio_context_release(aio_context);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001798}
1799
John Snow50f43f02015-11-05 18:13:09 -05001800static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001801{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001802 ExternalSnapshotState *state =
1803 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001804 AioContext *aio_context;
1805
1806 aio_context = bdrv_get_aio_context(state->old_bs);
1807 aio_context_acquire(aio_context);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001808
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001809 /* We don't need (or want) to use the transactional
1810 * bdrv_reopen_multiple() across all the entries at once, because we
1811 * don't want to abort all of them if one of them fails the reopen */
Paolo Bonzinid3faa132017-06-05 14:38:50 +02001812 if (!atomic_read(&state->old_bs->copy_on_read)) {
Kevin Wolf4c844982016-02-29 13:12:26 +01001813 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1814 NULL);
1815 }
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001816
1817 aio_context_release(aio_context);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001818}
1819
John Snow50f43f02015-11-05 18:13:09 -05001820static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001821{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001822 ExternalSnapshotState *state =
1823 DO_UPCAST(ExternalSnapshotState, common, common);
1824 if (state->new_bs) {
Kevin Wolf067acf22017-03-02 15:26:18 +01001825 if (state->overlay_appended) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001826 AioContext *aio_context;
1827
1828 aio_context = bdrv_get_aio_context(state->old_bs);
1829 aio_context_acquire(aio_context);
1830
Jeff Cody719fc282017-06-07 09:55:22 -04001831 bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
1832 close state->old_bs; we need it */
1833 bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
Kevin Wolf5fe31c22017-03-06 16:20:51 +01001834 bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
Jeff Cody719fc282017-06-07 09:55:22 -04001835 bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001836
1837 aio_context_release(aio_context);
Kevin Wolfb2c28322017-02-20 12:46:42 +01001838 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001839 }
Fam Zhengda763e82015-10-23 11:08:10 +08001840}
1841
John Snow50f43f02015-11-05 18:13:09 -05001842static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001843{
1844 ExternalSnapshotState *state =
1845 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001846 AioContext *aio_context;
1847
1848 if (!state->old_bs) {
1849 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001850 }
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001851
1852 aio_context = bdrv_get_aio_context(state->old_bs);
1853 aio_context_acquire(aio_context);
1854
1855 bdrv_drained_end(state->old_bs);
1856 bdrv_unref(state->new_bs);
1857
1858 aio_context_release(aio_context);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001859}
1860
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001861typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001862 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001863 BlockDriverState *bs;
1864 BlockJob *job;
1865} DriveBackupState;
1866
Kevin Wolf62c9e412018-04-19 16:09:52 +02001867static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn,
Pavel Butsykin81206a82016-07-22 11:17:50 +03001868 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001869
John Snow50f43f02015-11-05 18:13:09 -05001870static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001871{
1872 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001873 BlockDriverState *bs;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001874 DriveBackup *backup;
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001875 AioContext *aio_context;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001876 Error *local_err = NULL;
1877
Eric Blake6a8f9662015-10-26 16:34:54 -06001878 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001879 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001880
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001881 bs = qmp_get_root_bs(backup->device, errp);
1882 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001883 return;
1884 }
1885
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001886 aio_context = bdrv_get_aio_context(bs);
1887 aio_context_acquire(aio_context);
1888
1889 /* Paired with .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001890 bdrv_drained_begin(bs);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001891
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001892 state->bs = bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001893
John Snow111049a2016-11-08 01:50:38 -05001894 state->job = do_drive_backup(backup, common->block_job_txn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001895 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001896 error_propagate(errp, local_err);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001897 goto out;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001898 }
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001899
1900out:
1901 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05001902}
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001903
John Snow111049a2016-11-08 01:50:38 -05001904static void drive_backup_commit(BlkActionState *common)
1905{
1906 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001907 AioContext *aio_context;
1908
1909 aio_context = bdrv_get_aio_context(state->bs);
1910 aio_context_acquire(aio_context);
1911
John Snow111049a2016-11-08 01:50:38 -05001912 assert(state->job);
Kevin Wolfda01ff72018-04-13 17:31:02 +02001913 job_start(&state->job->job);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001914
1915 aio_context_release(aio_context);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001916}
1917
John Snow50f43f02015-11-05 18:13:09 -05001918static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001919{
1920 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001921
John Snow111049a2016-11-08 01:50:38 -05001922 if (state->job) {
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001923 AioContext *aio_context;
1924
1925 aio_context = bdrv_get_aio_context(state->bs);
1926 aio_context_acquire(aio_context);
1927
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001928 job_cancel_sync(&state->job->job);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001929
1930 aio_context_release(aio_context);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001931 }
1932}
1933
John Snow50f43f02015-11-05 18:13:09 -05001934static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001935{
1936 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001937 AioContext *aio_context;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001938
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001939 if (!state->bs) {
1940 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001941 }
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001942
1943 aio_context = bdrv_get_aio_context(state->bs);
1944 aio_context_acquire(aio_context);
1945
1946 bdrv_drained_end(state->bs);
1947
1948 aio_context_release(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001949}
1950
Fam Zhengbd8baec2014-12-18 18:37:06 +08001951typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001952 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001953 BlockDriverState *bs;
1954 BlockJob *job;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001955} BlockdevBackupState;
1956
Kevin Wolf62c9e412018-04-19 16:09:52 +02001957static BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn,
John Snow111049a2016-11-08 01:50:38 -05001958 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001959
John Snow50f43f02015-11-05 18:13:09 -05001960static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001961{
1962 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1963 BlockdevBackup *backup;
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001964 BlockDriverState *bs, *target;
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001965 AioContext *aio_context;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001966 Error *local_err = NULL;
1967
Eric Blake6a8f9662015-10-26 16:34:54 -06001968 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001969 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001970
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001971 bs = qmp_get_root_bs(backup->device, errp);
1972 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001973 return;
1974 }
1975
Kevin Wolf39d990a2016-08-02 19:22:04 +02001976 target = bdrv_lookup_bs(backup->target, backup->target, errp);
Max Reitz5433c242015-10-19 17:53:29 +02001977 if (!target) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001978 return;
1979 }
1980
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001981 aio_context = bdrv_get_aio_context(bs);
1982 if (aio_context != bdrv_get_aio_context(target)) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001983 error_setg(errp, "Backup between two IO threads is not implemented");
1984 return;
1985 }
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001986 aio_context_acquire(aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001987 state->bs = bs;
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001988
1989 /* Paired with .clean() */
Fam Zhengff52bf32015-10-23 11:08:12 +08001990 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001991
John Snow111049a2016-11-08 01:50:38 -05001992 state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001993 if (local_err) {
1994 error_propagate(errp, local_err);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001995 goto out;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001996 }
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001997
1998out:
1999 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05002000}
Fam Zhengbd8baec2014-12-18 18:37:06 +08002001
John Snow111049a2016-11-08 01:50:38 -05002002static void blockdev_backup_commit(BlkActionState *common)
2003{
2004 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002005 AioContext *aio_context;
2006
2007 aio_context = bdrv_get_aio_context(state->bs);
2008 aio_context_acquire(aio_context);
2009
John Snow111049a2016-11-08 01:50:38 -05002010 assert(state->job);
Kevin Wolfda01ff72018-04-13 17:31:02 +02002011 job_start(&state->job->job);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002012
2013 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002014}
2015
John Snow50f43f02015-11-05 18:13:09 -05002016static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08002017{
2018 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002019
John Snow111049a2016-11-08 01:50:38 -05002020 if (state->job) {
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002021 AioContext *aio_context;
2022
2023 aio_context = bdrv_get_aio_context(state->bs);
2024 aio_context_acquire(aio_context);
2025
Kevin Wolf3d70ff52018-04-24 16:13:52 +02002026 job_cancel_sync(&state->job->job);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002027
2028 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002029 }
2030}
2031
John Snow50f43f02015-11-05 18:13:09 -05002032static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08002033{
2034 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002035 AioContext *aio_context;
Fam Zhengbd8baec2014-12-18 18:37:06 +08002036
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002037 if (!state->bs) {
2038 return;
Fam Zhengbd8baec2014-12-18 18:37:06 +08002039 }
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002040
2041 aio_context = bdrv_get_aio_context(state->bs);
2042 aio_context_acquire(aio_context);
2043
2044 bdrv_drained_end(state->bs);
2045
2046 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002047}
2048
Fam Zhengdf9a6812015-11-09 18:16:54 +08002049typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05002050 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002051 BdrvDirtyBitmap *bitmap;
2052 BlockDriverState *bs;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002053 HBitmap *backup;
2054 bool prepared;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002055 bool was_enabled;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002056} BlockDirtyBitmapState;
2057
John Snow50f43f02015-11-05 18:13:09 -05002058static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002059 Error **errp)
2060{
2061 Error *local_err = NULL;
2062 BlockDirtyBitmapAdd *action;
2063 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2064 common, common);
2065
John Snow94d16a62015-11-05 18:13:18 -05002066 if (action_check_completion_mode(common, errp) < 0) {
2067 return;
2068 }
2069
Eric Blake32bafa82016-03-17 16:48:37 -06002070 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002071 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2072 qmp_block_dirty_bitmap_add(action->node, action->name,
2073 action->has_granularity, action->granularity,
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002074 action->has_persistent, action->persistent,
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03002075 action->has_autoload, action->autoload,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002076 &local_err);
2077
2078 if (!local_err) {
2079 state->prepared = true;
2080 } else {
2081 error_propagate(errp, local_err);
2082 }
2083}
2084
John Snow50f43f02015-11-05 18:13:09 -05002085static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002086{
2087 BlockDirtyBitmapAdd *action;
2088 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2089 common, common);
2090
Eric Blake32bafa82016-03-17 16:48:37 -06002091 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002092 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2093 * then the node reference and bitmap name must have been valid.
2094 */
2095 if (state->prepared) {
2096 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2097 }
2098}
2099
John Snow50f43f02015-11-05 18:13:09 -05002100static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002101 Error **errp)
2102{
2103 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2104 common, common);
2105 BlockDirtyBitmap *action;
2106
John Snow94d16a62015-11-05 18:13:18 -05002107 if (action_check_completion_mode(common, errp) < 0) {
2108 return;
2109 }
2110
Eric Blake32bafa82016-03-17 16:48:37 -06002111 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002112 state->bitmap = block_dirty_bitmap_lookup(action->node,
2113 action->name,
2114 &state->bs,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002115 errp);
2116 if (!state->bitmap) {
2117 return;
2118 }
2119
2120 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2121 error_setg(errp, "Cannot modify a frozen bitmap");
2122 return;
Vladimir Sementsov-Ogievskiy4f43e952018-03-13 15:34:00 -04002123 } else if (bdrv_dirty_bitmap_qmp_locked(state->bitmap)) {
2124 error_setg(errp, "Cannot modify a locked bitmap");
2125 return;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002126 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2127 error_setg(errp, "Cannot clear a disabled bitmap");
2128 return;
Vladimir Sementsov-Ogievskiyd6883bc2017-06-28 15:05:10 +03002129 } else if (bdrv_dirty_bitmap_readonly(state->bitmap)) {
2130 error_setg(errp, "Cannot clear a readonly bitmap");
2131 return;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002132 }
2133
2134 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
Fam Zhengdf9a6812015-11-09 18:16:54 +08002135}
2136
John Snow50f43f02015-11-05 18:13:09 -05002137static void block_dirty_bitmap_clear_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002138{
2139 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2140 common, common);
2141
John Snow184dd9c2017-03-15 17:28:11 -04002142 if (state->backup) {
2143 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2144 }
Fam Zhengdf9a6812015-11-09 18:16:54 +08002145}
2146
John Snow50f43f02015-11-05 18:13:09 -05002147static void block_dirty_bitmap_clear_commit(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002148{
2149 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2150 common, common);
2151
2152 hbitmap_free(state->backup);
2153}
2154
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002155static void block_dirty_bitmap_enable_prepare(BlkActionState *common,
2156 Error **errp)
2157{
2158 BlockDirtyBitmap *action;
2159 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2160 common, common);
2161
2162 if (action_check_completion_mode(common, errp) < 0) {
2163 return;
2164 }
2165
2166 action = common->action->u.x_block_dirty_bitmap_enable.data;
2167 state->bitmap = block_dirty_bitmap_lookup(action->node,
2168 action->name,
2169 NULL,
2170 errp);
2171 if (!state->bitmap) {
2172 return;
2173 }
2174
2175 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2176 bdrv_enable_dirty_bitmap(state->bitmap);
2177}
2178
2179static void block_dirty_bitmap_enable_abort(BlkActionState *common)
2180{
2181 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2182 common, common);
2183
2184 if (!state->was_enabled) {
2185 bdrv_disable_dirty_bitmap(state->bitmap);
2186 }
2187}
2188
2189static void block_dirty_bitmap_disable_prepare(BlkActionState *common,
2190 Error **errp)
2191{
2192 BlockDirtyBitmap *action;
2193 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2194 common, common);
2195
2196 if (action_check_completion_mode(common, errp) < 0) {
2197 return;
2198 }
2199
2200 action = common->action->u.x_block_dirty_bitmap_disable.data;
2201 state->bitmap = block_dirty_bitmap_lookup(action->node,
2202 action->name,
2203 NULL,
2204 errp);
2205 if (!state->bitmap) {
2206 return;
2207 }
2208
2209 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2210 bdrv_disable_dirty_bitmap(state->bitmap);
2211}
2212
2213static void block_dirty_bitmap_disable_abort(BlkActionState *common)
2214{
2215 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2216 common, common);
2217
2218 if (state->was_enabled) {
2219 bdrv_enable_dirty_bitmap(state->bitmap);
2220 }
2221}
2222
John Snow50f43f02015-11-05 18:13:09 -05002223static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002224{
2225 error_setg(errp, "Transaction aborted using Abort action");
2226}
2227
John Snow50f43f02015-11-05 18:13:09 -05002228static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002229{
Stefan Weildfc6f862013-07-25 18:21:28 +02002230 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002231}
2232
John Snow50f43f02015-11-05 18:13:09 -05002233static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002234 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2235 .instance_size = sizeof(ExternalSnapshotState),
2236 .prepare = external_snapshot_prepare,
2237 .commit = external_snapshot_commit,
2238 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002239 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002240 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002241 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002242 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002243 .prepare = external_snapshot_prepare,
2244 .commit = external_snapshot_commit,
2245 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002246 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002247 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002248 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2249 .instance_size = sizeof(DriveBackupState),
2250 .prepare = drive_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002251 .commit = drive_backup_commit,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002252 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002253 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002254 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002255 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2256 .instance_size = sizeof(BlockdevBackupState),
2257 .prepare = blockdev_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002258 .commit = blockdev_backup_commit,
Fam Zhengbd8baec2014-12-18 18:37:06 +08002259 .abort = blockdev_backup_abort,
2260 .clean = blockdev_backup_clean,
2261 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002262 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002263 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002264 .prepare = abort_prepare,
2265 .commit = abort_commit,
2266 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002267 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2268 .instance_size = sizeof(InternalSnapshotState),
2269 .prepare = internal_snapshot_prepare,
2270 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002271 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002272 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002273 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2274 .instance_size = sizeof(BlockDirtyBitmapState),
2275 .prepare = block_dirty_bitmap_add_prepare,
2276 .abort = block_dirty_bitmap_add_abort,
2277 },
2278 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2279 .instance_size = sizeof(BlockDirtyBitmapState),
2280 .prepare = block_dirty_bitmap_clear_prepare,
2281 .commit = block_dirty_bitmap_clear_commit,
2282 .abort = block_dirty_bitmap_clear_abort,
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002283 },
2284 [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_ENABLE] = {
2285 .instance_size = sizeof(BlockDirtyBitmapState),
2286 .prepare = block_dirty_bitmap_enable_prepare,
2287 .abort = block_dirty_bitmap_enable_abort,
2288 },
2289 [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_DISABLE] = {
2290 .instance_size = sizeof(BlockDirtyBitmapState),
2291 .prepare = block_dirty_bitmap_disable_prepare,
2292 .abort = block_dirty_bitmap_disable_abort,
2293 }
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002294};
2295
John Snow94d16a62015-11-05 18:13:18 -05002296/**
2297 * Allocate a TransactionProperties structure if necessary, and fill
2298 * that structure with desired defaults if they are unset.
2299 */
2300static TransactionProperties *get_transaction_properties(
2301 TransactionProperties *props)
2302{
2303 if (!props) {
2304 props = g_new0(TransactionProperties, 1);
2305 }
2306
2307 if (!props->has_completion_mode) {
2308 props->has_completion_mode = true;
2309 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2310 }
2311
2312 return props;
2313}
2314
Jeff Cody8802d1f2012-02-28 15:54:06 -05002315/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002316 * 'Atomic' group operations. The operations are performed as a set, and if
2317 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002318 */
John Snow94d16a62015-11-05 18:13:18 -05002319void qmp_transaction(TransactionActionList *dev_list,
2320 bool has_props,
2321 struct TransactionProperties *props,
2322 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002323{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002324 TransactionActionList *dev_entry = dev_list;
Kevin Wolf62c9e412018-04-19 16:09:52 +02002325 JobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002326 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002327 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002328
John Snow50f43f02015-11-05 18:13:09 -05002329 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002330 QSIMPLEQ_INIT(&snap_bdrv_states);
2331
John Snow94d16a62015-11-05 18:13:18 -05002332 /* Does this transaction get canceled as a group on failure?
Kevin Wolf62c9e412018-04-19 16:09:52 +02002333 * If not, we don't really need to make a JobTxn.
John Snow94d16a62015-11-05 18:13:18 -05002334 */
2335 props = get_transaction_properties(props);
2336 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02002337 block_job_txn = job_txn_new();
John Snow94d16a62015-11-05 18:13:18 -05002338 }
2339
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002340 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002341 bdrv_drain_all();
2342
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002343 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002344 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002345 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002346 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002347
Jeff Cody8802d1f2012-02-28 15:54:06 -05002348 dev_info = dev_entry->value;
2349 dev_entry = dev_entry->next;
2350
Eric Blake6a8f9662015-10-26 16:34:54 -06002351 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002352
Eric Blake6a8f9662015-10-26 16:34:54 -06002353 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002354 assert(ops->instance_size > 0);
2355
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002356 state = g_malloc0(ops->instance_size);
2357 state->ops = ops;
2358 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002359 state->block_job_txn = block_job_txn;
2360 state->txn_props = props;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002361 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002362
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002363 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002364 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002365 error_propagate(errp, local_err);
2366 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002367 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002368 }
2369
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002370 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002371 if (state->ops->commit) {
2372 state->ops->commit(state);
2373 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002374 }
2375
2376 /* success */
2377 goto exit;
2378
2379delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002380 /* failure, and it is all-or-none; roll back all operations */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002381 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2382 if (state->ops->abort) {
2383 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002384 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002385 }
2386exit:
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002387 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2388 if (state->ops->clean) {
2389 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002390 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002391 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002392 }
John Snow94d16a62015-11-05 18:13:18 -05002393 if (!has_props) {
2394 qapi_free_TransactionProperties(props);
2395 }
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02002396 job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002397}
2398
Kevin Wolffbe2d812016-09-20 13:38:46 +02002399void qmp_eject(bool has_device, const char *device,
2400 bool has_id, const char *id,
2401 bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002402{
Max Reitz38f54bd2015-10-26 21:39:12 +01002403 Error *local_err = NULL;
John Snow3a3086b2016-05-18 17:53:40 -04002404 int rc;
Markus Armbruster666daa62010-06-02 18:48:27 +02002405
John Snow3a3086b2016-05-18 17:53:40 -04002406 if (!has_force) {
2407 force = false;
2408 }
2409
Kevin Wolffbe2d812016-09-20 13:38:46 +02002410 rc = do_open_tray(has_device ? device : NULL,
2411 has_id ? id : NULL,
2412 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002413 if (rc && rc != -ENOSYS) {
Max Reitz38f54bd2015-10-26 21:39:12 +01002414 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002415 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002416 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002417 error_free(local_err);
John Snow3a3086b2016-05-18 17:53:40 -04002418
Max Reitz82fcf662017-11-10 23:43:01 +01002419 blockdev_remove_medium(has_device, device, has_id, id, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002420}
2421
Benoît Canet12d3ba82014-01-23 21:31:35 +01002422void qmp_block_passwd(bool has_device, const char *device,
2423 bool has_node_name, const char *node_name,
2424 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002425{
Daniel P. Berrangec01c2142017-06-23 17:24:16 +01002426 error_setg(errp,
2427 "Setting block passwords directly is no longer supported");
Markus Armbruster666daa62010-06-02 18:48:27 +02002428}
2429
Colin Lordbf18bee2016-06-06 14:15:22 -04002430/*
2431 * Attempt to open the tray of @device.
2432 * If @force, ignore its tray lock.
2433 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2434 * On error, store an error through @errp and return -errno.
2435 * If @device does not exist, return -ENODEV.
2436 * If it has no removable media, return -ENOTSUP.
2437 * If it has no tray, return -ENOSYS.
2438 * If the guest was asked to open the tray, return -EINPROGRESS.
2439 * Else, return 0.
John Snow3a3086b2016-05-18 17:53:40 -04002440 */
Kevin Wolfb33945c2016-09-20 13:38:43 +02002441static int do_open_tray(const char *blk_name, const char *qdev_id,
2442 bool force, Error **errp)
Max Reitz7d8a9f72015-10-26 21:39:08 +01002443{
2444 BlockBackend *blk;
Kevin Wolfb33945c2016-09-20 13:38:43 +02002445 const char *device = qdev_id ?: blk_name;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002446 bool locked;
2447
Kevin Wolfb33945c2016-09-20 13:38:43 +02002448 blk = qmp_get_blk(blk_name, qdev_id, errp);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002449 if (!blk) {
John Snow3a3086b2016-05-18 17:53:40 -04002450 return -ENODEV;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002451 }
2452
2453 if (!blk_dev_has_removable_media(blk)) {
2454 error_setg(errp, "Device '%s' is not removable", device);
John Snow3a3086b2016-05-18 17:53:40 -04002455 return -ENOTSUP;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002456 }
2457
Max Reitz12c7ec82016-01-29 20:49:11 +01002458 if (!blk_dev_has_tray(blk)) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002459 error_setg(errp, "Device '%s' does not have a tray", device);
2460 return -ENOSYS;
Max Reitz12c7ec82016-01-29 20:49:11 +01002461 }
2462
Max Reitz7d8a9f72015-10-26 21:39:08 +01002463 if (blk_dev_is_tray_open(blk)) {
John Snow3a3086b2016-05-18 17:53:40 -04002464 return 0;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002465 }
2466
2467 locked = blk_dev_is_medium_locked(blk);
2468 if (locked) {
2469 blk_dev_eject_request(blk, force);
2470 }
2471
2472 if (!locked || force) {
Kevin Wolf39829a02017-01-24 14:21:41 +01002473 blk_dev_change_media_cb(blk, false, &error_abort);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002474 }
John Snow3a3086b2016-05-18 17:53:40 -04002475
2476 if (locked && !force) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002477 error_setg(errp, "Device '%s' is locked and force was not specified, "
2478 "wait for tray to open and try again", device);
2479 return -EINPROGRESS;
John Snow3a3086b2016-05-18 17:53:40 -04002480 }
2481
2482 return 0;
2483}
2484
Kevin Wolfb33945c2016-09-20 13:38:43 +02002485void qmp_blockdev_open_tray(bool has_device, const char *device,
2486 bool has_id, const char *id,
2487 bool has_force, bool force,
John Snow3a3086b2016-05-18 17:53:40 -04002488 Error **errp)
2489{
Colin Lordbf18bee2016-06-06 14:15:22 -04002490 Error *local_err = NULL;
2491 int rc;
2492
John Snow3a3086b2016-05-18 17:53:40 -04002493 if (!has_force) {
2494 force = false;
2495 }
Kevin Wolfb33945c2016-09-20 13:38:43 +02002496 rc = do_open_tray(has_device ? device : NULL,
2497 has_id ? id : NULL,
2498 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002499 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2500 error_propagate(errp, local_err);
2501 return;
2502 }
2503 error_free(local_err);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002504}
2505
Kevin Wolfb33945c2016-09-20 13:38:43 +02002506void qmp_blockdev_close_tray(bool has_device, const char *device,
2507 bool has_id, const char *id,
2508 Error **errp)
Max Reitzabaaf592015-10-26 21:39:09 +01002509{
2510 BlockBackend *blk;
Kevin Wolf39829a02017-01-24 14:21:41 +01002511 Error *local_err = NULL;
Max Reitzabaaf592015-10-26 21:39:09 +01002512
Kevin Wolfb33945c2016-09-20 13:38:43 +02002513 device = has_device ? device : NULL;
2514 id = has_id ? id : NULL;
2515
2516 blk = qmp_get_blk(device, id, errp);
Max Reitzabaaf592015-10-26 21:39:09 +01002517 if (!blk) {
Max Reitzabaaf592015-10-26 21:39:09 +01002518 return;
2519 }
2520
2521 if (!blk_dev_has_removable_media(blk)) {
Kevin Wolfb33945c2016-09-20 13:38:43 +02002522 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitzabaaf592015-10-26 21:39:09 +01002523 return;
2524 }
2525
Max Reitz12c7ec82016-01-29 20:49:11 +01002526 if (!blk_dev_has_tray(blk)) {
2527 /* Ignore this command on tray-less devices */
2528 return;
2529 }
2530
Max Reitzabaaf592015-10-26 21:39:09 +01002531 if (!blk_dev_is_tray_open(blk)) {
2532 return;
2533 }
2534
Kevin Wolf39829a02017-01-24 14:21:41 +01002535 blk_dev_change_media_cb(blk, true, &local_err);
2536 if (local_err) {
2537 error_propagate(errp, local_err);
2538 return;
2539 }
Max Reitzabaaf592015-10-26 21:39:09 +01002540}
2541
Max Reitz82fcf662017-11-10 23:43:01 +01002542static void blockdev_remove_medium(bool has_device, const char *device,
2543 bool has_id, const char *id, Error **errp)
Max Reitz2814f672015-10-26 21:39:10 +01002544{
2545 BlockBackend *blk;
2546 BlockDriverState *bs;
2547 AioContext *aio_context;
Kevin Wolf00949ba2016-09-20 13:38:45 +02002548 bool has_attached_device;
Max Reitz2814f672015-10-26 21:39:10 +01002549
Kevin Wolf00949ba2016-09-20 13:38:45 +02002550 device = has_device ? device : NULL;
2551 id = has_id ? id : NULL;
2552
2553 blk = qmp_get_blk(device, id, errp);
Max Reitz2814f672015-10-26 21:39:10 +01002554 if (!blk) {
Max Reitz2814f672015-10-26 21:39:10 +01002555 return;
2556 }
2557
2558 /* For BBs without a device, we can exchange the BDS tree at will */
Kevin Wolf00949ba2016-09-20 13:38:45 +02002559 has_attached_device = blk_get_attached_dev(blk);
Max Reitz2814f672015-10-26 21:39:10 +01002560
Kevin Wolf00949ba2016-09-20 13:38:45 +02002561 if (has_attached_device && !blk_dev_has_removable_media(blk)) {
2562 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002563 return;
2564 }
2565
Kevin Wolf00949ba2016-09-20 13:38:45 +02002566 if (has_attached_device && blk_dev_has_tray(blk) &&
2567 !blk_dev_is_tray_open(blk))
2568 {
2569 error_setg(errp, "Tray of device '%s' is not open", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002570 return;
2571 }
2572
2573 bs = blk_bs(blk);
2574 if (!bs) {
2575 return;
2576 }
2577
2578 aio_context = bdrv_get_aio_context(bs);
2579 aio_context_acquire(aio_context);
2580
2581 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2582 goto out;
2583 }
2584
Max Reitz2814f672015-10-26 21:39:10 +01002585 blk_remove_bs(blk);
2586
Max Reitz12c7ec82016-01-29 20:49:11 +01002587 if (!blk_dev_has_tray(blk)) {
2588 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2589 * called at all); therefore, the medium needs to be ejected here.
2590 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2591 * value passed here (i.e. false). */
Kevin Wolf39829a02017-01-24 14:21:41 +01002592 blk_dev_change_media_cb(blk, false, &error_abort);
Max Reitz12c7ec82016-01-29 20:49:11 +01002593 }
2594
Max Reitz2814f672015-10-26 21:39:10 +01002595out:
2596 aio_context_release(aio_context);
2597}
2598
Max Reitz34ce1112017-11-10 23:43:02 +01002599void qmp_blockdev_remove_medium(const char *id, Error **errp)
Max Reitz82fcf662017-11-10 23:43:01 +01002600{
2601 blockdev_remove_medium(false, NULL, true, id, errp);
2602}
2603
Kevin Wolf716df212016-09-20 13:38:44 +02002604static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
Max Reitzd1299882015-10-26 21:39:11 +01002605 BlockDriverState *bs, Error **errp)
2606{
Kevin Wolf39829a02017-01-24 14:21:41 +01002607 Error *local_err = NULL;
Max Reitzd1299882015-10-26 21:39:11 +01002608 bool has_device;
Kevin Wolfd7086422017-01-13 19:02:32 +01002609 int ret;
Max Reitzd1299882015-10-26 21:39:11 +01002610
Max Reitzd1299882015-10-26 21:39:11 +01002611 /* For BBs without a device, we can exchange the BDS tree at will */
2612 has_device = blk_get_attached_dev(blk);
2613
2614 if (has_device && !blk_dev_has_removable_media(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002615 error_setg(errp, "Device is not removable");
Max Reitzd1299882015-10-26 21:39:11 +01002616 return;
2617 }
2618
Max Reitz12c7ec82016-01-29 20:49:11 +01002619 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002620 error_setg(errp, "Tray of the device is not open");
Max Reitzd1299882015-10-26 21:39:11 +01002621 return;
2622 }
2623
2624 if (blk_bs(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002625 error_setg(errp, "There already is a medium in the device");
Max Reitzd1299882015-10-26 21:39:11 +01002626 return;
2627 }
2628
Kevin Wolfd7086422017-01-13 19:02:32 +01002629 ret = blk_insert_bs(blk, bs, errp);
2630 if (ret < 0) {
2631 return;
2632 }
Max Reitzd1299882015-10-26 21:39:11 +01002633
Max Reitz12c7ec82016-01-29 20:49:11 +01002634 if (!blk_dev_has_tray(blk)) {
2635 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2636 * called at all); therefore, the medium needs to be pushed into the
2637 * slot here.
2638 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2639 * value passed here (i.e. true). */
Kevin Wolf39829a02017-01-24 14:21:41 +01002640 blk_dev_change_media_cb(blk, true, &local_err);
2641 if (local_err) {
2642 error_propagate(errp, local_err);
2643 blk_remove_bs(blk);
2644 return;
2645 }
Max Reitz12c7ec82016-01-29 20:49:11 +01002646 }
Max Reitzd1299882015-10-26 21:39:11 +01002647}
2648
Max Reitz82fcf662017-11-10 23:43:01 +01002649static void blockdev_insert_medium(bool has_device, const char *device,
2650 bool has_id, const char *id,
2651 const char *node_name, Error **errp)
Max Reitzd1299882015-10-26 21:39:11 +01002652{
Kevin Wolf716df212016-09-20 13:38:44 +02002653 BlockBackend *blk;
Max Reitzd1299882015-10-26 21:39:11 +01002654 BlockDriverState *bs;
2655
Kevin Wolf716df212016-09-20 13:38:44 +02002656 blk = qmp_get_blk(has_device ? device : NULL,
2657 has_id ? id : NULL,
2658 errp);
2659 if (!blk) {
2660 return;
2661 }
2662
Max Reitzd1299882015-10-26 21:39:11 +01002663 bs = bdrv_find_node(node_name);
2664 if (!bs) {
2665 error_setg(errp, "Node '%s' not found", node_name);
2666 return;
2667 }
2668
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002669 if (bdrv_has_blk(bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02002670 error_setg(errp, "Node '%s' is already in use", node_name);
Max Reitzd1299882015-10-26 21:39:11 +01002671 return;
2672 }
2673
Kevin Wolf716df212016-09-20 13:38:44 +02002674 qmp_blockdev_insert_anon_medium(blk, bs, errp);
Max Reitzd1299882015-10-26 21:39:11 +01002675}
2676
Max Reitz34ce1112017-11-10 23:43:02 +01002677void qmp_blockdev_insert_medium(const char *id, const char *node_name,
2678 Error **errp)
Max Reitz82fcf662017-11-10 23:43:01 +01002679{
2680 blockdev_insert_medium(false, NULL, true, id, node_name, errp);
2681}
2682
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002683void qmp_blockdev_change_medium(bool has_device, const char *device,
2684 bool has_id, const char *id,
2685 const char *filename,
Max Reitz24fb4132015-11-06 16:27:06 +01002686 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002687 bool has_read_only,
2688 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002689 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002690{
2691 BlockBackend *blk;
2692 BlockDriverState *medium_bs = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002693 int bdrv_flags;
Kevin Wolfb85114f2016-09-12 19:08:31 +02002694 bool detect_zeroes;
Colin Lord38a53d52016-06-08 13:56:28 -04002695 int rc;
Max Reitzde2c6c02015-10-26 21:39:13 +01002696 QDict *options = NULL;
2697 Error *err = NULL;
2698
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002699 blk = qmp_get_blk(has_device ? device : NULL,
2700 has_id ? id : NULL,
2701 errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002702 if (!blk) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002703 goto fail;
2704 }
2705
2706 if (blk_bs(blk)) {
2707 blk_update_root_state(blk);
2708 }
2709
2710 bdrv_flags = blk_get_open_flags_from_root_state(blk);
Alyssa Milburn156abc22016-02-06 13:36:18 +00002711 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2712 BDRV_O_PROTOCOL);
Max Reitzde2c6c02015-10-26 21:39:13 +01002713
Max Reitz39ff43d2015-11-11 04:49:44 +01002714 if (!has_read_only) {
2715 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2716 }
2717
2718 switch (read_only) {
2719 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2720 break;
2721
2722 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2723 bdrv_flags &= ~BDRV_O_RDWR;
2724 break;
2725
2726 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2727 bdrv_flags |= BDRV_O_RDWR;
2728 break;
2729
2730 default:
2731 abort();
2732 }
2733
Kevin Wolfb85114f2016-09-12 19:08:31 +02002734 options = qdict_new();
2735 detect_zeroes = blk_get_detect_zeroes_from_root_state(blk);
Eric Blake46f5ac22017-04-27 16:58:17 -05002736 qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off");
Kevin Wolfb85114f2016-09-12 19:08:31 +02002737
Max Reitz24fb4132015-11-06 16:27:06 +01002738 if (has_format) {
Eric Blake46f5ac22017-04-27 16:58:17 -05002739 qdict_put_str(options, "driver", format);
Max Reitzde2c6c02015-10-26 21:39:13 +01002740 }
2741
Max Reitz5b363932016-05-17 16:41:31 +02002742 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2743 if (!medium_bs) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002744 goto fail;
2745 }
2746
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002747 rc = do_open_tray(has_device ? device : NULL,
2748 has_id ? id : NULL,
2749 false, &err);
Colin Lord38a53d52016-06-08 13:56:28 -04002750 if (rc && rc != -ENOSYS) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002751 error_propagate(errp, err);
2752 goto fail;
2753 }
Colin Lord38a53d52016-06-08 13:56:28 -04002754 error_free(err);
2755 err = NULL;
Max Reitzde2c6c02015-10-26 21:39:13 +01002756
Max Reitz82fcf662017-11-10 23:43:01 +01002757 blockdev_remove_medium(has_device, device, has_id, id, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002758 if (err) {
2759 error_propagate(errp, err);
2760 goto fail;
2761 }
2762
Kevin Wolf716df212016-09-20 13:38:44 +02002763 qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002764 if (err) {
2765 error_propagate(errp, err);
2766 goto fail;
2767 }
2768
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002769 qmp_blockdev_close_tray(has_device, device, has_id, id, errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002770
2771fail:
2772 /* If the medium has been inserted, the device has its own reference, so
2773 * ours must be relinquished; and if it has not been inserted successfully,
2774 * the reference must be relinquished anyway */
2775 bdrv_unref(medium_bs);
2776}
2777
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002778/* throttling disk I/O limits */
Eric Blake4dc93972016-07-13 21:50:21 -06002779void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002780{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002781 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002782 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002783 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002784 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002785
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002786 blk = qmp_get_blk(arg->has_device ? arg->device : NULL,
2787 arg->has_id ? arg->id : NULL,
2788 errp);
Fam Zhenga0e85442015-03-02 19:36:48 +08002789 if (!blk) {
Luiz Capitulino80047da2011-12-14 16:49:14 -02002790 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002791 }
Max Reitz5433c242015-10-19 17:53:29 +02002792
2793 aio_context = blk_get_aio_context(blk);
2794 aio_context_acquire(aio_context);
2795
Fam Zhenga0e85442015-03-02 19:36:48 +08002796 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002797 if (!bs) {
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002798 error_setg(errp, "Device has no medium");
Max Reitz5433c242015-10-19 17:53:29 +02002799 goto out;
2800 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002801
Alberto Garcia1588ab52016-02-18 12:27:00 +02002802 throttle_config_init(&cfg);
Eric Blake4dc93972016-07-13 21:50:21 -06002803 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2804 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2805 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002806
Eric Blake4dc93972016-07-13 21:50:21 -06002807 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2808 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2809 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
Benoît Canetcc0681c2013-09-02 14:14:39 +02002810
Eric Blake4dc93972016-07-13 21:50:21 -06002811 if (arg->has_bps_max) {
2812 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002813 }
Eric Blake4dc93972016-07-13 21:50:21 -06002814 if (arg->has_bps_rd_max) {
2815 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002816 }
Eric Blake4dc93972016-07-13 21:50:21 -06002817 if (arg->has_bps_wr_max) {
2818 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002819 }
Eric Blake4dc93972016-07-13 21:50:21 -06002820 if (arg->has_iops_max) {
2821 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002822 }
Eric Blake4dc93972016-07-13 21:50:21 -06002823 if (arg->has_iops_rd_max) {
2824 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002825 }
Eric Blake4dc93972016-07-13 21:50:21 -06002826 if (arg->has_iops_wr_max) {
2827 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002828 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002829
Eric Blake4dc93972016-07-13 21:50:21 -06002830 if (arg->has_bps_max_length) {
2831 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002832 }
Eric Blake4dc93972016-07-13 21:50:21 -06002833 if (arg->has_bps_rd_max_length) {
2834 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002835 }
Eric Blake4dc93972016-07-13 21:50:21 -06002836 if (arg->has_bps_wr_max_length) {
2837 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002838 }
Eric Blake4dc93972016-07-13 21:50:21 -06002839 if (arg->has_iops_max_length) {
2840 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002841 }
Eric Blake4dc93972016-07-13 21:50:21 -06002842 if (arg->has_iops_rd_max_length) {
2843 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002844 }
Eric Blake4dc93972016-07-13 21:50:21 -06002845 if (arg->has_iops_wr_max_length) {
2846 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002847 }
2848
Eric Blake4dc93972016-07-13 21:50:21 -06002849 if (arg->has_iops_size) {
2850 cfg.op_size = arg->iops_size;
Benoît Canet2024c1d2013-09-02 14:14:41 +02002851 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002852
Alberto Garciad5851082016-02-18 12:26:59 +02002853 if (!throttle_is_valid(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002854 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002855 }
2856
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002857 if (throttle_enabled(&cfg)) {
2858 /* Enable I/O limits if they're not enabled yet, otherwise
2859 * just update the throttling group. */
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +03002860 if (!blk_get_public(blk)->throttle_group_member.throttle_state) {
Eric Blake4dc93972016-07-13 21:50:21 -06002861 blk_io_limits_enable(blk,
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002862 arg->has_group ? arg->group :
2863 arg->has_device ? arg->device :
2864 arg->id);
Eric Blake4dc93972016-07-13 21:50:21 -06002865 } else if (arg->has_group) {
2866 blk_io_limits_update_group(blk, arg->group);
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002867 }
2868 /* Set the new throttling configuration */
Kevin Wolf97148072016-03-21 13:53:52 +01002869 blk_set_io_limits(blk, &cfg);
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +03002870 } else if (blk_get_public(blk)->throttle_group_member.throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002871 /* If all throttling settings are set to 0, disable I/O limits */
Kevin Wolf97148072016-03-21 13:53:52 +01002872 blk_io_limits_disable(blk);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002873 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002874
Max Reitz5433c242015-10-19 17:53:29 +02002875out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002876 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002877}
2878
John Snow341ebc22015-04-17 19:49:52 -04002879void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2880 bool has_granularity, uint32_t granularity,
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002881 bool has_persistent, bool persistent,
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03002882 bool has_autoload, bool autoload,
John Snow341ebc22015-04-17 19:49:52 -04002883 Error **errp)
2884{
John Snow341ebc22015-04-17 19:49:52 -04002885 BlockDriverState *bs;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002886 BdrvDirtyBitmap *bitmap;
John Snow341ebc22015-04-17 19:49:52 -04002887
2888 if (!name || name[0] == '\0') {
2889 error_setg(errp, "Bitmap name cannot be empty");
2890 return;
2891 }
2892
2893 bs = bdrv_lookup_bs(node, node, errp);
2894 if (!bs) {
2895 return;
2896 }
2897
John Snow341ebc22015-04-17 19:49:52 -04002898 if (has_granularity) {
2899 if (granularity < 512 || !is_power_of_2(granularity)) {
2900 error_setg(errp, "Granularity must be power of 2 "
2901 "and at least 512");
Paolo Bonzini21198822017-06-05 14:39:03 +02002902 return;
John Snow341ebc22015-04-17 19:49:52 -04002903 }
2904 } else {
2905 /* Default to cluster size, if available: */
2906 granularity = bdrv_get_default_bitmap_granularity(bs);
2907 }
2908
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002909 if (!has_persistent) {
2910 persistent = false;
2911 }
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03002912
Vladimir Sementsov-Ogievskiy3e99da52018-02-02 19:07:52 +03002913 if (has_autoload) {
2914 warn_report("Autoload option is deprecated and its value is ignored");
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03002915 }
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002916
2917 if (persistent &&
2918 !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp))
2919 {
2920 return;
2921 }
2922
2923 bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp);
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03002924 if (bitmap == NULL) {
2925 return;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002926 }
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03002927
2928 bdrv_dirty_bitmap_set_persistance(bitmap, persistent);
John Snow341ebc22015-04-17 19:49:52 -04002929}
2930
2931void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2932 Error **errp)
2933{
John Snow341ebc22015-04-17 19:49:52 -04002934 BlockDriverState *bs;
2935 BdrvDirtyBitmap *bitmap;
Vladimir Sementsov-Ogievskiy5c36c1a2017-06-28 15:05:29 +03002936 Error *local_err = NULL;
John Snow341ebc22015-04-17 19:49:52 -04002937
Paolo Bonzini21198822017-06-05 14:39:03 +02002938 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
John Snow341ebc22015-04-17 19:49:52 -04002939 if (!bitmap || !bs) {
2940 return;
2941 }
2942
John Snow9bd2b082015-04-17 19:49:57 -04002943 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2944 error_setg(errp,
2945 "Bitmap '%s' is currently frozen and cannot be removed",
2946 name);
Paolo Bonzini21198822017-06-05 14:39:03 +02002947 return;
Vladimir Sementsov-Ogievskiy4f43e952018-03-13 15:34:00 -04002948 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) {
2949 error_setg(errp,
2950 "Bitmap '%s' is currently locked and cannot be removed",
2951 name);
2952 return;
John Snow9bd2b082015-04-17 19:49:57 -04002953 }
Vladimir Sementsov-Ogievskiy5c36c1a2017-06-28 15:05:29 +03002954
2955 if (bdrv_dirty_bitmap_get_persistance(bitmap)) {
2956 bdrv_remove_persistent_dirty_bitmap(bs, name, &local_err);
2957 if (local_err != NULL) {
2958 error_propagate(errp, local_err);
2959 return;
2960 }
2961 }
2962
John Snow341ebc22015-04-17 19:49:52 -04002963 bdrv_release_dirty_bitmap(bs, bitmap);
John Snow341ebc22015-04-17 19:49:52 -04002964}
2965
John Snowe74e6b72015-04-17 19:49:59 -04002966/**
2967 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2968 * immediately after a full backup operation.
2969 */
2970void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2971 Error **errp)
2972{
John Snowe74e6b72015-04-17 19:49:59 -04002973 BdrvDirtyBitmap *bitmap;
2974 BlockDriverState *bs;
2975
Paolo Bonzini21198822017-06-05 14:39:03 +02002976 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
John Snowe74e6b72015-04-17 19:49:59 -04002977 if (!bitmap || !bs) {
2978 return;
2979 }
2980
2981 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2982 error_setg(errp,
2983 "Bitmap '%s' is currently frozen and cannot be modified",
2984 name);
Paolo Bonzini21198822017-06-05 14:39:03 +02002985 return;
Vladimir Sementsov-Ogievskiy4f43e952018-03-13 15:34:00 -04002986 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) {
2987 error_setg(errp,
2988 "Bitmap '%s' is currently locked and cannot be modified",
2989 name);
2990 return;
John Snowe74e6b72015-04-17 19:49:59 -04002991 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2992 error_setg(errp,
2993 "Bitmap '%s' is currently disabled and cannot be cleared",
2994 name);
Paolo Bonzini21198822017-06-05 14:39:03 +02002995 return;
Vladimir Sementsov-Ogievskiyd6883bc2017-06-28 15:05:10 +03002996 } else if (bdrv_dirty_bitmap_readonly(bitmap)) {
2997 error_setg(errp, "Bitmap '%s' is readonly and cannot be cleared", name);
2998 return;
John Snowe74e6b72015-04-17 19:49:59 -04002999 }
3000
Fam Zhengdf9a6812015-11-09 18:16:54 +08003001 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04003002}
3003
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04003004void qmp_x_block_dirty_bitmap_enable(const char *node, const char *name,
3005 Error **errp)
3006{
3007 BlockDriverState *bs;
3008 BdrvDirtyBitmap *bitmap;
3009
3010 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
3011 if (!bitmap) {
3012 return;
3013 }
3014
3015 if (bdrv_dirty_bitmap_frozen(bitmap)) {
3016 error_setg(errp,
3017 "Bitmap '%s' is currently frozen and cannot be enabled",
3018 name);
3019 return;
3020 }
3021
3022 bdrv_enable_dirty_bitmap(bitmap);
3023}
3024
3025void qmp_x_block_dirty_bitmap_disable(const char *node, const char *name,
3026 Error **errp)
3027{
3028 BlockDriverState *bs;
3029 BdrvDirtyBitmap *bitmap;
3030
3031 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
3032 if (!bitmap) {
3033 return;
3034 }
3035
3036 if (bdrv_dirty_bitmap_frozen(bitmap)) {
3037 error_setg(errp,
3038 "Bitmap '%s' is currently frozen and cannot be disabled",
3039 name);
3040 return;
3041 }
3042
3043 bdrv_disable_dirty_bitmap(bitmap);
3044}
3045
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003046void qmp_x_block_dirty_bitmap_merge(const char *node, const char *dst_name,
3047 const char *src_name, Error **errp)
3048{
3049 BlockDriverState *bs;
3050 BdrvDirtyBitmap *dst, *src;
3051
3052 dst = block_dirty_bitmap_lookup(node, dst_name, &bs, errp);
3053 if (!dst) {
3054 return;
3055 }
3056
3057 if (bdrv_dirty_bitmap_frozen(dst)) {
3058 error_setg(errp, "Bitmap '%s' is frozen and cannot be modified",
3059 dst_name);
3060 return;
3061 } else if (bdrv_dirty_bitmap_readonly(dst)) {
3062 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
3063 dst_name);
3064 return;
3065 }
3066
3067 src = bdrv_find_dirty_bitmap(bs, src_name);
3068 if (!src) {
3069 error_setg(errp, "Dirty bitmap '%s' not found", src_name);
3070 return;
3071 }
3072
3073 bdrv_merge_dirty_bitmap(dst, src, errp);
3074}
3075
Vladimir Sementsov-Ogievskiya3b52532017-06-28 15:05:25 +03003076BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
3077 const char *name,
3078 Error **errp)
3079{
3080 BdrvDirtyBitmap *bitmap;
3081 BlockDriverState *bs;
3082 BlockDirtyBitmapSha256 *ret = NULL;
3083 char *sha256;
3084
3085 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
3086 if (!bitmap || !bs) {
3087 return NULL;
3088 }
3089
3090 sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp);
3091 if (sha256 == NULL) {
3092 return NULL;
3093 }
3094
3095 ret = g_new(BlockDirtyBitmapSha256, 1);
3096 ret->sha256 = sha256;
3097
3098 return ret;
3099}
3100
Markus Armbruster072ebe62015-03-05 17:00:56 +01003101void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06003102{
3103 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003104 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06003105 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003106 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08003107 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06003108
Kevin Wolf2073d412016-02-23 17:50:37 +01003109 bs = bdrv_find_node(id);
3110 if (bs) {
Markus Armbruster79b7a772017-03-21 17:53:28 +01003111 qmp_blockdev_del(id, &local_err);
Kevin Wolf2073d412016-02-23 17:50:37 +01003112 if (local_err) {
3113 error_report_err(local_err);
3114 }
3115 return;
3116 }
3117
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003118 blk = blk_by_name(id);
3119 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02003120 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01003121 return;
Ryan Harper9063f812010-11-12 11:07:13 -06003122 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003123
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02003124 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02003125 error_report("Deleting device added with blockdev-add"
3126 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01003127 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02003128 }
3129
Max Reitz5433c242015-10-19 17:53:29 +02003130 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003131 aio_context_acquire(aio_context);
3132
Max Reitz5433c242015-10-19 17:53:29 +02003133 bs = blk_bs(blk);
3134 if (bs) {
3135 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
3136 error_report_err(local_err);
3137 aio_context_release(aio_context);
3138 return;
3139 }
Ryan Harper9063f812010-11-12 11:07:13 -06003140
Max Reitz938abd42016-01-29 16:36:09 +01003141 blk_remove_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02003142 }
Ryan Harper9063f812010-11-12 11:07:13 -06003143
Max Reitz7c735872016-03-16 19:54:39 +01003144 /* Make the BlockBackend and the attached BlockDriverState anonymous */
Max Reitzefaa7c42016-03-16 19:54:38 +01003145 monitor_remove_blk(blk);
3146
Max Reitz7c735872016-03-16 19:54:39 +01003147 /* If this BlockBackend has a device attached to it, its refcount will be
3148 * decremented when the device is removed; otherwise we have to do so here.
Ryan Harperd22b2f42011-03-29 20:51:47 -05003149 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02003150 if (blk_get_attached_dev(blk)) {
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02003151 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02003152 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
3153 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05003154 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02003155 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06003156 }
3157
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003158 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06003159}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003160
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003161void qmp_block_resize(bool has_device, const char *device,
3162 bool has_node_name, const char *node_name,
3163 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003164{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003165 Error *local_err = NULL;
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01003166 BlockBackend *blk = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003167 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003168 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02003169 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003170
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003171 bs = bdrv_lookup_bs(has_device ? device : NULL,
3172 has_node_name ? node_name : NULL,
3173 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003174 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003175 error_propagate(errp, local_err);
3176 return;
3177 }
3178
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003179 aio_context = bdrv_get_aio_context(bs);
3180 aio_context_acquire(aio_context);
3181
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003182 if (!bdrv_is_first_non_filter(bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003183 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003184 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003185 }
3186
3187 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003188 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003189 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003190 }
3191
Jeff Cody9c75e162014-06-25 16:55:30 -04003192 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003193 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003194 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04003195 }
3196
Kevin Wolf6d0eb642017-01-20 17:07:26 +01003197 blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL);
Kevin Wolfd7086422017-01-13 19:02:32 +01003198 ret = blk_insert_bs(blk, bs, errp);
3199 if (ret < 0) {
3200 goto out;
3201 }
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01003202
John Snow698bdfa2017-05-10 13:39:45 -04003203 bdrv_drained_begin(bs);
Max Reitz3a691c52017-06-13 22:20:54 +02003204 ret = blk_truncate(blk, size, PREALLOC_MODE_OFF, errp);
John Snow698bdfa2017-05-10 13:39:45 -04003205 bdrv_drained_end(bs);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003206
3207out:
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01003208 blk_unref(blk);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003209 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003210}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003211
Alberto Garcia23233222016-07-05 17:28:59 +03003212void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04003213 bool has_base, const char *base,
Alberto Garcia312fe092016-10-28 10:08:19 +03003214 bool has_base_node, const char *base_node,
Jeff Cody13d8cc52014-06-25 15:40:11 -04003215 bool has_backing_file, const char *backing_file,
3216 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02003217 bool has_on_error, BlockdevOnError on_error,
3218 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003219{
Alberto Garcia554b6142016-10-28 10:08:11 +03003220 BlockDriverState *bs, *iter;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003221 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003222 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003223 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003224 const char *base_name = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003225
Paolo Bonzini1d809092012-09-28 17:22:59 +02003226 if (!has_on_error) {
3227 on_error = BLOCKDEV_ON_ERROR_REPORT;
3228 }
3229
Alberto Garcia554b6142016-10-28 10:08:11 +03003230 bs = bdrv_lookup_bs(device, device, errp);
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003231 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003232 return;
3233 }
3234
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003235 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003236 aio_context_acquire(aio_context);
3237
Alberto Garcia312fe092016-10-28 10:08:19 +03003238 if (has_base && has_base_node) {
3239 error_setg(errp, "'base' and 'base-node' cannot be specified "
3240 "at the same time");
3241 goto out;
3242 }
3243
Jeff Cody13d8cc52014-06-25 15:40:11 -04003244 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003245 base_bs = bdrv_find_backing_image(bs, base);
3246 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003247 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003248 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003249 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003250 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04003251 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003252 }
3253
Alberto Garcia312fe092016-10-28 10:08:19 +03003254 if (has_base_node) {
3255 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
3256 if (!base_bs) {
3257 goto out;
3258 }
3259 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
3260 error_setg(errp, "Node '%s' is not a backing image of '%s'",
3261 base_node, device);
3262 goto out;
3263 }
3264 assert(bdrv_get_aio_context(base_bs) == aio_context);
3265 base_name = base_bs->filename;
3266 }
3267
Alberto Garcia554b6142016-10-28 10:08:11 +03003268 /* Check for op blockers in the whole chain between bs and base */
3269 for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) {
3270 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
3271 goto out;
3272 }
3273 }
3274
Jeff Cody13d8cc52014-06-25 15:40:11 -04003275 /* if we are streaming the entire chain, the result will have no backing
3276 * file, and specifying one is therefore an error */
3277 if (base_bs == NULL && has_backing_file) {
3278 error_setg(errp, "backing file specified, but streaming the "
3279 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003280 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003281 }
3282
3283 /* backing_file string overrides base bs filename */
3284 base_name = has_backing_file ? backing_file : base_name;
3285
Alberto Garcia23233222016-07-05 17:28:59 +03003286 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
John Snow8254b6d2016-10-27 12:06:58 -04003287 has_speed ? speed : 0, on_error, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003288 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003289 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003290 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003291 }
3292
3293 trace_qmp_block_stream(bs, bs->job);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003294
3295out:
3296 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003297}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003298
Alberto Garciafd62c602016-07-05 17:29:00 +03003299void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003300 bool has_base, const char *base,
3301 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003302 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04003303 bool has_speed, int64_t speed,
Kevin Wolf0db832f2017-02-20 18:10:05 +01003304 bool has_filter_node_name, const char *filter_node_name,
Jeff Codyed61fc12012-09-27 13:29:16 -04003305 Error **errp)
3306{
3307 BlockDriverState *bs;
Alberto Garcia058223a2016-10-28 10:08:07 +03003308 BlockDriverState *iter;
Jeff Codyed61fc12012-09-27 13:29:16 -04003309 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003310 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04003311 Error *local_err = NULL;
3312 /* This will be part of the QMP command, if/when the
3313 * BlockdevOnError change for blkmirror makes it in
3314 */
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02003315 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
Jeff Codyed61fc12012-09-27 13:29:16 -04003316
Max Reitz54504662014-04-10 19:36:25 +02003317 if (!has_speed) {
3318 speed = 0;
3319 }
Kevin Wolf0db832f2017-02-20 18:10:05 +01003320 if (!has_filter_node_name) {
3321 filter_node_name = NULL;
3322 }
Max Reitz54504662014-04-10 19:36:25 +02003323
Jeff Cody7676e2c2014-06-30 15:14:15 +02003324 /* Important Note:
3325 * libvirt relies on the DeviceNotFound error class in order to probe for
3326 * live commit feature versions; for this to work, we must make sure to
3327 * perform the device lookup before any generic errors that may occur in a
3328 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02003329 bs = qmp_get_root_bs(device, &local_err);
3330 if (!bs) {
3331 bs = bdrv_lookup_bs(device, device, NULL);
3332 if (!bs) {
3333 error_free(local_err);
3334 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3335 "Device '%s' not found", device);
3336 } else {
3337 error_propagate(errp, local_err);
3338 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003339 return;
3340 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003341
Kevin Wolf1d13b162016-06-23 14:20:24 +02003342 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003343 aio_context_acquire(aio_context);
3344
Fam Zhengbb000212014-09-11 13:14:00 +08003345 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003346 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003347 }
3348
Jeff Codyed61fc12012-09-27 13:29:16 -04003349 /* default top_bs is the active layer */
3350 top_bs = bs;
3351
Jeff Cody7676e2c2014-06-30 15:14:15 +02003352 if (has_top && top) {
Jeff Codyed61fc12012-09-27 13:29:16 -04003353 if (strcmp(bs->filename, top) != 0) {
3354 top_bs = bdrv_find_backing_image(bs, top);
3355 }
3356 }
3357
3358 if (top_bs == NULL) {
3359 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003360 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003361 }
3362
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003363 assert(bdrv_get_aio_context(top_bs) == aio_context);
3364
Jeff Codyd5208c42012-10-16 15:49:10 -04003365 if (has_base && base) {
3366 base_bs = bdrv_find_backing_image(top_bs, base);
3367 } else {
3368 base_bs = bdrv_find_base(top_bs);
3369 }
3370
3371 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003372 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003373 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04003374 }
3375
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003376 assert(bdrv_get_aio_context(base_bs) == aio_context);
3377
Alberto Garcia058223a2016-10-28 10:08:07 +03003378 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
3379 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3380 goto out;
3381 }
Fam Zhengbb000212014-09-11 13:14:00 +08003382 }
3383
Jeff Cody7676e2c2014-06-30 15:14:15 +02003384 /* Do not allow attempts to commit an image into itself */
3385 if (top_bs == base_bs) {
3386 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003387 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02003388 }
3389
Fam Zheng20a63d22013-12-16 14:45:31 +08003390 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04003391 if (has_backing_file) {
3392 error_setg(errp, "'backing-file' specified,"
3393 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003394 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003395 }
John Snow47970df2016-10-27 12:06:57 -04003396 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
Kevin Wolfbb02b652018-04-19 17:54:56 +02003397 JOB_DEFAULT, speed, on_error,
Fam Zheng78bbd912017-04-21 20:27:04 +08003398 filter_node_name, NULL, NULL, false, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003399 } else {
Alberto Garcia058223a2016-10-28 10:08:07 +03003400 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
3401 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3402 goto out;
3403 }
Alberto Garciafd62c602016-07-05 17:29:00 +03003404 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed,
John Snow8254b6d2016-10-27 12:06:58 -04003405 on_error, has_backing_file ? backing_file : NULL,
Kevin Wolf0db832f2017-02-20 18:10:05 +01003406 filter_node_name, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003407 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003408 if (local_err != NULL) {
3409 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003410 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003411 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003412
3413out:
3414 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003415}
3416
Kevin Wolf62c9e412018-04-19 16:09:52 +02003417static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn,
John Snow111049a2016-11-08 01:50:38 -05003418 Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003419{
3420 BlockDriverState *bs;
3421 BlockDriverState *target_bs;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003422 BlockDriverState *source = NULL;
John Snow111049a2016-11-08 01:50:38 -05003423 BlockJob *job = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003424 BdrvDirtyBitmap *bmap = NULL;
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003425 AioContext *aio_context;
Max Reitze6641712015-08-26 19:47:48 +02003426 QDict *options = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003427 Error *local_err = NULL;
Kevin Wolfbb02b652018-04-19 17:54:56 +02003428 int flags, job_flags = JOB_DEFAULT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003429 int64_t size;
Fam Zhengfc0932f2017-05-03 00:35:53 +08003430 bool set_backing_hd = false;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003431
Pavel Butsykin81206a82016-07-22 11:17:50 +03003432 if (!backup->has_speed) {
3433 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003434 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003435 if (!backup->has_on_source_error) {
3436 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003437 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003438 if (!backup->has_on_target_error) {
3439 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003440 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003441 if (!backup->has_mode) {
3442 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3443 }
3444 if (!backup->has_job_id) {
3445 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003446 }
John Snowb40dacd2018-03-10 03:27:44 -05003447 if (!backup->has_auto_finalize) {
3448 backup->auto_finalize = true;
3449 }
3450 if (!backup->has_auto_dismiss) {
3451 backup->auto_dismiss = true;
3452 }
Pavel Butsykin13b94142016-07-22 11:17:52 +03003453 if (!backup->has_compress) {
3454 backup->compress = false;
3455 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003456
Pavel Butsykin81206a82016-07-22 11:17:50 +03003457 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003458 if (!bs) {
John Snow111049a2016-11-08 01:50:38 -05003459 return NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003460 }
3461
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003462 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003463 aio_context_acquire(aio_context);
3464
Pavel Butsykin81206a82016-07-22 11:17:50 +03003465 if (!backup->has_format) {
3466 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
3467 NULL : (char*) bs->drv->format_name;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003468 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003469
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003470 /* Early check to avoid creating target */
Fam Zheng3718d8a2014-05-23 21:29:43 +08003471 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003472 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003473 }
3474
Kevin Wolf61de4c62016-03-18 17:46:45 +01003475 flags = bs->open_flags | BDRV_O_RDWR;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003476
Ian Mainfc5d3f82013-07-26 11:39:04 -07003477 /* See if we have a backing HD we can use to create our new image
3478 * on top of. */
Pavel Butsykin81206a82016-07-22 11:17:50 +03003479 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003480 source = backing_bs(bs);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003481 if (!source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003482 backup->sync = MIRROR_SYNC_MODE_FULL;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003483 }
3484 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003485 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
Ian Mainfc5d3f82013-07-26 11:39:04 -07003486 source = bs;
Fam Zhengfc0932f2017-05-03 00:35:53 +08003487 flags |= BDRV_O_NO_BACKING;
3488 set_backing_hd = true;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003489 }
3490
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003491 size = bdrv_getlength(bs);
3492 if (size < 0) {
3493 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003494 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003495 }
3496
Pavel Butsykin81206a82016-07-22 11:17:50 +03003497 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
3498 assert(backup->format);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003499 if (source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003500 bdrv_img_create(backup->target, backup->format, source->filename,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003501 source->drv->format_name, NULL,
Fam Zheng92172832017-04-21 20:27:01 +08003502 size, flags, false, &local_err);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003503 } else {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003504 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
Fam Zheng92172832017-04-21 20:27:01 +08003505 size, flags, false, &local_err);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003506 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003507 }
3508
Markus Armbruster84d18f02014-01-30 15:07:28 +01003509 if (local_err) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003510 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003511 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003512 }
3513
Pavel Butsykin81206a82016-07-22 11:17:50 +03003514 if (backup->format) {
Fam Zhengfc0932f2017-05-03 00:35:53 +08003515 if (!options) {
3516 options = qdict_new();
3517 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003518 qdict_put_str(options, "driver", backup->format);
Max Reitze6641712015-08-26 19:47:48 +02003519 }
3520
Pavel Butsykin81206a82016-07-22 11:17:50 +03003521 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003522 if (!target_bs) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003523 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003524 }
3525
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003526 bdrv_set_aio_context(target_bs, aio_context);
3527
Fam Zhengfc0932f2017-05-03 00:35:53 +08003528 if (set_backing_hd) {
3529 bdrv_set_backing_hd(target_bs, source, &local_err);
3530 if (local_err) {
3531 bdrv_unref(target_bs);
3532 goto out;
3533 }
3534 }
3535
Pavel Butsykin81206a82016-07-22 11:17:50 +03003536 if (backup->has_bitmap) {
3537 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
John Snowd58d8452015-04-17 19:49:58 -04003538 if (!bmap) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003539 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
Max Reitz0702d3d2015-11-09 23:39:10 +01003540 bdrv_unref(target_bs);
John Snowd58d8452015-04-17 19:49:58 -04003541 goto out;
3542 }
Vladimir Sementsov-Ogievskiy4f43e952018-03-13 15:34:00 -04003543 if (bdrv_dirty_bitmap_qmp_locked(bmap)) {
3544 error_setg(errp,
3545 "Bitmap '%s' is currently locked and cannot be used for "
3546 "backup", backup->bitmap);
3547 goto out;
3548 }
John Snowd58d8452015-04-17 19:49:58 -04003549 }
John Snowb40dacd2018-03-10 03:27:44 -05003550 if (!backup->auto_finalize) {
Kevin Wolfbb02b652018-04-19 17:54:56 +02003551 job_flags |= JOB_MANUAL_FINALIZE;
John Snowb40dacd2018-03-10 03:27:44 -05003552 }
3553 if (!backup->auto_dismiss) {
Kevin Wolfbb02b652018-04-19 17:54:56 +02003554 job_flags |= JOB_MANUAL_DISMISS;
John Snowb40dacd2018-03-10 03:27:44 -05003555 }
John Snowd58d8452015-04-17 19:49:58 -04003556
John Snow111049a2016-11-08 01:50:38 -05003557 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
3558 backup->sync, bmap, backup->compress,
3559 backup->on_source_error, backup->on_target_error,
John Snowb40dacd2018-03-10 03:27:44 -05003560 job_flags, NULL, NULL, txn, &local_err);
Kevin Wolf5c438bc2016-04-14 13:09:53 +02003561 bdrv_unref(target_bs);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003562 if (local_err != NULL) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003563 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003564 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003565 }
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003566
3567out:
3568 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05003569 return job;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003570}
3571
Pavel Butsykin81206a82016-07-22 11:17:50 +03003572void qmp_drive_backup(DriveBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003573{
John Snow111049a2016-11-08 01:50:38 -05003574
3575 BlockJob *job;
3576 job = do_drive_backup(arg, NULL, errp);
3577 if (job) {
Kevin Wolfda01ff72018-04-13 17:31:02 +02003578 job_start(&job->job);
John Snow111049a2016-11-08 01:50:38 -05003579 }
John Snow78f51fd2015-11-05 18:13:17 -05003580}
3581
Benoît Canetc13163f2014-01-23 21:31:34 +01003582BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3583{
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003584 return bdrv_named_nodes_list(errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003585}
3586
Kevin Wolf62c9e412018-04-19 16:09:52 +02003587BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn,
John Snow111049a2016-11-08 01:50:38 -05003588 Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003589{
3590 BlockDriverState *bs;
3591 BlockDriverState *target_bs;
3592 Error *local_err = NULL;
3593 AioContext *aio_context;
John Snow111049a2016-11-08 01:50:38 -05003594 BlockJob *job = NULL;
Kevin Wolfbb02b652018-04-19 17:54:56 +02003595 int job_flags = JOB_DEFAULT;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003596
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003597 if (!backup->has_speed) {
3598 backup->speed = 0;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003599 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003600 if (!backup->has_on_source_error) {
3601 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003602 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003603 if (!backup->has_on_target_error) {
3604 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3605 }
3606 if (!backup->has_job_id) {
3607 backup->job_id = NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003608 }
John Snowb40dacd2018-03-10 03:27:44 -05003609 if (!backup->has_auto_finalize) {
3610 backup->auto_finalize = true;
3611 }
3612 if (!backup->has_auto_dismiss) {
3613 backup->auto_dismiss = true;
3614 }
Pavel Butsykin3b7b1232016-07-22 11:17:53 +03003615 if (!backup->has_compress) {
3616 backup->compress = false;
3617 }
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003618
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003619 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003620 if (!bs) {
John Snow111049a2016-11-08 01:50:38 -05003621 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003622 }
3623
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003624 aio_context = bdrv_get_aio_context(bs);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003625 aio_context_acquire(aio_context);
3626
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003627 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
Fam Zheng0d978912016-05-23 10:19:35 +08003628 if (!target_bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003629 goto out;
3630 }
Max Reitz5433c242015-10-19 17:53:29 +02003631
Fam Zhengefd75562016-05-23 10:19:36 +08003632 if (bdrv_get_aio_context(target_bs) != aio_context) {
3633 if (!bdrv_has_blk(target_bs)) {
3634 /* The target BDS is not attached, we can safely move it to another
3635 * AioContext. */
3636 bdrv_set_aio_context(target_bs, aio_context);
3637 } else {
3638 error_setg(errp, "Target is attached to a different thread from "
3639 "source.");
3640 goto out;
3641 }
3642 }
John Snowb40dacd2018-03-10 03:27:44 -05003643 if (!backup->auto_finalize) {
Kevin Wolfbb02b652018-04-19 17:54:56 +02003644 job_flags |= JOB_MANUAL_FINALIZE;
John Snowb40dacd2018-03-10 03:27:44 -05003645 }
3646 if (!backup->auto_dismiss) {
Kevin Wolfbb02b652018-04-19 17:54:56 +02003647 job_flags |= JOB_MANUAL_DISMISS;
John Snowb40dacd2018-03-10 03:27:44 -05003648 }
John Snow111049a2016-11-08 01:50:38 -05003649 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
3650 backup->sync, NULL, backup->compress,
3651 backup->on_source_error, backup->on_target_error,
John Snowb40dacd2018-03-10 03:27:44 -05003652 job_flags, NULL, NULL, txn, &local_err);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003653 if (local_err != NULL) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003654 error_propagate(errp, local_err);
3655 }
3656out:
3657 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05003658 return job;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003659}
3660
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003661void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003662{
John Snow111049a2016-11-08 01:50:38 -05003663 BlockJob *job;
3664 job = do_blockdev_backup(arg, NULL, errp);
3665 if (job) {
Kevin Wolfda01ff72018-04-13 17:31:02 +02003666 job_start(&job->job);
John Snow111049a2016-11-08 01:50:38 -05003667 }
John Snow78f51fd2015-11-05 18:13:17 -05003668}
3669
Fam Zheng4193cdd2015-12-24 12:45:03 +08003670/* Parameter check and block job starting for drive mirroring.
3671 * Caller should hold @device and @target's aio context (must be the same).
3672 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003673static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003674 BlockDriverState *target,
3675 bool has_replaces, const char *replaces,
3676 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003677 BlockMirrorBackingMode backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003678 bool has_speed, int64_t speed,
3679 bool has_granularity, uint32_t granularity,
3680 bool has_buf_size, int64_t buf_size,
3681 bool has_on_source_error,
3682 BlockdevOnError on_source_error,
3683 bool has_on_target_error,
3684 BlockdevOnError on_target_error,
3685 bool has_unmap, bool unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003686 bool has_filter_node_name,
3687 const char *filter_node_name,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003688 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003689{
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003690
3691 if (!has_speed) {
3692 speed = 0;
3693 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003694 if (!has_on_source_error) {
3695 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3696 }
3697 if (!has_on_target_error) {
3698 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3699 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003700 if (!has_granularity) {
3701 granularity = 0;
3702 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003703 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003704 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003705 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003706 if (!has_unmap) {
3707 unmap = true;
3708 }
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003709 if (!has_filter_node_name) {
3710 filter_node_name = NULL;
3711 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003712
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003713 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003714 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3715 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003716 return;
3717 }
3718 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003719 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3720 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003721 return;
3722 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003723
Fam Zheng4193cdd2015-12-24 12:45:03 +08003724 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3725 return;
3726 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003727 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3728 return;
3729 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003730
3731 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3732 sync = MIRROR_SYNC_MODE_FULL;
3733 }
3734
3735 /* pass the node name to replace to mirror start since it's loose coupling
3736 * and will allow to check whether the node still exist at mirror completion
3737 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003738 mirror_start(job_id, bs, target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003739 has_replaces ? replaces : NULL,
Max Reitz274fcce2016-06-10 20:57:47 +02003740 speed, granularity, buf_size, sync, backing_mode,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003741 on_source_error, on_target_error, unmap, filter_node_name,
3742 errp);
Fam Zheng4193cdd2015-12-24 12:45:03 +08003743}
3744
Eric Blakefaecd402016-07-14 16:37:58 -06003745void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003746{
3747 BlockDriverState *bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003748 BlockDriverState *source, *target_bs;
3749 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003750 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003751 Error *local_err = NULL;
3752 QDict *options = NULL;
3753 int flags;
3754 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003755 const char *format = arg->format;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003756
Kevin Wolf0524e932016-06-23 14:20:24 +02003757 bs = qmp_get_root_bs(arg->device, errp);
3758 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003759 return;
3760 }
3761
Paolo Bonzinicb2af912018-02-07 17:29:20 +01003762 /* Early check to avoid creating target */
3763 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3764 return;
3765 }
3766
Kevin Wolf0524e932016-06-23 14:20:24 +02003767 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003768 aio_context_acquire(aio_context);
3769
Eric Blakefaecd402016-07-14 16:37:58 -06003770 if (!arg->has_mode) {
3771 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003772 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003773
Eric Blakefaecd402016-07-14 16:37:58 -06003774 if (!arg->has_format) {
3775 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3776 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003777 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003778
Kevin Wolf61de4c62016-03-18 17:46:45 +01003779 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003780 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003781 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3782 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003783 }
Eric Blakefaecd402016-07-14 16:37:58 -06003784 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003785 source = bs;
3786 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003787
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003788 size = bdrv_getlength(bs);
3789 if (size < 0) {
3790 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003791 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003792 }
3793
Eric Blakefaecd402016-07-14 16:37:58 -06003794 if (arg->has_replaces) {
Benoît Canet09158f02014-06-27 18:25:25 +02003795 BlockDriverState *to_replace_bs;
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003796 AioContext *replace_aio_context;
3797 int64_t replace_size;
Benoît Canet09158f02014-06-27 18:25:25 +02003798
Eric Blakefaecd402016-07-14 16:37:58 -06003799 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003800 error_setg(errp, "a node-name must be provided when replacing a"
3801 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003802 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003803 }
3804
Eric Blakefaecd402016-07-14 16:37:58 -06003805 to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err);
Benoît Canet09158f02014-06-27 18:25:25 +02003806
3807 if (!to_replace_bs) {
3808 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003809 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003810 }
3811
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003812 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3813 aio_context_acquire(replace_aio_context);
3814 replace_size = bdrv_getlength(to_replace_bs);
3815 aio_context_release(replace_aio_context);
3816
3817 if (size != replace_size) {
Benoît Canet09158f02014-06-27 18:25:25 +02003818 error_setg(errp, "cannot replace image with a mirror image of "
3819 "different size");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003820 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003821 }
3822 }
3823
Eric Blakefaecd402016-07-14 16:37:58 -06003824 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003825 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3826 } else {
3827 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3828 }
3829
John Snow2a32c6e2017-07-17 20:34:21 -04003830 /* Don't open backing image in create() */
3831 flags |= BDRV_O_NO_BACKING;
3832
Eric Blakefaecd402016-07-14 16:37:58 -06003833 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3834 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003835 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003836 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003837 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003838 bdrv_img_create(arg->target, format,
Fam Zheng92172832017-04-21 20:27:01 +08003839 NULL, NULL, NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003840 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003841 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003842 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003843 break;
3844 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3845 /* create new image with backing file */
Eric Blakefaecd402016-07-14 16:37:58 -06003846 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003847 source->filename,
3848 source->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08003849 NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003850 break;
3851 default:
3852 abort();
3853 }
3854 }
3855
Markus Armbruster84d18f02014-01-30 15:07:28 +01003856 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003857 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003858 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003859 }
3860
Max Reitze6641712015-08-26 19:47:48 +02003861 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003862 if (arg->has_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003863 qdict_put_str(options, "node-name", arg->node_name);
Benoît Canet4c828dc2014-06-16 12:00:55 +02003864 }
Max Reitze6641712015-08-26 19:47:48 +02003865 if (format) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003866 qdict_put_str(options, "driver", format);
Max Reitze6641712015-08-26 19:47:48 +02003867 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003868
Paolo Bonzinib812f672013-01-21 17:09:43 +01003869 /* Mirroring takes care of copy-on-write using the source's backing
3870 * file.
3871 */
John Snow2a32c6e2017-07-17 20:34:21 -04003872 target_bs = bdrv_open(arg->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003873 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003874 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003875 }
3876
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003877 bdrv_set_aio_context(target_bs, aio_context);
3878
Eric Blakefaecd402016-07-14 16:37:58 -06003879 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3880 arg->has_replaces, arg->replaces, arg->sync,
3881 backing_mode, arg->has_speed, arg->speed,
3882 arg->has_granularity, arg->granularity,
3883 arg->has_buf_size, arg->buf_size,
3884 arg->has_on_source_error, arg->on_source_error,
3885 arg->has_on_target_error, arg->on_target_error,
3886 arg->has_unmap, arg->unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003887 false, NULL,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003888 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02003889 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003890 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003891out:
3892 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003893}
3894
Alberto Garcia71aa9862016-07-05 17:28:57 +03003895void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3896 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08003897 bool has_replaces, const char *replaces,
3898 MirrorSyncMode sync,
3899 bool has_speed, int64_t speed,
3900 bool has_granularity, uint32_t granularity,
3901 bool has_buf_size, int64_t buf_size,
3902 bool has_on_source_error,
3903 BlockdevOnError on_source_error,
3904 bool has_on_target_error,
3905 BlockdevOnError on_target_error,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003906 bool has_filter_node_name,
3907 const char *filter_node_name,
Fam Zhengdf925622015-12-24 12:45:05 +08003908 Error **errp)
3909{
3910 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08003911 BlockDriverState *target_bs;
3912 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003913 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08003914 Error *local_err = NULL;
3915
Kevin Wolf07eec652016-06-23 14:20:24 +02003916 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08003917 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08003918 return;
3919 }
3920
3921 target_bs = bdrv_lookup_bs(target, target, errp);
3922 if (!target_bs) {
3923 return;
3924 }
3925
3926 aio_context = bdrv_get_aio_context(bs);
3927 aio_context_acquire(aio_context);
3928
Fam Zhengdf925622015-12-24 12:45:05 +08003929 bdrv_set_aio_context(target_bs, aio_context);
3930
Alberto Garcia71aa9862016-07-05 17:28:57 +03003931 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02003932 has_replaces, replaces, sync, backing_mode,
Fam Zhengdf925622015-12-24 12:45:05 +08003933 has_speed, speed,
3934 has_granularity, granularity,
3935 has_buf_size, buf_size,
3936 has_on_source_error, on_source_error,
3937 has_on_target_error, on_target_error,
3938 true, true,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003939 has_filter_node_name, filter_node_name,
Fam Zhengdf925622015-12-24 12:45:05 +08003940 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003941 error_propagate(errp, local_err);
Fam Zhengdf925622015-12-24 12:45:05 +08003942
3943 aio_context_release(aio_context);
3944}
3945
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003946/* Get a block job using its ID and acquire its AioContext */
3947static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003948 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003949{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003950 BlockJob *job;
3951
3952 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003953
Max Reitz5433c242015-10-19 17:53:29 +02003954 *aio_context = NULL;
3955
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003956 job = block_job_get(id);
3957
3958 if (!job) {
3959 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3960 "Block job '%s' not found", id);
3961 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003962 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003963
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003964 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003965 aio_context_acquire(*aio_context);
3966
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003967 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003968}
3969
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003970void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003971{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003972 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003973 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003974
3975 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003976 return;
3977 }
3978
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003979 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003980 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003981}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003982
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003983void qmp_block_job_cancel(const char *device,
3984 bool has_force, bool force, Error **errp)
3985{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003986 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003987 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003988
3989 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003990 return;
3991 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003992
3993 if (!has_force) {
3994 force = false;
3995 }
3996
Kevin Wolfb15de822018-04-18 17:10:26 +02003997 if (job_user_paused(&job->job) && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003998 error_setg(errp, "The block job for device '%s' is currently paused",
3999 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004000 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004001 }
4002
4003 trace_qmp_block_job_cancel(job);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02004004 job_user_cancel(&job->job, force, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004005out:
4006 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004007}
4008
4009void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004010{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004011 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004012 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004013
John Snow0ec4dfb2018-03-10 03:27:32 -05004014 if (!job) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004015 return;
4016 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004017
4018 trace_qmp_block_job_pause(job);
Kevin Wolfb15de822018-04-18 17:10:26 +02004019 job_user_pause(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004020 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004021}
4022
4023void qmp_block_job_resume(const char *device, Error **errp)
4024{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004025 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004026 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004027
John Snow0ec4dfb2018-03-10 03:27:32 -05004028 if (!job) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02004029 return;
4030 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004031
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004032 trace_qmp_block_job_resume(job);
Kevin Wolfb15de822018-04-18 17:10:26 +02004033 job_user_resume(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004034 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004035}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004036
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004037void qmp_block_job_complete(const char *device, Error **errp)
4038{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004039 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004040 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004041
4042 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004043 return;
4044 }
4045
4046 trace_qmp_block_job_complete(job);
Kevin Wolf3453d972018-04-23 12:24:16 +02004047 job_complete(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004048 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004049}
4050
John Snow11b61fb2018-03-10 03:27:43 -05004051void qmp_block_job_finalize(const char *id, Error **errp)
4052{
4053 AioContext *aio_context;
4054 BlockJob *job = find_block_job(id, &aio_context, errp);
4055
4056 if (!job) {
4057 return;
4058 }
4059
4060 trace_qmp_block_job_finalize(job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02004061 job_finalize(&job->job, errp);
John Snow11b61fb2018-03-10 03:27:43 -05004062 aio_context_release(aio_context);
4063}
4064
John Snow75f71052018-03-10 03:27:36 -05004065void qmp_block_job_dismiss(const char *id, Error **errp)
4066{
4067 AioContext *aio_context;
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02004068 BlockJob *bjob = find_block_job(id, &aio_context, errp);
4069 Job *job;
John Snow75f71052018-03-10 03:27:36 -05004070
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02004071 if (!bjob) {
John Snow75f71052018-03-10 03:27:36 -05004072 return;
4073 }
4074
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02004075 trace_qmp_block_job_dismiss(bjob);
4076 job = &bjob->job;
4077 job_dismiss(&job, errp);
John Snow75f71052018-03-10 03:27:36 -05004078 aio_context_release(aio_context);
4079}
4080
Jeff Codyfa40e652014-07-01 09:52:16 +02004081void qmp_change_backing_file(const char *device,
4082 const char *image_node_name,
4083 const char *backing_file,
4084 Error **errp)
4085{
4086 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004087 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02004088 BlockDriverState *image_bs = NULL;
4089 Error *local_err = NULL;
4090 bool ro;
4091 int open_flags;
4092 int ret;
4093
Kevin Wolf7b5dca32016-06-23 14:20:24 +02004094 bs = qmp_get_root_bs(device, errp);
4095 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02004096 return;
4097 }
4098
Kevin Wolf7b5dca32016-06-23 14:20:24 +02004099 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004100 aio_context_acquire(aio_context);
4101
Jeff Codyfa40e652014-07-01 09:52:16 +02004102 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
4103 if (local_err) {
4104 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004105 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004106 }
4107
4108 if (!image_bs) {
4109 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004110 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004111 }
4112
4113 if (bdrv_find_base(image_bs) == image_bs) {
4114 error_setg(errp, "not allowing backing file change on an image "
4115 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004116 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004117 }
4118
4119 /* even though we are not necessarily operating on bs, we need it to
4120 * determine if block ops are currently prohibited on the chain */
4121 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004122 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004123 }
4124
4125 /* final sanity check */
4126 if (!bdrv_chain_contains(bs, image_bs)) {
4127 error_setg(errp, "'%s' and image file are not in the same chain",
4128 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004129 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004130 }
4131
4132 /* if not r/w, reopen to make r/w */
4133 open_flags = image_bs->open_flags;
4134 ro = bdrv_is_read_only(image_bs);
4135
4136 if (ro) {
4137 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
4138 if (local_err) {
4139 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004140 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004141 }
4142 }
4143
4144 ret = bdrv_change_backing_file(image_bs, backing_file,
4145 image_bs->drv ? image_bs->drv->format_name : "");
4146
4147 if (ret < 0) {
4148 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
4149 backing_file);
4150 /* don't exit here, so we can try to restore open flags if
4151 * appropriate */
4152 }
4153
4154 if (ro) {
4155 bdrv_reopen(image_bs, open_flags, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03004156 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02004157 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004158
4159out:
4160 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02004161}
4162
Kevin Wolfabb21ac2016-02-23 17:33:24 +01004163void hmp_drive_add_node(Monitor *mon, const char *optstr)
4164{
4165 QemuOpts *opts;
4166 QDict *qdict;
4167 Error *local_err = NULL;
4168
4169 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
4170 if (!opts) {
4171 return;
4172 }
4173
4174 qdict = qemu_opts_to_qdict(opts, NULL);
4175
4176 if (!qdict_get_try_str(qdict, "node-name")) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004177 qobject_unref(qdict);
Kevin Wolfabb21ac2016-02-23 17:33:24 +01004178 error_report("'node-name' needs to be specified");
4179 goto out;
4180 }
4181
4182 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
4183 if (!bs) {
4184 error_report_err(local_err);
4185 goto out;
4186 }
4187
4188 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
4189
4190out:
4191 qemu_opts_del(opts);
4192}
4193
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004194void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
4195{
Max Reitzbe4b67b2015-10-19 17:53:09 +02004196 BlockDriverState *bs;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004197 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01004198 Visitor *v = qobject_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004199 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004200 Error *local_err = NULL;
4201
Eric Blake3b098d52016-06-09 10:48:43 -06004202 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01004203 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004204 error_propagate(errp, local_err);
4205 goto fail;
4206 }
4207
Eric Blake3b098d52016-06-09 10:48:43 -06004208 visit_complete(v, &obj);
Max Reitz7dc847e2018-02-24 16:40:29 +01004209 qdict = qobject_to(QDict, obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004210
4211 qdict_flatten(qdict);
4212
Kevin Wolf9ec88732016-09-21 14:56:11 +02004213 if (!qdict_get_try_str(qdict, "node-name")) {
4214 error_setg(errp, "'node-name' must be specified for the root node");
4215 goto fail;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004216 }
4217
Kevin Wolf9ec88732016-09-21 14:56:11 +02004218 bs = bds_tree_init(qdict, errp);
4219 if (!bs) {
4220 goto fail;
4221 }
4222
4223 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
4224
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004225fail:
Eric Blake3b098d52016-06-09 10:48:43 -06004226 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004227}
4228
Markus Armbruster79b7a772017-03-21 17:53:28 +01004229void qmp_blockdev_del(const char *node_name, Error **errp)
Alberto Garcia81b936a2015-11-02 16:51:55 +02004230{
4231 AioContext *aio_context;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004232 BlockDriverState *bs;
4233
Kevin Wolf9ec88732016-09-21 14:56:11 +02004234 bs = bdrv_find_node(node_name);
4235 if (!bs) {
4236 error_setg(errp, "Cannot find node %s", node_name);
Alberto Garcia81b936a2015-11-02 16:51:55 +02004237 return;
4238 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02004239 if (bdrv_has_blk(bs)) {
4240 error_setg(errp, "Node %s is in use", node_name);
4241 return;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004242 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02004243 aio_context = bdrv_get_aio_context(bs);
Alberto Garcia81b936a2015-11-02 16:51:55 +02004244 aio_context_acquire(aio_context);
4245
Kevin Wolf9ec88732016-09-21 14:56:11 +02004246 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
4247 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004248 }
4249
Kevin Wolf9ec88732016-09-21 14:56:11 +02004250 if (!bs->monitor_list.tqe_prev) {
4251 error_setg(errp, "Node %s is not owned by the monitor",
4252 bs->node_name);
4253 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004254 }
4255
Kevin Wolf9ec88732016-09-21 14:56:11 +02004256 if (bs->refcnt > 1) {
4257 error_setg(errp, "Block device %s is in use",
4258 bdrv_get_device_or_node_name(bs));
4259 goto out;
4260 }
4261
4262 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
4263 bdrv_unref(bs);
4264
Alberto Garcia81b936a2015-11-02 16:51:55 +02004265out:
4266 aio_context_release(aio_context);
4267}
4268
Wen Congyang7f821592016-05-10 15:36:39 +08004269static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
4270 const char *child_name)
4271{
4272 BdrvChild *child;
4273
4274 QLIST_FOREACH(child, &parent_bs->children, next) {
4275 if (strcmp(child->name, child_name) == 0) {
4276 return child;
4277 }
4278 }
4279
4280 return NULL;
4281}
4282
4283void qmp_x_blockdev_change(const char *parent, bool has_child,
4284 const char *child, bool has_node,
4285 const char *node, Error **errp)
4286{
4287 BlockDriverState *parent_bs, *new_bs = NULL;
4288 BdrvChild *p_child;
4289
4290 parent_bs = bdrv_lookup_bs(parent, parent, errp);
4291 if (!parent_bs) {
4292 return;
4293 }
4294
4295 if (has_child == has_node) {
4296 if (has_child) {
4297 error_setg(errp, "The parameters child and node are in conflict");
4298 } else {
4299 error_setg(errp, "Either child or node must be specified");
4300 }
4301 return;
4302 }
4303
4304 if (has_child) {
4305 p_child = bdrv_find_child(parent_bs, child);
4306 if (!p_child) {
4307 error_setg(errp, "Node '%s' does not have child '%s'",
4308 parent, child);
4309 return;
4310 }
4311 bdrv_del_child(parent_bs, p_child, errp);
4312 }
4313
4314 if (has_node) {
4315 new_bs = bdrv_find_node(node);
4316 if (!new_bs) {
4317 error_setg(errp, "Node '%s' not found", node);
4318 return;
4319 }
4320 bdrv_add_child(parent_bs, new_bs, errp);
4321 }
4322}
4323
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004324BlockJobInfoList *qmp_query_block_jobs(Error **errp)
4325{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004326 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004327 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004328
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004329 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
John Snow559b9352016-10-27 12:06:55 -04004330 BlockJobInfoList *elem;
4331 AioContext *aio_context;
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004332
John Snow559b9352016-10-27 12:06:55 -04004333 if (block_job_is_internal(job)) {
4334 continue;
4335 }
4336 elem = g_new0(BlockJobInfoList, 1);
4337 aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004338 aio_context_acquire(aio_context);
John Snow559b9352016-10-27 12:06:55 -04004339 elem->value = block_job_query(job, errp);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004340 aio_context_release(aio_context);
John Snow559b9352016-10-27 12:06:55 -04004341 if (!elem->value) {
4342 g_free(elem);
4343 qapi_free_BlockJobInfoList(head);
4344 return NULL;
4345 }
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004346 *p_next = elem;
4347 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004348 }
4349
4350 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004351}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004352
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004353void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
Stefan Hajnoczi882e9b82017-12-07 20:13:17 +00004354 bool has_force, bool force, Error **errp)
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004355{
4356 AioContext *old_context;
4357 AioContext *new_context;
4358 BlockDriverState *bs;
4359
4360 bs = bdrv_find_node(node_name);
4361 if (!bs) {
4362 error_setg(errp, "Cannot find node %s", node_name);
4363 return;
4364 }
4365
Stefan Hajnoczi882e9b82017-12-07 20:13:17 +00004366 /* Protects against accidents. */
4367 if (!(has_force && force) && bdrv_has_blk(bs)) {
4368 error_setg(errp, "Node %s is associated with a BlockBackend and could "
4369 "be in use (use force=true to override this check)",
4370 node_name);
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004371 return;
4372 }
4373
4374 if (iothread->type == QTYPE_QSTRING) {
4375 IOThread *obj = iothread_by_id(iothread->u.s);
4376 if (!obj) {
4377 error_setg(errp, "Cannot find iothread %s", iothread->u.s);
4378 return;
4379 }
4380
4381 new_context = iothread_get_aio_context(obj);
4382 } else {
4383 new_context = qemu_get_aio_context();
4384 }
4385
4386 old_context = bdrv_get_aio_context(bs);
4387 aio_context_acquire(old_context);
4388
4389 bdrv_set_aio_context(bs, new_context);
4390
4391 aio_context_release(old_context);
4392}
4393
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004394void qmp_x_block_latency_histogram_set(
4395 const char *device,
4396 bool has_boundaries, uint64List *boundaries,
4397 bool has_boundaries_read, uint64List *boundaries_read,
4398 bool has_boundaries_write, uint64List *boundaries_write,
4399 bool has_boundaries_flush, uint64List *boundaries_flush,
4400 Error **errp)
4401{
4402 BlockBackend *blk = blk_by_name(device);
4403 BlockAcctStats *stats;
4404
4405 if (!blk) {
4406 error_setg(errp, "Device '%s' not found", device);
4407 return;
4408 }
4409 stats = blk_get_stats(blk);
4410
4411 if (!has_boundaries && !has_boundaries_read && !has_boundaries_write &&
4412 !has_boundaries_flush)
4413 {
4414 block_latency_histograms_clear(stats);
4415 return;
4416 }
4417
4418 if (has_boundaries || has_boundaries_read) {
4419 block_latency_histogram_set(
4420 stats, BLOCK_ACCT_READ,
4421 has_boundaries_read ? boundaries_read : boundaries);
4422 }
4423
4424 if (has_boundaries || has_boundaries_write) {
4425 block_latency_histogram_set(
4426 stats, BLOCK_ACCT_WRITE,
4427 has_boundaries_write ? boundaries_write : boundaries);
4428 }
4429
4430 if (has_boundaries || has_boundaries_flush) {
4431 block_latency_histogram_set(
4432 stats, BLOCK_ACCT_FLUSH,
4433 has_boundaries_flush ? boundaries_flush : boundaries);
4434 }
4435}
4436
Kevin Wolf00063832013-03-15 10:35:07 +01004437QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004438 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01004439 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01004440 .desc = {
4441 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004442 .name = "snapshot",
4443 .type = QEMU_OPT_BOOL,
4444 .help = "enable/disable snapshot mode",
4445 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004446 .name = "aio",
4447 .type = QEMU_OPT_STRING,
4448 .help = "host AIO implementation (threads, native)",
4449 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01004450 .name = BDRV_OPT_CACHE_WB,
4451 .type = QEMU_OPT_BOOL,
4452 .help = "Enable writeback mode",
4453 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004454 .name = "format",
4455 .type = QEMU_OPT_STRING,
4456 .help = "disk format (raw, qcow2, ...)",
4457 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004458 .name = "rerror",
4459 .type = QEMU_OPT_STRING,
4460 .help = "read error action",
4461 },{
4462 .name = "werror",
4463 .type = QEMU_OPT_STRING,
4464 .help = "write error action",
4465 },{
Alberto Garcia4e200cf2016-09-15 17:53:05 +03004466 .name = BDRV_OPT_READ_ONLY,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004467 .type = QEMU_OPT_BOOL,
4468 .help = "open drive file as read-only",
Pradeep Jagadeesha2a78622017-02-28 10:31:46 +01004469 },
4470
4471 THROTTLE_OPTS,
4472
4473 {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004474 .name = "throttling.group",
4475 .type = QEMU_OPT_STRING,
4476 .help = "name of the block throttling group",
4477 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004478 .name = "copy-on-read",
4479 .type = QEMU_OPT_BOOL,
4480 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004481 },{
4482 .name = "detect-zeroes",
4483 .type = QEMU_OPT_STRING,
4484 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004485 },{
4486 .name = "stats-account-invalid",
4487 .type = QEMU_OPT_BOOL,
4488 .help = "whether to account for invalid I/O operations "
4489 "in the statistics",
4490 },{
4491 .name = "stats-account-failed",
4492 .type = QEMU_OPT_BOOL,
4493 .help = "whether to account for failed I/O operations "
4494 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004495 },
4496 { /* end of list */ }
4497 },
4498};
Kevin Wolf00063832013-03-15 10:35:07 +01004499
4500QemuOptsList qemu_drive_opts = {
4501 .name = "drive",
4502 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4503 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004504 /*
4505 * no elements => accept any params
4506 * validation will happen later
4507 */
Kevin Wolf00063832013-03-15 10:35:07 +01004508 { /* end of list */ }
4509 },
4510};