blob: 374189a426a4d26af250ae4ba6fa917725ae62be [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"
Max Reitz609f45e2018-06-14 21:14:28 +020038#include "block/qdict.h"
Alberto Garcia76f4afb2015-06-08 18:17:44 +020039#include "block/throttle-groups.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010040#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010041#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010042#include "qemu/option.h"
Markus Armbrustercdcd4362019-04-17 21:06:38 +020043#include "qemu/qemu-print.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010044#include "qemu/config-file.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010045#include "qapi/qapi-commands-block.h"
46#include "qapi/qapi-commands-transaction.h"
47#include "qapi/qapi-visit-block-core.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010048#include "qapi/qmp/qdict.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010049#include "qapi/qmp/qnum.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010050#include "qapi/qmp/qstring.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010051#include "qapi/error.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010052#include "qapi/qmp/qerror.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010053#include "qapi/qmp/qlist.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010054#include "qapi/qobject-output-visitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010055#include "sysemu/sysemu.h"
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +000056#include "sysemu/iothread.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010057#include "block/block_int.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000058#include "block/trace.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010059#include "sysemu/arch_init.h"
Thomas Huthc616f162017-05-12 12:33:49 +020060#include "sysemu/qtest.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020061#include "sysemu/runstate.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020062#include "qemu/cutils.h"
63#include "qemu/help_option.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020064#include "qemu/main-loop.h"
Pradeep Jagadeesha2a78622017-02-28 10:31:46 +010065#include "qemu/throttle-options.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020066
Max Reitz9c4218e2016-01-29 16:36:12 +010067static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
68 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
69
Kevin Wolfb33945c2016-09-20 13:38:43 +020070static int do_open_tray(const char *blk_name, const char *qdev_id,
71 bool force, Error **errp);
Max Reitz82fcf662017-11-10 23:43:01 +010072static void blockdev_remove_medium(bool has_device, const char *device,
73 bool has_id, const char *id, Error **errp);
74static void blockdev_insert_medium(bool has_device, const char *device,
75 bool has_id, const char *id,
76 const char *node_name, Error **errp);
Colin Lordbf18bee2016-06-06 14:15:22 -040077
Markus Armbruster19609662011-01-28 11:21:39 +010078static const char *const if_name[IF_COUNT] = {
79 [IF_NONE] = "none",
80 [IF_IDE] = "ide",
81 [IF_SCSI] = "scsi",
82 [IF_FLOPPY] = "floppy",
83 [IF_PFLASH] = "pflash",
84 [IF_MTD] = "mtd",
85 [IF_SD] = "sd",
86 [IF_VIRTIO] = "virtio",
87 [IF_XEN] = "xen",
88};
89
John Snow21dff8c2014-10-01 14:19:25 -040090static int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010091 /*
92 * Do not change these numbers! They govern how drive option
93 * index maps to unit and bus. That mapping is ABI.
94 *
Wei Jiangang547cb152016-04-26 18:12:43 +080095 * All controllers used to implement if=T drives need to support
Markus Armbruster27d6bf42011-01-28 11:21:40 +010096 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
97 * Otherwise, some index values map to "impossible" bus, unit
98 * values.
99 *
100 * For instance, if you change [IF_SCSI] to 255, -drive
101 * if=scsi,index=12 no longer means bus=1,unit=5, but
102 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
103 * the drive can't be set up. Regression.
104 */
105 [IF_IDE] = 2,
106 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +0100107};
108
John Snow21dff8c2014-10-01 14:19:25 -0400109/**
110 * Boards may call this to offer board-by-board overrides
111 * of the default, global values.
112 */
113void override_max_devs(BlockInterfaceType type, int max_devs)
114{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200115 BlockBackend *blk;
John Snow21dff8c2014-10-01 14:19:25 -0400116 DriveInfo *dinfo;
117
118 if (max_devs <= 0) {
119 return;
120 }
121
Markus Armbruster18e46a02014-10-07 13:59:06 +0200122 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
123 dinfo = blk_legacy_dinfo(blk);
John Snow21dff8c2014-10-01 14:19:25 -0400124 if (dinfo->type == type) {
125 fprintf(stderr, "Cannot override units-per-bus property of"
126 " the %s interface, because a drive of that type has"
127 " already been added.\n", if_name[type]);
128 g_assert_not_reached();
129 }
130 }
131
132 if_max_devs[type] = max_devs;
133}
134
Markus Armbruster14bafc52010-06-25 08:09:10 +0200135/*
136 * We automatically delete the drive when a device using it gets
137 * unplugged. Questionable feature, but we can't just drop it.
138 * Device models call blockdev_mark_auto_del() to schedule the
139 * automatic deletion, and generic qdev code calls blockdev_auto_del()
140 * when deletion is actually safe.
141 */
Markus Armbruster4be74632014-10-07 13:59:18 +0200142void blockdev_mark_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200143{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200144 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Vladimir Sementsov-Ogievskiy81641022019-06-06 18:41:31 +0300145 BlockJob *job;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200146
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200147 if (!dinfo) {
Kevin Wolf2d246f02013-09-18 15:14:47 +0200148 return;
149 }
150
Vladimir Sementsov-Ogievskiy81641022019-06-06 18:41:31 +0300151 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
152 if (block_job_has_bdrv(job, blk_bs(blk))) {
153 AioContext *aio_context = job->job.aio_context;
154 aio_context_acquire(aio_context);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100155
Vladimir Sementsov-Ogievskiy81641022019-06-06 18:41:31 +0300156 job_cancel(&job->job, false);
157
158 aio_context_release(aio_context);
Max Reitz5433c242015-10-19 17:53:29 +0200159 }
Paolo Bonzini12bde0e2012-03-30 13:17:10 +0200160 }
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100161
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200162 dinfo->auto_del = 1;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200163}
164
Markus Armbruster4be74632014-10-07 13:59:18 +0200165void blockdev_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200166{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200167 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200168
Ryan Harper0fc0f1f2010-12-08 10:05:00 -0600169 if (dinfo && dinfo->auto_del) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100170 monitor_remove_blk(blk);
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +0200171 blk_unref(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200172 }
173}
174
John Snowd8f94e12014-10-01 14:19:27 -0400175/**
176 * Returns the current mapping of how many units per bus
177 * a particular interface can support.
178 *
179 * A positive integer indicates n units per bus.
180 * 0 implies the mapping has not been established.
181 * -1 indicates an invalid BlockInterfaceType was given.
182 */
183int drive_get_max_devs(BlockInterfaceType type)
184{
185 if (type >= IF_IDE && type < IF_COUNT) {
186 return if_max_devs[type];
187 }
188
189 return -1;
190}
191
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100192static int drive_index_to_bus_id(BlockInterfaceType type, int index)
193{
194 int max_devs = if_max_devs[type];
195 return max_devs ? index / max_devs : 0;
196}
197
198static int drive_index_to_unit_id(BlockInterfaceType type, int index)
199{
200 int max_devs = if_max_devs[type];
201 return max_devs ? index % max_devs : index;
202}
203
Markus Armbruster2292dda2011-01-28 11:21:41 +0100204QemuOpts *drive_def(const char *optstr)
205{
Markus Armbruster70b94332015-02-13 12:50:26 +0100206 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100207}
208
209QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +0100210 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +0200211{
Markus Armbruster666daa62010-06-02 18:48:27 +0200212 QemuOpts *opts;
213
Markus Armbruster2292dda2011-01-28 11:21:41 +0100214 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200215 if (!opts) {
216 return NULL;
217 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100218 if (type != IF_DEFAULT) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100219 qemu_opt_set(opts, "if", if_name[type], &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100220 }
221 if (index >= 0) {
Markus Armbrustera8b18f82015-02-13 15:50:43 +0100222 qemu_opt_set_number(opts, "index", index, &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100223 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200224 if (file)
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100225 qemu_opt_set(opts, "file", file, &error_abort);
Markus Armbruster666daa62010-06-02 18:48:27 +0200226 return opts;
227}
228
229DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
230{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200231 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200232 DriveInfo *dinfo;
233
Markus Armbruster18e46a02014-10-07 13:59:06 +0200234 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
235 dinfo = blk_legacy_dinfo(blk);
236 if (dinfo && dinfo->type == type
237 && dinfo->bus == bus && dinfo->unit == unit) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200238 return dinfo;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200239 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200240 }
241
242 return NULL;
243}
244
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100245void drive_check_orphaned(void)
John Snowa66c9dc2014-10-01 14:19:24 -0400246{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200247 BlockBackend *blk;
John Snowa66c9dc2014-10-01 14:19:24 -0400248 DriveInfo *dinfo;
Markus Armbruster664cc622017-02-15 11:05:45 +0100249 Location loc;
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100250 bool orphans = false;
John Snowa66c9dc2014-10-01 14:19:24 -0400251
Markus Armbruster18e46a02014-10-07 13:59:06 +0200252 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
253 dinfo = blk_legacy_dinfo(blk);
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200254 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
John Snowa66c9dc2014-10-01 14:19:24 -0400255 dinfo->type != IF_NONE) {
Markus Armbruster664cc622017-02-15 11:05:45 +0100256 loc_push_none(&loc);
257 qemu_opts_loc_restore(dinfo->opts);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100258 error_report("machine type does not support"
Markus Armbruster664cc622017-02-15 11:05:45 +0100259 " if=%s,bus=%d,unit=%d",
260 if_name[dinfo->type], dinfo->bus, dinfo->unit);
261 loc_pop(&loc);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100262 orphans = true;
John Snowa66c9dc2014-10-01 14:19:24 -0400263 }
264 }
265
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100266 if (orphans) {
267 exit(1);
268 }
John Snowa66c9dc2014-10-01 14:19:24 -0400269}
270
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100271DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
272{
273 return drive_get(type,
274 drive_index_to_bus_id(type, index),
275 drive_index_to_unit_id(type, index));
276}
277
Markus Armbruster666daa62010-06-02 18:48:27 +0200278int drive_get_max_bus(BlockInterfaceType type)
279{
280 int max_bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200281 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200282 DriveInfo *dinfo;
283
284 max_bus = -1;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200285 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
286 dinfo = blk_legacy_dinfo(blk);
287 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200288 max_bus = dinfo->bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200289 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200290 }
291 return max_bus;
292}
293
Markus Armbruster13839972011-01-28 11:21:37 +0100294/* Get a block device. This should only be used for single-drive devices
295 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
296 appropriate bus. */
297DriveInfo *drive_get_next(BlockInterfaceType type)
298{
299 static int next_block_unit[IF_COUNT];
300
301 return drive_get(type, 0, next_block_unit[type]++);
302}
303
Markus Armbruster666daa62010-06-02 18:48:27 +0200304static void bdrv_format_print(void *opaque, const char *name)
305{
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200306 qemu_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200307}
308
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000309typedef struct {
310 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800311 BlockDriverState *bs;
312} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000313
Kevin Wolfb6810722013-09-20 11:33:11 +0200314static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200315{
316 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200317 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200318 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200319 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200320 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200321 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200322 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200323 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200324 } else {
Kevin Wolfb6810722013-09-20 11:33:11 +0200325 error_setg(errp, "'%s' invalid %s error action",
326 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200327 return -1;
328 }
329}
330
Alberto Garcia40119ef2015-11-16 11:28:38 +0200331static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
332 Error **errp)
333{
334 const QListEntry *entry;
335 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
336 switch (qobject_type(entry->value)) {
337
338 case QTYPE_QSTRING: {
339 unsigned long long length;
Max Reitz7dc847e2018-02-24 16:40:29 +0100340 const char *str = qstring_get_str(qobject_to(QString,
341 entry->value));
Alberto Garcia40119ef2015-11-16 11:28:38 +0200342 if (parse_uint_full(str, &length, 10) == 0 &&
343 length > 0 && length <= UINT_MAX) {
344 block_acct_add_interval(stats, (unsigned) length);
345 } else {
346 error_setg(errp, "Invalid interval length: %s", str);
347 return false;
348 }
349 break;
350 }
351
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400352 case QTYPE_QNUM: {
Max Reitz7dc847e2018-02-24 16:40:29 +0100353 int64_t length = qnum_get_int(qobject_to(QNum, entry->value));
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400354
Alberto Garcia40119ef2015-11-16 11:28:38 +0200355 if (length > 0 && length <= UINT_MAX) {
356 block_acct_add_interval(stats, (unsigned) length);
357 } else {
358 error_setg(errp, "Invalid interval length: %" PRId64, length);
359 return false;
360 }
361 break;
362 }
363
364 default:
365 error_setg(errp, "The specification of stats-intervals is invalid");
366 return false;
367 }
368 }
369 return true;
370}
371
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200372typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
373
Max Reitzfbf81752015-10-19 17:53:31 +0200374/* All parameters but @opts are optional and may be set to NULL. */
375static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
376 const char **throttling_group, ThrottleConfig *throttle_cfg,
377 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
378{
Max Reitzfbf81752015-10-19 17:53:31 +0200379 Error *local_error = NULL;
380 const char *aio;
381
382 if (bdrv_flags) {
Max Reitzfbf81752015-10-19 17:53:31 +0200383 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
384 *bdrv_flags |= BDRV_O_COPY_ON_READ;
385 }
386
Max Reitzfbf81752015-10-19 17:53:31 +0200387 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
Aarushi Mehtaf80f2672020-01-20 14:18:50 +0000388 if (bdrv_parse_aio(aio, bdrv_flags) < 0) {
389 error_setg(errp, "invalid aio option");
390 return;
Max Reitzfbf81752015-10-19 17:53:31 +0200391 }
392 }
393 }
394
395 /* disk I/O throttling */
396 if (throttling_group) {
397 *throttling_group = qemu_opt_get(opts, "throttling.group");
398 }
399
400 if (throttle_cfg) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200401 throttle_config_init(throttle_cfg);
Max Reitzfbf81752015-10-19 17:53:31 +0200402 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
403 qemu_opt_get_number(opts, "throttling.bps-total", 0);
404 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
405 qemu_opt_get_number(opts, "throttling.bps-read", 0);
406 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
407 qemu_opt_get_number(opts, "throttling.bps-write", 0);
408 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
409 qemu_opt_get_number(opts, "throttling.iops-total", 0);
410 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
411 qemu_opt_get_number(opts, "throttling.iops-read", 0);
412 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
413 qemu_opt_get_number(opts, "throttling.iops-write", 0);
414
415 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
416 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
417 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
418 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
419 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
420 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
421 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
422 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
423 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
424 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
425 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
426 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
427
Alberto Garcia8a0fc182016-02-18 12:27:02 +0200428 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
429 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
430 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
431 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
432 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
433 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
434 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
435 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
436 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
437 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
438 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
439 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
440
Max Reitzfbf81752015-10-19 17:53:31 +0200441 throttle_cfg->op_size =
442 qemu_opt_get_number(opts, "throttling.iops-size", 0);
443
Alberto Garciad5851082016-02-18 12:26:59 +0200444 if (!throttle_is_valid(throttle_cfg, errp)) {
Max Reitzfbf81752015-10-19 17:53:31 +0200445 return;
446 }
447 }
448
449 if (detect_zeroes) {
450 *detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200451 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Max Reitzfbf81752015-10-19 17:53:31 +0200452 qemu_opt_get(opts, "detect-zeroes"),
Max Reitzfbf81752015-10-19 17:53:31 +0200453 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
454 &local_error);
455 if (local_error) {
456 error_propagate(errp, local_error);
457 return;
458 }
Max Reitzfbf81752015-10-19 17:53:31 +0200459 }
460}
461
Kevin Wolff298d072013-09-10 12:01:20 +0200462/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200463static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
464 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200465{
466 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200467 int bdrv_flags = 0;
468 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200469 bool account_invalid, account_failed;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300470 bool writethrough, read_only;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200471 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200472 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200473 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200474 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100475 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100476 QemuOpts *opts;
Alberto Garcia40119ef2015-11-16 11:28:38 +0200477 QDict *interval_dict = NULL;
478 QList *interval_list = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100479 const char *id;
Max Reitzfbf81752015-10-19 17:53:31 +0200480 BlockdevDetectZeroesOptions detect_zeroes =
481 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
482 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200483
Kevin Wolff298d072013-09-10 12:01:20 +0200484 /* Check common options by copying from bs_opts to opts, all other options
485 * stay in bs_opts for processing by bdrv_open(). */
486 id = qdict_get_try_str(bs_opts, "id");
Kevin Wolf00063832013-03-15 10:35:07 +0100487 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100488 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200489 error_propagate(errp, error);
Markus Armbruster6376f952014-05-28 11:17:01 +0200490 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100491 }
492
Kevin Wolf00063832013-03-15 10:35:07 +0100493 qemu_opts_absorb_qdict(opts, bs_opts, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100494 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200495 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100496 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100497 }
498
499 if (id) {
500 qdict_del(bs_opts, "id");
501 }
502
Markus Armbruster666daa62010-06-02 18:48:27 +0200503 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200504 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200505
Alberto Garcia362e9292015-10-28 17:33:04 +0200506 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
507 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
508
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100509 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
510
Alberto Garciaff356ee2016-07-08 17:03:00 +0300511 id = qemu_opts_id(opts);
512
Alberto Garcia40119ef2015-11-16 11:28:38 +0200513 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
514 qdict_array_split(interval_dict, &interval_list);
515
516 if (qdict_size(interval_dict) != 0) {
517 error_setg(errp, "Invalid option stats-intervals.%s",
518 qdict_first(interval_dict)->key);
519 goto early_err;
520 }
Alberto Garcia2be55062015-10-28 17:33:07 +0200521
Max Reitzfbf81752015-10-19 17:53:31 +0200522 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
523 &detect_zeroes, &error);
524 if (error) {
525 error_propagate(errp, error);
526 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200527 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200528
529 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100530 if (is_help_option(buf)) {
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200531 qemu_printf("Supported formats:");
Andrey Shinkevich9ac404c2019-03-07 16:33:58 +0300532 bdrv_iterate_format(bdrv_format_print, NULL, false);
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200533 qemu_printf("\nSupported formats (read-only):");
Andrey Shinkevich9ac404c2019-03-07 16:33:58 +0300534 bdrv_iterate_format(bdrv_format_print, NULL, true);
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200535 qemu_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100536 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200537 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200538
Max Reitze4342ce2015-02-05 13:58:14 -0500539 if (qdict_haskey(bs_opts, "driver")) {
540 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100541 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400542 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500543 qdict_put_str(bs_opts, "driver", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200544 }
545
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200546 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200547 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200548 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100549 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200550 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100551 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200552 }
553 }
554
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200555 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200556 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200557 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100558 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200559 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100560 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200561 }
562 }
563
Max Reitz5ec18f82015-10-19 17:53:30 +0200564 if (snapshot) {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200565 bdrv_flags |= BDRV_O_SNAPSHOT;
Max Reitz5ec18f82015-10-19 17:53:30 +0200566 }
567
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300568 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
569
Kevin Wolf326642b2013-07-11 12:52:34 +0200570 /* init */
Kevin Wolf39c4ae92015-11-13 14:45:42 +0100571 if ((!file || !*file) && !qdict_size(bs_opts)) {
Max Reitz5ec18f82015-10-19 17:53:30 +0200572 BlockBackendRootState *blk_rs;
573
Kevin Wolfd861ab32019-04-25 14:25:10 +0200574 blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
Max Reitz5ec18f82015-10-19 17:53:30 +0200575 blk_rs = blk_get_root_state(blk);
576 blk_rs->open_flags = bdrv_flags;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300577 blk_rs->read_only = read_only;
Max Reitz5ec18f82015-10-19 17:53:30 +0200578 blk_rs->detect_zeroes = detect_zeroes;
579
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200580 qobject_unref(bs_opts);
Max Reitze4342ce2015-02-05 13:58:14 -0500581 } else {
582 if (file && !*file) {
583 file = NULL;
584 }
585
Kevin Wolf91a097e2015-05-08 17:49:53 +0200586 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
587 * with other callers) rather than what we want as the real defaults.
588 * Apply the defaults here instead. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200589 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
590 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300591 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
592 read_only ? "on" : "off");
Kevin Wolf9384a442018-10-17 18:24:30 +0200593 qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on");
Kevin Wolf72e775c2016-03-15 14:34:37 +0100594 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200595
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100596 if (runstate_check(RUN_STATE_INMIGRATE)) {
597 bdrv_flags |= BDRV_O_INACTIVE;
598 }
599
Max Reitzefaa7c42016-03-16 19:54:38 +0100600 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500601 if (!blk) {
602 goto err_no_bs_opts;
603 }
604 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500605
Max Reitz5ec18f82015-10-19 17:53:30 +0200606 bs->detect_zeroes = detect_zeroes;
607
Paolo Bonzini9caa6f32017-06-05 14:39:07 +0200608 block_acct_setup(blk_get_stats(blk), account_invalid, account_failed);
Alberto Garcia2be55062015-10-28 17:33:07 +0200609
Alberto Garcia40119ef2015-11-16 11:28:38 +0200610 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
611 blk_unref(blk);
612 blk = NULL;
613 goto err_no_bs_opts;
Alberto Garcia2be55062015-10-28 17:33:07 +0200614 }
Max Reitz5ec18f82015-10-19 17:53:30 +0200615 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200616
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100617 /* disk I/O throttling */
618 if (throttle_enabled(&cfg)) {
619 if (!throttling_group) {
Alberto Garciaff356ee2016-07-08 17:03:00 +0300620 throttling_group = id;
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100621 }
622 blk_io_limits_enable(blk, throttling_group);
623 blk_set_io_limits(blk, &cfg);
624 }
625
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100626 blk_set_enable_write_cache(blk, !writethrough);
Max Reitz373340b2015-10-19 17:53:22 +0200627 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200628
Alberto Garciaff356ee2016-07-08 17:03:00 +0300629 if (!monitor_add_blk(blk, id, errp)) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100630 blk_unref(blk);
631 blk = NULL;
632 goto err_no_bs_opts;
633 }
634
Max Reitze4342ce2015-02-05 13:58:14 -0500635err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100636 qemu_opts_del(opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200637 qobject_unref(interval_dict);
638 qobject_unref(interval_list);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200639 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100640
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100641early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100642 qemu_opts_del(opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200643 qobject_unref(interval_dict);
644 qobject_unref(interval_list);
Markus Armbruster6376f952014-05-28 11:17:01 +0200645err_no_opts:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200646 qobject_unref(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100647 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200648}
649
Max Reitzbd745e22015-10-19 17:53:32 +0200650/* Takes the ownership of bs_opts */
651static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
652{
Max Reitzbd745e22015-10-19 17:53:32 +0200653 int bdrv_flags = 0;
654
Kevin Wolfa81d6162016-03-07 14:23:04 +0100655 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
656 * with other callers) rather than what we want as the real defaults.
657 * Apply the defaults here instead. */
Kevin Wolfa81d6162016-03-07 14:23:04 +0100658 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
659 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300660 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
Kevin Wolfa81d6162016-03-07 14:23:04 +0100661
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100662 if (runstate_check(RUN_STATE_INMIGRATE)) {
663 bdrv_flags |= BDRV_O_INACTIVE;
664 }
665
Kevin Wolf74e1ae72016-09-20 20:48:52 +0200666 return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
Max Reitzbd745e22015-10-19 17:53:32 +0200667}
668
Max Reitz9c4218e2016-01-29 16:36:12 +0100669void blockdev_close_all_bdrv_states(void)
670{
671 BlockDriverState *bs, *next_bs;
672
673 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
674 AioContext *ctx = bdrv_get_aio_context(bs);
675
676 aio_context_acquire(ctx);
677 bdrv_unref(bs);
678 aio_context_release(ctx);
679 }
680}
681
Max Reitz262b4e82016-03-16 19:54:41 +0100682/* Iterates over the list of monitor-owned BlockDriverStates */
683BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
684{
685 return bs ? QTAILQ_NEXT(bs, monitor_list)
686 : QTAILQ_FIRST(&monitor_bdrv_states);
687}
688
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200689static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
690 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200691{
692 const char *value;
693
694 value = qemu_opt_get(opts, from);
695 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200696 if (qemu_opt_find(opts, to)) {
697 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
698 "same time", to, from);
699 return;
700 }
Jun Li20d6cd42014-09-24 13:45:27 +0800701 }
702
703 /* rename all items in opts */
704 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100705 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200706 qemu_opt_unset(opts, from);
707 }
708}
709
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200710QemuOptsList qemu_legacy_drive_opts = {
711 .name = "drive",
712 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
713 .desc = {
714 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200715 .name = "bus",
716 .type = QEMU_OPT_NUMBER,
717 .help = "bus number",
718 },{
719 .name = "unit",
720 .type = QEMU_OPT_NUMBER,
721 .help = "unit number (i.e. lun for scsi)",
722 },{
723 .name = "index",
724 .type = QEMU_OPT_NUMBER,
725 .help = "index number",
726 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200727 .name = "media",
728 .type = QEMU_OPT_STRING,
729 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200730 },{
731 .name = "if",
732 .type = QEMU_OPT_STRING,
733 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200734 },{
Max Reitzd095b462013-12-20 19:28:14 +0100735 .name = "file",
736 .type = QEMU_OPT_STRING,
737 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200738 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200739
740 /* Options that are passed on, but have special semantics with -drive */
741 {
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300742 .name = BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200743 .type = QEMU_OPT_BOOL,
744 .help = "open drive file as read-only",
745 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100746 .name = "rerror",
747 .type = QEMU_OPT_STRING,
748 .help = "read error action",
749 },{
750 .name = "werror",
751 .type = QEMU_OPT_STRING,
752 .help = "write error action",
753 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200754 .name = "copy-on-read",
755 .type = QEMU_OPT_BOOL,
756 .help = "copy read data from backing file into image file",
757 },
758
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200759 { /* end of list */ }
760 },
761};
762
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200763DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,
764 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200765{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200766 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200767 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200768 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200769 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200770 QemuOpts *legacy_opts;
771 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200772 BlockInterfaceType type;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200773 int max_devs, bus_id, unit_id, index;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100774 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800775 bool read_only = false;
776 bool copy_on_read;
Max Reitzd095b462013-12-20 19:28:14 +0100777 const char *filename;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200778 Error *local_err = NULL;
Kevin Wolf247147f2014-09-24 16:37:14 +0200779 int i;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200780
Kevin Wolf57975222013-07-17 14:41:54 +0200781 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200782 static const struct {
783 const char *from;
784 const char *to;
785 } opt_renames[] = {
786 { "iops", "throttling.iops-total" },
787 { "iops_rd", "throttling.iops-read" },
788 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200789
Kevin Wolf247147f2014-09-24 16:37:14 +0200790 { "bps", "throttling.bps-total" },
791 { "bps_rd", "throttling.bps-read" },
792 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200793
Kevin Wolf247147f2014-09-24 16:37:14 +0200794 { "iops_max", "throttling.iops-total-max" },
795 { "iops_rd_max", "throttling.iops-read-max" },
796 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200797
Kevin Wolf247147f2014-09-24 16:37:14 +0200798 { "bps_max", "throttling.bps-total-max" },
799 { "bps_rd_max", "throttling.bps-read-max" },
800 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200801
Kevin Wolf247147f2014-09-24 16:37:14 +0200802 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200803
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200804 { "group", "throttling.group" },
805
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300806 { "readonly", BDRV_OPT_READ_ONLY },
Kevin Wolf247147f2014-09-24 16:37:14 +0200807 };
808
809 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200810 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
811 &local_err);
812 if (local_err) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200813 error_propagate(errp, local_err);
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200814 return NULL;
815 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200816 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200817
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200818 value = qemu_opt_get(all_opts, "cache");
819 if (value) {
820 int flags = 0;
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100821 bool writethrough;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200822
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100823 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200824 error_setg(errp, "invalid cache option");
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200825 return NULL;
826 }
827
828 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200829 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
830 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100831 !writethrough, &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200832 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200833 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
834 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100835 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200836 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200837 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
838 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100839 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200840 }
841 qemu_opt_unset(all_opts, "cache");
842 }
843
Kevin Wolff298d072013-09-10 12:01:20 +0200844 /* Get a QDict for processing the options */
845 bs_opts = qdict_new();
846 qemu_opts_to_qdict(all_opts, bs_opts);
847
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800848 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
849 &error_abort);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200850 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100851 if (local_err) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200852 error_propagate(errp, local_err);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200853 goto fail;
854 }
855
856 /* Media type */
857 value = qemu_opt_get(legacy_opts, "media");
858 if (value) {
859 if (!strcmp(value, "disk")) {
860 media = MEDIA_DISK;
861 } else if (!strcmp(value, "cdrom")) {
862 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800863 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200864 } else {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200865 error_setg(errp, "'%s' invalid media", value);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200866 goto fail;
867 }
868 }
869
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200870 /* copy-on-read is disabled with a warning for read-only devices */
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300871 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200872 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
873
874 if (read_only && copy_on_read) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700875 warn_report("disabling copy-on-read on read-only drive");
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200876 copy_on_read = false;
877 }
878
Eric Blake46f5ac22017-04-27 16:58:17 -0500879 qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off");
880 qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off");
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200881
Kevin Wolf593d4642013-08-28 17:24:51 +0200882 /* Controller type */
883 value = qemu_opt_get(legacy_opts, "if");
884 if (value) {
885 for (type = 0;
886 type < IF_COUNT && strcmp(value, if_name[type]);
887 type++) {
888 }
889 if (type == IF_COUNT) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200890 error_setg(errp, "unsupported bus type '%s'", value);
Kevin Wolf593d4642013-08-28 17:24:51 +0200891 goto fail;
892 }
893 } else {
894 type = block_default_type;
895 }
896
Kevin Wolf87a899c2013-09-10 15:48:13 +0200897 /* Device address specified by bus/unit or index.
898 * If none was specified, try to find the first free one. */
899 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
900 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
901 index = qemu_opt_get_number(legacy_opts, "index", -1);
902
903 max_devs = if_max_devs[type];
904
905 if (index != -1) {
906 if (bus_id != 0 || unit_id != -1) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200907 error_setg(errp, "index cannot be used with bus and unit");
Kevin Wolf87a899c2013-09-10 15:48:13 +0200908 goto fail;
909 }
910 bus_id = drive_index_to_bus_id(type, index);
911 unit_id = drive_index_to_unit_id(type, index);
912 }
913
914 if (unit_id == -1) {
915 unit_id = 0;
916 while (drive_get(type, bus_id, unit_id) != NULL) {
917 unit_id++;
918 if (max_devs && unit_id >= max_devs) {
919 unit_id -= max_devs;
920 bus_id++;
921 }
922 }
923 }
924
925 if (max_devs && unit_id >= max_devs) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200926 error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1);
Kevin Wolf87a899c2013-09-10 15:48:13 +0200927 goto fail;
928 }
929
930 if (drive_get(type, bus_id, unit_id) != NULL) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200931 error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists",
932 bus_id, unit_id, index);
Kevin Wolf87a899c2013-09-10 15:48:13 +0200933 goto fail;
934 }
935
936 /* no id supplied -> create one */
937 if (qemu_opts_id(all_opts) == NULL) {
938 char *new_id;
939 const char *mediastr = "";
940 if (type == IF_IDE || type == IF_SCSI) {
941 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
942 }
943 if (max_devs) {
944 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
945 mediastr, unit_id);
946 } else {
947 new_id = g_strdup_printf("%s%s%i", if_name[type],
948 mediastr, unit_id);
949 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500950 qdict_put_str(bs_opts, "id", new_id);
Kevin Wolf87a899c2013-09-10 15:48:13 +0200951 g_free(new_id);
952 }
953
Kevin Wolf394c7d42013-09-13 14:09:17 +0200954 /* Add virtio block device */
Kevin Wolf394c7d42013-09-13 14:09:17 +0200955 if (type == IF_VIRTIO) {
956 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800957 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
958 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200959 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +0200960 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200961 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100962 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200963 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100964 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
965 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200966 }
967
Max Reitzd095b462013-12-20 19:28:14 +0100968 filename = qemu_opt_get(legacy_opts, "file");
969
Kevin Wolfee13ed12014-02-09 09:52:32 +0100970 /* Check werror/rerror compatibility with if=... */
971 werror = qemu_opt_get(legacy_opts, "werror");
972 if (werror != NULL) {
973 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
974 type != IF_NONE) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200975 error_setg(errp, "werror is not supported by this bus type");
Kevin Wolfee13ed12014-02-09 09:52:32 +0100976 goto fail;
977 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500978 qdict_put_str(bs_opts, "werror", werror);
Kevin Wolfee13ed12014-02-09 09:52:32 +0100979 }
980
981 rerror = qemu_opt_get(legacy_opts, "rerror");
982 if (rerror != NULL) {
983 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
984 type != IF_NONE) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200985 error_setg(errp, "rerror is not supported by this bus type");
Kevin Wolfee13ed12014-02-09 09:52:32 +0100986 goto fail;
987 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500988 qdict_put_str(bs_opts, "rerror", rerror);
Kevin Wolfee13ed12014-02-09 09:52:32 +0100989 }
990
Kevin Wolf2d246f02013-09-18 15:14:47 +0200991 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200992 blk = blockdev_init(filename, bs_opts, &local_err);
Markus Armbruster3cb0e252014-05-28 11:17:02 +0200993 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200994 if (!blk) {
Markus Armbrusterb2322002018-12-13 18:31:13 +0100995 error_propagate(errp, local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +0200996 goto fail;
Kevin Wolfb6810722013-09-20 11:33:11 +0200997 } else {
Markus Armbruster84d18f02014-01-30 15:07:28 +0100998 assert(!local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +0200999 }
1000
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001001 /* Create legacy DriveInfo */
1002 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001003 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001004
Kevin Wolfee13ed12014-02-09 09:52:32 +01001005 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001006 dinfo->bus = bus_id;
1007 dinfo->unit = unit_id;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001008
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001009 blk_set_legacy_dinfo(blk, dinfo);
1010
Kevin Wolfe34ef042013-09-19 14:24:10 +02001011 switch(type) {
1012 case IF_IDE:
1013 case IF_SCSI:
1014 case IF_XEN:
1015 case IF_NONE:
1016 dinfo->media_cd = media == MEDIA_CDROM;
1017 break;
1018 default:
1019 break;
1020 }
1021
Kevin Wolf2d246f02013-09-18 15:14:47 +02001022fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001023 qemu_opts_del(legacy_opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001024 qobject_unref(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001025 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001026}
1027
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02001028static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1029{
1030 BlockDriverState *bs;
1031
1032 bs = bdrv_lookup_bs(name, name, errp);
1033 if (bs == NULL) {
1034 return NULL;
1035 }
1036
1037 if (!bdrv_is_root_node(bs)) {
1038 error_setg(errp, "Need a root block node");
1039 return NULL;
1040 }
1041
1042 if (!bdrv_is_inserted(bs)) {
1043 error_setg(errp, "Device has no medium");
1044 return NULL;
1045 }
1046
1047 return bs;
1048}
1049
Kevin Wolfb33945c2016-09-20 13:38:43 +02001050static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id,
1051 Error **errp)
1052{
1053 BlockBackend *blk;
1054
1055 if (!blk_name == !qdev_id) {
1056 error_setg(errp, "Need exactly one of 'device' and 'id'");
1057 return NULL;
1058 }
1059
1060 if (qdev_id) {
1061 blk = blk_by_qdev_id(qdev_id, errp);
1062 } else {
1063 blk = blk_by_name(blk_name);
1064 if (blk == NULL) {
1065 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1066 "Device '%s' not found", blk_name);
1067 }
1068 }
1069
1070 return blk;
1071}
1072
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001073void hmp_commit(Monitor *mon, const QDict *qdict)
Markus Armbruster666daa62010-06-02 18:48:27 +02001074{
Markus Armbruster666daa62010-06-02 18:48:27 +02001075 const char *device = qdict_get_str(qdict, "device");
Fam Zhenga0e85442015-03-02 19:36:48 +08001076 BlockBackend *blk;
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001077 int ret;
Markus Armbruster666daa62010-06-02 18:48:27 +02001078
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001079 if (!strcmp(device, "all")) {
Max Reitzda31d592016-03-16 19:54:32 +01001080 ret = blk_commit_all();
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001081 } else {
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001082 BlockDriverState *bs;
1083 AioContext *aio_context;
1084
Fam Zhenga0e85442015-03-02 19:36:48 +08001085 blk = blk_by_name(device);
1086 if (!blk) {
Kevin Wolf46741112019-05-27 18:01:41 +02001087 error_report("Device '%s' not found", device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +02001088 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001089 }
Max Reitz5433c242015-10-19 17:53:29 +02001090 if (!blk_is_available(blk)) {
Kevin Wolf46741112019-05-27 18:01:41 +02001091 error_report("Device '%s' has no medium", device);
Max Reitz5433c242015-10-19 17:53:29 +02001092 return;
1093 }
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001094
1095 bs = blk_bs(blk);
1096 aio_context = bdrv_get_aio_context(bs);
1097 aio_context_acquire(aio_context);
1098
1099 ret = bdrv_commit(bs);
1100
1101 aio_context_release(aio_context);
Jeff Cody58513bd2013-01-18 12:45:35 -05001102 }
1103 if (ret < 0) {
Kevin Wolf46741112019-05-27 18:01:41 +02001104 error_report("'commit' error for '%s': %s", device, strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +02001105 }
1106}
1107
Eric Blake10f75902016-03-03 09:16:50 -07001108static void blockdev_do_action(TransactionAction *action, Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001109{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001110 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001111
Eric Blake10f75902016-03-03 09:16:50 -07001112 list.value = action;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001113 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001114 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001115}
1116
Benoît Canet0901f672014-01-23 21:31:38 +01001117void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1118 bool has_node_name, const char *node_name,
1119 const char *snapshot_file,
1120 bool has_snapshot_node_name,
1121 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001122 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001123 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001124{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001125 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001126 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001127 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001128 .has_node_name = has_node_name,
1129 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001130 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001131 .has_snapshot_node_name = has_snapshot_node_name,
1132 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001133 .has_format = has_format,
1134 .format = (char *) format,
1135 .has_mode = has_mode,
1136 .mode = mode,
1137 };
Eric Blake10f75902016-03-03 09:16:50 -07001138 TransactionAction action = {
1139 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001140 .u.blockdev_snapshot_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001141 };
1142 blockdev_do_action(&action, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001143}
1144
Alberto Garcia43de7e22015-10-26 14:27:16 +02001145void qmp_blockdev_snapshot(const char *node, const char *overlay,
1146 Error **errp)
1147{
1148 BlockdevSnapshot snapshot_data = {
1149 .node = (char *) node,
1150 .overlay = (char *) overlay
1151 };
Eric Blake10f75902016-03-03 09:16:50 -07001152 TransactionAction action = {
1153 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
Eric Blake32bafa82016-03-17 16:48:37 -06001154 .u.blockdev_snapshot.data = &snapshot_data,
Eric Blake10f75902016-03-03 09:16:50 -07001155 };
1156 blockdev_do_action(&action, errp);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001157}
1158
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001159void qmp_blockdev_snapshot_internal_sync(const char *device,
1160 const char *name,
1161 Error **errp)
1162{
1163 BlockdevSnapshotInternal snapshot = {
1164 .device = (char *) device,
1165 .name = (char *) name
1166 };
Eric Blake10f75902016-03-03 09:16:50 -07001167 TransactionAction action = {
1168 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001169 .u.blockdev_snapshot_internal_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001170 };
1171 blockdev_do_action(&action, errp);
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001172}
1173
Wenchao Xia44e3e052013-09-11 14:04:36 +08001174SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1175 bool has_id,
1176 const char *id,
1177 bool has_name,
1178 const char *name,
1179 Error **errp)
1180{
Fam Zhenga0e85442015-03-02 19:36:48 +08001181 BlockDriverState *bs;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001182 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001183 QEMUSnapshotInfo sn;
1184 Error *local_err = NULL;
1185 SnapshotInfo *info = NULL;
1186 int ret;
1187
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001188 bs = qmp_get_root_bs(device, errp);
1189 if (!bs) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001190 return NULL;
1191 }
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001192 aio_context = bdrv_get_aio_context(bs);
Max Reitz5433c242015-10-19 17:53:29 +02001193 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001194
1195 if (!has_id) {
1196 id = NULL;
1197 }
1198
1199 if (!has_name) {
1200 name = NULL;
1201 }
1202
1203 if (!id && !name) {
1204 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001205 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001206 }
1207
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001208 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1209 goto out_aio_context;
1210 }
1211
Wenchao Xia44e3e052013-09-11 14:04:36 +08001212 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001213 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001214 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001215 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001216 }
1217 if (!ret) {
1218 error_setg(errp,
1219 "Snapshot with id '%s' and name '%s' does not exist on "
1220 "device '%s'",
1221 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001222 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001223 }
1224
1225 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001226 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001227 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001228 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001229 }
1230
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001231 aio_context_release(aio_context);
1232
Markus Armbruster5839e532014-08-19 10:31:08 +02001233 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001234 info->id = g_strdup(sn.id_str);
1235 info->name = g_strdup(sn.name);
1236 info->date_nsec = sn.date_nsec;
1237 info->date_sec = sn.date_sec;
1238 info->vm_state_size = sn.vm_state_size;
1239 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1240 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1241
1242 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001243
1244out_aio_context:
1245 aio_context_release(aio_context);
1246 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001247}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001248
John Snow341ebc22015-04-17 19:49:52 -04001249/**
1250 * block_dirty_bitmap_lookup:
1251 * Return a dirty bitmap (if present), after validating
1252 * the node reference and bitmap names.
1253 *
1254 * @node: The name of the BDS node to search for bitmaps
1255 * @name: The name of the bitmap to search for
1256 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
John Snow341ebc22015-04-17 19:49:52 -04001257 * @errp: Output pointer for error information. Can be NULL.
1258 *
1259 * @return: A bitmap object on success, or NULL on failure.
1260 */
1261static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1262 const char *name,
1263 BlockDriverState **pbs,
John Snow341ebc22015-04-17 19:49:52 -04001264 Error **errp)
1265{
1266 BlockDriverState *bs;
1267 BdrvDirtyBitmap *bitmap;
John Snow341ebc22015-04-17 19:49:52 -04001268
1269 if (!node) {
1270 error_setg(errp, "Node cannot be NULL");
1271 return NULL;
1272 }
1273 if (!name) {
1274 error_setg(errp, "Bitmap name cannot be NULL");
1275 return NULL;
1276 }
1277 bs = bdrv_lookup_bs(node, node, NULL);
1278 if (!bs) {
1279 error_setg(errp, "Node '%s' not found", node);
1280 return NULL;
1281 }
1282
John Snow341ebc22015-04-17 19:49:52 -04001283 bitmap = bdrv_find_dirty_bitmap(bs, name);
1284 if (!bitmap) {
1285 error_setg(errp, "Dirty bitmap '%s' not found", name);
Paolo Bonzini21198822017-06-05 14:39:03 +02001286 return NULL;
John Snow341ebc22015-04-17 19:49:52 -04001287 }
1288
1289 if (pbs) {
1290 *pbs = bs;
1291 }
John Snow341ebc22015-04-17 19:49:52 -04001292
1293 return bitmap;
John Snow341ebc22015-04-17 19:49:52 -04001294}
1295
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001296/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001297
John Snow50f43f02015-11-05 18:13:09 -05001298typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001299
John Snow50f43f02015-11-05 18:13:09 -05001300/**
1301 * BlkActionOps:
1302 * Table of operations that define an Action.
1303 *
1304 * @instance_size: Size of state struct, in bytes.
1305 * @prepare: Prepare the work, must NOT be NULL.
1306 * @commit: Commit the changes, can be NULL.
1307 * @abort: Abort the changes on fail, can be NULL.
1308 * @clean: Clean up resources after all transaction actions have called
1309 * commit() or abort(). Can be NULL.
1310 *
1311 * Only prepare() may fail. In a single transaction, only one of commit() or
1312 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001313 */
John Snow50f43f02015-11-05 18:13:09 -05001314typedef struct BlkActionOps {
1315 size_t instance_size;
1316 void (*prepare)(BlkActionState *common, Error **errp);
1317 void (*commit)(BlkActionState *common);
1318 void (*abort)(BlkActionState *common);
1319 void (*clean)(BlkActionState *common);
1320} BlkActionOps;
1321
1322/**
1323 * BlkActionState:
1324 * Describes one Action's state within a Transaction.
1325 *
1326 * @action: QAPI-defined enum identifying which Action to perform.
1327 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001328 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001329 * @entry: List membership for all Actions in this Transaction.
1330 *
1331 * This structure must be arranged as first member in a subclassed type,
1332 * assuming that the compiler will also arrange it to the same offsets as the
1333 * base class.
1334 */
1335struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001336 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001337 const BlkActionOps *ops;
Kevin Wolf62c9e412018-04-19 16:09:52 +02001338 JobTxn *block_job_txn;
John Snow94d16a62015-11-05 18:13:18 -05001339 TransactionProperties *txn_props;
John Snowf4de0f82019-01-11 11:59:16 -06001340 QTAILQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001341};
1342
Wenchao Xiabbe86012013-09-11 14:04:34 +08001343/* internal snapshot private data */
1344typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001345 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001346 BlockDriverState *bs;
1347 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001348 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001349} InternalSnapshotState;
1350
John Snow94d16a62015-11-05 18:13:18 -05001351
1352static int action_check_completion_mode(BlkActionState *s, Error **errp)
1353{
1354 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1355 error_setg(errp,
1356 "Action '%s' does not support Transaction property "
1357 "completion-mode = %s",
Markus Armbruster977c7362017-08-24 10:46:08 +02001358 TransactionActionKind_str(s->action->type),
1359 ActionCompletionMode_str(s->txn_props->completion_mode));
John Snow94d16a62015-11-05 18:13:18 -05001360 return -1;
1361 }
1362 return 0;
1363}
1364
John Snow50f43f02015-11-05 18:13:09 -05001365static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001366 Error **errp)
1367{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001368 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001369 const char *device;
1370 const char *name;
1371 BlockDriverState *bs;
1372 QEMUSnapshotInfo old_sn, *sn;
1373 bool ret;
1374 qemu_timeval tv;
1375 BlockdevSnapshotInternal *internal;
1376 InternalSnapshotState *state;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001377 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001378 int ret1;
1379
Eric Blake6a8f9662015-10-26 16:34:54 -06001380 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001381 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001382 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001383 state = DO_UPCAST(InternalSnapshotState, common, common);
1384
1385 /* 1. parse input */
1386 device = internal->device;
1387 name = internal->name;
1388
1389 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001390 if (action_check_completion_mode(common, errp) < 0) {
1391 return;
1392 }
1393
Kevin Wolf75dfd402016-06-23 14:20:24 +02001394 bs = qmp_get_root_bs(device, errp);
1395 if (!bs) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001396 return;
1397 }
1398
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001399 aio_context = bdrv_get_aio_context(bs);
1400 aio_context_acquire(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001401
Fam Zheng507306c2015-10-23 11:08:13 +08001402 state->bs = bs;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001403
1404 /* Paired with .clean() */
Fam Zheng507306c2015-10-23 11:08:13 +08001405 bdrv_drained_begin(bs);
1406
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001407 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001408 goto out;
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001409 }
1410
Wenchao Xiabbe86012013-09-11 14:04:34 +08001411 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001412 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001413 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001414 }
1415
1416 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001417 error_setg(errp, "Block format '%s' used by device '%s' "
1418 "does not support internal snapshots",
1419 bs->drv->format_name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001420 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001421 }
1422
1423 if (!strlen(name)) {
1424 error_setg(errp, "Name is empty");
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001425 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001426 }
1427
1428 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001429 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1430 &local_err);
1431 if (local_err) {
1432 error_propagate(errp, local_err);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001433 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001434 } else if (ret) {
1435 error_setg(errp,
1436 "Snapshot with name '%s' already exists on device '%s'",
1437 name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001438 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001439 }
1440
1441 /* 3. take the snapshot */
1442 sn = &state->sn;
1443 pstrcpy(sn->name, sizeof(sn->name), name);
1444 qemu_gettimeofday(&tv);
1445 sn->date_sec = tv.tv_sec;
1446 sn->date_nsec = tv.tv_usec * 1000;
1447 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1448
1449 ret1 = bdrv_snapshot_create(bs, sn);
1450 if (ret1 < 0) {
1451 error_setg_errno(errp, -ret1,
1452 "Failed to create snapshot '%s' on device '%s'",
1453 name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001454 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001455 }
1456
1457 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001458 state->created = true;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001459
1460out:
1461 aio_context_release(aio_context);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001462}
1463
John Snow50f43f02015-11-05 18:13:09 -05001464static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001465{
1466 InternalSnapshotState *state =
1467 DO_UPCAST(InternalSnapshotState, common, common);
1468 BlockDriverState *bs = state->bs;
1469 QEMUSnapshotInfo *sn = &state->sn;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001470 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001471 Error *local_error = NULL;
1472
Fam Zheng507306c2015-10-23 11:08:13 +08001473 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001474 return;
1475 }
1476
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001477 aio_context = bdrv_get_aio_context(state->bs);
1478 aio_context_acquire(aio_context);
1479
Wenchao Xiabbe86012013-09-11 14:04:34 +08001480 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001481 error_reportf_err(local_error,
1482 "Failed to delete snapshot with id '%s' and "
1483 "name '%s' on device '%s' in abort: ",
1484 sn->id_str, sn->name,
1485 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001486 }
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001487
1488 aio_context_release(aio_context);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001489}
1490
John Snow50f43f02015-11-05 18:13:09 -05001491static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001492{
1493 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1494 common, common);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001495 AioContext *aio_context;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001496
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001497 if (!state->bs) {
1498 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001499 }
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001500
1501 aio_context = bdrv_get_aio_context(state->bs);
1502 aio_context_acquire(aio_context);
1503
1504 bdrv_drained_end(state->bs);
1505
1506 aio_context_release(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001507}
1508
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001509/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001510typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001511 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001512 BlockDriverState *old_bs;
1513 BlockDriverState *new_bs;
Kevin Wolf067acf22017-03-02 15:26:18 +01001514 bool overlay_appended;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001515} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001516
John Snow50f43f02015-11-05 18:13:09 -05001517static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001518 Error **errp)
1519{
Max Reitz5b363932016-05-17 16:41:31 +02001520 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001521 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001522 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001523 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001524 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001525 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001526 /* Reference to the new image (for 'blockdev-snapshot') */
1527 const char *snapshot_ref;
1528 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001529 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001530 ExternalSnapshotState *state =
1531 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001532 TransactionAction *action = common->action;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001533 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001534 AioContext *old_context;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02001535 int ret;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001536
Alberto Garcia43de7e22015-10-26 14:27:16 +02001537 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1538 * purpose but a different set of parameters */
1539 switch (action->type) {
1540 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1541 {
Eric Blake32bafa82016-03-17 16:48:37 -06001542 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001543 device = s->node;
1544 node_name = s->node;
1545 new_image_file = NULL;
1546 snapshot_ref = s->overlay;
1547 }
1548 break;
1549 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1550 {
Eric Blake32bafa82016-03-17 16:48:37 -06001551 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001552 device = s->has_device ? s->device : NULL;
1553 node_name = s->has_node_name ? s->node_name : NULL;
1554 new_image_file = s->snapshot_file;
1555 snapshot_ref = NULL;
1556 }
1557 break;
1558 default:
1559 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001560 }
1561
1562 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001563 if (action_check_completion_mode(common, errp) < 0) {
1564 return;
1565 }
1566
Alberto Garcia43de7e22015-10-26 14:27:16 +02001567 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1568 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001569 return;
1570 }
1571
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001572 aio_context = bdrv_get_aio_context(state->old_bs);
1573 aio_context_acquire(aio_context);
1574
1575 /* Paired with .clean() */
Fam Zhengda763e82015-10-23 11:08:10 +08001576 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001577
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001578 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001579 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001580 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001581 }
1582
Fam Zheng3718d8a2014-05-23 21:29:43 +08001583 if (bdrv_op_is_blocked(state->old_bs,
1584 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001585 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001586 }
1587
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001588 if (!bdrv_is_read_only(state->old_bs)) {
1589 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001590 error_setg(errp, QERR_IO_ERROR);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001591 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001592 }
1593 }
1594
Benoît Canet212a5a82014-01-23 21:31:36 +01001595 if (!bdrv_is_first_non_filter(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001596 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001597 goto out;
Benoît Canetf6186f42013-10-02 14:33:48 +02001598 }
1599
Alberto Garcia43de7e22015-10-26 14:27:16 +02001600 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001601 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001602 const char *format = s->has_format ? s->format : "qcow2";
1603 enum NewImageMode mode;
1604 const char *snapshot_node_name =
1605 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001606
Alberto Garcia43de7e22015-10-26 14:27:16 +02001607 if (node_name && !snapshot_node_name) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001608 error_setg(errp, "New overlay node name missing");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001609 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001610 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001611
1612 if (snapshot_node_name &&
1613 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001614 error_setg(errp, "New overlay node name already in use");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001615 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001616 }
1617
1618 flags = state->old_bs->open_flags;
John Snow2a32c6e2017-07-17 20:34:21 -04001619 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ);
1620 flags |= BDRV_O_NO_BACKING;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001621
1622 /* create new image w/backing file */
1623 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1624 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001625 int64_t size = bdrv_getlength(state->old_bs);
1626 if (size < 0) {
1627 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001628 goto out;
Kevin Wolff86b8b52016-03-02 12:16:44 +01001629 }
Max Reitzf30c66b2019-02-01 20:29:05 +01001630 bdrv_refresh_filename(state->old_bs);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001631 bdrv_img_create(new_image_file, format,
1632 state->old_bs->filename,
1633 state->old_bs->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08001634 NULL, size, flags, false, &local_err);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001635 if (local_err) {
1636 error_propagate(errp, local_err);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001637 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001638 }
1639 }
1640
1641 options = qdict_new();
Peter Maydelld52e1a02018-11-01 16:30:37 +00001642 if (snapshot_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05001643 qdict_put_str(options, "node-name", snapshot_node_name);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001644 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001645 qdict_put_str(options, "driver", format);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001646 }
1647
Max Reitz5b363932016-05-17 16:41:31 +02001648 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1649 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001650 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001651 if (!state->new_bs) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001652 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001653 }
1654
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001655 if (bdrv_has_blk(state->new_bs)) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001656 error_setg(errp, "The overlay is already in use");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001657 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001658 }
1659
1660 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1661 errp)) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001662 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001663 }
1664
1665 if (state->new_bs->backing != NULL) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001666 error_setg(errp, "The overlay already has a backing image");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001667 goto out;
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001668 }
1669
1670 if (!state->new_bs->drv->supports_backing) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001671 error_setg(errp, "The overlay does not support backing images");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001672 goto out;
Kevin Wolfb2c28322017-02-20 12:46:42 +01001673 }
1674
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001675 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1676 old_context = bdrv_get_aio_context(state->new_bs);
1677 aio_context_release(aio_context);
1678 aio_context_acquire(old_context);
1679
Kevin Wolf8ed7d422019-04-26 16:12:27 +02001680 ret = bdrv_try_set_aio_context(state->new_bs, aio_context, errp);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001681
1682 aio_context_release(old_context);
1683 aio_context_acquire(aio_context);
1684
Kevin Wolf8ed7d422019-04-26 16:12:27 +02001685 if (ret < 0) {
1686 goto out;
1687 }
Fam Zhengc26a5ab2017-04-07 14:54:09 +08001688
Kevin Wolfb2c28322017-02-20 12:46:42 +01001689 /* This removes our old bs and adds the new bs. This is an operation that
1690 * can fail, so we need to do it in .prepare; undoing it for abort is
1691 * always possible. */
1692 bdrv_ref(state->new_bs);
1693 bdrv_append(state->new_bs, state->old_bs, &local_err);
1694 if (local_err) {
1695 error_propagate(errp, local_err);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001696 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001697 }
Kevin Wolf067acf22017-03-02 15:26:18 +01001698 state->overlay_appended = true;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001699
1700out:
1701 aio_context_release(aio_context);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001702}
1703
John Snow50f43f02015-11-05 18:13:09 -05001704static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001705{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001706 ExternalSnapshotState *state =
1707 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001708 AioContext *aio_context;
1709
1710 aio_context = bdrv_get_aio_context(state->old_bs);
1711 aio_context_acquire(aio_context);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001712
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001713 /* We don't need (or want) to use the transactional
1714 * bdrv_reopen_multiple() across all the entries at once, because we
1715 * don't want to abort all of them if one of them fails the reopen */
Paolo Bonzinid3faa132017-06-05 14:38:50 +02001716 if (!atomic_read(&state->old_bs->copy_on_read)) {
Alberto Garcia1b577742018-11-12 16:00:39 +02001717 bdrv_reopen_set_read_only(state->old_bs, true, NULL);
Kevin Wolf4c844982016-02-29 13:12:26 +01001718 }
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001719
1720 aio_context_release(aio_context);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001721}
1722
John Snow50f43f02015-11-05 18:13:09 -05001723static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001724{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001725 ExternalSnapshotState *state =
1726 DO_UPCAST(ExternalSnapshotState, common, common);
1727 if (state->new_bs) {
Kevin Wolf067acf22017-03-02 15:26:18 +01001728 if (state->overlay_appended) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001729 AioContext *aio_context;
Sergio Lopez377410f2020-01-08 15:31:37 +01001730 AioContext *tmp_context;
1731 int ret;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001732
1733 aio_context = bdrv_get_aio_context(state->old_bs);
1734 aio_context_acquire(aio_context);
1735
Jeff Cody719fc282017-06-07 09:55:22 -04001736 bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
1737 close state->old_bs; we need it */
1738 bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
Sergio Lopez377410f2020-01-08 15:31:37 +01001739
1740 /*
1741 * The call to bdrv_set_backing_hd() above returns state->old_bs to
1742 * the main AioContext. As we're still going to be using it, return
1743 * it to the AioContext it was before.
1744 */
1745 tmp_context = bdrv_get_aio_context(state->old_bs);
1746 if (aio_context != tmp_context) {
1747 aio_context_release(aio_context);
1748 aio_context_acquire(tmp_context);
1749
1750 ret = bdrv_try_set_aio_context(state->old_bs,
1751 aio_context, NULL);
1752 assert(ret == 0);
1753
1754 aio_context_release(tmp_context);
1755 aio_context_acquire(aio_context);
1756 }
1757
Kevin Wolf5fe31c22017-03-06 16:20:51 +01001758 bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
Jeff Cody719fc282017-06-07 09:55:22 -04001759 bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001760
1761 aio_context_release(aio_context);
Kevin Wolfb2c28322017-02-20 12:46:42 +01001762 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001763 }
Fam Zhengda763e82015-10-23 11:08:10 +08001764}
1765
John Snow50f43f02015-11-05 18:13:09 -05001766static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001767{
1768 ExternalSnapshotState *state =
1769 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001770 AioContext *aio_context;
1771
1772 if (!state->old_bs) {
1773 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001774 }
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001775
1776 aio_context = bdrv_get_aio_context(state->old_bs);
1777 aio_context_acquire(aio_context);
1778
1779 bdrv_drained_end(state->old_bs);
1780 bdrv_unref(state->new_bs);
1781
1782 aio_context_release(aio_context);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001783}
1784
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001785typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001786 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001787 BlockDriverState *bs;
1788 BlockJob *job;
1789} DriveBackupState;
1790
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001791static BlockJob *do_backup_common(BackupCommon *backup,
1792 BlockDriverState *bs,
1793 BlockDriverState *target_bs,
1794 AioContext *aio_context,
1795 JobTxn *txn, Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001796
John Snow50f43f02015-11-05 18:13:09 -05001797static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001798{
1799 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1800 DriveBackup *backup;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001801 BlockDriverState *bs;
1802 BlockDriverState *target_bs;
1803 BlockDriverState *source = NULL;
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001804 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001805 AioContext *old_context;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001806 QDict *options;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001807 Error *local_err = NULL;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001808 int flags;
1809 int64_t size;
1810 bool set_backing_hd = false;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001811 int ret;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001812
Eric Blake6a8f9662015-10-26 16:34:54 -06001813 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001814 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001815
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001816 if (!backup->has_mode) {
1817 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1818 }
1819
Vladimir Sementsov-Ogievskiy85c9d132019-06-18 17:08:04 +03001820 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001821 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001822 return;
1823 }
1824
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001825 if (!bs->drv) {
1826 error_setg(errp, "Device has no medium");
1827 return;
1828 }
1829
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001830 aio_context = bdrv_get_aio_context(bs);
1831 aio_context_acquire(aio_context);
1832
1833 /* Paired with .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001834 bdrv_drained_begin(bs);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001835
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001836 if (!backup->has_format) {
1837 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
1838 NULL : (char *) bs->drv->format_name;
1839 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001840
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001841 /* Early check to avoid creating target */
1842 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
1843 goto out;
1844 }
1845
1846 flags = bs->open_flags | BDRV_O_RDWR;
1847
1848 /*
1849 * See if we have a backing HD we can use to create our new image
1850 * on top of.
1851 */
1852 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
1853 source = backing_bs(bs);
1854 if (!source) {
1855 backup->sync = MIRROR_SYNC_MODE_FULL;
1856 }
1857 }
1858 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
1859 source = bs;
1860 flags |= BDRV_O_NO_BACKING;
1861 set_backing_hd = true;
1862 }
1863
1864 size = bdrv_getlength(bs);
1865 if (size < 0) {
1866 error_setg_errno(errp, -size, "bdrv_getlength failed");
1867 goto out;
1868 }
1869
1870 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
1871 assert(backup->format);
1872 if (source) {
1873 bdrv_refresh_filename(source);
1874 bdrv_img_create(backup->target, backup->format, source->filename,
1875 source->drv->format_name, NULL,
1876 size, flags, false, &local_err);
1877 } else {
1878 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
1879 size, flags, false, &local_err);
1880 }
1881 }
1882
Markus Armbruster84d18f02014-01-30 15:07:28 +01001883 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001884 error_propagate(errp, local_err);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001885 goto out;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001886 }
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001887
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001888 options = qdict_new();
1889 qdict_put_str(options, "discard", "unmap");
1890 qdict_put_str(options, "detect-zeroes", "unmap");
1891 if (backup->format) {
1892 qdict_put_str(options, "driver", backup->format);
1893 }
1894
1895 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
1896 if (!target_bs) {
1897 goto out;
1898 }
1899
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001900 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1901 old_context = bdrv_get_aio_context(target_bs);
1902 aio_context_release(aio_context);
1903 aio_context_acquire(old_context);
1904
1905 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
1906 if (ret < 0) {
1907 bdrv_unref(target_bs);
1908 aio_context_release(old_context);
1909 return;
1910 }
1911
1912 aio_context_release(old_context);
1913 aio_context_acquire(aio_context);
1914
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001915 if (set_backing_hd) {
1916 bdrv_set_backing_hd(target_bs, source, &local_err);
1917 if (local_err) {
1918 goto unref;
1919 }
1920 }
1921
1922 state->bs = bs;
1923
1924 state->job = do_backup_common(qapi_DriveBackup_base(backup),
1925 bs, target_bs, aio_context,
1926 common->block_job_txn, errp);
1927
1928unref:
1929 bdrv_unref(target_bs);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001930out:
1931 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05001932}
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001933
John Snow111049a2016-11-08 01:50:38 -05001934static void drive_backup_commit(BlkActionState *common)
1935{
1936 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001937 AioContext *aio_context;
1938
1939 aio_context = bdrv_get_aio_context(state->bs);
1940 aio_context_acquire(aio_context);
1941
John Snow111049a2016-11-08 01:50:38 -05001942 assert(state->job);
Kevin Wolfda01ff72018-04-13 17:31:02 +02001943 job_start(&state->job->job);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001944
1945 aio_context_release(aio_context);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001946}
1947
John Snow50f43f02015-11-05 18:13:09 -05001948static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001949{
1950 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001951
John Snow111049a2016-11-08 01:50:38 -05001952 if (state->job) {
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001953 AioContext *aio_context;
1954
1955 aio_context = bdrv_get_aio_context(state->bs);
1956 aio_context_acquire(aio_context);
1957
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001958 job_cancel_sync(&state->job->job);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001959
1960 aio_context_release(aio_context);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001961 }
1962}
1963
John Snow50f43f02015-11-05 18:13:09 -05001964static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001965{
1966 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001967 AioContext *aio_context;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001968
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001969 if (!state->bs) {
1970 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001971 }
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001972
1973 aio_context = bdrv_get_aio_context(state->bs);
1974 aio_context_acquire(aio_context);
1975
1976 bdrv_drained_end(state->bs);
1977
1978 aio_context_release(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001979}
1980
Fam Zhengbd8baec2014-12-18 18:37:06 +08001981typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001982 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001983 BlockDriverState *bs;
1984 BlockJob *job;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001985} BlockdevBackupState;
1986
John Snow50f43f02015-11-05 18:13:09 -05001987static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001988{
1989 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1990 BlockdevBackup *backup;
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01001991 BlockDriverState *bs;
1992 BlockDriverState *target_bs;
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001993 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001994 AioContext *old_context;
1995 int ret;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001996
Eric Blake6a8f9662015-10-26 16:34:54 -06001997 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001998 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001999
Vladimir Sementsov-Ogievskiy930fe172018-07-02 15:46:29 -04002000 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02002001 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08002002 return;
2003 }
2004
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01002005 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
2006 if (!target_bs) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08002007 return;
2008 }
2009
Sergio Lopez3ea67e02020-01-08 15:31:34 +01002010 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002011 aio_context = bdrv_get_aio_context(bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01002012 old_context = bdrv_get_aio_context(target_bs);
2013 aio_context_acquire(old_context);
2014
2015 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
2016 if (ret < 0) {
2017 aio_context_release(old_context);
2018 return;
2019 }
2020
2021 aio_context_release(old_context);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002022 aio_context_acquire(aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02002023 state->bs = bs;
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002024
2025 /* Paired with .clean() */
Fam Zhengff52bf32015-10-23 11:08:12 +08002026 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002027
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01002028 state->job = do_backup_common(qapi_BlockdevBackup_base(backup),
2029 bs, target_bs, aio_context,
2030 common->block_job_txn, errp);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002031
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002032 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05002033}
Fam Zhengbd8baec2014-12-18 18:37:06 +08002034
John Snow111049a2016-11-08 01:50:38 -05002035static void blockdev_backup_commit(BlkActionState *common)
2036{
2037 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002038 AioContext *aio_context;
2039
2040 aio_context = bdrv_get_aio_context(state->bs);
2041 aio_context_acquire(aio_context);
2042
John Snow111049a2016-11-08 01:50:38 -05002043 assert(state->job);
Kevin Wolfda01ff72018-04-13 17:31:02 +02002044 job_start(&state->job->job);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002045
2046 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002047}
2048
John Snow50f43f02015-11-05 18:13:09 -05002049static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08002050{
2051 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002052
John Snow111049a2016-11-08 01:50:38 -05002053 if (state->job) {
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002054 AioContext *aio_context;
2055
2056 aio_context = bdrv_get_aio_context(state->bs);
2057 aio_context_acquire(aio_context);
2058
Kevin Wolf3d70ff52018-04-24 16:13:52 +02002059 job_cancel_sync(&state->job->job);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002060
2061 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002062 }
2063}
2064
John Snow50f43f02015-11-05 18:13:09 -05002065static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08002066{
2067 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002068 AioContext *aio_context;
Fam Zhengbd8baec2014-12-18 18:37:06 +08002069
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002070 if (!state->bs) {
2071 return;
Fam Zhengbd8baec2014-12-18 18:37:06 +08002072 }
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002073
2074 aio_context = bdrv_get_aio_context(state->bs);
2075 aio_context_acquire(aio_context);
2076
2077 bdrv_drained_end(state->bs);
2078
2079 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002080}
2081
Fam Zhengdf9a6812015-11-09 18:16:54 +08002082typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05002083 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002084 BdrvDirtyBitmap *bitmap;
2085 BlockDriverState *bs;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002086 HBitmap *backup;
2087 bool prepared;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002088 bool was_enabled;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002089} BlockDirtyBitmapState;
2090
John Snow50f43f02015-11-05 18:13:09 -05002091static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002092 Error **errp)
2093{
2094 Error *local_err = NULL;
2095 BlockDirtyBitmapAdd *action;
2096 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2097 common, common);
2098
John Snow94d16a62015-11-05 18:13:18 -05002099 if (action_check_completion_mode(common, errp) < 0) {
2100 return;
2101 }
2102
Eric Blake32bafa82016-03-17 16:48:37 -06002103 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002104 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2105 qmp_block_dirty_bitmap_add(action->node, action->name,
2106 action->has_granularity, action->granularity,
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002107 action->has_persistent, action->persistent,
John Snow0e2b7f02018-12-21 04:35:22 -05002108 action->has_disabled, action->disabled,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002109 &local_err);
2110
2111 if (!local_err) {
2112 state->prepared = true;
2113 } else {
2114 error_propagate(errp, local_err);
2115 }
2116}
2117
John Snow50f43f02015-11-05 18:13:09 -05002118static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002119{
2120 BlockDirtyBitmapAdd *action;
2121 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2122 common, common);
2123
Eric Blake32bafa82016-03-17 16:48:37 -06002124 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002125 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2126 * then the node reference and bitmap name must have been valid.
2127 */
2128 if (state->prepared) {
2129 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2130 }
2131}
2132
John Snow50f43f02015-11-05 18:13:09 -05002133static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002134 Error **errp)
2135{
2136 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2137 common, common);
2138 BlockDirtyBitmap *action;
2139
John Snow94d16a62015-11-05 18:13:18 -05002140 if (action_check_completion_mode(common, errp) < 0) {
2141 return;
2142 }
2143
Eric Blake32bafa82016-03-17 16:48:37 -06002144 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002145 state->bitmap = block_dirty_bitmap_lookup(action->node,
2146 action->name,
2147 &state->bs,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002148 errp);
2149 if (!state->bitmap) {
2150 return;
2151 }
2152
John Snow3ae96d62019-03-12 12:05:49 -04002153 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) {
Vladimir Sementsov-Ogievskiyd6883bc2017-06-28 15:05:10 +03002154 return;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002155 }
2156
2157 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
Fam Zhengdf9a6812015-11-09 18:16:54 +08002158}
2159
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002160static void block_dirty_bitmap_restore(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002161{
2162 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2163 common, common);
2164
John Snow184dd9c2017-03-15 17:28:11 -04002165 if (state->backup) {
Vladimir Sementsov-Ogievskiy56bd6622018-10-29 16:23:14 -04002166 bdrv_restore_dirty_bitmap(state->bitmap, state->backup);
John Snow184dd9c2017-03-15 17:28:11 -04002167 }
Fam Zhengdf9a6812015-11-09 18:16:54 +08002168}
2169
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002170static void block_dirty_bitmap_free_backup(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002171{
2172 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2173 common, common);
2174
2175 hbitmap_free(state->backup);
2176}
2177
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002178static void block_dirty_bitmap_enable_prepare(BlkActionState *common,
2179 Error **errp)
2180{
2181 BlockDirtyBitmap *action;
2182 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2183 common, common);
2184
2185 if (action_check_completion_mode(common, errp) < 0) {
2186 return;
2187 }
2188
John Snow0e2b7f02018-12-21 04:35:22 -05002189 action = common->action->u.block_dirty_bitmap_enable.data;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002190 state->bitmap = block_dirty_bitmap_lookup(action->node,
2191 action->name,
2192 NULL,
2193 errp);
2194 if (!state->bitmap) {
2195 return;
2196 }
2197
John Snow3ae96d62019-03-12 12:05:49 -04002198 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snowb053bb52018-10-29 16:23:16 -04002199 return;
2200 }
2201
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002202 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2203 bdrv_enable_dirty_bitmap(state->bitmap);
2204}
2205
2206static void block_dirty_bitmap_enable_abort(BlkActionState *common)
2207{
2208 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2209 common, common);
2210
2211 if (!state->was_enabled) {
2212 bdrv_disable_dirty_bitmap(state->bitmap);
2213 }
2214}
2215
2216static void block_dirty_bitmap_disable_prepare(BlkActionState *common,
2217 Error **errp)
2218{
2219 BlockDirtyBitmap *action;
2220 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2221 common, common);
2222
2223 if (action_check_completion_mode(common, errp) < 0) {
2224 return;
2225 }
2226
John Snow0e2b7f02018-12-21 04:35:22 -05002227 action = common->action->u.block_dirty_bitmap_disable.data;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002228 state->bitmap = block_dirty_bitmap_lookup(action->node,
2229 action->name,
2230 NULL,
2231 errp);
2232 if (!state->bitmap) {
2233 return;
2234 }
2235
John Snow3ae96d62019-03-12 12:05:49 -04002236 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snowb053bb52018-10-29 16:23:16 -04002237 return;
2238 }
2239
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002240 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2241 bdrv_disable_dirty_bitmap(state->bitmap);
2242}
2243
2244static void block_dirty_bitmap_disable_abort(BlkActionState *common)
2245{
2246 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2247 common, common);
2248
2249 if (state->was_enabled) {
2250 bdrv_enable_dirty_bitmap(state->bitmap);
2251 }
2252}
2253
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04002254static BdrvDirtyBitmap *do_block_dirty_bitmap_merge(
2255 const char *node, const char *target,
2256 BlockDirtyBitmapMergeSourceList *bitmaps,
2257 HBitmap **backup, Error **errp);
John Snow360d4e42018-12-21 04:35:21 -05002258
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002259static void block_dirty_bitmap_merge_prepare(BlkActionState *common,
2260 Error **errp)
2261{
2262 BlockDirtyBitmapMerge *action;
2263 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2264 common, common);
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002265
2266 if (action_check_completion_mode(common, errp) < 0) {
2267 return;
2268 }
2269
John Snow0e2b7f02018-12-21 04:35:22 -05002270 action = common->action->u.block_dirty_bitmap_merge.data;
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002271
John Snow360d4e42018-12-21 04:35:21 -05002272 state->bitmap = do_block_dirty_bitmap_merge(action->node, action->target,
2273 action->bitmaps, &state->backup,
2274 errp);
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002275}
2276
John Snowc4e4b0f2019-07-29 16:35:54 -04002277static BdrvDirtyBitmap *do_block_dirty_bitmap_remove(
2278 const char *node, const char *name, bool release,
2279 BlockDriverState **bitmap_bs, Error **errp);
2280
2281static void block_dirty_bitmap_remove_prepare(BlkActionState *common,
2282 Error **errp)
2283{
2284 BlockDirtyBitmap *action;
2285 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2286 common, common);
2287
2288 if (action_check_completion_mode(common, errp) < 0) {
2289 return;
2290 }
2291
2292 action = common->action->u.block_dirty_bitmap_remove.data;
2293
2294 state->bitmap = do_block_dirty_bitmap_remove(action->node, action->name,
2295 false, &state->bs, errp);
2296 if (state->bitmap) {
2297 bdrv_dirty_bitmap_skip_store(state->bitmap, true);
2298 bdrv_dirty_bitmap_set_busy(state->bitmap, true);
2299 }
2300}
2301
2302static void block_dirty_bitmap_remove_abort(BlkActionState *common)
2303{
2304 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2305 common, common);
2306
2307 if (state->bitmap) {
2308 bdrv_dirty_bitmap_skip_store(state->bitmap, false);
2309 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
2310 }
2311}
2312
2313static void block_dirty_bitmap_remove_commit(BlkActionState *common)
2314{
2315 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2316 common, common);
2317
2318 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +03002319 bdrv_release_dirty_bitmap(state->bitmap);
John Snowc4e4b0f2019-07-29 16:35:54 -04002320}
2321
John Snow50f43f02015-11-05 18:13:09 -05002322static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002323{
2324 error_setg(errp, "Transaction aborted using Abort action");
2325}
2326
John Snow50f43f02015-11-05 18:13:09 -05002327static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002328{
Stefan Weildfc6f862013-07-25 18:21:28 +02002329 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002330}
2331
John Snow50f43f02015-11-05 18:13:09 -05002332static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002333 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2334 .instance_size = sizeof(ExternalSnapshotState),
2335 .prepare = external_snapshot_prepare,
2336 .commit = external_snapshot_commit,
2337 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002338 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002339 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002340 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002341 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002342 .prepare = external_snapshot_prepare,
2343 .commit = external_snapshot_commit,
2344 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002345 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002346 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002347 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2348 .instance_size = sizeof(DriveBackupState),
2349 .prepare = drive_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002350 .commit = drive_backup_commit,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002351 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002352 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002353 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002354 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2355 .instance_size = sizeof(BlockdevBackupState),
2356 .prepare = blockdev_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002357 .commit = blockdev_backup_commit,
Fam Zhengbd8baec2014-12-18 18:37:06 +08002358 .abort = blockdev_backup_abort,
2359 .clean = blockdev_backup_clean,
2360 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002361 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002362 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002363 .prepare = abort_prepare,
2364 .commit = abort_commit,
2365 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002366 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2367 .instance_size = sizeof(InternalSnapshotState),
2368 .prepare = internal_snapshot_prepare,
2369 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002370 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002371 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002372 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2373 .instance_size = sizeof(BlockDirtyBitmapState),
2374 .prepare = block_dirty_bitmap_add_prepare,
2375 .abort = block_dirty_bitmap_add_abort,
2376 },
2377 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2378 .instance_size = sizeof(BlockDirtyBitmapState),
2379 .prepare = block_dirty_bitmap_clear_prepare,
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002380 .commit = block_dirty_bitmap_free_backup,
2381 .abort = block_dirty_bitmap_restore,
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002382 },
John Snow0e2b7f02018-12-21 04:35:22 -05002383 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = {
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002384 .instance_size = sizeof(BlockDirtyBitmapState),
2385 .prepare = block_dirty_bitmap_enable_prepare,
2386 .abort = block_dirty_bitmap_enable_abort,
2387 },
John Snow0e2b7f02018-12-21 04:35:22 -05002388 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = {
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002389 .instance_size = sizeof(BlockDirtyBitmapState),
2390 .prepare = block_dirty_bitmap_disable_prepare,
2391 .abort = block_dirty_bitmap_disable_abort,
John Snow66da04d2018-09-06 09:02:25 -04002392 },
John Snow0e2b7f02018-12-21 04:35:22 -05002393 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = {
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002394 .instance_size = sizeof(BlockDirtyBitmapState),
2395 .prepare = block_dirty_bitmap_merge_prepare,
2396 .commit = block_dirty_bitmap_free_backup,
2397 .abort = block_dirty_bitmap_restore,
2398 },
John Snowc4e4b0f2019-07-29 16:35:54 -04002399 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = {
2400 .instance_size = sizeof(BlockDirtyBitmapState),
2401 .prepare = block_dirty_bitmap_remove_prepare,
2402 .commit = block_dirty_bitmap_remove_commit,
2403 .abort = block_dirty_bitmap_remove_abort,
2404 },
John Snow66da04d2018-09-06 09:02:25 -04002405 /* Where are transactions for MIRROR, COMMIT and STREAM?
2406 * Although these blockjobs use transaction callbacks like the backup job,
2407 * these jobs do not necessarily adhere to transaction semantics.
2408 * These jobs may not fully undo all of their actions on abort, nor do they
2409 * necessarily work in transactions with more than one job in them.
2410 */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002411};
2412
John Snow94d16a62015-11-05 18:13:18 -05002413/**
2414 * Allocate a TransactionProperties structure if necessary, and fill
2415 * that structure with desired defaults if they are unset.
2416 */
2417static TransactionProperties *get_transaction_properties(
2418 TransactionProperties *props)
2419{
2420 if (!props) {
2421 props = g_new0(TransactionProperties, 1);
2422 }
2423
2424 if (!props->has_completion_mode) {
2425 props->has_completion_mode = true;
2426 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2427 }
2428
2429 return props;
2430}
2431
Jeff Cody8802d1f2012-02-28 15:54:06 -05002432/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002433 * 'Atomic' group operations. The operations are performed as a set, and if
2434 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002435 */
John Snow94d16a62015-11-05 18:13:18 -05002436void qmp_transaction(TransactionActionList *dev_list,
2437 bool has_props,
2438 struct TransactionProperties *props,
2439 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002440{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002441 TransactionActionList *dev_entry = dev_list;
Kevin Wolf62c9e412018-04-19 16:09:52 +02002442 JobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002443 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002444 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002445
John Snowf4de0f82019-01-11 11:59:16 -06002446 QTAILQ_HEAD(, BlkActionState) snap_bdrv_states;
2447 QTAILQ_INIT(&snap_bdrv_states);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002448
John Snow94d16a62015-11-05 18:13:18 -05002449 /* Does this transaction get canceled as a group on failure?
Kevin Wolf62c9e412018-04-19 16:09:52 +02002450 * If not, we don't really need to make a JobTxn.
John Snow94d16a62015-11-05 18:13:18 -05002451 */
2452 props = get_transaction_properties(props);
2453 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02002454 block_job_txn = job_txn_new();
John Snow94d16a62015-11-05 18:13:18 -05002455 }
2456
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002457 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002458 bdrv_drain_all();
2459
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002460 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002461 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002462 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002463 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002464
Jeff Cody8802d1f2012-02-28 15:54:06 -05002465 dev_info = dev_entry->value;
2466 dev_entry = dev_entry->next;
2467
Eric Blake6a8f9662015-10-26 16:34:54 -06002468 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002469
Eric Blake6a8f9662015-10-26 16:34:54 -06002470 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002471 assert(ops->instance_size > 0);
2472
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002473 state = g_malloc0(ops->instance_size);
2474 state->ops = ops;
2475 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002476 state->block_job_txn = block_job_txn;
2477 state->txn_props = props;
John Snowf4de0f82019-01-11 11:59:16 -06002478 QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002479
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002480 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002481 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002482 error_propagate(errp, local_err);
2483 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002484 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002485 }
2486
John Snowf4de0f82019-01-11 11:59:16 -06002487 QTAILQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002488 if (state->ops->commit) {
2489 state->ops->commit(state);
2490 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002491 }
2492
2493 /* success */
2494 goto exit;
2495
2496delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002497 /* failure, and it is all-or-none; roll back all operations */
John Snowf4de0f82019-01-11 11:59:16 -06002498 QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002499 if (state->ops->abort) {
2500 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002501 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002502 }
2503exit:
John Snowf4de0f82019-01-11 11:59:16 -06002504 QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002505 if (state->ops->clean) {
2506 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002507 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002508 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002509 }
John Snow94d16a62015-11-05 18:13:18 -05002510 if (!has_props) {
2511 qapi_free_TransactionProperties(props);
2512 }
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02002513 job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002514}
2515
Kevin Wolffbe2d812016-09-20 13:38:46 +02002516void qmp_eject(bool has_device, const char *device,
2517 bool has_id, const char *id,
2518 bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002519{
Max Reitz38f54bd2015-10-26 21:39:12 +01002520 Error *local_err = NULL;
John Snow3a3086b2016-05-18 17:53:40 -04002521 int rc;
Markus Armbruster666daa62010-06-02 18:48:27 +02002522
John Snow3a3086b2016-05-18 17:53:40 -04002523 if (!has_force) {
2524 force = false;
2525 }
2526
Kevin Wolffbe2d812016-09-20 13:38:46 +02002527 rc = do_open_tray(has_device ? device : NULL,
2528 has_id ? id : NULL,
2529 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002530 if (rc && rc != -ENOSYS) {
Max Reitz38f54bd2015-10-26 21:39:12 +01002531 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002532 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002533 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002534 error_free(local_err);
John Snow3a3086b2016-05-18 17:53:40 -04002535
Max Reitz82fcf662017-11-10 23:43:01 +01002536 blockdev_remove_medium(has_device, device, has_id, id, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002537}
2538
Benoît Canet12d3ba82014-01-23 21:31:35 +01002539void qmp_block_passwd(bool has_device, const char *device,
2540 bool has_node_name, const char *node_name,
2541 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002542{
Daniel P. Berrangec01c2142017-06-23 17:24:16 +01002543 error_setg(errp,
2544 "Setting block passwords directly is no longer supported");
Markus Armbruster666daa62010-06-02 18:48:27 +02002545}
2546
Colin Lordbf18bee2016-06-06 14:15:22 -04002547/*
2548 * Attempt to open the tray of @device.
2549 * If @force, ignore its tray lock.
2550 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2551 * On error, store an error through @errp and return -errno.
2552 * If @device does not exist, return -ENODEV.
2553 * If it has no removable media, return -ENOTSUP.
2554 * If it has no tray, return -ENOSYS.
2555 * If the guest was asked to open the tray, return -EINPROGRESS.
2556 * Else, return 0.
John Snow3a3086b2016-05-18 17:53:40 -04002557 */
Kevin Wolfb33945c2016-09-20 13:38:43 +02002558static int do_open_tray(const char *blk_name, const char *qdev_id,
2559 bool force, Error **errp)
Max Reitz7d8a9f72015-10-26 21:39:08 +01002560{
2561 BlockBackend *blk;
Kevin Wolfb33945c2016-09-20 13:38:43 +02002562 const char *device = qdev_id ?: blk_name;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002563 bool locked;
2564
Kevin Wolfb33945c2016-09-20 13:38:43 +02002565 blk = qmp_get_blk(blk_name, qdev_id, errp);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002566 if (!blk) {
John Snow3a3086b2016-05-18 17:53:40 -04002567 return -ENODEV;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002568 }
2569
2570 if (!blk_dev_has_removable_media(blk)) {
2571 error_setg(errp, "Device '%s' is not removable", device);
John Snow3a3086b2016-05-18 17:53:40 -04002572 return -ENOTSUP;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002573 }
2574
Max Reitz12c7ec82016-01-29 20:49:11 +01002575 if (!blk_dev_has_tray(blk)) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002576 error_setg(errp, "Device '%s' does not have a tray", device);
2577 return -ENOSYS;
Max Reitz12c7ec82016-01-29 20:49:11 +01002578 }
2579
Max Reitz7d8a9f72015-10-26 21:39:08 +01002580 if (blk_dev_is_tray_open(blk)) {
John Snow3a3086b2016-05-18 17:53:40 -04002581 return 0;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002582 }
2583
2584 locked = blk_dev_is_medium_locked(blk);
2585 if (locked) {
2586 blk_dev_eject_request(blk, force);
2587 }
2588
2589 if (!locked || force) {
Kevin Wolf39829a02017-01-24 14:21:41 +01002590 blk_dev_change_media_cb(blk, false, &error_abort);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002591 }
John Snow3a3086b2016-05-18 17:53:40 -04002592
2593 if (locked && !force) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002594 error_setg(errp, "Device '%s' is locked and force was not specified, "
2595 "wait for tray to open and try again", device);
2596 return -EINPROGRESS;
John Snow3a3086b2016-05-18 17:53:40 -04002597 }
2598
2599 return 0;
2600}
2601
Kevin Wolfb33945c2016-09-20 13:38:43 +02002602void qmp_blockdev_open_tray(bool has_device, const char *device,
2603 bool has_id, const char *id,
2604 bool has_force, bool force,
John Snow3a3086b2016-05-18 17:53:40 -04002605 Error **errp)
2606{
Colin Lordbf18bee2016-06-06 14:15:22 -04002607 Error *local_err = NULL;
2608 int rc;
2609
John Snow3a3086b2016-05-18 17:53:40 -04002610 if (!has_force) {
2611 force = false;
2612 }
Kevin Wolfb33945c2016-09-20 13:38:43 +02002613 rc = do_open_tray(has_device ? device : NULL,
2614 has_id ? id : NULL,
2615 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002616 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2617 error_propagate(errp, local_err);
2618 return;
2619 }
2620 error_free(local_err);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002621}
2622
Kevin Wolfb33945c2016-09-20 13:38:43 +02002623void qmp_blockdev_close_tray(bool has_device, const char *device,
2624 bool has_id, const char *id,
2625 Error **errp)
Max Reitzabaaf592015-10-26 21:39:09 +01002626{
2627 BlockBackend *blk;
Kevin Wolf39829a02017-01-24 14:21:41 +01002628 Error *local_err = NULL;
Max Reitzabaaf592015-10-26 21:39:09 +01002629
Kevin Wolfb33945c2016-09-20 13:38:43 +02002630 device = has_device ? device : NULL;
2631 id = has_id ? id : NULL;
2632
2633 blk = qmp_get_blk(device, id, errp);
Max Reitzabaaf592015-10-26 21:39:09 +01002634 if (!blk) {
Max Reitzabaaf592015-10-26 21:39:09 +01002635 return;
2636 }
2637
2638 if (!blk_dev_has_removable_media(blk)) {
Kevin Wolfb33945c2016-09-20 13:38:43 +02002639 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitzabaaf592015-10-26 21:39:09 +01002640 return;
2641 }
2642
Max Reitz12c7ec82016-01-29 20:49:11 +01002643 if (!blk_dev_has_tray(blk)) {
2644 /* Ignore this command on tray-less devices */
2645 return;
2646 }
2647
Max Reitzabaaf592015-10-26 21:39:09 +01002648 if (!blk_dev_is_tray_open(blk)) {
2649 return;
2650 }
2651
Kevin Wolf39829a02017-01-24 14:21:41 +01002652 blk_dev_change_media_cb(blk, true, &local_err);
2653 if (local_err) {
2654 error_propagate(errp, local_err);
2655 return;
2656 }
Max Reitzabaaf592015-10-26 21:39:09 +01002657}
2658
Max Reitz82fcf662017-11-10 23:43:01 +01002659static void blockdev_remove_medium(bool has_device, const char *device,
2660 bool has_id, const char *id, Error **errp)
Max Reitz2814f672015-10-26 21:39:10 +01002661{
2662 BlockBackend *blk;
2663 BlockDriverState *bs;
2664 AioContext *aio_context;
Kevin Wolf00949ba2016-09-20 13:38:45 +02002665 bool has_attached_device;
Max Reitz2814f672015-10-26 21:39:10 +01002666
Kevin Wolf00949ba2016-09-20 13:38:45 +02002667 device = has_device ? device : NULL;
2668 id = has_id ? id : NULL;
2669
2670 blk = qmp_get_blk(device, id, errp);
Max Reitz2814f672015-10-26 21:39:10 +01002671 if (!blk) {
Max Reitz2814f672015-10-26 21:39:10 +01002672 return;
2673 }
2674
2675 /* For BBs without a device, we can exchange the BDS tree at will */
Kevin Wolf00949ba2016-09-20 13:38:45 +02002676 has_attached_device = blk_get_attached_dev(blk);
Max Reitz2814f672015-10-26 21:39:10 +01002677
Kevin Wolf00949ba2016-09-20 13:38:45 +02002678 if (has_attached_device && !blk_dev_has_removable_media(blk)) {
2679 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002680 return;
2681 }
2682
Kevin Wolf00949ba2016-09-20 13:38:45 +02002683 if (has_attached_device && blk_dev_has_tray(blk) &&
2684 !blk_dev_is_tray_open(blk))
2685 {
2686 error_setg(errp, "Tray of device '%s' is not open", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002687 return;
2688 }
2689
2690 bs = blk_bs(blk);
2691 if (!bs) {
2692 return;
2693 }
2694
2695 aio_context = bdrv_get_aio_context(bs);
2696 aio_context_acquire(aio_context);
2697
2698 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2699 goto out;
2700 }
2701
Max Reitz2814f672015-10-26 21:39:10 +01002702 blk_remove_bs(blk);
2703
Max Reitz12c7ec82016-01-29 20:49:11 +01002704 if (!blk_dev_has_tray(blk)) {
2705 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2706 * called at all); therefore, the medium needs to be ejected here.
2707 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2708 * value passed here (i.e. false). */
Kevin Wolf39829a02017-01-24 14:21:41 +01002709 blk_dev_change_media_cb(blk, false, &error_abort);
Max Reitz12c7ec82016-01-29 20:49:11 +01002710 }
2711
Max Reitz2814f672015-10-26 21:39:10 +01002712out:
2713 aio_context_release(aio_context);
2714}
2715
Max Reitz34ce1112017-11-10 23:43:02 +01002716void qmp_blockdev_remove_medium(const char *id, Error **errp)
Max Reitz82fcf662017-11-10 23:43:01 +01002717{
2718 blockdev_remove_medium(false, NULL, true, id, errp);
2719}
2720
Kevin Wolf716df212016-09-20 13:38:44 +02002721static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
Max Reitzd1299882015-10-26 21:39:11 +01002722 BlockDriverState *bs, Error **errp)
2723{
Kevin Wolf39829a02017-01-24 14:21:41 +01002724 Error *local_err = NULL;
Max Reitzd1299882015-10-26 21:39:11 +01002725 bool has_device;
Kevin Wolfd7086422017-01-13 19:02:32 +01002726 int ret;
Max Reitzd1299882015-10-26 21:39:11 +01002727
Max Reitzd1299882015-10-26 21:39:11 +01002728 /* For BBs without a device, we can exchange the BDS tree at will */
2729 has_device = blk_get_attached_dev(blk);
2730
2731 if (has_device && !blk_dev_has_removable_media(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002732 error_setg(errp, "Device is not removable");
Max Reitzd1299882015-10-26 21:39:11 +01002733 return;
2734 }
2735
Max Reitz12c7ec82016-01-29 20:49:11 +01002736 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002737 error_setg(errp, "Tray of the device is not open");
Max Reitzd1299882015-10-26 21:39:11 +01002738 return;
2739 }
2740
2741 if (blk_bs(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002742 error_setg(errp, "There already is a medium in the device");
Max Reitzd1299882015-10-26 21:39:11 +01002743 return;
2744 }
2745
Kevin Wolfd7086422017-01-13 19:02:32 +01002746 ret = blk_insert_bs(blk, bs, errp);
2747 if (ret < 0) {
2748 return;
2749 }
Max Reitzd1299882015-10-26 21:39:11 +01002750
Max Reitz12c7ec82016-01-29 20:49:11 +01002751 if (!blk_dev_has_tray(blk)) {
2752 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2753 * called at all); therefore, the medium needs to be pushed into the
2754 * slot here.
2755 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2756 * value passed here (i.e. true). */
Kevin Wolf39829a02017-01-24 14:21:41 +01002757 blk_dev_change_media_cb(blk, true, &local_err);
2758 if (local_err) {
2759 error_propagate(errp, local_err);
2760 blk_remove_bs(blk);
2761 return;
2762 }
Max Reitz12c7ec82016-01-29 20:49:11 +01002763 }
Max Reitzd1299882015-10-26 21:39:11 +01002764}
2765
Max Reitz82fcf662017-11-10 23:43:01 +01002766static void blockdev_insert_medium(bool has_device, const char *device,
2767 bool has_id, const char *id,
2768 const char *node_name, Error **errp)
Max Reitzd1299882015-10-26 21:39:11 +01002769{
Kevin Wolf716df212016-09-20 13:38:44 +02002770 BlockBackend *blk;
Max Reitzd1299882015-10-26 21:39:11 +01002771 BlockDriverState *bs;
2772
Kevin Wolf716df212016-09-20 13:38:44 +02002773 blk = qmp_get_blk(has_device ? device : NULL,
2774 has_id ? id : NULL,
2775 errp);
2776 if (!blk) {
2777 return;
2778 }
2779
Max Reitzd1299882015-10-26 21:39:11 +01002780 bs = bdrv_find_node(node_name);
2781 if (!bs) {
2782 error_setg(errp, "Node '%s' not found", node_name);
2783 return;
2784 }
2785
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002786 if (bdrv_has_blk(bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02002787 error_setg(errp, "Node '%s' is already in use", node_name);
Max Reitzd1299882015-10-26 21:39:11 +01002788 return;
2789 }
2790
Kevin Wolf716df212016-09-20 13:38:44 +02002791 qmp_blockdev_insert_anon_medium(blk, bs, errp);
Max Reitzd1299882015-10-26 21:39:11 +01002792}
2793
Max Reitz34ce1112017-11-10 23:43:02 +01002794void qmp_blockdev_insert_medium(const char *id, const char *node_name,
2795 Error **errp)
Max Reitz82fcf662017-11-10 23:43:01 +01002796{
2797 blockdev_insert_medium(false, NULL, true, id, node_name, errp);
2798}
2799
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002800void qmp_blockdev_change_medium(bool has_device, const char *device,
2801 bool has_id, const char *id,
2802 const char *filename,
Max Reitz24fb4132015-11-06 16:27:06 +01002803 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002804 bool has_read_only,
2805 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002806 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002807{
2808 BlockBackend *blk;
2809 BlockDriverState *medium_bs = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002810 int bdrv_flags;
Kevin Wolfb85114f2016-09-12 19:08:31 +02002811 bool detect_zeroes;
Colin Lord38a53d52016-06-08 13:56:28 -04002812 int rc;
Max Reitzde2c6c02015-10-26 21:39:13 +01002813 QDict *options = NULL;
2814 Error *err = NULL;
2815
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002816 blk = qmp_get_blk(has_device ? device : NULL,
2817 has_id ? id : NULL,
2818 errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002819 if (!blk) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002820 goto fail;
2821 }
2822
2823 if (blk_bs(blk)) {
2824 blk_update_root_state(blk);
2825 }
2826
2827 bdrv_flags = blk_get_open_flags_from_root_state(blk);
Alyssa Milburn156abc22016-02-06 13:36:18 +00002828 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
Kevin Wolfe35bdc12018-10-05 18:57:40 +02002829 BDRV_O_PROTOCOL | BDRV_O_AUTO_RDONLY);
Max Reitzde2c6c02015-10-26 21:39:13 +01002830
Max Reitz39ff43d2015-11-11 04:49:44 +01002831 if (!has_read_only) {
2832 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2833 }
2834
2835 switch (read_only) {
2836 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2837 break;
2838
2839 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2840 bdrv_flags &= ~BDRV_O_RDWR;
2841 break;
2842
2843 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2844 bdrv_flags |= BDRV_O_RDWR;
2845 break;
2846
2847 default:
2848 abort();
2849 }
2850
Kevin Wolfb85114f2016-09-12 19:08:31 +02002851 options = qdict_new();
2852 detect_zeroes = blk_get_detect_zeroes_from_root_state(blk);
Eric Blake46f5ac22017-04-27 16:58:17 -05002853 qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off");
Kevin Wolfb85114f2016-09-12 19:08:31 +02002854
Max Reitz24fb4132015-11-06 16:27:06 +01002855 if (has_format) {
Eric Blake46f5ac22017-04-27 16:58:17 -05002856 qdict_put_str(options, "driver", format);
Max Reitzde2c6c02015-10-26 21:39:13 +01002857 }
2858
Max Reitz5b363932016-05-17 16:41:31 +02002859 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2860 if (!medium_bs) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002861 goto fail;
2862 }
2863
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002864 rc = do_open_tray(has_device ? device : NULL,
2865 has_id ? id : NULL,
2866 false, &err);
Colin Lord38a53d52016-06-08 13:56:28 -04002867 if (rc && rc != -ENOSYS) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002868 error_propagate(errp, err);
2869 goto fail;
2870 }
Colin Lord38a53d52016-06-08 13:56:28 -04002871 error_free(err);
2872 err = NULL;
Max Reitzde2c6c02015-10-26 21:39:13 +01002873
Max Reitz82fcf662017-11-10 23:43:01 +01002874 blockdev_remove_medium(has_device, device, has_id, id, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002875 if (err) {
2876 error_propagate(errp, err);
2877 goto fail;
2878 }
2879
Kevin Wolf716df212016-09-20 13:38:44 +02002880 qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002881 if (err) {
2882 error_propagate(errp, err);
2883 goto fail;
2884 }
2885
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002886 qmp_blockdev_close_tray(has_device, device, has_id, id, errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002887
2888fail:
2889 /* If the medium has been inserted, the device has its own reference, so
2890 * ours must be relinquished; and if it has not been inserted successfully,
2891 * the reference must be relinquished anyway */
2892 bdrv_unref(medium_bs);
2893}
2894
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002895/* throttling disk I/O limits */
Eric Blake4dc93972016-07-13 21:50:21 -06002896void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002897{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002898 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002899 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002900 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002901 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002902
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002903 blk = qmp_get_blk(arg->has_device ? arg->device : NULL,
2904 arg->has_id ? arg->id : NULL,
2905 errp);
Fam Zhenga0e85442015-03-02 19:36:48 +08002906 if (!blk) {
Luiz Capitulino80047da2011-12-14 16:49:14 -02002907 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002908 }
Max Reitz5433c242015-10-19 17:53:29 +02002909
2910 aio_context = blk_get_aio_context(blk);
2911 aio_context_acquire(aio_context);
2912
Fam Zhenga0e85442015-03-02 19:36:48 +08002913 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002914 if (!bs) {
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002915 error_setg(errp, "Device has no medium");
Max Reitz5433c242015-10-19 17:53:29 +02002916 goto out;
2917 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002918
Alberto Garcia1588ab52016-02-18 12:27:00 +02002919 throttle_config_init(&cfg);
Eric Blake4dc93972016-07-13 21:50:21 -06002920 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2921 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2922 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002923
Eric Blake4dc93972016-07-13 21:50:21 -06002924 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2925 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2926 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
Benoît Canetcc0681c2013-09-02 14:14:39 +02002927
Eric Blake4dc93972016-07-13 21:50:21 -06002928 if (arg->has_bps_max) {
2929 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002930 }
Eric Blake4dc93972016-07-13 21:50:21 -06002931 if (arg->has_bps_rd_max) {
2932 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002933 }
Eric Blake4dc93972016-07-13 21:50:21 -06002934 if (arg->has_bps_wr_max) {
2935 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002936 }
Eric Blake4dc93972016-07-13 21:50:21 -06002937 if (arg->has_iops_max) {
2938 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002939 }
Eric Blake4dc93972016-07-13 21:50:21 -06002940 if (arg->has_iops_rd_max) {
2941 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002942 }
Eric Blake4dc93972016-07-13 21:50:21 -06002943 if (arg->has_iops_wr_max) {
2944 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002945 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002946
Eric Blake4dc93972016-07-13 21:50:21 -06002947 if (arg->has_bps_max_length) {
2948 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002949 }
Eric Blake4dc93972016-07-13 21:50:21 -06002950 if (arg->has_bps_rd_max_length) {
2951 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002952 }
Eric Blake4dc93972016-07-13 21:50:21 -06002953 if (arg->has_bps_wr_max_length) {
2954 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002955 }
Eric Blake4dc93972016-07-13 21:50:21 -06002956 if (arg->has_iops_max_length) {
2957 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002958 }
Eric Blake4dc93972016-07-13 21:50:21 -06002959 if (arg->has_iops_rd_max_length) {
2960 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002961 }
Eric Blake4dc93972016-07-13 21:50:21 -06002962 if (arg->has_iops_wr_max_length) {
2963 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002964 }
2965
Eric Blake4dc93972016-07-13 21:50:21 -06002966 if (arg->has_iops_size) {
2967 cfg.op_size = arg->iops_size;
Benoît Canet2024c1d2013-09-02 14:14:41 +02002968 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002969
Alberto Garciad5851082016-02-18 12:26:59 +02002970 if (!throttle_is_valid(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002971 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002972 }
2973
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002974 if (throttle_enabled(&cfg)) {
2975 /* Enable I/O limits if they're not enabled yet, otherwise
2976 * just update the throttling group. */
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +03002977 if (!blk_get_public(blk)->throttle_group_member.throttle_state) {
Eric Blake4dc93972016-07-13 21:50:21 -06002978 blk_io_limits_enable(blk,
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002979 arg->has_group ? arg->group :
2980 arg->has_device ? arg->device :
2981 arg->id);
Eric Blake4dc93972016-07-13 21:50:21 -06002982 } else if (arg->has_group) {
2983 blk_io_limits_update_group(blk, arg->group);
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002984 }
2985 /* Set the new throttling configuration */
Kevin Wolf97148072016-03-21 13:53:52 +01002986 blk_set_io_limits(blk, &cfg);
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +03002987 } else if (blk_get_public(blk)->throttle_group_member.throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002988 /* If all throttling settings are set to 0, disable I/O limits */
Kevin Wolf97148072016-03-21 13:53:52 +01002989 blk_io_limits_disable(blk);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002990 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002991
Max Reitz5433c242015-10-19 17:53:29 +02002992out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002993 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002994}
2995
John Snow341ebc22015-04-17 19:49:52 -04002996void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2997 bool has_granularity, uint32_t granularity,
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002998 bool has_persistent, bool persistent,
Vladimir Sementsov-Ogievskiya6e2ca52018-06-11 14:53:32 -04002999 bool has_disabled, bool disabled,
John Snow341ebc22015-04-17 19:49:52 -04003000 Error **errp)
3001{
John Snow341ebc22015-04-17 19:49:52 -04003002 BlockDriverState *bs;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03003003 BdrvDirtyBitmap *bitmap;
Sergio Lopez91005a42020-01-08 15:31:36 +01003004 AioContext *aio_context;
John Snow341ebc22015-04-17 19:49:52 -04003005
3006 if (!name || name[0] == '\0') {
3007 error_setg(errp, "Bitmap name cannot be empty");
3008 return;
3009 }
3010
3011 bs = bdrv_lookup_bs(node, node, errp);
3012 if (!bs) {
3013 return;
3014 }
3015
Sergio Lopez91005a42020-01-08 15:31:36 +01003016 aio_context = bdrv_get_aio_context(bs);
3017 aio_context_acquire(aio_context);
3018
John Snow341ebc22015-04-17 19:49:52 -04003019 if (has_granularity) {
3020 if (granularity < 512 || !is_power_of_2(granularity)) {
3021 error_setg(errp, "Granularity must be power of 2 "
3022 "and at least 512");
Sergio Lopez91005a42020-01-08 15:31:36 +01003023 goto out;
John Snow341ebc22015-04-17 19:49:52 -04003024 }
3025 } else {
3026 /* Default to cluster size, if available: */
3027 granularity = bdrv_get_default_bitmap_granularity(bs);
3028 }
3029
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03003030 if (!has_persistent) {
3031 persistent = false;
3032 }
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03003033
Vladimir Sementsov-Ogievskiya6e2ca52018-06-11 14:53:32 -04003034 if (!has_disabled) {
3035 disabled = false;
3036 }
3037
Vladimir Sementsov-Ogievskiyd2c30802019-09-20 11:25:43 +03003038 if (persistent &&
3039 !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp))
3040 {
Sergio Lopez91005a42020-01-08 15:31:36 +01003041 goto out;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03003042 }
3043
3044 bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp);
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03003045 if (bitmap == NULL) {
Sergio Lopez91005a42020-01-08 15:31:36 +01003046 goto out;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03003047 }
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03003048
Vladimir Sementsov-Ogievskiya6e2ca52018-06-11 14:53:32 -04003049 if (disabled) {
3050 bdrv_disable_dirty_bitmap(bitmap);
3051 }
3052
Eric Blake796a3792019-03-12 12:05:49 -04003053 bdrv_dirty_bitmap_set_persistence(bitmap, persistent);
Sergio Lopez91005a42020-01-08 15:31:36 +01003054
3055out:
3056 aio_context_release(aio_context);
John Snow341ebc22015-04-17 19:49:52 -04003057}
3058
John Snowc4e4b0f2019-07-29 16:35:54 -04003059static BdrvDirtyBitmap *do_block_dirty_bitmap_remove(
3060 const char *node, const char *name, bool release,
3061 BlockDriverState **bitmap_bs, Error **errp)
John Snow341ebc22015-04-17 19:49:52 -04003062{
John Snow341ebc22015-04-17 19:49:52 -04003063 BlockDriverState *bs;
3064 BdrvDirtyBitmap *bitmap;
Sergio Lopez91005a42020-01-08 15:31:36 +01003065 AioContext *aio_context;
John Snow341ebc22015-04-17 19:49:52 -04003066
Paolo Bonzini21198822017-06-05 14:39:03 +02003067 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
John Snow341ebc22015-04-17 19:49:52 -04003068 if (!bitmap || !bs) {
John Snowc4e4b0f2019-07-29 16:35:54 -04003069 return NULL;
John Snow341ebc22015-04-17 19:49:52 -04003070 }
3071
Sergio Lopez91005a42020-01-08 15:31:36 +01003072 aio_context = bdrv_get_aio_context(bs);
3073 aio_context_acquire(aio_context);
3074
John Snowc3edf132019-03-12 12:05:49 -04003075 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY | BDRV_BITMAP_RO,
3076 errp)) {
Sergio Lopez91005a42020-01-08 15:31:36 +01003077 aio_context_release(aio_context);
John Snowc4e4b0f2019-07-29 16:35:54 -04003078 return NULL;
John Snow9bd2b082015-04-17 19:49:57 -04003079 }
Vladimir Sementsov-Ogievskiy5c36c1a2017-06-28 15:05:29 +03003080
Vladimir Sementsov-Ogievskiyd2c30802019-09-20 11:25:43 +03003081 if (bdrv_dirty_bitmap_get_persistence(bitmap) &&
3082 bdrv_remove_persistent_dirty_bitmap(bs, name, errp) < 0)
3083 {
Sergio Lopez91005a42020-01-08 15:31:36 +01003084 aio_context_release(aio_context);
3085 return NULL;
Vladimir Sementsov-Ogievskiy5c36c1a2017-06-28 15:05:29 +03003086 }
3087
John Snowc4e4b0f2019-07-29 16:35:54 -04003088 if (release) {
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +03003089 bdrv_release_dirty_bitmap(bitmap);
John Snowc4e4b0f2019-07-29 16:35:54 -04003090 }
3091
3092 if (bitmap_bs) {
3093 *bitmap_bs = bs;
3094 }
3095
Sergio Lopez91005a42020-01-08 15:31:36 +01003096 aio_context_release(aio_context);
John Snowc4e4b0f2019-07-29 16:35:54 -04003097 return release ? NULL : bitmap;
3098}
3099
3100void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
3101 Error **errp)
3102{
3103 do_block_dirty_bitmap_remove(node, name, true, NULL, errp);
John Snow341ebc22015-04-17 19:49:52 -04003104}
3105
John Snowe74e6b72015-04-17 19:49:59 -04003106/**
3107 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
3108 * immediately after a full backup operation.
3109 */
3110void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
3111 Error **errp)
3112{
John Snowe74e6b72015-04-17 19:49:59 -04003113 BdrvDirtyBitmap *bitmap;
3114 BlockDriverState *bs;
3115
Paolo Bonzini21198822017-06-05 14:39:03 +02003116 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
John Snowe74e6b72015-04-17 19:49:59 -04003117 if (!bitmap || !bs) {
3118 return;
3119 }
3120
John Snow3ae96d62019-03-12 12:05:49 -04003121 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) {
Vladimir Sementsov-Ogievskiyd6883bc2017-06-28 15:05:10 +03003122 return;
John Snowe74e6b72015-04-17 19:49:59 -04003123 }
3124
Fam Zhengdf9a6812015-11-09 18:16:54 +08003125 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04003126}
3127
John Snow0e2b7f02018-12-21 04:35:22 -05003128void qmp_block_dirty_bitmap_enable(const char *node, const char *name,
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04003129 Error **errp)
3130{
3131 BlockDriverState *bs;
3132 BdrvDirtyBitmap *bitmap;
3133
3134 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
3135 if (!bitmap) {
3136 return;
3137 }
3138
John Snow3ae96d62019-03-12 12:05:49 -04003139 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04003140 return;
3141 }
3142
3143 bdrv_enable_dirty_bitmap(bitmap);
3144}
3145
John Snow0e2b7f02018-12-21 04:35:22 -05003146void qmp_block_dirty_bitmap_disable(const char *node, const char *name,
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04003147 Error **errp)
3148{
3149 BlockDriverState *bs;
3150 BdrvDirtyBitmap *bitmap;
3151
3152 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
3153 if (!bitmap) {
3154 return;
3155 }
3156
John Snow3ae96d62019-03-12 12:05:49 -04003157 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04003158 return;
3159 }
3160
3161 bdrv_disable_dirty_bitmap(bitmap);
3162}
3163
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04003164static BdrvDirtyBitmap *do_block_dirty_bitmap_merge(
3165 const char *node, const char *target,
3166 BlockDirtyBitmapMergeSourceList *bitmaps,
3167 HBitmap **backup, Error **errp)
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003168{
3169 BlockDriverState *bs;
John Snow360d4e42018-12-21 04:35:21 -05003170 BdrvDirtyBitmap *dst, *src, *anon;
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04003171 BlockDirtyBitmapMergeSourceList *lst;
John Snow360d4e42018-12-21 04:35:21 -05003172 Error *local_err = NULL;
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003173
John Snow360d4e42018-12-21 04:35:21 -05003174 dst = block_dirty_bitmap_lookup(node, target, &bs, errp);
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003175 if (!dst) {
John Snow360d4e42018-12-21 04:35:21 -05003176 return NULL;
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003177 }
3178
John Snow360d4e42018-12-21 04:35:21 -05003179 anon = bdrv_create_dirty_bitmap(bs, bdrv_dirty_bitmap_granularity(dst),
3180 NULL, errp);
3181 if (!anon) {
3182 return NULL;
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003183 }
3184
John Snow360d4e42018-12-21 04:35:21 -05003185 for (lst = bitmaps; lst; lst = lst->next) {
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04003186 switch (lst->value->type) {
3187 const char *name, *node;
3188 case QTYPE_QSTRING:
3189 name = lst->value->u.local;
3190 src = bdrv_find_dirty_bitmap(bs, name);
3191 if (!src) {
3192 error_setg(errp, "Dirty bitmap '%s' not found", name);
3193 dst = NULL;
3194 goto out;
3195 }
3196 break;
3197 case QTYPE_QDICT:
3198 node = lst->value->u.external.node;
3199 name = lst->value->u.external.name;
3200 src = block_dirty_bitmap_lookup(node, name, NULL, errp);
3201 if (!src) {
3202 dst = NULL;
3203 goto out;
3204 }
3205 break;
3206 default:
3207 abort();
John Snow360d4e42018-12-21 04:35:21 -05003208 }
3209
3210 bdrv_merge_dirty_bitmap(anon, src, NULL, &local_err);
3211 if (local_err) {
3212 error_propagate(errp, local_err);
3213 dst = NULL;
3214 goto out;
3215 }
3216 }
3217
3218 /* Merge into dst; dst is unchanged on failure. */
3219 bdrv_merge_dirty_bitmap(dst, anon, backup, errp);
3220
3221 out:
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +03003222 bdrv_release_dirty_bitmap(anon);
John Snow360d4e42018-12-21 04:35:21 -05003223 return dst;
3224}
3225
John Snow0e2b7f02018-12-21 04:35:22 -05003226void qmp_block_dirty_bitmap_merge(const char *node, const char *target,
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04003227 BlockDirtyBitmapMergeSourceList *bitmaps,
3228 Error **errp)
John Snow360d4e42018-12-21 04:35:21 -05003229{
3230 do_block_dirty_bitmap_merge(node, target, bitmaps, NULL, errp);
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003231}
3232
Vladimir Sementsov-Ogievskiya3b52532017-06-28 15:05:25 +03003233BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
3234 const char *name,
3235 Error **errp)
3236{
3237 BdrvDirtyBitmap *bitmap;
3238 BlockDriverState *bs;
3239 BlockDirtyBitmapSha256 *ret = NULL;
3240 char *sha256;
3241
3242 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
3243 if (!bitmap || !bs) {
3244 return NULL;
3245 }
3246
3247 sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp);
3248 if (sha256 == NULL) {
3249 return NULL;
3250 }
3251
3252 ret = g_new(BlockDirtyBitmapSha256, 1);
3253 ret->sha256 = sha256;
3254
3255 return ret;
3256}
3257
Markus Armbruster072ebe62015-03-05 17:00:56 +01003258void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06003259{
3260 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003261 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06003262 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003263 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08003264 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06003265
Kevin Wolf2073d412016-02-23 17:50:37 +01003266 bs = bdrv_find_node(id);
3267 if (bs) {
Markus Armbruster79b7a772017-03-21 17:53:28 +01003268 qmp_blockdev_del(id, &local_err);
Kevin Wolf2073d412016-02-23 17:50:37 +01003269 if (local_err) {
3270 error_report_err(local_err);
3271 }
3272 return;
3273 }
3274
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003275 blk = blk_by_name(id);
3276 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02003277 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01003278 return;
Ryan Harper9063f812010-11-12 11:07:13 -06003279 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003280
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02003281 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02003282 error_report("Deleting device added with blockdev-add"
3283 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01003284 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02003285 }
3286
Max Reitz5433c242015-10-19 17:53:29 +02003287 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003288 aio_context_acquire(aio_context);
3289
Max Reitz5433c242015-10-19 17:53:29 +02003290 bs = blk_bs(blk);
3291 if (bs) {
3292 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
3293 error_report_err(local_err);
3294 aio_context_release(aio_context);
3295 return;
3296 }
Ryan Harper9063f812010-11-12 11:07:13 -06003297
Max Reitz938abd42016-01-29 16:36:09 +01003298 blk_remove_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02003299 }
Ryan Harper9063f812010-11-12 11:07:13 -06003300
Max Reitz7c735872016-03-16 19:54:39 +01003301 /* Make the BlockBackend and the attached BlockDriverState anonymous */
Max Reitzefaa7c42016-03-16 19:54:38 +01003302 monitor_remove_blk(blk);
3303
Max Reitz7c735872016-03-16 19:54:39 +01003304 /* If this BlockBackend has a device attached to it, its refcount will be
3305 * decremented when the device is removed; otherwise we have to do so here.
Ryan Harperd22b2f42011-03-29 20:51:47 -05003306 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02003307 if (blk_get_attached_dev(blk)) {
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02003308 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02003309 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
3310 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05003311 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02003312 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06003313 }
3314
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003315 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06003316}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003317
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003318void qmp_block_resize(bool has_device, const char *device,
3319 bool has_node_name, const char *node_name,
3320 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003321{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003322 Error *local_err = NULL;
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01003323 BlockBackend *blk = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003324 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003325 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02003326 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003327
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003328 bs = bdrv_lookup_bs(has_device ? device : NULL,
3329 has_node_name ? node_name : NULL,
3330 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003331 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003332 error_propagate(errp, local_err);
3333 return;
3334 }
3335
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003336 aio_context = bdrv_get_aio_context(bs);
3337 aio_context_acquire(aio_context);
3338
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003339 if (!bdrv_is_first_non_filter(bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003340 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003341 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003342 }
3343
3344 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003345 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003346 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003347 }
3348
Jeff Cody9c75e162014-06-25 16:55:30 -04003349 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003350 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003351 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04003352 }
3353
Kevin Wolfd861ab32019-04-25 14:25:10 +02003354 blk = blk_new(bdrv_get_aio_context(bs), BLK_PERM_RESIZE, BLK_PERM_ALL);
Kevin Wolfd7086422017-01-13 19:02:32 +01003355 ret = blk_insert_bs(blk, bs, errp);
3356 if (ret < 0) {
3357 goto out;
3358 }
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01003359
John Snow698bdfa2017-05-10 13:39:45 -04003360 bdrv_drained_begin(bs);
Max Reitzc80d8b02019-09-18 11:51:40 +02003361 ret = blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp);
John Snow698bdfa2017-05-10 13:39:45 -04003362 bdrv_drained_end(bs);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003363
3364out:
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01003365 blk_unref(blk);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003366 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003367}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003368
Alberto Garcia23233222016-07-05 17:28:59 +03003369void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04003370 bool has_base, const char *base,
Alberto Garcia312fe092016-10-28 10:08:19 +03003371 bool has_base_node, const char *base_node,
Jeff Cody13d8cc52014-06-25 15:40:11 -04003372 bool has_backing_file, const char *backing_file,
3373 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02003374 bool has_on_error, BlockdevOnError on_error,
John Snow241ca1a2018-09-06 09:02:23 -04003375 bool has_auto_finalize, bool auto_finalize,
3376 bool has_auto_dismiss, bool auto_dismiss,
Paolo Bonzini1d809092012-09-28 17:22:59 +02003377 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003378{
Alberto Garcia554b6142016-10-28 10:08:11 +03003379 BlockDriverState *bs, *iter;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003380 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003381 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003382 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003383 const char *base_name = NULL;
John Snowcf6320d2018-09-06 09:02:12 -04003384 int job_flags = JOB_DEFAULT;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003385
Paolo Bonzini1d809092012-09-28 17:22:59 +02003386 if (!has_on_error) {
3387 on_error = BLOCKDEV_ON_ERROR_REPORT;
3388 }
3389
Alberto Garcia554b6142016-10-28 10:08:11 +03003390 bs = bdrv_lookup_bs(device, device, errp);
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003391 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003392 return;
3393 }
3394
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003395 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003396 aio_context_acquire(aio_context);
3397
Alberto Garcia312fe092016-10-28 10:08:19 +03003398 if (has_base && has_base_node) {
3399 error_setg(errp, "'base' and 'base-node' cannot be specified "
3400 "at the same time");
3401 goto out;
3402 }
3403
Jeff Cody13d8cc52014-06-25 15:40:11 -04003404 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003405 base_bs = bdrv_find_backing_image(bs, base);
3406 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003407 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003408 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003409 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003410 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04003411 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003412 }
3413
Alberto Garcia312fe092016-10-28 10:08:19 +03003414 if (has_base_node) {
3415 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
3416 if (!base_bs) {
3417 goto out;
3418 }
3419 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
3420 error_setg(errp, "Node '%s' is not a backing image of '%s'",
3421 base_node, device);
3422 goto out;
3423 }
3424 assert(bdrv_get_aio_context(base_bs) == aio_context);
Max Reitzf30c66b2019-02-01 20:29:05 +01003425 bdrv_refresh_filename(base_bs);
Alberto Garcia312fe092016-10-28 10:08:19 +03003426 base_name = base_bs->filename;
3427 }
3428
Alberto Garcia554b6142016-10-28 10:08:11 +03003429 /* Check for op blockers in the whole chain between bs and base */
3430 for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) {
3431 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
3432 goto out;
3433 }
3434 }
3435
Jeff Cody13d8cc52014-06-25 15:40:11 -04003436 /* if we are streaming the entire chain, the result will have no backing
3437 * file, and specifying one is therefore an error */
3438 if (base_bs == NULL && has_backing_file) {
3439 error_setg(errp, "backing file specified, but streaming the "
3440 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003441 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003442 }
3443
3444 /* backing_file string overrides base bs filename */
3445 base_name = has_backing_file ? backing_file : base_name;
3446
John Snow241ca1a2018-09-06 09:02:23 -04003447 if (has_auto_finalize && !auto_finalize) {
3448 job_flags |= JOB_MANUAL_FINALIZE;
3449 }
3450 if (has_auto_dismiss && !auto_dismiss) {
3451 job_flags |= JOB_MANUAL_DISMISS;
3452 }
3453
Alberto Garcia23233222016-07-05 17:28:59 +03003454 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
John Snowcf6320d2018-09-06 09:02:12 -04003455 job_flags, has_speed ? speed : 0, on_error, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003456 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003457 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003458 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003459 }
3460
Vladimir Sementsov-Ogievskiyb23c5802019-06-06 18:41:32 +03003461 trace_qmp_block_stream(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003462
3463out:
3464 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003465}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003466
Alberto Garciafd62c602016-07-05 17:29:00 +03003467void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Kevin Wolf3c605f42017-06-27 17:18:20 +02003468 bool has_base_node, const char *base_node,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003469 bool has_base, const char *base,
Kevin Wolf3c605f42017-06-27 17:18:20 +02003470 bool has_top_node, const char *top_node,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003471 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003472 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04003473 bool has_speed, int64_t speed,
Kevin Wolf8faad1c2020-02-14 21:08:11 +01003474 bool has_on_error, BlockdevOnError on_error,
Kevin Wolf0db832f2017-02-20 18:10:05 +01003475 bool has_filter_node_name, const char *filter_node_name,
John Snow96fbf532018-09-06 09:02:21 -04003476 bool has_auto_finalize, bool auto_finalize,
3477 bool has_auto_dismiss, bool auto_dismiss,
Jeff Codyed61fc12012-09-27 13:29:16 -04003478 Error **errp)
3479{
3480 BlockDriverState *bs;
Alberto Garcia058223a2016-10-28 10:08:07 +03003481 BlockDriverState *iter;
Jeff Codyed61fc12012-09-27 13:29:16 -04003482 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003483 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04003484 Error *local_err = NULL;
John Snow53607822018-09-06 09:02:10 -04003485 int job_flags = JOB_DEFAULT;
Jeff Codyed61fc12012-09-27 13:29:16 -04003486
Max Reitz54504662014-04-10 19:36:25 +02003487 if (!has_speed) {
3488 speed = 0;
3489 }
Kevin Wolf8faad1c2020-02-14 21:08:11 +01003490 if (!has_on_error) {
3491 on_error = BLOCKDEV_ON_ERROR_REPORT;
3492 }
Kevin Wolf0db832f2017-02-20 18:10:05 +01003493 if (!has_filter_node_name) {
3494 filter_node_name = NULL;
3495 }
John Snow96fbf532018-09-06 09:02:21 -04003496 if (has_auto_finalize && !auto_finalize) {
3497 job_flags |= JOB_MANUAL_FINALIZE;
3498 }
3499 if (has_auto_dismiss && !auto_dismiss) {
3500 job_flags |= JOB_MANUAL_DISMISS;
3501 }
Max Reitz54504662014-04-10 19:36:25 +02003502
Jeff Cody7676e2c2014-06-30 15:14:15 +02003503 /* Important Note:
3504 * libvirt relies on the DeviceNotFound error class in order to probe for
3505 * live commit feature versions; for this to work, we must make sure to
3506 * perform the device lookup before any generic errors that may occur in a
3507 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02003508 bs = qmp_get_root_bs(device, &local_err);
3509 if (!bs) {
3510 bs = bdrv_lookup_bs(device, device, NULL);
3511 if (!bs) {
3512 error_free(local_err);
3513 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3514 "Device '%s' not found", device);
3515 } else {
3516 error_propagate(errp, local_err);
3517 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003518 return;
3519 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003520
Kevin Wolf1d13b162016-06-23 14:20:24 +02003521 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003522 aio_context_acquire(aio_context);
3523
Fam Zhengbb000212014-09-11 13:14:00 +08003524 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003525 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003526 }
3527
Jeff Codyed61fc12012-09-27 13:29:16 -04003528 /* default top_bs is the active layer */
3529 top_bs = bs;
3530
Kevin Wolf3c605f42017-06-27 17:18:20 +02003531 if (has_top_node && has_top) {
3532 error_setg(errp, "'top-node' and 'top' are mutually exclusive");
3533 goto out;
3534 } else if (has_top_node) {
3535 top_bs = bdrv_lookup_bs(NULL, top_node, errp);
3536 if (top_bs == NULL) {
3537 goto out;
3538 }
3539 if (!bdrv_chain_contains(bs, top_bs)) {
3540 error_setg(errp, "'%s' is not in this backing file chain",
3541 top_node);
3542 goto out;
3543 }
3544 } else if (has_top && top) {
Max Reitzf30c66b2019-02-01 20:29:05 +01003545 /* This strcmp() is just a shortcut, there is no need to
3546 * refresh @bs's filename. If it mismatches,
3547 * bdrv_find_backing_image() will do the refresh and may still
3548 * return @bs. */
Jeff Codyed61fc12012-09-27 13:29:16 -04003549 if (strcmp(bs->filename, top) != 0) {
3550 top_bs = bdrv_find_backing_image(bs, top);
3551 }
3552 }
3553
3554 if (top_bs == NULL) {
3555 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003556 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003557 }
3558
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003559 assert(bdrv_get_aio_context(top_bs) == aio_context);
3560
Kevin Wolf3c605f42017-06-27 17:18:20 +02003561 if (has_base_node && has_base) {
3562 error_setg(errp, "'base-node' and 'base' are mutually exclusive");
3563 goto out;
3564 } else if (has_base_node) {
3565 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
3566 if (base_bs == NULL) {
3567 goto out;
3568 }
3569 if (!bdrv_chain_contains(top_bs, base_bs)) {
3570 error_setg(errp, "'%s' is not in this backing file chain",
3571 base_node);
3572 goto out;
3573 }
3574 } else if (has_base && base) {
Jeff Codyd5208c42012-10-16 15:49:10 -04003575 base_bs = bdrv_find_backing_image(top_bs, base);
3576 } else {
3577 base_bs = bdrv_find_base(top_bs);
3578 }
3579
3580 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003581 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003582 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04003583 }
3584
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003585 assert(bdrv_get_aio_context(base_bs) == aio_context);
3586
Alberto Garcia058223a2016-10-28 10:08:07 +03003587 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
3588 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3589 goto out;
3590 }
Fam Zhengbb000212014-09-11 13:14:00 +08003591 }
3592
Jeff Cody7676e2c2014-06-30 15:14:15 +02003593 /* Do not allow attempts to commit an image into itself */
3594 if (top_bs == base_bs) {
3595 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003596 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02003597 }
3598
Fam Zheng20a63d22013-12-16 14:45:31 +08003599 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04003600 if (has_backing_file) {
3601 error_setg(errp, "'backing-file' specified,"
3602 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003603 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003604 }
John Snow47970df2016-10-27 12:06:57 -04003605 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
John Snow53607822018-09-06 09:02:10 -04003606 job_flags, speed, on_error,
Fam Zheng78bbd912017-04-21 20:27:04 +08003607 filter_node_name, NULL, NULL, false, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003608 } else {
Alberto Garcia058223a2016-10-28 10:08:07 +03003609 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
3610 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3611 goto out;
3612 }
John Snow53607822018-09-06 09:02:10 -04003613 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags,
3614 speed, on_error, has_backing_file ? backing_file : NULL,
Kevin Wolf0db832f2017-02-20 18:10:05 +01003615 filter_node_name, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003616 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003617 if (local_err != NULL) {
3618 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003619 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003620 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003621
3622out:
3623 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003624}
3625
John Snow7b0b8702019-07-29 16:35:52 -04003626/* Common QMP interface for drive-backup and blockdev-backup */
3627static BlockJob *do_backup_common(BackupCommon *backup,
3628 BlockDriverState *bs,
3629 BlockDriverState *target_bs,
3630 AioContext *aio_context,
3631 JobTxn *txn, Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003632{
John Snow111049a2016-11-08 01:50:38 -05003633 BlockJob *job = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003634 BdrvDirtyBitmap *bmap = NULL;
John Snow7b0b8702019-07-29 16:35:52 -04003635 int job_flags = JOB_DEFAULT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003636
Pavel Butsykin81206a82016-07-22 11:17:50 +03003637 if (!backup->has_speed) {
3638 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003639 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003640 if (!backup->has_on_source_error) {
3641 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003642 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003643 if (!backup->has_on_target_error) {
3644 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003645 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003646 if (!backup->has_job_id) {
3647 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003648 }
John Snowb40dacd2018-03-10 03:27:44 -05003649 if (!backup->has_auto_finalize) {
3650 backup->auto_finalize = true;
3651 }
3652 if (!backup->has_auto_dismiss) {
3653 backup->auto_dismiss = true;
3654 }
Pavel Butsykin13b94142016-07-22 11:17:52 +03003655 if (!backup->has_compress) {
3656 backup->compress = false;
3657 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003658
John Snowa6c93652019-07-29 16:35:55 -04003659 if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) ||
3660 (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) {
3661 /* done before desugaring 'incremental' to print the right message */
3662 if (!backup->has_bitmap) {
3663 error_setg(errp, "must provide a valid bitmap name for "
3664 "'%s' sync mode", MirrorSyncMode_str(backup->sync));
3665 return NULL;
3666 }
3667 }
3668
John Snowc8b56502019-07-29 16:35:52 -04003669 if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) {
3670 if (backup->has_bitmap_mode &&
3671 backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) {
3672 error_setg(errp, "Bitmap sync mode must be '%s' "
3673 "when using sync mode '%s'",
3674 BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS),
3675 MirrorSyncMode_str(backup->sync));
3676 return NULL;
3677 }
3678 backup->has_bitmap_mode = true;
3679 backup->sync = MIRROR_SYNC_MODE_BITMAP;
3680 backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS;
3681 }
3682
John Snow7b0b8702019-07-29 16:35:52 -04003683 if (backup->has_bitmap) {
3684 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
3685 if (!bmap) {
3686 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
3687 return NULL;
3688 }
John Snowc8b56502019-07-29 16:35:52 -04003689 if (!backup->has_bitmap_mode) {
3690 error_setg(errp, "Bitmap sync mode must be given "
3691 "when providing a bitmap");
3692 return NULL;
3693 }
John Snowb30ffbe2019-07-29 16:35:54 -04003694 if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snow7b0b8702019-07-29 16:35:52 -04003695 return NULL;
3696 }
John Snow1a2b8b42019-07-29 16:35:55 -04003697
3698 /* This does not produce a useful bitmap artifact: */
3699 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
3700 error_setg(errp, "sync mode '%s' does not produce meaningful bitmap"
3701 " outputs", MirrorSyncMode_str(backup->sync));
3702 return NULL;
3703 }
3704
3705 /* If the bitmap isn't used for input or output, this is useless: */
3706 if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER &&
3707 backup->sync != MIRROR_SYNC_MODE_BITMAP) {
3708 error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect"
3709 " when combined with sync mode '%s'",
3710 BitmapSyncMode_str(backup->bitmap_mode),
3711 MirrorSyncMode_str(backup->sync));
3712 return NULL;
3713 }
3714 }
3715
3716 if (!backup->has_bitmap && backup->has_bitmap_mode) {
3717 error_setg(errp, "Cannot specify bitmap sync mode without a bitmap");
3718 return NULL;
John Snow7b0b8702019-07-29 16:35:52 -04003719 }
3720
3721 if (!backup->auto_finalize) {
3722 job_flags |= JOB_MANUAL_FINALIZE;
3723 }
3724 if (!backup->auto_dismiss) {
3725 job_flags |= JOB_MANUAL_DISMISS;
3726 }
3727
3728 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
John Snowc8b56502019-07-29 16:35:52 -04003729 backup->sync, bmap, backup->bitmap_mode,
3730 backup->compress,
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +03003731 backup->filter_node_name,
John Snowc8b56502019-07-29 16:35:52 -04003732 backup->on_source_error,
3733 backup->on_target_error,
John Snow7b0b8702019-07-29 16:35:52 -04003734 job_flags, NULL, NULL, txn, errp);
3735 return job;
3736}
3737
Sergio Lopez2288ccf2020-01-08 15:31:32 +01003738void qmp_drive_backup(DriveBackup *backup, Error **errp)
John Snow7b0b8702019-07-29 16:35:52 -04003739{
Sergio Lopez2288ccf2020-01-08 15:31:32 +01003740 TransactionAction action = {
3741 .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP,
3742 .u.drive_backup.data = backup,
3743 };
3744 blockdev_do_action(&action, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003745}
3746
Benoît Canetc13163f2014-01-23 21:31:34 +01003747BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3748{
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003749 return bdrv_named_nodes_list(errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003750}
3751
Vladimir Sementsov-Ogievskiy5d3b4e92018-12-21 20:09:07 +03003752XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp)
3753{
3754 return bdrv_get_xdbg_block_graph(errp);
3755}
3756
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01003757void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003758{
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01003759 TransactionAction action = {
3760 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP,
3761 .u.blockdev_backup.data = backup,
3762 };
3763 blockdev_do_action(&action, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003764}
3765
Fam Zheng4193cdd2015-12-24 12:45:03 +08003766/* Parameter check and block job starting for drive mirroring.
3767 * Caller should hold @device and @target's aio context (must be the same).
3768 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003769static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003770 BlockDriverState *target,
3771 bool has_replaces, const char *replaces,
3772 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003773 BlockMirrorBackingMode backing_mode,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003774 bool zero_target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003775 bool has_speed, int64_t speed,
3776 bool has_granularity, uint32_t granularity,
3777 bool has_buf_size, int64_t buf_size,
3778 bool has_on_source_error,
3779 BlockdevOnError on_source_error,
3780 bool has_on_target_error,
3781 BlockdevOnError on_target_error,
3782 bool has_unmap, bool unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003783 bool has_filter_node_name,
3784 const char *filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003785 bool has_copy_mode, MirrorCopyMode copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04003786 bool has_auto_finalize, bool auto_finalize,
3787 bool has_auto_dismiss, bool auto_dismiss,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003788 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003789{
John Snowa1999b32018-09-06 09:02:11 -04003790 int job_flags = JOB_DEFAULT;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003791
3792 if (!has_speed) {
3793 speed = 0;
3794 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003795 if (!has_on_source_error) {
3796 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3797 }
3798 if (!has_on_target_error) {
3799 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3800 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003801 if (!has_granularity) {
3802 granularity = 0;
3803 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003804 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003805 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003806 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003807 if (!has_unmap) {
3808 unmap = true;
3809 }
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003810 if (!has_filter_node_name) {
3811 filter_node_name = NULL;
3812 }
Max Reitz481deba2018-06-13 20:18:22 +02003813 if (!has_copy_mode) {
3814 copy_mode = MIRROR_COPY_MODE_BACKGROUND;
3815 }
John Snowa6b58ad2018-09-06 09:02:22 -04003816 if (has_auto_finalize && !auto_finalize) {
3817 job_flags |= JOB_MANUAL_FINALIZE;
3818 }
3819 if (has_auto_dismiss && !auto_dismiss) {
3820 job_flags |= JOB_MANUAL_DISMISS;
3821 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003822
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003823 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003824 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3825 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003826 return;
3827 }
3828 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003829 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3830 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003831 return;
3832 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003833
Fam Zheng4193cdd2015-12-24 12:45:03 +08003834 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3835 return;
3836 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003837 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3838 return;
3839 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003840
3841 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3842 sync = MIRROR_SYNC_MODE_FULL;
3843 }
3844
Max Reitz74ce9e42019-02-13 23:53:01 +01003845 if (has_replaces) {
3846 BlockDriverState *to_replace_bs;
3847 AioContext *replace_aio_context;
3848 int64_t bs_size, replace_size;
3849
3850 bs_size = bdrv_getlength(bs);
3851 if (bs_size < 0) {
3852 error_setg_errno(errp, -bs_size, "Failed to query device's size");
3853 return;
3854 }
3855
3856 to_replace_bs = check_to_replace_node(bs, replaces, errp);
3857 if (!to_replace_bs) {
3858 return;
3859 }
3860
3861 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3862 aio_context_acquire(replace_aio_context);
3863 replace_size = bdrv_getlength(to_replace_bs);
3864 aio_context_release(replace_aio_context);
3865
3866 if (replace_size < 0) {
3867 error_setg_errno(errp, -replace_size,
3868 "Failed to query the replacement node's size");
3869 return;
3870 }
3871 if (bs_size != replace_size) {
3872 error_setg(errp, "cannot replace image with a mirror image of "
3873 "different size");
3874 return;
3875 }
3876 }
3877
Fam Zheng4193cdd2015-12-24 12:45:03 +08003878 /* pass the node name to replace to mirror start since it's loose coupling
3879 * and will allow to check whether the node still exist at mirror completion
3880 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003881 mirror_start(job_id, bs, target,
John Snowa1999b32018-09-06 09:02:11 -04003882 has_replaces ? replaces : NULL, job_flags,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003883 speed, granularity, buf_size, sync, backing_mode, zero_target,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003884 on_source_error, on_target_error, unmap, filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003885 copy_mode, errp);
Fam Zheng4193cdd2015-12-24 12:45:03 +08003886}
3887
Eric Blakefaecd402016-07-14 16:37:58 -06003888void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003889{
3890 BlockDriverState *bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003891 BlockDriverState *source, *target_bs;
3892 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003893 AioContext *old_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003894 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003895 Error *local_err = NULL;
3896 QDict *options = NULL;
3897 int flags;
3898 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003899 const char *format = arg->format;
Max Reitzcdf3bc92019-07-24 19:12:30 +02003900 bool zero_target;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003901 int ret;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003902
Kevin Wolf0524e932016-06-23 14:20:24 +02003903 bs = qmp_get_root_bs(arg->device, errp);
3904 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003905 return;
3906 }
3907
Paolo Bonzinicb2af912018-02-07 17:29:20 +01003908 /* Early check to avoid creating target */
3909 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3910 return;
3911 }
3912
Kevin Wolf0524e932016-06-23 14:20:24 +02003913 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003914 aio_context_acquire(aio_context);
3915
Eric Blakefaecd402016-07-14 16:37:58 -06003916 if (!arg->has_mode) {
3917 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003918 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003919
Eric Blakefaecd402016-07-14 16:37:58 -06003920 if (!arg->has_format) {
3921 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3922 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003923 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003924
Kevin Wolf61de4c62016-03-18 17:46:45 +01003925 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003926 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003927 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3928 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003929 }
Eric Blakefaecd402016-07-14 16:37:58 -06003930 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003931 source = bs;
3932 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003933
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003934 size = bdrv_getlength(bs);
3935 if (size < 0) {
3936 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003937 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003938 }
3939
Eric Blakefaecd402016-07-14 16:37:58 -06003940 if (arg->has_replaces) {
Eric Blakefaecd402016-07-14 16:37:58 -06003941 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003942 error_setg(errp, "a node-name must be provided when replacing a"
3943 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003944 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003945 }
Benoît Canet09158f02014-06-27 18:25:25 +02003946 }
3947
Eric Blakefaecd402016-07-14 16:37:58 -06003948 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003949 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3950 } else {
3951 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3952 }
3953
John Snow2a32c6e2017-07-17 20:34:21 -04003954 /* Don't open backing image in create() */
3955 flags |= BDRV_O_NO_BACKING;
3956
Eric Blakefaecd402016-07-14 16:37:58 -06003957 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3958 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003959 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003960 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003961 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003962 bdrv_img_create(arg->target, format,
Fam Zheng92172832017-04-21 20:27:01 +08003963 NULL, NULL, NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003964 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003965 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003966 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003967 break;
3968 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3969 /* create new image with backing file */
Max Reitzf30c66b2019-02-01 20:29:05 +01003970 bdrv_refresh_filename(source);
Eric Blakefaecd402016-07-14 16:37:58 -06003971 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003972 source->filename,
3973 source->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08003974 NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003975 break;
3976 default:
3977 abort();
3978 }
3979 }
3980
Markus Armbruster84d18f02014-01-30 15:07:28 +01003981 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003982 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003983 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003984 }
3985
Max Reitze6641712015-08-26 19:47:48 +02003986 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003987 if (arg->has_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003988 qdict_put_str(options, "node-name", arg->node_name);
Benoît Canet4c828dc2014-06-16 12:00:55 +02003989 }
Max Reitze6641712015-08-26 19:47:48 +02003990 if (format) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003991 qdict_put_str(options, "driver", format);
Max Reitze6641712015-08-26 19:47:48 +02003992 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003993
Paolo Bonzinib812f672013-01-21 17:09:43 +01003994 /* Mirroring takes care of copy-on-write using the source's backing
3995 * file.
3996 */
John Snow2a32c6e2017-07-17 20:34:21 -04003997 target_bs = bdrv_open(arg->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003998 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003999 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02004000 }
4001
Max Reitzcdf3bc92019-07-24 19:12:30 +02004002 zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL &&
4003 (arg->mode == NEW_IMAGE_MODE_EXISTING ||
4004 !bdrv_has_zero_init(target_bs)));
4005
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004006
4007 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
4008 old_context = bdrv_get_aio_context(target_bs);
4009 aio_context_release(aio_context);
4010 aio_context_acquire(old_context);
4011
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004012 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
4013 if (ret < 0) {
4014 bdrv_unref(target_bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004015 aio_context_release(old_context);
4016 return;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004017 }
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01004018
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004019 aio_context_release(old_context);
4020 aio_context_acquire(aio_context);
4021
Eric Blakefaecd402016-07-14 16:37:58 -06004022 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
4023 arg->has_replaces, arg->replaces, arg->sync,
Max Reitzcdf3bc92019-07-24 19:12:30 +02004024 backing_mode, zero_target,
4025 arg->has_speed, arg->speed,
Eric Blakefaecd402016-07-14 16:37:58 -06004026 arg->has_granularity, arg->granularity,
4027 arg->has_buf_size, arg->buf_size,
4028 arg->has_on_source_error, arg->on_source_error,
4029 arg->has_on_target_error, arg->on_target_error,
4030 arg->has_unmap, arg->unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01004031 false, NULL,
Max Reitz481deba2018-06-13 20:18:22 +02004032 arg->has_copy_mode, arg->copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04004033 arg->has_auto_finalize, arg->auto_finalize,
4034 arg->has_auto_dismiss, arg->auto_dismiss,
Fam Zheng4193cdd2015-12-24 12:45:03 +08004035 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02004036 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03004037 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01004038out:
4039 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02004040}
4041
Alberto Garcia71aa9862016-07-05 17:28:57 +03004042void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
4043 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08004044 bool has_replaces, const char *replaces,
4045 MirrorSyncMode sync,
4046 bool has_speed, int64_t speed,
4047 bool has_granularity, uint32_t granularity,
4048 bool has_buf_size, int64_t buf_size,
4049 bool has_on_source_error,
4050 BlockdevOnError on_source_error,
4051 bool has_on_target_error,
4052 BlockdevOnError on_target_error,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01004053 bool has_filter_node_name,
4054 const char *filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02004055 bool has_copy_mode, MirrorCopyMode copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04004056 bool has_auto_finalize, bool auto_finalize,
4057 bool has_auto_dismiss, bool auto_dismiss,
Fam Zhengdf925622015-12-24 12:45:05 +08004058 Error **errp)
4059{
4060 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08004061 BlockDriverState *target_bs;
4062 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004063 AioContext *old_context;
Max Reitz274fcce2016-06-10 20:57:47 +02004064 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08004065 Error *local_err = NULL;
Max Reitzcdf3bc92019-07-24 19:12:30 +02004066 bool zero_target;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004067 int ret;
Fam Zhengdf925622015-12-24 12:45:05 +08004068
Kevin Wolf07eec652016-06-23 14:20:24 +02004069 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08004070 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08004071 return;
4072 }
4073
4074 target_bs = bdrv_lookup_bs(target, target, errp);
4075 if (!target_bs) {
4076 return;
4077 }
4078
Max Reitzcdf3bc92019-07-24 19:12:30 +02004079 zero_target = (sync == MIRROR_SYNC_MODE_FULL);
4080
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004081 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
4082 old_context = bdrv_get_aio_context(target_bs);
Fam Zhengdf925622015-12-24 12:45:05 +08004083 aio_context = bdrv_get_aio_context(bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004084 aio_context_acquire(old_context);
Fam Zhengdf925622015-12-24 12:45:05 +08004085
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004086 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004087
4088 aio_context_release(old_context);
4089 aio_context_acquire(aio_context);
4090
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004091 if (ret < 0) {
4092 goto out;
4093 }
Fam Zhengdf925622015-12-24 12:45:05 +08004094
Alberto Garcia71aa9862016-07-05 17:28:57 +03004095 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02004096 has_replaces, replaces, sync, backing_mode,
Max Reitzcdf3bc92019-07-24 19:12:30 +02004097 zero_target, has_speed, speed,
Fam Zhengdf925622015-12-24 12:45:05 +08004098 has_granularity, granularity,
4099 has_buf_size, buf_size,
4100 has_on_source_error, on_source_error,
4101 has_on_target_error, on_target_error,
4102 true, true,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01004103 has_filter_node_name, filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02004104 has_copy_mode, copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04004105 has_auto_finalize, auto_finalize,
4106 has_auto_dismiss, auto_dismiss,
Fam Zhengdf925622015-12-24 12:45:05 +08004107 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03004108 error_propagate(errp, local_err);
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004109out:
Fam Zhengdf925622015-12-24 12:45:05 +08004110 aio_context_release(aio_context);
4111}
4112
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03004113/* Get a block job using its ID and acquire its AioContext */
4114static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004115 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004116{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03004117 BlockJob *job;
4118
4119 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004120
Max Reitz5433c242015-10-19 17:53:29 +02004121 *aio_context = NULL;
4122
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03004123 job = block_job_get(id);
4124
4125 if (!job) {
4126 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
4127 "Block job '%s' not found", id);
4128 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004129 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004130
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03004131 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004132 aio_context_acquire(*aio_context);
4133
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03004134 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004135}
4136
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01004137void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004138{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004139 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004140 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004141
4142 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004143 return;
4144 }
4145
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01004146 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004147 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004148}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004149
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004150void qmp_block_job_cancel(const char *device,
4151 bool has_force, bool force, Error **errp)
4152{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004153 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004154 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004155
4156 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004157 return;
4158 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004159
4160 if (!has_force) {
4161 force = false;
4162 }
4163
Kevin Wolfb15de822018-04-18 17:10:26 +02004164 if (job_user_paused(&job->job) && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04004165 error_setg(errp, "The block job for device '%s' is currently paused",
4166 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004167 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004168 }
4169
4170 trace_qmp_block_job_cancel(job);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02004171 job_user_cancel(&job->job, force, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004172out:
4173 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004174}
4175
4176void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004177{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004178 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004179 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004180
John Snow0ec4dfb2018-03-10 03:27:32 -05004181 if (!job) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004182 return;
4183 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004184
4185 trace_qmp_block_job_pause(job);
Kevin Wolfb15de822018-04-18 17:10:26 +02004186 job_user_pause(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004187 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004188}
4189
4190void qmp_block_job_resume(const char *device, Error **errp)
4191{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004192 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004193 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004194
John Snow0ec4dfb2018-03-10 03:27:32 -05004195 if (!job) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02004196 return;
4197 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004198
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004199 trace_qmp_block_job_resume(job);
Kevin Wolfb15de822018-04-18 17:10:26 +02004200 job_user_resume(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004201 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004202}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004203
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004204void qmp_block_job_complete(const char *device, Error **errp)
4205{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004206 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004207 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004208
4209 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004210 return;
4211 }
4212
4213 trace_qmp_block_job_complete(job);
Kevin Wolf3453d972018-04-23 12:24:16 +02004214 job_complete(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004215 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004216}
4217
John Snow11b61fb2018-03-10 03:27:43 -05004218void qmp_block_job_finalize(const char *id, Error **errp)
4219{
4220 AioContext *aio_context;
4221 BlockJob *job = find_block_job(id, &aio_context, errp);
4222
4223 if (!job) {
4224 return;
4225 }
4226
4227 trace_qmp_block_job_finalize(job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02004228 job_finalize(&job->job, errp);
John Snow11b61fb2018-03-10 03:27:43 -05004229 aio_context_release(aio_context);
4230}
4231
John Snow75f71052018-03-10 03:27:36 -05004232void qmp_block_job_dismiss(const char *id, Error **errp)
4233{
4234 AioContext *aio_context;
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02004235 BlockJob *bjob = find_block_job(id, &aio_context, errp);
4236 Job *job;
John Snow75f71052018-03-10 03:27:36 -05004237
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02004238 if (!bjob) {
John Snow75f71052018-03-10 03:27:36 -05004239 return;
4240 }
4241
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02004242 trace_qmp_block_job_dismiss(bjob);
4243 job = &bjob->job;
4244 job_dismiss(&job, errp);
John Snow75f71052018-03-10 03:27:36 -05004245 aio_context_release(aio_context);
4246}
4247
Jeff Codyfa40e652014-07-01 09:52:16 +02004248void qmp_change_backing_file(const char *device,
4249 const char *image_node_name,
4250 const char *backing_file,
4251 Error **errp)
4252{
4253 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004254 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02004255 BlockDriverState *image_bs = NULL;
4256 Error *local_err = NULL;
4257 bool ro;
Jeff Codyfa40e652014-07-01 09:52:16 +02004258 int ret;
4259
Kevin Wolf7b5dca32016-06-23 14:20:24 +02004260 bs = qmp_get_root_bs(device, errp);
4261 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02004262 return;
4263 }
4264
Kevin Wolf7b5dca32016-06-23 14:20:24 +02004265 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004266 aio_context_acquire(aio_context);
4267
Jeff Codyfa40e652014-07-01 09:52:16 +02004268 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
4269 if (local_err) {
4270 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004271 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004272 }
4273
4274 if (!image_bs) {
4275 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004276 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004277 }
4278
4279 if (bdrv_find_base(image_bs) == image_bs) {
4280 error_setg(errp, "not allowing backing file change on an image "
4281 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004282 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004283 }
4284
4285 /* even though we are not necessarily operating on bs, we need it to
4286 * determine if block ops are currently prohibited on the chain */
4287 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004288 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004289 }
4290
4291 /* final sanity check */
4292 if (!bdrv_chain_contains(bs, image_bs)) {
4293 error_setg(errp, "'%s' and image file are not in the same chain",
4294 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004295 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004296 }
4297
4298 /* if not r/w, reopen to make r/w */
Jeff Codyfa40e652014-07-01 09:52:16 +02004299 ro = bdrv_is_read_only(image_bs);
4300
4301 if (ro) {
Alberto Garcia051a60f2018-11-12 16:00:38 +02004302 if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004303 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004304 }
4305 }
4306
4307 ret = bdrv_change_backing_file(image_bs, backing_file,
4308 image_bs->drv ? image_bs->drv->format_name : "");
4309
4310 if (ret < 0) {
4311 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
4312 backing_file);
4313 /* don't exit here, so we can try to restore open flags if
4314 * appropriate */
4315 }
4316
4317 if (ro) {
Alberto Garcia051a60f2018-11-12 16:00:38 +02004318 bdrv_reopen_set_read_only(image_bs, true, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03004319 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02004320 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004321
4322out:
4323 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02004324}
4325
Kevin Wolfabb21ac2016-02-23 17:33:24 +01004326void hmp_drive_add_node(Monitor *mon, const char *optstr)
4327{
4328 QemuOpts *opts;
4329 QDict *qdict;
4330 Error *local_err = NULL;
4331
4332 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
4333 if (!opts) {
4334 return;
4335 }
4336
4337 qdict = qemu_opts_to_qdict(opts, NULL);
4338
4339 if (!qdict_get_try_str(qdict, "node-name")) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004340 qobject_unref(qdict);
Kevin Wolfabb21ac2016-02-23 17:33:24 +01004341 error_report("'node-name' needs to be specified");
4342 goto out;
4343 }
4344
4345 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
4346 if (!bs) {
4347 error_report_err(local_err);
4348 goto out;
4349 }
4350
4351 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
4352
4353out:
4354 qemu_opts_del(opts);
4355}
4356
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004357void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
4358{
Max Reitzbe4b67b2015-10-19 17:53:09 +02004359 BlockDriverState *bs;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004360 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01004361 Visitor *v = qobject_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004362 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004363 Error *local_err = NULL;
4364
Eric Blake3b098d52016-06-09 10:48:43 -06004365 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01004366 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004367 error_propagate(errp, local_err);
4368 goto fail;
4369 }
4370
Eric Blake3b098d52016-06-09 10:48:43 -06004371 visit_complete(v, &obj);
Max Reitz7dc847e2018-02-24 16:40:29 +01004372 qdict = qobject_to(QDict, obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004373
4374 qdict_flatten(qdict);
4375
Kevin Wolf9ec88732016-09-21 14:56:11 +02004376 if (!qdict_get_try_str(qdict, "node-name")) {
4377 error_setg(errp, "'node-name' must be specified for the root node");
4378 goto fail;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004379 }
4380
Kevin Wolf9ec88732016-09-21 14:56:11 +02004381 bs = bds_tree_init(qdict, errp);
4382 if (!bs) {
4383 goto fail;
4384 }
4385
4386 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
4387
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004388fail:
Eric Blake3b098d52016-06-09 10:48:43 -06004389 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004390}
4391
Alberto Garcia1479c732019-03-12 18:48:51 +02004392void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp)
4393{
4394 BlockDriverState *bs;
4395 AioContext *ctx;
4396 QObject *obj;
4397 Visitor *v = qobject_output_visitor_new(&obj);
4398 Error *local_err = NULL;
4399 BlockReopenQueue *queue;
4400 QDict *qdict;
4401
4402 /* Check for the selected node name */
4403 if (!options->has_node_name) {
4404 error_setg(errp, "Node name not specified");
4405 goto fail;
4406 }
4407
4408 bs = bdrv_find_node(options->node_name);
4409 if (!bs) {
4410 error_setg(errp, "Cannot find node named '%s'", options->node_name);
4411 goto fail;
4412 }
4413
4414 /* Put all options in a QDict and flatten it */
4415 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
4416 if (local_err) {
4417 error_propagate(errp, local_err);
4418 goto fail;
4419 }
4420
4421 visit_complete(v, &obj);
4422 qdict = qobject_to(QDict, obj);
4423
4424 qdict_flatten(qdict);
4425
4426 /* Perform the reopen operation */
4427 ctx = bdrv_get_aio_context(bs);
4428 aio_context_acquire(ctx);
4429 bdrv_subtree_drained_begin(bs);
4430 queue = bdrv_reopen_queue(NULL, bs, qdict, false);
4431 bdrv_reopen_multiple(queue, errp);
4432 bdrv_subtree_drained_end(bs);
4433 aio_context_release(ctx);
4434
4435fail:
4436 visit_free(v);
4437}
4438
Markus Armbruster79b7a772017-03-21 17:53:28 +01004439void qmp_blockdev_del(const char *node_name, Error **errp)
Alberto Garcia81b936a2015-11-02 16:51:55 +02004440{
4441 AioContext *aio_context;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004442 BlockDriverState *bs;
4443
Kevin Wolf9ec88732016-09-21 14:56:11 +02004444 bs = bdrv_find_node(node_name);
4445 if (!bs) {
4446 error_setg(errp, "Cannot find node %s", node_name);
Alberto Garcia81b936a2015-11-02 16:51:55 +02004447 return;
4448 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02004449 if (bdrv_has_blk(bs)) {
4450 error_setg(errp, "Node %s is in use", node_name);
4451 return;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004452 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02004453 aio_context = bdrv_get_aio_context(bs);
Alberto Garcia81b936a2015-11-02 16:51:55 +02004454 aio_context_acquire(aio_context);
4455
Kevin Wolf9ec88732016-09-21 14:56:11 +02004456 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
4457 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004458 }
4459
Paolo Bonzini70537ed2018-12-06 23:00:09 +01004460 if (!QTAILQ_IN_USE(bs, monitor_list)) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02004461 error_setg(errp, "Node %s is not owned by the monitor",
4462 bs->node_name);
4463 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004464 }
4465
Kevin Wolf9ec88732016-09-21 14:56:11 +02004466 if (bs->refcnt > 1) {
4467 error_setg(errp, "Block device %s is in use",
4468 bdrv_get_device_or_node_name(bs));
4469 goto out;
4470 }
4471
4472 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
4473 bdrv_unref(bs);
4474
Alberto Garcia81b936a2015-11-02 16:51:55 +02004475out:
4476 aio_context_release(aio_context);
4477}
4478
Wen Congyang7f821592016-05-10 15:36:39 +08004479static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
4480 const char *child_name)
4481{
4482 BdrvChild *child;
4483
4484 QLIST_FOREACH(child, &parent_bs->children, next) {
4485 if (strcmp(child->name, child_name) == 0) {
4486 return child;
4487 }
4488 }
4489
4490 return NULL;
4491}
4492
4493void qmp_x_blockdev_change(const char *parent, bool has_child,
4494 const char *child, bool has_node,
4495 const char *node, Error **errp)
4496{
4497 BlockDriverState *parent_bs, *new_bs = NULL;
4498 BdrvChild *p_child;
4499
4500 parent_bs = bdrv_lookup_bs(parent, parent, errp);
4501 if (!parent_bs) {
4502 return;
4503 }
4504
4505 if (has_child == has_node) {
4506 if (has_child) {
4507 error_setg(errp, "The parameters child and node are in conflict");
4508 } else {
4509 error_setg(errp, "Either child or node must be specified");
4510 }
4511 return;
4512 }
4513
4514 if (has_child) {
4515 p_child = bdrv_find_child(parent_bs, child);
4516 if (!p_child) {
4517 error_setg(errp, "Node '%s' does not have child '%s'",
4518 parent, child);
4519 return;
4520 }
4521 bdrv_del_child(parent_bs, p_child, errp);
4522 }
4523
4524 if (has_node) {
4525 new_bs = bdrv_find_node(node);
4526 if (!new_bs) {
4527 error_setg(errp, "Node '%s' not found", node);
4528 return;
4529 }
4530 bdrv_add_child(parent_bs, new_bs, errp);
4531 }
4532}
4533
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004534BlockJobInfoList *qmp_query_block_jobs(Error **errp)
4535{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004536 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004537 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004538
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004539 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
John Snow559b9352016-10-27 12:06:55 -04004540 BlockJobInfoList *elem;
4541 AioContext *aio_context;
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004542
John Snow559b9352016-10-27 12:06:55 -04004543 if (block_job_is_internal(job)) {
4544 continue;
4545 }
4546 elem = g_new0(BlockJobInfoList, 1);
4547 aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004548 aio_context_acquire(aio_context);
John Snow559b9352016-10-27 12:06:55 -04004549 elem->value = block_job_query(job, errp);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004550 aio_context_release(aio_context);
John Snow559b9352016-10-27 12:06:55 -04004551 if (!elem->value) {
4552 g_free(elem);
4553 qapi_free_BlockJobInfoList(head);
4554 return NULL;
4555 }
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004556 *p_next = elem;
4557 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004558 }
4559
4560 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004561}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004562
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004563void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
Stefan Hajnoczi882e9b82017-12-07 20:13:17 +00004564 bool has_force, bool force, Error **errp)
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004565{
4566 AioContext *old_context;
4567 AioContext *new_context;
4568 BlockDriverState *bs;
4569
4570 bs = bdrv_find_node(node_name);
4571 if (!bs) {
4572 error_setg(errp, "Cannot find node %s", node_name);
4573 return;
4574 }
4575
Stefan Hajnoczi882e9b82017-12-07 20:13:17 +00004576 /* Protects against accidents. */
4577 if (!(has_force && force) && bdrv_has_blk(bs)) {
4578 error_setg(errp, "Node %s is associated with a BlockBackend and could "
4579 "be in use (use force=true to override this check)",
4580 node_name);
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004581 return;
4582 }
4583
4584 if (iothread->type == QTYPE_QSTRING) {
4585 IOThread *obj = iothread_by_id(iothread->u.s);
4586 if (!obj) {
4587 error_setg(errp, "Cannot find iothread %s", iothread->u.s);
4588 return;
4589 }
4590
4591 new_context = iothread_get_aio_context(obj);
4592 } else {
4593 new_context = qemu_get_aio_context();
4594 }
4595
4596 old_context = bdrv_get_aio_context(bs);
4597 aio_context_acquire(old_context);
4598
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004599 bdrv_try_set_aio_context(bs, new_context, errp);
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004600
4601 aio_context_release(old_context);
4602}
4603
Vladimir Sementsov-Ogievskiycb8aac32019-03-05 15:53:17 +03004604void qmp_block_latency_histogram_set(
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004605 const char *id,
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004606 bool has_boundaries, uint64List *boundaries,
4607 bool has_boundaries_read, uint64List *boundaries_read,
4608 bool has_boundaries_write, uint64List *boundaries_write,
4609 bool has_boundaries_flush, uint64List *boundaries_flush,
4610 Error **errp)
4611{
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004612 BlockBackend *blk = qmp_get_blk(NULL, id, errp);
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004613 BlockAcctStats *stats;
zhenwei pi63d53412018-11-05 11:04:56 +08004614 int ret;
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004615
4616 if (!blk) {
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004617 return;
4618 }
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004619
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004620 stats = blk_get_stats(blk);
4621
4622 if (!has_boundaries && !has_boundaries_read && !has_boundaries_write &&
4623 !has_boundaries_flush)
4624 {
4625 block_latency_histograms_clear(stats);
4626 return;
4627 }
4628
4629 if (has_boundaries || has_boundaries_read) {
zhenwei pi63d53412018-11-05 11:04:56 +08004630 ret = block_latency_histogram_set(
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004631 stats, BLOCK_ACCT_READ,
4632 has_boundaries_read ? boundaries_read : boundaries);
zhenwei pi63d53412018-11-05 11:04:56 +08004633 if (ret) {
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004634 error_setg(errp, "Device '%s' set read boundaries fail", id);
zhenwei pi63d53412018-11-05 11:04:56 +08004635 return;
4636 }
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004637 }
4638
4639 if (has_boundaries || has_boundaries_write) {
zhenwei pi63d53412018-11-05 11:04:56 +08004640 ret = block_latency_histogram_set(
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004641 stats, BLOCK_ACCT_WRITE,
4642 has_boundaries_write ? boundaries_write : boundaries);
zhenwei pi63d53412018-11-05 11:04:56 +08004643 if (ret) {
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004644 error_setg(errp, "Device '%s' set write boundaries fail", id);
zhenwei pi63d53412018-11-05 11:04:56 +08004645 return;
4646 }
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004647 }
4648
4649 if (has_boundaries || has_boundaries_flush) {
zhenwei pi63d53412018-11-05 11:04:56 +08004650 ret = block_latency_histogram_set(
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004651 stats, BLOCK_ACCT_FLUSH,
4652 has_boundaries_flush ? boundaries_flush : boundaries);
zhenwei pi63d53412018-11-05 11:04:56 +08004653 if (ret) {
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004654 error_setg(errp, "Device '%s' set flush boundaries fail", id);
zhenwei pi63d53412018-11-05 11:04:56 +08004655 return;
4656 }
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004657 }
4658}
4659
Kevin Wolf00063832013-03-15 10:35:07 +01004660QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004661 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01004662 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01004663 .desc = {
4664 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004665 .name = "snapshot",
4666 .type = QEMU_OPT_BOOL,
4667 .help = "enable/disable snapshot mode",
4668 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004669 .name = "aio",
4670 .type = QEMU_OPT_STRING,
Aarushi Mehtaf80f2672020-01-20 14:18:50 +00004671 .help = "host AIO implementation (threads, native, io_uring)",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004672 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01004673 .name = BDRV_OPT_CACHE_WB,
4674 .type = QEMU_OPT_BOOL,
4675 .help = "Enable writeback mode",
4676 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004677 .name = "format",
4678 .type = QEMU_OPT_STRING,
4679 .help = "disk format (raw, qcow2, ...)",
4680 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004681 .name = "rerror",
4682 .type = QEMU_OPT_STRING,
4683 .help = "read error action",
4684 },{
4685 .name = "werror",
4686 .type = QEMU_OPT_STRING,
4687 .help = "write error action",
4688 },{
Alberto Garcia4e200cf2016-09-15 17:53:05 +03004689 .name = BDRV_OPT_READ_ONLY,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004690 .type = QEMU_OPT_BOOL,
4691 .help = "open drive file as read-only",
Pradeep Jagadeesha2a78622017-02-28 10:31:46 +01004692 },
4693
4694 THROTTLE_OPTS,
4695
4696 {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004697 .name = "throttling.group",
4698 .type = QEMU_OPT_STRING,
4699 .help = "name of the block throttling group",
4700 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004701 .name = "copy-on-read",
4702 .type = QEMU_OPT_BOOL,
4703 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004704 },{
4705 .name = "detect-zeroes",
4706 .type = QEMU_OPT_STRING,
4707 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004708 },{
4709 .name = "stats-account-invalid",
4710 .type = QEMU_OPT_BOOL,
4711 .help = "whether to account for invalid I/O operations "
4712 "in the statistics",
4713 },{
4714 .name = "stats-account-failed",
4715 .type = QEMU_OPT_BOOL,
4716 .help = "whether to account for failed I/O operations "
4717 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004718 },
4719 { /* end of list */ }
4720 },
4721};
Kevin Wolf00063832013-03-15 10:35:07 +01004722
4723QemuOptsList qemu_drive_opts = {
4724 .name = "drive",
4725 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4726 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004727 /*
4728 * no elements => accept any params
4729 * validation will happen later
4730 */
Kevin Wolf00063832013-03-15 10:35:07 +01004731 { /* end of list */ }
4732 },
4733};