blob: 011dcfec27704514884b7a5684bbc97d57d0772b [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
Alberto Garcia43de7e22015-10-26 14:27:16 +02001595 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001596 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001597 const char *format = s->has_format ? s->format : "qcow2";
1598 enum NewImageMode mode;
1599 const char *snapshot_node_name =
1600 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001601
Alberto Garcia43de7e22015-10-26 14:27:16 +02001602 if (node_name && !snapshot_node_name) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001603 error_setg(errp, "New overlay node name missing");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001604 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001605 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001606
1607 if (snapshot_node_name &&
1608 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001609 error_setg(errp, "New overlay node name already in use");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001610 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001611 }
1612
1613 flags = state->old_bs->open_flags;
John Snow2a32c6e2017-07-17 20:34:21 -04001614 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ);
1615 flags |= BDRV_O_NO_BACKING;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001616
1617 /* create new image w/backing file */
1618 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1619 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001620 int64_t size = bdrv_getlength(state->old_bs);
1621 if (size < 0) {
1622 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001623 goto out;
Kevin Wolff86b8b52016-03-02 12:16:44 +01001624 }
Max Reitzf30c66b2019-02-01 20:29:05 +01001625 bdrv_refresh_filename(state->old_bs);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001626 bdrv_img_create(new_image_file, format,
1627 state->old_bs->filename,
1628 state->old_bs->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08001629 NULL, size, flags, false, &local_err);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001630 if (local_err) {
1631 error_propagate(errp, local_err);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001632 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001633 }
1634 }
1635
1636 options = qdict_new();
Peter Maydelld52e1a02018-11-01 16:30:37 +00001637 if (snapshot_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05001638 qdict_put_str(options, "node-name", snapshot_node_name);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001639 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001640 qdict_put_str(options, "driver", format);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001641 }
1642
Max Reitz5b363932016-05-17 16:41:31 +02001643 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1644 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001645 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001646 if (!state->new_bs) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001647 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001648 }
1649
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001650 if (bdrv_has_blk(state->new_bs)) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001651 error_setg(errp, "The overlay is already in use");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001652 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001653 }
1654
1655 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1656 errp)) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001657 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001658 }
1659
1660 if (state->new_bs->backing != NULL) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001661 error_setg(errp, "The overlay already has a backing image");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001662 goto out;
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001663 }
1664
1665 if (!state->new_bs->drv->supports_backing) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001666 error_setg(errp, "The overlay does not support backing images");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001667 goto out;
Kevin Wolfb2c28322017-02-20 12:46:42 +01001668 }
1669
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001670 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1671 old_context = bdrv_get_aio_context(state->new_bs);
1672 aio_context_release(aio_context);
1673 aio_context_acquire(old_context);
1674
Kevin Wolf8ed7d422019-04-26 16:12:27 +02001675 ret = bdrv_try_set_aio_context(state->new_bs, aio_context, errp);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001676
1677 aio_context_release(old_context);
1678 aio_context_acquire(aio_context);
1679
Kevin Wolf8ed7d422019-04-26 16:12:27 +02001680 if (ret < 0) {
1681 goto out;
1682 }
Fam Zhengc26a5ab2017-04-07 14:54:09 +08001683
Kevin Wolfb2c28322017-02-20 12:46:42 +01001684 /* This removes our old bs and adds the new bs. This is an operation that
1685 * can fail, so we need to do it in .prepare; undoing it for abort is
1686 * always possible. */
1687 bdrv_ref(state->new_bs);
1688 bdrv_append(state->new_bs, state->old_bs, &local_err);
1689 if (local_err) {
1690 error_propagate(errp, local_err);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001691 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001692 }
Kevin Wolf067acf22017-03-02 15:26:18 +01001693 state->overlay_appended = true;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001694
1695out:
1696 aio_context_release(aio_context);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001697}
1698
John Snow50f43f02015-11-05 18:13:09 -05001699static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001700{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001701 ExternalSnapshotState *state =
1702 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001703 AioContext *aio_context;
1704
1705 aio_context = bdrv_get_aio_context(state->old_bs);
1706 aio_context_acquire(aio_context);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001707
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001708 /* We don't need (or want) to use the transactional
1709 * bdrv_reopen_multiple() across all the entries at once, because we
1710 * don't want to abort all of them if one of them fails the reopen */
Paolo Bonzinid3faa132017-06-05 14:38:50 +02001711 if (!atomic_read(&state->old_bs->copy_on_read)) {
Alberto Garcia1b577742018-11-12 16:00:39 +02001712 bdrv_reopen_set_read_only(state->old_bs, true, NULL);
Kevin Wolf4c844982016-02-29 13:12:26 +01001713 }
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001714
1715 aio_context_release(aio_context);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001716}
1717
John Snow50f43f02015-11-05 18:13:09 -05001718static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001719{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001720 ExternalSnapshotState *state =
1721 DO_UPCAST(ExternalSnapshotState, common, common);
1722 if (state->new_bs) {
Kevin Wolf067acf22017-03-02 15:26:18 +01001723 if (state->overlay_appended) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001724 AioContext *aio_context;
Sergio Lopez377410f2020-01-08 15:31:37 +01001725 AioContext *tmp_context;
1726 int ret;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001727
1728 aio_context = bdrv_get_aio_context(state->old_bs);
1729 aio_context_acquire(aio_context);
1730
Jeff Cody719fc282017-06-07 09:55:22 -04001731 bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
1732 close state->old_bs; we need it */
1733 bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
Sergio Lopez377410f2020-01-08 15:31:37 +01001734
1735 /*
1736 * The call to bdrv_set_backing_hd() above returns state->old_bs to
1737 * the main AioContext. As we're still going to be using it, return
1738 * it to the AioContext it was before.
1739 */
1740 tmp_context = bdrv_get_aio_context(state->old_bs);
1741 if (aio_context != tmp_context) {
1742 aio_context_release(aio_context);
1743 aio_context_acquire(tmp_context);
1744
1745 ret = bdrv_try_set_aio_context(state->old_bs,
1746 aio_context, NULL);
1747 assert(ret == 0);
1748
1749 aio_context_release(tmp_context);
1750 aio_context_acquire(aio_context);
1751 }
1752
Kevin Wolf5fe31c22017-03-06 16:20:51 +01001753 bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
Jeff Cody719fc282017-06-07 09:55:22 -04001754 bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001755
1756 aio_context_release(aio_context);
Kevin Wolfb2c28322017-02-20 12:46:42 +01001757 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001758 }
Fam Zhengda763e82015-10-23 11:08:10 +08001759}
1760
John Snow50f43f02015-11-05 18:13:09 -05001761static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001762{
1763 ExternalSnapshotState *state =
1764 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001765 AioContext *aio_context;
1766
1767 if (!state->old_bs) {
1768 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001769 }
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001770
1771 aio_context = bdrv_get_aio_context(state->old_bs);
1772 aio_context_acquire(aio_context);
1773
1774 bdrv_drained_end(state->old_bs);
1775 bdrv_unref(state->new_bs);
1776
1777 aio_context_release(aio_context);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001778}
1779
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001780typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001781 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001782 BlockDriverState *bs;
1783 BlockJob *job;
1784} DriveBackupState;
1785
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001786static BlockJob *do_backup_common(BackupCommon *backup,
1787 BlockDriverState *bs,
1788 BlockDriverState *target_bs,
1789 AioContext *aio_context,
1790 JobTxn *txn, Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001791
John Snow50f43f02015-11-05 18:13:09 -05001792static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001793{
1794 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1795 DriveBackup *backup;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001796 BlockDriverState *bs;
1797 BlockDriverState *target_bs;
1798 BlockDriverState *source = NULL;
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001799 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001800 AioContext *old_context;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001801 QDict *options;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001802 Error *local_err = NULL;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001803 int flags;
1804 int64_t size;
1805 bool set_backing_hd = false;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001806 int ret;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001807
Eric Blake6a8f9662015-10-26 16:34:54 -06001808 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001809 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001810
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001811 if (!backup->has_mode) {
1812 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1813 }
1814
Vladimir Sementsov-Ogievskiy85c9d132019-06-18 17:08:04 +03001815 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001816 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001817 return;
1818 }
1819
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001820 if (!bs->drv) {
1821 error_setg(errp, "Device has no medium");
1822 return;
1823 }
1824
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001825 aio_context = bdrv_get_aio_context(bs);
1826 aio_context_acquire(aio_context);
1827
1828 /* Paired with .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001829 bdrv_drained_begin(bs);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001830
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001831 if (!backup->has_format) {
1832 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
1833 NULL : (char *) bs->drv->format_name;
1834 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001835
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001836 /* Early check to avoid creating target */
1837 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
1838 goto out;
1839 }
1840
1841 flags = bs->open_flags | BDRV_O_RDWR;
1842
1843 /*
1844 * See if we have a backing HD we can use to create our new image
1845 * on top of.
1846 */
1847 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
1848 source = backing_bs(bs);
1849 if (!source) {
1850 backup->sync = MIRROR_SYNC_MODE_FULL;
1851 }
1852 }
1853 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
1854 source = bs;
1855 flags |= BDRV_O_NO_BACKING;
1856 set_backing_hd = true;
1857 }
1858
1859 size = bdrv_getlength(bs);
1860 if (size < 0) {
1861 error_setg_errno(errp, -size, "bdrv_getlength failed");
1862 goto out;
1863 }
1864
1865 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
1866 assert(backup->format);
1867 if (source) {
1868 bdrv_refresh_filename(source);
1869 bdrv_img_create(backup->target, backup->format, source->filename,
1870 source->drv->format_name, NULL,
1871 size, flags, false, &local_err);
1872 } else {
1873 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
1874 size, flags, false, &local_err);
1875 }
1876 }
1877
Markus Armbruster84d18f02014-01-30 15:07:28 +01001878 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001879 error_propagate(errp, local_err);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001880 goto out;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001881 }
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001882
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001883 options = qdict_new();
1884 qdict_put_str(options, "discard", "unmap");
1885 qdict_put_str(options, "detect-zeroes", "unmap");
1886 if (backup->format) {
1887 qdict_put_str(options, "driver", backup->format);
1888 }
1889
1890 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
1891 if (!target_bs) {
1892 goto out;
1893 }
1894
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001895 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1896 old_context = bdrv_get_aio_context(target_bs);
1897 aio_context_release(aio_context);
1898 aio_context_acquire(old_context);
1899
1900 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
1901 if (ret < 0) {
1902 bdrv_unref(target_bs);
1903 aio_context_release(old_context);
1904 return;
1905 }
1906
1907 aio_context_release(old_context);
1908 aio_context_acquire(aio_context);
1909
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001910 if (set_backing_hd) {
1911 bdrv_set_backing_hd(target_bs, source, &local_err);
1912 if (local_err) {
1913 goto unref;
1914 }
1915 }
1916
1917 state->bs = bs;
1918
1919 state->job = do_backup_common(qapi_DriveBackup_base(backup),
1920 bs, target_bs, aio_context,
1921 common->block_job_txn, errp);
1922
1923unref:
1924 bdrv_unref(target_bs);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001925out:
1926 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05001927}
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001928
John Snow111049a2016-11-08 01:50:38 -05001929static void drive_backup_commit(BlkActionState *common)
1930{
1931 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001932 AioContext *aio_context;
1933
1934 aio_context = bdrv_get_aio_context(state->bs);
1935 aio_context_acquire(aio_context);
1936
John Snow111049a2016-11-08 01:50:38 -05001937 assert(state->job);
Kevin Wolfda01ff72018-04-13 17:31:02 +02001938 job_start(&state->job->job);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001939
1940 aio_context_release(aio_context);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001941}
1942
John Snow50f43f02015-11-05 18:13:09 -05001943static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001944{
1945 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001946
John Snow111049a2016-11-08 01:50:38 -05001947 if (state->job) {
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001948 AioContext *aio_context;
1949
1950 aio_context = bdrv_get_aio_context(state->bs);
1951 aio_context_acquire(aio_context);
1952
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001953 job_cancel_sync(&state->job->job);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001954
1955 aio_context_release(aio_context);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001956 }
1957}
1958
John Snow50f43f02015-11-05 18:13:09 -05001959static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001960{
1961 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001962 AioContext *aio_context;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001963
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001964 if (!state->bs) {
1965 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001966 }
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001967
1968 aio_context = bdrv_get_aio_context(state->bs);
1969 aio_context_acquire(aio_context);
1970
1971 bdrv_drained_end(state->bs);
1972
1973 aio_context_release(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001974}
1975
Fam Zhengbd8baec2014-12-18 18:37:06 +08001976typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001977 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001978 BlockDriverState *bs;
1979 BlockJob *job;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001980} BlockdevBackupState;
1981
John Snow50f43f02015-11-05 18:13:09 -05001982static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001983{
1984 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1985 BlockdevBackup *backup;
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01001986 BlockDriverState *bs;
1987 BlockDriverState *target_bs;
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001988 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001989 AioContext *old_context;
1990 int ret;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001991
Eric Blake6a8f9662015-10-26 16:34:54 -06001992 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001993 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001994
Vladimir Sementsov-Ogievskiy930fe172018-07-02 15:46:29 -04001995 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001996 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001997 return;
1998 }
1999
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01002000 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
2001 if (!target_bs) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08002002 return;
2003 }
2004
Sergio Lopez3ea67e02020-01-08 15:31:34 +01002005 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002006 aio_context = bdrv_get_aio_context(bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01002007 old_context = bdrv_get_aio_context(target_bs);
2008 aio_context_acquire(old_context);
2009
2010 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
2011 if (ret < 0) {
2012 aio_context_release(old_context);
2013 return;
2014 }
2015
2016 aio_context_release(old_context);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002017 aio_context_acquire(aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02002018 state->bs = bs;
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002019
2020 /* Paired with .clean() */
Fam Zhengff52bf32015-10-23 11:08:12 +08002021 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002022
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01002023 state->job = do_backup_common(qapi_BlockdevBackup_base(backup),
2024 bs, target_bs, aio_context,
2025 common->block_job_txn, errp);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002026
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002027 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05002028}
Fam Zhengbd8baec2014-12-18 18:37:06 +08002029
John Snow111049a2016-11-08 01:50:38 -05002030static void blockdev_backup_commit(BlkActionState *common)
2031{
2032 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002033 AioContext *aio_context;
2034
2035 aio_context = bdrv_get_aio_context(state->bs);
2036 aio_context_acquire(aio_context);
2037
John Snow111049a2016-11-08 01:50:38 -05002038 assert(state->job);
Kevin Wolfda01ff72018-04-13 17:31:02 +02002039 job_start(&state->job->job);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002040
2041 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002042}
2043
John Snow50f43f02015-11-05 18:13:09 -05002044static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08002045{
2046 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002047
John Snow111049a2016-11-08 01:50:38 -05002048 if (state->job) {
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002049 AioContext *aio_context;
2050
2051 aio_context = bdrv_get_aio_context(state->bs);
2052 aio_context_acquire(aio_context);
2053
Kevin Wolf3d70ff52018-04-24 16:13:52 +02002054 job_cancel_sync(&state->job->job);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002055
2056 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002057 }
2058}
2059
John Snow50f43f02015-11-05 18:13:09 -05002060static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08002061{
2062 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002063 AioContext *aio_context;
Fam Zhengbd8baec2014-12-18 18:37:06 +08002064
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002065 if (!state->bs) {
2066 return;
Fam Zhengbd8baec2014-12-18 18:37:06 +08002067 }
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002068
2069 aio_context = bdrv_get_aio_context(state->bs);
2070 aio_context_acquire(aio_context);
2071
2072 bdrv_drained_end(state->bs);
2073
2074 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002075}
2076
Fam Zhengdf9a6812015-11-09 18:16:54 +08002077typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05002078 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002079 BdrvDirtyBitmap *bitmap;
2080 BlockDriverState *bs;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002081 HBitmap *backup;
2082 bool prepared;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002083 bool was_enabled;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002084} BlockDirtyBitmapState;
2085
John Snow50f43f02015-11-05 18:13:09 -05002086static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002087 Error **errp)
2088{
2089 Error *local_err = NULL;
2090 BlockDirtyBitmapAdd *action;
2091 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2092 common, common);
2093
John Snow94d16a62015-11-05 18:13:18 -05002094 if (action_check_completion_mode(common, errp) < 0) {
2095 return;
2096 }
2097
Eric Blake32bafa82016-03-17 16:48:37 -06002098 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002099 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2100 qmp_block_dirty_bitmap_add(action->node, action->name,
2101 action->has_granularity, action->granularity,
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002102 action->has_persistent, action->persistent,
John Snow0e2b7f02018-12-21 04:35:22 -05002103 action->has_disabled, action->disabled,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002104 &local_err);
2105
2106 if (!local_err) {
2107 state->prepared = true;
2108 } else {
2109 error_propagate(errp, local_err);
2110 }
2111}
2112
John Snow50f43f02015-11-05 18:13:09 -05002113static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002114{
2115 BlockDirtyBitmapAdd *action;
2116 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2117 common, common);
2118
Eric Blake32bafa82016-03-17 16:48:37 -06002119 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002120 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2121 * then the node reference and bitmap name must have been valid.
2122 */
2123 if (state->prepared) {
2124 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2125 }
2126}
2127
John Snow50f43f02015-11-05 18:13:09 -05002128static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002129 Error **errp)
2130{
2131 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2132 common, common);
2133 BlockDirtyBitmap *action;
2134
John Snow94d16a62015-11-05 18:13:18 -05002135 if (action_check_completion_mode(common, errp) < 0) {
2136 return;
2137 }
2138
Eric Blake32bafa82016-03-17 16:48:37 -06002139 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002140 state->bitmap = block_dirty_bitmap_lookup(action->node,
2141 action->name,
2142 &state->bs,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002143 errp);
2144 if (!state->bitmap) {
2145 return;
2146 }
2147
John Snow3ae96d62019-03-12 12:05:49 -04002148 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) {
Vladimir Sementsov-Ogievskiyd6883bc2017-06-28 15:05:10 +03002149 return;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002150 }
2151
2152 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
Fam Zhengdf9a6812015-11-09 18:16:54 +08002153}
2154
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002155static void block_dirty_bitmap_restore(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002156{
2157 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2158 common, common);
2159
John Snow184dd9c2017-03-15 17:28:11 -04002160 if (state->backup) {
Vladimir Sementsov-Ogievskiy56bd6622018-10-29 16:23:14 -04002161 bdrv_restore_dirty_bitmap(state->bitmap, state->backup);
John Snow184dd9c2017-03-15 17:28:11 -04002162 }
Fam Zhengdf9a6812015-11-09 18:16:54 +08002163}
2164
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002165static void block_dirty_bitmap_free_backup(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002166{
2167 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2168 common, common);
2169
2170 hbitmap_free(state->backup);
2171}
2172
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002173static void block_dirty_bitmap_enable_prepare(BlkActionState *common,
2174 Error **errp)
2175{
2176 BlockDirtyBitmap *action;
2177 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2178 common, common);
2179
2180 if (action_check_completion_mode(common, errp) < 0) {
2181 return;
2182 }
2183
John Snow0e2b7f02018-12-21 04:35:22 -05002184 action = common->action->u.block_dirty_bitmap_enable.data;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002185 state->bitmap = block_dirty_bitmap_lookup(action->node,
2186 action->name,
2187 NULL,
2188 errp);
2189 if (!state->bitmap) {
2190 return;
2191 }
2192
John Snow3ae96d62019-03-12 12:05:49 -04002193 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snowb053bb52018-10-29 16:23:16 -04002194 return;
2195 }
2196
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002197 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2198 bdrv_enable_dirty_bitmap(state->bitmap);
2199}
2200
2201static void block_dirty_bitmap_enable_abort(BlkActionState *common)
2202{
2203 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2204 common, common);
2205
2206 if (!state->was_enabled) {
2207 bdrv_disable_dirty_bitmap(state->bitmap);
2208 }
2209}
2210
2211static void block_dirty_bitmap_disable_prepare(BlkActionState *common,
2212 Error **errp)
2213{
2214 BlockDirtyBitmap *action;
2215 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2216 common, common);
2217
2218 if (action_check_completion_mode(common, errp) < 0) {
2219 return;
2220 }
2221
John Snow0e2b7f02018-12-21 04:35:22 -05002222 action = common->action->u.block_dirty_bitmap_disable.data;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002223 state->bitmap = block_dirty_bitmap_lookup(action->node,
2224 action->name,
2225 NULL,
2226 errp);
2227 if (!state->bitmap) {
2228 return;
2229 }
2230
John Snow3ae96d62019-03-12 12:05:49 -04002231 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snowb053bb52018-10-29 16:23:16 -04002232 return;
2233 }
2234
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002235 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2236 bdrv_disable_dirty_bitmap(state->bitmap);
2237}
2238
2239static void block_dirty_bitmap_disable_abort(BlkActionState *common)
2240{
2241 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2242 common, common);
2243
2244 if (state->was_enabled) {
2245 bdrv_enable_dirty_bitmap(state->bitmap);
2246 }
2247}
2248
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04002249static BdrvDirtyBitmap *do_block_dirty_bitmap_merge(
2250 const char *node, const char *target,
2251 BlockDirtyBitmapMergeSourceList *bitmaps,
2252 HBitmap **backup, Error **errp);
John Snow360d4e42018-12-21 04:35:21 -05002253
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002254static void block_dirty_bitmap_merge_prepare(BlkActionState *common,
2255 Error **errp)
2256{
2257 BlockDirtyBitmapMerge *action;
2258 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2259 common, common);
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002260
2261 if (action_check_completion_mode(common, errp) < 0) {
2262 return;
2263 }
2264
John Snow0e2b7f02018-12-21 04:35:22 -05002265 action = common->action->u.block_dirty_bitmap_merge.data;
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002266
John Snow360d4e42018-12-21 04:35:21 -05002267 state->bitmap = do_block_dirty_bitmap_merge(action->node, action->target,
2268 action->bitmaps, &state->backup,
2269 errp);
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002270}
2271
John Snowc4e4b0f2019-07-29 16:35:54 -04002272static BdrvDirtyBitmap *do_block_dirty_bitmap_remove(
2273 const char *node, const char *name, bool release,
2274 BlockDriverState **bitmap_bs, Error **errp);
2275
2276static void block_dirty_bitmap_remove_prepare(BlkActionState *common,
2277 Error **errp)
2278{
2279 BlockDirtyBitmap *action;
2280 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2281 common, common);
2282
2283 if (action_check_completion_mode(common, errp) < 0) {
2284 return;
2285 }
2286
2287 action = common->action->u.block_dirty_bitmap_remove.data;
2288
2289 state->bitmap = do_block_dirty_bitmap_remove(action->node, action->name,
2290 false, &state->bs, errp);
2291 if (state->bitmap) {
2292 bdrv_dirty_bitmap_skip_store(state->bitmap, true);
2293 bdrv_dirty_bitmap_set_busy(state->bitmap, true);
2294 }
2295}
2296
2297static void block_dirty_bitmap_remove_abort(BlkActionState *common)
2298{
2299 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2300 common, common);
2301
2302 if (state->bitmap) {
2303 bdrv_dirty_bitmap_skip_store(state->bitmap, false);
2304 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
2305 }
2306}
2307
2308static void block_dirty_bitmap_remove_commit(BlkActionState *common)
2309{
2310 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2311 common, common);
2312
2313 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +03002314 bdrv_release_dirty_bitmap(state->bitmap);
John Snowc4e4b0f2019-07-29 16:35:54 -04002315}
2316
John Snow50f43f02015-11-05 18:13:09 -05002317static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002318{
2319 error_setg(errp, "Transaction aborted using Abort action");
2320}
2321
John Snow50f43f02015-11-05 18:13:09 -05002322static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002323{
Stefan Weildfc6f862013-07-25 18:21:28 +02002324 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002325}
2326
John Snow50f43f02015-11-05 18:13:09 -05002327static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002328 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2329 .instance_size = sizeof(ExternalSnapshotState),
2330 .prepare = external_snapshot_prepare,
2331 .commit = external_snapshot_commit,
2332 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002333 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002334 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002335 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002336 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002337 .prepare = external_snapshot_prepare,
2338 .commit = external_snapshot_commit,
2339 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002340 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002341 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002342 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2343 .instance_size = sizeof(DriveBackupState),
2344 .prepare = drive_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002345 .commit = drive_backup_commit,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002346 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002347 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002348 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002349 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2350 .instance_size = sizeof(BlockdevBackupState),
2351 .prepare = blockdev_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002352 .commit = blockdev_backup_commit,
Fam Zhengbd8baec2014-12-18 18:37:06 +08002353 .abort = blockdev_backup_abort,
2354 .clean = blockdev_backup_clean,
2355 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002356 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002357 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002358 .prepare = abort_prepare,
2359 .commit = abort_commit,
2360 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002361 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2362 .instance_size = sizeof(InternalSnapshotState),
2363 .prepare = internal_snapshot_prepare,
2364 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002365 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002366 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002367 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2368 .instance_size = sizeof(BlockDirtyBitmapState),
2369 .prepare = block_dirty_bitmap_add_prepare,
2370 .abort = block_dirty_bitmap_add_abort,
2371 },
2372 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2373 .instance_size = sizeof(BlockDirtyBitmapState),
2374 .prepare = block_dirty_bitmap_clear_prepare,
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002375 .commit = block_dirty_bitmap_free_backup,
2376 .abort = block_dirty_bitmap_restore,
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002377 },
John Snow0e2b7f02018-12-21 04:35:22 -05002378 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = {
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002379 .instance_size = sizeof(BlockDirtyBitmapState),
2380 .prepare = block_dirty_bitmap_enable_prepare,
2381 .abort = block_dirty_bitmap_enable_abort,
2382 },
John Snow0e2b7f02018-12-21 04:35:22 -05002383 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = {
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002384 .instance_size = sizeof(BlockDirtyBitmapState),
2385 .prepare = block_dirty_bitmap_disable_prepare,
2386 .abort = block_dirty_bitmap_disable_abort,
John Snow66da04d2018-09-06 09:02:25 -04002387 },
John Snow0e2b7f02018-12-21 04:35:22 -05002388 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = {
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002389 .instance_size = sizeof(BlockDirtyBitmapState),
2390 .prepare = block_dirty_bitmap_merge_prepare,
2391 .commit = block_dirty_bitmap_free_backup,
2392 .abort = block_dirty_bitmap_restore,
2393 },
John Snowc4e4b0f2019-07-29 16:35:54 -04002394 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = {
2395 .instance_size = sizeof(BlockDirtyBitmapState),
2396 .prepare = block_dirty_bitmap_remove_prepare,
2397 .commit = block_dirty_bitmap_remove_commit,
2398 .abort = block_dirty_bitmap_remove_abort,
2399 },
John Snow66da04d2018-09-06 09:02:25 -04002400 /* Where are transactions for MIRROR, COMMIT and STREAM?
2401 * Although these blockjobs use transaction callbacks like the backup job,
2402 * these jobs do not necessarily adhere to transaction semantics.
2403 * These jobs may not fully undo all of their actions on abort, nor do they
2404 * necessarily work in transactions with more than one job in them.
2405 */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002406};
2407
John Snow94d16a62015-11-05 18:13:18 -05002408/**
2409 * Allocate a TransactionProperties structure if necessary, and fill
2410 * that structure with desired defaults if they are unset.
2411 */
2412static TransactionProperties *get_transaction_properties(
2413 TransactionProperties *props)
2414{
2415 if (!props) {
2416 props = g_new0(TransactionProperties, 1);
2417 }
2418
2419 if (!props->has_completion_mode) {
2420 props->has_completion_mode = true;
2421 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2422 }
2423
2424 return props;
2425}
2426
Jeff Cody8802d1f2012-02-28 15:54:06 -05002427/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002428 * 'Atomic' group operations. The operations are performed as a set, and if
2429 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002430 */
John Snow94d16a62015-11-05 18:13:18 -05002431void qmp_transaction(TransactionActionList *dev_list,
2432 bool has_props,
2433 struct TransactionProperties *props,
2434 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002435{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002436 TransactionActionList *dev_entry = dev_list;
Kevin Wolf62c9e412018-04-19 16:09:52 +02002437 JobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002438 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002439 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002440
John Snowf4de0f82019-01-11 11:59:16 -06002441 QTAILQ_HEAD(, BlkActionState) snap_bdrv_states;
2442 QTAILQ_INIT(&snap_bdrv_states);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002443
John Snow94d16a62015-11-05 18:13:18 -05002444 /* Does this transaction get canceled as a group on failure?
Kevin Wolf62c9e412018-04-19 16:09:52 +02002445 * If not, we don't really need to make a JobTxn.
John Snow94d16a62015-11-05 18:13:18 -05002446 */
2447 props = get_transaction_properties(props);
2448 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02002449 block_job_txn = job_txn_new();
John Snow94d16a62015-11-05 18:13:18 -05002450 }
2451
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002452 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002453 bdrv_drain_all();
2454
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002455 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002456 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002457 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002458 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002459
Jeff Cody8802d1f2012-02-28 15:54:06 -05002460 dev_info = dev_entry->value;
2461 dev_entry = dev_entry->next;
2462
Eric Blake6a8f9662015-10-26 16:34:54 -06002463 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002464
Eric Blake6a8f9662015-10-26 16:34:54 -06002465 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002466 assert(ops->instance_size > 0);
2467
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002468 state = g_malloc0(ops->instance_size);
2469 state->ops = ops;
2470 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002471 state->block_job_txn = block_job_txn;
2472 state->txn_props = props;
John Snowf4de0f82019-01-11 11:59:16 -06002473 QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002474
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002475 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002476 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002477 error_propagate(errp, local_err);
2478 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002479 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002480 }
2481
John Snowf4de0f82019-01-11 11:59:16 -06002482 QTAILQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002483 if (state->ops->commit) {
2484 state->ops->commit(state);
2485 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002486 }
2487
2488 /* success */
2489 goto exit;
2490
2491delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002492 /* failure, and it is all-or-none; roll back all operations */
John Snowf4de0f82019-01-11 11:59:16 -06002493 QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002494 if (state->ops->abort) {
2495 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002496 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002497 }
2498exit:
John Snowf4de0f82019-01-11 11:59:16 -06002499 QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002500 if (state->ops->clean) {
2501 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002502 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002503 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002504 }
John Snow94d16a62015-11-05 18:13:18 -05002505 if (!has_props) {
2506 qapi_free_TransactionProperties(props);
2507 }
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02002508 job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002509}
2510
Kevin Wolffbe2d812016-09-20 13:38:46 +02002511void qmp_eject(bool has_device, const char *device,
2512 bool has_id, const char *id,
2513 bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002514{
Max Reitz38f54bd2015-10-26 21:39:12 +01002515 Error *local_err = NULL;
John Snow3a3086b2016-05-18 17:53:40 -04002516 int rc;
Markus Armbruster666daa62010-06-02 18:48:27 +02002517
John Snow3a3086b2016-05-18 17:53:40 -04002518 if (!has_force) {
2519 force = false;
2520 }
2521
Kevin Wolffbe2d812016-09-20 13:38:46 +02002522 rc = do_open_tray(has_device ? device : NULL,
2523 has_id ? id : NULL,
2524 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002525 if (rc && rc != -ENOSYS) {
Max Reitz38f54bd2015-10-26 21:39:12 +01002526 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002527 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002528 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002529 error_free(local_err);
John Snow3a3086b2016-05-18 17:53:40 -04002530
Max Reitz82fcf662017-11-10 23:43:01 +01002531 blockdev_remove_medium(has_device, device, has_id, id, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002532}
2533
Benoît Canet12d3ba82014-01-23 21:31:35 +01002534void qmp_block_passwd(bool has_device, const char *device,
2535 bool has_node_name, const char *node_name,
2536 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002537{
Daniel P. Berrangec01c2142017-06-23 17:24:16 +01002538 error_setg(errp,
2539 "Setting block passwords directly is no longer supported");
Markus Armbruster666daa62010-06-02 18:48:27 +02002540}
2541
Colin Lordbf18bee2016-06-06 14:15:22 -04002542/*
2543 * Attempt to open the tray of @device.
2544 * If @force, ignore its tray lock.
2545 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2546 * On error, store an error through @errp and return -errno.
2547 * If @device does not exist, return -ENODEV.
2548 * If it has no removable media, return -ENOTSUP.
2549 * If it has no tray, return -ENOSYS.
2550 * If the guest was asked to open the tray, return -EINPROGRESS.
2551 * Else, return 0.
John Snow3a3086b2016-05-18 17:53:40 -04002552 */
Kevin Wolfb33945c2016-09-20 13:38:43 +02002553static int do_open_tray(const char *blk_name, const char *qdev_id,
2554 bool force, Error **errp)
Max Reitz7d8a9f72015-10-26 21:39:08 +01002555{
2556 BlockBackend *blk;
Kevin Wolfb33945c2016-09-20 13:38:43 +02002557 const char *device = qdev_id ?: blk_name;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002558 bool locked;
2559
Kevin Wolfb33945c2016-09-20 13:38:43 +02002560 blk = qmp_get_blk(blk_name, qdev_id, errp);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002561 if (!blk) {
John Snow3a3086b2016-05-18 17:53:40 -04002562 return -ENODEV;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002563 }
2564
2565 if (!blk_dev_has_removable_media(blk)) {
2566 error_setg(errp, "Device '%s' is not removable", device);
John Snow3a3086b2016-05-18 17:53:40 -04002567 return -ENOTSUP;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002568 }
2569
Max Reitz12c7ec82016-01-29 20:49:11 +01002570 if (!blk_dev_has_tray(blk)) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002571 error_setg(errp, "Device '%s' does not have a tray", device);
2572 return -ENOSYS;
Max Reitz12c7ec82016-01-29 20:49:11 +01002573 }
2574
Max Reitz7d8a9f72015-10-26 21:39:08 +01002575 if (blk_dev_is_tray_open(blk)) {
John Snow3a3086b2016-05-18 17:53:40 -04002576 return 0;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002577 }
2578
2579 locked = blk_dev_is_medium_locked(blk);
2580 if (locked) {
2581 blk_dev_eject_request(blk, force);
2582 }
2583
2584 if (!locked || force) {
Kevin Wolf39829a02017-01-24 14:21:41 +01002585 blk_dev_change_media_cb(blk, false, &error_abort);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002586 }
John Snow3a3086b2016-05-18 17:53:40 -04002587
2588 if (locked && !force) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002589 error_setg(errp, "Device '%s' is locked and force was not specified, "
2590 "wait for tray to open and try again", device);
2591 return -EINPROGRESS;
John Snow3a3086b2016-05-18 17:53:40 -04002592 }
2593
2594 return 0;
2595}
2596
Kevin Wolfb33945c2016-09-20 13:38:43 +02002597void qmp_blockdev_open_tray(bool has_device, const char *device,
2598 bool has_id, const char *id,
2599 bool has_force, bool force,
John Snow3a3086b2016-05-18 17:53:40 -04002600 Error **errp)
2601{
Colin Lordbf18bee2016-06-06 14:15:22 -04002602 Error *local_err = NULL;
2603 int rc;
2604
John Snow3a3086b2016-05-18 17:53:40 -04002605 if (!has_force) {
2606 force = false;
2607 }
Kevin Wolfb33945c2016-09-20 13:38:43 +02002608 rc = do_open_tray(has_device ? device : NULL,
2609 has_id ? id : NULL,
2610 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002611 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2612 error_propagate(errp, local_err);
2613 return;
2614 }
2615 error_free(local_err);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002616}
2617
Kevin Wolfb33945c2016-09-20 13:38:43 +02002618void qmp_blockdev_close_tray(bool has_device, const char *device,
2619 bool has_id, const char *id,
2620 Error **errp)
Max Reitzabaaf592015-10-26 21:39:09 +01002621{
2622 BlockBackend *blk;
Kevin Wolf39829a02017-01-24 14:21:41 +01002623 Error *local_err = NULL;
Max Reitzabaaf592015-10-26 21:39:09 +01002624
Kevin Wolfb33945c2016-09-20 13:38:43 +02002625 device = has_device ? device : NULL;
2626 id = has_id ? id : NULL;
2627
2628 blk = qmp_get_blk(device, id, errp);
Max Reitzabaaf592015-10-26 21:39:09 +01002629 if (!blk) {
Max Reitzabaaf592015-10-26 21:39:09 +01002630 return;
2631 }
2632
2633 if (!blk_dev_has_removable_media(blk)) {
Kevin Wolfb33945c2016-09-20 13:38:43 +02002634 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitzabaaf592015-10-26 21:39:09 +01002635 return;
2636 }
2637
Max Reitz12c7ec82016-01-29 20:49:11 +01002638 if (!blk_dev_has_tray(blk)) {
2639 /* Ignore this command on tray-less devices */
2640 return;
2641 }
2642
Max Reitzabaaf592015-10-26 21:39:09 +01002643 if (!blk_dev_is_tray_open(blk)) {
2644 return;
2645 }
2646
Kevin Wolf39829a02017-01-24 14:21:41 +01002647 blk_dev_change_media_cb(blk, true, &local_err);
2648 if (local_err) {
2649 error_propagate(errp, local_err);
2650 return;
2651 }
Max Reitzabaaf592015-10-26 21:39:09 +01002652}
2653
Max Reitz82fcf662017-11-10 23:43:01 +01002654static void blockdev_remove_medium(bool has_device, const char *device,
2655 bool has_id, const char *id, Error **errp)
Max Reitz2814f672015-10-26 21:39:10 +01002656{
2657 BlockBackend *blk;
2658 BlockDriverState *bs;
2659 AioContext *aio_context;
Kevin Wolf00949ba2016-09-20 13:38:45 +02002660 bool has_attached_device;
Max Reitz2814f672015-10-26 21:39:10 +01002661
Kevin Wolf00949ba2016-09-20 13:38:45 +02002662 device = has_device ? device : NULL;
2663 id = has_id ? id : NULL;
2664
2665 blk = qmp_get_blk(device, id, errp);
Max Reitz2814f672015-10-26 21:39:10 +01002666 if (!blk) {
Max Reitz2814f672015-10-26 21:39:10 +01002667 return;
2668 }
2669
2670 /* For BBs without a device, we can exchange the BDS tree at will */
Kevin Wolf00949ba2016-09-20 13:38:45 +02002671 has_attached_device = blk_get_attached_dev(blk);
Max Reitz2814f672015-10-26 21:39:10 +01002672
Kevin Wolf00949ba2016-09-20 13:38:45 +02002673 if (has_attached_device && !blk_dev_has_removable_media(blk)) {
2674 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002675 return;
2676 }
2677
Kevin Wolf00949ba2016-09-20 13:38:45 +02002678 if (has_attached_device && blk_dev_has_tray(blk) &&
2679 !blk_dev_is_tray_open(blk))
2680 {
2681 error_setg(errp, "Tray of device '%s' is not open", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002682 return;
2683 }
2684
2685 bs = blk_bs(blk);
2686 if (!bs) {
2687 return;
2688 }
2689
2690 aio_context = bdrv_get_aio_context(bs);
2691 aio_context_acquire(aio_context);
2692
2693 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2694 goto out;
2695 }
2696
Max Reitz2814f672015-10-26 21:39:10 +01002697 blk_remove_bs(blk);
2698
Max Reitz12c7ec82016-01-29 20:49:11 +01002699 if (!blk_dev_has_tray(blk)) {
2700 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2701 * called at all); therefore, the medium needs to be ejected here.
2702 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2703 * value passed here (i.e. false). */
Kevin Wolf39829a02017-01-24 14:21:41 +01002704 blk_dev_change_media_cb(blk, false, &error_abort);
Max Reitz12c7ec82016-01-29 20:49:11 +01002705 }
2706
Max Reitz2814f672015-10-26 21:39:10 +01002707out:
2708 aio_context_release(aio_context);
2709}
2710
Max Reitz34ce1112017-11-10 23:43:02 +01002711void qmp_blockdev_remove_medium(const char *id, Error **errp)
Max Reitz82fcf662017-11-10 23:43:01 +01002712{
2713 blockdev_remove_medium(false, NULL, true, id, errp);
2714}
2715
Kevin Wolf716df212016-09-20 13:38:44 +02002716static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
Max Reitzd1299882015-10-26 21:39:11 +01002717 BlockDriverState *bs, Error **errp)
2718{
Kevin Wolf39829a02017-01-24 14:21:41 +01002719 Error *local_err = NULL;
Max Reitzd1299882015-10-26 21:39:11 +01002720 bool has_device;
Kevin Wolfd7086422017-01-13 19:02:32 +01002721 int ret;
Max Reitzd1299882015-10-26 21:39:11 +01002722
Max Reitzd1299882015-10-26 21:39:11 +01002723 /* For BBs without a device, we can exchange the BDS tree at will */
2724 has_device = blk_get_attached_dev(blk);
2725
2726 if (has_device && !blk_dev_has_removable_media(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002727 error_setg(errp, "Device is not removable");
Max Reitzd1299882015-10-26 21:39:11 +01002728 return;
2729 }
2730
Max Reitz12c7ec82016-01-29 20:49:11 +01002731 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002732 error_setg(errp, "Tray of the device is not open");
Max Reitzd1299882015-10-26 21:39:11 +01002733 return;
2734 }
2735
2736 if (blk_bs(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002737 error_setg(errp, "There already is a medium in the device");
Max Reitzd1299882015-10-26 21:39:11 +01002738 return;
2739 }
2740
Kevin Wolfd7086422017-01-13 19:02:32 +01002741 ret = blk_insert_bs(blk, bs, errp);
2742 if (ret < 0) {
2743 return;
2744 }
Max Reitzd1299882015-10-26 21:39:11 +01002745
Max Reitz12c7ec82016-01-29 20:49:11 +01002746 if (!blk_dev_has_tray(blk)) {
2747 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2748 * called at all); therefore, the medium needs to be pushed into the
2749 * slot here.
2750 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2751 * value passed here (i.e. true). */
Kevin Wolf39829a02017-01-24 14:21:41 +01002752 blk_dev_change_media_cb(blk, true, &local_err);
2753 if (local_err) {
2754 error_propagate(errp, local_err);
2755 blk_remove_bs(blk);
2756 return;
2757 }
Max Reitz12c7ec82016-01-29 20:49:11 +01002758 }
Max Reitzd1299882015-10-26 21:39:11 +01002759}
2760
Max Reitz82fcf662017-11-10 23:43:01 +01002761static void blockdev_insert_medium(bool has_device, const char *device,
2762 bool has_id, const char *id,
2763 const char *node_name, Error **errp)
Max Reitzd1299882015-10-26 21:39:11 +01002764{
Kevin Wolf716df212016-09-20 13:38:44 +02002765 BlockBackend *blk;
Max Reitzd1299882015-10-26 21:39:11 +01002766 BlockDriverState *bs;
2767
Kevin Wolf716df212016-09-20 13:38:44 +02002768 blk = qmp_get_blk(has_device ? device : NULL,
2769 has_id ? id : NULL,
2770 errp);
2771 if (!blk) {
2772 return;
2773 }
2774
Max Reitzd1299882015-10-26 21:39:11 +01002775 bs = bdrv_find_node(node_name);
2776 if (!bs) {
2777 error_setg(errp, "Node '%s' not found", node_name);
2778 return;
2779 }
2780
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002781 if (bdrv_has_blk(bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02002782 error_setg(errp, "Node '%s' is already in use", node_name);
Max Reitzd1299882015-10-26 21:39:11 +01002783 return;
2784 }
2785
Kevin Wolf716df212016-09-20 13:38:44 +02002786 qmp_blockdev_insert_anon_medium(blk, bs, errp);
Max Reitzd1299882015-10-26 21:39:11 +01002787}
2788
Max Reitz34ce1112017-11-10 23:43:02 +01002789void qmp_blockdev_insert_medium(const char *id, const char *node_name,
2790 Error **errp)
Max Reitz82fcf662017-11-10 23:43:01 +01002791{
2792 blockdev_insert_medium(false, NULL, true, id, node_name, errp);
2793}
2794
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002795void qmp_blockdev_change_medium(bool has_device, const char *device,
2796 bool has_id, const char *id,
2797 const char *filename,
Max Reitz24fb4132015-11-06 16:27:06 +01002798 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002799 bool has_read_only,
2800 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002801 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002802{
2803 BlockBackend *blk;
2804 BlockDriverState *medium_bs = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002805 int bdrv_flags;
Kevin Wolfb85114f2016-09-12 19:08:31 +02002806 bool detect_zeroes;
Colin Lord38a53d52016-06-08 13:56:28 -04002807 int rc;
Max Reitzde2c6c02015-10-26 21:39:13 +01002808 QDict *options = NULL;
2809 Error *err = NULL;
2810
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002811 blk = qmp_get_blk(has_device ? device : NULL,
2812 has_id ? id : NULL,
2813 errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002814 if (!blk) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002815 goto fail;
2816 }
2817
2818 if (blk_bs(blk)) {
2819 blk_update_root_state(blk);
2820 }
2821
2822 bdrv_flags = blk_get_open_flags_from_root_state(blk);
Alyssa Milburn156abc22016-02-06 13:36:18 +00002823 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
Kevin Wolfe35bdc12018-10-05 18:57:40 +02002824 BDRV_O_PROTOCOL | BDRV_O_AUTO_RDONLY);
Max Reitzde2c6c02015-10-26 21:39:13 +01002825
Max Reitz39ff43d2015-11-11 04:49:44 +01002826 if (!has_read_only) {
2827 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2828 }
2829
2830 switch (read_only) {
2831 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2832 break;
2833
2834 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2835 bdrv_flags &= ~BDRV_O_RDWR;
2836 break;
2837
2838 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2839 bdrv_flags |= BDRV_O_RDWR;
2840 break;
2841
2842 default:
2843 abort();
2844 }
2845
Kevin Wolfb85114f2016-09-12 19:08:31 +02002846 options = qdict_new();
2847 detect_zeroes = blk_get_detect_zeroes_from_root_state(blk);
Eric Blake46f5ac22017-04-27 16:58:17 -05002848 qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off");
Kevin Wolfb85114f2016-09-12 19:08:31 +02002849
Max Reitz24fb4132015-11-06 16:27:06 +01002850 if (has_format) {
Eric Blake46f5ac22017-04-27 16:58:17 -05002851 qdict_put_str(options, "driver", format);
Max Reitzde2c6c02015-10-26 21:39:13 +01002852 }
2853
Max Reitz5b363932016-05-17 16:41:31 +02002854 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2855 if (!medium_bs) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002856 goto fail;
2857 }
2858
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002859 rc = do_open_tray(has_device ? device : NULL,
2860 has_id ? id : NULL,
2861 false, &err);
Colin Lord38a53d52016-06-08 13:56:28 -04002862 if (rc && rc != -ENOSYS) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002863 error_propagate(errp, err);
2864 goto fail;
2865 }
Colin Lord38a53d52016-06-08 13:56:28 -04002866 error_free(err);
2867 err = NULL;
Max Reitzde2c6c02015-10-26 21:39:13 +01002868
Max Reitz82fcf662017-11-10 23:43:01 +01002869 blockdev_remove_medium(has_device, device, has_id, id, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002870 if (err) {
2871 error_propagate(errp, err);
2872 goto fail;
2873 }
2874
Kevin Wolf716df212016-09-20 13:38:44 +02002875 qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002876 if (err) {
2877 error_propagate(errp, err);
2878 goto fail;
2879 }
2880
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002881 qmp_blockdev_close_tray(has_device, device, has_id, id, errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002882
2883fail:
2884 /* If the medium has been inserted, the device has its own reference, so
2885 * ours must be relinquished; and if it has not been inserted successfully,
2886 * the reference must be relinquished anyway */
2887 bdrv_unref(medium_bs);
2888}
2889
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002890/* throttling disk I/O limits */
Eric Blake4dc93972016-07-13 21:50:21 -06002891void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002892{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002893 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002894 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002895 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002896 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002897
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002898 blk = qmp_get_blk(arg->has_device ? arg->device : NULL,
2899 arg->has_id ? arg->id : NULL,
2900 errp);
Fam Zhenga0e85442015-03-02 19:36:48 +08002901 if (!blk) {
Luiz Capitulino80047da2011-12-14 16:49:14 -02002902 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002903 }
Max Reitz5433c242015-10-19 17:53:29 +02002904
2905 aio_context = blk_get_aio_context(blk);
2906 aio_context_acquire(aio_context);
2907
Fam Zhenga0e85442015-03-02 19:36:48 +08002908 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002909 if (!bs) {
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002910 error_setg(errp, "Device has no medium");
Max Reitz5433c242015-10-19 17:53:29 +02002911 goto out;
2912 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002913
Alberto Garcia1588ab52016-02-18 12:27:00 +02002914 throttle_config_init(&cfg);
Eric Blake4dc93972016-07-13 21:50:21 -06002915 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2916 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2917 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002918
Eric Blake4dc93972016-07-13 21:50:21 -06002919 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2920 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2921 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
Benoît Canetcc0681c2013-09-02 14:14:39 +02002922
Eric Blake4dc93972016-07-13 21:50:21 -06002923 if (arg->has_bps_max) {
2924 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002925 }
Eric Blake4dc93972016-07-13 21:50:21 -06002926 if (arg->has_bps_rd_max) {
2927 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002928 }
Eric Blake4dc93972016-07-13 21:50:21 -06002929 if (arg->has_bps_wr_max) {
2930 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002931 }
Eric Blake4dc93972016-07-13 21:50:21 -06002932 if (arg->has_iops_max) {
2933 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002934 }
Eric Blake4dc93972016-07-13 21:50:21 -06002935 if (arg->has_iops_rd_max) {
2936 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002937 }
Eric Blake4dc93972016-07-13 21:50:21 -06002938 if (arg->has_iops_wr_max) {
2939 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002940 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002941
Eric Blake4dc93972016-07-13 21:50:21 -06002942 if (arg->has_bps_max_length) {
2943 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002944 }
Eric Blake4dc93972016-07-13 21:50:21 -06002945 if (arg->has_bps_rd_max_length) {
2946 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002947 }
Eric Blake4dc93972016-07-13 21:50:21 -06002948 if (arg->has_bps_wr_max_length) {
2949 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002950 }
Eric Blake4dc93972016-07-13 21:50:21 -06002951 if (arg->has_iops_max_length) {
2952 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002953 }
Eric Blake4dc93972016-07-13 21:50:21 -06002954 if (arg->has_iops_rd_max_length) {
2955 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002956 }
Eric Blake4dc93972016-07-13 21:50:21 -06002957 if (arg->has_iops_wr_max_length) {
2958 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002959 }
2960
Eric Blake4dc93972016-07-13 21:50:21 -06002961 if (arg->has_iops_size) {
2962 cfg.op_size = arg->iops_size;
Benoît Canet2024c1d2013-09-02 14:14:41 +02002963 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002964
Alberto Garciad5851082016-02-18 12:26:59 +02002965 if (!throttle_is_valid(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002966 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002967 }
2968
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002969 if (throttle_enabled(&cfg)) {
2970 /* Enable I/O limits if they're not enabled yet, otherwise
2971 * just update the throttling group. */
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +03002972 if (!blk_get_public(blk)->throttle_group_member.throttle_state) {
Eric Blake4dc93972016-07-13 21:50:21 -06002973 blk_io_limits_enable(blk,
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002974 arg->has_group ? arg->group :
2975 arg->has_device ? arg->device :
2976 arg->id);
Eric Blake4dc93972016-07-13 21:50:21 -06002977 } else if (arg->has_group) {
2978 blk_io_limits_update_group(blk, arg->group);
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002979 }
2980 /* Set the new throttling configuration */
Kevin Wolf97148072016-03-21 13:53:52 +01002981 blk_set_io_limits(blk, &cfg);
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +03002982 } else if (blk_get_public(blk)->throttle_group_member.throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002983 /* If all throttling settings are set to 0, disable I/O limits */
Kevin Wolf97148072016-03-21 13:53:52 +01002984 blk_io_limits_disable(blk);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002985 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002986
Max Reitz5433c242015-10-19 17:53:29 +02002987out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002988 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002989}
2990
John Snow341ebc22015-04-17 19:49:52 -04002991void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2992 bool has_granularity, uint32_t granularity,
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002993 bool has_persistent, bool persistent,
Vladimir Sementsov-Ogievskiya6e2ca52018-06-11 14:53:32 -04002994 bool has_disabled, bool disabled,
John Snow341ebc22015-04-17 19:49:52 -04002995 Error **errp)
2996{
John Snow341ebc22015-04-17 19:49:52 -04002997 BlockDriverState *bs;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002998 BdrvDirtyBitmap *bitmap;
Sergio Lopez91005a42020-01-08 15:31:36 +01002999 AioContext *aio_context;
John Snow341ebc22015-04-17 19:49:52 -04003000
3001 if (!name || name[0] == '\0') {
3002 error_setg(errp, "Bitmap name cannot be empty");
3003 return;
3004 }
3005
3006 bs = bdrv_lookup_bs(node, node, errp);
3007 if (!bs) {
3008 return;
3009 }
3010
Sergio Lopez91005a42020-01-08 15:31:36 +01003011 aio_context = bdrv_get_aio_context(bs);
3012 aio_context_acquire(aio_context);
3013
John Snow341ebc22015-04-17 19:49:52 -04003014 if (has_granularity) {
3015 if (granularity < 512 || !is_power_of_2(granularity)) {
3016 error_setg(errp, "Granularity must be power of 2 "
3017 "and at least 512");
Sergio Lopez91005a42020-01-08 15:31:36 +01003018 goto out;
John Snow341ebc22015-04-17 19:49:52 -04003019 }
3020 } else {
3021 /* Default to cluster size, if available: */
3022 granularity = bdrv_get_default_bitmap_granularity(bs);
3023 }
3024
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03003025 if (!has_persistent) {
3026 persistent = false;
3027 }
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03003028
Vladimir Sementsov-Ogievskiya6e2ca52018-06-11 14:53:32 -04003029 if (!has_disabled) {
3030 disabled = false;
3031 }
3032
Vladimir Sementsov-Ogievskiyd2c30802019-09-20 11:25:43 +03003033 if (persistent &&
3034 !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp))
3035 {
Sergio Lopez91005a42020-01-08 15:31:36 +01003036 goto out;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03003037 }
3038
3039 bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp);
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03003040 if (bitmap == NULL) {
Sergio Lopez91005a42020-01-08 15:31:36 +01003041 goto out;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03003042 }
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03003043
Vladimir Sementsov-Ogievskiya6e2ca52018-06-11 14:53:32 -04003044 if (disabled) {
3045 bdrv_disable_dirty_bitmap(bitmap);
3046 }
3047
Eric Blake796a3792019-03-12 12:05:49 -04003048 bdrv_dirty_bitmap_set_persistence(bitmap, persistent);
Sergio Lopez91005a42020-01-08 15:31:36 +01003049
3050out:
3051 aio_context_release(aio_context);
John Snow341ebc22015-04-17 19:49:52 -04003052}
3053
John Snowc4e4b0f2019-07-29 16:35:54 -04003054static BdrvDirtyBitmap *do_block_dirty_bitmap_remove(
3055 const char *node, const char *name, bool release,
3056 BlockDriverState **bitmap_bs, Error **errp)
John Snow341ebc22015-04-17 19:49:52 -04003057{
John Snow341ebc22015-04-17 19:49:52 -04003058 BlockDriverState *bs;
3059 BdrvDirtyBitmap *bitmap;
Sergio Lopez91005a42020-01-08 15:31:36 +01003060 AioContext *aio_context;
John Snow341ebc22015-04-17 19:49:52 -04003061
Paolo Bonzini21198822017-06-05 14:39:03 +02003062 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
John Snow341ebc22015-04-17 19:49:52 -04003063 if (!bitmap || !bs) {
John Snowc4e4b0f2019-07-29 16:35:54 -04003064 return NULL;
John Snow341ebc22015-04-17 19:49:52 -04003065 }
3066
Sergio Lopez91005a42020-01-08 15:31:36 +01003067 aio_context = bdrv_get_aio_context(bs);
3068 aio_context_acquire(aio_context);
3069
John Snowc3edf132019-03-12 12:05:49 -04003070 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY | BDRV_BITMAP_RO,
3071 errp)) {
Sergio Lopez91005a42020-01-08 15:31:36 +01003072 aio_context_release(aio_context);
John Snowc4e4b0f2019-07-29 16:35:54 -04003073 return NULL;
John Snow9bd2b082015-04-17 19:49:57 -04003074 }
Vladimir Sementsov-Ogievskiy5c36c1a2017-06-28 15:05:29 +03003075
Vladimir Sementsov-Ogievskiyd2c30802019-09-20 11:25:43 +03003076 if (bdrv_dirty_bitmap_get_persistence(bitmap) &&
3077 bdrv_remove_persistent_dirty_bitmap(bs, name, errp) < 0)
3078 {
Sergio Lopez91005a42020-01-08 15:31:36 +01003079 aio_context_release(aio_context);
3080 return NULL;
Vladimir Sementsov-Ogievskiy5c36c1a2017-06-28 15:05:29 +03003081 }
3082
John Snowc4e4b0f2019-07-29 16:35:54 -04003083 if (release) {
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +03003084 bdrv_release_dirty_bitmap(bitmap);
John Snowc4e4b0f2019-07-29 16:35:54 -04003085 }
3086
3087 if (bitmap_bs) {
3088 *bitmap_bs = bs;
3089 }
3090
Sergio Lopez91005a42020-01-08 15:31:36 +01003091 aio_context_release(aio_context);
John Snowc4e4b0f2019-07-29 16:35:54 -04003092 return release ? NULL : bitmap;
3093}
3094
3095void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
3096 Error **errp)
3097{
3098 do_block_dirty_bitmap_remove(node, name, true, NULL, errp);
John Snow341ebc22015-04-17 19:49:52 -04003099}
3100
John Snowe74e6b72015-04-17 19:49:59 -04003101/**
3102 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
3103 * immediately after a full backup operation.
3104 */
3105void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
3106 Error **errp)
3107{
John Snowe74e6b72015-04-17 19:49:59 -04003108 BdrvDirtyBitmap *bitmap;
3109 BlockDriverState *bs;
3110
Paolo Bonzini21198822017-06-05 14:39:03 +02003111 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
John Snowe74e6b72015-04-17 19:49:59 -04003112 if (!bitmap || !bs) {
3113 return;
3114 }
3115
John Snow3ae96d62019-03-12 12:05:49 -04003116 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) {
Vladimir Sementsov-Ogievskiyd6883bc2017-06-28 15:05:10 +03003117 return;
John Snowe74e6b72015-04-17 19:49:59 -04003118 }
3119
Fam Zhengdf9a6812015-11-09 18:16:54 +08003120 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04003121}
3122
John Snow0e2b7f02018-12-21 04:35:22 -05003123void qmp_block_dirty_bitmap_enable(const char *node, const char *name,
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04003124 Error **errp)
3125{
3126 BlockDriverState *bs;
3127 BdrvDirtyBitmap *bitmap;
3128
3129 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
3130 if (!bitmap) {
3131 return;
3132 }
3133
John Snow3ae96d62019-03-12 12:05:49 -04003134 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04003135 return;
3136 }
3137
3138 bdrv_enable_dirty_bitmap(bitmap);
3139}
3140
John Snow0e2b7f02018-12-21 04:35:22 -05003141void qmp_block_dirty_bitmap_disable(const char *node, const char *name,
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04003142 Error **errp)
3143{
3144 BlockDriverState *bs;
3145 BdrvDirtyBitmap *bitmap;
3146
3147 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
3148 if (!bitmap) {
3149 return;
3150 }
3151
John Snow3ae96d62019-03-12 12:05:49 -04003152 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04003153 return;
3154 }
3155
3156 bdrv_disable_dirty_bitmap(bitmap);
3157}
3158
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04003159static BdrvDirtyBitmap *do_block_dirty_bitmap_merge(
3160 const char *node, const char *target,
3161 BlockDirtyBitmapMergeSourceList *bitmaps,
3162 HBitmap **backup, Error **errp)
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003163{
3164 BlockDriverState *bs;
John Snow360d4e42018-12-21 04:35:21 -05003165 BdrvDirtyBitmap *dst, *src, *anon;
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04003166 BlockDirtyBitmapMergeSourceList *lst;
John Snow360d4e42018-12-21 04:35:21 -05003167 Error *local_err = NULL;
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003168
John Snow360d4e42018-12-21 04:35:21 -05003169 dst = block_dirty_bitmap_lookup(node, target, &bs, errp);
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003170 if (!dst) {
John Snow360d4e42018-12-21 04:35:21 -05003171 return NULL;
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003172 }
3173
John Snow360d4e42018-12-21 04:35:21 -05003174 anon = bdrv_create_dirty_bitmap(bs, bdrv_dirty_bitmap_granularity(dst),
3175 NULL, errp);
3176 if (!anon) {
3177 return NULL;
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003178 }
3179
John Snow360d4e42018-12-21 04:35:21 -05003180 for (lst = bitmaps; lst; lst = lst->next) {
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04003181 switch (lst->value->type) {
3182 const char *name, *node;
3183 case QTYPE_QSTRING:
3184 name = lst->value->u.local;
3185 src = bdrv_find_dirty_bitmap(bs, name);
3186 if (!src) {
3187 error_setg(errp, "Dirty bitmap '%s' not found", name);
3188 dst = NULL;
3189 goto out;
3190 }
3191 break;
3192 case QTYPE_QDICT:
3193 node = lst->value->u.external.node;
3194 name = lst->value->u.external.name;
3195 src = block_dirty_bitmap_lookup(node, name, NULL, errp);
3196 if (!src) {
3197 dst = NULL;
3198 goto out;
3199 }
3200 break;
3201 default:
3202 abort();
John Snow360d4e42018-12-21 04:35:21 -05003203 }
3204
3205 bdrv_merge_dirty_bitmap(anon, src, NULL, &local_err);
3206 if (local_err) {
3207 error_propagate(errp, local_err);
3208 dst = NULL;
3209 goto out;
3210 }
3211 }
3212
3213 /* Merge into dst; dst is unchanged on failure. */
3214 bdrv_merge_dirty_bitmap(dst, anon, backup, errp);
3215
3216 out:
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +03003217 bdrv_release_dirty_bitmap(anon);
John Snow360d4e42018-12-21 04:35:21 -05003218 return dst;
3219}
3220
John Snow0e2b7f02018-12-21 04:35:22 -05003221void qmp_block_dirty_bitmap_merge(const char *node, const char *target,
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04003222 BlockDirtyBitmapMergeSourceList *bitmaps,
3223 Error **errp)
John Snow360d4e42018-12-21 04:35:21 -05003224{
3225 do_block_dirty_bitmap_merge(node, target, bitmaps, NULL, errp);
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04003226}
3227
Vladimir Sementsov-Ogievskiya3b52532017-06-28 15:05:25 +03003228BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
3229 const char *name,
3230 Error **errp)
3231{
3232 BdrvDirtyBitmap *bitmap;
3233 BlockDriverState *bs;
3234 BlockDirtyBitmapSha256 *ret = NULL;
3235 char *sha256;
3236
3237 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
3238 if (!bitmap || !bs) {
3239 return NULL;
3240 }
3241
3242 sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp);
3243 if (sha256 == NULL) {
3244 return NULL;
3245 }
3246
3247 ret = g_new(BlockDirtyBitmapSha256, 1);
3248 ret->sha256 = sha256;
3249
3250 return ret;
3251}
3252
Markus Armbruster072ebe62015-03-05 17:00:56 +01003253void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06003254{
3255 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003256 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06003257 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003258 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08003259 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06003260
Kevin Wolf2073d412016-02-23 17:50:37 +01003261 bs = bdrv_find_node(id);
3262 if (bs) {
Markus Armbruster79b7a772017-03-21 17:53:28 +01003263 qmp_blockdev_del(id, &local_err);
Kevin Wolf2073d412016-02-23 17:50:37 +01003264 if (local_err) {
3265 error_report_err(local_err);
3266 }
3267 return;
3268 }
3269
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003270 blk = blk_by_name(id);
3271 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02003272 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01003273 return;
Ryan Harper9063f812010-11-12 11:07:13 -06003274 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003275
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02003276 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02003277 error_report("Deleting device added with blockdev-add"
3278 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01003279 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02003280 }
3281
Max Reitz5433c242015-10-19 17:53:29 +02003282 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003283 aio_context_acquire(aio_context);
3284
Max Reitz5433c242015-10-19 17:53:29 +02003285 bs = blk_bs(blk);
3286 if (bs) {
3287 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
3288 error_report_err(local_err);
3289 aio_context_release(aio_context);
3290 return;
3291 }
Ryan Harper9063f812010-11-12 11:07:13 -06003292
Max Reitz938abd42016-01-29 16:36:09 +01003293 blk_remove_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02003294 }
Ryan Harper9063f812010-11-12 11:07:13 -06003295
Max Reitz7c735872016-03-16 19:54:39 +01003296 /* Make the BlockBackend and the attached BlockDriverState anonymous */
Max Reitzefaa7c42016-03-16 19:54:38 +01003297 monitor_remove_blk(blk);
3298
Max Reitz7c735872016-03-16 19:54:39 +01003299 /* If this BlockBackend has a device attached to it, its refcount will be
3300 * decremented when the device is removed; otherwise we have to do so here.
Ryan Harperd22b2f42011-03-29 20:51:47 -05003301 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02003302 if (blk_get_attached_dev(blk)) {
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02003303 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02003304 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
3305 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05003306 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02003307 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06003308 }
3309
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01003310 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06003311}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003312
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003313void qmp_block_resize(bool has_device, const char *device,
3314 bool has_node_name, const char *node_name,
3315 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003316{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003317 Error *local_err = NULL;
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01003318 BlockBackend *blk = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003319 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003320 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02003321 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003322
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003323 bs = bdrv_lookup_bs(has_device ? device : NULL,
3324 has_node_name ? node_name : NULL,
3325 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003326 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01003327 error_propagate(errp, local_err);
3328 return;
3329 }
3330
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003331 aio_context = bdrv_get_aio_context(bs);
3332 aio_context_acquire(aio_context);
3333
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003334 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003335 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003336 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003337 }
3338
Jeff Cody9c75e162014-06-25 16:55:30 -04003339 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003340 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003341 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04003342 }
3343
Kevin Wolfd861ab32019-04-25 14:25:10 +02003344 blk = blk_new(bdrv_get_aio_context(bs), BLK_PERM_RESIZE, BLK_PERM_ALL);
Kevin Wolfd7086422017-01-13 19:02:32 +01003345 ret = blk_insert_bs(blk, bs, errp);
3346 if (ret < 0) {
3347 goto out;
3348 }
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01003349
John Snow698bdfa2017-05-10 13:39:45 -04003350 bdrv_drained_begin(bs);
Max Reitzc80d8b02019-09-18 11:51:40 +02003351 ret = blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp);
John Snow698bdfa2017-05-10 13:39:45 -04003352 bdrv_drained_end(bs);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003353
3354out:
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01003355 blk_unref(blk);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01003356 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01003357}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003358
Alberto Garcia23233222016-07-05 17:28:59 +03003359void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04003360 bool has_base, const char *base,
Alberto Garcia312fe092016-10-28 10:08:19 +03003361 bool has_base_node, const char *base_node,
Jeff Cody13d8cc52014-06-25 15:40:11 -04003362 bool has_backing_file, const char *backing_file,
3363 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02003364 bool has_on_error, BlockdevOnError on_error,
John Snow241ca1a2018-09-06 09:02:23 -04003365 bool has_auto_finalize, bool auto_finalize,
3366 bool has_auto_dismiss, bool auto_dismiss,
Paolo Bonzini1d809092012-09-28 17:22:59 +02003367 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003368{
Alberto Garcia554b6142016-10-28 10:08:11 +03003369 BlockDriverState *bs, *iter;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003370 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003371 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003372 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003373 const char *base_name = NULL;
John Snowcf6320d2018-09-06 09:02:12 -04003374 int job_flags = JOB_DEFAULT;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003375
Paolo Bonzini1d809092012-09-28 17:22:59 +02003376 if (!has_on_error) {
3377 on_error = BLOCKDEV_ON_ERROR_REPORT;
3378 }
3379
Alberto Garcia554b6142016-10-28 10:08:11 +03003380 bs = bdrv_lookup_bs(device, device, errp);
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003381 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003382 return;
3383 }
3384
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02003385 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003386 aio_context_acquire(aio_context);
3387
Alberto Garcia312fe092016-10-28 10:08:19 +03003388 if (has_base && has_base_node) {
3389 error_setg(errp, "'base' and 'base-node' cannot be specified "
3390 "at the same time");
3391 goto out;
3392 }
3393
Jeff Cody13d8cc52014-06-25 15:40:11 -04003394 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003395 base_bs = bdrv_find_backing_image(bs, base);
3396 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003397 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003398 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00003399 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003400 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04003401 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003402 }
3403
Alberto Garcia312fe092016-10-28 10:08:19 +03003404 if (has_base_node) {
3405 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
3406 if (!base_bs) {
3407 goto out;
3408 }
3409 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
3410 error_setg(errp, "Node '%s' is not a backing image of '%s'",
3411 base_node, device);
3412 goto out;
3413 }
3414 assert(bdrv_get_aio_context(base_bs) == aio_context);
Max Reitzf30c66b2019-02-01 20:29:05 +01003415 bdrv_refresh_filename(base_bs);
Alberto Garcia312fe092016-10-28 10:08:19 +03003416 base_name = base_bs->filename;
3417 }
3418
Alberto Garcia554b6142016-10-28 10:08:11 +03003419 /* Check for op blockers in the whole chain between bs and base */
3420 for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) {
3421 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
3422 goto out;
3423 }
3424 }
3425
Jeff Cody13d8cc52014-06-25 15:40:11 -04003426 /* if we are streaming the entire chain, the result will have no backing
3427 * file, and specifying one is therefore an error */
3428 if (base_bs == NULL && has_backing_file) {
3429 error_setg(errp, "backing file specified, but streaming the "
3430 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003431 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003432 }
3433
3434 /* backing_file string overrides base bs filename */
3435 base_name = has_backing_file ? backing_file : base_name;
3436
John Snow241ca1a2018-09-06 09:02:23 -04003437 if (has_auto_finalize && !auto_finalize) {
3438 job_flags |= JOB_MANUAL_FINALIZE;
3439 }
3440 if (has_auto_dismiss && !auto_dismiss) {
3441 job_flags |= JOB_MANUAL_DISMISS;
3442 }
3443
Alberto Garcia23233222016-07-05 17:28:59 +03003444 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
John Snowcf6320d2018-09-06 09:02:12 -04003445 job_flags, has_speed ? speed : 0, on_error, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003446 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003447 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003448 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003449 }
3450
Vladimir Sementsov-Ogievskiyb23c5802019-06-06 18:41:32 +03003451 trace_qmp_block_stream(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003452
3453out:
3454 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003455}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003456
Alberto Garciafd62c602016-07-05 17:29:00 +03003457void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Kevin Wolf3c605f42017-06-27 17:18:20 +02003458 bool has_base_node, const char *base_node,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003459 bool has_base, const char *base,
Kevin Wolf3c605f42017-06-27 17:18:20 +02003460 bool has_top_node, const char *top_node,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003461 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003462 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04003463 bool has_speed, int64_t speed,
Kevin Wolf8faad1c2020-02-14 21:08:11 +01003464 bool has_on_error, BlockdevOnError on_error,
Kevin Wolf0db832f2017-02-20 18:10:05 +01003465 bool has_filter_node_name, const char *filter_node_name,
John Snow96fbf532018-09-06 09:02:21 -04003466 bool has_auto_finalize, bool auto_finalize,
3467 bool has_auto_dismiss, bool auto_dismiss,
Jeff Codyed61fc12012-09-27 13:29:16 -04003468 Error **errp)
3469{
3470 BlockDriverState *bs;
Alberto Garcia058223a2016-10-28 10:08:07 +03003471 BlockDriverState *iter;
Jeff Codyed61fc12012-09-27 13:29:16 -04003472 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003473 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04003474 Error *local_err = NULL;
John Snow53607822018-09-06 09:02:10 -04003475 int job_flags = JOB_DEFAULT;
Jeff Codyed61fc12012-09-27 13:29:16 -04003476
Max Reitz54504662014-04-10 19:36:25 +02003477 if (!has_speed) {
3478 speed = 0;
3479 }
Kevin Wolf8faad1c2020-02-14 21:08:11 +01003480 if (!has_on_error) {
3481 on_error = BLOCKDEV_ON_ERROR_REPORT;
3482 }
Kevin Wolf0db832f2017-02-20 18:10:05 +01003483 if (!has_filter_node_name) {
3484 filter_node_name = NULL;
3485 }
John Snow96fbf532018-09-06 09:02:21 -04003486 if (has_auto_finalize && !auto_finalize) {
3487 job_flags |= JOB_MANUAL_FINALIZE;
3488 }
3489 if (has_auto_dismiss && !auto_dismiss) {
3490 job_flags |= JOB_MANUAL_DISMISS;
3491 }
Max Reitz54504662014-04-10 19:36:25 +02003492
Jeff Cody7676e2c2014-06-30 15:14:15 +02003493 /* Important Note:
3494 * libvirt relies on the DeviceNotFound error class in order to probe for
3495 * live commit feature versions; for this to work, we must make sure to
3496 * perform the device lookup before any generic errors that may occur in a
3497 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02003498 bs = qmp_get_root_bs(device, &local_err);
3499 if (!bs) {
3500 bs = bdrv_lookup_bs(device, device, NULL);
3501 if (!bs) {
3502 error_free(local_err);
3503 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3504 "Device '%s' not found", device);
3505 } else {
3506 error_propagate(errp, local_err);
3507 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003508 return;
3509 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003510
Kevin Wolf1d13b162016-06-23 14:20:24 +02003511 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003512 aio_context_acquire(aio_context);
3513
Fam Zhengbb000212014-09-11 13:14:00 +08003514 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003515 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003516 }
3517
Jeff Codyed61fc12012-09-27 13:29:16 -04003518 /* default top_bs is the active layer */
3519 top_bs = bs;
3520
Kevin Wolf3c605f42017-06-27 17:18:20 +02003521 if (has_top_node && has_top) {
3522 error_setg(errp, "'top-node' and 'top' are mutually exclusive");
3523 goto out;
3524 } else if (has_top_node) {
3525 top_bs = bdrv_lookup_bs(NULL, top_node, errp);
3526 if (top_bs == NULL) {
3527 goto out;
3528 }
3529 if (!bdrv_chain_contains(bs, top_bs)) {
3530 error_setg(errp, "'%s' is not in this backing file chain",
3531 top_node);
3532 goto out;
3533 }
3534 } else if (has_top && top) {
Max Reitzf30c66b2019-02-01 20:29:05 +01003535 /* This strcmp() is just a shortcut, there is no need to
3536 * refresh @bs's filename. If it mismatches,
3537 * bdrv_find_backing_image() will do the refresh and may still
3538 * return @bs. */
Jeff Codyed61fc12012-09-27 13:29:16 -04003539 if (strcmp(bs->filename, top) != 0) {
3540 top_bs = bdrv_find_backing_image(bs, top);
3541 }
3542 }
3543
3544 if (top_bs == NULL) {
3545 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003546 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003547 }
3548
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003549 assert(bdrv_get_aio_context(top_bs) == aio_context);
3550
Kevin Wolf3c605f42017-06-27 17:18:20 +02003551 if (has_base_node && has_base) {
3552 error_setg(errp, "'base-node' and 'base' are mutually exclusive");
3553 goto out;
3554 } else if (has_base_node) {
3555 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
3556 if (base_bs == NULL) {
3557 goto out;
3558 }
3559 if (!bdrv_chain_contains(top_bs, base_bs)) {
3560 error_setg(errp, "'%s' is not in this backing file chain",
3561 base_node);
3562 goto out;
3563 }
3564 } else if (has_base && base) {
Jeff Codyd5208c42012-10-16 15:49:10 -04003565 base_bs = bdrv_find_backing_image(top_bs, base);
3566 } else {
3567 base_bs = bdrv_find_base(top_bs);
3568 }
3569
3570 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003571 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003572 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04003573 }
3574
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003575 assert(bdrv_get_aio_context(base_bs) == aio_context);
3576
Alberto Garcia058223a2016-10-28 10:08:07 +03003577 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
3578 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3579 goto out;
3580 }
Fam Zhengbb000212014-09-11 13:14:00 +08003581 }
3582
Jeff Cody7676e2c2014-06-30 15:14:15 +02003583 /* Do not allow attempts to commit an image into itself */
3584 if (top_bs == base_bs) {
3585 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003586 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02003587 }
3588
Fam Zheng20a63d22013-12-16 14:45:31 +08003589 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04003590 if (has_backing_file) {
3591 error_setg(errp, "'backing-file' specified,"
3592 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003593 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003594 }
John Snow47970df2016-10-27 12:06:57 -04003595 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
John Snow53607822018-09-06 09:02:10 -04003596 job_flags, speed, on_error,
Fam Zheng78bbd912017-04-21 20:27:04 +08003597 filter_node_name, NULL, NULL, false, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003598 } else {
Alberto Garcia058223a2016-10-28 10:08:07 +03003599 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
3600 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3601 goto out;
3602 }
John Snow53607822018-09-06 09:02:10 -04003603 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags,
3604 speed, on_error, has_backing_file ? backing_file : NULL,
Kevin Wolf0db832f2017-02-20 18:10:05 +01003605 filter_node_name, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003606 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003607 if (local_err != NULL) {
3608 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003609 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003610 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003611
3612out:
3613 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003614}
3615
John Snow7b0b8702019-07-29 16:35:52 -04003616/* Common QMP interface for drive-backup and blockdev-backup */
3617static BlockJob *do_backup_common(BackupCommon *backup,
3618 BlockDriverState *bs,
3619 BlockDriverState *target_bs,
3620 AioContext *aio_context,
3621 JobTxn *txn, Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003622{
John Snow111049a2016-11-08 01:50:38 -05003623 BlockJob *job = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003624 BdrvDirtyBitmap *bmap = NULL;
John Snow7b0b8702019-07-29 16:35:52 -04003625 int job_flags = JOB_DEFAULT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003626
Pavel Butsykin81206a82016-07-22 11:17:50 +03003627 if (!backup->has_speed) {
3628 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003629 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003630 if (!backup->has_on_source_error) {
3631 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003632 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003633 if (!backup->has_on_target_error) {
3634 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003635 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003636 if (!backup->has_job_id) {
3637 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003638 }
John Snowb40dacd2018-03-10 03:27:44 -05003639 if (!backup->has_auto_finalize) {
3640 backup->auto_finalize = true;
3641 }
3642 if (!backup->has_auto_dismiss) {
3643 backup->auto_dismiss = true;
3644 }
Pavel Butsykin13b94142016-07-22 11:17:52 +03003645 if (!backup->has_compress) {
3646 backup->compress = false;
3647 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003648
John Snowa6c93652019-07-29 16:35:55 -04003649 if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) ||
3650 (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) {
3651 /* done before desugaring 'incremental' to print the right message */
3652 if (!backup->has_bitmap) {
3653 error_setg(errp, "must provide a valid bitmap name for "
3654 "'%s' sync mode", MirrorSyncMode_str(backup->sync));
3655 return NULL;
3656 }
3657 }
3658
John Snowc8b56502019-07-29 16:35:52 -04003659 if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) {
3660 if (backup->has_bitmap_mode &&
3661 backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) {
3662 error_setg(errp, "Bitmap sync mode must be '%s' "
3663 "when using sync mode '%s'",
3664 BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS),
3665 MirrorSyncMode_str(backup->sync));
3666 return NULL;
3667 }
3668 backup->has_bitmap_mode = true;
3669 backup->sync = MIRROR_SYNC_MODE_BITMAP;
3670 backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS;
3671 }
3672
John Snow7b0b8702019-07-29 16:35:52 -04003673 if (backup->has_bitmap) {
3674 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
3675 if (!bmap) {
3676 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
3677 return NULL;
3678 }
John Snowc8b56502019-07-29 16:35:52 -04003679 if (!backup->has_bitmap_mode) {
3680 error_setg(errp, "Bitmap sync mode must be given "
3681 "when providing a bitmap");
3682 return NULL;
3683 }
John Snowb30ffbe2019-07-29 16:35:54 -04003684 if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snow7b0b8702019-07-29 16:35:52 -04003685 return NULL;
3686 }
John Snow1a2b8b42019-07-29 16:35:55 -04003687
3688 /* This does not produce a useful bitmap artifact: */
3689 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
3690 error_setg(errp, "sync mode '%s' does not produce meaningful bitmap"
3691 " outputs", MirrorSyncMode_str(backup->sync));
3692 return NULL;
3693 }
3694
3695 /* If the bitmap isn't used for input or output, this is useless: */
3696 if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER &&
3697 backup->sync != MIRROR_SYNC_MODE_BITMAP) {
3698 error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect"
3699 " when combined with sync mode '%s'",
3700 BitmapSyncMode_str(backup->bitmap_mode),
3701 MirrorSyncMode_str(backup->sync));
3702 return NULL;
3703 }
3704 }
3705
3706 if (!backup->has_bitmap && backup->has_bitmap_mode) {
3707 error_setg(errp, "Cannot specify bitmap sync mode without a bitmap");
3708 return NULL;
John Snow7b0b8702019-07-29 16:35:52 -04003709 }
3710
3711 if (!backup->auto_finalize) {
3712 job_flags |= JOB_MANUAL_FINALIZE;
3713 }
3714 if (!backup->auto_dismiss) {
3715 job_flags |= JOB_MANUAL_DISMISS;
3716 }
3717
3718 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
John Snowc8b56502019-07-29 16:35:52 -04003719 backup->sync, bmap, backup->bitmap_mode,
3720 backup->compress,
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +03003721 backup->filter_node_name,
John Snowc8b56502019-07-29 16:35:52 -04003722 backup->on_source_error,
3723 backup->on_target_error,
John Snow7b0b8702019-07-29 16:35:52 -04003724 job_flags, NULL, NULL, txn, errp);
3725 return job;
3726}
3727
Sergio Lopez2288ccf2020-01-08 15:31:32 +01003728void qmp_drive_backup(DriveBackup *backup, Error **errp)
John Snow7b0b8702019-07-29 16:35:52 -04003729{
Sergio Lopez2288ccf2020-01-08 15:31:32 +01003730 TransactionAction action = {
3731 .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP,
3732 .u.drive_backup.data = backup,
3733 };
3734 blockdev_do_action(&action, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003735}
3736
Peter Krempafacda542020-01-20 09:50:49 +01003737BlockDeviceInfoList *qmp_query_named_block_nodes(bool has_flat,
3738 bool flat,
3739 Error **errp)
Benoît Canetc13163f2014-01-23 21:31:34 +01003740{
Peter Krempafacda542020-01-20 09:50:49 +01003741 bool return_flat = has_flat && flat;
3742
3743 return bdrv_named_nodes_list(return_flat, errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003744}
3745
Vladimir Sementsov-Ogievskiy5d3b4e92018-12-21 20:09:07 +03003746XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp)
3747{
3748 return bdrv_get_xdbg_block_graph(errp);
3749}
3750
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01003751void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003752{
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01003753 TransactionAction action = {
3754 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP,
3755 .u.blockdev_backup.data = backup,
3756 };
3757 blockdev_do_action(&action, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003758}
3759
Fam Zheng4193cdd2015-12-24 12:45:03 +08003760/* Parameter check and block job starting for drive mirroring.
3761 * Caller should hold @device and @target's aio context (must be the same).
3762 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003763static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003764 BlockDriverState *target,
3765 bool has_replaces, const char *replaces,
3766 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003767 BlockMirrorBackingMode backing_mode,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003768 bool zero_target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003769 bool has_speed, int64_t speed,
3770 bool has_granularity, uint32_t granularity,
3771 bool has_buf_size, int64_t buf_size,
3772 bool has_on_source_error,
3773 BlockdevOnError on_source_error,
3774 bool has_on_target_error,
3775 BlockdevOnError on_target_error,
3776 bool has_unmap, bool unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003777 bool has_filter_node_name,
3778 const char *filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003779 bool has_copy_mode, MirrorCopyMode copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04003780 bool has_auto_finalize, bool auto_finalize,
3781 bool has_auto_dismiss, bool auto_dismiss,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003782 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003783{
John Snowa1999b32018-09-06 09:02:11 -04003784 int job_flags = JOB_DEFAULT;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003785
3786 if (!has_speed) {
3787 speed = 0;
3788 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003789 if (!has_on_source_error) {
3790 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3791 }
3792 if (!has_on_target_error) {
3793 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3794 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003795 if (!has_granularity) {
3796 granularity = 0;
3797 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003798 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003799 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003800 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003801 if (!has_unmap) {
3802 unmap = true;
3803 }
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003804 if (!has_filter_node_name) {
3805 filter_node_name = NULL;
3806 }
Max Reitz481deba2018-06-13 20:18:22 +02003807 if (!has_copy_mode) {
3808 copy_mode = MIRROR_COPY_MODE_BACKGROUND;
3809 }
John Snowa6b58ad2018-09-06 09:02:22 -04003810 if (has_auto_finalize && !auto_finalize) {
3811 job_flags |= JOB_MANUAL_FINALIZE;
3812 }
3813 if (has_auto_dismiss && !auto_dismiss) {
3814 job_flags |= JOB_MANUAL_DISMISS;
3815 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003816
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003817 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003818 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3819 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003820 return;
3821 }
3822 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003823 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3824 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003825 return;
3826 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003827
Fam Zheng4193cdd2015-12-24 12:45:03 +08003828 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3829 return;
3830 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003831 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3832 return;
3833 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003834
3835 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3836 sync = MIRROR_SYNC_MODE_FULL;
3837 }
3838
Max Reitz74ce9e42019-02-13 23:53:01 +01003839 if (has_replaces) {
3840 BlockDriverState *to_replace_bs;
3841 AioContext *replace_aio_context;
3842 int64_t bs_size, replace_size;
3843
3844 bs_size = bdrv_getlength(bs);
3845 if (bs_size < 0) {
3846 error_setg_errno(errp, -bs_size, "Failed to query device's size");
3847 return;
3848 }
3849
3850 to_replace_bs = check_to_replace_node(bs, replaces, errp);
3851 if (!to_replace_bs) {
3852 return;
3853 }
3854
3855 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3856 aio_context_acquire(replace_aio_context);
3857 replace_size = bdrv_getlength(to_replace_bs);
3858 aio_context_release(replace_aio_context);
3859
3860 if (replace_size < 0) {
3861 error_setg_errno(errp, -replace_size,
3862 "Failed to query the replacement node's size");
3863 return;
3864 }
3865 if (bs_size != replace_size) {
3866 error_setg(errp, "cannot replace image with a mirror image of "
3867 "different size");
3868 return;
3869 }
3870 }
3871
Fam Zheng4193cdd2015-12-24 12:45:03 +08003872 /* pass the node name to replace to mirror start since it's loose coupling
3873 * and will allow to check whether the node still exist at mirror completion
3874 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003875 mirror_start(job_id, bs, target,
John Snowa1999b32018-09-06 09:02:11 -04003876 has_replaces ? replaces : NULL, job_flags,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003877 speed, granularity, buf_size, sync, backing_mode, zero_target,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003878 on_source_error, on_target_error, unmap, filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003879 copy_mode, errp);
Fam Zheng4193cdd2015-12-24 12:45:03 +08003880}
3881
Eric Blakefaecd402016-07-14 16:37:58 -06003882void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003883{
3884 BlockDriverState *bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003885 BlockDriverState *source, *target_bs;
3886 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003887 AioContext *old_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003888 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003889 Error *local_err = NULL;
3890 QDict *options = NULL;
3891 int flags;
3892 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003893 const char *format = arg->format;
Max Reitzcdf3bc92019-07-24 19:12:30 +02003894 bool zero_target;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003895 int ret;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003896
Kevin Wolf0524e932016-06-23 14:20:24 +02003897 bs = qmp_get_root_bs(arg->device, errp);
3898 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003899 return;
3900 }
3901
Paolo Bonzinicb2af912018-02-07 17:29:20 +01003902 /* Early check to avoid creating target */
3903 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3904 return;
3905 }
3906
Kevin Wolf0524e932016-06-23 14:20:24 +02003907 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003908 aio_context_acquire(aio_context);
3909
Eric Blakefaecd402016-07-14 16:37:58 -06003910 if (!arg->has_mode) {
3911 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003912 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003913
Eric Blakefaecd402016-07-14 16:37:58 -06003914 if (!arg->has_format) {
3915 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3916 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003917 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003918
Kevin Wolf61de4c62016-03-18 17:46:45 +01003919 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003920 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003921 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3922 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003923 }
Eric Blakefaecd402016-07-14 16:37:58 -06003924 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003925 source = bs;
3926 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003927
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003928 size = bdrv_getlength(bs);
3929 if (size < 0) {
3930 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003931 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003932 }
3933
Eric Blakefaecd402016-07-14 16:37:58 -06003934 if (arg->has_replaces) {
Eric Blakefaecd402016-07-14 16:37:58 -06003935 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003936 error_setg(errp, "a node-name must be provided when replacing a"
3937 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003938 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003939 }
Benoît Canet09158f02014-06-27 18:25:25 +02003940 }
3941
Eric Blakefaecd402016-07-14 16:37:58 -06003942 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003943 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3944 } else {
3945 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3946 }
3947
John Snow2a32c6e2017-07-17 20:34:21 -04003948 /* Don't open backing image in create() */
3949 flags |= BDRV_O_NO_BACKING;
3950
Eric Blakefaecd402016-07-14 16:37:58 -06003951 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3952 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003953 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003954 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003955 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003956 bdrv_img_create(arg->target, format,
Fam Zheng92172832017-04-21 20:27:01 +08003957 NULL, NULL, NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003958 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003959 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003960 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003961 break;
3962 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3963 /* create new image with backing file */
Max Reitzf30c66b2019-02-01 20:29:05 +01003964 bdrv_refresh_filename(source);
Eric Blakefaecd402016-07-14 16:37:58 -06003965 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003966 source->filename,
3967 source->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08003968 NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003969 break;
3970 default:
3971 abort();
3972 }
3973 }
3974
Markus Armbruster84d18f02014-01-30 15:07:28 +01003975 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003976 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003977 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003978 }
3979
Max Reitze6641712015-08-26 19:47:48 +02003980 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003981 if (arg->has_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003982 qdict_put_str(options, "node-name", arg->node_name);
Benoît Canet4c828dc2014-06-16 12:00:55 +02003983 }
Max Reitze6641712015-08-26 19:47:48 +02003984 if (format) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003985 qdict_put_str(options, "driver", format);
Max Reitze6641712015-08-26 19:47:48 +02003986 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003987
Paolo Bonzinib812f672013-01-21 17:09:43 +01003988 /* Mirroring takes care of copy-on-write using the source's backing
3989 * file.
3990 */
John Snow2a32c6e2017-07-17 20:34:21 -04003991 target_bs = bdrv_open(arg->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003992 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003993 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003994 }
3995
Max Reitzcdf3bc92019-07-24 19:12:30 +02003996 zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL &&
3997 (arg->mode == NEW_IMAGE_MODE_EXISTING ||
3998 !bdrv_has_zero_init(target_bs)));
3999
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004000
4001 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
4002 old_context = bdrv_get_aio_context(target_bs);
4003 aio_context_release(aio_context);
4004 aio_context_acquire(old_context);
4005
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004006 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
4007 if (ret < 0) {
4008 bdrv_unref(target_bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004009 aio_context_release(old_context);
4010 return;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004011 }
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01004012
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004013 aio_context_release(old_context);
4014 aio_context_acquire(aio_context);
4015
Eric Blakefaecd402016-07-14 16:37:58 -06004016 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
4017 arg->has_replaces, arg->replaces, arg->sync,
Max Reitzcdf3bc92019-07-24 19:12:30 +02004018 backing_mode, zero_target,
4019 arg->has_speed, arg->speed,
Eric Blakefaecd402016-07-14 16:37:58 -06004020 arg->has_granularity, arg->granularity,
4021 arg->has_buf_size, arg->buf_size,
4022 arg->has_on_source_error, arg->on_source_error,
4023 arg->has_on_target_error, arg->on_target_error,
4024 arg->has_unmap, arg->unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01004025 false, NULL,
Max Reitz481deba2018-06-13 20:18:22 +02004026 arg->has_copy_mode, arg->copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04004027 arg->has_auto_finalize, arg->auto_finalize,
4028 arg->has_auto_dismiss, arg->auto_dismiss,
Fam Zheng4193cdd2015-12-24 12:45:03 +08004029 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02004030 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03004031 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01004032out:
4033 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02004034}
4035
Alberto Garcia71aa9862016-07-05 17:28:57 +03004036void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
4037 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08004038 bool has_replaces, const char *replaces,
4039 MirrorSyncMode sync,
4040 bool has_speed, int64_t speed,
4041 bool has_granularity, uint32_t granularity,
4042 bool has_buf_size, int64_t buf_size,
4043 bool has_on_source_error,
4044 BlockdevOnError on_source_error,
4045 bool has_on_target_error,
4046 BlockdevOnError on_target_error,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01004047 bool has_filter_node_name,
4048 const char *filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02004049 bool has_copy_mode, MirrorCopyMode copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04004050 bool has_auto_finalize, bool auto_finalize,
4051 bool has_auto_dismiss, bool auto_dismiss,
Fam Zhengdf925622015-12-24 12:45:05 +08004052 Error **errp)
4053{
4054 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08004055 BlockDriverState *target_bs;
4056 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004057 AioContext *old_context;
Max Reitz274fcce2016-06-10 20:57:47 +02004058 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08004059 Error *local_err = NULL;
Max Reitzcdf3bc92019-07-24 19:12:30 +02004060 bool zero_target;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004061 int ret;
Fam Zhengdf925622015-12-24 12:45:05 +08004062
Kevin Wolf07eec652016-06-23 14:20:24 +02004063 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08004064 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08004065 return;
4066 }
4067
4068 target_bs = bdrv_lookup_bs(target, target, errp);
4069 if (!target_bs) {
4070 return;
4071 }
4072
Max Reitzcdf3bc92019-07-24 19:12:30 +02004073 zero_target = (sync == MIRROR_SYNC_MODE_FULL);
4074
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004075 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
4076 old_context = bdrv_get_aio_context(target_bs);
Fam Zhengdf925622015-12-24 12:45:05 +08004077 aio_context = bdrv_get_aio_context(bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004078 aio_context_acquire(old_context);
Fam Zhengdf925622015-12-24 12:45:05 +08004079
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004080 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01004081
4082 aio_context_release(old_context);
4083 aio_context_acquire(aio_context);
4084
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004085 if (ret < 0) {
4086 goto out;
4087 }
Fam Zhengdf925622015-12-24 12:45:05 +08004088
Alberto Garcia71aa9862016-07-05 17:28:57 +03004089 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02004090 has_replaces, replaces, sync, backing_mode,
Max Reitzcdf3bc92019-07-24 19:12:30 +02004091 zero_target, has_speed, speed,
Fam Zhengdf925622015-12-24 12:45:05 +08004092 has_granularity, granularity,
4093 has_buf_size, buf_size,
4094 has_on_source_error, on_source_error,
4095 has_on_target_error, on_target_error,
4096 true, true,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01004097 has_filter_node_name, filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02004098 has_copy_mode, copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04004099 has_auto_finalize, auto_finalize,
4100 has_auto_dismiss, auto_dismiss,
Fam Zhengdf925622015-12-24 12:45:05 +08004101 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03004102 error_propagate(errp, local_err);
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004103out:
Fam Zhengdf925622015-12-24 12:45:05 +08004104 aio_context_release(aio_context);
4105}
4106
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03004107/* Get a block job using its ID and acquire its AioContext */
4108static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004109 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004110{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03004111 BlockJob *job;
4112
4113 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004114
Max Reitz5433c242015-10-19 17:53:29 +02004115 *aio_context = NULL;
4116
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03004117 job = block_job_get(id);
4118
4119 if (!job) {
4120 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
4121 "Block job '%s' not found", id);
4122 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004123 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004124
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03004125 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004126 aio_context_acquire(*aio_context);
4127
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03004128 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004129}
4130
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01004131void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004132{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004133 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004134 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004135
4136 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004137 return;
4138 }
4139
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01004140 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004141 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00004142}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004143
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004144void qmp_block_job_cancel(const char *device,
4145 bool has_force, bool force, Error **errp)
4146{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004147 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004148 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004149
4150 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004151 return;
4152 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004153
4154 if (!has_force) {
4155 force = false;
4156 }
4157
Kevin Wolfb15de822018-04-18 17:10:26 +02004158 if (job_user_paused(&job->job) && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04004159 error_setg(errp, "The block job for device '%s' is currently paused",
4160 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004161 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004162 }
4163
4164 trace_qmp_block_job_cancel(job);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02004165 job_user_cancel(&job->job, force, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004166out:
4167 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004168}
4169
4170void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004171{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004172 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004173 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004174
John Snow0ec4dfb2018-03-10 03:27:32 -05004175 if (!job) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004176 return;
4177 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004178
4179 trace_qmp_block_job_pause(job);
Kevin Wolfb15de822018-04-18 17:10:26 +02004180 job_user_pause(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004181 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004182}
4183
4184void qmp_block_job_resume(const char *device, Error **errp)
4185{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004186 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004187 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004188
John Snow0ec4dfb2018-03-10 03:27:32 -05004189 if (!job) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02004190 return;
4191 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004192
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02004193 trace_qmp_block_job_resume(job);
Kevin Wolfb15de822018-04-18 17:10:26 +02004194 job_user_resume(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004195 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00004196}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004197
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004198void qmp_block_job_complete(const char *device, Error **errp)
4199{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004200 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01004201 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004202
4203 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004204 return;
4205 }
4206
4207 trace_qmp_block_job_complete(job);
Kevin Wolf3453d972018-04-23 12:24:16 +02004208 job_complete(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01004209 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02004210}
4211
John Snow11b61fb2018-03-10 03:27:43 -05004212void qmp_block_job_finalize(const char *id, Error **errp)
4213{
4214 AioContext *aio_context;
4215 BlockJob *job = find_block_job(id, &aio_context, errp);
4216
4217 if (!job) {
4218 return;
4219 }
4220
4221 trace_qmp_block_job_finalize(job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02004222 job_finalize(&job->job, errp);
John Snow11b61fb2018-03-10 03:27:43 -05004223 aio_context_release(aio_context);
4224}
4225
John Snow75f71052018-03-10 03:27:36 -05004226void qmp_block_job_dismiss(const char *id, Error **errp)
4227{
4228 AioContext *aio_context;
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02004229 BlockJob *bjob = find_block_job(id, &aio_context, errp);
4230 Job *job;
John Snow75f71052018-03-10 03:27:36 -05004231
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02004232 if (!bjob) {
John Snow75f71052018-03-10 03:27:36 -05004233 return;
4234 }
4235
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02004236 trace_qmp_block_job_dismiss(bjob);
4237 job = &bjob->job;
4238 job_dismiss(&job, errp);
John Snow75f71052018-03-10 03:27:36 -05004239 aio_context_release(aio_context);
4240}
4241
Jeff Codyfa40e652014-07-01 09:52:16 +02004242void qmp_change_backing_file(const char *device,
4243 const char *image_node_name,
4244 const char *backing_file,
4245 Error **errp)
4246{
4247 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004248 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02004249 BlockDriverState *image_bs = NULL;
4250 Error *local_err = NULL;
4251 bool ro;
Jeff Codyfa40e652014-07-01 09:52:16 +02004252 int ret;
4253
Kevin Wolf7b5dca32016-06-23 14:20:24 +02004254 bs = qmp_get_root_bs(device, errp);
4255 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02004256 return;
4257 }
4258
Kevin Wolf7b5dca32016-06-23 14:20:24 +02004259 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004260 aio_context_acquire(aio_context);
4261
Jeff Codyfa40e652014-07-01 09:52:16 +02004262 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
4263 if (local_err) {
4264 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004265 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004266 }
4267
4268 if (!image_bs) {
4269 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004270 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004271 }
4272
4273 if (bdrv_find_base(image_bs) == image_bs) {
4274 error_setg(errp, "not allowing backing file change on an image "
4275 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004276 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004277 }
4278
4279 /* even though we are not necessarily operating on bs, we need it to
4280 * determine if block ops are currently prohibited on the chain */
4281 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004282 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004283 }
4284
4285 /* final sanity check */
4286 if (!bdrv_chain_contains(bs, image_bs)) {
4287 error_setg(errp, "'%s' and image file are not in the same chain",
4288 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004289 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004290 }
4291
4292 /* if not r/w, reopen to make r/w */
Jeff Codyfa40e652014-07-01 09:52:16 +02004293 ro = bdrv_is_read_only(image_bs);
4294
4295 if (ro) {
Alberto Garcia051a60f2018-11-12 16:00:38 +02004296 if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004297 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02004298 }
4299 }
4300
4301 ret = bdrv_change_backing_file(image_bs, backing_file,
4302 image_bs->drv ? image_bs->drv->format_name : "");
4303
4304 if (ret < 0) {
4305 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
4306 backing_file);
4307 /* don't exit here, so we can try to restore open flags if
4308 * appropriate */
4309 }
4310
4311 if (ro) {
Alberto Garcia051a60f2018-11-12 16:00:38 +02004312 bdrv_reopen_set_read_only(image_bs, true, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03004313 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02004314 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00004315
4316out:
4317 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02004318}
4319
Kevin Wolfabb21ac2016-02-23 17:33:24 +01004320void hmp_drive_add_node(Monitor *mon, const char *optstr)
4321{
4322 QemuOpts *opts;
4323 QDict *qdict;
4324 Error *local_err = NULL;
4325
4326 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
4327 if (!opts) {
4328 return;
4329 }
4330
4331 qdict = qemu_opts_to_qdict(opts, NULL);
4332
4333 if (!qdict_get_try_str(qdict, "node-name")) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004334 qobject_unref(qdict);
Kevin Wolfabb21ac2016-02-23 17:33:24 +01004335 error_report("'node-name' needs to be specified");
4336 goto out;
4337 }
4338
4339 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
4340 if (!bs) {
4341 error_report_err(local_err);
4342 goto out;
4343 }
4344
4345 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
4346
4347out:
4348 qemu_opts_del(opts);
4349}
4350
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004351void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
4352{
Max Reitzbe4b67b2015-10-19 17:53:09 +02004353 BlockDriverState *bs;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004354 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01004355 Visitor *v = qobject_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004356 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004357 Error *local_err = NULL;
4358
Eric Blake3b098d52016-06-09 10:48:43 -06004359 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01004360 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004361 error_propagate(errp, local_err);
4362 goto fail;
4363 }
4364
Eric Blake3b098d52016-06-09 10:48:43 -06004365 visit_complete(v, &obj);
Max Reitz7dc847e2018-02-24 16:40:29 +01004366 qdict = qobject_to(QDict, obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004367
4368 qdict_flatten(qdict);
4369
Kevin Wolf9ec88732016-09-21 14:56:11 +02004370 if (!qdict_get_try_str(qdict, "node-name")) {
4371 error_setg(errp, "'node-name' must be specified for the root node");
4372 goto fail;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004373 }
4374
Kevin Wolf9ec88732016-09-21 14:56:11 +02004375 bs = bds_tree_init(qdict, errp);
4376 if (!bs) {
4377 goto fail;
4378 }
4379
4380 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
4381
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004382fail:
Eric Blake3b098d52016-06-09 10:48:43 -06004383 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02004384}
4385
Alberto Garcia1479c732019-03-12 18:48:51 +02004386void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp)
4387{
4388 BlockDriverState *bs;
4389 AioContext *ctx;
4390 QObject *obj;
4391 Visitor *v = qobject_output_visitor_new(&obj);
4392 Error *local_err = NULL;
4393 BlockReopenQueue *queue;
4394 QDict *qdict;
4395
4396 /* Check for the selected node name */
4397 if (!options->has_node_name) {
4398 error_setg(errp, "Node name not specified");
4399 goto fail;
4400 }
4401
4402 bs = bdrv_find_node(options->node_name);
4403 if (!bs) {
4404 error_setg(errp, "Cannot find node named '%s'", options->node_name);
4405 goto fail;
4406 }
4407
4408 /* Put all options in a QDict and flatten it */
4409 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
4410 if (local_err) {
4411 error_propagate(errp, local_err);
4412 goto fail;
4413 }
4414
4415 visit_complete(v, &obj);
4416 qdict = qobject_to(QDict, obj);
4417
4418 qdict_flatten(qdict);
4419
4420 /* Perform the reopen operation */
4421 ctx = bdrv_get_aio_context(bs);
4422 aio_context_acquire(ctx);
4423 bdrv_subtree_drained_begin(bs);
4424 queue = bdrv_reopen_queue(NULL, bs, qdict, false);
4425 bdrv_reopen_multiple(queue, errp);
4426 bdrv_subtree_drained_end(bs);
4427 aio_context_release(ctx);
4428
4429fail:
4430 visit_free(v);
4431}
4432
Markus Armbruster79b7a772017-03-21 17:53:28 +01004433void qmp_blockdev_del(const char *node_name, Error **errp)
Alberto Garcia81b936a2015-11-02 16:51:55 +02004434{
4435 AioContext *aio_context;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004436 BlockDriverState *bs;
4437
Kevin Wolf9ec88732016-09-21 14:56:11 +02004438 bs = bdrv_find_node(node_name);
4439 if (!bs) {
4440 error_setg(errp, "Cannot find node %s", node_name);
Alberto Garcia81b936a2015-11-02 16:51:55 +02004441 return;
4442 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02004443 if (bdrv_has_blk(bs)) {
4444 error_setg(errp, "Node %s is in use", node_name);
4445 return;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004446 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02004447 aio_context = bdrv_get_aio_context(bs);
Alberto Garcia81b936a2015-11-02 16:51:55 +02004448 aio_context_acquire(aio_context);
4449
Kevin Wolf9ec88732016-09-21 14:56:11 +02004450 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
4451 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004452 }
4453
Paolo Bonzini70537ed2018-12-06 23:00:09 +01004454 if (!QTAILQ_IN_USE(bs, monitor_list)) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02004455 error_setg(errp, "Node %s is not owned by the monitor",
4456 bs->node_name);
4457 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02004458 }
4459
Kevin Wolf9ec88732016-09-21 14:56:11 +02004460 if (bs->refcnt > 1) {
4461 error_setg(errp, "Block device %s is in use",
4462 bdrv_get_device_or_node_name(bs));
4463 goto out;
4464 }
4465
4466 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
4467 bdrv_unref(bs);
4468
Alberto Garcia81b936a2015-11-02 16:51:55 +02004469out:
4470 aio_context_release(aio_context);
4471}
4472
Wen Congyang7f821592016-05-10 15:36:39 +08004473static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
4474 const char *child_name)
4475{
4476 BdrvChild *child;
4477
4478 QLIST_FOREACH(child, &parent_bs->children, next) {
4479 if (strcmp(child->name, child_name) == 0) {
4480 return child;
4481 }
4482 }
4483
4484 return NULL;
4485}
4486
4487void qmp_x_blockdev_change(const char *parent, bool has_child,
4488 const char *child, bool has_node,
4489 const char *node, Error **errp)
4490{
4491 BlockDriverState *parent_bs, *new_bs = NULL;
4492 BdrvChild *p_child;
4493
4494 parent_bs = bdrv_lookup_bs(parent, parent, errp);
4495 if (!parent_bs) {
4496 return;
4497 }
4498
4499 if (has_child == has_node) {
4500 if (has_child) {
4501 error_setg(errp, "The parameters child and node are in conflict");
4502 } else {
4503 error_setg(errp, "Either child or node must be specified");
4504 }
4505 return;
4506 }
4507
4508 if (has_child) {
4509 p_child = bdrv_find_child(parent_bs, child);
4510 if (!p_child) {
4511 error_setg(errp, "Node '%s' does not have child '%s'",
4512 parent, child);
4513 return;
4514 }
4515 bdrv_del_child(parent_bs, p_child, errp);
4516 }
4517
4518 if (has_node) {
4519 new_bs = bdrv_find_node(node);
4520 if (!new_bs) {
4521 error_setg(errp, "Node '%s' not found", node);
4522 return;
4523 }
4524 bdrv_add_child(parent_bs, new_bs, errp);
4525 }
4526}
4527
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004528BlockJobInfoList *qmp_query_block_jobs(Error **errp)
4529{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004530 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004531 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004532
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004533 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
John Snow559b9352016-10-27 12:06:55 -04004534 BlockJobInfoList *elem;
4535 AioContext *aio_context;
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004536
John Snow559b9352016-10-27 12:06:55 -04004537 if (block_job_is_internal(job)) {
4538 continue;
4539 }
4540 elem = g_new0(BlockJobInfoList, 1);
4541 aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004542 aio_context_acquire(aio_context);
John Snow559b9352016-10-27 12:06:55 -04004543 elem->value = block_job_query(job, errp);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004544 aio_context_release(aio_context);
John Snow559b9352016-10-27 12:06:55 -04004545 if (!elem->value) {
4546 g_free(elem);
4547 qapi_free_BlockJobInfoList(head);
4548 return NULL;
4549 }
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004550 *p_next = elem;
4551 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004552 }
4553
4554 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004555}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004556
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004557void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
Stefan Hajnoczi882e9b82017-12-07 20:13:17 +00004558 bool has_force, bool force, Error **errp)
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004559{
4560 AioContext *old_context;
4561 AioContext *new_context;
4562 BlockDriverState *bs;
4563
4564 bs = bdrv_find_node(node_name);
4565 if (!bs) {
4566 error_setg(errp, "Cannot find node %s", node_name);
4567 return;
4568 }
4569
Stefan Hajnoczi882e9b82017-12-07 20:13:17 +00004570 /* Protects against accidents. */
4571 if (!(has_force && force) && bdrv_has_blk(bs)) {
4572 error_setg(errp, "Node %s is associated with a BlockBackend and could "
4573 "be in use (use force=true to override this check)",
4574 node_name);
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004575 return;
4576 }
4577
4578 if (iothread->type == QTYPE_QSTRING) {
4579 IOThread *obj = iothread_by_id(iothread->u.s);
4580 if (!obj) {
4581 error_setg(errp, "Cannot find iothread %s", iothread->u.s);
4582 return;
4583 }
4584
4585 new_context = iothread_get_aio_context(obj);
4586 } else {
4587 new_context = qemu_get_aio_context();
4588 }
4589
4590 old_context = bdrv_get_aio_context(bs);
4591 aio_context_acquire(old_context);
4592
Kevin Wolf8ed7d422019-04-26 16:12:27 +02004593 bdrv_try_set_aio_context(bs, new_context, errp);
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00004594
4595 aio_context_release(old_context);
4596}
4597
Vladimir Sementsov-Ogievskiycb8aac32019-03-05 15:53:17 +03004598void qmp_block_latency_histogram_set(
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004599 const char *id,
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004600 bool has_boundaries, uint64List *boundaries,
4601 bool has_boundaries_read, uint64List *boundaries_read,
4602 bool has_boundaries_write, uint64List *boundaries_write,
4603 bool has_boundaries_flush, uint64List *boundaries_flush,
4604 Error **errp)
4605{
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004606 BlockBackend *blk = qmp_get_blk(NULL, id, errp);
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004607 BlockAcctStats *stats;
zhenwei pi63d53412018-11-05 11:04:56 +08004608 int ret;
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004609
4610 if (!blk) {
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004611 return;
4612 }
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004613
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004614 stats = blk_get_stats(blk);
4615
4616 if (!has_boundaries && !has_boundaries_read && !has_boundaries_write &&
4617 !has_boundaries_flush)
4618 {
4619 block_latency_histograms_clear(stats);
4620 return;
4621 }
4622
4623 if (has_boundaries || has_boundaries_read) {
zhenwei pi63d53412018-11-05 11:04:56 +08004624 ret = block_latency_histogram_set(
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004625 stats, BLOCK_ACCT_READ,
4626 has_boundaries_read ? boundaries_read : boundaries);
zhenwei pi63d53412018-11-05 11:04:56 +08004627 if (ret) {
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004628 error_setg(errp, "Device '%s' set read boundaries fail", id);
zhenwei pi63d53412018-11-05 11:04:56 +08004629 return;
4630 }
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004631 }
4632
4633 if (has_boundaries || has_boundaries_write) {
zhenwei pi63d53412018-11-05 11:04:56 +08004634 ret = block_latency_histogram_set(
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004635 stats, BLOCK_ACCT_WRITE,
4636 has_boundaries_write ? boundaries_write : boundaries);
zhenwei pi63d53412018-11-05 11:04:56 +08004637 if (ret) {
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004638 error_setg(errp, "Device '%s' set write boundaries fail", id);
zhenwei pi63d53412018-11-05 11:04:56 +08004639 return;
4640 }
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004641 }
4642
4643 if (has_boundaries || has_boundaries_flush) {
zhenwei pi63d53412018-11-05 11:04:56 +08004644 ret = block_latency_histogram_set(
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004645 stats, BLOCK_ACCT_FLUSH,
4646 has_boundaries_flush ? boundaries_flush : boundaries);
zhenwei pi63d53412018-11-05 11:04:56 +08004647 if (ret) {
Vladimir Sementsov-Ogievskiyaf0a2262019-03-05 15:53:16 +03004648 error_setg(errp, "Device '%s' set flush boundaries fail", id);
zhenwei pi63d53412018-11-05 11:04:56 +08004649 return;
4650 }
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +03004651 }
4652}
4653
Kevin Wolf00063832013-03-15 10:35:07 +01004654QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004655 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01004656 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01004657 .desc = {
4658 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004659 .name = "snapshot",
4660 .type = QEMU_OPT_BOOL,
4661 .help = "enable/disable snapshot mode",
4662 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004663 .name = "aio",
4664 .type = QEMU_OPT_STRING,
Aarushi Mehtaf80f2672020-01-20 14:18:50 +00004665 .help = "host AIO implementation (threads, native, io_uring)",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004666 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01004667 .name = BDRV_OPT_CACHE_WB,
4668 .type = QEMU_OPT_BOOL,
4669 .help = "Enable writeback mode",
4670 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004671 .name = "format",
4672 .type = QEMU_OPT_STRING,
4673 .help = "disk format (raw, qcow2, ...)",
4674 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004675 .name = "rerror",
4676 .type = QEMU_OPT_STRING,
4677 .help = "read error action",
4678 },{
4679 .name = "werror",
4680 .type = QEMU_OPT_STRING,
4681 .help = "write error action",
4682 },{
Alberto Garcia4e200cf2016-09-15 17:53:05 +03004683 .name = BDRV_OPT_READ_ONLY,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004684 .type = QEMU_OPT_BOOL,
4685 .help = "open drive file as read-only",
Pradeep Jagadeesha2a78622017-02-28 10:31:46 +01004686 },
4687
4688 THROTTLE_OPTS,
4689
4690 {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004691 .name = "throttling.group",
4692 .type = QEMU_OPT_STRING,
4693 .help = "name of the block throttling group",
4694 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004695 .name = "copy-on-read",
4696 .type = QEMU_OPT_BOOL,
4697 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004698 },{
4699 .name = "detect-zeroes",
4700 .type = QEMU_OPT_STRING,
4701 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004702 },{
4703 .name = "stats-account-invalid",
4704 .type = QEMU_OPT_BOOL,
4705 .help = "whether to account for invalid I/O operations "
4706 "in the statistics",
4707 },{
4708 .name = "stats-account-failed",
4709 .type = QEMU_OPT_BOOL,
4710 .help = "whether to account for failed I/O operations "
4711 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004712 },
4713 { /* end of list */ }
4714 },
4715};
Kevin Wolf00063832013-03-15 10:35:07 +01004716
4717QemuOptsList qemu_drive_opts = {
4718 .name = "drive",
4719 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4720 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004721 /*
4722 * no elements => accept any params
4723 * validation will happen later
4724 */
Kevin Wolf00063832013-03-15 10:35:07 +01004725 { /* end of list */ }
4726 },
4727};