blob: 257cb376825e6e386f41247ae922238f7d652c83 [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
Maxim Levitsky89802d52020-03-08 11:24:40 +020067QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
Max Reitz9c4218e2016-01-29 16:36:12 +010068 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
69
Maxim Levitsky89802d52020-03-08 11:24:40 +020070void bdrv_set_monitor_owned(BlockDriverState *bs)
71{
72 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
73}
74
Markus Armbruster19609662011-01-28 11:21:39 +010075static const char *const if_name[IF_COUNT] = {
76 [IF_NONE] = "none",
77 [IF_IDE] = "ide",
78 [IF_SCSI] = "scsi",
79 [IF_FLOPPY] = "floppy",
80 [IF_PFLASH] = "pflash",
81 [IF_MTD] = "mtd",
82 [IF_SD] = "sd",
83 [IF_VIRTIO] = "virtio",
84 [IF_XEN] = "xen",
85};
86
John Snow21dff8c2014-10-01 14:19:25 -040087static int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010088 /*
89 * Do not change these numbers! They govern how drive option
90 * index maps to unit and bus. That mapping is ABI.
91 *
Wei Jiangang547cb152016-04-26 18:12:43 +080092 * All controllers used to implement if=T drives need to support
Markus Armbruster27d6bf42011-01-28 11:21:40 +010093 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
94 * Otherwise, some index values map to "impossible" bus, unit
95 * values.
96 *
97 * For instance, if you change [IF_SCSI] to 255, -drive
98 * if=scsi,index=12 no longer means bus=1,unit=5, but
99 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
100 * the drive can't be set up. Regression.
101 */
102 [IF_IDE] = 2,
103 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +0100104};
105
John Snow21dff8c2014-10-01 14:19:25 -0400106/**
107 * Boards may call this to offer board-by-board overrides
108 * of the default, global values.
109 */
110void override_max_devs(BlockInterfaceType type, int max_devs)
111{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200112 BlockBackend *blk;
John Snow21dff8c2014-10-01 14:19:25 -0400113 DriveInfo *dinfo;
114
115 if (max_devs <= 0) {
116 return;
117 }
118
Markus Armbruster18e46a02014-10-07 13:59:06 +0200119 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
120 dinfo = blk_legacy_dinfo(blk);
John Snow21dff8c2014-10-01 14:19:25 -0400121 if (dinfo->type == type) {
122 fprintf(stderr, "Cannot override units-per-bus property of"
123 " the %s interface, because a drive of that type has"
124 " already been added.\n", if_name[type]);
125 g_assert_not_reached();
126 }
127 }
128
129 if_max_devs[type] = max_devs;
130}
131
Markus Armbruster14bafc52010-06-25 08:09:10 +0200132/*
133 * We automatically delete the drive when a device using it gets
134 * unplugged. Questionable feature, but we can't just drop it.
135 * Device models call blockdev_mark_auto_del() to schedule the
136 * automatic deletion, and generic qdev code calls blockdev_auto_del()
137 * when deletion is actually safe.
138 */
Markus Armbruster4be74632014-10-07 13:59:18 +0200139void blockdev_mark_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200140{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200141 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Vladimir Sementsov-Ogievskiy81641022019-06-06 18:41:31 +0300142 BlockJob *job;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200143
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200144 if (!dinfo) {
Kevin Wolf2d246f02013-09-18 15:14:47 +0200145 return;
146 }
147
Vladimir Sementsov-Ogievskiy81641022019-06-06 18:41:31 +0300148 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
149 if (block_job_has_bdrv(job, blk_bs(blk))) {
150 AioContext *aio_context = job->job.aio_context;
151 aio_context_acquire(aio_context);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100152
Vladimir Sementsov-Ogievskiy81641022019-06-06 18:41:31 +0300153 job_cancel(&job->job, false);
154
155 aio_context_release(aio_context);
Max Reitz5433c242015-10-19 17:53:29 +0200156 }
Paolo Bonzini12bde0e2012-03-30 13:17:10 +0200157 }
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100158
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200159 dinfo->auto_del = 1;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200160}
161
Markus Armbruster4be74632014-10-07 13:59:18 +0200162void blockdev_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200163{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200164 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200165
Ryan Harper0fc0f1f2010-12-08 10:05:00 -0600166 if (dinfo && dinfo->auto_del) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100167 monitor_remove_blk(blk);
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +0200168 blk_unref(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200169 }
170}
171
John Snowd8f94e12014-10-01 14:19:27 -0400172/**
173 * Returns the current mapping of how many units per bus
174 * a particular interface can support.
175 *
176 * A positive integer indicates n units per bus.
177 * 0 implies the mapping has not been established.
178 * -1 indicates an invalid BlockInterfaceType was given.
179 */
180int drive_get_max_devs(BlockInterfaceType type)
181{
182 if (type >= IF_IDE && type < IF_COUNT) {
183 return if_max_devs[type];
184 }
185
186 return -1;
187}
188
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100189static int drive_index_to_bus_id(BlockInterfaceType type, int index)
190{
191 int max_devs = if_max_devs[type];
192 return max_devs ? index / max_devs : 0;
193}
194
195static int drive_index_to_unit_id(BlockInterfaceType type, int index)
196{
197 int max_devs = if_max_devs[type];
198 return max_devs ? index % max_devs : index;
199}
200
Markus Armbruster2292dda2011-01-28 11:21:41 +0100201QemuOpts *drive_def(const char *optstr)
202{
Markus Armbruster70b94332015-02-13 12:50:26 +0100203 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100204}
205
206QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +0100207 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +0200208{
Markus Armbruster666daa62010-06-02 18:48:27 +0200209 QemuOpts *opts;
210
Markus Armbruster2292dda2011-01-28 11:21:41 +0100211 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200212 if (!opts) {
213 return NULL;
214 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100215 if (type != IF_DEFAULT) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100216 qemu_opt_set(opts, "if", if_name[type], &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100217 }
218 if (index >= 0) {
Markus Armbrustera8b18f82015-02-13 15:50:43 +0100219 qemu_opt_set_number(opts, "index", index, &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100220 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200221 if (file)
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100222 qemu_opt_set(opts, "file", file, &error_abort);
Markus Armbruster666daa62010-06-02 18:48:27 +0200223 return opts;
224}
225
226DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
227{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200228 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200229 DriveInfo *dinfo;
230
Markus Armbruster18e46a02014-10-07 13:59:06 +0200231 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
232 dinfo = blk_legacy_dinfo(blk);
233 if (dinfo && dinfo->type == type
234 && dinfo->bus == bus && dinfo->unit == unit) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200235 return dinfo;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200236 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200237 }
238
239 return NULL;
240}
241
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100242void drive_check_orphaned(void)
John Snowa66c9dc2014-10-01 14:19:24 -0400243{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200244 BlockBackend *blk;
John Snowa66c9dc2014-10-01 14:19:24 -0400245 DriveInfo *dinfo;
Markus Armbruster664cc622017-02-15 11:05:45 +0100246 Location loc;
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100247 bool orphans = false;
John Snowa66c9dc2014-10-01 14:19:24 -0400248
Markus Armbruster18e46a02014-10-07 13:59:06 +0200249 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
250 dinfo = blk_legacy_dinfo(blk);
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200251 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
John Snowa66c9dc2014-10-01 14:19:24 -0400252 dinfo->type != IF_NONE) {
Markus Armbruster664cc622017-02-15 11:05:45 +0100253 loc_push_none(&loc);
254 qemu_opts_loc_restore(dinfo->opts);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100255 error_report("machine type does not support"
Markus Armbruster664cc622017-02-15 11:05:45 +0100256 " if=%s,bus=%d,unit=%d",
257 if_name[dinfo->type], dinfo->bus, dinfo->unit);
258 loc_pop(&loc);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100259 orphans = true;
John Snowa66c9dc2014-10-01 14:19:24 -0400260 }
261 }
262
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100263 if (orphans) {
264 exit(1);
265 }
John Snowa66c9dc2014-10-01 14:19:24 -0400266}
267
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100268DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
269{
270 return drive_get(type,
271 drive_index_to_bus_id(type, index),
272 drive_index_to_unit_id(type, index));
273}
274
Markus Armbruster666daa62010-06-02 18:48:27 +0200275int drive_get_max_bus(BlockInterfaceType type)
276{
277 int max_bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200278 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200279 DriveInfo *dinfo;
280
281 max_bus = -1;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200282 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
283 dinfo = blk_legacy_dinfo(blk);
284 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200285 max_bus = dinfo->bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200286 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200287 }
288 return max_bus;
289}
290
Markus Armbruster13839972011-01-28 11:21:37 +0100291/* Get a block device. This should only be used for single-drive devices
292 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
293 appropriate bus. */
294DriveInfo *drive_get_next(BlockInterfaceType type)
295{
296 static int next_block_unit[IF_COUNT];
297
298 return drive_get(type, 0, next_block_unit[type]++);
299}
300
Markus Armbruster666daa62010-06-02 18:48:27 +0200301static void bdrv_format_print(void *opaque, const char *name)
302{
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200303 qemu_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200304}
305
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000306typedef struct {
307 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800308 BlockDriverState *bs;
309} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000310
Kevin Wolfb6810722013-09-20 11:33:11 +0200311static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200312{
313 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200314 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200315 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200316 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200317 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200318 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200319 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200320 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200321 } else {
Kevin Wolfb6810722013-09-20 11:33:11 +0200322 error_setg(errp, "'%s' invalid %s error action",
323 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200324 return -1;
325 }
326}
327
Alberto Garcia40119ef2015-11-16 11:28:38 +0200328static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
329 Error **errp)
330{
331 const QListEntry *entry;
332 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
333 switch (qobject_type(entry->value)) {
334
335 case QTYPE_QSTRING: {
336 unsigned long long length;
Max Reitz7dc847e2018-02-24 16:40:29 +0100337 const char *str = qstring_get_str(qobject_to(QString,
338 entry->value));
Alberto Garcia40119ef2015-11-16 11:28:38 +0200339 if (parse_uint_full(str, &length, 10) == 0 &&
340 length > 0 && length <= UINT_MAX) {
341 block_acct_add_interval(stats, (unsigned) length);
342 } else {
343 error_setg(errp, "Invalid interval length: %s", str);
344 return false;
345 }
346 break;
347 }
348
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400349 case QTYPE_QNUM: {
Max Reitz7dc847e2018-02-24 16:40:29 +0100350 int64_t length = qnum_get_int(qobject_to(QNum, entry->value));
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400351
Alberto Garcia40119ef2015-11-16 11:28:38 +0200352 if (length > 0 && length <= UINT_MAX) {
353 block_acct_add_interval(stats, (unsigned) length);
354 } else {
355 error_setg(errp, "Invalid interval length: %" PRId64, length);
356 return false;
357 }
358 break;
359 }
360
361 default:
362 error_setg(errp, "The specification of stats-intervals is invalid");
363 return false;
364 }
365 }
366 return true;
367}
368
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200369typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
370
Max Reitzfbf81752015-10-19 17:53:31 +0200371/* All parameters but @opts are optional and may be set to NULL. */
372static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
373 const char **throttling_group, ThrottleConfig *throttle_cfg,
374 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
375{
Max Reitzfbf81752015-10-19 17:53:31 +0200376 Error *local_error = NULL;
377 const char *aio;
378
379 if (bdrv_flags) {
Max Reitzfbf81752015-10-19 17:53:31 +0200380 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
381 *bdrv_flags |= BDRV_O_COPY_ON_READ;
382 }
383
Max Reitzfbf81752015-10-19 17:53:31 +0200384 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
Aarushi Mehtaf80f2672020-01-20 14:18:50 +0000385 if (bdrv_parse_aio(aio, bdrv_flags) < 0) {
386 error_setg(errp, "invalid aio option");
387 return;
Max Reitzfbf81752015-10-19 17:53:31 +0200388 }
389 }
390 }
391
392 /* disk I/O throttling */
393 if (throttling_group) {
394 *throttling_group = qemu_opt_get(opts, "throttling.group");
395 }
396
397 if (throttle_cfg) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200398 throttle_config_init(throttle_cfg);
Max Reitzfbf81752015-10-19 17:53:31 +0200399 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
400 qemu_opt_get_number(opts, "throttling.bps-total", 0);
401 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
402 qemu_opt_get_number(opts, "throttling.bps-read", 0);
403 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
404 qemu_opt_get_number(opts, "throttling.bps-write", 0);
405 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
406 qemu_opt_get_number(opts, "throttling.iops-total", 0);
407 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
408 qemu_opt_get_number(opts, "throttling.iops-read", 0);
409 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
410 qemu_opt_get_number(opts, "throttling.iops-write", 0);
411
412 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
413 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
414 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
415 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
416 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
417 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
418 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
419 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
420 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
421 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
422 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
423 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
424
Alberto Garcia8a0fc182016-02-18 12:27:02 +0200425 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
426 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
427 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
428 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
429 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
430 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
431 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
432 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
433 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
434 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
435 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
436 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
437
Max Reitzfbf81752015-10-19 17:53:31 +0200438 throttle_cfg->op_size =
439 qemu_opt_get_number(opts, "throttling.iops-size", 0);
440
Alberto Garciad5851082016-02-18 12:26:59 +0200441 if (!throttle_is_valid(throttle_cfg, errp)) {
Max Reitzfbf81752015-10-19 17:53:31 +0200442 return;
443 }
444 }
445
446 if (detect_zeroes) {
447 *detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200448 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Max Reitzfbf81752015-10-19 17:53:31 +0200449 qemu_opt_get(opts, "detect-zeroes"),
Max Reitzfbf81752015-10-19 17:53:31 +0200450 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
451 &local_error);
452 if (local_error) {
453 error_propagate(errp, local_error);
454 return;
455 }
Max Reitzfbf81752015-10-19 17:53:31 +0200456 }
457}
458
Kevin Wolff298d072013-09-10 12:01:20 +0200459/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200460static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
461 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200462{
463 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200464 int bdrv_flags = 0;
465 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200466 bool account_invalid, account_failed;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300467 bool writethrough, read_only;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200468 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200469 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200470 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200471 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100472 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100473 QemuOpts *opts;
Alberto Garcia40119ef2015-11-16 11:28:38 +0200474 QDict *interval_dict = NULL;
475 QList *interval_list = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100476 const char *id;
Max Reitzfbf81752015-10-19 17:53:31 +0200477 BlockdevDetectZeroesOptions detect_zeroes =
478 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
479 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200480
Kevin Wolff298d072013-09-10 12:01:20 +0200481 /* Check common options by copying from bs_opts to opts, all other options
482 * stay in bs_opts for processing by bdrv_open(). */
483 id = qdict_get_try_str(bs_opts, "id");
Kevin Wolf00063832013-03-15 10:35:07 +0100484 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100485 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200486 error_propagate(errp, error);
Markus Armbruster6376f952014-05-28 11:17:01 +0200487 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100488 }
489
Kevin Wolf00063832013-03-15 10:35:07 +0100490 qemu_opts_absorb_qdict(opts, bs_opts, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100491 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200492 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100493 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100494 }
495
496 if (id) {
497 qdict_del(bs_opts, "id");
498 }
499
Markus Armbruster666daa62010-06-02 18:48:27 +0200500 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200501 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200502
Alberto Garcia362e9292015-10-28 17:33:04 +0200503 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
504 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
505
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100506 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
507
Alberto Garciaff356ee2016-07-08 17:03:00 +0300508 id = qemu_opts_id(opts);
509
Alberto Garcia40119ef2015-11-16 11:28:38 +0200510 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
511 qdict_array_split(interval_dict, &interval_list);
512
513 if (qdict_size(interval_dict) != 0) {
514 error_setg(errp, "Invalid option stats-intervals.%s",
515 qdict_first(interval_dict)->key);
516 goto early_err;
517 }
Alberto Garcia2be55062015-10-28 17:33:07 +0200518
Max Reitzfbf81752015-10-19 17:53:31 +0200519 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
520 &detect_zeroes, &error);
521 if (error) {
522 error_propagate(errp, error);
523 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200524 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200525
526 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100527 if (is_help_option(buf)) {
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200528 qemu_printf("Supported formats:");
Andrey Shinkevich9ac404c2019-03-07 16:33:58 +0300529 bdrv_iterate_format(bdrv_format_print, NULL, false);
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200530 qemu_printf("\nSupported formats (read-only):");
Andrey Shinkevich9ac404c2019-03-07 16:33:58 +0300531 bdrv_iterate_format(bdrv_format_print, NULL, true);
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200532 qemu_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100533 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200534 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200535
Max Reitze4342ce2015-02-05 13:58:14 -0500536 if (qdict_haskey(bs_opts, "driver")) {
537 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100538 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400539 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500540 qdict_put_str(bs_opts, "driver", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200541 }
542
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200543 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200544 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200545 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100546 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200547 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100548 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200549 }
550 }
551
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200552 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200553 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200554 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100555 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200556 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100557 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200558 }
559 }
560
Max Reitz5ec18f82015-10-19 17:53:30 +0200561 if (snapshot) {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200562 bdrv_flags |= BDRV_O_SNAPSHOT;
Max Reitz5ec18f82015-10-19 17:53:30 +0200563 }
564
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300565 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
566
Kevin Wolf326642b2013-07-11 12:52:34 +0200567 /* init */
Kevin Wolf39c4ae92015-11-13 14:45:42 +0100568 if ((!file || !*file) && !qdict_size(bs_opts)) {
Max Reitz5ec18f82015-10-19 17:53:30 +0200569 BlockBackendRootState *blk_rs;
570
Kevin Wolfd861ab32019-04-25 14:25:10 +0200571 blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
Max Reitz5ec18f82015-10-19 17:53:30 +0200572 blk_rs = blk_get_root_state(blk);
573 blk_rs->open_flags = bdrv_flags;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300574 blk_rs->read_only = read_only;
Max Reitz5ec18f82015-10-19 17:53:30 +0200575 blk_rs->detect_zeroes = detect_zeroes;
576
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200577 qobject_unref(bs_opts);
Max Reitze4342ce2015-02-05 13:58:14 -0500578 } else {
579 if (file && !*file) {
580 file = NULL;
581 }
582
Kevin Wolf91a097e2015-05-08 17:49:53 +0200583 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
584 * with other callers) rather than what we want as the real defaults.
585 * Apply the defaults here instead. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200586 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
587 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300588 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
589 read_only ? "on" : "off");
Kevin Wolf9384a442018-10-17 18:24:30 +0200590 qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on");
Kevin Wolf72e775c2016-03-15 14:34:37 +0100591 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200592
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100593 if (runstate_check(RUN_STATE_INMIGRATE)) {
594 bdrv_flags |= BDRV_O_INACTIVE;
595 }
596
Max Reitzefaa7c42016-03-16 19:54:38 +0100597 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500598 if (!blk) {
599 goto err_no_bs_opts;
600 }
601 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500602
Max Reitz5ec18f82015-10-19 17:53:30 +0200603 bs->detect_zeroes = detect_zeroes;
604
Paolo Bonzini9caa6f32017-06-05 14:39:07 +0200605 block_acct_setup(blk_get_stats(blk), account_invalid, account_failed);
Alberto Garcia2be55062015-10-28 17:33:07 +0200606
Alberto Garcia40119ef2015-11-16 11:28:38 +0200607 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
608 blk_unref(blk);
609 blk = NULL;
610 goto err_no_bs_opts;
Alberto Garcia2be55062015-10-28 17:33:07 +0200611 }
Max Reitz5ec18f82015-10-19 17:53:30 +0200612 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200613
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100614 /* disk I/O throttling */
615 if (throttle_enabled(&cfg)) {
616 if (!throttling_group) {
Alberto Garciaff356ee2016-07-08 17:03:00 +0300617 throttling_group = id;
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100618 }
619 blk_io_limits_enable(blk, throttling_group);
620 blk_set_io_limits(blk, &cfg);
621 }
622
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100623 blk_set_enable_write_cache(blk, !writethrough);
Max Reitz373340b2015-10-19 17:53:22 +0200624 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200625
Alberto Garciaff356ee2016-07-08 17:03:00 +0300626 if (!monitor_add_blk(blk, id, errp)) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100627 blk_unref(blk);
628 blk = NULL;
629 goto err_no_bs_opts;
630 }
631
Max Reitze4342ce2015-02-05 13:58:14 -0500632err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100633 qemu_opts_del(opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200634 qobject_unref(interval_dict);
635 qobject_unref(interval_list);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200636 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100637
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100638early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100639 qemu_opts_del(opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200640 qobject_unref(interval_dict);
641 qobject_unref(interval_list);
Markus Armbruster6376f952014-05-28 11:17:01 +0200642err_no_opts:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200643 qobject_unref(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100644 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200645}
646
Max Reitzbd745e22015-10-19 17:53:32 +0200647/* Takes the ownership of bs_opts */
Maxim Levitsky89802d52020-03-08 11:24:40 +0200648BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
Max Reitzbd745e22015-10-19 17:53:32 +0200649{
Max Reitzbd745e22015-10-19 17:53:32 +0200650 int bdrv_flags = 0;
651
Kevin Wolfa81d6162016-03-07 14:23:04 +0100652 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
653 * with other callers) rather than what we want as the real defaults.
654 * Apply the defaults here instead. */
Kevin Wolfa81d6162016-03-07 14:23:04 +0100655 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
656 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300657 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
Kevin Wolfa81d6162016-03-07 14:23:04 +0100658
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100659 if (runstate_check(RUN_STATE_INMIGRATE)) {
660 bdrv_flags |= BDRV_O_INACTIVE;
661 }
662
Kevin Wolf74e1ae72016-09-20 20:48:52 +0200663 return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
Max Reitzbd745e22015-10-19 17:53:32 +0200664}
665
Max Reitz9c4218e2016-01-29 16:36:12 +0100666void blockdev_close_all_bdrv_states(void)
667{
668 BlockDriverState *bs, *next_bs;
669
670 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
671 AioContext *ctx = bdrv_get_aio_context(bs);
672
673 aio_context_acquire(ctx);
674 bdrv_unref(bs);
675 aio_context_release(ctx);
676 }
677}
678
Max Reitz262b4e82016-03-16 19:54:41 +0100679/* Iterates over the list of monitor-owned BlockDriverStates */
680BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
681{
682 return bs ? QTAILQ_NEXT(bs, monitor_list)
683 : QTAILQ_FIRST(&monitor_bdrv_states);
684}
685
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200686static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
687 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200688{
689 const char *value;
690
691 value = qemu_opt_get(opts, from);
692 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200693 if (qemu_opt_find(opts, to)) {
694 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
695 "same time", to, from);
696 return;
697 }
Jun Li20d6cd42014-09-24 13:45:27 +0800698 }
699
700 /* rename all items in opts */
701 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100702 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200703 qemu_opt_unset(opts, from);
704 }
705}
706
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200707QemuOptsList qemu_legacy_drive_opts = {
708 .name = "drive",
709 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
710 .desc = {
711 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200712 .name = "bus",
713 .type = QEMU_OPT_NUMBER,
714 .help = "bus number",
715 },{
716 .name = "unit",
717 .type = QEMU_OPT_NUMBER,
718 .help = "unit number (i.e. lun for scsi)",
719 },{
720 .name = "index",
721 .type = QEMU_OPT_NUMBER,
722 .help = "index number",
723 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200724 .name = "media",
725 .type = QEMU_OPT_STRING,
726 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200727 },{
728 .name = "if",
729 .type = QEMU_OPT_STRING,
730 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200731 },{
Max Reitzd095b462013-12-20 19:28:14 +0100732 .name = "file",
733 .type = QEMU_OPT_STRING,
734 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200735 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200736
737 /* Options that are passed on, but have special semantics with -drive */
738 {
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300739 .name = BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200740 .type = QEMU_OPT_BOOL,
741 .help = "open drive file as read-only",
742 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100743 .name = "rerror",
744 .type = QEMU_OPT_STRING,
745 .help = "read error action",
746 },{
747 .name = "werror",
748 .type = QEMU_OPT_STRING,
749 .help = "write error action",
750 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200751 .name = "copy-on-read",
752 .type = QEMU_OPT_BOOL,
753 .help = "copy read data from backing file into image file",
754 },
755
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200756 { /* end of list */ }
757 },
758};
759
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200760DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,
761 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200762{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200763 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200764 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200765 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200766 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200767 QemuOpts *legacy_opts;
768 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200769 BlockInterfaceType type;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200770 int max_devs, bus_id, unit_id, index;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100771 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800772 bool read_only = false;
773 bool copy_on_read;
Max Reitzd095b462013-12-20 19:28:14 +0100774 const char *filename;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200775 Error *local_err = NULL;
Kevin Wolf247147f2014-09-24 16:37:14 +0200776 int i;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200777
Kevin Wolf57975222013-07-17 14:41:54 +0200778 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200779 static const struct {
780 const char *from;
781 const char *to;
782 } opt_renames[] = {
783 { "iops", "throttling.iops-total" },
784 { "iops_rd", "throttling.iops-read" },
785 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200786
Kevin Wolf247147f2014-09-24 16:37:14 +0200787 { "bps", "throttling.bps-total" },
788 { "bps_rd", "throttling.bps-read" },
789 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200790
Kevin Wolf247147f2014-09-24 16:37:14 +0200791 { "iops_max", "throttling.iops-total-max" },
792 { "iops_rd_max", "throttling.iops-read-max" },
793 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200794
Kevin Wolf247147f2014-09-24 16:37:14 +0200795 { "bps_max", "throttling.bps-total-max" },
796 { "bps_rd_max", "throttling.bps-read-max" },
797 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200798
Kevin Wolf247147f2014-09-24 16:37:14 +0200799 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200800
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200801 { "group", "throttling.group" },
802
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300803 { "readonly", BDRV_OPT_READ_ONLY },
Kevin Wolf247147f2014-09-24 16:37:14 +0200804 };
805
806 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200807 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
808 &local_err);
809 if (local_err) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200810 error_propagate(errp, local_err);
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200811 return NULL;
812 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200813 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200814
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200815 value = qemu_opt_get(all_opts, "cache");
816 if (value) {
817 int flags = 0;
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100818 bool writethrough;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200819
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100820 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200821 error_setg(errp, "invalid cache option");
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200822 return NULL;
823 }
824
825 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200826 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
827 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100828 !writethrough, &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200829 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200830 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
831 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100832 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200833 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200834 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
835 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100836 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200837 }
838 qemu_opt_unset(all_opts, "cache");
839 }
840
Kevin Wolff298d072013-09-10 12:01:20 +0200841 /* Get a QDict for processing the options */
842 bs_opts = qdict_new();
843 qemu_opts_to_qdict(all_opts, bs_opts);
844
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800845 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
846 &error_abort);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200847 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100848 if (local_err) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200849 error_propagate(errp, local_err);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200850 goto fail;
851 }
852
853 /* Media type */
854 value = qemu_opt_get(legacy_opts, "media");
855 if (value) {
856 if (!strcmp(value, "disk")) {
857 media = MEDIA_DISK;
858 } else if (!strcmp(value, "cdrom")) {
859 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800860 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200861 } else {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200862 error_setg(errp, "'%s' invalid media", value);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200863 goto fail;
864 }
865 }
866
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200867 /* copy-on-read is disabled with a warning for read-only devices */
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300868 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200869 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
870
871 if (read_only && copy_on_read) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700872 warn_report("disabling copy-on-read on read-only drive");
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200873 copy_on_read = false;
874 }
875
Eric Blake46f5ac22017-04-27 16:58:17 -0500876 qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off");
877 qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off");
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200878
Kevin Wolf593d4642013-08-28 17:24:51 +0200879 /* Controller type */
880 value = qemu_opt_get(legacy_opts, "if");
881 if (value) {
882 for (type = 0;
883 type < IF_COUNT && strcmp(value, if_name[type]);
884 type++) {
885 }
886 if (type == IF_COUNT) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200887 error_setg(errp, "unsupported bus type '%s'", value);
Kevin Wolf593d4642013-08-28 17:24:51 +0200888 goto fail;
889 }
890 } else {
891 type = block_default_type;
892 }
893
Kevin Wolf87a899c2013-09-10 15:48:13 +0200894 /* Device address specified by bus/unit or index.
895 * If none was specified, try to find the first free one. */
896 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
897 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
898 index = qemu_opt_get_number(legacy_opts, "index", -1);
899
900 max_devs = if_max_devs[type];
901
902 if (index != -1) {
903 if (bus_id != 0 || unit_id != -1) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200904 error_setg(errp, "index cannot be used with bus and unit");
Kevin Wolf87a899c2013-09-10 15:48:13 +0200905 goto fail;
906 }
907 bus_id = drive_index_to_bus_id(type, index);
908 unit_id = drive_index_to_unit_id(type, index);
909 }
910
911 if (unit_id == -1) {
912 unit_id = 0;
913 while (drive_get(type, bus_id, unit_id) != NULL) {
914 unit_id++;
915 if (max_devs && unit_id >= max_devs) {
916 unit_id -= max_devs;
917 bus_id++;
918 }
919 }
920 }
921
922 if (max_devs && unit_id >= max_devs) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200923 error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1);
Kevin Wolf87a899c2013-09-10 15:48:13 +0200924 goto fail;
925 }
926
927 if (drive_get(type, bus_id, unit_id) != NULL) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200928 error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists",
929 bus_id, unit_id, index);
Kevin Wolf87a899c2013-09-10 15:48:13 +0200930 goto fail;
931 }
932
933 /* no id supplied -> create one */
934 if (qemu_opts_id(all_opts) == NULL) {
935 char *new_id;
936 const char *mediastr = "";
937 if (type == IF_IDE || type == IF_SCSI) {
938 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
939 }
940 if (max_devs) {
941 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
942 mediastr, unit_id);
943 } else {
944 new_id = g_strdup_printf("%s%s%i", if_name[type],
945 mediastr, unit_id);
946 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500947 qdict_put_str(bs_opts, "id", new_id);
Kevin Wolf87a899c2013-09-10 15:48:13 +0200948 g_free(new_id);
949 }
950
Kevin Wolf394c7d42013-09-13 14:09:17 +0200951 /* Add virtio block device */
Kevin Wolf394c7d42013-09-13 14:09:17 +0200952 if (type == IF_VIRTIO) {
953 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800954 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
955 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200956 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +0200957 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200958 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100959 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200960 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100961 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
962 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200963 }
964
Max Reitzd095b462013-12-20 19:28:14 +0100965 filename = qemu_opt_get(legacy_opts, "file");
966
Kevin Wolfee13ed12014-02-09 09:52:32 +0100967 /* Check werror/rerror compatibility with if=... */
968 werror = qemu_opt_get(legacy_opts, "werror");
969 if (werror != NULL) {
970 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
971 type != IF_NONE) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200972 error_setg(errp, "werror is not supported by this bus type");
Kevin Wolfee13ed12014-02-09 09:52:32 +0100973 goto fail;
974 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500975 qdict_put_str(bs_opts, "werror", werror);
Kevin Wolfee13ed12014-02-09 09:52:32 +0100976 }
977
978 rerror = qemu_opt_get(legacy_opts, "rerror");
979 if (rerror != NULL) {
980 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
981 type != IF_NONE) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200982 error_setg(errp, "rerror is not supported by this bus type");
Kevin Wolfee13ed12014-02-09 09:52:32 +0100983 goto fail;
984 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500985 qdict_put_str(bs_opts, "rerror", rerror);
Kevin Wolfee13ed12014-02-09 09:52:32 +0100986 }
987
Kevin Wolf2d246f02013-09-18 15:14:47 +0200988 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200989 blk = blockdev_init(filename, bs_opts, &local_err);
Markus Armbruster3cb0e252014-05-28 11:17:02 +0200990 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200991 if (!blk) {
Markus Armbrusterb2322002018-12-13 18:31:13 +0100992 error_propagate(errp, local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +0200993 goto fail;
Kevin Wolfb6810722013-09-20 11:33:11 +0200994 } else {
Markus Armbruster84d18f02014-01-30 15:07:28 +0100995 assert(!local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +0200996 }
997
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200998 /* Create legacy DriveInfo */
999 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001000 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001001
Kevin Wolfee13ed12014-02-09 09:52:32 +01001002 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001003 dinfo->bus = bus_id;
1004 dinfo->unit = unit_id;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001005
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001006 blk_set_legacy_dinfo(blk, dinfo);
1007
Kevin Wolfe34ef042013-09-19 14:24:10 +02001008 switch(type) {
1009 case IF_IDE:
1010 case IF_SCSI:
1011 case IF_XEN:
1012 case IF_NONE:
1013 dinfo->media_cd = media == MEDIA_CDROM;
1014 break;
1015 default:
1016 break;
1017 }
1018
Kevin Wolf2d246f02013-09-18 15:14:47 +02001019fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001020 qemu_opts_del(legacy_opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001021 qobject_unref(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001022 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001023}
1024
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02001025static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1026{
1027 BlockDriverState *bs;
1028
1029 bs = bdrv_lookup_bs(name, name, errp);
1030 if (bs == NULL) {
1031 return NULL;
1032 }
1033
1034 if (!bdrv_is_root_node(bs)) {
1035 error_setg(errp, "Need a root block node");
1036 return NULL;
1037 }
1038
1039 if (!bdrv_is_inserted(bs)) {
1040 error_setg(errp, "Device has no medium");
1041 return NULL;
1042 }
1043
1044 return bs;
1045}
1046
Eric Blake10f75902016-03-03 09:16:50 -07001047static void blockdev_do_action(TransactionAction *action, Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001048{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001049 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001050
Eric Blake10f75902016-03-03 09:16:50 -07001051 list.value = action;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001052 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001053 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001054}
1055
Benoît Canet0901f672014-01-23 21:31:38 +01001056void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1057 bool has_node_name, const char *node_name,
1058 const char *snapshot_file,
1059 bool has_snapshot_node_name,
1060 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001061 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001062 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001063{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001064 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001065 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001066 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001067 .has_node_name = has_node_name,
1068 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001069 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001070 .has_snapshot_node_name = has_snapshot_node_name,
1071 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001072 .has_format = has_format,
1073 .format = (char *) format,
1074 .has_mode = has_mode,
1075 .mode = mode,
1076 };
Eric Blake10f75902016-03-03 09:16:50 -07001077 TransactionAction action = {
1078 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001079 .u.blockdev_snapshot_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001080 };
1081 blockdev_do_action(&action, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001082}
1083
Alberto Garcia43de7e22015-10-26 14:27:16 +02001084void qmp_blockdev_snapshot(const char *node, const char *overlay,
1085 Error **errp)
1086{
1087 BlockdevSnapshot snapshot_data = {
1088 .node = (char *) node,
1089 .overlay = (char *) overlay
1090 };
Eric Blake10f75902016-03-03 09:16:50 -07001091 TransactionAction action = {
1092 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
Eric Blake32bafa82016-03-17 16:48:37 -06001093 .u.blockdev_snapshot.data = &snapshot_data,
Eric Blake10f75902016-03-03 09:16:50 -07001094 };
1095 blockdev_do_action(&action, errp);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001096}
1097
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001098void qmp_blockdev_snapshot_internal_sync(const char *device,
1099 const char *name,
1100 Error **errp)
1101{
1102 BlockdevSnapshotInternal snapshot = {
1103 .device = (char *) device,
1104 .name = (char *) name
1105 };
Eric Blake10f75902016-03-03 09:16:50 -07001106 TransactionAction action = {
1107 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001108 .u.blockdev_snapshot_internal_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001109 };
1110 blockdev_do_action(&action, errp);
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001111}
1112
Wenchao Xia44e3e052013-09-11 14:04:36 +08001113SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1114 bool has_id,
1115 const char *id,
1116 bool has_name,
1117 const char *name,
1118 Error **errp)
1119{
Fam Zhenga0e85442015-03-02 19:36:48 +08001120 BlockDriverState *bs;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001121 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001122 QEMUSnapshotInfo sn;
1123 Error *local_err = NULL;
1124 SnapshotInfo *info = NULL;
1125 int ret;
1126
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001127 bs = qmp_get_root_bs(device, errp);
1128 if (!bs) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001129 return NULL;
1130 }
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001131 aio_context = bdrv_get_aio_context(bs);
Max Reitz5433c242015-10-19 17:53:29 +02001132 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001133
1134 if (!has_id) {
1135 id = NULL;
1136 }
1137
1138 if (!has_name) {
1139 name = NULL;
1140 }
1141
1142 if (!id && !name) {
1143 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001144 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001145 }
1146
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001147 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1148 goto out_aio_context;
1149 }
1150
Wenchao Xia44e3e052013-09-11 14:04:36 +08001151 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001152 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001153 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001154 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001155 }
1156 if (!ret) {
1157 error_setg(errp,
1158 "Snapshot with id '%s' and name '%s' does not exist on "
1159 "device '%s'",
1160 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001161 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001162 }
1163
1164 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001165 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001166 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001167 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001168 }
1169
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001170 aio_context_release(aio_context);
1171
Markus Armbruster5839e532014-08-19 10:31:08 +02001172 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001173 info->id = g_strdup(sn.id_str);
1174 info->name = g_strdup(sn.name);
1175 info->date_nsec = sn.date_nsec;
1176 info->date_sec = sn.date_sec;
1177 info->vm_state_size = sn.vm_state_size;
1178 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1179 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1180
1181 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001182
1183out_aio_context:
1184 aio_context_release(aio_context);
1185 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001186}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001187
John Snow341ebc22015-04-17 19:49:52 -04001188/**
1189 * block_dirty_bitmap_lookup:
1190 * Return a dirty bitmap (if present), after validating
1191 * the node reference and bitmap names.
1192 *
1193 * @node: The name of the BDS node to search for bitmaps
1194 * @name: The name of the bitmap to search for
1195 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
John Snow341ebc22015-04-17 19:49:52 -04001196 * @errp: Output pointer for error information. Can be NULL.
1197 *
1198 * @return: A bitmap object on success, or NULL on failure.
1199 */
1200static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1201 const char *name,
1202 BlockDriverState **pbs,
John Snow341ebc22015-04-17 19:49:52 -04001203 Error **errp)
1204{
1205 BlockDriverState *bs;
1206 BdrvDirtyBitmap *bitmap;
John Snow341ebc22015-04-17 19:49:52 -04001207
1208 if (!node) {
1209 error_setg(errp, "Node cannot be NULL");
1210 return NULL;
1211 }
1212 if (!name) {
1213 error_setg(errp, "Bitmap name cannot be NULL");
1214 return NULL;
1215 }
1216 bs = bdrv_lookup_bs(node, node, NULL);
1217 if (!bs) {
1218 error_setg(errp, "Node '%s' not found", node);
1219 return NULL;
1220 }
1221
John Snow341ebc22015-04-17 19:49:52 -04001222 bitmap = bdrv_find_dirty_bitmap(bs, name);
1223 if (!bitmap) {
1224 error_setg(errp, "Dirty bitmap '%s' not found", name);
Paolo Bonzini21198822017-06-05 14:39:03 +02001225 return NULL;
John Snow341ebc22015-04-17 19:49:52 -04001226 }
1227
1228 if (pbs) {
1229 *pbs = bs;
1230 }
John Snow341ebc22015-04-17 19:49:52 -04001231
1232 return bitmap;
John Snow341ebc22015-04-17 19:49:52 -04001233}
1234
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001235/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001236
John Snow50f43f02015-11-05 18:13:09 -05001237typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001238
John Snow50f43f02015-11-05 18:13:09 -05001239/**
1240 * BlkActionOps:
1241 * Table of operations that define an Action.
1242 *
1243 * @instance_size: Size of state struct, in bytes.
1244 * @prepare: Prepare the work, must NOT be NULL.
1245 * @commit: Commit the changes, can be NULL.
1246 * @abort: Abort the changes on fail, can be NULL.
1247 * @clean: Clean up resources after all transaction actions have called
1248 * commit() or abort(). Can be NULL.
1249 *
1250 * Only prepare() may fail. In a single transaction, only one of commit() or
1251 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001252 */
John Snow50f43f02015-11-05 18:13:09 -05001253typedef struct BlkActionOps {
1254 size_t instance_size;
1255 void (*prepare)(BlkActionState *common, Error **errp);
1256 void (*commit)(BlkActionState *common);
1257 void (*abort)(BlkActionState *common);
1258 void (*clean)(BlkActionState *common);
1259} BlkActionOps;
1260
1261/**
1262 * BlkActionState:
1263 * Describes one Action's state within a Transaction.
1264 *
1265 * @action: QAPI-defined enum identifying which Action to perform.
1266 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001267 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001268 * @entry: List membership for all Actions in this Transaction.
1269 *
1270 * This structure must be arranged as first member in a subclassed type,
1271 * assuming that the compiler will also arrange it to the same offsets as the
1272 * base class.
1273 */
1274struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001275 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001276 const BlkActionOps *ops;
Kevin Wolf62c9e412018-04-19 16:09:52 +02001277 JobTxn *block_job_txn;
John Snow94d16a62015-11-05 18:13:18 -05001278 TransactionProperties *txn_props;
John Snowf4de0f82019-01-11 11:59:16 -06001279 QTAILQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001280};
1281
Wenchao Xiabbe86012013-09-11 14:04:34 +08001282/* internal snapshot private data */
1283typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001284 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001285 BlockDriverState *bs;
1286 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001287 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001288} InternalSnapshotState;
1289
John Snow94d16a62015-11-05 18:13:18 -05001290
1291static int action_check_completion_mode(BlkActionState *s, Error **errp)
1292{
1293 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1294 error_setg(errp,
1295 "Action '%s' does not support Transaction property "
1296 "completion-mode = %s",
Markus Armbruster977c7362017-08-24 10:46:08 +02001297 TransactionActionKind_str(s->action->type),
1298 ActionCompletionMode_str(s->txn_props->completion_mode));
John Snow94d16a62015-11-05 18:13:18 -05001299 return -1;
1300 }
1301 return 0;
1302}
1303
John Snow50f43f02015-11-05 18:13:09 -05001304static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001305 Error **errp)
1306{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001307 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001308 const char *device;
1309 const char *name;
1310 BlockDriverState *bs;
1311 QEMUSnapshotInfo old_sn, *sn;
1312 bool ret;
1313 qemu_timeval tv;
1314 BlockdevSnapshotInternal *internal;
1315 InternalSnapshotState *state;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001316 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001317 int ret1;
1318
Eric Blake6a8f9662015-10-26 16:34:54 -06001319 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001320 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001321 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001322 state = DO_UPCAST(InternalSnapshotState, common, common);
1323
1324 /* 1. parse input */
1325 device = internal->device;
1326 name = internal->name;
1327
1328 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001329 if (action_check_completion_mode(common, errp) < 0) {
1330 return;
1331 }
1332
Kevin Wolf75dfd402016-06-23 14:20:24 +02001333 bs = qmp_get_root_bs(device, errp);
1334 if (!bs) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001335 return;
1336 }
1337
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001338 aio_context = bdrv_get_aio_context(bs);
1339 aio_context_acquire(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001340
Fam Zheng507306c2015-10-23 11:08:13 +08001341 state->bs = bs;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001342
1343 /* Paired with .clean() */
Fam Zheng507306c2015-10-23 11:08:13 +08001344 bdrv_drained_begin(bs);
1345
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001346 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001347 goto out;
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001348 }
1349
Wenchao Xiabbe86012013-09-11 14:04:34 +08001350 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001351 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001352 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001353 }
1354
1355 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001356 error_setg(errp, "Block format '%s' used by device '%s' "
1357 "does not support internal snapshots",
1358 bs->drv->format_name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001359 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001360 }
1361
1362 if (!strlen(name)) {
1363 error_setg(errp, "Name is empty");
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001364 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001365 }
1366
1367 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001368 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1369 &local_err);
1370 if (local_err) {
1371 error_propagate(errp, local_err);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001372 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001373 } else if (ret) {
1374 error_setg(errp,
1375 "Snapshot with name '%s' already exists on device '%s'",
1376 name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001377 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001378 }
1379
1380 /* 3. take the snapshot */
1381 sn = &state->sn;
1382 pstrcpy(sn->name, sizeof(sn->name), name);
1383 qemu_gettimeofday(&tv);
1384 sn->date_sec = tv.tv_sec;
1385 sn->date_nsec = tv.tv_usec * 1000;
1386 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1387
1388 ret1 = bdrv_snapshot_create(bs, sn);
1389 if (ret1 < 0) {
1390 error_setg_errno(errp, -ret1,
1391 "Failed to create snapshot '%s' on device '%s'",
1392 name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001393 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001394 }
1395
1396 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001397 state->created = true;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001398
1399out:
1400 aio_context_release(aio_context);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001401}
1402
John Snow50f43f02015-11-05 18:13:09 -05001403static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001404{
1405 InternalSnapshotState *state =
1406 DO_UPCAST(InternalSnapshotState, common, common);
1407 BlockDriverState *bs = state->bs;
1408 QEMUSnapshotInfo *sn = &state->sn;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001409 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001410 Error *local_error = NULL;
1411
Fam Zheng507306c2015-10-23 11:08:13 +08001412 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001413 return;
1414 }
1415
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001416 aio_context = bdrv_get_aio_context(state->bs);
1417 aio_context_acquire(aio_context);
1418
Wenchao Xiabbe86012013-09-11 14:04:34 +08001419 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001420 error_reportf_err(local_error,
1421 "Failed to delete snapshot with id '%s' and "
1422 "name '%s' on device '%s' in abort: ",
1423 sn->id_str, sn->name,
1424 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001425 }
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001426
1427 aio_context_release(aio_context);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001428}
1429
John Snow50f43f02015-11-05 18:13:09 -05001430static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001431{
1432 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1433 common, common);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001434 AioContext *aio_context;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001435
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001436 if (!state->bs) {
1437 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001438 }
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001439
1440 aio_context = bdrv_get_aio_context(state->bs);
1441 aio_context_acquire(aio_context);
1442
1443 bdrv_drained_end(state->bs);
1444
1445 aio_context_release(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001446}
1447
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001448/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001449typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001450 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001451 BlockDriverState *old_bs;
1452 BlockDriverState *new_bs;
Kevin Wolf067acf22017-03-02 15:26:18 +01001453 bool overlay_appended;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001454} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001455
John Snow50f43f02015-11-05 18:13:09 -05001456static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001457 Error **errp)
1458{
Max Reitz5b363932016-05-17 16:41:31 +02001459 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001460 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001461 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001462 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001463 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001464 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001465 /* Reference to the new image (for 'blockdev-snapshot') */
1466 const char *snapshot_ref;
1467 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001468 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001469 ExternalSnapshotState *state =
1470 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001471 TransactionAction *action = common->action;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001472 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001473 AioContext *old_context;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02001474 int ret;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001475
Alberto Garcia43de7e22015-10-26 14:27:16 +02001476 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1477 * purpose but a different set of parameters */
1478 switch (action->type) {
1479 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1480 {
Eric Blake32bafa82016-03-17 16:48:37 -06001481 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001482 device = s->node;
1483 node_name = s->node;
1484 new_image_file = NULL;
1485 snapshot_ref = s->overlay;
1486 }
1487 break;
1488 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1489 {
Eric Blake32bafa82016-03-17 16:48:37 -06001490 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001491 device = s->has_device ? s->device : NULL;
1492 node_name = s->has_node_name ? s->node_name : NULL;
1493 new_image_file = s->snapshot_file;
1494 snapshot_ref = NULL;
1495 }
1496 break;
1497 default:
1498 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001499 }
1500
1501 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001502 if (action_check_completion_mode(common, errp) < 0) {
1503 return;
1504 }
1505
Alberto Garcia43de7e22015-10-26 14:27:16 +02001506 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1507 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001508 return;
1509 }
1510
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001511 aio_context = bdrv_get_aio_context(state->old_bs);
1512 aio_context_acquire(aio_context);
1513
1514 /* Paired with .clean() */
Fam Zhengda763e82015-10-23 11:08:10 +08001515 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001516
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001517 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001518 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001519 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001520 }
1521
Fam Zheng3718d8a2014-05-23 21:29:43 +08001522 if (bdrv_op_is_blocked(state->old_bs,
1523 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001524 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001525 }
1526
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001527 if (!bdrv_is_read_only(state->old_bs)) {
1528 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001529 error_setg(errp, QERR_IO_ERROR);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001530 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001531 }
1532 }
1533
Alberto Garcia43de7e22015-10-26 14:27:16 +02001534 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001535 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001536 const char *format = s->has_format ? s->format : "qcow2";
1537 enum NewImageMode mode;
1538 const char *snapshot_node_name =
1539 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001540
Alberto Garcia43de7e22015-10-26 14:27:16 +02001541 if (node_name && !snapshot_node_name) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001542 error_setg(errp, "New overlay node name missing");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001543 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001544 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001545
1546 if (snapshot_node_name &&
1547 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001548 error_setg(errp, "New overlay node name already in use");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001549 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001550 }
1551
1552 flags = state->old_bs->open_flags;
John Snow2a32c6e2017-07-17 20:34:21 -04001553 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ);
1554 flags |= BDRV_O_NO_BACKING;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001555
1556 /* create new image w/backing file */
1557 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1558 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001559 int64_t size = bdrv_getlength(state->old_bs);
1560 if (size < 0) {
1561 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001562 goto out;
Kevin Wolff86b8b52016-03-02 12:16:44 +01001563 }
Max Reitzf30c66b2019-02-01 20:29:05 +01001564 bdrv_refresh_filename(state->old_bs);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001565 bdrv_img_create(new_image_file, format,
1566 state->old_bs->filename,
1567 state->old_bs->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08001568 NULL, size, flags, false, &local_err);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001569 if (local_err) {
1570 error_propagate(errp, local_err);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001571 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001572 }
1573 }
1574
1575 options = qdict_new();
Peter Maydelld52e1a02018-11-01 16:30:37 +00001576 if (snapshot_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05001577 qdict_put_str(options, "node-name", snapshot_node_name);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001578 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001579 qdict_put_str(options, "driver", format);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001580 }
1581
Max Reitz5b363932016-05-17 16:41:31 +02001582 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1583 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001584 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001585 if (!state->new_bs) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001586 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001587 }
1588
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001589 if (bdrv_has_blk(state->new_bs)) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001590 error_setg(errp, "The overlay is already in use");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001591 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001592 }
1593
1594 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1595 errp)) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001596 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001597 }
1598
1599 if (state->new_bs->backing != NULL) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001600 error_setg(errp, "The overlay already has a backing image");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001601 goto out;
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001602 }
1603
1604 if (!state->new_bs->drv->supports_backing) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001605 error_setg(errp, "The overlay does not support backing images");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001606 goto out;
Kevin Wolfb2c28322017-02-20 12:46:42 +01001607 }
1608
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001609 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1610 old_context = bdrv_get_aio_context(state->new_bs);
1611 aio_context_release(aio_context);
1612 aio_context_acquire(old_context);
1613
Kevin Wolf8ed7d422019-04-26 16:12:27 +02001614 ret = bdrv_try_set_aio_context(state->new_bs, aio_context, errp);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001615
1616 aio_context_release(old_context);
1617 aio_context_acquire(aio_context);
1618
Kevin Wolf8ed7d422019-04-26 16:12:27 +02001619 if (ret < 0) {
1620 goto out;
1621 }
Fam Zhengc26a5ab2017-04-07 14:54:09 +08001622
Kevin Wolfb2c28322017-02-20 12:46:42 +01001623 /* This removes our old bs and adds the new bs. This is an operation that
1624 * can fail, so we need to do it in .prepare; undoing it for abort is
1625 * always possible. */
1626 bdrv_ref(state->new_bs);
1627 bdrv_append(state->new_bs, state->old_bs, &local_err);
1628 if (local_err) {
1629 error_propagate(errp, local_err);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001630 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001631 }
Kevin Wolf067acf22017-03-02 15:26:18 +01001632 state->overlay_appended = true;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001633
1634out:
1635 aio_context_release(aio_context);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001636}
1637
John Snow50f43f02015-11-05 18:13:09 -05001638static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001639{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001640 ExternalSnapshotState *state =
1641 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001642 AioContext *aio_context;
1643
1644 aio_context = bdrv_get_aio_context(state->old_bs);
1645 aio_context_acquire(aio_context);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001646
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001647 /* We don't need (or want) to use the transactional
1648 * bdrv_reopen_multiple() across all the entries at once, because we
1649 * don't want to abort all of them if one of them fails the reopen */
Paolo Bonzinid3faa132017-06-05 14:38:50 +02001650 if (!atomic_read(&state->old_bs->copy_on_read)) {
Alberto Garcia1b577742018-11-12 16:00:39 +02001651 bdrv_reopen_set_read_only(state->old_bs, true, NULL);
Kevin Wolf4c844982016-02-29 13:12:26 +01001652 }
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001653
1654 aio_context_release(aio_context);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001655}
1656
John Snow50f43f02015-11-05 18:13:09 -05001657static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001658{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001659 ExternalSnapshotState *state =
1660 DO_UPCAST(ExternalSnapshotState, common, common);
1661 if (state->new_bs) {
Kevin Wolf067acf22017-03-02 15:26:18 +01001662 if (state->overlay_appended) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001663 AioContext *aio_context;
Sergio Lopez377410f2020-01-08 15:31:37 +01001664 AioContext *tmp_context;
1665 int ret;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001666
1667 aio_context = bdrv_get_aio_context(state->old_bs);
1668 aio_context_acquire(aio_context);
1669
Jeff Cody719fc282017-06-07 09:55:22 -04001670 bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
1671 close state->old_bs; we need it */
1672 bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
Sergio Lopez377410f2020-01-08 15:31:37 +01001673
1674 /*
1675 * The call to bdrv_set_backing_hd() above returns state->old_bs to
1676 * the main AioContext. As we're still going to be using it, return
1677 * it to the AioContext it was before.
1678 */
1679 tmp_context = bdrv_get_aio_context(state->old_bs);
1680 if (aio_context != tmp_context) {
1681 aio_context_release(aio_context);
1682 aio_context_acquire(tmp_context);
1683
1684 ret = bdrv_try_set_aio_context(state->old_bs,
1685 aio_context, NULL);
1686 assert(ret == 0);
1687
1688 aio_context_release(tmp_context);
1689 aio_context_acquire(aio_context);
1690 }
1691
Kevin Wolf5fe31c22017-03-06 16:20:51 +01001692 bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
Jeff Cody719fc282017-06-07 09:55:22 -04001693 bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001694
1695 aio_context_release(aio_context);
Kevin Wolfb2c28322017-02-20 12:46:42 +01001696 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001697 }
Fam Zhengda763e82015-10-23 11:08:10 +08001698}
1699
John Snow50f43f02015-11-05 18:13:09 -05001700static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001701{
1702 ExternalSnapshotState *state =
1703 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001704 AioContext *aio_context;
1705
1706 if (!state->old_bs) {
1707 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001708 }
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001709
1710 aio_context = bdrv_get_aio_context(state->old_bs);
1711 aio_context_acquire(aio_context);
1712
1713 bdrv_drained_end(state->old_bs);
1714 bdrv_unref(state->new_bs);
1715
1716 aio_context_release(aio_context);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001717}
1718
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001719typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001720 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001721 BlockDriverState *bs;
1722 BlockJob *job;
1723} DriveBackupState;
1724
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001725static BlockJob *do_backup_common(BackupCommon *backup,
1726 BlockDriverState *bs,
1727 BlockDriverState *target_bs,
1728 AioContext *aio_context,
1729 JobTxn *txn, Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001730
John Snow50f43f02015-11-05 18:13:09 -05001731static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001732{
1733 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1734 DriveBackup *backup;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001735 BlockDriverState *bs;
1736 BlockDriverState *target_bs;
1737 BlockDriverState *source = NULL;
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001738 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001739 AioContext *old_context;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001740 QDict *options;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001741 Error *local_err = NULL;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001742 int flags;
1743 int64_t size;
1744 bool set_backing_hd = false;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001745 int ret;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001746
Eric Blake6a8f9662015-10-26 16:34:54 -06001747 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001748 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001749
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001750 if (!backup->has_mode) {
1751 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1752 }
1753
Vladimir Sementsov-Ogievskiy85c9d132019-06-18 17:08:04 +03001754 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001755 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001756 return;
1757 }
1758
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001759 if (!bs->drv) {
1760 error_setg(errp, "Device has no medium");
1761 return;
1762 }
1763
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001764 aio_context = bdrv_get_aio_context(bs);
1765 aio_context_acquire(aio_context);
1766
1767 /* Paired with .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001768 bdrv_drained_begin(bs);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001769
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001770 if (!backup->has_format) {
1771 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
1772 NULL : (char *) bs->drv->format_name;
1773 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001774
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001775 /* Early check to avoid creating target */
1776 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
1777 goto out;
1778 }
1779
1780 flags = bs->open_flags | BDRV_O_RDWR;
1781
1782 /*
1783 * See if we have a backing HD we can use to create our new image
1784 * on top of.
1785 */
1786 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
1787 source = backing_bs(bs);
1788 if (!source) {
1789 backup->sync = MIRROR_SYNC_MODE_FULL;
1790 }
1791 }
1792 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
1793 source = bs;
1794 flags |= BDRV_O_NO_BACKING;
1795 set_backing_hd = true;
1796 }
1797
1798 size = bdrv_getlength(bs);
1799 if (size < 0) {
1800 error_setg_errno(errp, -size, "bdrv_getlength failed");
1801 goto out;
1802 }
1803
1804 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
1805 assert(backup->format);
1806 if (source) {
1807 bdrv_refresh_filename(source);
1808 bdrv_img_create(backup->target, backup->format, source->filename,
1809 source->drv->format_name, NULL,
1810 size, flags, false, &local_err);
1811 } else {
1812 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
1813 size, flags, false, &local_err);
1814 }
1815 }
1816
Markus Armbruster84d18f02014-01-30 15:07:28 +01001817 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001818 error_propagate(errp, local_err);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001819 goto out;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001820 }
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001821
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001822 options = qdict_new();
1823 qdict_put_str(options, "discard", "unmap");
1824 qdict_put_str(options, "detect-zeroes", "unmap");
1825 if (backup->format) {
1826 qdict_put_str(options, "driver", backup->format);
1827 }
1828
1829 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
1830 if (!target_bs) {
1831 goto out;
1832 }
1833
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001834 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1835 old_context = bdrv_get_aio_context(target_bs);
1836 aio_context_release(aio_context);
1837 aio_context_acquire(old_context);
1838
1839 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
1840 if (ret < 0) {
1841 bdrv_unref(target_bs);
1842 aio_context_release(old_context);
1843 return;
1844 }
1845
1846 aio_context_release(old_context);
1847 aio_context_acquire(aio_context);
1848
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001849 if (set_backing_hd) {
1850 bdrv_set_backing_hd(target_bs, source, &local_err);
1851 if (local_err) {
1852 goto unref;
1853 }
1854 }
1855
1856 state->bs = bs;
1857
1858 state->job = do_backup_common(qapi_DriveBackup_base(backup),
1859 bs, target_bs, aio_context,
1860 common->block_job_txn, errp);
1861
1862unref:
1863 bdrv_unref(target_bs);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001864out:
1865 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05001866}
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001867
John Snow111049a2016-11-08 01:50:38 -05001868static void drive_backup_commit(BlkActionState *common)
1869{
1870 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001871 AioContext *aio_context;
1872
1873 aio_context = bdrv_get_aio_context(state->bs);
1874 aio_context_acquire(aio_context);
1875
John Snow111049a2016-11-08 01:50:38 -05001876 assert(state->job);
Kevin Wolfda01ff72018-04-13 17:31:02 +02001877 job_start(&state->job->job);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001878
1879 aio_context_release(aio_context);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001880}
1881
John Snow50f43f02015-11-05 18:13:09 -05001882static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001883{
1884 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001885
John Snow111049a2016-11-08 01:50:38 -05001886 if (state->job) {
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001887 AioContext *aio_context;
1888
1889 aio_context = bdrv_get_aio_context(state->bs);
1890 aio_context_acquire(aio_context);
1891
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001892 job_cancel_sync(&state->job->job);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001893
1894 aio_context_release(aio_context);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001895 }
1896}
1897
John Snow50f43f02015-11-05 18:13:09 -05001898static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001899{
1900 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001901 AioContext *aio_context;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001902
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001903 if (!state->bs) {
1904 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001905 }
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001906
1907 aio_context = bdrv_get_aio_context(state->bs);
1908 aio_context_acquire(aio_context);
1909
1910 bdrv_drained_end(state->bs);
1911
1912 aio_context_release(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001913}
1914
Fam Zhengbd8baec2014-12-18 18:37:06 +08001915typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001916 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001917 BlockDriverState *bs;
1918 BlockJob *job;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001919} BlockdevBackupState;
1920
John Snow50f43f02015-11-05 18:13:09 -05001921static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001922{
1923 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1924 BlockdevBackup *backup;
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01001925 BlockDriverState *bs;
1926 BlockDriverState *target_bs;
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001927 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001928 AioContext *old_context;
1929 int ret;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001930
Eric Blake6a8f9662015-10-26 16:34:54 -06001931 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001932 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001933
Vladimir Sementsov-Ogievskiy930fe172018-07-02 15:46:29 -04001934 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001935 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001936 return;
1937 }
1938
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01001939 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
1940 if (!target_bs) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001941 return;
1942 }
1943
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001944 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001945 aio_context = bdrv_get_aio_context(bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001946 old_context = bdrv_get_aio_context(target_bs);
1947 aio_context_acquire(old_context);
1948
1949 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
1950 if (ret < 0) {
1951 aio_context_release(old_context);
1952 return;
1953 }
1954
1955 aio_context_release(old_context);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001956 aio_context_acquire(aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001957 state->bs = bs;
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001958
1959 /* Paired with .clean() */
Fam Zhengff52bf32015-10-23 11:08:12 +08001960 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001961
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01001962 state->job = do_backup_common(qapi_BlockdevBackup_base(backup),
1963 bs, target_bs, aio_context,
1964 common->block_job_txn, errp);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001965
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001966 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05001967}
Fam Zhengbd8baec2014-12-18 18:37:06 +08001968
John Snow111049a2016-11-08 01:50:38 -05001969static void blockdev_backup_commit(BlkActionState *common)
1970{
1971 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001972 AioContext *aio_context;
1973
1974 aio_context = bdrv_get_aio_context(state->bs);
1975 aio_context_acquire(aio_context);
1976
John Snow111049a2016-11-08 01:50:38 -05001977 assert(state->job);
Kevin Wolfda01ff72018-04-13 17:31:02 +02001978 job_start(&state->job->job);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001979
1980 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001981}
1982
John Snow50f43f02015-11-05 18:13:09 -05001983static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001984{
1985 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001986
John Snow111049a2016-11-08 01:50:38 -05001987 if (state->job) {
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001988 AioContext *aio_context;
1989
1990 aio_context = bdrv_get_aio_context(state->bs);
1991 aio_context_acquire(aio_context);
1992
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001993 job_cancel_sync(&state->job->job);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001994
1995 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001996 }
1997}
1998
John Snow50f43f02015-11-05 18:13:09 -05001999static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08002000{
2001 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002002 AioContext *aio_context;
Fam Zhengbd8baec2014-12-18 18:37:06 +08002003
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002004 if (!state->bs) {
2005 return;
Fam Zhengbd8baec2014-12-18 18:37:06 +08002006 }
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00002007
2008 aio_context = bdrv_get_aio_context(state->bs);
2009 aio_context_acquire(aio_context);
2010
2011 bdrv_drained_end(state->bs);
2012
2013 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08002014}
2015
Fam Zhengdf9a6812015-11-09 18:16:54 +08002016typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05002017 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002018 BdrvDirtyBitmap *bitmap;
2019 BlockDriverState *bs;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002020 HBitmap *backup;
2021 bool prepared;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002022 bool was_enabled;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002023} BlockDirtyBitmapState;
2024
John Snow50f43f02015-11-05 18:13:09 -05002025static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002026 Error **errp)
2027{
2028 Error *local_err = NULL;
2029 BlockDirtyBitmapAdd *action;
2030 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2031 common, common);
2032
John Snow94d16a62015-11-05 18:13:18 -05002033 if (action_check_completion_mode(common, errp) < 0) {
2034 return;
2035 }
2036
Eric Blake32bafa82016-03-17 16:48:37 -06002037 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002038 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2039 qmp_block_dirty_bitmap_add(action->node, action->name,
2040 action->has_granularity, action->granularity,
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002041 action->has_persistent, action->persistent,
John Snow0e2b7f02018-12-21 04:35:22 -05002042 action->has_disabled, action->disabled,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002043 &local_err);
2044
2045 if (!local_err) {
2046 state->prepared = true;
2047 } else {
2048 error_propagate(errp, local_err);
2049 }
2050}
2051
John Snow50f43f02015-11-05 18:13:09 -05002052static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002053{
2054 BlockDirtyBitmapAdd *action;
2055 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2056 common, common);
2057
Eric Blake32bafa82016-03-17 16:48:37 -06002058 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002059 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2060 * then the node reference and bitmap name must have been valid.
2061 */
2062 if (state->prepared) {
2063 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2064 }
2065}
2066
John Snow50f43f02015-11-05 18:13:09 -05002067static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002068 Error **errp)
2069{
2070 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2071 common, common);
2072 BlockDirtyBitmap *action;
2073
John Snow94d16a62015-11-05 18:13:18 -05002074 if (action_check_completion_mode(common, errp) < 0) {
2075 return;
2076 }
2077
Eric Blake32bafa82016-03-17 16:48:37 -06002078 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002079 state->bitmap = block_dirty_bitmap_lookup(action->node,
2080 action->name,
2081 &state->bs,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002082 errp);
2083 if (!state->bitmap) {
2084 return;
2085 }
2086
John Snow3ae96d62019-03-12 12:05:49 -04002087 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) {
Vladimir Sementsov-Ogievskiyd6883bc2017-06-28 15:05:10 +03002088 return;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002089 }
2090
2091 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
Fam Zhengdf9a6812015-11-09 18:16:54 +08002092}
2093
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002094static void block_dirty_bitmap_restore(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002095{
2096 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2097 common, common);
2098
John Snow184dd9c2017-03-15 17:28:11 -04002099 if (state->backup) {
Vladimir Sementsov-Ogievskiy56bd6622018-10-29 16:23:14 -04002100 bdrv_restore_dirty_bitmap(state->bitmap, state->backup);
John Snow184dd9c2017-03-15 17:28:11 -04002101 }
Fam Zhengdf9a6812015-11-09 18:16:54 +08002102}
2103
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002104static void block_dirty_bitmap_free_backup(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002105{
2106 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2107 common, common);
2108
2109 hbitmap_free(state->backup);
2110}
2111
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002112static void block_dirty_bitmap_enable_prepare(BlkActionState *common,
2113 Error **errp)
2114{
2115 BlockDirtyBitmap *action;
2116 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2117 common, common);
2118
2119 if (action_check_completion_mode(common, errp) < 0) {
2120 return;
2121 }
2122
John Snow0e2b7f02018-12-21 04:35:22 -05002123 action = common->action->u.block_dirty_bitmap_enable.data;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002124 state->bitmap = block_dirty_bitmap_lookup(action->node,
2125 action->name,
2126 NULL,
2127 errp);
2128 if (!state->bitmap) {
2129 return;
2130 }
2131
John Snow3ae96d62019-03-12 12:05:49 -04002132 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snowb053bb52018-10-29 16:23:16 -04002133 return;
2134 }
2135
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002136 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2137 bdrv_enable_dirty_bitmap(state->bitmap);
2138}
2139
2140static void block_dirty_bitmap_enable_abort(BlkActionState *common)
2141{
2142 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2143 common, common);
2144
2145 if (!state->was_enabled) {
2146 bdrv_disable_dirty_bitmap(state->bitmap);
2147 }
2148}
2149
2150static void block_dirty_bitmap_disable_prepare(BlkActionState *common,
2151 Error **errp)
2152{
2153 BlockDirtyBitmap *action;
2154 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2155 common, common);
2156
2157 if (action_check_completion_mode(common, errp) < 0) {
2158 return;
2159 }
2160
John Snow0e2b7f02018-12-21 04:35:22 -05002161 action = common->action->u.block_dirty_bitmap_disable.data;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002162 state->bitmap = block_dirty_bitmap_lookup(action->node,
2163 action->name,
2164 NULL,
2165 errp);
2166 if (!state->bitmap) {
2167 return;
2168 }
2169
John Snow3ae96d62019-03-12 12:05:49 -04002170 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snowb053bb52018-10-29 16:23:16 -04002171 return;
2172 }
2173
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002174 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2175 bdrv_disable_dirty_bitmap(state->bitmap);
2176}
2177
2178static void block_dirty_bitmap_disable_abort(BlkActionState *common)
2179{
2180 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2181 common, common);
2182
2183 if (state->was_enabled) {
2184 bdrv_enable_dirty_bitmap(state->bitmap);
2185 }
2186}
2187
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04002188static BdrvDirtyBitmap *do_block_dirty_bitmap_merge(
2189 const char *node, const char *target,
2190 BlockDirtyBitmapMergeSourceList *bitmaps,
2191 HBitmap **backup, Error **errp);
John Snow360d4e42018-12-21 04:35:21 -05002192
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002193static void block_dirty_bitmap_merge_prepare(BlkActionState *common,
2194 Error **errp)
2195{
2196 BlockDirtyBitmapMerge *action;
2197 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2198 common, common);
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002199
2200 if (action_check_completion_mode(common, errp) < 0) {
2201 return;
2202 }
2203
John Snow0e2b7f02018-12-21 04:35:22 -05002204 action = common->action->u.block_dirty_bitmap_merge.data;
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002205
John Snow360d4e42018-12-21 04:35:21 -05002206 state->bitmap = do_block_dirty_bitmap_merge(action->node, action->target,
2207 action->bitmaps, &state->backup,
2208 errp);
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002209}
2210
John Snowc4e4b0f2019-07-29 16:35:54 -04002211static BdrvDirtyBitmap *do_block_dirty_bitmap_remove(
2212 const char *node, const char *name, bool release,
2213 BlockDriverState **bitmap_bs, Error **errp);
2214
2215static void block_dirty_bitmap_remove_prepare(BlkActionState *common,
2216 Error **errp)
2217{
2218 BlockDirtyBitmap *action;
2219 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2220 common, common);
2221
2222 if (action_check_completion_mode(common, errp) < 0) {
2223 return;
2224 }
2225
2226 action = common->action->u.block_dirty_bitmap_remove.data;
2227
2228 state->bitmap = do_block_dirty_bitmap_remove(action->node, action->name,
2229 false, &state->bs, errp);
2230 if (state->bitmap) {
2231 bdrv_dirty_bitmap_skip_store(state->bitmap, true);
2232 bdrv_dirty_bitmap_set_busy(state->bitmap, true);
2233 }
2234}
2235
2236static void block_dirty_bitmap_remove_abort(BlkActionState *common)
2237{
2238 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2239 common, common);
2240
2241 if (state->bitmap) {
2242 bdrv_dirty_bitmap_skip_store(state->bitmap, false);
2243 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
2244 }
2245}
2246
2247static void block_dirty_bitmap_remove_commit(BlkActionState *common)
2248{
2249 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2250 common, common);
2251
2252 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +03002253 bdrv_release_dirty_bitmap(state->bitmap);
John Snowc4e4b0f2019-07-29 16:35:54 -04002254}
2255
John Snow50f43f02015-11-05 18:13:09 -05002256static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002257{
2258 error_setg(errp, "Transaction aborted using Abort action");
2259}
2260
John Snow50f43f02015-11-05 18:13:09 -05002261static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002262{
Stefan Weildfc6f862013-07-25 18:21:28 +02002263 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002264}
2265
John Snow50f43f02015-11-05 18:13:09 -05002266static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002267 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2268 .instance_size = sizeof(ExternalSnapshotState),
2269 .prepare = external_snapshot_prepare,
2270 .commit = external_snapshot_commit,
2271 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002272 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002273 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002274 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002275 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002276 .prepare = external_snapshot_prepare,
2277 .commit = external_snapshot_commit,
2278 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002279 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002280 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002281 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2282 .instance_size = sizeof(DriveBackupState),
2283 .prepare = drive_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002284 .commit = drive_backup_commit,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002285 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002286 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002287 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002288 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2289 .instance_size = sizeof(BlockdevBackupState),
2290 .prepare = blockdev_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002291 .commit = blockdev_backup_commit,
Fam Zhengbd8baec2014-12-18 18:37:06 +08002292 .abort = blockdev_backup_abort,
2293 .clean = blockdev_backup_clean,
2294 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002295 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002296 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002297 .prepare = abort_prepare,
2298 .commit = abort_commit,
2299 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002300 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2301 .instance_size = sizeof(InternalSnapshotState),
2302 .prepare = internal_snapshot_prepare,
2303 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002304 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002305 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002306 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2307 .instance_size = sizeof(BlockDirtyBitmapState),
2308 .prepare = block_dirty_bitmap_add_prepare,
2309 .abort = block_dirty_bitmap_add_abort,
2310 },
2311 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2312 .instance_size = sizeof(BlockDirtyBitmapState),
2313 .prepare = block_dirty_bitmap_clear_prepare,
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002314 .commit = block_dirty_bitmap_free_backup,
2315 .abort = block_dirty_bitmap_restore,
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002316 },
John Snow0e2b7f02018-12-21 04:35:22 -05002317 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = {
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002318 .instance_size = sizeof(BlockDirtyBitmapState),
2319 .prepare = block_dirty_bitmap_enable_prepare,
2320 .abort = block_dirty_bitmap_enable_abort,
2321 },
John Snow0e2b7f02018-12-21 04:35:22 -05002322 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = {
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002323 .instance_size = sizeof(BlockDirtyBitmapState),
2324 .prepare = block_dirty_bitmap_disable_prepare,
2325 .abort = block_dirty_bitmap_disable_abort,
John Snow66da04d2018-09-06 09:02:25 -04002326 },
John Snow0e2b7f02018-12-21 04:35:22 -05002327 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = {
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002328 .instance_size = sizeof(BlockDirtyBitmapState),
2329 .prepare = block_dirty_bitmap_merge_prepare,
2330 .commit = block_dirty_bitmap_free_backup,
2331 .abort = block_dirty_bitmap_restore,
2332 },
John Snowc4e4b0f2019-07-29 16:35:54 -04002333 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = {
2334 .instance_size = sizeof(BlockDirtyBitmapState),
2335 .prepare = block_dirty_bitmap_remove_prepare,
2336 .commit = block_dirty_bitmap_remove_commit,
2337 .abort = block_dirty_bitmap_remove_abort,
2338 },
John Snow66da04d2018-09-06 09:02:25 -04002339 /* Where are transactions for MIRROR, COMMIT and STREAM?
2340 * Although these blockjobs use transaction callbacks like the backup job,
2341 * these jobs do not necessarily adhere to transaction semantics.
2342 * These jobs may not fully undo all of their actions on abort, nor do they
2343 * necessarily work in transactions with more than one job in them.
2344 */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002345};
2346
John Snow94d16a62015-11-05 18:13:18 -05002347/**
2348 * Allocate a TransactionProperties structure if necessary, and fill
2349 * that structure with desired defaults if they are unset.
2350 */
2351static TransactionProperties *get_transaction_properties(
2352 TransactionProperties *props)
2353{
2354 if (!props) {
2355 props = g_new0(TransactionProperties, 1);
2356 }
2357
2358 if (!props->has_completion_mode) {
2359 props->has_completion_mode = true;
2360 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2361 }
2362
2363 return props;
2364}
2365
Jeff Cody8802d1f2012-02-28 15:54:06 -05002366/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002367 * 'Atomic' group operations. The operations are performed as a set, and if
2368 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002369 */
John Snow94d16a62015-11-05 18:13:18 -05002370void qmp_transaction(TransactionActionList *dev_list,
2371 bool has_props,
2372 struct TransactionProperties *props,
2373 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002374{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002375 TransactionActionList *dev_entry = dev_list;
Kevin Wolf62c9e412018-04-19 16:09:52 +02002376 JobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002377 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002378 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002379
John Snowf4de0f82019-01-11 11:59:16 -06002380 QTAILQ_HEAD(, BlkActionState) snap_bdrv_states;
2381 QTAILQ_INIT(&snap_bdrv_states);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002382
John Snow94d16a62015-11-05 18:13:18 -05002383 /* Does this transaction get canceled as a group on failure?
Kevin Wolf62c9e412018-04-19 16:09:52 +02002384 * If not, we don't really need to make a JobTxn.
John Snow94d16a62015-11-05 18:13:18 -05002385 */
2386 props = get_transaction_properties(props);
2387 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02002388 block_job_txn = job_txn_new();
John Snow94d16a62015-11-05 18:13:18 -05002389 }
2390
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002391 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002392 bdrv_drain_all();
2393
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002394 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002395 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002396 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002397 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002398
Jeff Cody8802d1f2012-02-28 15:54:06 -05002399 dev_info = dev_entry->value;
2400 dev_entry = dev_entry->next;
2401
Eric Blake6a8f9662015-10-26 16:34:54 -06002402 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002403
Eric Blake6a8f9662015-10-26 16:34:54 -06002404 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002405 assert(ops->instance_size > 0);
2406
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002407 state = g_malloc0(ops->instance_size);
2408 state->ops = ops;
2409 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002410 state->block_job_txn = block_job_txn;
2411 state->txn_props = props;
John Snowf4de0f82019-01-11 11:59:16 -06002412 QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002413
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002414 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002415 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002416 error_propagate(errp, local_err);
2417 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002418 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002419 }
2420
John Snowf4de0f82019-01-11 11:59:16 -06002421 QTAILQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002422 if (state->ops->commit) {
2423 state->ops->commit(state);
2424 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002425 }
2426
2427 /* success */
2428 goto exit;
2429
2430delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002431 /* failure, and it is all-or-none; roll back all operations */
John Snowf4de0f82019-01-11 11:59:16 -06002432 QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002433 if (state->ops->abort) {
2434 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002435 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002436 }
2437exit:
John Snowf4de0f82019-01-11 11:59:16 -06002438 QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002439 if (state->ops->clean) {
2440 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002441 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002442 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002443 }
John Snow94d16a62015-11-05 18:13:18 -05002444 if (!has_props) {
2445 qapi_free_TransactionProperties(props);
2446 }
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02002447 job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002448}
2449
Benoît Canet12d3ba82014-01-23 21:31:35 +01002450void qmp_block_passwd(bool has_device, const char *device,
2451 bool has_node_name, const char *node_name,
2452 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002453{
Daniel P. Berrangec01c2142017-06-23 17:24:16 +01002454 error_setg(errp,
2455 "Setting block passwords directly is no longer supported");
Markus Armbruster666daa62010-06-02 18:48:27 +02002456}
2457
John Snow341ebc22015-04-17 19:49:52 -04002458void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2459 bool has_granularity, uint32_t granularity,
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002460 bool has_persistent, bool persistent,
Vladimir Sementsov-Ogievskiya6e2ca52018-06-11 14:53:32 -04002461 bool has_disabled, bool disabled,
John Snow341ebc22015-04-17 19:49:52 -04002462 Error **errp)
2463{
John Snow341ebc22015-04-17 19:49:52 -04002464 BlockDriverState *bs;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002465 BdrvDirtyBitmap *bitmap;
Sergio Lopez91005a42020-01-08 15:31:36 +01002466 AioContext *aio_context;
John Snow341ebc22015-04-17 19:49:52 -04002467
2468 if (!name || name[0] == '\0') {
2469 error_setg(errp, "Bitmap name cannot be empty");
2470 return;
2471 }
2472
2473 bs = bdrv_lookup_bs(node, node, errp);
2474 if (!bs) {
2475 return;
2476 }
2477
Sergio Lopez91005a42020-01-08 15:31:36 +01002478 aio_context = bdrv_get_aio_context(bs);
2479 aio_context_acquire(aio_context);
2480
John Snow341ebc22015-04-17 19:49:52 -04002481 if (has_granularity) {
2482 if (granularity < 512 || !is_power_of_2(granularity)) {
2483 error_setg(errp, "Granularity must be power of 2 "
2484 "and at least 512");
Sergio Lopez91005a42020-01-08 15:31:36 +01002485 goto out;
John Snow341ebc22015-04-17 19:49:52 -04002486 }
2487 } else {
2488 /* Default to cluster size, if available: */
2489 granularity = bdrv_get_default_bitmap_granularity(bs);
2490 }
2491
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002492 if (!has_persistent) {
2493 persistent = false;
2494 }
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03002495
Vladimir Sementsov-Ogievskiya6e2ca52018-06-11 14:53:32 -04002496 if (!has_disabled) {
2497 disabled = false;
2498 }
2499
Vladimir Sementsov-Ogievskiyd2c30802019-09-20 11:25:43 +03002500 if (persistent &&
2501 !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp))
2502 {
Sergio Lopez91005a42020-01-08 15:31:36 +01002503 goto out;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002504 }
2505
2506 bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp);
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03002507 if (bitmap == NULL) {
Sergio Lopez91005a42020-01-08 15:31:36 +01002508 goto out;
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002509 }
Vladimir Sementsov-Ogievskiyeb738bb2017-06-28 15:05:24 +03002510
Vladimir Sementsov-Ogievskiya6e2ca52018-06-11 14:53:32 -04002511 if (disabled) {
2512 bdrv_disable_dirty_bitmap(bitmap);
2513 }
2514
Eric Blake796a3792019-03-12 12:05:49 -04002515 bdrv_dirty_bitmap_set_persistence(bitmap, persistent);
Sergio Lopez91005a42020-01-08 15:31:36 +01002516
2517out:
2518 aio_context_release(aio_context);
John Snow341ebc22015-04-17 19:49:52 -04002519}
2520
John Snowc4e4b0f2019-07-29 16:35:54 -04002521static BdrvDirtyBitmap *do_block_dirty_bitmap_remove(
2522 const char *node, const char *name, bool release,
2523 BlockDriverState **bitmap_bs, Error **errp)
John Snow341ebc22015-04-17 19:49:52 -04002524{
John Snow341ebc22015-04-17 19:49:52 -04002525 BlockDriverState *bs;
2526 BdrvDirtyBitmap *bitmap;
Sergio Lopez91005a42020-01-08 15:31:36 +01002527 AioContext *aio_context;
John Snow341ebc22015-04-17 19:49:52 -04002528
Paolo Bonzini21198822017-06-05 14:39:03 +02002529 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
John Snow341ebc22015-04-17 19:49:52 -04002530 if (!bitmap || !bs) {
John Snowc4e4b0f2019-07-29 16:35:54 -04002531 return NULL;
John Snow341ebc22015-04-17 19:49:52 -04002532 }
2533
Sergio Lopez91005a42020-01-08 15:31:36 +01002534 aio_context = bdrv_get_aio_context(bs);
2535 aio_context_acquire(aio_context);
2536
John Snowc3edf132019-03-12 12:05:49 -04002537 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY | BDRV_BITMAP_RO,
2538 errp)) {
Sergio Lopez91005a42020-01-08 15:31:36 +01002539 aio_context_release(aio_context);
John Snowc4e4b0f2019-07-29 16:35:54 -04002540 return NULL;
John Snow9bd2b082015-04-17 19:49:57 -04002541 }
Vladimir Sementsov-Ogievskiy5c36c1a2017-06-28 15:05:29 +03002542
Vladimir Sementsov-Ogievskiyd2c30802019-09-20 11:25:43 +03002543 if (bdrv_dirty_bitmap_get_persistence(bitmap) &&
2544 bdrv_remove_persistent_dirty_bitmap(bs, name, errp) < 0)
2545 {
Sergio Lopez91005a42020-01-08 15:31:36 +01002546 aio_context_release(aio_context);
2547 return NULL;
Vladimir Sementsov-Ogievskiy5c36c1a2017-06-28 15:05:29 +03002548 }
2549
John Snowc4e4b0f2019-07-29 16:35:54 -04002550 if (release) {
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +03002551 bdrv_release_dirty_bitmap(bitmap);
John Snowc4e4b0f2019-07-29 16:35:54 -04002552 }
2553
2554 if (bitmap_bs) {
2555 *bitmap_bs = bs;
2556 }
2557
Sergio Lopez91005a42020-01-08 15:31:36 +01002558 aio_context_release(aio_context);
John Snowc4e4b0f2019-07-29 16:35:54 -04002559 return release ? NULL : bitmap;
2560}
2561
2562void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2563 Error **errp)
2564{
2565 do_block_dirty_bitmap_remove(node, name, true, NULL, errp);
John Snow341ebc22015-04-17 19:49:52 -04002566}
2567
John Snowe74e6b72015-04-17 19:49:59 -04002568/**
2569 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2570 * immediately after a full backup operation.
2571 */
2572void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2573 Error **errp)
2574{
John Snowe74e6b72015-04-17 19:49:59 -04002575 BdrvDirtyBitmap *bitmap;
2576 BlockDriverState *bs;
2577
Paolo Bonzini21198822017-06-05 14:39:03 +02002578 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
John Snowe74e6b72015-04-17 19:49:59 -04002579 if (!bitmap || !bs) {
2580 return;
2581 }
2582
John Snow3ae96d62019-03-12 12:05:49 -04002583 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) {
Vladimir Sementsov-Ogievskiyd6883bc2017-06-28 15:05:10 +03002584 return;
John Snowe74e6b72015-04-17 19:49:59 -04002585 }
2586
Fam Zhengdf9a6812015-11-09 18:16:54 +08002587 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04002588}
2589
John Snow0e2b7f02018-12-21 04:35:22 -05002590void qmp_block_dirty_bitmap_enable(const char *node, const char *name,
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04002591 Error **errp)
2592{
2593 BlockDriverState *bs;
2594 BdrvDirtyBitmap *bitmap;
2595
2596 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
2597 if (!bitmap) {
2598 return;
2599 }
2600
John Snow3ae96d62019-03-12 12:05:49 -04002601 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04002602 return;
2603 }
2604
2605 bdrv_enable_dirty_bitmap(bitmap);
2606}
2607
John Snow0e2b7f02018-12-21 04:35:22 -05002608void qmp_block_dirty_bitmap_disable(const char *node, const char *name,
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04002609 Error **errp)
2610{
2611 BlockDriverState *bs;
2612 BdrvDirtyBitmap *bitmap;
2613
2614 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
2615 if (!bitmap) {
2616 return;
2617 }
2618
John Snow3ae96d62019-03-12 12:05:49 -04002619 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
Vladimir Sementsov-Ogievskiy5c5d2e52018-06-11 14:53:32 -04002620 return;
2621 }
2622
2623 bdrv_disable_dirty_bitmap(bitmap);
2624}
2625
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04002626static BdrvDirtyBitmap *do_block_dirty_bitmap_merge(
2627 const char *node, const char *target,
2628 BlockDirtyBitmapMergeSourceList *bitmaps,
2629 HBitmap **backup, Error **errp)
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04002630{
2631 BlockDriverState *bs;
John Snow360d4e42018-12-21 04:35:21 -05002632 BdrvDirtyBitmap *dst, *src, *anon;
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04002633 BlockDirtyBitmapMergeSourceList *lst;
John Snow360d4e42018-12-21 04:35:21 -05002634 Error *local_err = NULL;
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04002635
John Snow360d4e42018-12-21 04:35:21 -05002636 dst = block_dirty_bitmap_lookup(node, target, &bs, errp);
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04002637 if (!dst) {
John Snow360d4e42018-12-21 04:35:21 -05002638 return NULL;
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04002639 }
2640
John Snow360d4e42018-12-21 04:35:21 -05002641 anon = bdrv_create_dirty_bitmap(bs, bdrv_dirty_bitmap_granularity(dst),
2642 NULL, errp);
2643 if (!anon) {
2644 return NULL;
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04002645 }
2646
John Snow360d4e42018-12-21 04:35:21 -05002647 for (lst = bitmaps; lst; lst = lst->next) {
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04002648 switch (lst->value->type) {
2649 const char *name, *node;
2650 case QTYPE_QSTRING:
2651 name = lst->value->u.local;
2652 src = bdrv_find_dirty_bitmap(bs, name);
2653 if (!src) {
2654 error_setg(errp, "Dirty bitmap '%s' not found", name);
2655 dst = NULL;
2656 goto out;
2657 }
2658 break;
2659 case QTYPE_QDICT:
2660 node = lst->value->u.external.node;
2661 name = lst->value->u.external.name;
2662 src = block_dirty_bitmap_lookup(node, name, NULL, errp);
2663 if (!src) {
2664 dst = NULL;
2665 goto out;
2666 }
2667 break;
2668 default:
2669 abort();
John Snow360d4e42018-12-21 04:35:21 -05002670 }
2671
2672 bdrv_merge_dirty_bitmap(anon, src, NULL, &local_err);
2673 if (local_err) {
2674 error_propagate(errp, local_err);
2675 dst = NULL;
2676 goto out;
2677 }
2678 }
2679
2680 /* Merge into dst; dst is unchanged on failure. */
2681 bdrv_merge_dirty_bitmap(dst, anon, backup, errp);
2682
2683 out:
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +03002684 bdrv_release_dirty_bitmap(anon);
John Snow360d4e42018-12-21 04:35:21 -05002685 return dst;
2686}
2687
John Snow0e2b7f02018-12-21 04:35:22 -05002688void qmp_block_dirty_bitmap_merge(const char *node, const char *target,
Vladimir Sementsov-Ogievskiyeff08292019-05-28 19:33:31 -04002689 BlockDirtyBitmapMergeSourceList *bitmaps,
2690 Error **errp)
John Snow360d4e42018-12-21 04:35:21 -05002691{
2692 do_block_dirty_bitmap_merge(node, target, bitmaps, NULL, errp);
Vladimir Sementsov-Ogievskiyb598e532018-06-11 14:53:32 -04002693}
2694
Vladimir Sementsov-Ogievskiya3b52532017-06-28 15:05:25 +03002695BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
2696 const char *name,
2697 Error **errp)
2698{
2699 BdrvDirtyBitmap *bitmap;
2700 BlockDriverState *bs;
2701 BlockDirtyBitmapSha256 *ret = NULL;
2702 char *sha256;
2703
2704 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
2705 if (!bitmap || !bs) {
2706 return NULL;
2707 }
2708
2709 sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp);
2710 if (sha256 == NULL) {
2711 return NULL;
2712 }
2713
2714 ret = g_new(BlockDirtyBitmapSha256, 1);
2715 ret->sha256 = sha256;
2716
2717 return ret;
2718}
2719
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002720void qmp_block_resize(bool has_device, const char *device,
2721 bool has_node_name, const char *node_name,
2722 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002723{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002724 Error *local_err = NULL;
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002725 BlockBackend *blk = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002726 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002727 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02002728 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002729
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002730 bs = bdrv_lookup_bs(has_device ? device : NULL,
2731 has_node_name ? node_name : NULL,
2732 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002733 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002734 error_propagate(errp, local_err);
2735 return;
2736 }
2737
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002738 aio_context = bdrv_get_aio_context(bs);
2739 aio_context_acquire(aio_context);
2740
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002741 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002742 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002743 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002744 }
2745
Jeff Cody9c75e162014-06-25 16:55:30 -04002746 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002747 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002748 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04002749 }
2750
Kevin Wolfd861ab32019-04-25 14:25:10 +02002751 blk = blk_new(bdrv_get_aio_context(bs), BLK_PERM_RESIZE, BLK_PERM_ALL);
Kevin Wolfd7086422017-01-13 19:02:32 +01002752 ret = blk_insert_bs(blk, bs, errp);
2753 if (ret < 0) {
2754 goto out;
2755 }
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002756
John Snow698bdfa2017-05-10 13:39:45 -04002757 bdrv_drained_begin(bs);
Max Reitzc80d8b02019-09-18 11:51:40 +02002758 ret = blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp);
John Snow698bdfa2017-05-10 13:39:45 -04002759 bdrv_drained_end(bs);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002760
2761out:
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002762 blk_unref(blk);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002763 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002764}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002765
Alberto Garcia23233222016-07-05 17:28:59 +03002766void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002767 bool has_base, const char *base,
Alberto Garcia312fe092016-10-28 10:08:19 +03002768 bool has_base_node, const char *base_node,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002769 bool has_backing_file, const char *backing_file,
2770 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002771 bool has_on_error, BlockdevOnError on_error,
John Snow241ca1a2018-09-06 09:02:23 -04002772 bool has_auto_finalize, bool auto_finalize,
2773 bool has_auto_dismiss, bool auto_dismiss,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002774 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002775{
Alberto Garcia554b6142016-10-28 10:08:11 +03002776 BlockDriverState *bs, *iter;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002777 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002778 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002779 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002780 const char *base_name = NULL;
John Snowcf6320d2018-09-06 09:02:12 -04002781 int job_flags = JOB_DEFAULT;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002782
Paolo Bonzini1d809092012-09-28 17:22:59 +02002783 if (!has_on_error) {
2784 on_error = BLOCKDEV_ON_ERROR_REPORT;
2785 }
2786
Alberto Garcia554b6142016-10-28 10:08:11 +03002787 bs = bdrv_lookup_bs(device, device, errp);
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002788 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002789 return;
2790 }
2791
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002792 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002793 aio_context_acquire(aio_context);
2794
Alberto Garcia312fe092016-10-28 10:08:19 +03002795 if (has_base && has_base_node) {
2796 error_setg(errp, "'base' and 'base-node' cannot be specified "
2797 "at the same time");
2798 goto out;
2799 }
2800
Jeff Cody13d8cc52014-06-25 15:40:11 -04002801 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002802 base_bs = bdrv_find_backing_image(bs, base);
2803 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002804 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002805 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002806 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002807 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04002808 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002809 }
2810
Alberto Garcia312fe092016-10-28 10:08:19 +03002811 if (has_base_node) {
2812 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2813 if (!base_bs) {
2814 goto out;
2815 }
2816 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
2817 error_setg(errp, "Node '%s' is not a backing image of '%s'",
2818 base_node, device);
2819 goto out;
2820 }
2821 assert(bdrv_get_aio_context(base_bs) == aio_context);
Max Reitzf30c66b2019-02-01 20:29:05 +01002822 bdrv_refresh_filename(base_bs);
Alberto Garcia312fe092016-10-28 10:08:19 +03002823 base_name = base_bs->filename;
2824 }
2825
Alberto Garcia554b6142016-10-28 10:08:11 +03002826 /* Check for op blockers in the whole chain between bs and base */
2827 for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) {
2828 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
2829 goto out;
2830 }
2831 }
2832
Jeff Cody13d8cc52014-06-25 15:40:11 -04002833 /* if we are streaming the entire chain, the result will have no backing
2834 * file, and specifying one is therefore an error */
2835 if (base_bs == NULL && has_backing_file) {
2836 error_setg(errp, "backing file specified, but streaming the "
2837 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002838 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002839 }
2840
2841 /* backing_file string overrides base bs filename */
2842 base_name = has_backing_file ? backing_file : base_name;
2843
John Snow241ca1a2018-09-06 09:02:23 -04002844 if (has_auto_finalize && !auto_finalize) {
2845 job_flags |= JOB_MANUAL_FINALIZE;
2846 }
2847 if (has_auto_dismiss && !auto_dismiss) {
2848 job_flags |= JOB_MANUAL_DISMISS;
2849 }
2850
Alberto Garcia23233222016-07-05 17:28:59 +03002851 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
John Snowcf6320d2018-09-06 09:02:12 -04002852 job_flags, has_speed ? speed : 0, on_error, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002853 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002854 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002855 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002856 }
2857
Vladimir Sementsov-Ogievskiyb23c5802019-06-06 18:41:32 +03002858 trace_qmp_block_stream(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002859
2860out:
2861 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002862}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00002863
Alberto Garciafd62c602016-07-05 17:29:00 +03002864void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Kevin Wolf3c605f42017-06-27 17:18:20 +02002865 bool has_base_node, const char *base_node,
Jeff Cody7676e2c2014-06-30 15:14:15 +02002866 bool has_base, const char *base,
Kevin Wolf3c605f42017-06-27 17:18:20 +02002867 bool has_top_node, const char *top_node,
Jeff Cody7676e2c2014-06-30 15:14:15 +02002868 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04002869 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04002870 bool has_speed, int64_t speed,
Kevin Wolf8faad1c2020-02-14 21:08:11 +01002871 bool has_on_error, BlockdevOnError on_error,
Kevin Wolf0db832f2017-02-20 18:10:05 +01002872 bool has_filter_node_name, const char *filter_node_name,
John Snow96fbf532018-09-06 09:02:21 -04002873 bool has_auto_finalize, bool auto_finalize,
2874 bool has_auto_dismiss, bool auto_dismiss,
Jeff Codyed61fc12012-09-27 13:29:16 -04002875 Error **errp)
2876{
2877 BlockDriverState *bs;
Alberto Garcia058223a2016-10-28 10:08:07 +03002878 BlockDriverState *iter;
Jeff Codyed61fc12012-09-27 13:29:16 -04002879 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002880 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04002881 Error *local_err = NULL;
John Snow53607822018-09-06 09:02:10 -04002882 int job_flags = JOB_DEFAULT;
Jeff Codyed61fc12012-09-27 13:29:16 -04002883
Max Reitz54504662014-04-10 19:36:25 +02002884 if (!has_speed) {
2885 speed = 0;
2886 }
Kevin Wolf8faad1c2020-02-14 21:08:11 +01002887 if (!has_on_error) {
2888 on_error = BLOCKDEV_ON_ERROR_REPORT;
2889 }
Kevin Wolf0db832f2017-02-20 18:10:05 +01002890 if (!has_filter_node_name) {
2891 filter_node_name = NULL;
2892 }
John Snow96fbf532018-09-06 09:02:21 -04002893 if (has_auto_finalize && !auto_finalize) {
2894 job_flags |= JOB_MANUAL_FINALIZE;
2895 }
2896 if (has_auto_dismiss && !auto_dismiss) {
2897 job_flags |= JOB_MANUAL_DISMISS;
2898 }
Max Reitz54504662014-04-10 19:36:25 +02002899
Jeff Cody7676e2c2014-06-30 15:14:15 +02002900 /* Important Note:
2901 * libvirt relies on the DeviceNotFound error class in order to probe for
2902 * live commit feature versions; for this to work, we must make sure to
2903 * perform the device lookup before any generic errors that may occur in a
2904 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02002905 bs = qmp_get_root_bs(device, &local_err);
2906 if (!bs) {
2907 bs = bdrv_lookup_bs(device, device, NULL);
2908 if (!bs) {
2909 error_free(local_err);
2910 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2911 "Device '%s' not found", device);
2912 } else {
2913 error_propagate(errp, local_err);
2914 }
Jeff Codyed61fc12012-09-27 13:29:16 -04002915 return;
2916 }
Jeff Codyed61fc12012-09-27 13:29:16 -04002917
Kevin Wolf1d13b162016-06-23 14:20:24 +02002918 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002919 aio_context_acquire(aio_context);
2920
Fam Zhengbb000212014-09-11 13:14:00 +08002921 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002922 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08002923 }
2924
Jeff Codyed61fc12012-09-27 13:29:16 -04002925 /* default top_bs is the active layer */
2926 top_bs = bs;
2927
Kevin Wolf3c605f42017-06-27 17:18:20 +02002928 if (has_top_node && has_top) {
2929 error_setg(errp, "'top-node' and 'top' are mutually exclusive");
2930 goto out;
2931 } else if (has_top_node) {
2932 top_bs = bdrv_lookup_bs(NULL, top_node, errp);
2933 if (top_bs == NULL) {
2934 goto out;
2935 }
2936 if (!bdrv_chain_contains(bs, top_bs)) {
2937 error_setg(errp, "'%s' is not in this backing file chain",
2938 top_node);
2939 goto out;
2940 }
2941 } else if (has_top && top) {
Max Reitzf30c66b2019-02-01 20:29:05 +01002942 /* This strcmp() is just a shortcut, there is no need to
2943 * refresh @bs's filename. If it mismatches,
2944 * bdrv_find_backing_image() will do the refresh and may still
2945 * return @bs. */
Jeff Codyed61fc12012-09-27 13:29:16 -04002946 if (strcmp(bs->filename, top) != 0) {
2947 top_bs = bdrv_find_backing_image(bs, top);
2948 }
2949 }
2950
2951 if (top_bs == NULL) {
2952 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002953 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04002954 }
2955
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002956 assert(bdrv_get_aio_context(top_bs) == aio_context);
2957
Kevin Wolf3c605f42017-06-27 17:18:20 +02002958 if (has_base_node && has_base) {
2959 error_setg(errp, "'base-node' and 'base' are mutually exclusive");
2960 goto out;
2961 } else if (has_base_node) {
2962 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2963 if (base_bs == NULL) {
2964 goto out;
2965 }
2966 if (!bdrv_chain_contains(top_bs, base_bs)) {
2967 error_setg(errp, "'%s' is not in this backing file chain",
2968 base_node);
2969 goto out;
2970 }
2971 } else if (has_base && base) {
Jeff Codyd5208c42012-10-16 15:49:10 -04002972 base_bs = bdrv_find_backing_image(top_bs, base);
2973 } else {
2974 base_bs = bdrv_find_base(top_bs);
2975 }
2976
2977 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002978 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002979 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04002980 }
2981
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002982 assert(bdrv_get_aio_context(base_bs) == aio_context);
2983
Alberto Garcia058223a2016-10-28 10:08:07 +03002984 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
2985 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2986 goto out;
2987 }
Fam Zhengbb000212014-09-11 13:14:00 +08002988 }
2989
Jeff Cody7676e2c2014-06-30 15:14:15 +02002990 /* Do not allow attempts to commit an image into itself */
2991 if (top_bs == base_bs) {
2992 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002993 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02002994 }
2995
Fam Zheng20a63d22013-12-16 14:45:31 +08002996 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04002997 if (has_backing_file) {
2998 error_setg(errp, "'backing-file' specified,"
2999 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003000 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003001 }
John Snow47970df2016-10-27 12:06:57 -04003002 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
John Snow53607822018-09-06 09:02:10 -04003003 job_flags, speed, on_error,
Fam Zheng78bbd912017-04-21 20:27:04 +08003004 filter_node_name, NULL, NULL, false, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003005 } else {
Alberto Garcia058223a2016-10-28 10:08:07 +03003006 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
3007 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3008 goto out;
3009 }
John Snow53607822018-09-06 09:02:10 -04003010 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags,
3011 speed, on_error, has_backing_file ? backing_file : NULL,
Kevin Wolf0db832f2017-02-20 18:10:05 +01003012 filter_node_name, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003013 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003014 if (local_err != NULL) {
3015 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003016 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003017 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003018
3019out:
3020 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003021}
3022
John Snow7b0b8702019-07-29 16:35:52 -04003023/* Common QMP interface for drive-backup and blockdev-backup */
3024static BlockJob *do_backup_common(BackupCommon *backup,
3025 BlockDriverState *bs,
3026 BlockDriverState *target_bs,
3027 AioContext *aio_context,
3028 JobTxn *txn, Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003029{
John Snow111049a2016-11-08 01:50:38 -05003030 BlockJob *job = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003031 BdrvDirtyBitmap *bmap = NULL;
John Snow7b0b8702019-07-29 16:35:52 -04003032 int job_flags = JOB_DEFAULT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003033
Pavel Butsykin81206a82016-07-22 11:17:50 +03003034 if (!backup->has_speed) {
3035 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003036 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003037 if (!backup->has_on_source_error) {
3038 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003039 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003040 if (!backup->has_on_target_error) {
3041 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003042 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003043 if (!backup->has_job_id) {
3044 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003045 }
John Snowb40dacd2018-03-10 03:27:44 -05003046 if (!backup->has_auto_finalize) {
3047 backup->auto_finalize = true;
3048 }
3049 if (!backup->has_auto_dismiss) {
3050 backup->auto_dismiss = true;
3051 }
Pavel Butsykin13b94142016-07-22 11:17:52 +03003052 if (!backup->has_compress) {
3053 backup->compress = false;
3054 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003055
John Snowa6c93652019-07-29 16:35:55 -04003056 if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) ||
3057 (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) {
3058 /* done before desugaring 'incremental' to print the right message */
3059 if (!backup->has_bitmap) {
3060 error_setg(errp, "must provide a valid bitmap name for "
3061 "'%s' sync mode", MirrorSyncMode_str(backup->sync));
3062 return NULL;
3063 }
3064 }
3065
John Snowc8b56502019-07-29 16:35:52 -04003066 if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) {
3067 if (backup->has_bitmap_mode &&
3068 backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) {
3069 error_setg(errp, "Bitmap sync mode must be '%s' "
3070 "when using sync mode '%s'",
3071 BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS),
3072 MirrorSyncMode_str(backup->sync));
3073 return NULL;
3074 }
3075 backup->has_bitmap_mode = true;
3076 backup->sync = MIRROR_SYNC_MODE_BITMAP;
3077 backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS;
3078 }
3079
John Snow7b0b8702019-07-29 16:35:52 -04003080 if (backup->has_bitmap) {
3081 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
3082 if (!bmap) {
3083 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
3084 return NULL;
3085 }
John Snowc8b56502019-07-29 16:35:52 -04003086 if (!backup->has_bitmap_mode) {
3087 error_setg(errp, "Bitmap sync mode must be given "
3088 "when providing a bitmap");
3089 return NULL;
3090 }
John Snowb30ffbe2019-07-29 16:35:54 -04003091 if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snow7b0b8702019-07-29 16:35:52 -04003092 return NULL;
3093 }
John Snow1a2b8b42019-07-29 16:35:55 -04003094
3095 /* This does not produce a useful bitmap artifact: */
3096 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
3097 error_setg(errp, "sync mode '%s' does not produce meaningful bitmap"
3098 " outputs", MirrorSyncMode_str(backup->sync));
3099 return NULL;
3100 }
3101
3102 /* If the bitmap isn't used for input or output, this is useless: */
3103 if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER &&
3104 backup->sync != MIRROR_SYNC_MODE_BITMAP) {
3105 error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect"
3106 " when combined with sync mode '%s'",
3107 BitmapSyncMode_str(backup->bitmap_mode),
3108 MirrorSyncMode_str(backup->sync));
3109 return NULL;
3110 }
3111 }
3112
3113 if (!backup->has_bitmap && backup->has_bitmap_mode) {
3114 error_setg(errp, "Cannot specify bitmap sync mode without a bitmap");
3115 return NULL;
John Snow7b0b8702019-07-29 16:35:52 -04003116 }
3117
3118 if (!backup->auto_finalize) {
3119 job_flags |= JOB_MANUAL_FINALIZE;
3120 }
3121 if (!backup->auto_dismiss) {
3122 job_flags |= JOB_MANUAL_DISMISS;
3123 }
3124
3125 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
John Snowc8b56502019-07-29 16:35:52 -04003126 backup->sync, bmap, backup->bitmap_mode,
3127 backup->compress,
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +03003128 backup->filter_node_name,
John Snowc8b56502019-07-29 16:35:52 -04003129 backup->on_source_error,
3130 backup->on_target_error,
John Snow7b0b8702019-07-29 16:35:52 -04003131 job_flags, NULL, NULL, txn, errp);
3132 return job;
3133}
3134
Sergio Lopez2288ccf2020-01-08 15:31:32 +01003135void qmp_drive_backup(DriveBackup *backup, Error **errp)
John Snow7b0b8702019-07-29 16:35:52 -04003136{
Sergio Lopez2288ccf2020-01-08 15:31:32 +01003137 TransactionAction action = {
3138 .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP,
3139 .u.drive_backup.data = backup,
3140 };
3141 blockdev_do_action(&action, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003142}
3143
Peter Krempafacda542020-01-20 09:50:49 +01003144BlockDeviceInfoList *qmp_query_named_block_nodes(bool has_flat,
3145 bool flat,
3146 Error **errp)
Benoît Canetc13163f2014-01-23 21:31:34 +01003147{
Peter Krempafacda542020-01-20 09:50:49 +01003148 bool return_flat = has_flat && flat;
3149
3150 return bdrv_named_nodes_list(return_flat, errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003151}
3152
Vladimir Sementsov-Ogievskiy5d3b4e92018-12-21 20:09:07 +03003153XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp)
3154{
3155 return bdrv_get_xdbg_block_graph(errp);
3156}
3157
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01003158void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003159{
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01003160 TransactionAction action = {
3161 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP,
3162 .u.blockdev_backup.data = backup,
3163 };
3164 blockdev_do_action(&action, errp);
John Snow78f51fd2015-11-05 18:13:17 -05003165}
3166
Fam Zheng4193cdd2015-12-24 12:45:03 +08003167/* Parameter check and block job starting for drive mirroring.
3168 * Caller should hold @device and @target's aio context (must be the same).
3169 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003170static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003171 BlockDriverState *target,
3172 bool has_replaces, const char *replaces,
3173 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003174 BlockMirrorBackingMode backing_mode,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003175 bool zero_target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003176 bool has_speed, int64_t speed,
3177 bool has_granularity, uint32_t granularity,
3178 bool has_buf_size, int64_t buf_size,
3179 bool has_on_source_error,
3180 BlockdevOnError on_source_error,
3181 bool has_on_target_error,
3182 BlockdevOnError on_target_error,
3183 bool has_unmap, bool unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003184 bool has_filter_node_name,
3185 const char *filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003186 bool has_copy_mode, MirrorCopyMode copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04003187 bool has_auto_finalize, bool auto_finalize,
3188 bool has_auto_dismiss, bool auto_dismiss,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003189 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003190{
John Snowa1999b32018-09-06 09:02:11 -04003191 int job_flags = JOB_DEFAULT;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003192
3193 if (!has_speed) {
3194 speed = 0;
3195 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003196 if (!has_on_source_error) {
3197 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3198 }
3199 if (!has_on_target_error) {
3200 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3201 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003202 if (!has_granularity) {
3203 granularity = 0;
3204 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003205 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003206 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003207 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003208 if (!has_unmap) {
3209 unmap = true;
3210 }
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003211 if (!has_filter_node_name) {
3212 filter_node_name = NULL;
3213 }
Max Reitz481deba2018-06-13 20:18:22 +02003214 if (!has_copy_mode) {
3215 copy_mode = MIRROR_COPY_MODE_BACKGROUND;
3216 }
John Snowa6b58ad2018-09-06 09:02:22 -04003217 if (has_auto_finalize && !auto_finalize) {
3218 job_flags |= JOB_MANUAL_FINALIZE;
3219 }
3220 if (has_auto_dismiss && !auto_dismiss) {
3221 job_flags |= JOB_MANUAL_DISMISS;
3222 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003223
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003224 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003225 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3226 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003227 return;
3228 }
3229 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003230 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3231 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003232 return;
3233 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003234
Fam Zheng4193cdd2015-12-24 12:45:03 +08003235 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3236 return;
3237 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003238 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3239 return;
3240 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003241
3242 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3243 sync = MIRROR_SYNC_MODE_FULL;
3244 }
3245
Max Reitz74ce9e42019-02-13 23:53:01 +01003246 if (has_replaces) {
3247 BlockDriverState *to_replace_bs;
3248 AioContext *replace_aio_context;
3249 int64_t bs_size, replace_size;
3250
3251 bs_size = bdrv_getlength(bs);
3252 if (bs_size < 0) {
3253 error_setg_errno(errp, -bs_size, "Failed to query device's size");
3254 return;
3255 }
3256
3257 to_replace_bs = check_to_replace_node(bs, replaces, errp);
3258 if (!to_replace_bs) {
3259 return;
3260 }
3261
3262 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3263 aio_context_acquire(replace_aio_context);
3264 replace_size = bdrv_getlength(to_replace_bs);
3265 aio_context_release(replace_aio_context);
3266
3267 if (replace_size < 0) {
3268 error_setg_errno(errp, -replace_size,
3269 "Failed to query the replacement node's size");
3270 return;
3271 }
3272 if (bs_size != replace_size) {
3273 error_setg(errp, "cannot replace image with a mirror image of "
3274 "different size");
3275 return;
3276 }
3277 }
3278
Fam Zheng4193cdd2015-12-24 12:45:03 +08003279 /* pass the node name to replace to mirror start since it's loose coupling
3280 * and will allow to check whether the node still exist at mirror completion
3281 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003282 mirror_start(job_id, bs, target,
John Snowa1999b32018-09-06 09:02:11 -04003283 has_replaces ? replaces : NULL, job_flags,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003284 speed, granularity, buf_size, sync, backing_mode, zero_target,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003285 on_source_error, on_target_error, unmap, filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003286 copy_mode, errp);
Fam Zheng4193cdd2015-12-24 12:45:03 +08003287}
3288
Eric Blakefaecd402016-07-14 16:37:58 -06003289void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003290{
3291 BlockDriverState *bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003292 BlockDriverState *source, *target_bs;
3293 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003294 AioContext *old_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003295 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003296 Error *local_err = NULL;
3297 QDict *options = NULL;
3298 int flags;
3299 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003300 const char *format = arg->format;
Max Reitzcdf3bc92019-07-24 19:12:30 +02003301 bool zero_target;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003302 int ret;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003303
Kevin Wolf0524e932016-06-23 14:20:24 +02003304 bs = qmp_get_root_bs(arg->device, errp);
3305 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003306 return;
3307 }
3308
Paolo Bonzinicb2af912018-02-07 17:29:20 +01003309 /* Early check to avoid creating target */
3310 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3311 return;
3312 }
3313
Kevin Wolf0524e932016-06-23 14:20:24 +02003314 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003315 aio_context_acquire(aio_context);
3316
Eric Blakefaecd402016-07-14 16:37:58 -06003317 if (!arg->has_mode) {
3318 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003319 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003320
Eric Blakefaecd402016-07-14 16:37:58 -06003321 if (!arg->has_format) {
3322 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3323 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003324 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003325
Kevin Wolf61de4c62016-03-18 17:46:45 +01003326 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003327 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003328 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3329 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003330 }
Eric Blakefaecd402016-07-14 16:37:58 -06003331 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003332 source = bs;
3333 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003334
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003335 size = bdrv_getlength(bs);
3336 if (size < 0) {
3337 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003338 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003339 }
3340
Eric Blakefaecd402016-07-14 16:37:58 -06003341 if (arg->has_replaces) {
Eric Blakefaecd402016-07-14 16:37:58 -06003342 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003343 error_setg(errp, "a node-name must be provided when replacing a"
3344 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003345 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003346 }
Benoît Canet09158f02014-06-27 18:25:25 +02003347 }
3348
Eric Blakefaecd402016-07-14 16:37:58 -06003349 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003350 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3351 } else {
3352 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3353 }
3354
John Snow2a32c6e2017-07-17 20:34:21 -04003355 /* Don't open backing image in create() */
3356 flags |= BDRV_O_NO_BACKING;
3357
Eric Blakefaecd402016-07-14 16:37:58 -06003358 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3359 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003360 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003361 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003362 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003363 bdrv_img_create(arg->target, format,
Fam Zheng92172832017-04-21 20:27:01 +08003364 NULL, NULL, NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003365 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003366 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003367 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003368 break;
3369 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3370 /* create new image with backing file */
Max Reitzf30c66b2019-02-01 20:29:05 +01003371 bdrv_refresh_filename(source);
Eric Blakefaecd402016-07-14 16:37:58 -06003372 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003373 source->filename,
3374 source->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08003375 NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003376 break;
3377 default:
3378 abort();
3379 }
3380 }
3381
Markus Armbruster84d18f02014-01-30 15:07:28 +01003382 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003383 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003384 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003385 }
3386
Max Reitze6641712015-08-26 19:47:48 +02003387 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003388 if (arg->has_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003389 qdict_put_str(options, "node-name", arg->node_name);
Benoît Canet4c828dc2014-06-16 12:00:55 +02003390 }
Max Reitze6641712015-08-26 19:47:48 +02003391 if (format) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003392 qdict_put_str(options, "driver", format);
Max Reitze6641712015-08-26 19:47:48 +02003393 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003394
Paolo Bonzinib812f672013-01-21 17:09:43 +01003395 /* Mirroring takes care of copy-on-write using the source's backing
3396 * file.
3397 */
John Snow2a32c6e2017-07-17 20:34:21 -04003398 target_bs = bdrv_open(arg->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003399 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003400 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003401 }
3402
Max Reitzcdf3bc92019-07-24 19:12:30 +02003403 zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL &&
3404 (arg->mode == NEW_IMAGE_MODE_EXISTING ||
3405 !bdrv_has_zero_init(target_bs)));
3406
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003407
3408 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3409 old_context = bdrv_get_aio_context(target_bs);
3410 aio_context_release(aio_context);
3411 aio_context_acquire(old_context);
3412
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003413 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
3414 if (ret < 0) {
3415 bdrv_unref(target_bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003416 aio_context_release(old_context);
3417 return;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003418 }
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003419
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003420 aio_context_release(old_context);
3421 aio_context_acquire(aio_context);
3422
Eric Blakefaecd402016-07-14 16:37:58 -06003423 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3424 arg->has_replaces, arg->replaces, arg->sync,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003425 backing_mode, zero_target,
3426 arg->has_speed, arg->speed,
Eric Blakefaecd402016-07-14 16:37:58 -06003427 arg->has_granularity, arg->granularity,
3428 arg->has_buf_size, arg->buf_size,
3429 arg->has_on_source_error, arg->on_source_error,
3430 arg->has_on_target_error, arg->on_target_error,
3431 arg->has_unmap, arg->unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003432 false, NULL,
Max Reitz481deba2018-06-13 20:18:22 +02003433 arg->has_copy_mode, arg->copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04003434 arg->has_auto_finalize, arg->auto_finalize,
3435 arg->has_auto_dismiss, arg->auto_dismiss,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003436 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02003437 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003438 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003439out:
3440 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003441}
3442
Alberto Garcia71aa9862016-07-05 17:28:57 +03003443void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3444 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08003445 bool has_replaces, const char *replaces,
3446 MirrorSyncMode sync,
3447 bool has_speed, int64_t speed,
3448 bool has_granularity, uint32_t granularity,
3449 bool has_buf_size, int64_t buf_size,
3450 bool has_on_source_error,
3451 BlockdevOnError on_source_error,
3452 bool has_on_target_error,
3453 BlockdevOnError on_target_error,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003454 bool has_filter_node_name,
3455 const char *filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003456 bool has_copy_mode, MirrorCopyMode copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04003457 bool has_auto_finalize, bool auto_finalize,
3458 bool has_auto_dismiss, bool auto_dismiss,
Fam Zhengdf925622015-12-24 12:45:05 +08003459 Error **errp)
3460{
3461 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08003462 BlockDriverState *target_bs;
3463 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003464 AioContext *old_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003465 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08003466 Error *local_err = NULL;
Max Reitzcdf3bc92019-07-24 19:12:30 +02003467 bool zero_target;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003468 int ret;
Fam Zhengdf925622015-12-24 12:45:05 +08003469
Kevin Wolf07eec652016-06-23 14:20:24 +02003470 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08003471 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08003472 return;
3473 }
3474
3475 target_bs = bdrv_lookup_bs(target, target, errp);
3476 if (!target_bs) {
3477 return;
3478 }
3479
Max Reitzcdf3bc92019-07-24 19:12:30 +02003480 zero_target = (sync == MIRROR_SYNC_MODE_FULL);
3481
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003482 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3483 old_context = bdrv_get_aio_context(target_bs);
Fam Zhengdf925622015-12-24 12:45:05 +08003484 aio_context = bdrv_get_aio_context(bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003485 aio_context_acquire(old_context);
Fam Zhengdf925622015-12-24 12:45:05 +08003486
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003487 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003488
3489 aio_context_release(old_context);
3490 aio_context_acquire(aio_context);
3491
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003492 if (ret < 0) {
3493 goto out;
3494 }
Fam Zhengdf925622015-12-24 12:45:05 +08003495
Alberto Garcia71aa9862016-07-05 17:28:57 +03003496 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02003497 has_replaces, replaces, sync, backing_mode,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003498 zero_target, has_speed, speed,
Fam Zhengdf925622015-12-24 12:45:05 +08003499 has_granularity, granularity,
3500 has_buf_size, buf_size,
3501 has_on_source_error, on_source_error,
3502 has_on_target_error, on_target_error,
3503 true, true,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003504 has_filter_node_name, filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003505 has_copy_mode, copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04003506 has_auto_finalize, auto_finalize,
3507 has_auto_dismiss, auto_dismiss,
Fam Zhengdf925622015-12-24 12:45:05 +08003508 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003509 error_propagate(errp, local_err);
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003510out:
Fam Zhengdf925622015-12-24 12:45:05 +08003511 aio_context_release(aio_context);
3512}
3513
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003514/* Get a block job using its ID and acquire its AioContext */
3515static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003516 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003517{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003518 BlockJob *job;
3519
3520 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003521
Max Reitz5433c242015-10-19 17:53:29 +02003522 *aio_context = NULL;
3523
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003524 job = block_job_get(id);
3525
3526 if (!job) {
3527 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3528 "Block job '%s' not found", id);
3529 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003530 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003531
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003532 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003533 aio_context_acquire(*aio_context);
3534
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003535 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003536}
3537
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003538void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003539{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003540 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003541 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003542
3543 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003544 return;
3545 }
3546
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003547 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003548 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003549}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003550
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003551void qmp_block_job_cancel(const char *device,
3552 bool has_force, bool force, Error **errp)
3553{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003554 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003555 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003556
3557 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003558 return;
3559 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003560
3561 if (!has_force) {
3562 force = false;
3563 }
3564
Kevin Wolfb15de822018-04-18 17:10:26 +02003565 if (job_user_paused(&job->job) && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003566 error_setg(errp, "The block job for device '%s' is currently paused",
3567 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003568 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003569 }
3570
3571 trace_qmp_block_job_cancel(job);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02003572 job_user_cancel(&job->job, force, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003573out:
3574 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003575}
3576
3577void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003578{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003579 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003580 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003581
John Snow0ec4dfb2018-03-10 03:27:32 -05003582 if (!job) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003583 return;
3584 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003585
3586 trace_qmp_block_job_pause(job);
Kevin Wolfb15de822018-04-18 17:10:26 +02003587 job_user_pause(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003588 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003589}
3590
3591void qmp_block_job_resume(const char *device, Error **errp)
3592{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003593 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003594 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003595
John Snow0ec4dfb2018-03-10 03:27:32 -05003596 if (!job) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02003597 return;
3598 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003599
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003600 trace_qmp_block_job_resume(job);
Kevin Wolfb15de822018-04-18 17:10:26 +02003601 job_user_resume(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003602 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003603}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003604
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003605void qmp_block_job_complete(const char *device, Error **errp)
3606{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003607 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003608 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003609
3610 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003611 return;
3612 }
3613
3614 trace_qmp_block_job_complete(job);
Kevin Wolf3453d972018-04-23 12:24:16 +02003615 job_complete(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003616 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003617}
3618
John Snow11b61fb2018-03-10 03:27:43 -05003619void qmp_block_job_finalize(const char *id, Error **errp)
3620{
3621 AioContext *aio_context;
3622 BlockJob *job = find_block_job(id, &aio_context, errp);
3623
3624 if (!job) {
3625 return;
3626 }
3627
3628 trace_qmp_block_job_finalize(job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02003629 job_finalize(&job->job, errp);
John Snow11b61fb2018-03-10 03:27:43 -05003630 aio_context_release(aio_context);
3631}
3632
John Snow75f71052018-03-10 03:27:36 -05003633void qmp_block_job_dismiss(const char *id, Error **errp)
3634{
3635 AioContext *aio_context;
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02003636 BlockJob *bjob = find_block_job(id, &aio_context, errp);
3637 Job *job;
John Snow75f71052018-03-10 03:27:36 -05003638
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02003639 if (!bjob) {
John Snow75f71052018-03-10 03:27:36 -05003640 return;
3641 }
3642
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02003643 trace_qmp_block_job_dismiss(bjob);
3644 job = &bjob->job;
3645 job_dismiss(&job, errp);
John Snow75f71052018-03-10 03:27:36 -05003646 aio_context_release(aio_context);
3647}
3648
Jeff Codyfa40e652014-07-01 09:52:16 +02003649void qmp_change_backing_file(const char *device,
3650 const char *image_node_name,
3651 const char *backing_file,
3652 Error **errp)
3653{
3654 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003655 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02003656 BlockDriverState *image_bs = NULL;
3657 Error *local_err = NULL;
3658 bool ro;
Jeff Codyfa40e652014-07-01 09:52:16 +02003659 int ret;
3660
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003661 bs = qmp_get_root_bs(device, errp);
3662 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02003663 return;
3664 }
3665
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003666 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003667 aio_context_acquire(aio_context);
3668
Jeff Codyfa40e652014-07-01 09:52:16 +02003669 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3670 if (local_err) {
3671 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003672 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003673 }
3674
3675 if (!image_bs) {
3676 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003677 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003678 }
3679
3680 if (bdrv_find_base(image_bs) == image_bs) {
3681 error_setg(errp, "not allowing backing file change on an image "
3682 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003683 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003684 }
3685
3686 /* even though we are not necessarily operating on bs, we need it to
3687 * determine if block ops are currently prohibited on the chain */
3688 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003689 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003690 }
3691
3692 /* final sanity check */
3693 if (!bdrv_chain_contains(bs, image_bs)) {
3694 error_setg(errp, "'%s' and image file are not in the same chain",
3695 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003696 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003697 }
3698
3699 /* if not r/w, reopen to make r/w */
Jeff Codyfa40e652014-07-01 09:52:16 +02003700 ro = bdrv_is_read_only(image_bs);
3701
3702 if (ro) {
Alberto Garcia051a60f2018-11-12 16:00:38 +02003703 if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003704 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003705 }
3706 }
3707
3708 ret = bdrv_change_backing_file(image_bs, backing_file,
3709 image_bs->drv ? image_bs->drv->format_name : "");
3710
3711 if (ret < 0) {
3712 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3713 backing_file);
3714 /* don't exit here, so we can try to restore open flags if
3715 * appropriate */
3716 }
3717
3718 if (ro) {
Alberto Garcia051a60f2018-11-12 16:00:38 +02003719 bdrv_reopen_set_read_only(image_bs, true, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003720 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02003721 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003722
3723out:
3724 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02003725}
3726
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003727void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3728{
Max Reitzbe4b67b2015-10-19 17:53:09 +02003729 BlockDriverState *bs;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003730 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01003731 Visitor *v = qobject_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003732 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003733 Error *local_err = NULL;
3734
Eric Blake3b098d52016-06-09 10:48:43 -06003735 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003736 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003737 error_propagate(errp, local_err);
3738 goto fail;
3739 }
3740
Eric Blake3b098d52016-06-09 10:48:43 -06003741 visit_complete(v, &obj);
Max Reitz7dc847e2018-02-24 16:40:29 +01003742 qdict = qobject_to(QDict, obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003743
3744 qdict_flatten(qdict);
3745
Kevin Wolf9ec88732016-09-21 14:56:11 +02003746 if (!qdict_get_try_str(qdict, "node-name")) {
3747 error_setg(errp, "'node-name' must be specified for the root node");
3748 goto fail;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003749 }
3750
Kevin Wolf9ec88732016-09-21 14:56:11 +02003751 bs = bds_tree_init(qdict, errp);
3752 if (!bs) {
3753 goto fail;
3754 }
3755
Maxim Levitsky89802d52020-03-08 11:24:40 +02003756 bdrv_set_monitor_owned(bs);
Kevin Wolf9ec88732016-09-21 14:56:11 +02003757
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003758fail:
Eric Blake3b098d52016-06-09 10:48:43 -06003759 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003760}
3761
Alberto Garcia1479c732019-03-12 18:48:51 +02003762void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp)
3763{
3764 BlockDriverState *bs;
3765 AioContext *ctx;
3766 QObject *obj;
3767 Visitor *v = qobject_output_visitor_new(&obj);
3768 Error *local_err = NULL;
3769 BlockReopenQueue *queue;
3770 QDict *qdict;
3771
3772 /* Check for the selected node name */
3773 if (!options->has_node_name) {
3774 error_setg(errp, "Node name not specified");
3775 goto fail;
3776 }
3777
3778 bs = bdrv_find_node(options->node_name);
3779 if (!bs) {
3780 error_setg(errp, "Cannot find node named '%s'", options->node_name);
3781 goto fail;
3782 }
3783
3784 /* Put all options in a QDict and flatten it */
3785 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
3786 if (local_err) {
3787 error_propagate(errp, local_err);
3788 goto fail;
3789 }
3790
3791 visit_complete(v, &obj);
3792 qdict = qobject_to(QDict, obj);
3793
3794 qdict_flatten(qdict);
3795
3796 /* Perform the reopen operation */
3797 ctx = bdrv_get_aio_context(bs);
3798 aio_context_acquire(ctx);
3799 bdrv_subtree_drained_begin(bs);
3800 queue = bdrv_reopen_queue(NULL, bs, qdict, false);
3801 bdrv_reopen_multiple(queue, errp);
3802 bdrv_subtree_drained_end(bs);
3803 aio_context_release(ctx);
3804
3805fail:
3806 visit_free(v);
3807}
3808
Markus Armbruster79b7a772017-03-21 17:53:28 +01003809void qmp_blockdev_del(const char *node_name, Error **errp)
Alberto Garcia81b936a2015-11-02 16:51:55 +02003810{
3811 AioContext *aio_context;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003812 BlockDriverState *bs;
3813
Kevin Wolf9ec88732016-09-21 14:56:11 +02003814 bs = bdrv_find_node(node_name);
3815 if (!bs) {
3816 error_setg(errp, "Cannot find node %s", node_name);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003817 return;
3818 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003819 if (bdrv_has_blk(bs)) {
3820 error_setg(errp, "Node %s is in use", node_name);
3821 return;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003822 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003823 aio_context = bdrv_get_aio_context(bs);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003824 aio_context_acquire(aio_context);
3825
Kevin Wolf9ec88732016-09-21 14:56:11 +02003826 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3827 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003828 }
3829
Paolo Bonzini70537ed2018-12-06 23:00:09 +01003830 if (!QTAILQ_IN_USE(bs, monitor_list)) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02003831 error_setg(errp, "Node %s is not owned by the monitor",
3832 bs->node_name);
3833 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003834 }
3835
Kevin Wolf9ec88732016-09-21 14:56:11 +02003836 if (bs->refcnt > 1) {
3837 error_setg(errp, "Block device %s is in use",
3838 bdrv_get_device_or_node_name(bs));
3839 goto out;
3840 }
3841
3842 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3843 bdrv_unref(bs);
3844
Alberto Garcia81b936a2015-11-02 16:51:55 +02003845out:
3846 aio_context_release(aio_context);
3847}
3848
Wen Congyang7f821592016-05-10 15:36:39 +08003849static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
3850 const char *child_name)
3851{
3852 BdrvChild *child;
3853
3854 QLIST_FOREACH(child, &parent_bs->children, next) {
3855 if (strcmp(child->name, child_name) == 0) {
3856 return child;
3857 }
3858 }
3859
3860 return NULL;
3861}
3862
3863void qmp_x_blockdev_change(const char *parent, bool has_child,
3864 const char *child, bool has_node,
3865 const char *node, Error **errp)
3866{
3867 BlockDriverState *parent_bs, *new_bs = NULL;
3868 BdrvChild *p_child;
3869
3870 parent_bs = bdrv_lookup_bs(parent, parent, errp);
3871 if (!parent_bs) {
3872 return;
3873 }
3874
3875 if (has_child == has_node) {
3876 if (has_child) {
3877 error_setg(errp, "The parameters child and node are in conflict");
3878 } else {
3879 error_setg(errp, "Either child or node must be specified");
3880 }
3881 return;
3882 }
3883
3884 if (has_child) {
3885 p_child = bdrv_find_child(parent_bs, child);
3886 if (!p_child) {
3887 error_setg(errp, "Node '%s' does not have child '%s'",
3888 parent, child);
3889 return;
3890 }
3891 bdrv_del_child(parent_bs, p_child, errp);
3892 }
3893
3894 if (has_node) {
3895 new_bs = bdrv_find_node(node);
3896 if (!new_bs) {
3897 error_setg(errp, "Node '%s' not found", node);
3898 return;
3899 }
3900 bdrv_add_child(parent_bs, new_bs, errp);
3901 }
3902}
3903
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003904BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3905{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003906 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003907 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003908
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003909 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
John Snow559b9352016-10-27 12:06:55 -04003910 BlockJobInfoList *elem;
3911 AioContext *aio_context;
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003912
John Snow559b9352016-10-27 12:06:55 -04003913 if (block_job_is_internal(job)) {
3914 continue;
3915 }
3916 elem = g_new0(BlockJobInfoList, 1);
3917 aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003918 aio_context_acquire(aio_context);
John Snow559b9352016-10-27 12:06:55 -04003919 elem->value = block_job_query(job, errp);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003920 aio_context_release(aio_context);
John Snow559b9352016-10-27 12:06:55 -04003921 if (!elem->value) {
3922 g_free(elem);
3923 qapi_free_BlockJobInfoList(head);
3924 return NULL;
3925 }
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003926 *p_next = elem;
3927 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003928 }
3929
3930 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003931}
Paolo Bonzini4d454572012-11-26 16:03:42 +01003932
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00003933void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
Stefan Hajnoczi882e9b82017-12-07 20:13:17 +00003934 bool has_force, bool force, Error **errp)
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00003935{
3936 AioContext *old_context;
3937 AioContext *new_context;
3938 BlockDriverState *bs;
3939
3940 bs = bdrv_find_node(node_name);
3941 if (!bs) {
3942 error_setg(errp, "Cannot find node %s", node_name);
3943 return;
3944 }
3945
Stefan Hajnoczi882e9b82017-12-07 20:13:17 +00003946 /* Protects against accidents. */
3947 if (!(has_force && force) && bdrv_has_blk(bs)) {
3948 error_setg(errp, "Node %s is associated with a BlockBackend and could "
3949 "be in use (use force=true to override this check)",
3950 node_name);
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00003951 return;
3952 }
3953
3954 if (iothread->type == QTYPE_QSTRING) {
3955 IOThread *obj = iothread_by_id(iothread->u.s);
3956 if (!obj) {
3957 error_setg(errp, "Cannot find iothread %s", iothread->u.s);
3958 return;
3959 }
3960
3961 new_context = iothread_get_aio_context(obj);
3962 } else {
3963 new_context = qemu_get_aio_context();
3964 }
3965
3966 old_context = bdrv_get_aio_context(bs);
3967 aio_context_acquire(old_context);
3968
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003969 bdrv_try_set_aio_context(bs, new_context, errp);
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00003970
3971 aio_context_release(old_context);
3972}
3973
Kevin Wolf00063832013-03-15 10:35:07 +01003974QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01003975 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01003976 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01003977 .desc = {
3978 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01003979 .name = "snapshot",
3980 .type = QEMU_OPT_BOOL,
3981 .help = "enable/disable snapshot mode",
3982 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003983 .name = "aio",
3984 .type = QEMU_OPT_STRING,
Aarushi Mehtaf80f2672020-01-20 14:18:50 +00003985 .help = "host AIO implementation (threads, native, io_uring)",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003986 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01003987 .name = BDRV_OPT_CACHE_WB,
3988 .type = QEMU_OPT_BOOL,
3989 .help = "Enable writeback mode",
3990 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003991 .name = "format",
3992 .type = QEMU_OPT_STRING,
3993 .help = "disk format (raw, qcow2, ...)",
3994 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003995 .name = "rerror",
3996 .type = QEMU_OPT_STRING,
3997 .help = "read error action",
3998 },{
3999 .name = "werror",
4000 .type = QEMU_OPT_STRING,
4001 .help = "write error action",
4002 },{
Alberto Garcia4e200cf2016-09-15 17:53:05 +03004003 .name = BDRV_OPT_READ_ONLY,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004004 .type = QEMU_OPT_BOOL,
4005 .help = "open drive file as read-only",
Pradeep Jagadeesha2a78622017-02-28 10:31:46 +01004006 },
4007
4008 THROTTLE_OPTS,
4009
4010 {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004011 .name = "throttling.group",
4012 .type = QEMU_OPT_STRING,
4013 .help = "name of the block throttling group",
4014 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004015 .name = "copy-on-read",
4016 .type = QEMU_OPT_BOOL,
4017 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004018 },{
4019 .name = "detect-zeroes",
4020 .type = QEMU_OPT_STRING,
4021 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004022 },{
4023 .name = "stats-account-invalid",
4024 .type = QEMU_OPT_BOOL,
4025 .help = "whether to account for invalid I/O operations "
4026 "in the statistics",
4027 },{
4028 .name = "stats-account-failed",
4029 .type = QEMU_OPT_BOOL,
4030 .help = "whether to account for failed I/O operations "
4031 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004032 },
4033 { /* end of list */ }
4034 },
4035};
Kevin Wolf00063832013-03-15 10:35:07 +01004036
4037QemuOptsList qemu_drive_opts = {
4038 .name = "drive",
4039 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4040 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004041 /*
4042 * no elements => accept any params
4043 * validation will happen later
4044 */
Kevin Wolf00063832013-03-15 10:35:07 +01004045 { /* end of list */ }
4046 },
4047};