blob: d53e39c303041614ac0aa9fabd10c6667d2a1081 [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 Armbrustera1b40bd2020-06-22 11:42:19 +0200242void drive_mark_claimed_by_board(void)
243{
244 BlockBackend *blk;
245 DriveInfo *dinfo;
246
247 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
248 dinfo = blk_legacy_dinfo(blk);
249 if (dinfo && blk_get_attached_dev(blk)) {
250 dinfo->claimed_by_board = true;
251 }
252 }
253}
254
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100255void drive_check_orphaned(void)
John Snowa66c9dc2014-10-01 14:19:24 -0400256{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200257 BlockBackend *blk;
John Snowa66c9dc2014-10-01 14:19:24 -0400258 DriveInfo *dinfo;
Markus Armbruster664cc622017-02-15 11:05:45 +0100259 Location loc;
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100260 bool orphans = false;
John Snowa66c9dc2014-10-01 14:19:24 -0400261
Markus Armbruster18e46a02014-10-07 13:59:06 +0200262 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
263 dinfo = blk_legacy_dinfo(blk);
Markus Armbrustera1b40bd2020-06-22 11:42:19 +0200264 if (dinfo->is_default || dinfo->type == IF_NONE) {
265 continue;
266 }
267 if (!blk_get_attached_dev(blk)) {
Markus Armbruster664cc622017-02-15 11:05:45 +0100268 loc_push_none(&loc);
269 qemu_opts_loc_restore(dinfo->opts);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100270 error_report("machine type does not support"
Markus Armbruster664cc622017-02-15 11:05:45 +0100271 " if=%s,bus=%d,unit=%d",
272 if_name[dinfo->type], dinfo->bus, dinfo->unit);
273 loc_pop(&loc);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100274 orphans = true;
Markus Armbrustera1b40bd2020-06-22 11:42:19 +0200275 continue;
276 }
277 if (!dinfo->claimed_by_board && dinfo->type != IF_VIRTIO) {
278 loc_push_none(&loc);
279 qemu_opts_loc_restore(dinfo->opts);
280 warn_report("bogus if=%s is deprecated, use if=none",
281 if_name[dinfo->type]);
282 loc_pop(&loc);
John Snowa66c9dc2014-10-01 14:19:24 -0400283 }
284 }
285
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100286 if (orphans) {
287 exit(1);
288 }
John Snowa66c9dc2014-10-01 14:19:24 -0400289}
290
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100291DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
292{
293 return drive_get(type,
294 drive_index_to_bus_id(type, index),
295 drive_index_to_unit_id(type, index));
296}
297
Markus Armbruster666daa62010-06-02 18:48:27 +0200298int drive_get_max_bus(BlockInterfaceType type)
299{
300 int max_bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200301 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200302 DriveInfo *dinfo;
303
304 max_bus = -1;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200305 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
306 dinfo = blk_legacy_dinfo(blk);
307 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200308 max_bus = dinfo->bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200309 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200310 }
311 return max_bus;
312}
313
Markus Armbruster13839972011-01-28 11:21:37 +0100314/* Get a block device. This should only be used for single-drive devices
315 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
316 appropriate bus. */
317DriveInfo *drive_get_next(BlockInterfaceType type)
318{
319 static int next_block_unit[IF_COUNT];
320
321 return drive_get(type, 0, next_block_unit[type]++);
322}
323
Markus Armbruster666daa62010-06-02 18:48:27 +0200324static void bdrv_format_print(void *opaque, const char *name)
325{
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200326 qemu_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200327}
328
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000329typedef struct {
330 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800331 BlockDriverState *bs;
332} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000333
Kevin Wolfb6810722013-09-20 11:33:11 +0200334static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200335{
336 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200337 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200338 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200339 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200340 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200341 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200342 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200343 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200344 } else {
Kevin Wolfb6810722013-09-20 11:33:11 +0200345 error_setg(errp, "'%s' invalid %s error action",
346 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200347 return -1;
348 }
349}
350
Alberto Garcia40119ef2015-11-16 11:28:38 +0200351static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
352 Error **errp)
353{
354 const QListEntry *entry;
355 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
356 switch (qobject_type(entry->value)) {
357
358 case QTYPE_QSTRING: {
359 unsigned long long length;
Max Reitz7dc847e2018-02-24 16:40:29 +0100360 const char *str = qstring_get_str(qobject_to(QString,
361 entry->value));
Alberto Garcia40119ef2015-11-16 11:28:38 +0200362 if (parse_uint_full(str, &length, 10) == 0 &&
363 length > 0 && length <= UINT_MAX) {
364 block_acct_add_interval(stats, (unsigned) length);
365 } else {
366 error_setg(errp, "Invalid interval length: %s", str);
367 return false;
368 }
369 break;
370 }
371
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400372 case QTYPE_QNUM: {
Max Reitz7dc847e2018-02-24 16:40:29 +0100373 int64_t length = qnum_get_int(qobject_to(QNum, entry->value));
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400374
Alberto Garcia40119ef2015-11-16 11:28:38 +0200375 if (length > 0 && length <= UINT_MAX) {
376 block_acct_add_interval(stats, (unsigned) length);
377 } else {
378 error_setg(errp, "Invalid interval length: %" PRId64, length);
379 return false;
380 }
381 break;
382 }
383
384 default:
385 error_setg(errp, "The specification of stats-intervals is invalid");
386 return false;
387 }
388 }
389 return true;
390}
391
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200392typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
393
Max Reitzfbf81752015-10-19 17:53:31 +0200394/* All parameters but @opts are optional and may be set to NULL. */
395static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
396 const char **throttling_group, ThrottleConfig *throttle_cfg,
397 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
398{
Max Reitzfbf81752015-10-19 17:53:31 +0200399 Error *local_error = NULL;
400 const char *aio;
401
402 if (bdrv_flags) {
Max Reitzfbf81752015-10-19 17:53:31 +0200403 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
404 *bdrv_flags |= BDRV_O_COPY_ON_READ;
405 }
406
Max Reitzfbf81752015-10-19 17:53:31 +0200407 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
Aarushi Mehtaf80f2672020-01-20 14:18:50 +0000408 if (bdrv_parse_aio(aio, bdrv_flags) < 0) {
409 error_setg(errp, "invalid aio option");
410 return;
Max Reitzfbf81752015-10-19 17:53:31 +0200411 }
412 }
413 }
414
415 /* disk I/O throttling */
416 if (throttling_group) {
417 *throttling_group = qemu_opt_get(opts, "throttling.group");
418 }
419
420 if (throttle_cfg) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200421 throttle_config_init(throttle_cfg);
Max Reitzfbf81752015-10-19 17:53:31 +0200422 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
423 qemu_opt_get_number(opts, "throttling.bps-total", 0);
424 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
425 qemu_opt_get_number(opts, "throttling.bps-read", 0);
426 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
427 qemu_opt_get_number(opts, "throttling.bps-write", 0);
428 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
429 qemu_opt_get_number(opts, "throttling.iops-total", 0);
430 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
431 qemu_opt_get_number(opts, "throttling.iops-read", 0);
432 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
433 qemu_opt_get_number(opts, "throttling.iops-write", 0);
434
435 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
436 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
437 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
438 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
439 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
440 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
441 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
442 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
443 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
444 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
445 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
446 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
447
Alberto Garcia8a0fc182016-02-18 12:27:02 +0200448 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
449 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
450 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
451 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
452 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
453 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
454 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
455 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
456 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
457 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
458 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
459 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
460
Max Reitzfbf81752015-10-19 17:53:31 +0200461 throttle_cfg->op_size =
462 qemu_opt_get_number(opts, "throttling.iops-size", 0);
463
Alberto Garciad5851082016-02-18 12:26:59 +0200464 if (!throttle_is_valid(throttle_cfg, errp)) {
Max Reitzfbf81752015-10-19 17:53:31 +0200465 return;
466 }
467 }
468
469 if (detect_zeroes) {
470 *detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200471 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Max Reitzfbf81752015-10-19 17:53:31 +0200472 qemu_opt_get(opts, "detect-zeroes"),
Max Reitzfbf81752015-10-19 17:53:31 +0200473 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
474 &local_error);
475 if (local_error) {
476 error_propagate(errp, local_error);
477 return;
478 }
Max Reitzfbf81752015-10-19 17:53:31 +0200479 }
480}
481
Kevin Wolff298d072013-09-10 12:01:20 +0200482/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200483static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
484 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200485{
486 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200487 int bdrv_flags = 0;
488 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200489 bool account_invalid, account_failed;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300490 bool writethrough, read_only;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200491 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200492 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200493 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200494 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100495 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100496 QemuOpts *opts;
Alberto Garcia40119ef2015-11-16 11:28:38 +0200497 QDict *interval_dict = NULL;
498 QList *interval_list = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100499 const char *id;
Max Reitzfbf81752015-10-19 17:53:31 +0200500 BlockdevDetectZeroesOptions detect_zeroes =
501 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
502 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200503
Kevin Wolff298d072013-09-10 12:01:20 +0200504 /* Check common options by copying from bs_opts to opts, all other options
505 * stay in bs_opts for processing by bdrv_open(). */
506 id = qdict_get_try_str(bs_opts, "id");
Markus Armbrusterc6ecec42020-07-07 18:05:35 +0200507 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, errp);
508 if (!opts) {
Markus Armbruster6376f952014-05-28 11:17:01 +0200509 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100510 }
511
Markus Armbruster668f62e2020-07-07 18:06:02 +0200512 if (!qemu_opts_absorb_qdict(opts, bs_opts, errp)) {
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100513 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100514 }
515
516 if (id) {
517 qdict_del(bs_opts, "id");
518 }
519
Markus Armbruster666daa62010-06-02 18:48:27 +0200520 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200521 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200522
Alberto Garcia362e9292015-10-28 17:33:04 +0200523 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
524 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
525
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100526 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
527
Alberto Garciaff356ee2016-07-08 17:03:00 +0300528 id = qemu_opts_id(opts);
529
Alberto Garcia40119ef2015-11-16 11:28:38 +0200530 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
531 qdict_array_split(interval_dict, &interval_list);
532
533 if (qdict_size(interval_dict) != 0) {
534 error_setg(errp, "Invalid option stats-intervals.%s",
535 qdict_first(interval_dict)->key);
536 goto early_err;
537 }
Alberto Garcia2be55062015-10-28 17:33:07 +0200538
Max Reitzfbf81752015-10-19 17:53:31 +0200539 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
540 &detect_zeroes, &error);
541 if (error) {
542 error_propagate(errp, error);
543 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200544 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200545
546 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100547 if (is_help_option(buf)) {
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200548 qemu_printf("Supported formats:");
Andrey Shinkevich9ac404c2019-03-07 16:33:58 +0300549 bdrv_iterate_format(bdrv_format_print, NULL, false);
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200550 qemu_printf("\nSupported formats (read-only):");
Andrey Shinkevich9ac404c2019-03-07 16:33:58 +0300551 bdrv_iterate_format(bdrv_format_print, NULL, true);
Markus Armbrustercdcd4362019-04-17 21:06:38 +0200552 qemu_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100553 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200554 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200555
Max Reitze4342ce2015-02-05 13:58:14 -0500556 if (qdict_haskey(bs_opts, "driver")) {
557 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100558 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400559 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500560 qdict_put_str(bs_opts, "driver", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200561 }
562
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200563 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200564 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200565 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100566 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200567 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100568 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200569 }
570 }
571
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200572 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200573 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200574 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100575 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200576 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100577 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200578 }
579 }
580
Max Reitz5ec18f82015-10-19 17:53:30 +0200581 if (snapshot) {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200582 bdrv_flags |= BDRV_O_SNAPSHOT;
Max Reitz5ec18f82015-10-19 17:53:30 +0200583 }
584
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300585 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
586
Kevin Wolf326642b2013-07-11 12:52:34 +0200587 /* init */
Kevin Wolf39c4ae92015-11-13 14:45:42 +0100588 if ((!file || !*file) && !qdict_size(bs_opts)) {
Max Reitz5ec18f82015-10-19 17:53:30 +0200589 BlockBackendRootState *blk_rs;
590
Kevin Wolfd861ab32019-04-25 14:25:10 +0200591 blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
Max Reitz5ec18f82015-10-19 17:53:30 +0200592 blk_rs = blk_get_root_state(blk);
593 blk_rs->open_flags = bdrv_flags;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300594 blk_rs->read_only = read_only;
Max Reitz5ec18f82015-10-19 17:53:30 +0200595 blk_rs->detect_zeroes = detect_zeroes;
596
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200597 qobject_unref(bs_opts);
Max Reitze4342ce2015-02-05 13:58:14 -0500598 } else {
599 if (file && !*file) {
600 file = NULL;
601 }
602
Kevin Wolf91a097e2015-05-08 17:49:53 +0200603 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
604 * with other callers) rather than what we want as the real defaults.
605 * Apply the defaults here instead. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200606 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
607 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300608 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
609 read_only ? "on" : "off");
Kevin Wolf9384a442018-10-17 18:24:30 +0200610 qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on");
Kevin Wolf72e775c2016-03-15 14:34:37 +0100611 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200612
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100613 if (runstate_check(RUN_STATE_INMIGRATE)) {
614 bdrv_flags |= BDRV_O_INACTIVE;
615 }
616
Max Reitzefaa7c42016-03-16 19:54:38 +0100617 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500618 if (!blk) {
619 goto err_no_bs_opts;
620 }
621 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500622
Max Reitz5ec18f82015-10-19 17:53:30 +0200623 bs->detect_zeroes = detect_zeroes;
624
Paolo Bonzini9caa6f32017-06-05 14:39:07 +0200625 block_acct_setup(blk_get_stats(blk), account_invalid, account_failed);
Alberto Garcia2be55062015-10-28 17:33:07 +0200626
Alberto Garcia40119ef2015-11-16 11:28:38 +0200627 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
628 blk_unref(blk);
629 blk = NULL;
630 goto err_no_bs_opts;
Alberto Garcia2be55062015-10-28 17:33:07 +0200631 }
Max Reitz5ec18f82015-10-19 17:53:30 +0200632 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200633
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100634 /* disk I/O throttling */
635 if (throttle_enabled(&cfg)) {
636 if (!throttling_group) {
Alberto Garciaff356ee2016-07-08 17:03:00 +0300637 throttling_group = id;
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100638 }
639 blk_io_limits_enable(blk, throttling_group);
640 blk_set_io_limits(blk, &cfg);
641 }
642
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100643 blk_set_enable_write_cache(blk, !writethrough);
Max Reitz373340b2015-10-19 17:53:22 +0200644 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200645
Alberto Garciaff356ee2016-07-08 17:03:00 +0300646 if (!monitor_add_blk(blk, id, errp)) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100647 blk_unref(blk);
648 blk = NULL;
649 goto err_no_bs_opts;
650 }
651
Max Reitze4342ce2015-02-05 13:58:14 -0500652err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100653 qemu_opts_del(opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200654 qobject_unref(interval_dict);
655 qobject_unref(interval_list);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200656 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100657
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100658early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100659 qemu_opts_del(opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200660 qobject_unref(interval_dict);
661 qobject_unref(interval_list);
Markus Armbruster6376f952014-05-28 11:17:01 +0200662err_no_opts:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200663 qobject_unref(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100664 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200665}
666
Max Reitzbd745e22015-10-19 17:53:32 +0200667/* Takes the ownership of bs_opts */
Maxim Levitsky89802d52020-03-08 11:24:40 +0200668BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
Max Reitzbd745e22015-10-19 17:53:32 +0200669{
Max Reitzbd745e22015-10-19 17:53:32 +0200670 int bdrv_flags = 0;
671
Kevin Wolfa81d6162016-03-07 14:23:04 +0100672 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
673 * with other callers) rather than what we want as the real defaults.
674 * Apply the defaults here instead. */
Kevin Wolfa81d6162016-03-07 14:23:04 +0100675 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
676 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300677 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
Kevin Wolfa81d6162016-03-07 14:23:04 +0100678
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100679 if (runstate_check(RUN_STATE_INMIGRATE)) {
680 bdrv_flags |= BDRV_O_INACTIVE;
681 }
682
Kevin Wolf74e1ae72016-09-20 20:48:52 +0200683 return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
Max Reitzbd745e22015-10-19 17:53:32 +0200684}
685
Max Reitz9c4218e2016-01-29 16:36:12 +0100686void blockdev_close_all_bdrv_states(void)
687{
688 BlockDriverState *bs, *next_bs;
689
690 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
691 AioContext *ctx = bdrv_get_aio_context(bs);
692
693 aio_context_acquire(ctx);
694 bdrv_unref(bs);
695 aio_context_release(ctx);
696 }
697}
698
Max Reitz262b4e82016-03-16 19:54:41 +0100699/* Iterates over the list of monitor-owned BlockDriverStates */
700BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
701{
702 return bs ? QTAILQ_NEXT(bs, monitor_list)
703 : QTAILQ_FIRST(&monitor_bdrv_states);
704}
705
Markus Armbrusterc75d7f72020-07-07 18:05:41 +0200706static bool qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200707 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200708{
709 const char *value;
710
711 value = qemu_opt_get(opts, from);
712 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200713 if (qemu_opt_find(opts, to)) {
714 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
715 "same time", to, from);
Markus Armbrusterc75d7f72020-07-07 18:05:41 +0200716 return false;
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200717 }
Jun Li20d6cd42014-09-24 13:45:27 +0800718 }
719
720 /* rename all items in opts */
721 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100722 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200723 qemu_opt_unset(opts, from);
724 }
Markus Armbrusterc75d7f72020-07-07 18:05:41 +0200725 return true;
Kevin Wolf57975222013-07-17 14:41:54 +0200726}
727
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200728QemuOptsList qemu_legacy_drive_opts = {
729 .name = "drive",
730 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
731 .desc = {
732 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200733 .name = "bus",
734 .type = QEMU_OPT_NUMBER,
735 .help = "bus number",
736 },{
737 .name = "unit",
738 .type = QEMU_OPT_NUMBER,
739 .help = "unit number (i.e. lun for scsi)",
740 },{
741 .name = "index",
742 .type = QEMU_OPT_NUMBER,
743 .help = "index number",
744 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200745 .name = "media",
746 .type = QEMU_OPT_STRING,
747 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200748 },{
749 .name = "if",
750 .type = QEMU_OPT_STRING,
751 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200752 },{
Max Reitzd095b462013-12-20 19:28:14 +0100753 .name = "file",
754 .type = QEMU_OPT_STRING,
755 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200756 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200757
758 /* Options that are passed on, but have special semantics with -drive */
759 {
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300760 .name = BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200761 .type = QEMU_OPT_BOOL,
762 .help = "open drive file as read-only",
763 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100764 .name = "rerror",
765 .type = QEMU_OPT_STRING,
766 .help = "read error action",
767 },{
768 .name = "werror",
769 .type = QEMU_OPT_STRING,
770 .help = "write error action",
771 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200772 .name = "copy-on-read",
773 .type = QEMU_OPT_BOOL,
774 .help = "copy read data from backing file into image file",
775 },
776
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200777 { /* end of list */ }
778 },
779};
780
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200781DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,
782 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200783{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200784 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200785 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200786 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200787 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200788 QemuOpts *legacy_opts;
789 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200790 BlockInterfaceType type;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200791 int max_devs, bus_id, unit_id, index;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100792 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800793 bool read_only = false;
794 bool copy_on_read;
Max Reitzd095b462013-12-20 19:28:14 +0100795 const char *filename;
Kevin Wolf247147f2014-09-24 16:37:14 +0200796 int i;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200797
Kevin Wolf57975222013-07-17 14:41:54 +0200798 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200799 static const struct {
800 const char *from;
801 const char *to;
802 } opt_renames[] = {
803 { "iops", "throttling.iops-total" },
804 { "iops_rd", "throttling.iops-read" },
805 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200806
Kevin Wolf247147f2014-09-24 16:37:14 +0200807 { "bps", "throttling.bps-total" },
808 { "bps_rd", "throttling.bps-read" },
809 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200810
Kevin Wolf247147f2014-09-24 16:37:14 +0200811 { "iops_max", "throttling.iops-total-max" },
812 { "iops_rd_max", "throttling.iops-read-max" },
813 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200814
Kevin Wolf247147f2014-09-24 16:37:14 +0200815 { "bps_max", "throttling.bps-total-max" },
816 { "bps_rd_max", "throttling.bps-read-max" },
817 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200818
Kevin Wolf247147f2014-09-24 16:37:14 +0200819 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200820
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200821 { "group", "throttling.group" },
822
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300823 { "readonly", BDRV_OPT_READ_ONLY },
Kevin Wolf247147f2014-09-24 16:37:14 +0200824 };
825
826 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Markus Armbruster235e59c2020-07-07 18:05:42 +0200827 if (!qemu_opt_rename(all_opts, opt_renames[i].from,
Markus Armbruster668f62e2020-07-07 18:06:02 +0200828 opt_renames[i].to, errp)) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200829 return NULL;
830 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200831 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200832
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200833 value = qemu_opt_get(all_opts, "cache");
834 if (value) {
835 int flags = 0;
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100836 bool writethrough;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200837
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100838 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200839 error_setg(errp, "invalid cache option");
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200840 return NULL;
841 }
842
843 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200844 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
845 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100846 !writethrough, &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200847 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200848 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
849 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100850 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200851 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200852 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
853 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100854 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200855 }
856 qemu_opt_unset(all_opts, "cache");
857 }
858
Kevin Wolff298d072013-09-10 12:01:20 +0200859 /* Get a QDict for processing the options */
860 bs_opts = qdict_new();
861 qemu_opts_to_qdict(all_opts, bs_opts);
862
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800863 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
864 &error_abort);
Markus Armbrusteraf175e82020-07-07 18:06:03 +0200865 if (!qemu_opts_absorb_qdict(legacy_opts, bs_opts, errp)) {
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200866 goto fail;
867 }
868
869 /* Media type */
870 value = qemu_opt_get(legacy_opts, "media");
871 if (value) {
872 if (!strcmp(value, "disk")) {
873 media = MEDIA_DISK;
874 } else if (!strcmp(value, "cdrom")) {
875 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800876 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200877 } else {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200878 error_setg(errp, "'%s' invalid media", value);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200879 goto fail;
880 }
881 }
882
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200883 /* copy-on-read is disabled with a warning for read-only devices */
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300884 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200885 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
886
887 if (read_only && copy_on_read) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700888 warn_report("disabling copy-on-read on read-only drive");
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200889 copy_on_read = false;
890 }
891
Eric Blake46f5ac22017-04-27 16:58:17 -0500892 qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off");
893 qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off");
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200894
Kevin Wolf593d4642013-08-28 17:24:51 +0200895 /* Controller type */
896 value = qemu_opt_get(legacy_opts, "if");
897 if (value) {
898 for (type = 0;
899 type < IF_COUNT && strcmp(value, if_name[type]);
900 type++) {
901 }
902 if (type == IF_COUNT) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200903 error_setg(errp, "unsupported bus type '%s'", value);
Kevin Wolf593d4642013-08-28 17:24:51 +0200904 goto fail;
905 }
906 } else {
907 type = block_default_type;
908 }
909
Kevin Wolf87a899c2013-09-10 15:48:13 +0200910 /* Device address specified by bus/unit or index.
911 * If none was specified, try to find the first free one. */
912 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
913 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
914 index = qemu_opt_get_number(legacy_opts, "index", -1);
915
916 max_devs = if_max_devs[type];
917
918 if (index != -1) {
919 if (bus_id != 0 || unit_id != -1) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200920 error_setg(errp, "index cannot be used with bus and unit");
Kevin Wolf87a899c2013-09-10 15:48:13 +0200921 goto fail;
922 }
923 bus_id = drive_index_to_bus_id(type, index);
924 unit_id = drive_index_to_unit_id(type, index);
925 }
926
927 if (unit_id == -1) {
928 unit_id = 0;
929 while (drive_get(type, bus_id, unit_id) != NULL) {
930 unit_id++;
931 if (max_devs && unit_id >= max_devs) {
932 unit_id -= max_devs;
933 bus_id++;
934 }
935 }
936 }
937
938 if (max_devs && unit_id >= max_devs) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200939 error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1);
Kevin Wolf87a899c2013-09-10 15:48:13 +0200940 goto fail;
941 }
942
943 if (drive_get(type, bus_id, unit_id) != NULL) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200944 error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists",
945 bus_id, unit_id, index);
Kevin Wolf87a899c2013-09-10 15:48:13 +0200946 goto fail;
947 }
948
949 /* no id supplied -> create one */
950 if (qemu_opts_id(all_opts) == NULL) {
951 char *new_id;
952 const char *mediastr = "";
953 if (type == IF_IDE || type == IF_SCSI) {
954 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
955 }
956 if (max_devs) {
957 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
958 mediastr, unit_id);
959 } else {
960 new_id = g_strdup_printf("%s%s%i", if_name[type],
961 mediastr, unit_id);
962 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500963 qdict_put_str(bs_opts, "id", new_id);
Kevin Wolf87a899c2013-09-10 15:48:13 +0200964 g_free(new_id);
965 }
966
Kevin Wolf394c7d42013-09-13 14:09:17 +0200967 /* Add virtio block device */
Kevin Wolf394c7d42013-09-13 14:09:17 +0200968 if (type == IF_VIRTIO) {
969 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800970 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
971 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200972 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +0200973 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200974 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100975 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200976 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100977 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
978 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +0200979 }
980
Max Reitzd095b462013-12-20 19:28:14 +0100981 filename = qemu_opt_get(legacy_opts, "file");
982
Kevin Wolfee13ed12014-02-09 09:52:32 +0100983 /* Check werror/rerror compatibility with if=... */
984 werror = qemu_opt_get(legacy_opts, "werror");
985 if (werror != NULL) {
986 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
987 type != IF_NONE) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200988 error_setg(errp, "werror is not supported by this bus type");
Kevin Wolfee13ed12014-02-09 09:52:32 +0100989 goto fail;
990 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500991 qdict_put_str(bs_opts, "werror", werror);
Kevin Wolfee13ed12014-02-09 09:52:32 +0100992 }
993
994 rerror = qemu_opt_get(legacy_opts, "rerror");
995 if (rerror != NULL) {
996 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
997 type != IF_NONE) {
Markus Armbrusterc4f26c92018-10-17 10:26:57 +0200998 error_setg(errp, "rerror is not supported by this bus type");
Kevin Wolfee13ed12014-02-09 09:52:32 +0100999 goto fail;
1000 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001001 qdict_put_str(bs_opts, "rerror", rerror);
Kevin Wolfee13ed12014-02-09 09:52:32 +01001002 }
1003
Kevin Wolf2d246f02013-09-18 15:14:47 +02001004 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster992861f2020-07-07 18:06:04 +02001005 blk = blockdev_init(filename, bs_opts, errp);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001006 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +02001007 if (!blk) {
Kevin Wolf2d246f02013-09-18 15:14:47 +02001008 goto fail;
1009 }
1010
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001011 /* Create legacy DriveInfo */
1012 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001013 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001014
Kevin Wolfee13ed12014-02-09 09:52:32 +01001015 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001016 dinfo->bus = bus_id;
1017 dinfo->unit = unit_id;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001018
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001019 blk_set_legacy_dinfo(blk, dinfo);
1020
Kevin Wolfe34ef042013-09-19 14:24:10 +02001021 switch(type) {
1022 case IF_IDE:
1023 case IF_SCSI:
1024 case IF_XEN:
1025 case IF_NONE:
1026 dinfo->media_cd = media == MEDIA_CDROM;
1027 break;
1028 default:
1029 break;
1030 }
1031
Kevin Wolf2d246f02013-09-18 15:14:47 +02001032fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001033 qemu_opts_del(legacy_opts);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001034 qobject_unref(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001035 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001036}
1037
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02001038static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1039{
1040 BlockDriverState *bs;
1041
1042 bs = bdrv_lookup_bs(name, name, errp);
1043 if (bs == NULL) {
1044 return NULL;
1045 }
1046
1047 if (!bdrv_is_root_node(bs)) {
1048 error_setg(errp, "Need a root block node");
1049 return NULL;
1050 }
1051
1052 if (!bdrv_is_inserted(bs)) {
1053 error_setg(errp, "Device has no medium");
1054 return NULL;
1055 }
1056
1057 return bs;
1058}
1059
Eric Blake10f75902016-03-03 09:16:50 -07001060static void blockdev_do_action(TransactionAction *action, Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001061{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001062 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001063
Eric Blake10f75902016-03-03 09:16:50 -07001064 list.value = action;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001065 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001066 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001067}
1068
Benoît Canet0901f672014-01-23 21:31:38 +01001069void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1070 bool has_node_name, const char *node_name,
1071 const char *snapshot_file,
1072 bool has_snapshot_node_name,
1073 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001074 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001075 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001076{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001077 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001078 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001079 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001080 .has_node_name = has_node_name,
1081 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001082 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001083 .has_snapshot_node_name = has_snapshot_node_name,
1084 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001085 .has_format = has_format,
1086 .format = (char *) format,
1087 .has_mode = has_mode,
1088 .mode = mode,
1089 };
Eric Blake10f75902016-03-03 09:16:50 -07001090 TransactionAction action = {
1091 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001092 .u.blockdev_snapshot_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001093 };
1094 blockdev_do_action(&action, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001095}
1096
Alberto Garcia43de7e22015-10-26 14:27:16 +02001097void qmp_blockdev_snapshot(const char *node, const char *overlay,
1098 Error **errp)
1099{
1100 BlockdevSnapshot snapshot_data = {
1101 .node = (char *) node,
1102 .overlay = (char *) overlay
1103 };
Eric Blake10f75902016-03-03 09:16:50 -07001104 TransactionAction action = {
1105 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
Eric Blake32bafa82016-03-17 16:48:37 -06001106 .u.blockdev_snapshot.data = &snapshot_data,
Eric Blake10f75902016-03-03 09:16:50 -07001107 };
1108 blockdev_do_action(&action, errp);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001109}
1110
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001111void qmp_blockdev_snapshot_internal_sync(const char *device,
1112 const char *name,
1113 Error **errp)
1114{
1115 BlockdevSnapshotInternal snapshot = {
1116 .device = (char *) device,
1117 .name = (char *) name
1118 };
Eric Blake10f75902016-03-03 09:16:50 -07001119 TransactionAction action = {
1120 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001121 .u.blockdev_snapshot_internal_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001122 };
1123 blockdev_do_action(&action, errp);
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001124}
1125
Wenchao Xia44e3e052013-09-11 14:04:36 +08001126SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1127 bool has_id,
1128 const char *id,
1129 bool has_name,
1130 const char *name,
1131 Error **errp)
1132{
Fam Zhenga0e85442015-03-02 19:36:48 +08001133 BlockDriverState *bs;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001134 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001135 QEMUSnapshotInfo sn;
1136 Error *local_err = NULL;
1137 SnapshotInfo *info = NULL;
1138 int ret;
1139
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001140 bs = qmp_get_root_bs(device, errp);
1141 if (!bs) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001142 return NULL;
1143 }
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001144 aio_context = bdrv_get_aio_context(bs);
Max Reitz5433c242015-10-19 17:53:29 +02001145 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001146
1147 if (!has_id) {
1148 id = NULL;
1149 }
1150
1151 if (!has_name) {
1152 name = NULL;
1153 }
1154
1155 if (!id && !name) {
1156 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001157 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001158 }
1159
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001160 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1161 goto out_aio_context;
1162 }
1163
Wenchao Xia44e3e052013-09-11 14:04:36 +08001164 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &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 if (!ret) {
1170 error_setg(errp,
1171 "Snapshot with id '%s' and name '%s' does not exist on "
1172 "device '%s'",
1173 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001174 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001175 }
1176
1177 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001178 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001179 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001180 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001181 }
1182
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001183 aio_context_release(aio_context);
1184
Markus Armbruster5839e532014-08-19 10:31:08 +02001185 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001186 info->id = g_strdup(sn.id_str);
1187 info->name = g_strdup(sn.name);
1188 info->date_nsec = sn.date_nsec;
1189 info->date_sec = sn.date_sec;
1190 info->vm_state_size = sn.vm_state_size;
1191 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1192 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1193
1194 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001195
1196out_aio_context:
1197 aio_context_release(aio_context);
1198 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001199}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001200
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001201/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001202
John Snow50f43f02015-11-05 18:13:09 -05001203typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001204
John Snow50f43f02015-11-05 18:13:09 -05001205/**
1206 * BlkActionOps:
1207 * Table of operations that define an Action.
1208 *
1209 * @instance_size: Size of state struct, in bytes.
1210 * @prepare: Prepare the work, must NOT be NULL.
1211 * @commit: Commit the changes, can be NULL.
1212 * @abort: Abort the changes on fail, can be NULL.
1213 * @clean: Clean up resources after all transaction actions have called
1214 * commit() or abort(). Can be NULL.
1215 *
1216 * Only prepare() may fail. In a single transaction, only one of commit() or
1217 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001218 */
John Snow50f43f02015-11-05 18:13:09 -05001219typedef struct BlkActionOps {
1220 size_t instance_size;
1221 void (*prepare)(BlkActionState *common, Error **errp);
1222 void (*commit)(BlkActionState *common);
1223 void (*abort)(BlkActionState *common);
1224 void (*clean)(BlkActionState *common);
1225} BlkActionOps;
1226
1227/**
1228 * BlkActionState:
1229 * Describes one Action's state within a Transaction.
1230 *
1231 * @action: QAPI-defined enum identifying which Action to perform.
1232 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001233 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001234 * @entry: List membership for all Actions in this Transaction.
1235 *
1236 * This structure must be arranged as first member in a subclassed type,
1237 * assuming that the compiler will also arrange it to the same offsets as the
1238 * base class.
1239 */
1240struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001241 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001242 const BlkActionOps *ops;
Kevin Wolf62c9e412018-04-19 16:09:52 +02001243 JobTxn *block_job_txn;
John Snow94d16a62015-11-05 18:13:18 -05001244 TransactionProperties *txn_props;
John Snowf4de0f82019-01-11 11:59:16 -06001245 QTAILQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001246};
1247
Wenchao Xiabbe86012013-09-11 14:04:34 +08001248/* internal snapshot private data */
1249typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001250 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001251 BlockDriverState *bs;
1252 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001253 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001254} InternalSnapshotState;
1255
John Snow94d16a62015-11-05 18:13:18 -05001256
1257static int action_check_completion_mode(BlkActionState *s, Error **errp)
1258{
1259 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1260 error_setg(errp,
1261 "Action '%s' does not support Transaction property "
1262 "completion-mode = %s",
Markus Armbruster977c7362017-08-24 10:46:08 +02001263 TransactionActionKind_str(s->action->type),
1264 ActionCompletionMode_str(s->txn_props->completion_mode));
John Snow94d16a62015-11-05 18:13:18 -05001265 return -1;
1266 }
1267 return 0;
1268}
1269
John Snow50f43f02015-11-05 18:13:09 -05001270static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001271 Error **errp)
1272{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001273 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001274 const char *device;
1275 const char *name;
1276 BlockDriverState *bs;
1277 QEMUSnapshotInfo old_sn, *sn;
1278 bool ret;
1279 qemu_timeval tv;
1280 BlockdevSnapshotInternal *internal;
1281 InternalSnapshotState *state;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001282 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001283 int ret1;
1284
Eric Blake6a8f9662015-10-26 16:34:54 -06001285 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001286 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001287 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001288 state = DO_UPCAST(InternalSnapshotState, common, common);
1289
1290 /* 1. parse input */
1291 device = internal->device;
1292 name = internal->name;
1293
1294 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001295 if (action_check_completion_mode(common, errp) < 0) {
1296 return;
1297 }
1298
Kevin Wolf75dfd402016-06-23 14:20:24 +02001299 bs = qmp_get_root_bs(device, errp);
1300 if (!bs) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001301 return;
1302 }
1303
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001304 aio_context = bdrv_get_aio_context(bs);
1305 aio_context_acquire(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001306
Fam Zheng507306c2015-10-23 11:08:13 +08001307 state->bs = bs;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001308
1309 /* Paired with .clean() */
Fam Zheng507306c2015-10-23 11:08:13 +08001310 bdrv_drained_begin(bs);
1311
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001312 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001313 goto out;
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001314 }
1315
Wenchao Xiabbe86012013-09-11 14:04:34 +08001316 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001317 error_setg(errp, "Device '%s' is read only", device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001318 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001319 }
1320
1321 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001322 error_setg(errp, "Block format '%s' used by device '%s' "
1323 "does not support internal snapshots",
1324 bs->drv->format_name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001325 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001326 }
1327
1328 if (!strlen(name)) {
1329 error_setg(errp, "Name is empty");
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001330 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001331 }
1332
1333 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001334 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1335 &local_err);
1336 if (local_err) {
1337 error_propagate(errp, local_err);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001338 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001339 } else if (ret) {
1340 error_setg(errp,
1341 "Snapshot with name '%s' already exists on device '%s'",
1342 name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001343 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001344 }
1345
1346 /* 3. take the snapshot */
1347 sn = &state->sn;
1348 pstrcpy(sn->name, sizeof(sn->name), name);
1349 qemu_gettimeofday(&tv);
1350 sn->date_sec = tv.tv_sec;
1351 sn->date_nsec = tv.tv_usec * 1000;
1352 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1353
1354 ret1 = bdrv_snapshot_create(bs, sn);
1355 if (ret1 < 0) {
1356 error_setg_errno(errp, -ret1,
1357 "Failed to create snapshot '%s' on device '%s'",
1358 name, device);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001359 goto out;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001360 }
1361
1362 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001363 state->created = true;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001364
1365out:
1366 aio_context_release(aio_context);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001367}
1368
John Snow50f43f02015-11-05 18:13:09 -05001369static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001370{
1371 InternalSnapshotState *state =
1372 DO_UPCAST(InternalSnapshotState, common, common);
1373 BlockDriverState *bs = state->bs;
1374 QEMUSnapshotInfo *sn = &state->sn;
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001375 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001376 Error *local_error = NULL;
1377
Fam Zheng507306c2015-10-23 11:08:13 +08001378 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001379 return;
1380 }
1381
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001382 aio_context = bdrv_get_aio_context(state->bs);
1383 aio_context_acquire(aio_context);
1384
Wenchao Xiabbe86012013-09-11 14:04:34 +08001385 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001386 error_reportf_err(local_error,
1387 "Failed to delete snapshot with id '%s' and "
1388 "name '%s' on device '%s' in abort: ",
1389 sn->id_str, sn->name,
1390 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001391 }
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001392
1393 aio_context_release(aio_context);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001394}
1395
John Snow50f43f02015-11-05 18:13:09 -05001396static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001397{
1398 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1399 common, common);
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001400 AioContext *aio_context;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001401
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001402 if (!state->bs) {
1403 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001404 }
Stefan Hajnoczia36e4582017-12-06 14:45:46 +00001405
1406 aio_context = bdrv_get_aio_context(state->bs);
1407 aio_context_acquire(aio_context);
1408
1409 bdrv_drained_end(state->bs);
1410
1411 aio_context_release(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001412}
1413
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001414/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001415typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001416 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001417 BlockDriverState *old_bs;
1418 BlockDriverState *new_bs;
Kevin Wolf067acf22017-03-02 15:26:18 +01001419 bool overlay_appended;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001420} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001421
John Snow50f43f02015-11-05 18:13:09 -05001422static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001423 Error **errp)
1424{
Max Reitz5b363932016-05-17 16:41:31 +02001425 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001426 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001427 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001428 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001429 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001430 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001431 /* Reference to the new image (for 'blockdev-snapshot') */
1432 const char *snapshot_ref;
1433 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001434 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001435 ExternalSnapshotState *state =
1436 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001437 TransactionAction *action = common->action;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001438 AioContext *aio_context;
Kevin Wolfd29d3d12020-03-10 12:38:26 +01001439 uint64_t perm, shared;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001440
Alberto Garcia43de7e22015-10-26 14:27:16 +02001441 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1442 * purpose but a different set of parameters */
1443 switch (action->type) {
1444 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1445 {
Eric Blake32bafa82016-03-17 16:48:37 -06001446 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001447 device = s->node;
1448 node_name = s->node;
1449 new_image_file = NULL;
1450 snapshot_ref = s->overlay;
1451 }
1452 break;
1453 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1454 {
Eric Blake32bafa82016-03-17 16:48:37 -06001455 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001456 device = s->has_device ? s->device : NULL;
1457 node_name = s->has_node_name ? s->node_name : NULL;
1458 new_image_file = s->snapshot_file;
1459 snapshot_ref = NULL;
1460 }
1461 break;
1462 default:
1463 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001464 }
1465
1466 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001467 if (action_check_completion_mode(common, errp) < 0) {
1468 return;
1469 }
1470
Alberto Garcia43de7e22015-10-26 14:27:16 +02001471 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1472 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001473 return;
1474 }
1475
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001476 aio_context = bdrv_get_aio_context(state->old_bs);
1477 aio_context_acquire(aio_context);
1478
1479 /* Paired with .clean() */
Fam Zhengda763e82015-10-23 11:08:10 +08001480 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001481
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001482 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001483 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001484 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001485 }
1486
Fam Zheng3718d8a2014-05-23 21:29:43 +08001487 if (bdrv_op_is_blocked(state->old_bs,
1488 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001489 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001490 }
1491
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001492 if (!bdrv_is_read_only(state->old_bs)) {
1493 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001494 error_setg(errp, QERR_IO_ERROR);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001495 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001496 }
1497 }
1498
Alberto Garcia43de7e22015-10-26 14:27:16 +02001499 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001500 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001501 const char *format = s->has_format ? s->format : "qcow2";
1502 enum NewImageMode mode;
1503 const char *snapshot_node_name =
1504 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001505
Alberto Garcia43de7e22015-10-26 14:27:16 +02001506 if (node_name && !snapshot_node_name) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001507 error_setg(errp, "New overlay node name missing");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001508 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001509 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001510
1511 if (snapshot_node_name &&
1512 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001513 error_setg(errp, "New overlay node name already in use");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001514 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001515 }
1516
1517 flags = state->old_bs->open_flags;
John Snow2a32c6e2017-07-17 20:34:21 -04001518 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ);
1519 flags |= BDRV_O_NO_BACKING;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001520
1521 /* create new image w/backing file */
1522 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1523 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001524 int64_t size = bdrv_getlength(state->old_bs);
1525 if (size < 0) {
1526 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001527 goto out;
Kevin Wolff86b8b52016-03-02 12:16:44 +01001528 }
Max Reitzf30c66b2019-02-01 20:29:05 +01001529 bdrv_refresh_filename(state->old_bs);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001530 bdrv_img_create(new_image_file, format,
1531 state->old_bs->filename,
1532 state->old_bs->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08001533 NULL, size, flags, false, &local_err);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001534 if (local_err) {
1535 error_propagate(errp, local_err);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001536 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001537 }
1538 }
1539
1540 options = qdict_new();
Peter Maydelld52e1a02018-11-01 16:30:37 +00001541 if (snapshot_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05001542 qdict_put_str(options, "node-name", snapshot_node_name);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001543 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001544 qdict_put_str(options, "driver", format);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001545 }
1546
Max Reitz5b363932016-05-17 16:41:31 +02001547 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1548 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001549 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001550 if (!state->new_bs) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001551 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001552 }
1553
Kevin Wolfd29d3d12020-03-10 12:38:26 +01001554 /*
1555 * Allow attaching a backing file to an overlay that's already in use only
1556 * if the parents don't assume that they are already seeing a valid image.
1557 * (Specifically, allow it as a mirror target, which is write-only access.)
1558 */
1559 bdrv_get_cumulative_perm(state->new_bs, &perm, &shared);
1560 if (perm & BLK_PERM_CONSISTENT_READ) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001561 error_setg(errp, "The overlay is already in use");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001562 goto out;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001563 }
1564
Max Reitz7cc734a2019-06-12 18:34:46 +02001565 if (state->new_bs->drv->is_filter) {
1566 error_setg(errp, "Filters cannot be used as overlays");
1567 goto out;
1568 }
1569
1570 if (bdrv_cow_child(state->new_bs)) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001571 error_setg(errp, "The overlay already has a backing image");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001572 goto out;
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001573 }
1574
1575 if (!state->new_bs->drv->supports_backing) {
Max Reitza2bb6f82019-06-03 22:22:36 +02001576 error_setg(errp, "The overlay does not support backing images");
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001577 goto out;
Kevin Wolfb2c28322017-02-20 12:46:42 +01001578 }
1579
1580 /* This removes our old bs and adds the new bs. This is an operation that
1581 * can fail, so we need to do it in .prepare; undoing it for abort is
1582 * always possible. */
1583 bdrv_ref(state->new_bs);
1584 bdrv_append(state->new_bs, state->old_bs, &local_err);
1585 if (local_err) {
1586 error_propagate(errp, local_err);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001587 goto out;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001588 }
Kevin Wolf067acf22017-03-02 15:26:18 +01001589 state->overlay_appended = true;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001590
1591out:
1592 aio_context_release(aio_context);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001593}
1594
John Snow50f43f02015-11-05 18:13:09 -05001595static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001596{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001597 ExternalSnapshotState *state =
1598 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001599 AioContext *aio_context;
1600
1601 aio_context = bdrv_get_aio_context(state->old_bs);
1602 aio_context_acquire(aio_context);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001603
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001604 /* We don't need (or want) to use the transactional
1605 * bdrv_reopen_multiple() across all the entries at once, because we
1606 * don't want to abort all of them if one of them fails the reopen */
Paolo Bonzinid3faa132017-06-05 14:38:50 +02001607 if (!atomic_read(&state->old_bs->copy_on_read)) {
Alberto Garcia1b577742018-11-12 16:00:39 +02001608 bdrv_reopen_set_read_only(state->old_bs, true, NULL);
Kevin Wolf4c844982016-02-29 13:12:26 +01001609 }
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001610
1611 aio_context_release(aio_context);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001612}
1613
John Snow50f43f02015-11-05 18:13:09 -05001614static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001615{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001616 ExternalSnapshotState *state =
1617 DO_UPCAST(ExternalSnapshotState, common, common);
1618 if (state->new_bs) {
Kevin Wolf067acf22017-03-02 15:26:18 +01001619 if (state->overlay_appended) {
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001620 AioContext *aio_context;
Sergio Lopez377410f2020-01-08 15:31:37 +01001621 AioContext *tmp_context;
1622 int ret;
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001623
1624 aio_context = bdrv_get_aio_context(state->old_bs);
1625 aio_context_acquire(aio_context);
1626
Jeff Cody719fc282017-06-07 09:55:22 -04001627 bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
1628 close state->old_bs; we need it */
1629 bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
Sergio Lopez377410f2020-01-08 15:31:37 +01001630
1631 /*
1632 * The call to bdrv_set_backing_hd() above returns state->old_bs to
1633 * the main AioContext. As we're still going to be using it, return
1634 * it to the AioContext it was before.
1635 */
1636 tmp_context = bdrv_get_aio_context(state->old_bs);
1637 if (aio_context != tmp_context) {
1638 aio_context_release(aio_context);
1639 aio_context_acquire(tmp_context);
1640
1641 ret = bdrv_try_set_aio_context(state->old_bs,
1642 aio_context, NULL);
1643 assert(ret == 0);
1644
1645 aio_context_release(tmp_context);
1646 aio_context_acquire(aio_context);
1647 }
1648
Kevin Wolf5fe31c22017-03-06 16:20:51 +01001649 bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
Jeff Cody719fc282017-06-07 09:55:22 -04001650 bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001651
1652 aio_context_release(aio_context);
Kevin Wolfb2c28322017-02-20 12:46:42 +01001653 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001654 }
Fam Zhengda763e82015-10-23 11:08:10 +08001655}
1656
John Snow50f43f02015-11-05 18:13:09 -05001657static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001658{
1659 ExternalSnapshotState *state =
1660 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi2d24b602017-12-06 14:45:43 +00001661 AioContext *aio_context;
1662
1663 if (!state->old_bs) {
1664 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001665 }
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
1670 bdrv_drained_end(state->old_bs);
1671 bdrv_unref(state->new_bs);
1672
1673 aio_context_release(aio_context);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001674}
1675
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001676typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001677 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001678 BlockDriverState *bs;
1679 BlockJob *job;
1680} DriveBackupState;
1681
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001682static BlockJob *do_backup_common(BackupCommon *backup,
1683 BlockDriverState *bs,
1684 BlockDriverState *target_bs,
1685 AioContext *aio_context,
1686 JobTxn *txn, Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001687
John Snow50f43f02015-11-05 18:13:09 -05001688static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001689{
1690 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1691 DriveBackup *backup;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001692 BlockDriverState *bs;
1693 BlockDriverState *target_bs;
1694 BlockDriverState *source = NULL;
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001695 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001696 AioContext *old_context;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001697 QDict *options;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001698 Error *local_err = NULL;
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001699 int flags;
1700 int64_t size;
1701 bool set_backing_hd = false;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001702 int ret;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001703
Eric Blake6a8f9662015-10-26 16:34:54 -06001704 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001705 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001706
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001707 if (!backup->has_mode) {
1708 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1709 }
1710
Vladimir Sementsov-Ogievskiy85c9d132019-06-18 17:08:04 +03001711 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001712 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001713 return;
1714 }
1715
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001716 if (!bs->drv) {
1717 error_setg(errp, "Device has no medium");
1718 return;
1719 }
1720
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001721 aio_context = bdrv_get_aio_context(bs);
1722 aio_context_acquire(aio_context);
1723
1724 /* Paired with .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001725 bdrv_drained_begin(bs);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001726
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001727 if (!backup->has_format) {
1728 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
1729 NULL : (char *) bs->drv->format_name;
1730 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001731
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001732 /* Early check to avoid creating target */
1733 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
1734 goto out;
1735 }
1736
1737 flags = bs->open_flags | BDRV_O_RDWR;
1738
1739 /*
1740 * See if we have a backing HD we can use to create our new image
1741 * on top of.
1742 */
1743 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
Max Reitz2b088c62019-06-12 17:46:45 +02001744 /*
1745 * Backup will not replace the source by the target, so none
1746 * of the filters skipped here will be removed (in contrast to
1747 * mirror). Therefore, we can skip all of them when looking
1748 * for the first COW relationship.
1749 */
1750 source = bdrv_cow_bs(bdrv_skip_filters(bs));
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001751 if (!source) {
1752 backup->sync = MIRROR_SYNC_MODE_FULL;
1753 }
1754 }
1755 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
1756 source = bs;
1757 flags |= BDRV_O_NO_BACKING;
1758 set_backing_hd = true;
1759 }
1760
1761 size = bdrv_getlength(bs);
1762 if (size < 0) {
1763 error_setg_errno(errp, -size, "bdrv_getlength failed");
1764 goto out;
1765 }
1766
1767 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
1768 assert(backup->format);
1769 if (source) {
Max Reitz2b088c62019-06-12 17:46:45 +02001770 /* Implicit filters should not appear in the filename */
1771 BlockDriverState *explicit_backing =
1772 bdrv_skip_implicit_filters(source);
1773
1774 bdrv_refresh_filename(explicit_backing);
1775 bdrv_img_create(backup->target, backup->format,
1776 explicit_backing->filename,
1777 explicit_backing->drv->format_name, NULL,
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001778 size, flags, false, &local_err);
1779 } else {
1780 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
1781 size, flags, false, &local_err);
1782 }
1783 }
1784
Markus Armbruster84d18f02014-01-30 15:07:28 +01001785 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001786 error_propagate(errp, local_err);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001787 goto out;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001788 }
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001789
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001790 options = qdict_new();
1791 qdict_put_str(options, "discard", "unmap");
1792 qdict_put_str(options, "detect-zeroes", "unmap");
1793 if (backup->format) {
1794 qdict_put_str(options, "driver", backup->format);
1795 }
1796
1797 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
1798 if (!target_bs) {
1799 goto out;
1800 }
1801
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001802 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1803 old_context = bdrv_get_aio_context(target_bs);
1804 aio_context_release(aio_context);
1805 aio_context_acquire(old_context);
1806
1807 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
1808 if (ret < 0) {
1809 bdrv_unref(target_bs);
1810 aio_context_release(old_context);
1811 return;
1812 }
1813
1814 aio_context_release(old_context);
1815 aio_context_acquire(aio_context);
1816
Sergio Lopez2288ccf2020-01-08 15:31:32 +01001817 if (set_backing_hd) {
1818 bdrv_set_backing_hd(target_bs, source, &local_err);
1819 if (local_err) {
1820 goto unref;
1821 }
1822 }
1823
1824 state->bs = bs;
1825
1826 state->job = do_backup_common(qapi_DriveBackup_base(backup),
1827 bs, target_bs, aio_context,
1828 common->block_job_txn, errp);
1829
1830unref:
1831 bdrv_unref(target_bs);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001832out:
1833 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05001834}
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001835
John Snow111049a2016-11-08 01:50:38 -05001836static void drive_backup_commit(BlkActionState *common)
1837{
1838 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001839 AioContext *aio_context;
1840
1841 aio_context = bdrv_get_aio_context(state->bs);
1842 aio_context_acquire(aio_context);
1843
John Snow111049a2016-11-08 01:50:38 -05001844 assert(state->job);
Kevin Wolfda01ff72018-04-13 17:31:02 +02001845 job_start(&state->job->job);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001846
1847 aio_context_release(aio_context);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001848}
1849
John Snow50f43f02015-11-05 18:13:09 -05001850static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001851{
1852 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001853
John Snow111049a2016-11-08 01:50:38 -05001854 if (state->job) {
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001855 AioContext *aio_context;
1856
1857 aio_context = bdrv_get_aio_context(state->bs);
1858 aio_context_acquire(aio_context);
1859
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001860 job_cancel_sync(&state->job->job);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001861
1862 aio_context_release(aio_context);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001863 }
1864}
1865
John Snow50f43f02015-11-05 18:13:09 -05001866static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001867{
1868 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001869 AioContext *aio_context;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001870
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001871 if (!state->bs) {
1872 return;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001873 }
Stefan Hajnoczi66d56052017-12-06 14:45:44 +00001874
1875 aio_context = bdrv_get_aio_context(state->bs);
1876 aio_context_acquire(aio_context);
1877
1878 bdrv_drained_end(state->bs);
1879
1880 aio_context_release(aio_context);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001881}
1882
Fam Zhengbd8baec2014-12-18 18:37:06 +08001883typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001884 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001885 BlockDriverState *bs;
1886 BlockJob *job;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001887} BlockdevBackupState;
1888
John Snow50f43f02015-11-05 18:13:09 -05001889static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001890{
1891 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1892 BlockdevBackup *backup;
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01001893 BlockDriverState *bs;
1894 BlockDriverState *target_bs;
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001895 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001896 AioContext *old_context;
1897 int ret;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001898
Eric Blake6a8f9662015-10-26 16:34:54 -06001899 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001900 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001901
Vladimir Sementsov-Ogievskiy930fe172018-07-02 15:46:29 -04001902 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001903 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001904 return;
1905 }
1906
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01001907 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
1908 if (!target_bs) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001909 return;
1910 }
1911
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001912 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001913 aio_context = bdrv_get_aio_context(bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01001914 old_context = bdrv_get_aio_context(target_bs);
1915 aio_context_acquire(old_context);
1916
1917 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
1918 if (ret < 0) {
1919 aio_context_release(old_context);
1920 return;
1921 }
1922
1923 aio_context_release(old_context);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001924 aio_context_acquire(aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001925 state->bs = bs;
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001926
1927 /* Paired with .clean() */
Fam Zhengff52bf32015-10-23 11:08:12 +08001928 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001929
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01001930 state->job = do_backup_common(qapi_BlockdevBackup_base(backup),
1931 bs, target_bs, aio_context,
1932 common->block_job_txn, errp);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001933
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001934 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05001935}
Fam Zhengbd8baec2014-12-18 18:37:06 +08001936
John Snow111049a2016-11-08 01:50:38 -05001937static void blockdev_backup_commit(BlkActionState *common)
1938{
1939 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001940 AioContext *aio_context;
1941
1942 aio_context = bdrv_get_aio_context(state->bs);
1943 aio_context_acquire(aio_context);
1944
John Snow111049a2016-11-08 01:50:38 -05001945 assert(state->job);
Kevin Wolfda01ff72018-04-13 17:31:02 +02001946 job_start(&state->job->job);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001947
1948 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001949}
1950
John Snow50f43f02015-11-05 18:13:09 -05001951static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001952{
1953 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001954
John Snow111049a2016-11-08 01:50:38 -05001955 if (state->job) {
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001956 AioContext *aio_context;
1957
1958 aio_context = bdrv_get_aio_context(state->bs);
1959 aio_context_acquire(aio_context);
1960
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001961 job_cancel_sync(&state->job->job);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001962
1963 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001964 }
1965}
1966
John Snow50f43f02015-11-05 18:13:09 -05001967static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001968{
1969 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001970 AioContext *aio_context;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001971
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001972 if (!state->bs) {
1973 return;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001974 }
Stefan Hajnocziedd5ade2017-12-06 14:45:45 +00001975
1976 aio_context = bdrv_get_aio_context(state->bs);
1977 aio_context_acquire(aio_context);
1978
1979 bdrv_drained_end(state->bs);
1980
1981 aio_context_release(aio_context);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001982}
1983
Fam Zhengdf9a6812015-11-09 18:16:54 +08001984typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05001985 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001986 BdrvDirtyBitmap *bitmap;
1987 BlockDriverState *bs;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001988 HBitmap *backup;
1989 bool prepared;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04001990 bool was_enabled;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001991} BlockDirtyBitmapState;
1992
John Snow50f43f02015-11-05 18:13:09 -05001993static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08001994 Error **errp)
1995{
1996 Error *local_err = NULL;
1997 BlockDirtyBitmapAdd *action;
1998 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1999 common, common);
2000
John Snow94d16a62015-11-05 18:13:18 -05002001 if (action_check_completion_mode(common, errp) < 0) {
2002 return;
2003 }
2004
Eric Blake32bafa82016-03-17 16:48:37 -06002005 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002006 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2007 qmp_block_dirty_bitmap_add(action->node, action->name,
2008 action->has_granularity, action->granularity,
Vladimir Sementsov-Ogievskiyfd5ae4c2017-06-28 15:05:23 +03002009 action->has_persistent, action->persistent,
John Snow0e2b7f02018-12-21 04:35:22 -05002010 action->has_disabled, action->disabled,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002011 &local_err);
2012
2013 if (!local_err) {
2014 state->prepared = true;
2015 } else {
2016 error_propagate(errp, local_err);
2017 }
2018}
2019
John Snow50f43f02015-11-05 18:13:09 -05002020static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002021{
2022 BlockDirtyBitmapAdd *action;
2023 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2024 common, common);
2025
Eric Blake32bafa82016-03-17 16:48:37 -06002026 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002027 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2028 * then the node reference and bitmap name must have been valid.
2029 */
2030 if (state->prepared) {
2031 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2032 }
2033}
2034
John Snow50f43f02015-11-05 18:13:09 -05002035static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002036 Error **errp)
2037{
2038 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2039 common, common);
2040 BlockDirtyBitmap *action;
2041
John Snow94d16a62015-11-05 18:13:18 -05002042 if (action_check_completion_mode(common, errp) < 0) {
2043 return;
2044 }
2045
Eric Blake32bafa82016-03-17 16:48:37 -06002046 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002047 state->bitmap = block_dirty_bitmap_lookup(action->node,
2048 action->name,
2049 &state->bs,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002050 errp);
2051 if (!state->bitmap) {
2052 return;
2053 }
2054
John Snow3ae96d62019-03-12 12:05:49 -04002055 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) {
Vladimir Sementsov-Ogievskiyd6883bc2017-06-28 15:05:10 +03002056 return;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002057 }
2058
2059 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
Fam Zhengdf9a6812015-11-09 18:16:54 +08002060}
2061
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002062static void block_dirty_bitmap_restore(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002063{
2064 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2065 common, common);
2066
John Snow184dd9c2017-03-15 17:28:11 -04002067 if (state->backup) {
Vladimir Sementsov-Ogievskiy56bd6622018-10-29 16:23:14 -04002068 bdrv_restore_dirty_bitmap(state->bitmap, state->backup);
John Snow184dd9c2017-03-15 17:28:11 -04002069 }
Fam Zhengdf9a6812015-11-09 18:16:54 +08002070}
2071
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002072static void block_dirty_bitmap_free_backup(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002073{
2074 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2075 common, common);
2076
2077 hbitmap_free(state->backup);
2078}
2079
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002080static void block_dirty_bitmap_enable_prepare(BlkActionState *common,
2081 Error **errp)
2082{
2083 BlockDirtyBitmap *action;
2084 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2085 common, common);
2086
2087 if (action_check_completion_mode(common, errp) < 0) {
2088 return;
2089 }
2090
John Snow0e2b7f02018-12-21 04:35:22 -05002091 action = common->action->u.block_dirty_bitmap_enable.data;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002092 state->bitmap = block_dirty_bitmap_lookup(action->node,
2093 action->name,
2094 NULL,
2095 errp);
2096 if (!state->bitmap) {
2097 return;
2098 }
2099
John Snow3ae96d62019-03-12 12:05:49 -04002100 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snowb053bb52018-10-29 16:23:16 -04002101 return;
2102 }
2103
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002104 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2105 bdrv_enable_dirty_bitmap(state->bitmap);
2106}
2107
2108static void block_dirty_bitmap_enable_abort(BlkActionState *common)
2109{
2110 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2111 common, common);
2112
2113 if (!state->was_enabled) {
2114 bdrv_disable_dirty_bitmap(state->bitmap);
2115 }
2116}
2117
2118static void block_dirty_bitmap_disable_prepare(BlkActionState *common,
2119 Error **errp)
2120{
2121 BlockDirtyBitmap *action;
2122 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2123 common, common);
2124
2125 if (action_check_completion_mode(common, errp) < 0) {
2126 return;
2127 }
2128
John Snow0e2b7f02018-12-21 04:35:22 -05002129 action = common->action->u.block_dirty_bitmap_disable.data;
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002130 state->bitmap = block_dirty_bitmap_lookup(action->node,
2131 action->name,
2132 NULL,
2133 errp);
2134 if (!state->bitmap) {
2135 return;
2136 }
2137
John Snow3ae96d62019-03-12 12:05:49 -04002138 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snowb053bb52018-10-29 16:23:16 -04002139 return;
2140 }
2141
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002142 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2143 bdrv_disable_dirty_bitmap(state->bitmap);
2144}
2145
2146static void block_dirty_bitmap_disable_abort(BlkActionState *common)
2147{
2148 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2149 common, common);
2150
2151 if (state->was_enabled) {
2152 bdrv_enable_dirty_bitmap(state->bitmap);
2153 }
2154}
2155
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002156static void block_dirty_bitmap_merge_prepare(BlkActionState *common,
2157 Error **errp)
2158{
2159 BlockDirtyBitmapMerge *action;
2160 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2161 common, common);
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002162
2163 if (action_check_completion_mode(common, errp) < 0) {
2164 return;
2165 }
2166
John Snow0e2b7f02018-12-21 04:35:22 -05002167 action = common->action->u.block_dirty_bitmap_merge.data;
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002168
Eric Blakec6996cf2020-05-12 20:16:43 -05002169 state->bitmap = block_dirty_bitmap_merge(action->node, action->target,
2170 action->bitmaps, &state->backup,
2171 errp);
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002172}
2173
John Snowc4e4b0f2019-07-29 16:35:54 -04002174static void block_dirty_bitmap_remove_prepare(BlkActionState *common,
2175 Error **errp)
2176{
2177 BlockDirtyBitmap *action;
2178 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2179 common, common);
2180
2181 if (action_check_completion_mode(common, errp) < 0) {
2182 return;
2183 }
2184
2185 action = common->action->u.block_dirty_bitmap_remove.data;
2186
Eric Blakec6996cf2020-05-12 20:16:43 -05002187 state->bitmap = block_dirty_bitmap_remove(action->node, action->name,
2188 false, &state->bs, errp);
John Snowc4e4b0f2019-07-29 16:35:54 -04002189 if (state->bitmap) {
2190 bdrv_dirty_bitmap_skip_store(state->bitmap, true);
2191 bdrv_dirty_bitmap_set_busy(state->bitmap, true);
2192 }
2193}
2194
2195static void block_dirty_bitmap_remove_abort(BlkActionState *common)
2196{
2197 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2198 common, common);
2199
2200 if (state->bitmap) {
2201 bdrv_dirty_bitmap_skip_store(state->bitmap, false);
2202 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
2203 }
2204}
2205
2206static void block_dirty_bitmap_remove_commit(BlkActionState *common)
2207{
2208 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2209 common, common);
2210
2211 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
Vladimir Sementsov-Ogievskiy5deb6cb2019-09-16 17:19:09 +03002212 bdrv_release_dirty_bitmap(state->bitmap);
John Snowc4e4b0f2019-07-29 16:35:54 -04002213}
2214
John Snow50f43f02015-11-05 18:13:09 -05002215static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002216{
2217 error_setg(errp, "Transaction aborted using Abort action");
2218}
2219
John Snow50f43f02015-11-05 18:13:09 -05002220static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002221{
Stefan Weildfc6f862013-07-25 18:21:28 +02002222 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002223}
2224
John Snow50f43f02015-11-05 18:13:09 -05002225static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002226 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2227 .instance_size = sizeof(ExternalSnapshotState),
2228 .prepare = external_snapshot_prepare,
2229 .commit = external_snapshot_commit,
2230 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002231 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002232 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002233 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002234 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002235 .prepare = external_snapshot_prepare,
2236 .commit = external_snapshot_commit,
2237 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002238 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002239 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002240 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2241 .instance_size = sizeof(DriveBackupState),
2242 .prepare = drive_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002243 .commit = drive_backup_commit,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002244 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002245 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002246 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002247 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2248 .instance_size = sizeof(BlockdevBackupState),
2249 .prepare = blockdev_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002250 .commit = blockdev_backup_commit,
Fam Zhengbd8baec2014-12-18 18:37:06 +08002251 .abort = blockdev_backup_abort,
2252 .clean = blockdev_backup_clean,
2253 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002254 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002255 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002256 .prepare = abort_prepare,
2257 .commit = abort_commit,
2258 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002259 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2260 .instance_size = sizeof(InternalSnapshotState),
2261 .prepare = internal_snapshot_prepare,
2262 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002263 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002264 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002265 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2266 .instance_size = sizeof(BlockDirtyBitmapState),
2267 .prepare = block_dirty_bitmap_add_prepare,
2268 .abort = block_dirty_bitmap_add_abort,
2269 },
2270 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2271 .instance_size = sizeof(BlockDirtyBitmapState),
2272 .prepare = block_dirty_bitmap_clear_prepare,
Vladimir Sementsov-Ogievskiy5c4cf8b2018-10-29 16:23:15 -04002273 .commit = block_dirty_bitmap_free_backup,
2274 .abort = block_dirty_bitmap_restore,
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002275 },
John Snow0e2b7f02018-12-21 04:35:22 -05002276 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = {
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002277 .instance_size = sizeof(BlockDirtyBitmapState),
2278 .prepare = block_dirty_bitmap_enable_prepare,
2279 .abort = block_dirty_bitmap_enable_abort,
2280 },
John Snow0e2b7f02018-12-21 04:35:22 -05002281 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = {
Vladimir Sementsov-Ogievskiyc6490442018-06-11 14:53:32 -04002282 .instance_size = sizeof(BlockDirtyBitmapState),
2283 .prepare = block_dirty_bitmap_disable_prepare,
2284 .abort = block_dirty_bitmap_disable_abort,
John Snow66da04d2018-09-06 09:02:25 -04002285 },
John Snow0e2b7f02018-12-21 04:35:22 -05002286 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = {
Vladimir Sementsov-Ogievskiy6fd2e402018-10-29 16:23:15 -04002287 .instance_size = sizeof(BlockDirtyBitmapState),
2288 .prepare = block_dirty_bitmap_merge_prepare,
2289 .commit = block_dirty_bitmap_free_backup,
2290 .abort = block_dirty_bitmap_restore,
2291 },
John Snowc4e4b0f2019-07-29 16:35:54 -04002292 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = {
2293 .instance_size = sizeof(BlockDirtyBitmapState),
2294 .prepare = block_dirty_bitmap_remove_prepare,
2295 .commit = block_dirty_bitmap_remove_commit,
2296 .abort = block_dirty_bitmap_remove_abort,
2297 },
John Snow66da04d2018-09-06 09:02:25 -04002298 /* Where are transactions for MIRROR, COMMIT and STREAM?
2299 * Although these blockjobs use transaction callbacks like the backup job,
2300 * these jobs do not necessarily adhere to transaction semantics.
2301 * These jobs may not fully undo all of their actions on abort, nor do they
2302 * necessarily work in transactions with more than one job in them.
2303 */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002304};
2305
John Snow94d16a62015-11-05 18:13:18 -05002306/**
2307 * Allocate a TransactionProperties structure if necessary, and fill
2308 * that structure with desired defaults if they are unset.
2309 */
2310static TransactionProperties *get_transaction_properties(
2311 TransactionProperties *props)
2312{
2313 if (!props) {
2314 props = g_new0(TransactionProperties, 1);
2315 }
2316
2317 if (!props->has_completion_mode) {
2318 props->has_completion_mode = true;
2319 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2320 }
2321
2322 return props;
2323}
2324
Jeff Cody8802d1f2012-02-28 15:54:06 -05002325/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002326 * 'Atomic' group operations. The operations are performed as a set, and if
2327 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002328 */
John Snow94d16a62015-11-05 18:13:18 -05002329void qmp_transaction(TransactionActionList *dev_list,
2330 bool has_props,
2331 struct TransactionProperties *props,
2332 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002333{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002334 TransactionActionList *dev_entry = dev_list;
Kevin Wolf62c9e412018-04-19 16:09:52 +02002335 JobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002336 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002337 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002338
John Snowf4de0f82019-01-11 11:59:16 -06002339 QTAILQ_HEAD(, BlkActionState) snap_bdrv_states;
2340 QTAILQ_INIT(&snap_bdrv_states);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002341
John Snow94d16a62015-11-05 18:13:18 -05002342 /* Does this transaction get canceled as a group on failure?
Kevin Wolf62c9e412018-04-19 16:09:52 +02002343 * If not, we don't really need to make a JobTxn.
John Snow94d16a62015-11-05 18:13:18 -05002344 */
2345 props = get_transaction_properties(props);
2346 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02002347 block_job_txn = job_txn_new();
John Snow94d16a62015-11-05 18:13:18 -05002348 }
2349
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002350 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002351 bdrv_drain_all();
2352
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002353 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002354 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002355 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002356 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002357
Jeff Cody8802d1f2012-02-28 15:54:06 -05002358 dev_info = dev_entry->value;
2359 dev_entry = dev_entry->next;
2360
Eric Blake6a8f9662015-10-26 16:34:54 -06002361 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002362
Eric Blake6a8f9662015-10-26 16:34:54 -06002363 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002364 assert(ops->instance_size > 0);
2365
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002366 state = g_malloc0(ops->instance_size);
2367 state->ops = ops;
2368 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002369 state->block_job_txn = block_job_txn;
2370 state->txn_props = props;
John Snowf4de0f82019-01-11 11:59:16 -06002371 QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002372
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002373 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002374 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002375 error_propagate(errp, local_err);
2376 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002377 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002378 }
2379
John Snowf4de0f82019-01-11 11:59:16 -06002380 QTAILQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002381 if (state->ops->commit) {
2382 state->ops->commit(state);
2383 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002384 }
2385
2386 /* success */
2387 goto exit;
2388
2389delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002390 /* failure, and it is all-or-none; roll back all operations */
John Snowf4de0f82019-01-11 11:59:16 -06002391 QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002392 if (state->ops->abort) {
2393 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002394 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002395 }
2396exit:
John Snowf4de0f82019-01-11 11:59:16 -06002397 QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002398 if (state->ops->clean) {
2399 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002400 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002401 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002402 }
John Snow94d16a62015-11-05 18:13:18 -05002403 if (!has_props) {
2404 qapi_free_TransactionProperties(props);
2405 }
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02002406 job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002407}
2408
Benoît Canet12d3ba82014-01-23 21:31:35 +01002409void qmp_block_passwd(bool has_device, const char *device,
2410 bool has_node_name, const char *node_name,
2411 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002412{
Daniel P. Berrangec01c2142017-06-23 17:24:16 +01002413 error_setg(errp,
2414 "Setting block passwords directly is no longer supported");
Markus Armbruster666daa62010-06-02 18:48:27 +02002415}
2416
Vladimir Sementsov-Ogievskiya3b52532017-06-28 15:05:25 +03002417BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
2418 const char *name,
2419 Error **errp)
2420{
2421 BdrvDirtyBitmap *bitmap;
2422 BlockDriverState *bs;
2423 BlockDirtyBitmapSha256 *ret = NULL;
2424 char *sha256;
2425
2426 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
2427 if (!bitmap || !bs) {
2428 return NULL;
2429 }
2430
2431 sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp);
2432 if (sha256 == NULL) {
2433 return NULL;
2434 }
2435
2436 ret = g_new(BlockDirtyBitmapSha256, 1);
2437 ret->sha256 = sha256;
2438
2439 return ret;
2440}
2441
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002442void qmp_block_resize(bool has_device, const char *device,
2443 bool has_node_name, const char *node_name,
2444 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002445{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002446 Error *local_err = NULL;
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002447 BlockBackend *blk = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002448 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002449 AioContext *aio_context;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002450
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002451 bs = bdrv_lookup_bs(has_device ? device : NULL,
2452 has_node_name ? node_name : NULL,
2453 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002454 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002455 error_propagate(errp, local_err);
2456 return;
2457 }
2458
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002459 aio_context = bdrv_get_aio_context(bs);
2460 aio_context_acquire(aio_context);
2461
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002462 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002463 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002464 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002465 }
2466
Jeff Cody9c75e162014-06-25 16:55:30 -04002467 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002468 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002469 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04002470 }
2471
Eric Blakea3aeeab2020-04-28 14:26:46 -05002472 blk = blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL, errp);
2473 if (!blk) {
Kevin Wolfd7086422017-01-13 19:02:32 +01002474 goto out;
2475 }
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002476
John Snow698bdfa2017-05-10 13:39:45 -04002477 bdrv_drained_begin(bs);
Eric Blakea3aeeab2020-04-28 14:26:46 -05002478 blk_truncate(blk, size, false, PREALLOC_MODE_OFF, 0, errp);
John Snow698bdfa2017-05-10 13:39:45 -04002479 bdrv_drained_end(bs);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002480
2481out:
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002482 blk_unref(blk);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002483 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002484}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002485
Alberto Garcia23233222016-07-05 17:28:59 +03002486void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002487 bool has_base, const char *base,
Alberto Garcia312fe092016-10-28 10:08:19 +03002488 bool has_base_node, const char *base_node,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002489 bool has_backing_file, const char *backing_file,
2490 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002491 bool has_on_error, BlockdevOnError on_error,
John Snow241ca1a2018-09-06 09:02:23 -04002492 bool has_auto_finalize, bool auto_finalize,
2493 bool has_auto_dismiss, bool auto_dismiss,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002494 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002495{
Alberto Garcia554b6142016-10-28 10:08:11 +03002496 BlockDriverState *bs, *iter;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002497 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002498 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002499 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002500 const char *base_name = NULL;
John Snowcf6320d2018-09-06 09:02:12 -04002501 int job_flags = JOB_DEFAULT;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002502
Paolo Bonzini1d809092012-09-28 17:22:59 +02002503 if (!has_on_error) {
2504 on_error = BLOCKDEV_ON_ERROR_REPORT;
2505 }
2506
Alberto Garcia554b6142016-10-28 10:08:11 +03002507 bs = bdrv_lookup_bs(device, device, errp);
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002508 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002509 return;
2510 }
2511
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002512 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002513 aio_context_acquire(aio_context);
2514
Alberto Garcia312fe092016-10-28 10:08:19 +03002515 if (has_base && has_base_node) {
2516 error_setg(errp, "'base' and 'base-node' cannot be specified "
2517 "at the same time");
2518 goto out;
2519 }
2520
Jeff Cody13d8cc52014-06-25 15:40:11 -04002521 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002522 base_bs = bdrv_find_backing_image(bs, base);
2523 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002524 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002525 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002526 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002527 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04002528 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002529 }
2530
Alberto Garcia312fe092016-10-28 10:08:19 +03002531 if (has_base_node) {
2532 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2533 if (!base_bs) {
2534 goto out;
2535 }
2536 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
2537 error_setg(errp, "Node '%s' is not a backing image of '%s'",
2538 base_node, device);
2539 goto out;
2540 }
2541 assert(bdrv_get_aio_context(base_bs) == aio_context);
Max Reitzf30c66b2019-02-01 20:29:05 +01002542 bdrv_refresh_filename(base_bs);
Alberto Garcia312fe092016-10-28 10:08:19 +03002543 base_name = base_bs->filename;
2544 }
2545
Alberto Garcia554b6142016-10-28 10:08:11 +03002546 /* Check for op blockers in the whole chain between bs and base */
Max Reitz67acfd22019-06-12 17:48:11 +02002547 for (iter = bs; iter && iter != base_bs;
2548 iter = bdrv_filter_or_cow_bs(iter))
2549 {
Alberto Garcia554b6142016-10-28 10:08:11 +03002550 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
2551 goto out;
2552 }
2553 }
2554
Jeff Cody13d8cc52014-06-25 15:40:11 -04002555 /* if we are streaming the entire chain, the result will have no backing
2556 * file, and specifying one is therefore an error */
2557 if (base_bs == NULL && has_backing_file) {
2558 error_setg(errp, "backing file specified, but streaming the "
2559 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002560 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002561 }
2562
2563 /* backing_file string overrides base bs filename */
2564 base_name = has_backing_file ? backing_file : base_name;
2565
John Snow241ca1a2018-09-06 09:02:23 -04002566 if (has_auto_finalize && !auto_finalize) {
2567 job_flags |= JOB_MANUAL_FINALIZE;
2568 }
2569 if (has_auto_dismiss && !auto_dismiss) {
2570 job_flags |= JOB_MANUAL_DISMISS;
2571 }
2572
Alberto Garcia23233222016-07-05 17:28:59 +03002573 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
John Snowcf6320d2018-09-06 09:02:12 -04002574 job_flags, has_speed ? speed : 0, on_error, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002575 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002576 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002577 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002578 }
2579
Vladimir Sementsov-Ogievskiyb23c5802019-06-06 18:41:32 +03002580 trace_qmp_block_stream(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002581
2582out:
2583 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002584}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00002585
Alberto Garciafd62c602016-07-05 17:29:00 +03002586void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Kevin Wolf3c605f42017-06-27 17:18:20 +02002587 bool has_base_node, const char *base_node,
Jeff Cody7676e2c2014-06-30 15:14:15 +02002588 bool has_base, const char *base,
Kevin Wolf3c605f42017-06-27 17:18:20 +02002589 bool has_top_node, const char *top_node,
Jeff Cody7676e2c2014-06-30 15:14:15 +02002590 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04002591 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04002592 bool has_speed, int64_t speed,
Kevin Wolf8faad1c2020-02-14 21:08:11 +01002593 bool has_on_error, BlockdevOnError on_error,
Kevin Wolf0db832f2017-02-20 18:10:05 +01002594 bool has_filter_node_name, const char *filter_node_name,
John Snow96fbf532018-09-06 09:02:21 -04002595 bool has_auto_finalize, bool auto_finalize,
2596 bool has_auto_dismiss, bool auto_dismiss,
Jeff Codyed61fc12012-09-27 13:29:16 -04002597 Error **errp)
2598{
2599 BlockDriverState *bs;
Alberto Garcia058223a2016-10-28 10:08:07 +03002600 BlockDriverState *iter;
Jeff Codyed61fc12012-09-27 13:29:16 -04002601 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002602 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04002603 Error *local_err = NULL;
John Snow53607822018-09-06 09:02:10 -04002604 int job_flags = JOB_DEFAULT;
Jeff Codyed61fc12012-09-27 13:29:16 -04002605
Max Reitz54504662014-04-10 19:36:25 +02002606 if (!has_speed) {
2607 speed = 0;
2608 }
Kevin Wolf8faad1c2020-02-14 21:08:11 +01002609 if (!has_on_error) {
2610 on_error = BLOCKDEV_ON_ERROR_REPORT;
2611 }
Kevin Wolf0db832f2017-02-20 18:10:05 +01002612 if (!has_filter_node_name) {
2613 filter_node_name = NULL;
2614 }
John Snow96fbf532018-09-06 09:02:21 -04002615 if (has_auto_finalize && !auto_finalize) {
2616 job_flags |= JOB_MANUAL_FINALIZE;
2617 }
2618 if (has_auto_dismiss && !auto_dismiss) {
2619 job_flags |= JOB_MANUAL_DISMISS;
2620 }
Max Reitz54504662014-04-10 19:36:25 +02002621
Jeff Cody7676e2c2014-06-30 15:14:15 +02002622 /* Important Note:
2623 * libvirt relies on the DeviceNotFound error class in order to probe for
2624 * live commit feature versions; for this to work, we must make sure to
2625 * perform the device lookup before any generic errors that may occur in a
2626 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02002627 bs = qmp_get_root_bs(device, &local_err);
2628 if (!bs) {
2629 bs = bdrv_lookup_bs(device, device, NULL);
2630 if (!bs) {
2631 error_free(local_err);
2632 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2633 "Device '%s' not found", device);
2634 } else {
2635 error_propagate(errp, local_err);
2636 }
Jeff Codyed61fc12012-09-27 13:29:16 -04002637 return;
2638 }
Jeff Codyed61fc12012-09-27 13:29:16 -04002639
Kevin Wolf1d13b162016-06-23 14:20:24 +02002640 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002641 aio_context_acquire(aio_context);
2642
Fam Zhengbb000212014-09-11 13:14:00 +08002643 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002644 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08002645 }
2646
Jeff Codyed61fc12012-09-27 13:29:16 -04002647 /* default top_bs is the active layer */
2648 top_bs = bs;
2649
Kevin Wolf3c605f42017-06-27 17:18:20 +02002650 if (has_top_node && has_top) {
2651 error_setg(errp, "'top-node' and 'top' are mutually exclusive");
2652 goto out;
2653 } else if (has_top_node) {
2654 top_bs = bdrv_lookup_bs(NULL, top_node, errp);
2655 if (top_bs == NULL) {
2656 goto out;
2657 }
2658 if (!bdrv_chain_contains(bs, top_bs)) {
2659 error_setg(errp, "'%s' is not in this backing file chain",
2660 top_node);
2661 goto out;
2662 }
2663 } else if (has_top && top) {
Max Reitzf30c66b2019-02-01 20:29:05 +01002664 /* This strcmp() is just a shortcut, there is no need to
2665 * refresh @bs's filename. If it mismatches,
2666 * bdrv_find_backing_image() will do the refresh and may still
2667 * return @bs. */
Jeff Codyed61fc12012-09-27 13:29:16 -04002668 if (strcmp(bs->filename, top) != 0) {
2669 top_bs = bdrv_find_backing_image(bs, top);
2670 }
2671 }
2672
2673 if (top_bs == NULL) {
2674 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002675 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04002676 }
2677
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002678 assert(bdrv_get_aio_context(top_bs) == aio_context);
2679
Kevin Wolf3c605f42017-06-27 17:18:20 +02002680 if (has_base_node && has_base) {
2681 error_setg(errp, "'base-node' and 'base' are mutually exclusive");
2682 goto out;
2683 } else if (has_base_node) {
2684 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2685 if (base_bs == NULL) {
2686 goto out;
2687 }
2688 if (!bdrv_chain_contains(top_bs, base_bs)) {
2689 error_setg(errp, "'%s' is not in this backing file chain",
2690 base_node);
2691 goto out;
2692 }
2693 } else if (has_base && base) {
Jeff Codyd5208c42012-10-16 15:49:10 -04002694 base_bs = bdrv_find_backing_image(top_bs, base);
2695 } else {
2696 base_bs = bdrv_find_base(top_bs);
2697 }
2698
2699 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002700 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002701 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04002702 }
2703
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002704 assert(bdrv_get_aio_context(base_bs) == aio_context);
2705
Alberto Garcia058223a2016-10-28 10:08:07 +03002706 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
2707 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2708 goto out;
2709 }
Fam Zhengbb000212014-09-11 13:14:00 +08002710 }
2711
Jeff Cody7676e2c2014-06-30 15:14:15 +02002712 /* Do not allow attempts to commit an image into itself */
2713 if (top_bs == base_bs) {
2714 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002715 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02002716 }
2717
Fam Zheng20a63d22013-12-16 14:45:31 +08002718 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04002719 if (has_backing_file) {
2720 error_setg(errp, "'backing-file' specified,"
2721 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002722 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04002723 }
John Snow47970df2016-10-27 12:06:57 -04002724 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
John Snow53607822018-09-06 09:02:10 -04002725 job_flags, speed, on_error,
Fam Zheng78bbd912017-04-21 20:27:04 +08002726 filter_node_name, NULL, NULL, false, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08002727 } else {
Alberto Garcia058223a2016-10-28 10:08:07 +03002728 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
2729 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2730 goto out;
2731 }
John Snow53607822018-09-06 09:02:10 -04002732 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags,
2733 speed, on_error, has_backing_file ? backing_file : NULL,
Kevin Wolf0db832f2017-02-20 18:10:05 +01002734 filter_node_name, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08002735 }
Jeff Codyed61fc12012-09-27 13:29:16 -04002736 if (local_err != NULL) {
2737 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002738 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04002739 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01002740
2741out:
2742 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04002743}
2744
John Snow7b0b8702019-07-29 16:35:52 -04002745/* Common QMP interface for drive-backup and blockdev-backup */
2746static BlockJob *do_backup_common(BackupCommon *backup,
2747 BlockDriverState *bs,
2748 BlockDriverState *target_bs,
2749 AioContext *aio_context,
2750 JobTxn *txn, Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02002751{
John Snow111049a2016-11-08 01:50:38 -05002752 BlockJob *job = NULL;
John Snowd58d8452015-04-17 19:49:58 -04002753 BdrvDirtyBitmap *bmap = NULL;
John Snow7b0b8702019-07-29 16:35:52 -04002754 int job_flags = JOB_DEFAULT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02002755
Pavel Butsykin81206a82016-07-22 11:17:50 +03002756 if (!backup->has_speed) {
2757 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02002758 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03002759 if (!backup->has_on_source_error) {
2760 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02002761 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03002762 if (!backup->has_on_target_error) {
2763 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02002764 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03002765 if (!backup->has_job_id) {
2766 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02002767 }
John Snowb40dacd2018-03-10 03:27:44 -05002768 if (!backup->has_auto_finalize) {
2769 backup->auto_finalize = true;
2770 }
2771 if (!backup->has_auto_dismiss) {
2772 backup->auto_dismiss = true;
2773 }
Pavel Butsykin13b94142016-07-22 11:17:52 +03002774 if (!backup->has_compress) {
2775 backup->compress = false;
2776 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02002777
John Snowa6c93652019-07-29 16:35:55 -04002778 if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) ||
2779 (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) {
2780 /* done before desugaring 'incremental' to print the right message */
2781 if (!backup->has_bitmap) {
2782 error_setg(errp, "must provide a valid bitmap name for "
2783 "'%s' sync mode", MirrorSyncMode_str(backup->sync));
2784 return NULL;
2785 }
2786 }
2787
John Snowc8b56502019-07-29 16:35:52 -04002788 if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) {
2789 if (backup->has_bitmap_mode &&
2790 backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) {
2791 error_setg(errp, "Bitmap sync mode must be '%s' "
2792 "when using sync mode '%s'",
2793 BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS),
2794 MirrorSyncMode_str(backup->sync));
2795 return NULL;
2796 }
2797 backup->has_bitmap_mode = true;
2798 backup->sync = MIRROR_SYNC_MODE_BITMAP;
2799 backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS;
2800 }
2801
John Snow7b0b8702019-07-29 16:35:52 -04002802 if (backup->has_bitmap) {
2803 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
2804 if (!bmap) {
2805 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
2806 return NULL;
2807 }
John Snowc8b56502019-07-29 16:35:52 -04002808 if (!backup->has_bitmap_mode) {
2809 error_setg(errp, "Bitmap sync mode must be given "
2810 "when providing a bitmap");
2811 return NULL;
2812 }
John Snowb30ffbe2019-07-29 16:35:54 -04002813 if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) {
John Snow7b0b8702019-07-29 16:35:52 -04002814 return NULL;
2815 }
John Snow1a2b8b42019-07-29 16:35:55 -04002816
2817 /* This does not produce a useful bitmap artifact: */
2818 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
2819 error_setg(errp, "sync mode '%s' does not produce meaningful bitmap"
2820 " outputs", MirrorSyncMode_str(backup->sync));
2821 return NULL;
2822 }
2823
2824 /* If the bitmap isn't used for input or output, this is useless: */
2825 if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER &&
2826 backup->sync != MIRROR_SYNC_MODE_BITMAP) {
2827 error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect"
2828 " when combined with sync mode '%s'",
2829 BitmapSyncMode_str(backup->bitmap_mode),
2830 MirrorSyncMode_str(backup->sync));
2831 return NULL;
2832 }
2833 }
2834
2835 if (!backup->has_bitmap && backup->has_bitmap_mode) {
2836 error_setg(errp, "Cannot specify bitmap sync mode without a bitmap");
2837 return NULL;
John Snow7b0b8702019-07-29 16:35:52 -04002838 }
2839
2840 if (!backup->auto_finalize) {
2841 job_flags |= JOB_MANUAL_FINALIZE;
2842 }
2843 if (!backup->auto_dismiss) {
2844 job_flags |= JOB_MANUAL_DISMISS;
2845 }
2846
2847 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
John Snowc8b56502019-07-29 16:35:52 -04002848 backup->sync, bmap, backup->bitmap_mode,
2849 backup->compress,
Vladimir Sementsov-Ogievskiy00e30f02019-10-01 16:14:09 +03002850 backup->filter_node_name,
John Snowc8b56502019-07-29 16:35:52 -04002851 backup->on_source_error,
2852 backup->on_target_error,
John Snow7b0b8702019-07-29 16:35:52 -04002853 job_flags, NULL, NULL, txn, errp);
2854 return job;
2855}
2856
Sergio Lopez2288ccf2020-01-08 15:31:32 +01002857void qmp_drive_backup(DriveBackup *backup, Error **errp)
John Snow7b0b8702019-07-29 16:35:52 -04002858{
Sergio Lopez2288ccf2020-01-08 15:31:32 +01002859 TransactionAction action = {
2860 .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP,
2861 .u.drive_backup.data = backup,
2862 };
2863 blockdev_do_action(&action, errp);
John Snow78f51fd2015-11-05 18:13:17 -05002864}
2865
Peter Krempafacda542020-01-20 09:50:49 +01002866BlockDeviceInfoList *qmp_query_named_block_nodes(bool has_flat,
2867 bool flat,
2868 Error **errp)
Benoît Canetc13163f2014-01-23 21:31:34 +01002869{
Peter Krempafacda542020-01-20 09:50:49 +01002870 bool return_flat = has_flat && flat;
2871
2872 return bdrv_named_nodes_list(return_flat, errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01002873}
2874
Vladimir Sementsov-Ogievskiy5d3b4e92018-12-21 20:09:07 +03002875XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp)
2876{
2877 return bdrv_get_xdbg_block_graph(errp);
2878}
2879
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01002880void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08002881{
Sergio Lopez5b7bfe52020-01-08 15:31:33 +01002882 TransactionAction action = {
2883 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP,
2884 .u.blockdev_backup.data = backup,
2885 };
2886 blockdev_do_action(&action, errp);
John Snow78f51fd2015-11-05 18:13:17 -05002887}
2888
Fam Zheng4193cdd2015-12-24 12:45:03 +08002889/* Parameter check and block job starting for drive mirroring.
2890 * Caller should hold @device and @target's aio context (must be the same).
2891 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03002892static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08002893 BlockDriverState *target,
2894 bool has_replaces, const char *replaces,
2895 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02002896 BlockMirrorBackingMode backing_mode,
Max Reitzcdf3bc92019-07-24 19:12:30 +02002897 bool zero_target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08002898 bool has_speed, int64_t speed,
2899 bool has_granularity, uint32_t granularity,
2900 bool has_buf_size, int64_t buf_size,
2901 bool has_on_source_error,
2902 BlockdevOnError on_source_error,
2903 bool has_on_target_error,
2904 BlockdevOnError on_target_error,
2905 bool has_unmap, bool unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01002906 bool has_filter_node_name,
2907 const char *filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02002908 bool has_copy_mode, MirrorCopyMode copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04002909 bool has_auto_finalize, bool auto_finalize,
2910 bool has_auto_dismiss, bool auto_dismiss,
Fam Zheng4193cdd2015-12-24 12:45:03 +08002911 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02002912{
Max Reitz3f072a72019-06-12 16:27:32 +02002913 BlockDriverState *unfiltered_bs;
John Snowa1999b32018-09-06 09:02:11 -04002914 int job_flags = JOB_DEFAULT;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02002915
2916 if (!has_speed) {
2917 speed = 0;
2918 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02002919 if (!has_on_source_error) {
2920 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2921 }
2922 if (!has_on_target_error) {
2923 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2924 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01002925 if (!has_granularity) {
2926 granularity = 0;
2927 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01002928 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08002929 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01002930 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08002931 if (!has_unmap) {
2932 unmap = true;
2933 }
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01002934 if (!has_filter_node_name) {
2935 filter_node_name = NULL;
2936 }
Max Reitz481deba2018-06-13 20:18:22 +02002937 if (!has_copy_mode) {
2938 copy_mode = MIRROR_COPY_MODE_BACKGROUND;
2939 }
John Snowa6b58ad2018-09-06 09:02:22 -04002940 if (has_auto_finalize && !auto_finalize) {
2941 job_flags |= JOB_MANUAL_FINALIZE;
2942 }
2943 if (has_auto_dismiss && !auto_dismiss) {
2944 job_flags |= JOB_MANUAL_DISMISS;
2945 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01002946
Paolo Bonzinieee13df2013-01-21 17:09:46 +01002947 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002948 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2949 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01002950 return;
2951 }
2952 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002953 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2954 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01002955 return;
2956 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02002957
Fam Zheng4193cdd2015-12-24 12:45:03 +08002958 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
2959 return;
2960 }
Fam Zhenge40e5022015-12-24 12:45:04 +08002961 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
2962 return;
2963 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08002964
Max Reitz3f072a72019-06-12 16:27:32 +02002965 if (!bdrv_backing_chain_next(bs) && sync == MIRROR_SYNC_MODE_TOP) {
Fam Zheng4193cdd2015-12-24 12:45:03 +08002966 sync = MIRROR_SYNC_MODE_FULL;
2967 }
2968
Max Reitz3f072a72019-06-12 16:27:32 +02002969 if (!has_replaces) {
2970 /* We want to mirror from @bs, but keep implicit filters on top */
2971 unfiltered_bs = bdrv_skip_implicit_filters(bs);
2972 if (unfiltered_bs != bs) {
2973 replaces = unfiltered_bs->node_name;
2974 has_replaces = true;
2975 }
2976 }
2977
Max Reitz74ce9e42019-02-13 23:53:01 +01002978 if (has_replaces) {
2979 BlockDriverState *to_replace_bs;
2980 AioContext *replace_aio_context;
2981 int64_t bs_size, replace_size;
2982
2983 bs_size = bdrv_getlength(bs);
2984 if (bs_size < 0) {
2985 error_setg_errno(errp, -bs_size, "Failed to query device's size");
2986 return;
2987 }
2988
2989 to_replace_bs = check_to_replace_node(bs, replaces, errp);
2990 if (!to_replace_bs) {
2991 return;
2992 }
2993
2994 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
2995 aio_context_acquire(replace_aio_context);
2996 replace_size = bdrv_getlength(to_replace_bs);
2997 aio_context_release(replace_aio_context);
2998
2999 if (replace_size < 0) {
3000 error_setg_errno(errp, -replace_size,
3001 "Failed to query the replacement node's size");
3002 return;
3003 }
3004 if (bs_size != replace_size) {
3005 error_setg(errp, "cannot replace image with a mirror image of "
3006 "different size");
3007 return;
3008 }
3009 }
3010
Fam Zheng4193cdd2015-12-24 12:45:03 +08003011 /* pass the node name to replace to mirror start since it's loose coupling
3012 * and will allow to check whether the node still exist at mirror completion
3013 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003014 mirror_start(job_id, bs, target,
John Snowa1999b32018-09-06 09:02:11 -04003015 has_replaces ? replaces : NULL, job_flags,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003016 speed, granularity, buf_size, sync, backing_mode, zero_target,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003017 on_source_error, on_target_error, unmap, filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003018 copy_mode, errp);
Fam Zheng4193cdd2015-12-24 12:45:03 +08003019}
3020
Eric Blakefaecd402016-07-14 16:37:58 -06003021void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003022{
3023 BlockDriverState *bs;
Max Reitz3f072a72019-06-12 16:27:32 +02003024 BlockDriverState *target_backing_bs, *target_bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003025 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003026 AioContext *old_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003027 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003028 Error *local_err = NULL;
3029 QDict *options = NULL;
3030 int flags;
3031 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003032 const char *format = arg->format;
Max Reitzcdf3bc92019-07-24 19:12:30 +02003033 bool zero_target;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003034 int ret;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003035
Kevin Wolf0524e932016-06-23 14:20:24 +02003036 bs = qmp_get_root_bs(arg->device, errp);
3037 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003038 return;
3039 }
3040
Paolo Bonzinicb2af912018-02-07 17:29:20 +01003041 /* Early check to avoid creating target */
3042 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3043 return;
3044 }
3045
Kevin Wolf0524e932016-06-23 14:20:24 +02003046 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003047 aio_context_acquire(aio_context);
3048
Eric Blakefaecd402016-07-14 16:37:58 -06003049 if (!arg->has_mode) {
3050 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003051 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003052
Eric Blakefaecd402016-07-14 16:37:58 -06003053 if (!arg->has_format) {
3054 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3055 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003056 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003057
Kevin Wolf61de4c62016-03-18 17:46:45 +01003058 flags = bs->open_flags | BDRV_O_RDWR;
Max Reitz3f072a72019-06-12 16:27:32 +02003059 target_backing_bs = bdrv_cow_bs(bdrv_skip_filters(bs));
3060 if (!target_backing_bs && arg->sync == MIRROR_SYNC_MODE_TOP) {
Eric Blakefaecd402016-07-14 16:37:58 -06003061 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003062 }
Eric Blakefaecd402016-07-14 16:37:58 -06003063 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz3f072a72019-06-12 16:27:32 +02003064 target_backing_bs = bs;
Max Reitz117e0c82013-11-25 20:28:55 +01003065 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003066
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003067 size = bdrv_getlength(bs);
3068 if (size < 0) {
3069 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003070 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003071 }
3072
Eric Blakefaecd402016-07-14 16:37:58 -06003073 if (arg->has_replaces) {
Eric Blakefaecd402016-07-14 16:37:58 -06003074 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003075 error_setg(errp, "a node-name must be provided when replacing a"
3076 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003077 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003078 }
Benoît Canet09158f02014-06-27 18:25:25 +02003079 }
3080
Eric Blakefaecd402016-07-14 16:37:58 -06003081 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003082 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3083 } else {
3084 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3085 }
3086
John Snow2a32c6e2017-07-17 20:34:21 -04003087 /* Don't open backing image in create() */
3088 flags |= BDRV_O_NO_BACKING;
3089
Max Reitz3f072a72019-06-12 16:27:32 +02003090 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !target_backing_bs)
Eric Blakefaecd402016-07-14 16:37:58 -06003091 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003092 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003093 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003094 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003095 bdrv_img_create(arg->target, format,
Fam Zheng92172832017-04-21 20:27:01 +08003096 NULL, NULL, NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003097 } else {
Max Reitz3f072a72019-06-12 16:27:32 +02003098 /* Implicit filters should not appear in the filename */
3099 BlockDriverState *explicit_backing =
3100 bdrv_skip_implicit_filters(target_backing_bs);
3101
Eric Blakefaecd402016-07-14 16:37:58 -06003102 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003103 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003104 break;
3105 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3106 /* create new image with backing file */
Max Reitz3f072a72019-06-12 16:27:32 +02003107 bdrv_refresh_filename(explicit_backing);
Eric Blakefaecd402016-07-14 16:37:58 -06003108 bdrv_img_create(arg->target, format,
Max Reitz3f072a72019-06-12 16:27:32 +02003109 explicit_backing->filename,
3110 explicit_backing->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08003111 NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003112 break;
3113 default:
3114 abort();
3115 }
3116 }
3117
Markus Armbruster84d18f02014-01-30 15:07:28 +01003118 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003119 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003120 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003121 }
3122
Max Reitze6641712015-08-26 19:47:48 +02003123 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003124 if (arg->has_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003125 qdict_put_str(options, "node-name", arg->node_name);
Benoît Canet4c828dc2014-06-16 12:00:55 +02003126 }
Max Reitze6641712015-08-26 19:47:48 +02003127 if (format) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003128 qdict_put_str(options, "driver", format);
Max Reitze6641712015-08-26 19:47:48 +02003129 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003130
Paolo Bonzinib812f672013-01-21 17:09:43 +01003131 /* Mirroring takes care of copy-on-write using the source's backing
3132 * file.
3133 */
John Snow2a32c6e2017-07-17 20:34:21 -04003134 target_bs = bdrv_open(arg->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003135 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003136 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003137 }
3138
Max Reitzcdf3bc92019-07-24 19:12:30 +02003139 zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL &&
3140 (arg->mode == NEW_IMAGE_MODE_EXISTING ||
3141 !bdrv_has_zero_init(target_bs)));
3142
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003143
3144 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3145 old_context = bdrv_get_aio_context(target_bs);
3146 aio_context_release(aio_context);
3147 aio_context_acquire(old_context);
3148
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003149 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
3150 if (ret < 0) {
3151 bdrv_unref(target_bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003152 aio_context_release(old_context);
3153 return;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003154 }
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003155
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003156 aio_context_release(old_context);
3157 aio_context_acquire(aio_context);
3158
Eric Blakefaecd402016-07-14 16:37:58 -06003159 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3160 arg->has_replaces, arg->replaces, arg->sync,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003161 backing_mode, zero_target,
3162 arg->has_speed, arg->speed,
Eric Blakefaecd402016-07-14 16:37:58 -06003163 arg->has_granularity, arg->granularity,
3164 arg->has_buf_size, arg->buf_size,
3165 arg->has_on_source_error, arg->on_source_error,
3166 arg->has_on_target_error, arg->on_target_error,
3167 arg->has_unmap, arg->unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003168 false, NULL,
Max Reitz481deba2018-06-13 20:18:22 +02003169 arg->has_copy_mode, arg->copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04003170 arg->has_auto_finalize, arg->auto_finalize,
3171 arg->has_auto_dismiss, arg->auto_dismiss,
Markus Armbruster992861f2020-07-07 18:06:04 +02003172 errp);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02003173 bdrv_unref(target_bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003174out:
3175 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003176}
3177
Alberto Garcia71aa9862016-07-05 17:28:57 +03003178void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3179 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08003180 bool has_replaces, const char *replaces,
3181 MirrorSyncMode sync,
3182 bool has_speed, int64_t speed,
3183 bool has_granularity, uint32_t granularity,
3184 bool has_buf_size, int64_t buf_size,
3185 bool has_on_source_error,
3186 BlockdevOnError on_source_error,
3187 bool has_on_target_error,
3188 BlockdevOnError on_target_error,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003189 bool has_filter_node_name,
3190 const char *filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003191 bool has_copy_mode, MirrorCopyMode copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04003192 bool has_auto_finalize, bool auto_finalize,
3193 bool has_auto_dismiss, bool auto_dismiss,
Fam Zhengdf925622015-12-24 12:45:05 +08003194 Error **errp)
3195{
3196 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08003197 BlockDriverState *target_bs;
3198 AioContext *aio_context;
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003199 AioContext *old_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003200 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Max Reitzcdf3bc92019-07-24 19:12:30 +02003201 bool zero_target;
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003202 int ret;
Fam Zhengdf925622015-12-24 12:45:05 +08003203
Kevin Wolf07eec652016-06-23 14:20:24 +02003204 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08003205 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08003206 return;
3207 }
3208
3209 target_bs = bdrv_lookup_bs(target, target, errp);
3210 if (!target_bs) {
3211 return;
3212 }
3213
Max Reitzcdf3bc92019-07-24 19:12:30 +02003214 zero_target = (sync == MIRROR_SYNC_MODE_FULL);
3215
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003216 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3217 old_context = bdrv_get_aio_context(target_bs);
Fam Zhengdf925622015-12-24 12:45:05 +08003218 aio_context = bdrv_get_aio_context(bs);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003219 aio_context_acquire(old_context);
Fam Zhengdf925622015-12-24 12:45:05 +08003220
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003221 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
Sergio Lopez3ea67e02020-01-08 15:31:34 +01003222
3223 aio_context_release(old_context);
3224 aio_context_acquire(aio_context);
3225
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003226 if (ret < 0) {
3227 goto out;
3228 }
Fam Zhengdf925622015-12-24 12:45:05 +08003229
Alberto Garcia71aa9862016-07-05 17:28:57 +03003230 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02003231 has_replaces, replaces, sync, backing_mode,
Max Reitzcdf3bc92019-07-24 19:12:30 +02003232 zero_target, has_speed, speed,
Fam Zhengdf925622015-12-24 12:45:05 +08003233 has_granularity, granularity,
3234 has_buf_size, buf_size,
3235 has_on_source_error, on_source_error,
3236 has_on_target_error, on_target_error,
3237 true, true,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003238 has_filter_node_name, filter_node_name,
Max Reitz481deba2018-06-13 20:18:22 +02003239 has_copy_mode, copy_mode,
John Snowa6b58ad2018-09-06 09:02:22 -04003240 has_auto_finalize, auto_finalize,
3241 has_auto_dismiss, auto_dismiss,
Markus Armbruster992861f2020-07-07 18:06:04 +02003242 errp);
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003243out:
Fam Zhengdf925622015-12-24 12:45:05 +08003244 aio_context_release(aio_context);
3245}
3246
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003247/* Get a block job using its ID and acquire its AioContext */
3248static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003249 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003250{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003251 BlockJob *job;
3252
3253 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003254
Max Reitz5433c242015-10-19 17:53:29 +02003255 *aio_context = NULL;
3256
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003257 job = block_job_get(id);
3258
3259 if (!job) {
3260 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3261 "Block job '%s' not found", id);
3262 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003263 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003264
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003265 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003266 aio_context_acquire(*aio_context);
3267
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003268 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003269}
3270
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003271void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003272{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003273 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003274 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003275
3276 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003277 return;
3278 }
3279
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003280 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003281 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003282}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003283
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003284void qmp_block_job_cancel(const char *device,
3285 bool has_force, bool force, Error **errp)
3286{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003287 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003288 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003289
3290 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003291 return;
3292 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003293
3294 if (!has_force) {
3295 force = false;
3296 }
3297
Kevin Wolfb15de822018-04-18 17:10:26 +02003298 if (job_user_paused(&job->job) && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003299 error_setg(errp, "The block job for device '%s' is currently paused",
3300 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003301 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003302 }
3303
3304 trace_qmp_block_job_cancel(job);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02003305 job_user_cancel(&job->job, force, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003306out:
3307 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003308}
3309
3310void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003311{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003312 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003313 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003314
John Snow0ec4dfb2018-03-10 03:27:32 -05003315 if (!job) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003316 return;
3317 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003318
3319 trace_qmp_block_job_pause(job);
Kevin Wolfb15de822018-04-18 17:10:26 +02003320 job_user_pause(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003321 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003322}
3323
3324void qmp_block_job_resume(const char *device, Error **errp)
3325{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003326 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003327 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003328
John Snow0ec4dfb2018-03-10 03:27:32 -05003329 if (!job) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02003330 return;
3331 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003332
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003333 trace_qmp_block_job_resume(job);
Kevin Wolfb15de822018-04-18 17:10:26 +02003334 job_user_resume(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003335 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003336}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003337
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003338void qmp_block_job_complete(const char *device, Error **errp)
3339{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003340 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003341 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003342
3343 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003344 return;
3345 }
3346
3347 trace_qmp_block_job_complete(job);
Kevin Wolf3453d972018-04-23 12:24:16 +02003348 job_complete(&job->job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003349 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003350}
3351
John Snow11b61fb2018-03-10 03:27:43 -05003352void qmp_block_job_finalize(const char *id, Error **errp)
3353{
3354 AioContext *aio_context;
3355 BlockJob *job = find_block_job(id, &aio_context, errp);
3356
3357 if (!job) {
3358 return;
3359 }
3360
3361 trace_qmp_block_job_finalize(job);
Stefan Reiterb660a842020-04-07 13:56:49 +02003362 job_ref(&job->job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02003363 job_finalize(&job->job, errp);
Stefan Reiterb660a842020-04-07 13:56:49 +02003364
3365 /*
3366 * Job's context might have changed via job_finalize (and job_txn_apply
3367 * automatically acquires the new one), so make sure we release the correct
3368 * one.
3369 */
3370 aio_context = blk_get_aio_context(job->blk);
3371 job_unref(&job->job);
John Snow11b61fb2018-03-10 03:27:43 -05003372 aio_context_release(aio_context);
3373}
3374
John Snow75f71052018-03-10 03:27:36 -05003375void qmp_block_job_dismiss(const char *id, Error **errp)
3376{
3377 AioContext *aio_context;
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02003378 BlockJob *bjob = find_block_job(id, &aio_context, errp);
3379 Job *job;
John Snow75f71052018-03-10 03:27:36 -05003380
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02003381 if (!bjob) {
John Snow75f71052018-03-10 03:27:36 -05003382 return;
3383 }
3384
Kevin Wolf5f9a6a02018-04-24 17:10:12 +02003385 trace_qmp_block_job_dismiss(bjob);
3386 job = &bjob->job;
3387 job_dismiss(&job, errp);
John Snow75f71052018-03-10 03:27:36 -05003388 aio_context_release(aio_context);
3389}
3390
Jeff Codyfa40e652014-07-01 09:52:16 +02003391void qmp_change_backing_file(const char *device,
3392 const char *image_node_name,
3393 const char *backing_file,
3394 Error **errp)
3395{
3396 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003397 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02003398 BlockDriverState *image_bs = NULL;
3399 Error *local_err = NULL;
3400 bool ro;
Jeff Codyfa40e652014-07-01 09:52:16 +02003401 int ret;
3402
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003403 bs = qmp_get_root_bs(device, errp);
3404 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02003405 return;
3406 }
3407
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003408 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003409 aio_context_acquire(aio_context);
3410
Jeff Codyfa40e652014-07-01 09:52:16 +02003411 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3412 if (local_err) {
3413 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003414 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003415 }
3416
3417 if (!image_bs) {
3418 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003419 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003420 }
3421
3422 if (bdrv_find_base(image_bs) == image_bs) {
3423 error_setg(errp, "not allowing backing file change on an image "
3424 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003425 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003426 }
3427
3428 /* even though we are not necessarily operating on bs, we need it to
3429 * determine if block ops are currently prohibited on the chain */
3430 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003431 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003432 }
3433
3434 /* final sanity check */
3435 if (!bdrv_chain_contains(bs, image_bs)) {
3436 error_setg(errp, "'%s' and image file are not in the same chain",
3437 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003438 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003439 }
3440
3441 /* if not r/w, reopen to make r/w */
Jeff Codyfa40e652014-07-01 09:52:16 +02003442 ro = bdrv_is_read_only(image_bs);
3443
3444 if (ro) {
Alberto Garcia051a60f2018-11-12 16:00:38 +02003445 if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003446 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003447 }
3448 }
3449
3450 ret = bdrv_change_backing_file(image_bs, backing_file,
Eric Blakee54ee1b2020-07-06 15:39:53 -05003451 image_bs->drv ? image_bs->drv->format_name : "",
3452 false);
Jeff Codyfa40e652014-07-01 09:52:16 +02003453
3454 if (ret < 0) {
3455 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3456 backing_file);
3457 /* don't exit here, so we can try to restore open flags if
3458 * appropriate */
3459 }
3460
3461 if (ro) {
Markus Armbruster992861f2020-07-07 18:06:04 +02003462 bdrv_reopen_set_read_only(image_bs, true, errp);
Jeff Codyfa40e652014-07-01 09:52:16 +02003463 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003464
3465out:
3466 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02003467}
3468
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003469void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3470{
Max Reitzbe4b67b2015-10-19 17:53:09 +02003471 BlockDriverState *bs;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003472 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01003473 Visitor *v = qobject_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003474 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003475
Markus Armbruster1f584242020-04-24 10:43:35 +02003476 visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
Eric Blake3b098d52016-06-09 10:48:43 -06003477 visit_complete(v, &obj);
Max Reitz7dc847e2018-02-24 16:40:29 +01003478 qdict = qobject_to(QDict, obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003479
3480 qdict_flatten(qdict);
3481
Kevin Wolf9ec88732016-09-21 14:56:11 +02003482 if (!qdict_get_try_str(qdict, "node-name")) {
3483 error_setg(errp, "'node-name' must be specified for the root node");
3484 goto fail;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003485 }
3486
Kevin Wolf9ec88732016-09-21 14:56:11 +02003487 bs = bds_tree_init(qdict, errp);
3488 if (!bs) {
3489 goto fail;
3490 }
3491
Maxim Levitsky89802d52020-03-08 11:24:40 +02003492 bdrv_set_monitor_owned(bs);
Kevin Wolf9ec88732016-09-21 14:56:11 +02003493
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003494fail:
Eric Blake3b098d52016-06-09 10:48:43 -06003495 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003496}
3497
Alberto Garcia1479c732019-03-12 18:48:51 +02003498void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp)
3499{
3500 BlockDriverState *bs;
3501 AioContext *ctx;
3502 QObject *obj;
3503 Visitor *v = qobject_output_visitor_new(&obj);
Alberto Garcia1479c732019-03-12 18:48:51 +02003504 BlockReopenQueue *queue;
3505 QDict *qdict;
3506
3507 /* Check for the selected node name */
3508 if (!options->has_node_name) {
3509 error_setg(errp, "Node name not specified");
3510 goto fail;
3511 }
3512
3513 bs = bdrv_find_node(options->node_name);
3514 if (!bs) {
3515 error_setg(errp, "Cannot find node named '%s'", options->node_name);
3516 goto fail;
3517 }
3518
3519 /* Put all options in a QDict and flatten it */
Markus Armbruster1f584242020-04-24 10:43:35 +02003520 visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
Alberto Garcia1479c732019-03-12 18:48:51 +02003521 visit_complete(v, &obj);
3522 qdict = qobject_to(QDict, obj);
3523
3524 qdict_flatten(qdict);
3525
3526 /* Perform the reopen operation */
3527 ctx = bdrv_get_aio_context(bs);
3528 aio_context_acquire(ctx);
3529 bdrv_subtree_drained_begin(bs);
3530 queue = bdrv_reopen_queue(NULL, bs, qdict, false);
3531 bdrv_reopen_multiple(queue, errp);
3532 bdrv_subtree_drained_end(bs);
3533 aio_context_release(ctx);
3534
3535fail:
3536 visit_free(v);
3537}
3538
Markus Armbruster79b7a772017-03-21 17:53:28 +01003539void qmp_blockdev_del(const char *node_name, Error **errp)
Alberto Garcia81b936a2015-11-02 16:51:55 +02003540{
3541 AioContext *aio_context;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003542 BlockDriverState *bs;
3543
Kevin Wolf9ec88732016-09-21 14:56:11 +02003544 bs = bdrv_find_node(node_name);
3545 if (!bs) {
3546 error_setg(errp, "Cannot find node %s", node_name);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003547 return;
3548 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003549 if (bdrv_has_blk(bs)) {
3550 error_setg(errp, "Node %s is in use", node_name);
3551 return;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003552 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003553 aio_context = bdrv_get_aio_context(bs);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003554 aio_context_acquire(aio_context);
3555
Kevin Wolf9ec88732016-09-21 14:56:11 +02003556 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3557 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003558 }
3559
Paolo Bonzini70537ed2018-12-06 23:00:09 +01003560 if (!QTAILQ_IN_USE(bs, monitor_list)) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02003561 error_setg(errp, "Node %s is not owned by the monitor",
3562 bs->node_name);
3563 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003564 }
3565
Kevin Wolf9ec88732016-09-21 14:56:11 +02003566 if (bs->refcnt > 1) {
3567 error_setg(errp, "Block device %s is in use",
3568 bdrv_get_device_or_node_name(bs));
3569 goto out;
3570 }
3571
3572 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3573 bdrv_unref(bs);
3574
Alberto Garcia81b936a2015-11-02 16:51:55 +02003575out:
3576 aio_context_release(aio_context);
3577}
3578
Wen Congyang7f821592016-05-10 15:36:39 +08003579static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
3580 const char *child_name)
3581{
3582 BdrvChild *child;
3583
3584 QLIST_FOREACH(child, &parent_bs->children, next) {
3585 if (strcmp(child->name, child_name) == 0) {
3586 return child;
3587 }
3588 }
3589
3590 return NULL;
3591}
3592
3593void qmp_x_blockdev_change(const char *parent, bool has_child,
3594 const char *child, bool has_node,
3595 const char *node, Error **errp)
3596{
3597 BlockDriverState *parent_bs, *new_bs = NULL;
3598 BdrvChild *p_child;
3599
3600 parent_bs = bdrv_lookup_bs(parent, parent, errp);
3601 if (!parent_bs) {
3602 return;
3603 }
3604
3605 if (has_child == has_node) {
3606 if (has_child) {
3607 error_setg(errp, "The parameters child and node are in conflict");
3608 } else {
3609 error_setg(errp, "Either child or node must be specified");
3610 }
3611 return;
3612 }
3613
3614 if (has_child) {
3615 p_child = bdrv_find_child(parent_bs, child);
3616 if (!p_child) {
3617 error_setg(errp, "Node '%s' does not have child '%s'",
3618 parent, child);
3619 return;
3620 }
3621 bdrv_del_child(parent_bs, p_child, errp);
3622 }
3623
3624 if (has_node) {
3625 new_bs = bdrv_find_node(node);
3626 if (!new_bs) {
3627 error_setg(errp, "Node '%s' not found", node);
3628 return;
3629 }
3630 bdrv_add_child(parent_bs, new_bs, errp);
3631 }
3632}
3633
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003634BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3635{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003636 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003637 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003638
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003639 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
John Snow559b9352016-10-27 12:06:55 -04003640 BlockJobInfoList *elem;
3641 AioContext *aio_context;
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003642
John Snow559b9352016-10-27 12:06:55 -04003643 if (block_job_is_internal(job)) {
3644 continue;
3645 }
3646 elem = g_new0(BlockJobInfoList, 1);
3647 aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003648 aio_context_acquire(aio_context);
John Snow559b9352016-10-27 12:06:55 -04003649 elem->value = block_job_query(job, errp);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003650 aio_context_release(aio_context);
John Snow559b9352016-10-27 12:06:55 -04003651 if (!elem->value) {
3652 g_free(elem);
3653 qapi_free_BlockJobInfoList(head);
3654 return NULL;
3655 }
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003656 *p_next = elem;
3657 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003658 }
3659
3660 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003661}
Paolo Bonzini4d454572012-11-26 16:03:42 +01003662
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00003663void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
Stefan Hajnoczi882e9b82017-12-07 20:13:17 +00003664 bool has_force, bool force, Error **errp)
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00003665{
3666 AioContext *old_context;
3667 AioContext *new_context;
3668 BlockDriverState *bs;
3669
3670 bs = bdrv_find_node(node_name);
3671 if (!bs) {
3672 error_setg(errp, "Cannot find node %s", node_name);
3673 return;
3674 }
3675
Stefan Hajnoczi882e9b82017-12-07 20:13:17 +00003676 /* Protects against accidents. */
3677 if (!(has_force && force) && bdrv_has_blk(bs)) {
3678 error_setg(errp, "Node %s is associated with a BlockBackend and could "
3679 "be in use (use force=true to override this check)",
3680 node_name);
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00003681 return;
3682 }
3683
3684 if (iothread->type == QTYPE_QSTRING) {
3685 IOThread *obj = iothread_by_id(iothread->u.s);
3686 if (!obj) {
3687 error_setg(errp, "Cannot find iothread %s", iothread->u.s);
3688 return;
3689 }
3690
3691 new_context = iothread_get_aio_context(obj);
3692 } else {
3693 new_context = qemu_get_aio_context();
3694 }
3695
3696 old_context = bdrv_get_aio_context(bs);
3697 aio_context_acquire(old_context);
3698
Kevin Wolf8ed7d422019-04-26 16:12:27 +02003699 bdrv_try_set_aio_context(bs, new_context, errp);
Stefan Hajnoczica00bbb2017-12-06 14:45:49 +00003700
3701 aio_context_release(old_context);
3702}
3703
Kevin Wolf00063832013-03-15 10:35:07 +01003704QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01003705 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01003706 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01003707 .desc = {
3708 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01003709 .name = "snapshot",
3710 .type = QEMU_OPT_BOOL,
3711 .help = "enable/disable snapshot mode",
3712 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003713 .name = "aio",
3714 .type = QEMU_OPT_STRING,
Aarushi Mehtaf80f2672020-01-20 14:18:50 +00003715 .help = "host AIO implementation (threads, native, io_uring)",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003716 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01003717 .name = BDRV_OPT_CACHE_WB,
3718 .type = QEMU_OPT_BOOL,
3719 .help = "Enable writeback mode",
3720 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003721 .name = "format",
3722 .type = QEMU_OPT_STRING,
3723 .help = "disk format (raw, qcow2, ...)",
3724 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003725 .name = "rerror",
3726 .type = QEMU_OPT_STRING,
3727 .help = "read error action",
3728 },{
3729 .name = "werror",
3730 .type = QEMU_OPT_STRING,
3731 .help = "write error action",
3732 },{
Alberto Garcia4e200cf2016-09-15 17:53:05 +03003733 .name = BDRV_OPT_READ_ONLY,
Paolo Bonzini4d454572012-11-26 16:03:42 +01003734 .type = QEMU_OPT_BOOL,
3735 .help = "open drive file as read-only",
Pradeep Jagadeesha2a78622017-02-28 10:31:46 +01003736 },
3737
3738 THROTTLE_OPTS,
3739
3740 {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02003741 .name = "throttling.group",
3742 .type = QEMU_OPT_STRING,
3743 .help = "name of the block throttling group",
3744 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01003745 .name = "copy-on-read",
3746 .type = QEMU_OPT_BOOL,
3747 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02003748 },{
3749 .name = "detect-zeroes",
3750 .type = QEMU_OPT_STRING,
3751 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02003752 },{
3753 .name = "stats-account-invalid",
3754 .type = QEMU_OPT_BOOL,
3755 .help = "whether to account for invalid I/O operations "
3756 "in the statistics",
3757 },{
3758 .name = "stats-account-failed",
3759 .type = QEMU_OPT_BOOL,
3760 .help = "whether to account for failed I/O operations "
3761 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01003762 },
3763 { /* end of list */ }
3764 },
3765};
Kevin Wolf00063832013-03-15 10:35:07 +01003766
3767QemuOptsList qemu_drive_opts = {
3768 .name = "drive",
3769 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3770 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02003771 /*
3772 * no elements => accept any params
3773 * validation will happen later
3774 */
Kevin Wolf00063832013-03-15 10:35:07 +01003775 { /* end of list */ }
3776 },
3777};