1666daa68SMarkus Armbruster /*
2666daa68SMarkus Armbruster * QEMU host block devices
3666daa68SMarkus Armbruster *
4666daa68SMarkus Armbruster * Copyright (c) 2003-2008 Fabrice Bellard
5666daa68SMarkus Armbruster *
6666daa68SMarkus Armbruster * This work is licensed under the terms of the GNU GPL, version 2 or
7666daa68SMarkus Armbruster * later. See the COPYING file in the top-level directory.
83618a094SMarkus Armbruster *
93618a094SMarkus Armbruster * This file incorporates work covered by the following copyright and
103618a094SMarkus Armbruster * permission notice:
113618a094SMarkus Armbruster *
123618a094SMarkus Armbruster * Copyright (c) 2003-2008 Fabrice Bellard
133618a094SMarkus Armbruster *
143618a094SMarkus Armbruster * Permission is hereby granted, free of charge, to any person obtaining a copy
153618a094SMarkus Armbruster * of this software and associated documentation files (the "Software"), to deal
163618a094SMarkus Armbruster * in the Software without restriction, including without limitation the rights
173618a094SMarkus Armbruster * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
183618a094SMarkus Armbruster * copies of the Software, and to permit persons to whom the Software is
193618a094SMarkus Armbruster * furnished to do so, subject to the following conditions:
203618a094SMarkus Armbruster *
213618a094SMarkus Armbruster * The above copyright notice and this permission notice shall be included in
223618a094SMarkus Armbruster * all copies or substantial portions of the Software.
233618a094SMarkus Armbruster *
243618a094SMarkus Armbruster * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
253618a094SMarkus Armbruster * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
263618a094SMarkus Armbruster * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
273618a094SMarkus Armbruster * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
283618a094SMarkus Armbruster * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
293618a094SMarkus Armbruster * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
303618a094SMarkus Armbruster * THE SOFTWARE.
31666daa68SMarkus Armbruster */
32666daa68SMarkus Armbruster
33d38ea87aSPeter Maydell #include "qemu/osdep.h"
3426f54e9aSMarkus Armbruster #include "sysemu/block-backend.h"
359c17d615SPaolo Bonzini #include "sysemu/blockdev.h"
360d09e41aSPaolo Bonzini #include "hw/block/block.h"
37737e150eSPaolo Bonzini #include "block/blockjob.h"
38e2c1c34fSMarkus Armbruster #include "block/dirty-bitmap.h"
39609f45eaSMax Reitz #include "block/qdict.h"
4076f4afb4SAlberto Garcia #include "block/throttle-groups.h"
4183c9089eSPaolo Bonzini #include "monitor/monitor.h"
42d49b6836SMarkus Armbruster #include "qemu/error-report.h"
431de7afc9SPaolo Bonzini #include "qemu/option.h"
44cdcd4361SMarkus Armbruster #include "qemu/qemu-print.h"
451de7afc9SPaolo Bonzini #include "qemu/config-file.h"
469af23989SMarkus Armbruster #include "qapi/qapi-commands-block.h"
479af23989SMarkus Armbruster #include "qapi/qapi-commands-transaction.h"
489af23989SMarkus Armbruster #include "qapi/qapi-visit-block-core.h"
49452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
5015280c36SMarkus Armbruster #include "qapi/qmp/qnum.h"
516b673957SMarkus Armbruster #include "qapi/qmp/qstring.h"
52e688df6bSMarkus Armbruster #include "qapi/error.h"
53cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h"
5447e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h"
55b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h"
569c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
57ca00bbb1SStefan Hajnoczi #include "sysemu/iothread.h"
58737e150eSPaolo Bonzini #include "block/block_int.h"
590ab8ed18SDaniel P. Berrange #include "block/trace.h"
6054d31236SMarkus Armbruster #include "sysemu/runstate.h"
61b39847a5SPavel Dovgalyuk #include "sysemu/replay.h"
62f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
63f348b6d1SVeronia Bahaa #include "qemu/help_option.h"
64db725815SMarkus Armbruster #include "qemu/main-loop.h"
65a2a7862cSPradeep Jagadeesh #include "qemu/throttle-options.h"
66666daa68SMarkus Armbruster
67ebc2752bSEmanuele Giuseppe Esposito /* Protected by BQL */
6889802d5aSMaxim Levitsky QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
699c4218e9SMax Reitz QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
709c4218e9SMax Reitz
bdrv_set_monitor_owned(BlockDriverState * bs)7189802d5aSMaxim Levitsky void bdrv_set_monitor_owned(BlockDriverState *bs)
7289802d5aSMaxim Levitsky {
73b4ad82aaSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
7489802d5aSMaxim Levitsky QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
7589802d5aSMaxim Levitsky }
7689802d5aSMaxim Levitsky
771960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = {
781960966dSMarkus Armbruster [IF_NONE] = "none",
791960966dSMarkus Armbruster [IF_IDE] = "ide",
801960966dSMarkus Armbruster [IF_SCSI] = "scsi",
811960966dSMarkus Armbruster [IF_FLOPPY] = "floppy",
821960966dSMarkus Armbruster [IF_PFLASH] = "pflash",
831960966dSMarkus Armbruster [IF_MTD] = "mtd",
841960966dSMarkus Armbruster [IF_SD] = "sd",
851960966dSMarkus Armbruster [IF_VIRTIO] = "virtio",
861960966dSMarkus Armbruster [IF_XEN] = "xen",
871960966dSMarkus Armbruster };
881960966dSMarkus Armbruster
8921dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = {
9027d6bf40SMarkus Armbruster /*
9127d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option
9227d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI.
9327d6bf40SMarkus Armbruster *
94547cb157SWei Jiangang * All controllers used to implement if=T drives need to support
9527d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
9627d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit
9727d6bf40SMarkus Armbruster * values.
9827d6bf40SMarkus Armbruster *
9927d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive
10027d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but
10127d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max),
10227d6bf40SMarkus Armbruster * the drive can't be set up. Regression.
10327d6bf40SMarkus Armbruster */
10427d6bf40SMarkus Armbruster [IF_IDE] = 2,
10527d6bf40SMarkus Armbruster [IF_SCSI] = 7,
1061960966dSMarkus Armbruster };
1071960966dSMarkus Armbruster
10821dff8cfSJohn Snow /**
10921dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides
11021dff8cfSJohn Snow * of the default, global values.
11121dff8cfSJohn Snow */
override_max_devs(BlockInterfaceType type,int max_devs)11221dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs)
11321dff8cfSJohn Snow {
11418e46a03SMarkus Armbruster BlockBackend *blk;
11521dff8cfSJohn Snow DriveInfo *dinfo;
11621dff8cfSJohn Snow
117c5be7445SEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
118c5be7445SEmanuele Giuseppe Esposito
11921dff8cfSJohn Snow if (max_devs <= 0) {
12021dff8cfSJohn Snow return;
12121dff8cfSJohn Snow }
12221dff8cfSJohn Snow
12318e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
12418e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk);
12521dff8cfSJohn Snow if (dinfo->type == type) {
12621dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of"
12721dff8cfSJohn Snow " the %s interface, because a drive of that type has"
12821dff8cfSJohn Snow " already been added.\n", if_name[type]);
12921dff8cfSJohn Snow g_assert_not_reached();
13021dff8cfSJohn Snow }
13121dff8cfSJohn Snow }
13221dff8cfSJohn Snow
13321dff8cfSJohn Snow if_max_devs[type] = max_devs;
13421dff8cfSJohn Snow }
13521dff8cfSJohn Snow
13614bafc54SMarkus Armbruster /*
13714bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets
13814bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it.
13914bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the
14014bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del()
14114bafc54SMarkus Armbruster * when deletion is actually safe.
14214bafc54SMarkus Armbruster */
blockdev_mark_auto_del(BlockBackend * blk)1434be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk)
14414bafc54SMarkus Armbruster {
14518e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk);
1468164102fSVladimir Sementsov-Ogievskiy BlockJob *job;
14714bafc54SMarkus Armbruster
148c5be7445SEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
149c5be7445SEmanuele Giuseppe Esposito
15026f8b3a8SMarkus Armbruster if (!dinfo) {
1512d246f01SKevin Wolf return;
1522d246f01SKevin Wolf }
1532d246f01SKevin Wolf
154880eeec6SEmanuele Giuseppe Esposito JOB_LOCK_GUARD();
155880eeec6SEmanuele Giuseppe Esposito
156e2626874SKevin Wolf do {
157e2626874SKevin Wolf job = block_job_next_locked(NULL);
158e2626874SKevin Wolf while (job && (job->job.cancelled ||
159e2626874SKevin Wolf job->job.deferred_to_main_loop ||
160e2626874SKevin Wolf !block_job_has_bdrv(job, blk_bs(blk))))
161e2626874SKevin Wolf {
162e2626874SKevin Wolf job = block_job_next_locked(job);
163e2626874SKevin Wolf }
164e2626874SKevin Wolf if (job) {
165e2626874SKevin Wolf /*
166e2626874SKevin Wolf * This drops the job lock temporarily and polls, so we need to
167e2626874SKevin Wolf * restart processing the list from the start after this.
168e2626874SKevin Wolf */
169880eeec6SEmanuele Giuseppe Esposito job_cancel_locked(&job->job, false);
1705433c24fSMax Reitz }
171e2626874SKevin Wolf } while (job);
17291fddb0dSStefan Hajnoczi
17314bafc54SMarkus Armbruster dinfo->auto_del = 1;
17414bafc54SMarkus Armbruster }
17514bafc54SMarkus Armbruster
blockdev_auto_del(BlockBackend * blk)1764be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk)
17714bafc54SMarkus Armbruster {
17818e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk);
179c5be7445SEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
18014bafc54SMarkus Armbruster
1810fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) {
182efaa7c4eSMax Reitz monitor_remove_blk(blk);
183b9fe8a7aSMarkus Armbruster blk_unref(blk);
18414bafc54SMarkus Armbruster }
18514bafc54SMarkus Armbruster }
18614bafc54SMarkus Armbruster
drive_index_to_bus_id(BlockInterfaceType type,int index)187505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index)
188505a7fb1SMarkus Armbruster {
189505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type];
190505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0;
191505a7fb1SMarkus Armbruster }
192505a7fb1SMarkus Armbruster
drive_index_to_unit_id(BlockInterfaceType type,int index)193505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index)
194505a7fb1SMarkus Armbruster {
195505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type];
196505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index;
197505a7fb1SMarkus Armbruster }
198505a7fb1SMarkus Armbruster
drive_add(BlockInterfaceType type,int index,const char * file,const char * optstr)1992292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
2005645b0f4SMarkus Armbruster const char *optstr)
201666daa68SMarkus Armbruster {
202666daa68SMarkus Armbruster QemuOpts *opts;
203666daa68SMarkus Armbruster
204c5be7445SEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
205c5be7445SEmanuele Giuseppe Esposito
206cc67f28eSEmanuele Giuseppe Esposito opts = qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
207666daa68SMarkus Armbruster if (!opts) {
208666daa68SMarkus Armbruster return NULL;
209666daa68SMarkus Armbruster }
2102292ddaeSMarkus Armbruster if (type != IF_DEFAULT) {
211f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort);
2122292ddaeSMarkus Armbruster }
2132292ddaeSMarkus Armbruster if (index >= 0) {
214a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort);
2152292ddaeSMarkus Armbruster }
216666daa68SMarkus Armbruster if (file)
217f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort);
218666daa68SMarkus Armbruster return opts;
219666daa68SMarkus Armbruster }
220666daa68SMarkus Armbruster
drive_get(BlockInterfaceType type,int bus,int unit)221666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
222666daa68SMarkus Armbruster {
22318e46a03SMarkus Armbruster BlockBackend *blk;
224666daa68SMarkus Armbruster DriveInfo *dinfo;
225666daa68SMarkus Armbruster
226c5be7445SEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
227c5be7445SEmanuele Giuseppe Esposito
22818e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
22918e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk);
23018e46a03SMarkus Armbruster if (dinfo && dinfo->type == type
23118e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) {
232666daa68SMarkus Armbruster return dinfo;
233666daa68SMarkus Armbruster }
23418e46a03SMarkus Armbruster }
235666daa68SMarkus Armbruster
236666daa68SMarkus Armbruster return NULL;
237666daa68SMarkus Armbruster }
238666daa68SMarkus Armbruster
239fe9f70a1SMarkus Armbruster /*
240fe9f70a1SMarkus Armbruster * Check board claimed all -drive that are meant to be claimed.
241fe9f70a1SMarkus Armbruster * Fatal error if any remain unclaimed.
242fe9f70a1SMarkus Armbruster */
drive_check_orphaned(void)243720b8dc0SMarkus Armbruster void drive_check_orphaned(void)
244a66c9dc7SJohn Snow {
24518e46a03SMarkus Armbruster BlockBackend *blk;
246a66c9dc7SJohn Snow DriveInfo *dinfo;
247664cc623SMarkus Armbruster Location loc;
248720b8dc0SMarkus Armbruster bool orphans = false;
249a66c9dc7SJohn Snow
250c5be7445SEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
251c5be7445SEmanuele Giuseppe Esposito
25218e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
25318e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk);
254fe9f70a1SMarkus Armbruster /*
255fe9f70a1SMarkus Armbruster * Ignore default drives, because we create certain default
256fe9f70a1SMarkus Armbruster * drives unconditionally, then leave them unclaimed. Not the
257fe9f70a1SMarkus Armbruster * users fault.
258d3256f88SDavid Woodhouse * Ignore IF_VIRTIO or IF_XEN, because it gets desugared into
259d3256f88SDavid Woodhouse * -device, so we can leave failing to -device.
260fe9f70a1SMarkus Armbruster * Ignore IF_NONE, because leaving unclaimed IF_NONE remains
261fe9f70a1SMarkus Armbruster * available for device_add is a feature.
262fe9f70a1SMarkus Armbruster */
263fe9f70a1SMarkus Armbruster if (dinfo->is_default || dinfo->type == IF_VIRTIO
264d3256f88SDavid Woodhouse || dinfo->type == IF_XEN || dinfo->type == IF_NONE) {
265a1b40bdaSMarkus Armbruster continue;
266a1b40bdaSMarkus Armbruster }
267a1b40bdaSMarkus Armbruster if (!blk_get_attached_dev(blk)) {
268664cc623SMarkus Armbruster loc_push_none(&loc);
269664cc623SMarkus Armbruster qemu_opts_loc_restore(dinfo->opts);
270720b8dc0SMarkus Armbruster error_report("machine type does not support"
271664cc623SMarkus Armbruster " if=%s,bus=%d,unit=%d",
2725433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit);
273664cc623SMarkus Armbruster loc_pop(&loc);
274720b8dc0SMarkus Armbruster orphans = true;
275a66c9dc7SJohn Snow }
276a66c9dc7SJohn Snow }
277a66c9dc7SJohn Snow
278720b8dc0SMarkus Armbruster if (orphans) {
279720b8dc0SMarkus Armbruster exit(1);
280720b8dc0SMarkus Armbruster }
281a66c9dc7SJohn Snow }
282a66c9dc7SJohn Snow
drive_get_by_index(BlockInterfaceType type,int index)283f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
284f1bd51acSMarkus Armbruster {
285c5be7445SEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
286f1bd51acSMarkus Armbruster return drive_get(type,
287f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index),
288f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index));
289f1bd51acSMarkus Armbruster }
290f1bd51acSMarkus Armbruster
drive_get_max_bus(BlockInterfaceType type)291666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type)
292666daa68SMarkus Armbruster {
293666daa68SMarkus Armbruster int max_bus;
29418e46a03SMarkus Armbruster BlockBackend *blk;
295666daa68SMarkus Armbruster DriveInfo *dinfo;
296666daa68SMarkus Armbruster
297c5be7445SEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
298c5be7445SEmanuele Giuseppe Esposito
299666daa68SMarkus Armbruster max_bus = -1;
30018e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
30118e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk);
30218e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
303666daa68SMarkus Armbruster max_bus = dinfo->bus;
304666daa68SMarkus Armbruster }
30518e46a03SMarkus Armbruster }
306666daa68SMarkus Armbruster return max_bus;
307666daa68SMarkus Armbruster }
308666daa68SMarkus Armbruster
bdrv_format_print(void * opaque,const char * name)309666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name)
310666daa68SMarkus Armbruster {
311cdcd4361SMarkus Armbruster qemu_printf(" %s", name);
312666daa68SMarkus Armbruster }
313666daa68SMarkus Armbruster
314aa398a5cSStefan Hajnoczi typedef struct {
315aa398a5cSStefan Hajnoczi QEMUBH *bh;
316fa510ebfSFam Zheng BlockDriverState *bs;
317fa510ebfSFam Zheng } BDRVPutRefBH;
318aa398a5cSStefan Hajnoczi
parse_block_error_action(const char * buf,bool is_read,Error ** errp)319b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
320666daa68SMarkus Armbruster {
321666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) {
32292aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE;
323666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) {
32492aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC;
325666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) {
32692aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP;
327666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) {
32892aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT;
329666daa68SMarkus Armbruster } else {
330b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action",
331666daa68SMarkus Armbruster buf, is_read ? "read" : "write");
332666daa68SMarkus Armbruster return -1;
333666daa68SMarkus Armbruster }
334666daa68SMarkus Armbruster }
335666daa68SMarkus Armbruster
parse_stats_intervals(BlockAcctStats * stats,QList * intervals,Error ** errp)33640119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
33740119effSAlberto Garcia Error **errp)
33840119effSAlberto Garcia {
33940119effSAlberto Garcia const QListEntry *entry;
34040119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
34140119effSAlberto Garcia switch (qobject_type(entry->value)) {
34240119effSAlberto Garcia
34340119effSAlberto Garcia case QTYPE_QSTRING: {
344bd1386ccSEric Blake uint64_t length;
3457dc847ebSMax Reitz const char *str = qstring_get_str(qobject_to(QString,
3467dc847ebSMax Reitz entry->value));
347bd1386ccSEric Blake if (parse_uint_full(str, 10, &length) == 0 &&
34840119effSAlberto Garcia length > 0 && length <= UINT_MAX) {
34940119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length);
35040119effSAlberto Garcia } else {
35140119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str);
35240119effSAlberto Garcia return false;
35340119effSAlberto Garcia }
35440119effSAlberto Garcia break;
35540119effSAlberto Garcia }
35640119effSAlberto Garcia
35701b2ffceSMarc-André Lureau case QTYPE_QNUM: {
3587dc847ebSMax Reitz int64_t length = qnum_get_int(qobject_to(QNum, entry->value));
35901b2ffceSMarc-André Lureau
36040119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) {
36140119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length);
36240119effSAlberto Garcia } else {
36340119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length);
36440119effSAlberto Garcia return false;
36540119effSAlberto Garcia }
36640119effSAlberto Garcia break;
36740119effSAlberto Garcia }
36840119effSAlberto Garcia
36940119effSAlberto Garcia default:
37040119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid");
37140119effSAlberto Garcia return false;
37240119effSAlberto Garcia }
37340119effSAlberto Garcia }
37440119effSAlberto Garcia return true;
37540119effSAlberto Garcia }
37640119effSAlberto Garcia
37733cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
37833cb7dc8SKevin Wolf
379fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */
extract_common_blockdev_options(QemuOpts * opts,int * bdrv_flags,const char ** throttling_group,ThrottleConfig * throttle_cfg,BlockdevDetectZeroesOptions * detect_zeroes,Error ** errp)380fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
381fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg,
382fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
383fbf8175eSMax Reitz {
384fbf8175eSMax Reitz Error *local_error = NULL;
385fbf8175eSMax Reitz const char *aio;
386fbf8175eSMax Reitz
387fbf8175eSMax Reitz if (bdrv_flags) {
388fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
389fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ;
390fbf8175eSMax Reitz }
391fbf8175eSMax Reitz
392fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
393f80f2673SAarushi Mehta if (bdrv_parse_aio(aio, bdrv_flags) < 0) {
394fbf8175eSMax Reitz error_setg(errp, "invalid aio option");
395fbf8175eSMax Reitz return;
396fbf8175eSMax Reitz }
397fbf8175eSMax Reitz }
398fbf8175eSMax Reitz }
399fbf8175eSMax Reitz
400fbf8175eSMax Reitz /* disk I/O throttling */
401fbf8175eSMax Reitz if (throttling_group) {
402fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group");
403fbf8175eSMax Reitz }
404fbf8175eSMax Reitz
405fbf8175eSMax Reitz if (throttle_cfg) {
4061588ab5dSAlberto Garcia throttle_config_init(throttle_cfg);
407fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
408fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0);
409fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
410fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0);
411fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
412fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0);
413fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
414fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0);
415fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
416fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0);
417fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
418fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0);
419fbf8175eSMax Reitz
420fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
421fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
422fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max =
423fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
424fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
425fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
426fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
427fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
428fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max =
429fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
430fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
431fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
432fbf8175eSMax Reitz
4338a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
4348a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
4358a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
4368a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
4378a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
4388a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
4398a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
4408a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
4418a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
4428a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
4438a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
4448a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
4458a0fc18dSAlberto Garcia
446fbf8175eSMax Reitz throttle_cfg->op_size =
447fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0);
448fbf8175eSMax Reitz
449d5851089SAlberto Garcia if (!throttle_is_valid(throttle_cfg, errp)) {
450fbf8175eSMax Reitz return;
451fbf8175eSMax Reitz }
452fbf8175eSMax Reitz }
453fbf8175eSMax Reitz
454fbf8175eSMax Reitz if (detect_zeroes) {
455fbf8175eSMax Reitz *detect_zeroes =
456f7abe0ecSMarc-André Lureau qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
457fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"),
458fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
459fbf8175eSMax Reitz &local_error);
460fbf8175eSMax Reitz if (local_error) {
461fbf8175eSMax Reitz error_propagate(errp, local_error);
462fbf8175eSMax Reitz return;
463fbf8175eSMax Reitz }
464fbf8175eSMax Reitz }
465fbf8175eSMax Reitz }
466fbf8175eSMax Reitz
account_get_opt(QemuOpts * opts,const char * name)467b2aaf354SDenis V. Lunev static OnOffAuto account_get_opt(QemuOpts *opts, const char *name)
468b2aaf354SDenis V. Lunev {
469b2aaf354SDenis V. Lunev if (!qemu_opt_find(opts, name)) {
470b2aaf354SDenis V. Lunev return ON_OFF_AUTO_AUTO;
471b2aaf354SDenis V. Lunev }
472b2aaf354SDenis V. Lunev if (qemu_opt_get_bool(opts, name, true)) {
473b2aaf354SDenis V. Lunev return ON_OFF_AUTO_ON;
474b2aaf354SDenis V. Lunev }
475b2aaf354SDenis V. Lunev return ON_OFF_AUTO_OFF;
476b2aaf354SDenis V. Lunev }
477b2aaf354SDenis V. Lunev
478f298d071SKevin Wolf /* Takes the ownership of bs_opts */
blockdev_init(const char * file,QDict * bs_opts,Error ** errp)47918e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
480b681072dSKevin Wolf Error **errp)
481666daa68SMarkus Armbruster {
482666daa68SMarkus Armbruster const char *buf;
483666daa68SMarkus Armbruster int bdrv_flags = 0;
484666daa68SMarkus Armbruster int on_read_error, on_write_error;
485b2aaf354SDenis V. Lunev OnOffAuto account_invalid, account_failed;
486f87a0e29SAlberto Garcia bool writethrough, read_only;
48726f54e9aSMarkus Armbruster BlockBackend *blk;
488a0f1eab1SMarkus Armbruster BlockDriverState *bs;
489cc0681c4SBenoît Canet ThrottleConfig cfg;
490666daa68SMarkus Armbruster int snapshot = 0;
491c546194fSStefan Hajnoczi Error *error = NULL;
4920006383eSKevin Wolf QemuOpts *opts;
49340119effSAlberto Garcia QDict *interval_dict = NULL;
49440119effSAlberto Garcia QList *interval_list = NULL;
4950006383eSKevin Wolf const char *id;
496fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes =
497fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
498fbf8175eSMax Reitz const char *throttling_group = NULL;
499666daa68SMarkus Armbruster
500f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options
501f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */
502f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id");
503c6ecec43SMarkus Armbruster opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, errp);
504c6ecec43SMarkus Armbruster if (!opts) {
5056376f952SMarkus Armbruster goto err_no_opts;
5060006383eSKevin Wolf }
5070006383eSKevin Wolf
508668f62ecSMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, bs_opts, errp)) {
509ec9c10d2SStefan Hajnoczi goto early_err;
5100006383eSKevin Wolf }
5110006383eSKevin Wolf
5120006383eSKevin Wolf if (id) {
5130006383eSKevin Wolf qdict_del(bs_opts, "id");
5140006383eSKevin Wolf }
5150006383eSKevin Wolf
516666daa68SMarkus Armbruster /* extract parameters */
517666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
518666daa68SMarkus Armbruster
519b2aaf354SDenis V. Lunev account_invalid = account_get_opt(opts, "stats-account-invalid");
520b2aaf354SDenis V. Lunev account_failed = account_get_opt(opts, "stats-account-failed");
521362e9299SAlberto Garcia
522e4b24b49SKevin Wolf writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
523e4b24b49SKevin Wolf
524ff356ee4SAlberto Garcia id = qemu_opts_id(opts);
525ff356ee4SAlberto Garcia
52640119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
52740119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list);
52840119effSAlberto Garcia
52940119effSAlberto Garcia if (qdict_size(interval_dict) != 0) {
53040119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s",
53140119effSAlberto Garcia qdict_first(interval_dict)->key);
53240119effSAlberto Garcia goto early_err;
53340119effSAlberto Garcia }
5342be5506fSAlberto Garcia
535fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
536fbf8175eSMax Reitz &detect_zeroes, &error);
537fbf8175eSMax Reitz if (error) {
538fbf8175eSMax Reitz error_propagate(errp, error);
539ec9c10d2SStefan Hajnoczi goto early_err;
540a9384affSPaolo Bonzini }
541666daa68SMarkus Armbruster
542666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) {
543c8057f95SPeter Maydell if (is_help_option(buf)) {
544cdcd4361SMarkus Armbruster qemu_printf("Supported formats:");
5459ac404c5SAndrey Shinkevich bdrv_iterate_format(bdrv_format_print, NULL, false);
546cdcd4361SMarkus Armbruster qemu_printf("\nSupported formats (read-only):");
5479ac404c5SAndrey Shinkevich bdrv_iterate_format(bdrv_format_print, NULL, true);
548cdcd4361SMarkus Armbruster qemu_printf("\n");
549ec9c10d2SStefan Hajnoczi goto early_err;
550666daa68SMarkus Armbruster }
55174fe54f2SKevin Wolf
552e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) {
553e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'");
554ec9c10d2SStefan Hajnoczi goto early_err;
5556db5f5d6SMike Qiu }
55646f5ac20SEric Blake qdict_put_str(bs_opts, "driver", buf);
557666daa68SMarkus Armbruster }
558666daa68SMarkus Armbruster
55992aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
560666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
561b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error);
56284d18f06SMarkus Armbruster if (error) {
563b681072dSKevin Wolf error_propagate(errp, error);
564ec9c10d2SStefan Hajnoczi goto early_err;
565666daa68SMarkus Armbruster }
566666daa68SMarkus Armbruster }
567666daa68SMarkus Armbruster
56892aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT;
569666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
570b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error);
57184d18f06SMarkus Armbruster if (error) {
572b681072dSKevin Wolf error_propagate(errp, error);
573ec9c10d2SStefan Hajnoczi goto early_err;
574666daa68SMarkus Armbruster }
575666daa68SMarkus Armbruster }
576666daa68SMarkus Armbruster
577666daa68SMarkus Armbruster if (snapshot) {
57891a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT;
579666daa68SMarkus Armbruster }
580666daa68SMarkus Armbruster
581f87a0e29SAlberto Garcia read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
582f87a0e29SAlberto Garcia
5835ec18f8cSMax Reitz /* init */
58439c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) {
5855ec18f8cSMax Reitz BlockBackendRootState *blk_rs;
5865ec18f8cSMax Reitz
587d861ab3aSKevin Wolf blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
5885ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk);
589260242a8SVladimir Sementsov-Ogievskiy blk_rs->open_flags = bdrv_flags | (read_only ? 0 : BDRV_O_RDWR);
5905ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes;
5915ec18f8cSMax Reitz
592cb3e7f08SMarc-André Lureau qobject_unref(bs_opts);
5935ec18f8cSMax Reitz } else {
5945ec18f8cSMax Reitz if (file && !*file) {
5955ec18f8cSMax Reitz file = NULL;
5965ec18f8cSMax Reitz }
5975ec18f8cSMax Reitz
59891a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
59991a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults.
60091a097e7SKevin Wolf * Apply the defaults here instead. */
60191a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
60291a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
603f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
604f87a0e29SAlberto Garcia read_only ? "on" : "off");
6059384a444SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on");
60672e775c7SKevin Wolf assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
60791a097e7SKevin Wolf
60812d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) {
60912d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE;
61012d5ee3aSKevin Wolf }
61112d5ee3aSKevin Wolf
612efaa7c4eSMax Reitz blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
613e4342ce5SMax Reitz if (!blk) {
614e4342ce5SMax Reitz goto err_no_bs_opts;
615e4342ce5SMax Reitz }
616e4342ce5SMax Reitz bs = blk_bs(blk);
6170006383eSKevin Wolf
618e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes;
619e4342ce5SMax Reitz
6209caa6f3dSPaolo Bonzini block_acct_setup(blk_get_stats(blk), account_invalid, account_failed);
6212be5506fSAlberto Garcia
62240119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
6232be5506fSAlberto Garcia blk_unref(blk);
6242be5506fSAlberto Garcia blk = NULL;
6252be5506fSAlberto Garcia goto err_no_bs_opts;
6262be5506fSAlberto Garcia }
6272be5506fSAlberto Garcia }
6285ec18f8cSMax Reitz
6297ca7f0f6SKevin Wolf /* disk I/O throttling */
6307ca7f0f6SKevin Wolf if (throttle_enabled(&cfg)) {
6317ca7f0f6SKevin Wolf if (!throttling_group) {
632ff356ee4SAlberto Garcia throttling_group = id;
6337ca7f0f6SKevin Wolf }
6347ca7f0f6SKevin Wolf blk_io_limits_enable(blk, throttling_group);
6357ca7f0f6SKevin Wolf blk_set_io_limits(blk, &cfg);
6367ca7f0f6SKevin Wolf }
6377ca7f0f6SKevin Wolf
638e4b24b49SKevin Wolf blk_set_enable_write_cache(blk, !writethrough);
6395ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error);
6400006383eSKevin Wolf
641ff356ee4SAlberto Garcia if (!monitor_add_blk(blk, id, errp)) {
642efaa7c4eSMax Reitz blk_unref(blk);
643efaa7c4eSMax Reitz blk = NULL;
644efaa7c4eSMax Reitz goto err_no_bs_opts;
645efaa7c4eSMax Reitz }
646efaa7c4eSMax Reitz
647e4342ce5SMax Reitz err_no_bs_opts:
6480006383eSKevin Wolf qemu_opts_del(opts);
649cb3e7f08SMarc-André Lureau qobject_unref(interval_dict);
650cb3e7f08SMarc-André Lureau qobject_unref(interval_list);
65118e46a03SMarkus Armbruster return blk;
652a9ae2bffSMarkus Armbruster
653ec9c10d2SStefan Hajnoczi early_err:
654ec9c10d2SStefan Hajnoczi qemu_opts_del(opts);
655cb3e7f08SMarc-André Lureau qobject_unref(interval_dict);
656cb3e7f08SMarc-André Lureau qobject_unref(interval_list);
6576376f952SMarkus Armbruster err_no_opts:
658cb3e7f08SMarc-André Lureau qobject_unref(bs_opts);
659a9ae2bffSMarkus Armbruster return NULL;
660666daa68SMarkus Armbruster }
661666daa68SMarkus Armbruster
662bd745e23SMax Reitz /* Takes the ownership of bs_opts */
bds_tree_init(QDict * bs_opts,Error ** errp)66389802d5aSMaxim Levitsky BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
664bd745e23SMax Reitz {
665bd745e23SMax Reitz int bdrv_flags = 0;
666bd745e23SMax Reitz
667b4ad82aaSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
668a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
669a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults.
670a81d6164SKevin Wolf * Apply the defaults here instead. */
671a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
672a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
673f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
674a81d6164SKevin Wolf
67512d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) {
67612d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE;
67712d5ee3aSKevin Wolf }
67812d5ee3aSKevin Wolf
679b49f4755SStefan Hajnoczi return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
680bd745e23SMax Reitz }
681bd745e23SMax Reitz
blockdev_close_all_bdrv_states(void)6829c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void)
6839c4218e9SMax Reitz {
6849c4218e9SMax Reitz BlockDriverState *bs, *next_bs;
6859c4218e9SMax Reitz
686b4ad82aaSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
6879c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
6889c4218e9SMax Reitz bdrv_unref(bs);
6899c4218e9SMax Reitz }
6909c4218e9SMax Reitz }
6919c4218e9SMax Reitz
692262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */
bdrv_next_monitor_owned(BlockDriverState * bs)693262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
694262b4e8fSMax Reitz {
695f791bf7fSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
696262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list)
697262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states);
698262b4e8fSMax Reitz }
699262b4e8fSMax Reitz
qemu_opt_rename(QemuOpts * opts,const char * from,const char * to,Error ** errp)700c75d7f71SMarkus Armbruster static bool qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
7015abbf0eeSKevin Wolf Error **errp)
70257975222SKevin Wolf {
70357975222SKevin Wolf const char *value;
70457975222SKevin Wolf
70557975222SKevin Wolf value = qemu_opt_get(opts, from);
70657975222SKevin Wolf if (value) {
7075abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) {
7085abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the "
7095abbf0eeSKevin Wolf "same time", to, from);
710c75d7f71SMarkus Armbruster return false;
7115abbf0eeSKevin Wolf }
71220d6cd47SJun Li }
71320d6cd47SJun Li
71420d6cd47SJun Li /* rename all items in opts */
71520d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) {
716f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort);
71757975222SKevin Wolf qemu_opt_unset(opts, from);
71857975222SKevin Wolf }
719c75d7f71SMarkus Armbruster return true;
72057975222SKevin Wolf }
72157975222SKevin Wolf
72233cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = {
72333cb7dc8SKevin Wolf .name = "drive",
72433cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
72533cb7dc8SKevin Wolf .desc = {
72633cb7dc8SKevin Wolf {
72787a899c5SKevin Wolf .name = "bus",
72887a899c5SKevin Wolf .type = QEMU_OPT_NUMBER,
72987a899c5SKevin Wolf .help = "bus number",
73087a899c5SKevin Wolf },{
73187a899c5SKevin Wolf .name = "unit",
73287a899c5SKevin Wolf .type = QEMU_OPT_NUMBER,
73387a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)",
73487a899c5SKevin Wolf },{
73587a899c5SKevin Wolf .name = "index",
73687a899c5SKevin Wolf .type = QEMU_OPT_NUMBER,
73787a899c5SKevin Wolf .help = "index number",
73887a899c5SKevin Wolf },{
73933cb7dc8SKevin Wolf .name = "media",
74033cb7dc8SKevin Wolf .type = QEMU_OPT_STRING,
74133cb7dc8SKevin Wolf .help = "media type (disk, cdrom)",
742593d464bSKevin Wolf },{
743593d464bSKevin Wolf .name = "if",
744593d464bSKevin Wolf .type = QEMU_OPT_STRING,
745593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
746b41a7338SKevin Wolf },{
747d095b465SMax Reitz .name = "file",
748d095b465SMax Reitz .type = QEMU_OPT_STRING,
749d095b465SMax Reitz .help = "file name",
75033cb7dc8SKevin Wolf },
7510ebd24e0SKevin Wolf
7520ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */
7530ebd24e0SKevin Wolf {
7544e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY,
7550ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL,
7560ebd24e0SKevin Wolf .help = "open drive file as read-only",
7570ebd24e0SKevin Wolf },{
758ee13ed1cSKevin Wolf .name = "rerror",
759ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING,
760ee13ed1cSKevin Wolf .help = "read error action",
761ee13ed1cSKevin Wolf },{
762ee13ed1cSKevin Wolf .name = "werror",
763ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING,
764ee13ed1cSKevin Wolf .help = "write error action",
765ee13ed1cSKevin Wolf },{
7660ebd24e0SKevin Wolf .name = "copy-on-read",
7670ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL,
7680ebd24e0SKevin Wolf .help = "copy read data from backing file into image file",
7690ebd24e0SKevin Wolf },
7700ebd24e0SKevin Wolf
77133cb7dc8SKevin Wolf { /* end of list */ }
77233cb7dc8SKevin Wolf },
77333cb7dc8SKevin Wolf };
77433cb7dc8SKevin Wolf
drive_new(QemuOpts * all_opts,BlockInterfaceType block_default_type,Error ** errp)775c4f26c9fSMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,
776c4f26c9fSMarkus Armbruster Error **errp)
77757975222SKevin Wolf {
77829c4e2b5SKevin Wolf const char *value;
77918e46a03SMarkus Armbruster BlockBackend *blk;
78033cb7dc8SKevin Wolf DriveInfo *dinfo = NULL;
781f298d071SKevin Wolf QDict *bs_opts;
78233cb7dc8SKevin Wolf QemuOpts *legacy_opts;
78333cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK;
784593d464bSKevin Wolf BlockInterfaceType type;
78587a899c5SKevin Wolf int max_devs, bus_id, unit_id, index;
786ee13ed1cSKevin Wolf const char *werror, *rerror;
787a7fdbcf0SFam Zheng bool read_only = false;
788a7fdbcf0SFam Zheng bool copy_on_read;
789d095b465SMax Reitz const char *filename;
790247147fbSKevin Wolf int i;
79129c4e2b5SKevin Wolf
792c5be7445SEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
793c5be7445SEmanuele Giuseppe Esposito
79457975222SKevin Wolf /* Change legacy command line options into QMP ones */
795247147fbSKevin Wolf static const struct {
796247147fbSKevin Wolf const char *from;
797247147fbSKevin Wolf const char *to;
798247147fbSKevin Wolf } opt_renames[] = {
799247147fbSKevin Wolf { "iops", "throttling.iops-total" },
800247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" },
801247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" },
80257975222SKevin Wolf
803247147fbSKevin Wolf { "bps", "throttling.bps-total" },
804247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" },
805247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" },
80657975222SKevin Wolf
807247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" },
808247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" },
809247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" },
8103e9fab69SBenoît Canet
811247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" },
812247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" },
813247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" },
8143e9fab69SBenoît Canet
815247147fbSKevin Wolf { "iops_size", "throttling.iops-size" },
8162024c1dfSBenoît Canet
81776f4afb4SAlberto Garcia { "group", "throttling.group" },
81876f4afb4SAlberto Garcia
8194e200cf8SAlberto Garcia { "readonly", BDRV_OPT_READ_ONLY },
820247147fbSKevin Wolf };
821247147fbSKevin Wolf
822247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
823235e59cfSMarkus Armbruster if (!qemu_opt_rename(all_opts, opt_renames[i].from,
824668f62ecSMarkus Armbruster opt_renames[i].to, errp)) {
8255abbf0eeSKevin Wolf return NULL;
8265abbf0eeSKevin Wolf }
827247147fbSKevin Wolf }
8280f227a94SKevin Wolf
82929c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache");
83029c4e2b5SKevin Wolf if (value) {
83129c4e2b5SKevin Wolf int flags = 0;
83204feb4a5SKevin Wolf bool writethrough;
83329c4e2b5SKevin Wolf
83404feb4a5SKevin Wolf if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
835c4f26c9fSMarkus Armbruster error_setg(errp, "invalid cache option");
83629c4e2b5SKevin Wolf return NULL;
83729c4e2b5SKevin Wolf }
83829c4e2b5SKevin Wolf
83929c4e2b5SKevin Wolf /* Specific options take precedence */
84054861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
84154861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
84204feb4a5SKevin Wolf !writethrough, &error_abort);
84329c4e2b5SKevin Wolf }
84454861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
84554861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
846cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort);
84729c4e2b5SKevin Wolf }
84854861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
84954861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
850cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort);
85129c4e2b5SKevin Wolf }
85229c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache");
85329c4e2b5SKevin Wolf }
85429c4e2b5SKevin Wolf
855f298d071SKevin Wolf /* Get a QDict for processing the options */
856f298d071SKevin Wolf bs_opts = qdict_new();
857f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts);
858f298d071SKevin Wolf
85987ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
86087ea75d5SPeter Crosthwaite &error_abort);
861af175e85SMarkus Armbruster if (!qemu_opts_absorb_qdict(legacy_opts, bs_opts, errp)) {
86233cb7dc8SKevin Wolf goto fail;
86333cb7dc8SKevin Wolf }
86433cb7dc8SKevin Wolf
86533cb7dc8SKevin Wolf /* Media type */
86633cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media");
86733cb7dc8SKevin Wolf if (value) {
86833cb7dc8SKevin Wolf if (!strcmp(value, "disk")) {
86933cb7dc8SKevin Wolf media = MEDIA_DISK;
87033cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) {
87133cb7dc8SKevin Wolf media = MEDIA_CDROM;
872a7fdbcf0SFam Zheng read_only = true;
87333cb7dc8SKevin Wolf } else {
874c4f26c9fSMarkus Armbruster error_setg(errp, "'%s' invalid media", value);
87533cb7dc8SKevin Wolf goto fail;
87633cb7dc8SKevin Wolf }
87733cb7dc8SKevin Wolf }
87833cb7dc8SKevin Wolf
8790ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */
8804e200cf8SAlberto Garcia read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
8810ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
8820ebd24e0SKevin Wolf
8830ebd24e0SKevin Wolf if (read_only && copy_on_read) {
8843dc6f869SAlistair Francis warn_report("disabling copy-on-read on read-only drive");
8850ebd24e0SKevin Wolf copy_on_read = false;
8860ebd24e0SKevin Wolf }
8870ebd24e0SKevin Wolf
88846f5ac20SEric Blake qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off");
88946f5ac20SEric Blake qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off");
8900ebd24e0SKevin Wolf
891593d464bSKevin Wolf /* Controller type */
892593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if");
893593d464bSKevin Wolf if (value) {
894593d464bSKevin Wolf for (type = 0;
895593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]);
896593d464bSKevin Wolf type++) {
897593d464bSKevin Wolf }
898593d464bSKevin Wolf if (type == IF_COUNT) {
899c4f26c9fSMarkus Armbruster error_setg(errp, "unsupported bus type '%s'", value);
900593d464bSKevin Wolf goto fail;
901593d464bSKevin Wolf }
902593d464bSKevin Wolf } else {
903593d464bSKevin Wolf type = block_default_type;
904593d464bSKevin Wolf }
905593d464bSKevin Wolf
90687a899c5SKevin Wolf /* Device address specified by bus/unit or index.
90787a899c5SKevin Wolf * If none was specified, try to find the first free one. */
90887a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
90987a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
91087a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1);
91187a899c5SKevin Wolf
91287a899c5SKevin Wolf max_devs = if_max_devs[type];
91387a899c5SKevin Wolf
91487a899c5SKevin Wolf if (index != -1) {
91587a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) {
916c4f26c9fSMarkus Armbruster error_setg(errp, "index cannot be used with bus and unit");
91787a899c5SKevin Wolf goto fail;
91887a899c5SKevin Wolf }
91987a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index);
92087a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index);
92187a899c5SKevin Wolf }
92287a899c5SKevin Wolf
92387a899c5SKevin Wolf if (unit_id == -1) {
92487a899c5SKevin Wolf unit_id = 0;
92587a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) {
92687a899c5SKevin Wolf unit_id++;
92787a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) {
92887a899c5SKevin Wolf unit_id -= max_devs;
92987a899c5SKevin Wolf bus_id++;
93087a899c5SKevin Wolf }
93187a899c5SKevin Wolf }
93287a899c5SKevin Wolf }
93387a899c5SKevin Wolf
93487a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) {
935c4f26c9fSMarkus Armbruster error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1);
93687a899c5SKevin Wolf goto fail;
93787a899c5SKevin Wolf }
93887a899c5SKevin Wolf
93987a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) {
940c4f26c9fSMarkus Armbruster error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists",
94187a899c5SKevin Wolf bus_id, unit_id, index);
94287a899c5SKevin Wolf goto fail;
94387a899c5SKevin Wolf }
94487a899c5SKevin Wolf
94587a899c5SKevin Wolf /* no id supplied -> create one */
94687a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) {
94787a899c5SKevin Wolf char *new_id;
94887a899c5SKevin Wolf const char *mediastr = "";
94987a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) {
95087a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
95187a899c5SKevin Wolf }
95287a899c5SKevin Wolf if (max_devs) {
95387a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
95487a899c5SKevin Wolf mediastr, unit_id);
95587a899c5SKevin Wolf } else {
95687a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type],
95787a899c5SKevin Wolf mediastr, unit_id);
95887a899c5SKevin Wolf }
95946f5ac20SEric Blake qdict_put_str(bs_opts, "id", new_id);
96087a899c5SKevin Wolf g_free(new_id);
96187a899c5SKevin Wolf }
96287a899c5SKevin Wolf
963394c7d4dSKevin Wolf /* Add virtio block device */
964394c7d4dSKevin Wolf if (type == IF_VIRTIO) {
965394c7d4dSKevin Wolf QemuOpts *devopts;
96687ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
96787ea75d5SPeter Crosthwaite &error_abort);
9689db1d3a2SLaurent Vivier qemu_opt_set(devopts, "driver", "virtio-blk", &error_abort);
969f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
970f43e47dbSMarkus Armbruster &error_abort);
971d3256f88SDavid Woodhouse } else if (type == IF_XEN) {
972d3256f88SDavid Woodhouse QemuOpts *devopts;
973d3256f88SDavid Woodhouse devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
974d3256f88SDavid Woodhouse &error_abort);
975d3256f88SDavid Woodhouse qemu_opt_set(devopts, "driver",
976d3256f88SDavid Woodhouse (media == MEDIA_CDROM) ? "xen-cdrom" : "xen-disk",
977d3256f88SDavid Woodhouse &error_abort);
978d3256f88SDavid Woodhouse qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
979d3256f88SDavid Woodhouse &error_abort);
980394c7d4dSKevin Wolf }
981394c7d4dSKevin Wolf
982d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file");
983d095b465SMax Reitz
984ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */
985ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror");
986ee13ed1cSKevin Wolf if (werror != NULL) {
987ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
988ee13ed1cSKevin Wolf type != IF_NONE) {
989c4f26c9fSMarkus Armbruster error_setg(errp, "werror is not supported by this bus type");
990ee13ed1cSKevin Wolf goto fail;
991ee13ed1cSKevin Wolf }
99246f5ac20SEric Blake qdict_put_str(bs_opts, "werror", werror);
993ee13ed1cSKevin Wolf }
994ee13ed1cSKevin Wolf
995ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror");
996ee13ed1cSKevin Wolf if (rerror != NULL) {
997ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
998ee13ed1cSKevin Wolf type != IF_NONE) {
999c4f26c9fSMarkus Armbruster error_setg(errp, "rerror is not supported by this bus type");
1000ee13ed1cSKevin Wolf goto fail;
1001ee13ed1cSKevin Wolf }
100246f5ac20SEric Blake qdict_put_str(bs_opts, "rerror", rerror);
1003ee13ed1cSKevin Wolf }
1004ee13ed1cSKevin Wolf
10052d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */
1006992861fbSMarkus Armbruster blk = blockdev_init(filename, bs_opts, errp);
10073cb0e25cSMarkus Armbruster bs_opts = NULL;
100818e46a03SMarkus Armbruster if (!blk) {
10092d246f01SKevin Wolf goto fail;
10102d246f01SKevin Wolf }
10112d246f01SKevin Wolf
101226f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */
101326f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo));
1014f298d071SKevin Wolf dinfo->opts = all_opts;
10152d246f01SKevin Wolf
1016ee13ed1cSKevin Wolf dinfo->type = type;
101787a899c5SKevin Wolf dinfo->bus = bus_id;
101887a899c5SKevin Wolf dinfo->unit = unit_id;
1019bcf83158SKevin Wolf
102026f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo);
102126f8b3a8SMarkus Armbruster
1022e34ef046SKevin Wolf switch(type) {
1023e34ef046SKevin Wolf case IF_IDE:
1024e34ef046SKevin Wolf case IF_SCSI:
1025e34ef046SKevin Wolf case IF_XEN:
1026e34ef046SKevin Wolf case IF_NONE:
1027e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM;
1028e34ef046SKevin Wolf break;
1029e34ef046SKevin Wolf default:
1030e34ef046SKevin Wolf break;
1031e34ef046SKevin Wolf }
1032e34ef046SKevin Wolf
10332d246f01SKevin Wolf fail:
103433cb7dc8SKevin Wolf qemu_opts_del(legacy_opts);
1035cb3e7f08SMarc-André Lureau qobject_unref(bs_opts);
10362d246f01SKevin Wolf return dinfo;
103757975222SKevin Wolf }
103857975222SKevin Wolf
qmp_get_root_bs(const char * name,Error ** errp)1039b6c1bae5SKevin Wolf static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1040b6c1bae5SKevin Wolf {
1041b6c1bae5SKevin Wolf BlockDriverState *bs;
1042b6c1bae5SKevin Wolf
10432b3912f1SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
10442b3912f1SKevin Wolf
1045b6c1bae5SKevin Wolf bs = bdrv_lookup_bs(name, name, errp);
1046b6c1bae5SKevin Wolf if (bs == NULL) {
1047b6c1bae5SKevin Wolf return NULL;
1048b6c1bae5SKevin Wolf }
1049b6c1bae5SKevin Wolf
1050b6c1bae5SKevin Wolf if (!bdrv_is_root_node(bs)) {
1051b6c1bae5SKevin Wolf error_setg(errp, "Need a root block node");
1052b6c1bae5SKevin Wolf return NULL;
1053b6c1bae5SKevin Wolf }
1054b6c1bae5SKevin Wolf
1055b6c1bae5SKevin Wolf if (!bdrv_is_inserted(bs)) {
1056b6c1bae5SKevin Wolf error_setg(errp, "Device has no medium");
10571e97be91SEmanuele Giuseppe Esposito bs = NULL;
1058b6c1bae5SKevin Wolf }
1059b6c1bae5SKevin Wolf
1060b6c1bae5SKevin Wolf return bs;
1061b6c1bae5SKevin Wolf }
1062b6c1bae5SKevin Wolf
blockdev_do_action(TransactionAction * action,Error ** errp)106310f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp)
10646cc2a415SPaolo Bonzini {
1065c8a83e85SKevin Wolf TransactionActionList list;
10666cc2a415SPaolo Bonzini
106710f75907SEric Blake list.value = action;
10686cc2a415SPaolo Bonzini list.next = NULL;
1069238e9202SMarkus Armbruster qmp_transaction(&list, NULL, errp);
10706cc2a415SPaolo Bonzini }
10716cc2a415SPaolo Bonzini
qmp_blockdev_snapshot_sync(const char * device,const char * node_name,const char * snapshot_file,const char * snapshot_node_name,const char * format,bool has_mode,NewImageMode mode,Error ** errp)107254fde4ffSMarkus Armbruster void qmp_blockdev_snapshot_sync(const char *device, const char *node_name,
10730901f67eSBenoît Canet const char *snapshot_file,
10740901f67eSBenoît Canet const char *snapshot_node_name,
107554fde4ffSMarkus Armbruster const char *format,
10760901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp)
1077f8882568SJes Sorensen {
1078a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = {
10796cc2a415SPaolo Bonzini .device = (char *) device,
10800901f67eSBenoît Canet .node_name = (char *) node_name,
10816cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file,
10820901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name,
10836cc2a415SPaolo Bonzini .format = (char *) format,
10846cc2a415SPaolo Bonzini .has_mode = has_mode,
10856cc2a415SPaolo Bonzini .mode = mode,
10866cc2a415SPaolo Bonzini };
108710f75907SEric Blake TransactionAction action = {
108810f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
108932bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot,
109010f75907SEric Blake };
109110f75907SEric Blake blockdev_do_action(&action, errp);
1092f8882568SJes Sorensen }
1093f8882568SJes Sorensen
qmp_blockdev_snapshot(const char * node,const char * overlay,Error ** errp)109443de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay,
109543de7e2dSAlberto Garcia Error **errp)
109643de7e2dSAlberto Garcia {
109743de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = {
109843de7e2dSAlberto Garcia .node = (char *) node,
109943de7e2dSAlberto Garcia .overlay = (char *) overlay
110043de7e2dSAlberto Garcia };
110110f75907SEric Blake TransactionAction action = {
110210f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
110332bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data,
110410f75907SEric Blake };
110510f75907SEric Blake blockdev_do_action(&action, errp);
110643de7e2dSAlberto Garcia }
110743de7e2dSAlberto Garcia
qmp_blockdev_snapshot_internal_sync(const char * device,const char * name,Error ** errp)1108f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device,
1109f323bc9eSWenchao Xia const char *name,
1110f323bc9eSWenchao Xia Error **errp)
1111f323bc9eSWenchao Xia {
1112f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = {
1113f323bc9eSWenchao Xia .device = (char *) device,
1114f323bc9eSWenchao Xia .name = (char *) name
1115f323bc9eSWenchao Xia };
111610f75907SEric Blake TransactionAction action = {
111710f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
111832bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot,
111910f75907SEric Blake };
112010f75907SEric Blake blockdev_do_action(&action, errp);
1121f323bc9eSWenchao Xia }
1122f323bc9eSWenchao Xia
qmp_blockdev_snapshot_delete_internal_sync(const char * device,const char * id,const char * name,Error ** errp)112344e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
112444e3e053SWenchao Xia const char *id,
112544e3e053SWenchao Xia const char *name,
112644e3e053SWenchao Xia Error **errp)
112744e3e053SWenchao Xia {
1128a0e8544cSFam Zheng BlockDriverState *bs;
112944e3e053SWenchao Xia QEMUSnapshotInfo sn;
113044e3e053SWenchao Xia Error *local_err = NULL;
113144e3e053SWenchao Xia SnapshotInfo *info = NULL;
113244e3e053SWenchao Xia int ret;
113344e3e053SWenchao Xia
1134a32e7818SKevin Wolf GLOBAL_STATE_CODE();
1135a32e7818SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
1136a32e7818SKevin Wolf
11372dfb4c03SKevin Wolf bs = qmp_get_root_bs(device, errp);
11382dfb4c03SKevin Wolf if (!bs) {
113944e3e053SWenchao Xia return NULL;
114044e3e053SWenchao Xia }
114144e3e053SWenchao Xia
114244e3e053SWenchao Xia if (!id && !name) {
114344e3e053SWenchao Xia error_setg(errp, "Name or id must be provided");
1144b49f4755SStefan Hajnoczi return NULL;
114544e3e053SWenchao Xia }
114644e3e053SWenchao Xia
11470b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1148b49f4755SStefan Hajnoczi return NULL;
11490b928854SStefan Hajnoczi }
11500b928854SStefan Hajnoczi
115144e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
115284d18f06SMarkus Armbruster if (local_err) {
115344e3e053SWenchao Xia error_propagate(errp, local_err);
1154b49f4755SStefan Hajnoczi return NULL;
115544e3e053SWenchao Xia }
115644e3e053SWenchao Xia if (!ret) {
115744e3e053SWenchao Xia error_setg(errp,
115844e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on "
115944e3e053SWenchao Xia "device '%s'",
116044e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device);
1161b49f4755SStefan Hajnoczi return NULL;
116244e3e053SWenchao Xia }
116344e3e053SWenchao Xia
116444e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err);
116584d18f06SMarkus Armbruster if (local_err) {
116644e3e053SWenchao Xia error_propagate(errp, local_err);
1167b49f4755SStefan Hajnoczi return NULL;
116844e3e053SWenchao Xia }
116944e3e053SWenchao Xia
11705839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1);
117144e3e053SWenchao Xia info->id = g_strdup(sn.id_str);
117244e3e053SWenchao Xia info->name = g_strdup(sn.name);
117344e3e053SWenchao Xia info->date_nsec = sn.date_nsec;
117444e3e053SWenchao Xia info->date_sec = sn.date_sec;
117544e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size;
117644e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
117744e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1178b39847a5SPavel Dovgalyuk if (sn.icount != -1ULL) {
1179b39847a5SPavel Dovgalyuk info->icount = sn.icount;
1180b39847a5SPavel Dovgalyuk info->has_icount = true;
1181b39847a5SPavel Dovgalyuk }
118244e3e053SWenchao Xia
118344e3e053SWenchao Xia return info;
118444e3e053SWenchao Xia }
11858802d1fdSJeff Cody
1186bbe86010SWenchao Xia /* internal snapshot private data */
1187bbe86010SWenchao Xia typedef struct InternalSnapshotState {
1188bbe86010SWenchao Xia BlockDriverState *bs;
1189bbe86010SWenchao Xia QEMUSnapshotInfo sn;
1190507306ccSFam Zheng bool created;
1191bbe86010SWenchao Xia } InternalSnapshotState;
1192bbe86010SWenchao Xia
11938187f63cSVladimir Sementsov-Ogievskiy static void internal_snapshot_abort(void *opaque);
11948187f63cSVladimir Sementsov-Ogievskiy static void internal_snapshot_clean(void *opaque);
11958187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv internal_snapshot_drv = {
11968187f63cSVladimir Sementsov-Ogievskiy .abort = internal_snapshot_abort,
11978187f63cSVladimir Sementsov-Ogievskiy .clean = internal_snapshot_clean,
11988187f63cSVladimir Sementsov-Ogievskiy };
119994d16a64SJohn Snow
internal_snapshot_action(BlockdevSnapshotInternal * internal,Transaction * tran,Error ** errp)1200d53c89aeSVladimir Sementsov-Ogievskiy static void internal_snapshot_action(BlockdevSnapshotInternal *internal,
12018187f63cSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
1202bbe86010SWenchao Xia {
1203f70edf99SMarkus Armbruster Error *local_err = NULL;
1204bbe86010SWenchao Xia const char *device;
1205bbe86010SWenchao Xia const char *name;
1206bbe86010SWenchao Xia BlockDriverState *bs;
1207bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn;
1208bbe86010SWenchao Xia bool ret;
1209f793dde0SMarc-André Lureau int64_t rt;
1210d53c89aeSVladimir Sementsov-Ogievskiy InternalSnapshotState *state = g_new0(InternalSnapshotState, 1);
1211bbe86010SWenchao Xia int ret1;
1212bbe86010SWenchao Xia
1213a32e7818SKevin Wolf GLOBAL_STATE_CODE();
1214a32e7818SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
1215a32e7818SKevin Wolf
12168187f63cSVladimir Sementsov-Ogievskiy tran_add(tran, &internal_snapshot_drv, state);
12178187f63cSVladimir Sementsov-Ogievskiy
1218bbe86010SWenchao Xia device = internal->device;
1219bbe86010SWenchao Xia name = internal->name;
1220bbe86010SWenchao Xia
122175dfd402SKevin Wolf bs = qmp_get_root_bs(device, errp);
122275dfd402SKevin Wolf if (!bs) {
1223bbe86010SWenchao Xia return;
1224bbe86010SWenchao Xia }
1225bbe86010SWenchao Xia
1226507306ccSFam Zheng state->bs = bs;
1227a36e458cSStefan Hajnoczi
1228a36e458cSStefan Hajnoczi /* Paired with .clean() */
1229507306ccSFam Zheng bdrv_drained_begin(bs);
1230507306ccSFam Zheng
12313dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1232b49f4755SStefan Hajnoczi return;
12333dc7ca3cSStefan Hajnoczi }
12343dc7ca3cSStefan Hajnoczi
1235bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) {
123681e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device);
1237b49f4755SStefan Hajnoczi return;
1238bbe86010SWenchao Xia }
1239bbe86010SWenchao Xia
1240bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) {
124181e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' "
124281e5f78aSAlberto Garcia "does not support internal snapshots",
124381e5f78aSAlberto Garcia bs->drv->format_name, device);
1244b49f4755SStefan Hajnoczi return;
1245bbe86010SWenchao Xia }
1246bbe86010SWenchao Xia
1247bbe86010SWenchao Xia if (!strlen(name)) {
1248bbe86010SWenchao Xia error_setg(errp, "Name is empty");
1249b49f4755SStefan Hajnoczi return;
1250bbe86010SWenchao Xia }
1251bbe86010SWenchao Xia
1252bbe86010SWenchao Xia /* check whether a snapshot with name exist */
1253f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1254f70edf99SMarkus Armbruster &local_err);
1255f70edf99SMarkus Armbruster if (local_err) {
1256f70edf99SMarkus Armbruster error_propagate(errp, local_err);
1257b49f4755SStefan Hajnoczi return;
1258bbe86010SWenchao Xia } else if (ret) {
1259bbe86010SWenchao Xia error_setg(errp,
1260bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'",
1261bbe86010SWenchao Xia name, device);
1262b49f4755SStefan Hajnoczi return;
1263bbe86010SWenchao Xia }
1264bbe86010SWenchao Xia
1265bbe86010SWenchao Xia /* 3. take the snapshot */
1266bbe86010SWenchao Xia sn = &state->sn;
1267bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name);
1268f793dde0SMarc-André Lureau rt = g_get_real_time();
1269f793dde0SMarc-André Lureau sn->date_sec = rt / G_USEC_PER_SEC;
1270f793dde0SMarc-André Lureau sn->date_nsec = (rt % G_USEC_PER_SEC) * 1000;
1271bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1272b39847a5SPavel Dovgalyuk if (replay_mode != REPLAY_MODE_NONE) {
1273b39847a5SPavel Dovgalyuk sn->icount = replay_get_current_icount();
1274b39847a5SPavel Dovgalyuk } else {
1275b39847a5SPavel Dovgalyuk sn->icount = -1ULL;
1276b39847a5SPavel Dovgalyuk }
1277bbe86010SWenchao Xia
1278bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn);
1279bbe86010SWenchao Xia if (ret1 < 0) {
1280bbe86010SWenchao Xia error_setg_errno(errp, -ret1,
1281bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'",
1282bbe86010SWenchao Xia name, device);
1283b49f4755SStefan Hajnoczi return;
1284bbe86010SWenchao Xia }
1285bbe86010SWenchao Xia
1286bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */
1287507306ccSFam Zheng state->created = true;
1288bbe86010SWenchao Xia }
1289bbe86010SWenchao Xia
internal_snapshot_abort(void * opaque)12908187f63cSVladimir Sementsov-Ogievskiy static void internal_snapshot_abort(void *opaque)
1291bbe86010SWenchao Xia {
12928187f63cSVladimir Sementsov-Ogievskiy InternalSnapshotState *state = opaque;
1293bbe86010SWenchao Xia BlockDriverState *bs = state->bs;
1294bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn;
1295bbe86010SWenchao Xia Error *local_error = NULL;
1296bbe86010SWenchao Xia
1297a32e7818SKevin Wolf GLOBAL_STATE_CODE();
1298a32e7818SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
1299a32e7818SKevin Wolf
1300507306ccSFam Zheng if (!state->created) {
1301bbe86010SWenchao Xia return;
1302bbe86010SWenchao Xia }
1303bbe86010SWenchao Xia
1304bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1305c29b77f9SMarkus Armbruster error_reportf_err(local_error,
1306c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and "
1307c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ",
1308c29b77f9SMarkus Armbruster sn->id_str, sn->name,
1309c29b77f9SMarkus Armbruster bdrv_get_device_name(bs));
1310bbe86010SWenchao Xia }
1311bbe86010SWenchao Xia }
1312bbe86010SWenchao Xia
internal_snapshot_clean(void * opaque)13138187f63cSVladimir Sementsov-Ogievskiy static void internal_snapshot_clean(void *opaque)
13145d6e96efSStefan Hajnoczi {
13158187f63cSVladimir Sementsov-Ogievskiy g_autofree InternalSnapshotState *state = opaque;
13165d6e96efSStefan Hajnoczi
1317a36e458cSStefan Hajnoczi if (!state->bs) {
1318a36e458cSStefan Hajnoczi return;
1319a36e458cSStefan Hajnoczi }
1320a36e458cSStefan Hajnoczi
1321507306ccSFam Zheng bdrv_drained_end(state->bs);
13225d6e96efSStefan Hajnoczi }
13235d6e96efSStefan Hajnoczi
1324ba0c86a3SWenchao Xia /* external snapshot private data */
1325ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState {
13268802d1fdSJeff Cody BlockDriverState *old_bs;
13278802d1fdSJeff Cody BlockDriverState *new_bs;
1328067acf28SKevin Wolf bool overlay_appended;
1329ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState;
13308802d1fdSJeff Cody
13318187f63cSVladimir Sementsov-Ogievskiy static void external_snapshot_commit(void *opaque);
13328187f63cSVladimir Sementsov-Ogievskiy static void external_snapshot_abort(void *opaque);
13338187f63cSVladimir Sementsov-Ogievskiy static void external_snapshot_clean(void *opaque);
13348187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv external_snapshot_drv = {
13358187f63cSVladimir Sementsov-Ogievskiy .commit = external_snapshot_commit,
13368187f63cSVladimir Sementsov-Ogievskiy .abort = external_snapshot_abort,
13378187f63cSVladimir Sementsov-Ogievskiy .clean = external_snapshot_clean,
13388187f63cSVladimir Sementsov-Ogievskiy };
13398187f63cSVladimir Sementsov-Ogievskiy
external_snapshot_action(TransactionAction * action,Transaction * tran,Error ** errp)1340d53c89aeSVladimir Sementsov-Ogievskiy static void external_snapshot_action(TransactionAction *action,
1341d53c89aeSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
13429b9877eeSWenchao Xia {
1343934aee14SVladimir Sementsov-Ogievskiy int ret;
13445b363937SMax Reitz int flags = 0;
134543de7e2dSAlberto Garcia QDict *options = NULL;
13469b9877eeSWenchao Xia Error *local_err = NULL;
134743de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */
1348e2a31e87SWenchao Xia const char *device;
13490901f67eSBenoît Canet const char *node_name;
135043de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */
135143de7e2dSAlberto Garcia const char *snapshot_ref;
135243de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */
1353e2a31e87SWenchao Xia const char *new_image_file;
1354d53c89aeSVladimir Sementsov-Ogievskiy ExternalSnapshotState *state = g_new0(ExternalSnapshotState, 1);
1355d29d3d1fSKevin Wolf uint64_t perm, shared;
13569b9877eeSWenchao Xia
1357bce73bc2SKevin Wolf /* TODO We'll eventually have to take a writer lock in this function */
1358bce73bc2SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
1359bce73bc2SKevin Wolf
13608187f63cSVladimir Sementsov-Ogievskiy tran_add(tran, &external_snapshot_drv, state);
13618187f63cSVladimir Sementsov-Ogievskiy
136243de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
136343de7e2dSAlberto Garcia * purpose but a different set of parameters */
136443de7e2dSAlberto Garcia switch (action->type) {
136543de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
136643de7e2dSAlberto Garcia {
136732bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
136843de7e2dSAlberto Garcia device = s->node;
136943de7e2dSAlberto Garcia node_name = s->node;
137043de7e2dSAlberto Garcia new_image_file = NULL;
137143de7e2dSAlberto Garcia snapshot_ref = s->overlay;
1372e2a31e87SWenchao Xia }
137343de7e2dSAlberto Garcia break;
137443de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
137543de7e2dSAlberto Garcia {
137632bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
137754fde4ffSMarkus Armbruster device = s->device;
137854fde4ffSMarkus Armbruster node_name = s->node_name;
137943de7e2dSAlberto Garcia new_image_file = s->snapshot_file;
138043de7e2dSAlberto Garcia snapshot_ref = NULL;
138143de7e2dSAlberto Garcia }
138243de7e2dSAlberto Garcia break;
138343de7e2dSAlberto Garcia default:
138443de7e2dSAlberto Garcia g_assert_not_reached();
1385e2a31e87SWenchao Xia }
1386e2a31e87SWenchao Xia
1387e2a31e87SWenchao Xia /* start processing */
138894d16a64SJohn Snow
138943de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp);
139043de7e2dSAlberto Garcia if (!state->old_bs) {
13919b9877eeSWenchao Xia return;
13929b9877eeSWenchao Xia }
13939b9877eeSWenchao Xia
13942d24b60bSStefan Hajnoczi /* Paired with .clean() */
1395da763e83SFam Zheng bdrv_drained_begin(state->old_bs);
13965d6e96efSStefan Hajnoczi
1397ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) {
1398a95921f1SPhilippe Mathieu-Daudé error_setg(errp, "Device '%s' has no medium",
13997fcb8c89SMarkus Armbruster bdrv_get_device_or_node_name(state->old_bs));
1400b49f4755SStefan Hajnoczi return;
14019b9877eeSWenchao Xia }
14029b9877eeSWenchao Xia
14033718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs,
14043718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1405b49f4755SStefan Hajnoczi return;
14069b9877eeSWenchao Xia }
14079b9877eeSWenchao Xia
1408ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) {
1409540d91b4SMarkus Armbruster ret = bdrv_flush(state->old_bs);
1410540d91b4SMarkus Armbruster if (ret < 0) {
1411540d91b4SMarkus Armbruster error_setg_errno(errp, -ret, "Write to node '%s' failed",
1412540d91b4SMarkus Armbruster bdrv_get_device_or_node_name(state->old_bs));
1413b49f4755SStefan Hajnoczi return;
14149b9877eeSWenchao Xia }
14159b9877eeSWenchao Xia }
14169b9877eeSWenchao Xia
141743de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
141832bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
141954fde4ffSMarkus Armbruster const char *format = s->format ?: "qcow2";
142043de7e2dSAlberto Garcia enum NewImageMode mode;
142154fde4ffSMarkus Armbruster const char *snapshot_node_name = s->snapshot_node_name;
142243de7e2dSAlberto Garcia
142343de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) {
1424ef2e38a1SConnor Kuehl error_setg(errp, "New overlay node-name missing");
1425b49f4755SStefan Hajnoczi return;
142643de7e2dSAlberto Garcia }
142743de7e2dSAlberto Garcia
142843de7e2dSAlberto Garcia if (snapshot_node_name &&
142943de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1430ef2e38a1SConnor Kuehl error_setg(errp, "New overlay node-name already in use");
1431b49f4755SStefan Hajnoczi return;
143243de7e2dSAlberto Garcia }
143343de7e2dSAlberto Garcia
1434ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags;
14352a32c6e8SJohn Snow flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ);
14362a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING;
14379b9877eeSWenchao Xia
14389b9877eeSWenchao Xia /* create new image w/backing file */
143943de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
14409b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) {
1441f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs);
1442f86b8b58SKevin Wolf if (size < 0) {
1443f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed");
1444b49f4755SStefan Hajnoczi return;
1445f86b8b58SKevin Wolf }
1446f30c66baSMax Reitz bdrv_refresh_filename(state->old_bs);
1447e1355055SKevin Wolf
14489b9877eeSWenchao Xia bdrv_img_create(new_image_file, format,
1449ba5d6ab6SStefan Hajnoczi state->old_bs->filename,
1450ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name,
14519217283dSFam Zheng NULL, size, flags, false, &local_err);
1452e1355055SKevin Wolf
145384d18f06SMarkus Armbruster if (local_err) {
14549b9877eeSWenchao Xia error_propagate(errp, local_err);
1455b49f4755SStefan Hajnoczi return;
14569b9877eeSWenchao Xia }
14579b9877eeSWenchao Xia }
14589b9877eeSWenchao Xia
14590901f67eSBenoît Canet options = qdict_new();
1460d52e1a0eSPeter Maydell if (snapshot_node_name) {
146146f5ac20SEric Blake qdict_put_str(options, "node-name", snapshot_node_name);
14620901f67eSBenoît Canet }
146346f5ac20SEric Blake qdict_put_str(options, "driver", format);
146443de7e2dSAlberto Garcia }
146543de7e2dSAlberto Garcia
14665b363937SMax Reitz state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
14675b363937SMax Reitz errp);
1468c6e0a6deSKevin Wolf
1469f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */
14705b363937SMax Reitz if (!state->new_bs) {
1471c6e0a6deSKevin Wolf return;
147243de7e2dSAlberto Garcia }
147343de7e2dSAlberto Garcia
1474d29d3d1fSKevin Wolf /*
1475d29d3d1fSKevin Wolf * Allow attaching a backing file to an overlay that's already in use only
1476d29d3d1fSKevin Wolf * if the parents don't assume that they are already seeing a valid image.
1477d29d3d1fSKevin Wolf * (Specifically, allow it as a mirror target, which is write-only access.)
1478d29d3d1fSKevin Wolf */
1479d29d3d1fSKevin Wolf bdrv_get_cumulative_perm(state->new_bs, &perm, &shared);
1480d29d3d1fSKevin Wolf if (perm & BLK_PERM_CONSISTENT_READ) {
1481a2bb6f8cSMax Reitz error_setg(errp, "The overlay is already in use");
1482b49f4755SStefan Hajnoczi return;
148343de7e2dSAlberto Garcia }
148443de7e2dSAlberto Garcia
14857cc734a9SMax Reitz if (state->new_bs->drv->is_filter) {
14867cc734a9SMax Reitz error_setg(errp, "Filters cannot be used as overlays");
1487b49f4755SStefan Hajnoczi return;
14887cc734a9SMax Reitz }
14897cc734a9SMax Reitz
14907cc734a9SMax Reitz if (bdrv_cow_child(state->new_bs)) {
1491a2bb6f8cSMax Reitz error_setg(errp, "The overlay already has a backing image");
1492b49f4755SStefan Hajnoczi return;
149308b24cfeSAlberto Garcia }
149408b24cfeSAlberto Garcia
149508b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) {
1496a2bb6f8cSMax Reitz error_setg(errp, "The overlay does not support backing images");
1497b49f4755SStefan Hajnoczi return;
1498b2c2832cSKevin Wolf }
1499b2c2832cSKevin Wolf
1500934aee14SVladimir Sementsov-Ogievskiy ret = bdrv_append(state->new_bs, state->old_bs, errp);
1501934aee14SVladimir Sementsov-Ogievskiy if (ret < 0) {
1502b49f4755SStefan Hajnoczi return;
15039b9877eeSWenchao Xia }
1504067acf28SKevin Wolf state->overlay_appended = true;
15059b9877eeSWenchao Xia }
15069b9877eeSWenchao Xia
external_snapshot_commit(void * opaque)15078187f63cSVladimir Sementsov-Ogievskiy static void external_snapshot_commit(void *opaque)
15083b0047e8SWenchao Xia {
15098187f63cSVladimir Sementsov-Ogievskiy ExternalSnapshotState *state = opaque;
1510ba0c86a3SWenchao Xia
15113b0047e8SWenchao Xia /* We don't need (or want) to use the transactional
15123b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we
15133b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */
1514d73415a3SStefan Hajnoczi if (!qatomic_read(&state->old_bs->copy_on_read)) {
15151b57774fSAlberto Garcia bdrv_reopen_set_read_only(state->old_bs, true, NULL);
15163b0047e8SWenchao Xia }
15174c844983SKevin Wolf }
15183b0047e8SWenchao Xia
external_snapshot_abort(void * opaque)15198187f63cSVladimir Sementsov-Ogievskiy static void external_snapshot_abort(void *opaque)
152096b86bf7SWenchao Xia {
15218187f63cSVladimir Sementsov-Ogievskiy ExternalSnapshotState *state = opaque;
1522ba5d6ab6SStefan Hajnoczi if (state->new_bs) {
1523067acf28SKevin Wolf if (state->overlay_appended) {
15242d24b60bSStefan Hajnoczi AioContext *aio_context;
1525377410f6SSergio Lopez AioContext *tmp_context;
1526377410f6SSergio Lopez int ret;
15272d24b60bSStefan Hajnoczi
15282d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs);
15292d24b60bSStefan Hajnoczi
1530719fc28cSJeff Cody bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
1531719fc28cSJeff Cody close state->old_bs; we need it */
1532719fc28cSJeff Cody bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
1533377410f6SSergio Lopez
1534377410f6SSergio Lopez /*
1535377410f6SSergio Lopez * The call to bdrv_set_backing_hd() above returns state->old_bs to
1536377410f6SSergio Lopez * the main AioContext. As we're still going to be using it, return
1537377410f6SSergio Lopez * it to the AioContext it was before.
1538377410f6SSergio Lopez */
1539377410f6SSergio Lopez tmp_context = bdrv_get_aio_context(state->old_bs);
1540377410f6SSergio Lopez if (aio_context != tmp_context) {
1541142e6907SEmanuele Giuseppe Esposito ret = bdrv_try_change_aio_context(state->old_bs,
1542142e6907SEmanuele Giuseppe Esposito aio_context, NULL, NULL);
1543377410f6SSergio Lopez assert(ret == 0);
1544377410f6SSergio Lopez }
1545377410f6SSergio Lopez
1546ccd6a379SKevin Wolf bdrv_drained_begin(state->new_bs);
15476bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
15485fe31c25SKevin Wolf bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
15496bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
1550ccd6a379SKevin Wolf bdrv_drained_end(state->new_bs);
1551ccd6a379SKevin Wolf
1552719fc28cSJeff Cody bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
1553b2c2832cSKevin Wolf }
155496b86bf7SWenchao Xia }
1555da763e83SFam Zheng }
1556da763e83SFam Zheng
external_snapshot_clean(void * opaque)15578187f63cSVladimir Sementsov-Ogievskiy static void external_snapshot_clean(void *opaque)
1558da763e83SFam Zheng {
15598187f63cSVladimir Sementsov-Ogievskiy g_autofree ExternalSnapshotState *state = opaque;
15602d24b60bSStefan Hajnoczi
15612d24b60bSStefan Hajnoczi if (!state->old_bs) {
15622d24b60bSStefan Hajnoczi return;
15632d24b60bSStefan Hajnoczi }
15642d24b60bSStefan Hajnoczi
1565da763e83SFam Zheng bdrv_drained_end(state->old_bs);
1566b2c2832cSKevin Wolf bdrv_unref(state->new_bs);
156796b86bf7SWenchao Xia }
156896b86bf7SWenchao Xia
15693037f364SStefan Hajnoczi typedef struct DriveBackupState {
15703037f364SStefan Hajnoczi BlockDriverState *bs;
15713037f364SStefan Hajnoczi BlockJob *job;
15723037f364SStefan Hajnoczi } DriveBackupState;
15733037f364SStefan Hajnoczi
15742288ccfaSSergio Lopez static BlockJob *do_backup_common(BackupCommon *backup,
15752288ccfaSSergio Lopez BlockDriverState *bs,
15762288ccfaSSergio Lopez BlockDriverState *target_bs,
15772288ccfaSSergio Lopez AioContext *aio_context,
15782288ccfaSSergio Lopez JobTxn *txn, Error **errp);
157978f51fdeSJohn Snow
15808187f63cSVladimir Sementsov-Ogievskiy static void drive_backup_commit(void *opaque);
15818187f63cSVladimir Sementsov-Ogievskiy static void drive_backup_abort(void *opaque);
15828187f63cSVladimir Sementsov-Ogievskiy static void drive_backup_clean(void *opaque);
15838187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv drive_backup_drv = {
15848187f63cSVladimir Sementsov-Ogievskiy .commit = drive_backup_commit,
15858187f63cSVladimir Sementsov-Ogievskiy .abort = drive_backup_abort,
15868187f63cSVladimir Sementsov-Ogievskiy .clean = drive_backup_clean,
15878187f63cSVladimir Sementsov-Ogievskiy };
15888187f63cSVladimir Sementsov-Ogievskiy
drive_backup_action(DriveBackup * backup,JobTxn * block_job_txn,Transaction * tran,Error ** errp)1589d53c89aeSVladimir Sementsov-Ogievskiy static void drive_backup_action(DriveBackup *backup,
1590d53c89aeSVladimir Sementsov-Ogievskiy JobTxn *block_job_txn,
1591d53c89aeSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
15923037f364SStefan Hajnoczi {
1593d53c89aeSVladimir Sementsov-Ogievskiy DriveBackupState *state = g_new0(DriveBackupState, 1);
15942288ccfaSSergio Lopez BlockDriverState *bs;
15952288ccfaSSergio Lopez BlockDriverState *target_bs;
15962288ccfaSSergio Lopez BlockDriverState *source = NULL;
159766d56054SStefan Hajnoczi AioContext *aio_context;
159804658a5bSMarkus Armbruster const char *format;
15992288ccfaSSergio Lopez QDict *options;
16003037f364SStefan Hajnoczi Error *local_err = NULL;
16012288ccfaSSergio Lopez int flags;
16022288ccfaSSergio Lopez int64_t size;
16032288ccfaSSergio Lopez bool set_backing_hd = false;
16043ea67e08SSergio Lopez int ret;
16053037f364SStefan Hajnoczi
1606b7cfc7d5SKevin Wolf GLOBAL_STATE_CODE();
1607b7cfc7d5SKevin Wolf
16088187f63cSVladimir Sementsov-Ogievskiy tran_add(tran, &drive_backup_drv, state);
16098187f63cSVladimir Sementsov-Ogievskiy
16102288ccfaSSergio Lopez if (!backup->has_mode) {
16112288ccfaSSergio Lopez backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
16122288ccfaSSergio Lopez }
16132288ccfaSSergio Lopez
161485c9d133SVladimir Sementsov-Ogievskiy bs = bdrv_lookup_bs(backup->device, backup->device, errp);
1615b7e4fa22SKevin Wolf if (!bs) {
16161fdd4b7bSFam Zheng return;
16171fdd4b7bSFam Zheng }
16181fdd4b7bSFam Zheng
16192288ccfaSSergio Lopez if (!bs->drv) {
16202288ccfaSSergio Lopez error_setg(errp, "Device has no medium");
16212288ccfaSSergio Lopez return;
16222288ccfaSSergio Lopez }
16232288ccfaSSergio Lopez
162466d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs);
162566d56054SStefan Hajnoczi
16262842ff2dSVladimir Sementsov-Ogievskiy state->bs = bs;
162766d56054SStefan Hajnoczi /* Paired with .clean() */
1628b7e4fa22SKevin Wolf bdrv_drained_begin(bs);
162966d56054SStefan Hajnoczi
163004658a5bSMarkus Armbruster format = backup->format;
163104658a5bSMarkus Armbruster if (!format && backup->mode != NEW_IMAGE_MODE_EXISTING) {
163204658a5bSMarkus Armbruster format = bs->drv->format_name;
16332288ccfaSSergio Lopez }
16345d6e96efSStefan Hajnoczi
16352288ccfaSSergio Lopez /* Early check to avoid creating target */
1636277f2007SKevin Wolf bdrv_graph_rdlock_main_loop();
16372288ccfaSSergio Lopez if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
1638277f2007SKevin Wolf bdrv_graph_rdunlock_main_loop();
1639b49f4755SStefan Hajnoczi return;
16402288ccfaSSergio Lopez }
16412288ccfaSSergio Lopez
16422288ccfaSSergio Lopez flags = bs->open_flags | BDRV_O_RDWR;
16432288ccfaSSergio Lopez
16442288ccfaSSergio Lopez /*
16452288ccfaSSergio Lopez * See if we have a backing HD we can use to create our new image
16462288ccfaSSergio Lopez * on top of.
16472288ccfaSSergio Lopez */
16482288ccfaSSergio Lopez if (backup->sync == MIRROR_SYNC_MODE_TOP) {
16492b088c60SMax Reitz /*
16502b088c60SMax Reitz * Backup will not replace the source by the target, so none
16512b088c60SMax Reitz * of the filters skipped here will be removed (in contrast to
16522b088c60SMax Reitz * mirror). Therefore, we can skip all of them when looking
16532b088c60SMax Reitz * for the first COW relationship.
16542b088c60SMax Reitz */
16552b088c60SMax Reitz source = bdrv_cow_bs(bdrv_skip_filters(bs));
16562288ccfaSSergio Lopez if (!source) {
16572288ccfaSSergio Lopez backup->sync = MIRROR_SYNC_MODE_FULL;
16582288ccfaSSergio Lopez }
16592288ccfaSSergio Lopez }
16602288ccfaSSergio Lopez if (backup->sync == MIRROR_SYNC_MODE_NONE) {
16612288ccfaSSergio Lopez source = bs;
16622288ccfaSSergio Lopez flags |= BDRV_O_NO_BACKING;
16632288ccfaSSergio Lopez set_backing_hd = true;
16642288ccfaSSergio Lopez }
1665ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop();
16662288ccfaSSergio Lopez
16672288ccfaSSergio Lopez size = bdrv_getlength(bs);
16682288ccfaSSergio Lopez if (size < 0) {
16692288ccfaSSergio Lopez error_setg_errno(errp, -size, "bdrv_getlength failed");
1670b49f4755SStefan Hajnoczi return;
16712288ccfaSSergio Lopez }
16722288ccfaSSergio Lopez
16732288ccfaSSergio Lopez if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
167404658a5bSMarkus Armbruster assert(format);
16752288ccfaSSergio Lopez if (source) {
16762b088c60SMax Reitz /* Implicit filters should not appear in the filename */
1677430da832SKevin Wolf BlockDriverState *explicit_backing;
16782b088c60SMax Reitz
1679b7cfc7d5SKevin Wolf bdrv_graph_rdlock_main_loop();
1680430da832SKevin Wolf explicit_backing = bdrv_skip_implicit_filters(source);
16812b088c60SMax Reitz bdrv_refresh_filename(explicit_backing);
1682b7cfc7d5SKevin Wolf bdrv_graph_rdunlock_main_loop();
1683b7cfc7d5SKevin Wolf
168404658a5bSMarkus Armbruster bdrv_img_create(backup->target, format,
16852b088c60SMax Reitz explicit_backing->filename,
16862b088c60SMax Reitz explicit_backing->drv->format_name, NULL,
16872288ccfaSSergio Lopez size, flags, false, &local_err);
16882288ccfaSSergio Lopez } else {
168904658a5bSMarkus Armbruster bdrv_img_create(backup->target, format, NULL, NULL, NULL,
16902288ccfaSSergio Lopez size, flags, false, &local_err);
16912288ccfaSSergio Lopez }
16922288ccfaSSergio Lopez }
16932288ccfaSSergio Lopez
169484d18f06SMarkus Armbruster if (local_err) {
16953037f364SStefan Hajnoczi error_propagate(errp, local_err);
1696b49f4755SStefan Hajnoczi return;
16973037f364SStefan Hajnoczi }
169866d56054SStefan Hajnoczi
16992288ccfaSSergio Lopez options = qdict_new();
17002288ccfaSSergio Lopez qdict_put_str(options, "discard", "unmap");
17012288ccfaSSergio Lopez qdict_put_str(options, "detect-zeroes", "unmap");
170204658a5bSMarkus Armbruster if (format) {
170304658a5bSMarkus Armbruster qdict_put_str(options, "driver", format);
17042288ccfaSSergio Lopez }
17052288ccfaSSergio Lopez
17062288ccfaSSergio Lopez target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
17072288ccfaSSergio Lopez if (!target_bs) {
1708c6e0a6deSKevin Wolf return;
17092288ccfaSSergio Lopez }
17102288ccfaSSergio Lopez
1711142e6907SEmanuele Giuseppe Esposito ret = bdrv_try_change_aio_context(target_bs, aio_context, NULL, errp);
17123ea67e08SSergio Lopez if (ret < 0) {
17133ea67e08SSergio Lopez bdrv_unref(target_bs);
17143ea67e08SSergio Lopez return;
17153ea67e08SSergio Lopez }
17163ea67e08SSergio Lopez
17172288ccfaSSergio Lopez if (set_backing_hd) {
17185a11a1caSVladimir Sementsov-Ogievskiy if (bdrv_set_backing_hd(target_bs, source, errp) < 0) {
17192288ccfaSSergio Lopez goto unref;
17202288ccfaSSergio Lopez }
17212288ccfaSSergio Lopez }
17222288ccfaSSergio Lopez
17232288ccfaSSergio Lopez state->job = do_backup_common(qapi_DriveBackup_base(backup),
17242288ccfaSSergio Lopez bs, target_bs, aio_context,
1725d53c89aeSVladimir Sementsov-Ogievskiy block_job_txn, errp);
17262288ccfaSSergio Lopez
17272288ccfaSSergio Lopez unref:
17282288ccfaSSergio Lopez bdrv_unref(target_bs);
1729111049a4SJohn Snow }
17303037f364SStefan Hajnoczi
drive_backup_commit(void * opaque)17318187f63cSVladimir Sementsov-Ogievskiy static void drive_backup_commit(void *opaque)
1732111049a4SJohn Snow {
17338187f63cSVladimir Sementsov-Ogievskiy DriveBackupState *state = opaque;
173466d56054SStefan Hajnoczi
1735111049a4SJohn Snow assert(state->job);
1736da01ff7fSKevin Wolf job_start(&state->job->job);
17373037f364SStefan Hajnoczi }
17383037f364SStefan Hajnoczi
drive_backup_abort(void * opaque)17398187f63cSVladimir Sementsov-Ogievskiy static void drive_backup_abort(void *opaque)
17403037f364SStefan Hajnoczi {
17418187f63cSVladimir Sementsov-Ogievskiy DriveBackupState *state = opaque;
17423037f364SStefan Hajnoczi
1743111049a4SJohn Snow if (state->job) {
17444cfb3f05SHanna Reitz job_cancel_sync(&state->job->job, true);
17453037f364SStefan Hajnoczi }
17463037f364SStefan Hajnoczi }
17473037f364SStefan Hajnoczi
drive_backup_clean(void * opaque)17488187f63cSVladimir Sementsov-Ogievskiy static void drive_backup_clean(void *opaque)
17495d6e96efSStefan Hajnoczi {
17508187f63cSVladimir Sementsov-Ogievskiy g_autofree DriveBackupState *state = opaque;
17515d6e96efSStefan Hajnoczi
175266d56054SStefan Hajnoczi if (!state->bs) {
175366d56054SStefan Hajnoczi return;
17545d6e96efSStefan Hajnoczi }
175566d56054SStefan Hajnoczi
175666d56054SStefan Hajnoczi bdrv_drained_end(state->bs);
17575d6e96efSStefan Hajnoczi }
17585d6e96efSStefan Hajnoczi
1759bd8baecdSFam Zheng typedef struct BlockdevBackupState {
1760bd8baecdSFam Zheng BlockDriverState *bs;
1761bd8baecdSFam Zheng BlockJob *job;
1762bd8baecdSFam Zheng } BlockdevBackupState;
1763bd8baecdSFam Zheng
17648187f63cSVladimir Sementsov-Ogievskiy static void blockdev_backup_commit(void *opaque);
17658187f63cSVladimir Sementsov-Ogievskiy static void blockdev_backup_abort(void *opaque);
17668187f63cSVladimir Sementsov-Ogievskiy static void blockdev_backup_clean(void *opaque);
17678187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv blockdev_backup_drv = {
17688187f63cSVladimir Sementsov-Ogievskiy .commit = blockdev_backup_commit,
17698187f63cSVladimir Sementsov-Ogievskiy .abort = blockdev_backup_abort,
17708187f63cSVladimir Sementsov-Ogievskiy .clean = blockdev_backup_clean,
17718187f63cSVladimir Sementsov-Ogievskiy };
17728187f63cSVladimir Sementsov-Ogievskiy
blockdev_backup_action(BlockdevBackup * backup,JobTxn * block_job_txn,Transaction * tran,Error ** errp)1773d53c89aeSVladimir Sementsov-Ogievskiy static void blockdev_backup_action(BlockdevBackup *backup,
1774d53c89aeSVladimir Sementsov-Ogievskiy JobTxn *block_job_txn,
1775d53c89aeSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
1776bd8baecdSFam Zheng {
1777d53c89aeSVladimir Sementsov-Ogievskiy BlockdevBackupState *state = g_new0(BlockdevBackupState, 1);
17785b7bfe51SSergio Lopez BlockDriverState *bs;
17795b7bfe51SSergio Lopez BlockDriverState *target_bs;
1780edd5adeeSStefan Hajnoczi AioContext *aio_context;
17813ea67e08SSergio Lopez int ret;
1782bd8baecdSFam Zheng
17838187f63cSVladimir Sementsov-Ogievskiy tran_add(tran, &blockdev_backup_drv, state);
17848187f63cSVladimir Sementsov-Ogievskiy
1785930fe17fSVladimir Sementsov-Ogievskiy bs = bdrv_lookup_bs(backup->device, backup->device, errp);
1786cef34eebSKevin Wolf if (!bs) {
1787ff52bf36SFam Zheng return;
1788ff52bf36SFam Zheng }
1789ff52bf36SFam Zheng
17905b7bfe51SSergio Lopez target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
17915b7bfe51SSergio Lopez if (!target_bs) {
1792bd8baecdSFam Zheng return;
1793bd8baecdSFam Zheng }
1794bd8baecdSFam Zheng
1795142e6907SEmanuele Giuseppe Esposito /* Honor bdrv_try_change_aio_context() context acquisition requirements. */
1796edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs);
17973ea67e08SSergio Lopez
1798142e6907SEmanuele Giuseppe Esposito ret = bdrv_try_change_aio_context(target_bs, aio_context, NULL, errp);
17993ea67e08SSergio Lopez if (ret < 0) {
18003ea67e08SSergio Lopez return;
18013ea67e08SSergio Lopez }
18023ea67e08SSergio Lopez
1803cef34eebSKevin Wolf state->bs = bs;
1804edd5adeeSStefan Hajnoczi
1805edd5adeeSStefan Hajnoczi /* Paired with .clean() */
1806ff52bf36SFam Zheng bdrv_drained_begin(state->bs);
1807bd8baecdSFam Zheng
18085b7bfe51SSergio Lopez state->job = do_backup_common(qapi_BlockdevBackup_base(backup),
18095b7bfe51SSergio Lopez bs, target_bs, aio_context,
1810d53c89aeSVladimir Sementsov-Ogievskiy block_job_txn, errp);
1811111049a4SJohn Snow }
1812bd8baecdSFam Zheng
blockdev_backup_commit(void * opaque)18138187f63cSVladimir Sementsov-Ogievskiy static void blockdev_backup_commit(void *opaque)
1814111049a4SJohn Snow {
18158187f63cSVladimir Sementsov-Ogievskiy BlockdevBackupState *state = opaque;
1816edd5adeeSStefan Hajnoczi
1817111049a4SJohn Snow assert(state->job);
1818da01ff7fSKevin Wolf job_start(&state->job->job);
1819bd8baecdSFam Zheng }
1820bd8baecdSFam Zheng
blockdev_backup_abort(void * opaque)18218187f63cSVladimir Sementsov-Ogievskiy static void blockdev_backup_abort(void *opaque)
1822bd8baecdSFam Zheng {
18238187f63cSVladimir Sementsov-Ogievskiy BlockdevBackupState *state = opaque;
1824bd8baecdSFam Zheng
1825111049a4SJohn Snow if (state->job) {
18264cfb3f05SHanna Reitz job_cancel_sync(&state->job->job, true);
1827bd8baecdSFam Zheng }
1828bd8baecdSFam Zheng }
1829bd8baecdSFam Zheng
blockdev_backup_clean(void * opaque)18308187f63cSVladimir Sementsov-Ogievskiy static void blockdev_backup_clean(void *opaque)
1831bd8baecdSFam Zheng {
18328187f63cSVladimir Sementsov-Ogievskiy g_autofree BlockdevBackupState *state = opaque;
1833bd8baecdSFam Zheng
1834edd5adeeSStefan Hajnoczi if (!state->bs) {
1835edd5adeeSStefan Hajnoczi return;
1836bd8baecdSFam Zheng }
1837edd5adeeSStefan Hajnoczi
1838edd5adeeSStefan Hajnoczi bdrv_drained_end(state->bs);
1839bd8baecdSFam Zheng }
1840bd8baecdSFam Zheng
1841df9a681dSFam Zheng typedef struct BlockDirtyBitmapState {
1842df9a681dSFam Zheng BdrvDirtyBitmap *bitmap;
1843df9a681dSFam Zheng BlockDriverState *bs;
1844df9a681dSFam Zheng HBitmap *backup;
1845c6490447SVladimir Sementsov-Ogievskiy bool was_enabled;
1846df9a681dSFam Zheng } BlockDirtyBitmapState;
1847df9a681dSFam Zheng
18488187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_add_abort(void *opaque);
18498187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv block_dirty_bitmap_add_drv = {
18508187f63cSVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_add_abort,
18518187f63cSVladimir Sementsov-Ogievskiy .clean = g_free,
18528187f63cSVladimir Sementsov-Ogievskiy };
18538187f63cSVladimir Sementsov-Ogievskiy
block_dirty_bitmap_add_action(BlockDirtyBitmapAdd * action,Transaction * tran,Error ** errp)1854d53c89aeSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_add_action(BlockDirtyBitmapAdd *action,
18558187f63cSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
1856df9a681dSFam Zheng {
1857df9a681dSFam Zheng Error *local_err = NULL;
1858d53c89aeSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = g_new0(BlockDirtyBitmapState, 1);
1859df9a681dSFam Zheng
18608187f63cSVladimir Sementsov-Ogievskiy tran_add(tran, &block_dirty_bitmap_add_drv, state);
18618187f63cSVladimir Sementsov-Ogievskiy
1862df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1863df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name,
1864df9a681dSFam Zheng action->has_granularity, action->granularity,
1865fd5ae4ccSVladimir Sementsov-Ogievskiy action->has_persistent, action->persistent,
18660e2b7f09SJohn Snow action->has_disabled, action->disabled,
1867df9a681dSFam Zheng &local_err);
1868df9a681dSFam Zheng
1869df9a681dSFam Zheng if (!local_err) {
1870c85feafaSVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, action->name,
1871c85feafaSVladimir Sementsov-Ogievskiy NULL, &error_abort);
1872df9a681dSFam Zheng } else {
1873df9a681dSFam Zheng error_propagate(errp, local_err);
1874df9a681dSFam Zheng }
1875df9a681dSFam Zheng }
1876df9a681dSFam Zheng
block_dirty_bitmap_add_abort(void * opaque)18778187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_add_abort(void *opaque)
1878df9a681dSFam Zheng {
18798187f63cSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = opaque;
1880df9a681dSFam Zheng
1881c85feafaSVladimir Sementsov-Ogievskiy if (state->bitmap) {
1882c85feafaSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(state->bitmap);
1883df9a681dSFam Zheng }
1884df9a681dSFam Zheng }
1885df9a681dSFam Zheng
18868187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_restore(void *opaque);
18878187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_free_backup(void *opaque);
18888187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv block_dirty_bitmap_clear_drv = {
18898187f63cSVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_restore,
18908187f63cSVladimir Sementsov-Ogievskiy .commit = block_dirty_bitmap_free_backup,
18918187f63cSVladimir Sementsov-Ogievskiy .clean = g_free,
18928187f63cSVladimir Sementsov-Ogievskiy };
18938187f63cSVladimir Sementsov-Ogievskiy
block_dirty_bitmap_clear_action(BlockDirtyBitmap * action,Transaction * tran,Error ** errp)1894d53c89aeSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_clear_action(BlockDirtyBitmap *action,
18958187f63cSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
1896df9a681dSFam Zheng {
1897d53c89aeSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = g_new0(BlockDirtyBitmapState, 1);
1898df9a681dSFam Zheng
18998187f63cSVladimir Sementsov-Ogievskiy tran_add(tran, &block_dirty_bitmap_clear_drv, state);
19008187f63cSVladimir Sementsov-Ogievskiy
1901df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node,
1902df9a681dSFam Zheng action->name,
1903df9a681dSFam Zheng &state->bs,
1904df9a681dSFam Zheng errp);
1905df9a681dSFam Zheng if (!state->bitmap) {
1906df9a681dSFam Zheng return;
1907df9a681dSFam Zheng }
1908df9a681dSFam Zheng
19093ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) {
1910d6883bc9SVladimir Sementsov-Ogievskiy return;
1911df9a681dSFam Zheng }
1912df9a681dSFam Zheng
1913df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
1914df9a681dSFam Zheng }
1915df9a681dSFam Zheng
block_dirty_bitmap_restore(void * opaque)19168187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_restore(void *opaque)
1917df9a681dSFam Zheng {
19188187f63cSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = opaque;
1919df9a681dSFam Zheng
1920184dd9c4SJohn Snow if (state->backup) {
192156bd6624SVladimir Sementsov-Ogievskiy bdrv_restore_dirty_bitmap(state->bitmap, state->backup);
1922df9a681dSFam Zheng }
1923184dd9c4SJohn Snow }
1924df9a681dSFam Zheng
block_dirty_bitmap_free_backup(void * opaque)19258187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_free_backup(void *opaque)
1926df9a681dSFam Zheng {
19278187f63cSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = opaque;
1928df9a681dSFam Zheng
1929df9a681dSFam Zheng hbitmap_free(state->backup);
1930df9a681dSFam Zheng }
1931df9a681dSFam Zheng
19328187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_abort(void *opaque);
19338187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv block_dirty_bitmap_enable_drv = {
19348187f63cSVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_enable_abort,
19358187f63cSVladimir Sementsov-Ogievskiy .clean = g_free,
19368187f63cSVladimir Sementsov-Ogievskiy };
19378187f63cSVladimir Sementsov-Ogievskiy
block_dirty_bitmap_enable_action(BlockDirtyBitmap * action,Transaction * tran,Error ** errp)1938d53c89aeSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_action(BlockDirtyBitmap *action,
19398187f63cSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
1940c6490447SVladimir Sementsov-Ogievskiy {
1941d53c89aeSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = g_new0(BlockDirtyBitmapState, 1);
1942c6490447SVladimir Sementsov-Ogievskiy
19438187f63cSVladimir Sementsov-Ogievskiy tran_add(tran, &block_dirty_bitmap_enable_drv, state);
19448187f63cSVladimir Sementsov-Ogievskiy
1945c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node,
1946c6490447SVladimir Sementsov-Ogievskiy action->name,
1947c6490447SVladimir Sementsov-Ogievskiy NULL,
1948c6490447SVladimir Sementsov-Ogievskiy errp);
1949c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) {
1950c6490447SVladimir Sementsov-Ogievskiy return;
1951c6490447SVladimir Sementsov-Ogievskiy }
1952c6490447SVladimir Sementsov-Ogievskiy
19533ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
1954b053bb55SJohn Snow return;
1955b053bb55SJohn Snow }
1956b053bb55SJohn Snow
1957c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
1958c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap);
1959c6490447SVladimir Sementsov-Ogievskiy }
1960c6490447SVladimir Sementsov-Ogievskiy
block_dirty_bitmap_enable_abort(void * opaque)19618187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_abort(void *opaque)
1962c6490447SVladimir Sementsov-Ogievskiy {
19638187f63cSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = opaque;
1964c6490447SVladimir Sementsov-Ogievskiy
1965c6490447SVladimir Sementsov-Ogievskiy if (!state->was_enabled) {
1966c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap);
1967c6490447SVladimir Sementsov-Ogievskiy }
1968c6490447SVladimir Sementsov-Ogievskiy }
1969c6490447SVladimir Sementsov-Ogievskiy
19708187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_abort(void *opaque);
19718187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv block_dirty_bitmap_disable_drv = {
19728187f63cSVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_disable_abort,
19738187f63cSVladimir Sementsov-Ogievskiy .clean = g_free,
19748187f63cSVladimir Sementsov-Ogievskiy };
19758187f63cSVladimir Sementsov-Ogievskiy
block_dirty_bitmap_disable_action(BlockDirtyBitmap * action,Transaction * tran,Error ** errp)1976d53c89aeSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_action(BlockDirtyBitmap *action,
19778187f63cSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
1978c6490447SVladimir Sementsov-Ogievskiy {
1979d53c89aeSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = g_new0(BlockDirtyBitmapState, 1);
1980c6490447SVladimir Sementsov-Ogievskiy
19818187f63cSVladimir Sementsov-Ogievskiy tran_add(tran, &block_dirty_bitmap_disable_drv, state);
19828187f63cSVladimir Sementsov-Ogievskiy
1983c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node,
1984c6490447SVladimir Sementsov-Ogievskiy action->name,
1985c6490447SVladimir Sementsov-Ogievskiy NULL,
1986c6490447SVladimir Sementsov-Ogievskiy errp);
1987c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) {
1988c6490447SVladimir Sementsov-Ogievskiy return;
1989c6490447SVladimir Sementsov-Ogievskiy }
1990c6490447SVladimir Sementsov-Ogievskiy
19913ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
1992b053bb55SJohn Snow return;
1993b053bb55SJohn Snow }
1994b053bb55SJohn Snow
1995c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
1996c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap);
1997c6490447SVladimir Sementsov-Ogievskiy }
1998c6490447SVladimir Sementsov-Ogievskiy
block_dirty_bitmap_disable_abort(void * opaque)19998187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_abort(void *opaque)
2000c6490447SVladimir Sementsov-Ogievskiy {
20018187f63cSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = opaque;
2002c6490447SVladimir Sementsov-Ogievskiy
2003c6490447SVladimir Sementsov-Ogievskiy if (state->was_enabled) {
2004c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap);
2005c6490447SVladimir Sementsov-Ogievskiy }
2006c6490447SVladimir Sementsov-Ogievskiy }
2007c6490447SVladimir Sementsov-Ogievskiy
20088187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv block_dirty_bitmap_merge_drv = {
20098187f63cSVladimir Sementsov-Ogievskiy .commit = block_dirty_bitmap_free_backup,
20108187f63cSVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_restore,
20118187f63cSVladimir Sementsov-Ogievskiy .clean = g_free,
20128187f63cSVladimir Sementsov-Ogievskiy };
20138187f63cSVladimir Sementsov-Ogievskiy
block_dirty_bitmap_merge_action(BlockDirtyBitmapMerge * action,Transaction * tran,Error ** errp)2014d53c89aeSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_merge_action(BlockDirtyBitmapMerge *action,
20158187f63cSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
20166fd2e407SVladimir Sementsov-Ogievskiy {
2017d53c89aeSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = g_new0(BlockDirtyBitmapState, 1);
20186fd2e407SVladimir Sementsov-Ogievskiy
20198187f63cSVladimir Sementsov-Ogievskiy tran_add(tran, &block_dirty_bitmap_merge_drv, state);
20208187f63cSVladimir Sementsov-Ogievskiy
2021c6996cf9SEric Blake state->bitmap = block_dirty_bitmap_merge(action->node, action->target,
2022360d4e4eSJohn Snow action->bitmaps, &state->backup,
20236fd2e407SVladimir Sementsov-Ogievskiy errp);
20246fd2e407SVladimir Sementsov-Ogievskiy }
20256fd2e407SVladimir Sementsov-Ogievskiy
20268187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_remove_commit(void *opaque);
20278187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_remove_abort(void *opaque);
20288187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv block_dirty_bitmap_remove_drv = {
20298187f63cSVladimir Sementsov-Ogievskiy .commit = block_dirty_bitmap_remove_commit,
20308187f63cSVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_remove_abort,
20318187f63cSVladimir Sementsov-Ogievskiy .clean = g_free,
20328187f63cSVladimir Sementsov-Ogievskiy };
20338187f63cSVladimir Sementsov-Ogievskiy
block_dirty_bitmap_remove_action(BlockDirtyBitmap * action,Transaction * tran,Error ** errp)2034d53c89aeSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_remove_action(BlockDirtyBitmap *action,
20358187f63cSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
2036c4e4b0faSJohn Snow {
2037d53c89aeSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = g_new0(BlockDirtyBitmapState, 1);
2038c4e4b0faSJohn Snow
20398187f63cSVladimir Sementsov-Ogievskiy tran_add(tran, &block_dirty_bitmap_remove_drv, state);
20408187f63cSVladimir Sementsov-Ogievskiy
2041c4e4b0faSJohn Snow
2042c6996cf9SEric Blake state->bitmap = block_dirty_bitmap_remove(action->node, action->name,
2043c4e4b0faSJohn Snow false, &state->bs, errp);
2044c4e4b0faSJohn Snow if (state->bitmap) {
2045c4e4b0faSJohn Snow bdrv_dirty_bitmap_skip_store(state->bitmap, true);
2046c4e4b0faSJohn Snow bdrv_dirty_bitmap_set_busy(state->bitmap, true);
2047c4e4b0faSJohn Snow }
2048c4e4b0faSJohn Snow }
2049c4e4b0faSJohn Snow
block_dirty_bitmap_remove_abort(void * opaque)20508187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_remove_abort(void *opaque)
2051c4e4b0faSJohn Snow {
20528187f63cSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = opaque;
2053c4e4b0faSJohn Snow
2054c4e4b0faSJohn Snow if (state->bitmap) {
2055c4e4b0faSJohn Snow bdrv_dirty_bitmap_skip_store(state->bitmap, false);
2056c4e4b0faSJohn Snow bdrv_dirty_bitmap_set_busy(state->bitmap, false);
2057c4e4b0faSJohn Snow }
2058c4e4b0faSJohn Snow }
2059c4e4b0faSJohn Snow
block_dirty_bitmap_remove_commit(void * opaque)20608187f63cSVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_remove_commit(void *opaque)
2061c4e4b0faSJohn Snow {
20628187f63cSVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = opaque;
2063c4e4b0faSJohn Snow
2064c4e4b0faSJohn Snow bdrv_dirty_bitmap_set_busy(state->bitmap, false);
20655deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(state->bitmap);
2066c4e4b0faSJohn Snow }
2067c4e4b0faSJohn Snow
20688187f63cSVladimir Sementsov-Ogievskiy static void abort_commit(void *opaque);
20698187f63cSVladimir Sementsov-Ogievskiy TransactionActionDrv abort_drv = {
20708187f63cSVladimir Sementsov-Ogievskiy .commit = abort_commit,
20718187f63cSVladimir Sementsov-Ogievskiy };
20728187f63cSVladimir Sementsov-Ogievskiy
abort_action(Transaction * tran,Error ** errp)2073d53c89aeSVladimir Sementsov-Ogievskiy static void abort_action(Transaction *tran, Error **errp)
207478b18b78SStefan Hajnoczi {
2075d53c89aeSVladimir Sementsov-Ogievskiy tran_add(tran, &abort_drv, NULL);
207678b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action");
207778b18b78SStefan Hajnoczi }
207878b18b78SStefan Hajnoczi
abort_commit(void * opaque)20798187f63cSVladimir Sementsov-Ogievskiy static void abort_commit(void *opaque)
208078b18b78SStefan Hajnoczi {
2081dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */
208278b18b78SStefan Hajnoczi }
208378b18b78SStefan Hajnoczi
transaction_action(TransactionAction * act,JobTxn * block_job_txn,Transaction * tran,Error ** errp)2084d53c89aeSVladimir Sementsov-Ogievskiy static void transaction_action(TransactionAction *act, JobTxn *block_job_txn,
2085d53c89aeSVladimir Sementsov-Ogievskiy Transaction *tran, Error **errp)
2086d53c89aeSVladimir Sementsov-Ogievskiy {
2087d53c89aeSVladimir Sementsov-Ogievskiy switch (act->type) {
2088d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
2089d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
2090d53c89aeSVladimir Sementsov-Ogievskiy external_snapshot_action(act, tran, errp);
2091d53c89aeSVladimir Sementsov-Ogievskiy return;
2092d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_DRIVE_BACKUP:
2093d53c89aeSVladimir Sementsov-Ogievskiy drive_backup_action(act->u.drive_backup.data,
2094d53c89aeSVladimir Sementsov-Ogievskiy block_job_txn, tran, errp);
2095d53c89aeSVladimir Sementsov-Ogievskiy return;
2096d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP:
2097d53c89aeSVladimir Sementsov-Ogievskiy blockdev_backup_action(act->u.blockdev_backup.data,
2098d53c89aeSVladimir Sementsov-Ogievskiy block_job_txn, tran, errp);
2099d53c89aeSVladimir Sementsov-Ogievskiy return;
2100d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_ABORT:
2101d53c89aeSVladimir Sementsov-Ogievskiy abort_action(tran, errp);
2102d53c89aeSVladimir Sementsov-Ogievskiy return;
2103d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC:
2104d53c89aeSVladimir Sementsov-Ogievskiy internal_snapshot_action(act->u.blockdev_snapshot_internal_sync.data,
2105d53c89aeSVladimir Sementsov-Ogievskiy tran, errp);
2106d53c89aeSVladimir Sementsov-Ogievskiy return;
2107d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD:
2108d53c89aeSVladimir Sementsov-Ogievskiy block_dirty_bitmap_add_action(act->u.block_dirty_bitmap_add.data,
2109d53c89aeSVladimir Sementsov-Ogievskiy tran, errp);
2110d53c89aeSVladimir Sementsov-Ogievskiy return;
2111d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR:
2112d53c89aeSVladimir Sementsov-Ogievskiy block_dirty_bitmap_clear_action(act->u.block_dirty_bitmap_clear.data,
2113d53c89aeSVladimir Sementsov-Ogievskiy tran, errp);
2114d53c89aeSVladimir Sementsov-Ogievskiy return;
2115d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE:
2116d53c89aeSVladimir Sementsov-Ogievskiy block_dirty_bitmap_enable_action(act->u.block_dirty_bitmap_enable.data,
2117d53c89aeSVladimir Sementsov-Ogievskiy tran, errp);
2118d53c89aeSVladimir Sementsov-Ogievskiy return;
2119d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE:
2120d53c89aeSVladimir Sementsov-Ogievskiy block_dirty_bitmap_disable_action(
2121d53c89aeSVladimir Sementsov-Ogievskiy act->u.block_dirty_bitmap_disable.data, tran, errp);
2122d53c89aeSVladimir Sementsov-Ogievskiy return;
2123d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE:
2124d53c89aeSVladimir Sementsov-Ogievskiy block_dirty_bitmap_merge_action(act->u.block_dirty_bitmap_merge.data,
2125d53c89aeSVladimir Sementsov-Ogievskiy tran, errp);
2126d53c89aeSVladimir Sementsov-Ogievskiy return;
2127d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE:
2128d53c89aeSVladimir Sementsov-Ogievskiy block_dirty_bitmap_remove_action(act->u.block_dirty_bitmap_remove.data,
2129d53c89aeSVladimir Sementsov-Ogievskiy tran, errp);
2130d53c89aeSVladimir Sementsov-Ogievskiy return;
2131d53c89aeSVladimir Sementsov-Ogievskiy /*
2132d53c89aeSVladimir Sementsov-Ogievskiy * Where are transactions for MIRROR, COMMIT and STREAM?
213366da04ddSJohn Snow * Although these blockjobs use transaction callbacks like the backup job,
213466da04ddSJohn Snow * these jobs do not necessarily adhere to transaction semantics.
213566da04ddSJohn Snow * These jobs may not fully undo all of their actions on abort, nor do they
213666da04ddSJohn Snow * necessarily work in transactions with more than one job in them.
213766da04ddSJohn Snow */
2138d53c89aeSVladimir Sementsov-Ogievskiy case TRANSACTION_ACTION_KIND__MAX:
2139d53c89aeSVladimir Sementsov-Ogievskiy default:
2140d53c89aeSVladimir Sementsov-Ogievskiy g_assert_not_reached();
2141ba0c86a3SWenchao Xia };
2142d53c89aeSVladimir Sementsov-Ogievskiy }
2143d53c89aeSVladimir Sementsov-Ogievskiy
2144ba0c86a3SWenchao Xia
21458802d1fdSJeff Cody /*
2146b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if
2147b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group.
2148ebc2752bSEmanuele Giuseppe Esposito *
2149ebc2752bSEmanuele Giuseppe Esposito * Always run under BQL.
21508802d1fdSJeff Cody */
qmp_transaction(TransactionActionList * actions,struct TransactionProperties * properties,Error ** errp)215124039696SVladimir Sementsov-Ogievskiy void qmp_transaction(TransactionActionList *actions,
215224039696SVladimir Sementsov-Ogievskiy struct TransactionProperties *properties,
215394d16a64SJohn Snow Error **errp)
21548802d1fdSJeff Cody {
215530c96b55SVladimir Sementsov-Ogievskiy TransactionActionList *act;
215662c9e416SKevin Wolf JobTxn *block_job_txn = NULL;
215743e17041SLuiz Capitulino Error *local_err = NULL;
2158c85f34cfSVladimir Sementsov-Ogievskiy Transaction *tran;
2159c85f34cfSVladimir Sementsov-Ogievskiy ActionCompletionMode comp_mode =
2160c85f34cfSVladimir Sementsov-Ogievskiy properties ? properties->completion_mode :
2161c85f34cfSVladimir Sementsov-Ogievskiy ACTION_COMPLETION_MODE_INDIVIDUAL;
21628802d1fdSJeff Cody
2163b4ad82aaSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
2164b4ad82aaSEmanuele Giuseppe Esposito
216594d16a64SJohn Snow /* Does this transaction get canceled as a group on failure?
216662c9e416SKevin Wolf * If not, we don't really need to make a JobTxn.
216794d16a64SJohn Snow */
2168c85f34cfSVladimir Sementsov-Ogievskiy if (comp_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2169c85f34cfSVladimir Sementsov-Ogievskiy for (act = actions; act; act = act->next) {
2170c85f34cfSVladimir Sementsov-Ogievskiy TransactionActionKind type = act->value->type;
2171c85f34cfSVladimir Sementsov-Ogievskiy
2172c85f34cfSVladimir Sementsov-Ogievskiy if (type != TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP &&
2173c85f34cfSVladimir Sementsov-Ogievskiy type != TRANSACTION_ACTION_KIND_DRIVE_BACKUP)
2174c85f34cfSVladimir Sementsov-Ogievskiy {
2175c85f34cfSVladimir Sementsov-Ogievskiy error_setg(errp,
2176c85f34cfSVladimir Sementsov-Ogievskiy "Action '%s' does not support transaction property "
2177c85f34cfSVladimir Sementsov-Ogievskiy "completion-mode = %s",
2178c85f34cfSVladimir Sementsov-Ogievskiy TransactionActionKind_str(type),
2179c85f34cfSVladimir Sementsov-Ogievskiy ActionCompletionMode_str(comp_mode));
2180c85f34cfSVladimir Sementsov-Ogievskiy return;
2181c85f34cfSVladimir Sementsov-Ogievskiy }
2182c85f34cfSVladimir Sementsov-Ogievskiy }
2183c85f34cfSVladimir Sementsov-Ogievskiy
21847eaa8fb5SKevin Wolf block_job_txn = job_txn_new();
218594d16a64SJohn Snow }
218694d16a64SJohn Snow
2187b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */
21888802d1fdSJeff Cody bdrv_drain_all();
21898802d1fdSJeff Cody
2190c85f34cfSVladimir Sementsov-Ogievskiy tran = tran_new();
2191c85f34cfSVladimir Sementsov-Ogievskiy
2192b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */
219330c96b55SVladimir Sementsov-Ogievskiy for (act = actions; act; act = act->next) {
2194d53c89aeSVladimir Sementsov-Ogievskiy transaction_action(act->value, block_job_txn, tran, &local_err);
219584d18f06SMarkus Armbruster if (local_err) {
21969b9877eeSWenchao Xia error_propagate(errp, local_err);
21979b9877eeSWenchao Xia goto delete_and_fail;
21989b9877eeSWenchao Xia }
219952e7c241SPaolo Bonzini }
22008802d1fdSJeff Cody
22018187f63cSVladimir Sementsov-Ogievskiy tran_commit(tran);
22028802d1fdSJeff Cody
22038802d1fdSJeff Cody /* success */
22048802d1fdSJeff Cody goto exit;
22058802d1fdSJeff Cody
22068802d1fdSJeff Cody delete_and_fail:
2207b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */
22088187f63cSVladimir Sementsov-Ogievskiy tran_abort(tran);
22098802d1fdSJeff Cody exit:
22107eaa8fb5SKevin Wolf job_txn_unref(block_job_txn);
22118802d1fdSJeff Cody }
22128802d1fdSJeff Cody
qmp_x_debug_block_dirty_bitmap_sha256(const char * node,const char * name,Error ** errp)2213a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
2214a3b52535SVladimir Sementsov-Ogievskiy const char *name,
2215a3b52535SVladimir Sementsov-Ogievskiy Error **errp)
2216a3b52535SVladimir Sementsov-Ogievskiy {
2217a3b52535SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap;
2218a3b52535SVladimir Sementsov-Ogievskiy BlockDriverState *bs;
2219a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *ret = NULL;
2220a3b52535SVladimir Sementsov-Ogievskiy char *sha256;
2221a3b52535SVladimir Sementsov-Ogievskiy
2222a3b52535SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
2223a3b52535SVladimir Sementsov-Ogievskiy if (!bitmap || !bs) {
2224a3b52535SVladimir Sementsov-Ogievskiy return NULL;
2225a3b52535SVladimir Sementsov-Ogievskiy }
2226a3b52535SVladimir Sementsov-Ogievskiy
2227a3b52535SVladimir Sementsov-Ogievskiy sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp);
2228a3b52535SVladimir Sementsov-Ogievskiy if (sha256 == NULL) {
2229a3b52535SVladimir Sementsov-Ogievskiy return NULL;
2230a3b52535SVladimir Sementsov-Ogievskiy }
2231a3b52535SVladimir Sementsov-Ogievskiy
2232a3b52535SVladimir Sementsov-Ogievskiy ret = g_new(BlockDirtyBitmapSha256, 1);
2233a3b52535SVladimir Sementsov-Ogievskiy ret->sha256 = sha256;
2234a3b52535SVladimir Sementsov-Ogievskiy
2235a3b52535SVladimir Sementsov-Ogievskiy return ret;
2236a3b52535SVladimir Sementsov-Ogievskiy }
2237a3b52535SVladimir Sementsov-Ogievskiy
qmp_block_resize(const char * device,const char * node_name,int64_t size,Error ** errp)223854fde4ffSMarkus Armbruster void coroutine_fn qmp_block_resize(const char *device, const char *node_name,
22393b1dbd11SBenoît Canet int64_t size, Error **errp)
22406d4a2b3aSChristoph Hellwig {
22413b1dbd11SBenoît Canet Error *local_err = NULL;
2242d9dbf25fSKevin Wolf BlockBackend *blk;
22436d4a2b3aSChristoph Hellwig BlockDriverState *bs;
2244eb94b81aSKevin Wolf AioContext *old_ctx;
22456d4a2b3aSChristoph Hellwig
224654fde4ffSMarkus Armbruster bs = bdrv_lookup_bs(device, node_name, &local_err);
224784d18f06SMarkus Armbruster if (local_err) {
22483b1dbd11SBenoît Canet error_propagate(errp, local_err);
22493b1dbd11SBenoît Canet return;
22503b1dbd11SBenoît Canet }
22513b1dbd11SBenoît Canet
22526d4a2b3aSChristoph Hellwig if (size < 0) {
2253c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2254d9dbf25fSKevin Wolf return;
22556d4a2b3aSChristoph Hellwig }
22566d4a2b3aSChristoph Hellwig
2257277f2007SKevin Wolf bdrv_graph_co_rdlock();
22589fe07533SMarkus Armbruster if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, errp)) {
2259277f2007SKevin Wolf bdrv_graph_co_rdunlock();
2260d9dbf25fSKevin Wolf return;
22619c75e168SJeff Cody }
2262277f2007SKevin Wolf bdrv_graph_co_rdunlock();
22639c75e168SJeff Cody
22640c7d204fSKevin Wolf blk = blk_co_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL, errp);
2265a3aeeab5SEric Blake if (!blk) {
2266d9dbf25fSKevin Wolf return;
2267d7086422SKevin Wolf }
22687dad9ee6SKevin Wolf
2269698bdfa0SJohn Snow bdrv_drained_begin(bs);
22708089eab2SKevin Wolf
2271eb94b81aSKevin Wolf old_ctx = bdrv_co_enter(bs);
2272247cf5c0SAlberto Faria blk_co_truncate(blk, size, false, PREALLOC_MODE_OFF, 0, errp);
2273eb94b81aSKevin Wolf bdrv_co_leave(bs, old_ctx);
2274927e0e76SStefan Hajnoczi
22758089eab2SKevin Wolf bdrv_drained_end(bs);
2276755ae381SKevin Wolf blk_co_unref(blk);
22776d4a2b3aSChristoph Hellwig }
227812bd451fSStefan Hajnoczi
qmp_block_stream(const char * job_id,const char * device,const char * base,const char * base_node,const char * backing_file,bool has_backing_mask_protocol,bool backing_mask_protocol,const char * bottom,bool has_speed,int64_t speed,bool has_on_error,BlockdevOnError on_error,const char * filter_node_name,bool has_auto_finalize,bool auto_finalize,bool has_auto_dismiss,bool auto_dismiss,Error ** errp)227954fde4ffSMarkus Armbruster void qmp_block_stream(const char *job_id, const char *device,
228054fde4ffSMarkus Armbruster const char *base,
228154fde4ffSMarkus Armbruster const char *base_node,
228254fde4ffSMarkus Armbruster const char *backing_file,
228372098a3aSPeter Krempa bool has_backing_mask_protocol,
228472098a3aSPeter Krempa bool backing_mask_protocol,
228554fde4ffSMarkus Armbruster const char *bottom,
228613d8cc51SJeff Cody bool has_speed, int64_t speed,
22871d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error,
228854fde4ffSMarkus Armbruster const char *filter_node_name,
2289241ca1abSJohn Snow bool has_auto_finalize, bool auto_finalize,
2290241ca1abSJohn Snow bool has_auto_dismiss, bool auto_dismiss,
22911d809098SPaolo Bonzini Error **errp)
229212bd451fSStefan Hajnoczi {
22937f4a396dSVladimir Sementsov-Ogievskiy BlockDriverState *bs, *iter, *iter_end;
2294c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL;
22957f4a396dSVladimir Sementsov-Ogievskiy BlockDriverState *bottom_bs = NULL;
2296f3e69bebSStefan Hajnoczi AioContext *aio_context;
2297fd7f8c65SStefan Hajnoczi Error *local_err = NULL;
2298cf6320dfSJohn Snow int job_flags = JOB_DEFAULT;
229912bd451fSStefan Hajnoczi
2300b7cfc7d5SKevin Wolf GLOBAL_STATE_CODE();
2301b7cfc7d5SKevin Wolf
230254fde4ffSMarkus Armbruster if (base && base_node) {
23037f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "'base' and 'base-node' cannot be specified "
23047f4a396dSVladimir Sementsov-Ogievskiy "at the same time");
23057f4a396dSVladimir Sementsov-Ogievskiy return;
23067f4a396dSVladimir Sementsov-Ogievskiy }
23077f4a396dSVladimir Sementsov-Ogievskiy
230854fde4ffSMarkus Armbruster if (base && bottom) {
23097f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "'base' and 'bottom' cannot be specified "
23107f4a396dSVladimir Sementsov-Ogievskiy "at the same time");
23117f4a396dSVladimir Sementsov-Ogievskiy return;
23127f4a396dSVladimir Sementsov-Ogievskiy }
23137f4a396dSVladimir Sementsov-Ogievskiy
231454fde4ffSMarkus Armbruster if (bottom && base_node) {
23157f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "'bottom' and 'base-node' cannot be specified "
23167f4a396dSVladimir Sementsov-Ogievskiy "at the same time");
23177f4a396dSVladimir Sementsov-Ogievskiy return;
23187f4a396dSVladimir Sementsov-Ogievskiy }
23197f4a396dSVladimir Sementsov-Ogievskiy
232072098a3aSPeter Krempa if (!has_backing_mask_protocol) {
232172098a3aSPeter Krempa backing_mask_protocol = false;
232272098a3aSPeter Krempa }
232372098a3aSPeter Krempa
23241d809098SPaolo Bonzini if (!has_on_error) {
23251d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT;
23261d809098SPaolo Bonzini }
23271d809098SPaolo Bonzini
2328554b6147SAlberto Garcia bs = bdrv_lookup_bs(device, device, errp);
2329b6c1bae5SKevin Wolf if (!bs) {
233012bd451fSStefan Hajnoczi return;
233112bd451fSStefan Hajnoczi }
233212bd451fSStefan Hajnoczi
2333b6c1bae5SKevin Wolf aio_context = bdrv_get_aio_context(bs);
2334f3e69bebSStefan Hajnoczi
233579bb7627SKevin Wolf bdrv_graph_rdlock_main_loop();
233654fde4ffSMarkus Armbruster if (base) {
2337c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base);
2338c8c3080fSMarcelo Tosatti if (base_bs == NULL) {
2339b0d7be2aSMarkus Armbruster error_setg(errp, "Can't find '%s' in the backing chain", base);
234079bb7627SKevin Wolf goto out_rdlock;
234112bd451fSStefan Hajnoczi }
2342f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context);
2343c8c3080fSMarcelo Tosatti }
234412bd451fSStefan Hajnoczi
234554fde4ffSMarkus Armbruster if (base_node) {
2346312fe09cSAlberto Garcia base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2347312fe09cSAlberto Garcia if (!base_bs) {
234879bb7627SKevin Wolf goto out_rdlock;
2349312fe09cSAlberto Garcia }
2350312fe09cSAlberto Garcia if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
2351312fe09cSAlberto Garcia error_setg(errp, "Node '%s' is not a backing image of '%s'",
2352312fe09cSAlberto Garcia base_node, device);
235379bb7627SKevin Wolf goto out_rdlock;
2354312fe09cSAlberto Garcia }
2355312fe09cSAlberto Garcia assert(bdrv_get_aio_context(base_bs) == aio_context);
2356b7cfc7d5SKevin Wolf
2357f30c66baSMax Reitz bdrv_refresh_filename(base_bs);
2358312fe09cSAlberto Garcia }
2359312fe09cSAlberto Garcia
236054fde4ffSMarkus Armbruster if (bottom) {
23617f4a396dSVladimir Sementsov-Ogievskiy bottom_bs = bdrv_lookup_bs(NULL, bottom, errp);
23627f4a396dSVladimir Sementsov-Ogievskiy if (!bottom_bs) {
236379bb7627SKevin Wolf goto out_rdlock;
23647f4a396dSVladimir Sementsov-Ogievskiy }
23657f4a396dSVladimir Sementsov-Ogievskiy if (!bottom_bs->drv) {
23667f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "Node '%s' is not open", bottom);
236779bb7627SKevin Wolf goto out_rdlock;
23687f4a396dSVladimir Sementsov-Ogievskiy }
23697f4a396dSVladimir Sementsov-Ogievskiy if (bottom_bs->drv->is_filter) {
23707f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "Node '%s' is a filter, use a non-filter node "
23717f4a396dSVladimir Sementsov-Ogievskiy "as 'bottom'", bottom);
237279bb7627SKevin Wolf goto out_rdlock;
23737f4a396dSVladimir Sementsov-Ogievskiy }
23747f4a396dSVladimir Sementsov-Ogievskiy if (!bdrv_chain_contains(bs, bottom_bs)) {
23757f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "Node '%s' is not in a chain starting from '%s'",
23767f4a396dSVladimir Sementsov-Ogievskiy bottom, device);
237779bb7627SKevin Wolf goto out_rdlock;
23787f4a396dSVladimir Sementsov-Ogievskiy }
23797f4a396dSVladimir Sementsov-Ogievskiy assert(bdrv_get_aio_context(bottom_bs) == aio_context);
23807f4a396dSVladimir Sementsov-Ogievskiy }
23817f4a396dSVladimir Sementsov-Ogievskiy
23827f4a396dSVladimir Sementsov-Ogievskiy /*
23837f4a396dSVladimir Sementsov-Ogievskiy * Check for op blockers in the whole chain between bs and base (or bottom)
23847f4a396dSVladimir Sementsov-Ogievskiy */
2385372b69f5SKevin Wolf iter_end = bottom ? bdrv_filter_or_cow_bs(bottom_bs) : base_bs;
23867f4a396dSVladimir Sementsov-Ogievskiy for (iter = bs; iter && iter != iter_end;
238767acfd21SMax Reitz iter = bdrv_filter_or_cow_bs(iter))
238867acfd21SMax Reitz {
2389554b6147SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
239079bb7627SKevin Wolf goto out_rdlock;
2391554b6147SAlberto Garcia }
2392554b6147SAlberto Garcia }
2393277f2007SKevin Wolf bdrv_graph_rdunlock_main_loop();
2394554b6147SAlberto Garcia
239513d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing
239613d8cc51SJeff Cody * file, and specifying one is therefore an error */
239754fde4ffSMarkus Armbruster if (!base_bs && backing_file) {
239813d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the "
239913d8cc51SJeff Cody "entire chain");
2400b49f4755SStefan Hajnoczi return;
240113d8cc51SJeff Cody }
240213d8cc51SJeff Cody
2403241ca1abSJohn Snow if (has_auto_finalize && !auto_finalize) {
2404241ca1abSJohn Snow job_flags |= JOB_MANUAL_FINALIZE;
2405241ca1abSJohn Snow }
2406241ca1abSJohn Snow if (has_auto_dismiss && !auto_dismiss) {
2407241ca1abSJohn Snow job_flags |= JOB_MANUAL_DISMISS;
2408241ca1abSJohn Snow }
2409241ca1abSJohn Snow
241054fde4ffSMarkus Armbruster stream_start(job_id, bs, base_bs, backing_file,
241172098a3aSPeter Krempa backing_mask_protocol,
24127f4a396dSVladimir Sementsov-Ogievskiy bottom_bs, job_flags, has_speed ? speed : 0, on_error,
2413880747a8SAndrey Shinkevich filter_node_name, &local_err);
241484d18f06SMarkus Armbruster if (local_err) {
2415fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err);
2416b49f4755SStefan Hajnoczi return;
241712bd451fSStefan Hajnoczi }
241812bd451fSStefan Hajnoczi
2419b23c580cSVladimir Sementsov-Ogievskiy trace_qmp_block_stream(bs);
242079bb7627SKevin Wolf return;
242179bb7627SKevin Wolf
242279bb7627SKevin Wolf out_rdlock:
242379bb7627SKevin Wolf bdrv_graph_rdunlock_main_loop();
242412bd451fSStefan Hajnoczi }
24252d47c6e9SStefan Hajnoczi
qmp_block_commit(const char * job_id,const char * device,const char * base_node,const char * base,const char * top_node,const char * top,const char * backing_file,bool has_backing_mask_protocol,bool backing_mask_protocol,bool has_speed,int64_t speed,bool has_on_error,BlockdevOnError on_error,const char * filter_node_name,bool has_auto_finalize,bool auto_finalize,bool has_auto_dismiss,bool auto_dismiss,Error ** errp)242654fde4ffSMarkus Armbruster void qmp_block_commit(const char *job_id, const char *device,
242754fde4ffSMarkus Armbruster const char *base_node,
242854fde4ffSMarkus Armbruster const char *base,
242954fde4ffSMarkus Armbruster const char *top_node,
243054fde4ffSMarkus Armbruster const char *top,
243154fde4ffSMarkus Armbruster const char *backing_file,
24324b028cbeSPeter Krempa bool has_backing_mask_protocol,
24334b028cbeSPeter Krempa bool backing_mask_protocol,
2434ed61fc10SJeff Cody bool has_speed, int64_t speed,
24358faad1c7SKevin Wolf bool has_on_error, BlockdevOnError on_error,
243654fde4ffSMarkus Armbruster const char *filter_node_name,
243796fbf534SJohn Snow bool has_auto_finalize, bool auto_finalize,
243896fbf534SJohn Snow bool has_auto_dismiss, bool auto_dismiss,
2439ed61fc10SJeff Cody Error **errp)
2440ed61fc10SJeff Cody {
2441ed61fc10SJeff Cody BlockDriverState *bs;
2442058223a6SAlberto Garcia BlockDriverState *iter;
2443ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs;
24449e85cd5cSStefan Hajnoczi AioContext *aio_context;
2445ed61fc10SJeff Cody Error *local_err = NULL;
24465360782dSJohn Snow int job_flags = JOB_DEFAULT;
244705ea385aSMax Reitz uint64_t top_perm, top_shared;
2448ed61fc10SJeff Cody
2449bce73bc2SKevin Wolf /* TODO We'll eventually have to take a writer lock in this function */
2450bce73bc2SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
2451bce73bc2SKevin Wolf
245254504663SMax Reitz if (!has_speed) {
245354504663SMax Reitz speed = 0;
245454504663SMax Reitz }
24558faad1c7SKevin Wolf if (!has_on_error) {
24568faad1c7SKevin Wolf on_error = BLOCKDEV_ON_ERROR_REPORT;
24578faad1c7SKevin Wolf }
245896fbf534SJohn Snow if (has_auto_finalize && !auto_finalize) {
245996fbf534SJohn Snow job_flags |= JOB_MANUAL_FINALIZE;
246096fbf534SJohn Snow }
246196fbf534SJohn Snow if (has_auto_dismiss && !auto_dismiss) {
246296fbf534SJohn Snow job_flags |= JOB_MANUAL_DISMISS;
246396fbf534SJohn Snow }
24644b028cbeSPeter Krempa if (!has_backing_mask_protocol) {
24654b028cbeSPeter Krempa backing_mask_protocol = false;
24664b028cbeSPeter Krempa }
246754504663SMax Reitz
24687676e2c5SJeff Cody /* Important Note:
24697676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for
24707676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to
24717676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a
24727676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */
24731d13b167SKevin Wolf bs = qmp_get_root_bs(device, &local_err);
24741d13b167SKevin Wolf if (!bs) {
24751d13b167SKevin Wolf bs = bdrv_lookup_bs(device, device, NULL);
24761d13b167SKevin Wolf if (!bs) {
24771d13b167SKevin Wolf error_free(local_err);
247875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
247975158ebbSMarkus Armbruster "Device '%s' not found", device);
24801d13b167SKevin Wolf } else {
24811d13b167SKevin Wolf error_propagate(errp, local_err);
24821d13b167SKevin Wolf }
2483ed61fc10SJeff Cody return;
2484ed61fc10SJeff Cody }
2485ed61fc10SJeff Cody
24861d13b167SKevin Wolf aio_context = bdrv_get_aio_context(bs);
24879e85cd5cSStefan Hajnoczi
2488bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
2489b49f4755SStefan Hajnoczi return;
2490628ff683SFam Zheng }
2491628ff683SFam Zheng
2492ed61fc10SJeff Cody /* default top_bs is the active layer */
2493ed61fc10SJeff Cody top_bs = bs;
2494ed61fc10SJeff Cody
249554fde4ffSMarkus Armbruster if (top_node && top) {
24963c605f40SKevin Wolf error_setg(errp, "'top-node' and 'top' are mutually exclusive");
2497b49f4755SStefan Hajnoczi return;
249854fde4ffSMarkus Armbruster } else if (top_node) {
24993c605f40SKevin Wolf top_bs = bdrv_lookup_bs(NULL, top_node, errp);
25003c605f40SKevin Wolf if (top_bs == NULL) {
2501b49f4755SStefan Hajnoczi return;
25023c605f40SKevin Wolf }
25033c605f40SKevin Wolf if (!bdrv_chain_contains(bs, top_bs)) {
25043c605f40SKevin Wolf error_setg(errp, "'%s' is not in this backing file chain",
25053c605f40SKevin Wolf top_node);
2506b49f4755SStefan Hajnoczi return;
25073c605f40SKevin Wolf }
250854fde4ffSMarkus Armbruster } else if (top) {
2509f30c66baSMax Reitz /* This strcmp() is just a shortcut, there is no need to
2510f30c66baSMax Reitz * refresh @bs's filename. If it mismatches,
2511f30c66baSMax Reitz * bdrv_find_backing_image() will do the refresh and may still
2512f30c66baSMax Reitz * return @bs. */
2513ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) {
2514ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top);
2515ed61fc10SJeff Cody }
2516ed61fc10SJeff Cody }
2517ed61fc10SJeff Cody
2518ed61fc10SJeff Cody if (top_bs == NULL) {
2519ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2520b49f4755SStefan Hajnoczi return;
2521ed61fc10SJeff Cody }
2522ed61fc10SJeff Cody
25239e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context);
25249e85cd5cSStefan Hajnoczi
252554fde4ffSMarkus Armbruster if (base_node && base) {
25263c605f40SKevin Wolf error_setg(errp, "'base-node' and 'base' are mutually exclusive");
2527b49f4755SStefan Hajnoczi return;
252854fde4ffSMarkus Armbruster } else if (base_node) {
25293c605f40SKevin Wolf base_bs = bdrv_lookup_bs(NULL, base_node, errp);
25303c605f40SKevin Wolf if (base_bs == NULL) {
2531b49f4755SStefan Hajnoczi return;
25323c605f40SKevin Wolf }
25333c605f40SKevin Wolf if (!bdrv_chain_contains(top_bs, base_bs)) {
25343c605f40SKevin Wolf error_setg(errp, "'%s' is not in this backing file chain",
25353c605f40SKevin Wolf base_node);
2536b49f4755SStefan Hajnoczi return;
25373c605f40SKevin Wolf }
253854fde4ffSMarkus Armbruster } else if (base) {
2539d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base);
2540b0d7be2aSMarkus Armbruster if (base_bs == NULL) {
2541b0d7be2aSMarkus Armbruster error_setg(errp, "Can't find '%s' in the backing chain", base);
2542b49f4755SStefan Hajnoczi return;
2543b0d7be2aSMarkus Armbruster }
2544d5208c45SJeff Cody } else {
2545d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs);
2546d5208c45SJeff Cody if (base_bs == NULL) {
2547b0d7be2aSMarkus Armbruster error_setg(errp, "There is no backimg image");
2548b49f4755SStefan Hajnoczi return;
2549d5208c45SJeff Cody }
2550b0d7be2aSMarkus Armbruster }
2551d5208c45SJeff Cody
25529e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context);
25539e85cd5cSStefan Hajnoczi
25549a71b9deSMax Reitz for (iter = top_bs; iter != bdrv_filter_or_cow_bs(base_bs);
25559a71b9deSMax Reitz iter = bdrv_filter_or_cow_bs(iter))
25569a71b9deSMax Reitz {
2557058223a6SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2558b49f4755SStefan Hajnoczi return;
2559bb00021dSFam Zheng }
2560058223a6SAlberto Garcia }
2561bb00021dSFam Zheng
25627676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */
25637676e2c5SJeff Cody if (top_bs == base_bs) {
25647676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself");
2565b49f4755SStefan Hajnoczi return;
25667676e2c5SJeff Cody }
25677676e2c5SJeff Cody
256805ea385aSMax Reitz /*
256905ea385aSMax Reitz * Active commit is required if and only if someone has taken a
257005ea385aSMax Reitz * WRITE permission on the top node. Historically, we have always
257105ea385aSMax Reitz * used active commit for top nodes, so continue that practice
257205ea385aSMax Reitz * lest we possibly break clients that rely on this behavior, e.g.
257305ea385aSMax Reitz * to later attach this node to a writing parent.
257405ea385aSMax Reitz * (Active commit is never really wrong.)
257505ea385aSMax Reitz */
257605ea385aSMax Reitz bdrv_get_cumulative_perm(top_bs, &top_perm, &top_shared);
257705ea385aSMax Reitz if (top_perm & BLK_PERM_WRITE ||
257805ea385aSMax Reitz bdrv_skip_filters(top_bs) == bdrv_skip_filters(bs))
257905ea385aSMax Reitz {
258054fde4ffSMarkus Armbruster if (backing_file) {
258105ea385aSMax Reitz if (bdrv_skip_filters(top_bs) == bdrv_skip_filters(bs)) {
258254e26900SJeff Cody error_setg(errp, "'backing-file' specified,"
258354e26900SJeff Cody " but 'top' is the active layer");
258405ea385aSMax Reitz } else {
258505ea385aSMax Reitz error_setg(errp, "'backing-file' specified, but 'top' has a "
258605ea385aSMax Reitz "writer on it");
258705ea385aSMax Reitz }
2588b49f4755SStefan Hajnoczi return;
258954e26900SJeff Cody }
259054fde4ffSMarkus Armbruster if (!job_id) {
259105ea385aSMax Reitz /*
259205ea385aSMax Reitz * Emulate here what block_job_create() does, because it
259305ea385aSMax Reitz * is possible that @bs != @top_bs (the block job should
259405ea385aSMax Reitz * be named after @bs, even if @top_bs is the actual
259505ea385aSMax Reitz * source)
259605ea385aSMax Reitz */
259705ea385aSMax Reitz job_id = bdrv_get_device_name(bs);
259805ea385aSMax Reitz }
259905ea385aSMax Reitz commit_active_start(job_id, top_bs, base_bs, job_flags, speed, on_error,
260078bbd910SFam Zheng filter_node_name, NULL, NULL, false, &local_err);
260120a63d2cSFam Zheng } else {
2602058223a6SAlberto Garcia BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
2603058223a6SAlberto Garcia if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2604b49f4755SStefan Hajnoczi return;
2605058223a6SAlberto Garcia }
260654fde4ffSMarkus Armbruster commit_start(job_id, bs, base_bs, top_bs, job_flags,
260754fde4ffSMarkus Armbruster speed, on_error, backing_file,
26084b028cbeSPeter Krempa backing_mask_protocol,
26090db832f4SKevin Wolf filter_node_name, &local_err);
261020a63d2cSFam Zheng }
2611ed61fc10SJeff Cody if (local_err != NULL) {
2612ed61fc10SJeff Cody error_propagate(errp, local_err);
2613b49f4755SStefan Hajnoczi return;
2614ed61fc10SJeff Cody }
2615ed61fc10SJeff Cody }
2616ed61fc10SJeff Cody
26177b0b870bSJohn Snow /* Common QMP interface for drive-backup and blockdev-backup */
do_backup_common(BackupCommon * backup,BlockDriverState * bs,BlockDriverState * target_bs,AioContext * aio_context,JobTxn * txn,Error ** errp)26187b0b870bSJohn Snow static BlockJob *do_backup_common(BackupCommon *backup,
26197b0b870bSJohn Snow BlockDriverState *bs,
26207b0b870bSJohn Snow BlockDriverState *target_bs,
26217b0b870bSJohn Snow AioContext *aio_context,
26227b0b870bSJohn Snow JobTxn *txn, Error **errp)
262399a9addfSStefan Hajnoczi {
2624111049a4SJohn Snow BlockJob *job = NULL;
2625d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL;
26266a30f663SVladimir Sementsov-Ogievskiy BackupPerf perf = { .max_workers = 64 };
26277b0b870bSJohn Snow int job_flags = JOB_DEFAULT;
262899a9addfSStefan Hajnoczi
262981206a89SPavel Butsykin if (!backup->has_speed) {
263081206a89SPavel Butsykin backup->speed = 0;
263199a9addfSStefan Hajnoczi }
263281206a89SPavel Butsykin if (!backup->has_on_source_error) {
263381206a89SPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
263499a9addfSStefan Hajnoczi }
263581206a89SPavel Butsykin if (!backup->has_on_target_error) {
263681206a89SPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
263799a9addfSStefan Hajnoczi }
2638b40dacdcSJohn Snow if (!backup->has_auto_finalize) {
2639b40dacdcSJohn Snow backup->auto_finalize = true;
2640b40dacdcSJohn Snow }
2641b40dacdcSJohn Snow if (!backup->has_auto_dismiss) {
2642b40dacdcSJohn Snow backup->auto_dismiss = true;
2643b40dacdcSJohn Snow }
264413b9414bSPavel Butsykin if (!backup->has_compress) {
264513b9414bSPavel Butsykin backup->compress = false;
264613b9414bSPavel Butsykin }
264799a9addfSStefan Hajnoczi
264886c6a3b6SVladimir Sementsov-Ogievskiy if (backup->x_perf) {
264986c6a3b6SVladimir Sementsov-Ogievskiy if (backup->x_perf->has_use_copy_range) {
265086c6a3b6SVladimir Sementsov-Ogievskiy perf.use_copy_range = backup->x_perf->use_copy_range;
265186c6a3b6SVladimir Sementsov-Ogievskiy }
26522c59fd83SVladimir Sementsov-Ogievskiy if (backup->x_perf->has_max_workers) {
26532c59fd83SVladimir Sementsov-Ogievskiy perf.max_workers = backup->x_perf->max_workers;
26542c59fd83SVladimir Sementsov-Ogievskiy }
26552c59fd83SVladimir Sementsov-Ogievskiy if (backup->x_perf->has_max_chunk) {
26562c59fd83SVladimir Sementsov-Ogievskiy perf.max_chunk = backup->x_perf->max_chunk;
26572c59fd83SVladimir Sementsov-Ogievskiy }
2658*6252deb2SFiona Ebner if (backup->x_perf->has_min_cluster_size) {
2659*6252deb2SFiona Ebner perf.min_cluster_size = backup->x_perf->min_cluster_size;
2660*6252deb2SFiona Ebner }
266186c6a3b6SVladimir Sementsov-Ogievskiy }
266286c6a3b6SVladimir Sementsov-Ogievskiy
2663a6c9365aSJohn Snow if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) ||
2664a6c9365aSJohn Snow (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) {
2665a6c9365aSJohn Snow /* done before desugaring 'incremental' to print the right message */
266654fde4ffSMarkus Armbruster if (!backup->bitmap) {
2667a6c9365aSJohn Snow error_setg(errp, "must provide a valid bitmap name for "
2668a6c9365aSJohn Snow "'%s' sync mode", MirrorSyncMode_str(backup->sync));
2669a6c9365aSJohn Snow return NULL;
2670a6c9365aSJohn Snow }
2671a6c9365aSJohn Snow }
2672a6c9365aSJohn Snow
2673c8b56501SJohn Snow if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) {
2674c8b56501SJohn Snow if (backup->has_bitmap_mode &&
2675c8b56501SJohn Snow backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) {
2676c8b56501SJohn Snow error_setg(errp, "Bitmap sync mode must be '%s' "
2677c8b56501SJohn Snow "when using sync mode '%s'",
2678c8b56501SJohn Snow BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS),
2679c8b56501SJohn Snow MirrorSyncMode_str(backup->sync));
2680c8b56501SJohn Snow return NULL;
2681c8b56501SJohn Snow }
2682c8b56501SJohn Snow backup->has_bitmap_mode = true;
2683c8b56501SJohn Snow backup->sync = MIRROR_SYNC_MODE_BITMAP;
2684c8b56501SJohn Snow backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS;
2685c8b56501SJohn Snow }
2686c8b56501SJohn Snow
268754fde4ffSMarkus Armbruster if (backup->bitmap) {
26887b0b870bSJohn Snow bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
26897b0b870bSJohn Snow if (!bmap) {
26907b0b870bSJohn Snow error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
26917b0b870bSJohn Snow return NULL;
26927b0b870bSJohn Snow }
2693c8b56501SJohn Snow if (!backup->has_bitmap_mode) {
2694c8b56501SJohn Snow error_setg(errp, "Bitmap sync mode must be given "
2695c8b56501SJohn Snow "when providing a bitmap");
2696c8b56501SJohn Snow return NULL;
2697c8b56501SJohn Snow }
2698b30ffbefSJohn Snow if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) {
26997b0b870bSJohn Snow return NULL;
27007b0b870bSJohn Snow }
27011a2b8b40SJohn Snow
27021a2b8b40SJohn Snow /* This does not produce a useful bitmap artifact: */
27031a2b8b40SJohn Snow if (backup->sync == MIRROR_SYNC_MODE_NONE) {
27041a2b8b40SJohn Snow error_setg(errp, "sync mode '%s' does not produce meaningful bitmap"
27051a2b8b40SJohn Snow " outputs", MirrorSyncMode_str(backup->sync));
27061a2b8b40SJohn Snow return NULL;
27071a2b8b40SJohn Snow }
27081a2b8b40SJohn Snow
27091a2b8b40SJohn Snow /* If the bitmap isn't used for input or output, this is useless: */
27101a2b8b40SJohn Snow if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER &&
27111a2b8b40SJohn Snow backup->sync != MIRROR_SYNC_MODE_BITMAP) {
27121a2b8b40SJohn Snow error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect"
27131a2b8b40SJohn Snow " when combined with sync mode '%s'",
27141a2b8b40SJohn Snow BitmapSyncMode_str(backup->bitmap_mode),
27151a2b8b40SJohn Snow MirrorSyncMode_str(backup->sync));
27161a2b8b40SJohn Snow return NULL;
27171a2b8b40SJohn Snow }
27181a2b8b40SJohn Snow }
27191a2b8b40SJohn Snow
272054fde4ffSMarkus Armbruster if (!backup->bitmap && backup->has_bitmap_mode) {
27211a2b8b40SJohn Snow error_setg(errp, "Cannot specify bitmap sync mode without a bitmap");
27221a2b8b40SJohn Snow return NULL;
27237b0b870bSJohn Snow }
27247b0b870bSJohn Snow
27257b0b870bSJohn Snow if (!backup->auto_finalize) {
27267b0b870bSJohn Snow job_flags |= JOB_MANUAL_FINALIZE;
27277b0b870bSJohn Snow }
27287b0b870bSJohn Snow if (!backup->auto_dismiss) {
27297b0b870bSJohn Snow job_flags |= JOB_MANUAL_DISMISS;
27307b0b870bSJohn Snow }
27317b0b870bSJohn Snow
27327b0b870bSJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
2733c8b56501SJohn Snow backup->sync, bmap, backup->bitmap_mode,
27340fd05c8dSVladimir Sementsov-Ogievskiy backup->compress, backup->discard_source,
273500e30f05SVladimir Sementsov-Ogievskiy backup->filter_node_name,
273686c6a3b6SVladimir Sementsov-Ogievskiy &perf,
2737c8b56501SJohn Snow backup->on_source_error,
2738c8b56501SJohn Snow backup->on_target_error,
27397b0b870bSJohn Snow job_flags, NULL, NULL, txn, errp);
27407b0b870bSJohn Snow return job;
27417b0b870bSJohn Snow }
27427b0b870bSJohn Snow
qmp_drive_backup(DriveBackup * backup,Error ** errp)27432288ccfaSSergio Lopez void qmp_drive_backup(DriveBackup *backup, Error **errp)
27447b0b870bSJohn Snow {
27452288ccfaSSergio Lopez TransactionAction action = {
27462288ccfaSSergio Lopez .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP,
27472288ccfaSSergio Lopez .u.drive_backup.data = backup,
27482288ccfaSSergio Lopez };
27492288ccfaSSergio Lopez blockdev_do_action(&action, errp);
275078f51fdeSJohn Snow }
275178f51fdeSJohn Snow
qmp_query_named_block_nodes(bool has_flat,bool flat,Error ** errp)2752facda544SPeter Krempa BlockDeviceInfoList *qmp_query_named_block_nodes(bool has_flat,
2753facda544SPeter Krempa bool flat,
2754facda544SPeter Krempa Error **errp)
2755c13163fbSBenoît Canet {
2756facda544SPeter Krempa bool return_flat = has_flat && flat;
2757facda544SPeter Krempa
2758facda544SPeter Krempa return bdrv_named_nodes_list(return_flat, errp);
2759c13163fbSBenoît Canet }
2760c13163fbSBenoît Canet
qmp_x_debug_query_block_graph(Error ** errp)27615d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp)
27625d3b4e99SVladimir Sementsov-Ogievskiy {
276315f3f1feSKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
276415f3f1feSKevin Wolf
27655d3b4e99SVladimir Sementsov-Ogievskiy return bdrv_get_xdbg_block_graph(errp);
27665d3b4e99SVladimir Sementsov-Ogievskiy }
27675d3b4e99SVladimir Sementsov-Ogievskiy
qmp_blockdev_backup(BlockdevBackup * backup,Error ** errp)27685b7bfe51SSergio Lopez void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp)
2769c29c1dd3SFam Zheng {
27705b7bfe51SSergio Lopez TransactionAction action = {
27715b7bfe51SSergio Lopez .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP,
27725b7bfe51SSergio Lopez .u.blockdev_backup.data = backup,
27735b7bfe51SSergio Lopez };
27745b7bfe51SSergio Lopez blockdev_do_action(&action, errp);
277578f51fdeSJohn Snow }
277678f51fdeSJohn Snow
27774193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring.
27784193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same).
27794193cdd7SFam Zheng **/
blockdev_mirror_common(const char * job_id,BlockDriverState * bs,BlockDriverState * target,const char * replaces,enum MirrorSyncMode sync,BlockMirrorBackingMode backing_mode,bool zero_target,bool has_speed,int64_t speed,bool has_granularity,uint32_t granularity,bool has_buf_size,int64_t buf_size,bool has_on_source_error,BlockdevOnError on_source_error,bool has_on_target_error,BlockdevOnError on_target_error,bool has_unmap,bool unmap,const char * filter_node_name,bool has_copy_mode,MirrorCopyMode copy_mode,bool has_auto_finalize,bool auto_finalize,bool has_auto_dismiss,bool auto_dismiss,Error ** errp)278071aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
27814193cdd7SFam Zheng BlockDriverState *target,
278254fde4ffSMarkus Armbruster const char *replaces,
2783d9b902dbSPaolo Bonzini enum MirrorSyncMode sync,
2784274fcceeSMax Reitz BlockMirrorBackingMode backing_mode,
2785cdf3bc93SMax Reitz bool zero_target,
2786b952b558SPaolo Bonzini bool has_speed, int64_t speed,
2787eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity,
278808e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size,
27894193cdd7SFam Zheng bool has_on_source_error,
27904193cdd7SFam Zheng BlockdevOnError on_source_error,
27914193cdd7SFam Zheng bool has_on_target_error,
27924193cdd7SFam Zheng BlockdevOnError on_target_error,
27930fc9f8eaSFam Zheng bool has_unmap, bool unmap,
27946cdbceb1SKevin Wolf const char *filter_node_name,
2795481debaaSMax Reitz bool has_copy_mode, MirrorCopyMode copy_mode,
2796a6b58adeSJohn Snow bool has_auto_finalize, bool auto_finalize,
2797a6b58adeSJohn Snow bool has_auto_dismiss, bool auto_dismiss,
2798b952b558SPaolo Bonzini Error **errp)
2799d9b902dbSPaolo Bonzini {
28003f072a7fSMax Reitz BlockDriverState *unfiltered_bs;
2801a1999b33SJohn Snow int job_flags = JOB_DEFAULT;
2802d9b902dbSPaolo Bonzini
2803533c6e4eSKevin Wolf GLOBAL_STATE_CODE();
2804533c6e4eSKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
2805533c6e4eSKevin Wolf
2806d9b902dbSPaolo Bonzini if (!has_speed) {
2807d9b902dbSPaolo Bonzini speed = 0;
2808d9b902dbSPaolo Bonzini }
2809b952b558SPaolo Bonzini if (!has_on_source_error) {
2810b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2811b952b558SPaolo Bonzini }
2812b952b558SPaolo Bonzini if (!has_on_target_error) {
2813b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2814b952b558SPaolo Bonzini }
2815eee13dfeSPaolo Bonzini if (!has_granularity) {
2816eee13dfeSPaolo Bonzini granularity = 0;
2817eee13dfeSPaolo Bonzini }
281808e4ed6cSPaolo Bonzini if (!has_buf_size) {
281948ac0a4dSWen Congyang buf_size = 0;
282008e4ed6cSPaolo Bonzini }
28210fc9f8eaSFam Zheng if (!has_unmap) {
28220fc9f8eaSFam Zheng unmap = true;
28230fc9f8eaSFam Zheng }
2824481debaaSMax Reitz if (!has_copy_mode) {
2825481debaaSMax Reitz copy_mode = MIRROR_COPY_MODE_BACKGROUND;
2826481debaaSMax Reitz }
2827a6b58adeSJohn Snow if (has_auto_finalize && !auto_finalize) {
2828a6b58adeSJohn Snow job_flags |= JOB_MANUAL_FINALIZE;
2829a6b58adeSJohn Snow }
2830a6b58adeSJohn Snow if (has_auto_dismiss && !auto_dismiss) {
2831a6b58adeSJohn Snow job_flags |= JOB_MANUAL_DISMISS;
2832a6b58adeSJohn Snow }
283308e4ed6cSPaolo Bonzini
2834eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
2835c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
28363cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]");
2837eee13dfeSPaolo Bonzini return;
2838eee13dfeSPaolo Bonzini }
2839eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) {
2840c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
28416cc0667dSMarkus Armbruster "a power of 2");
2842eee13dfeSPaolo Bonzini return;
2843eee13dfeSPaolo Bonzini }
2844d9b902dbSPaolo Bonzini
28454193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
28464193cdd7SFam Zheng return;
28474193cdd7SFam Zheng }
2848e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
2849e40e5027SFam Zheng return;
2850e40e5027SFam Zheng }
28514193cdd7SFam Zheng
28523f072a7fSMax Reitz if (!bdrv_backing_chain_next(bs) && sync == MIRROR_SYNC_MODE_TOP) {
28534193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL;
28544193cdd7SFam Zheng }
28554193cdd7SFam Zheng
285654fde4ffSMarkus Armbruster if (!replaces) {
28573f072a7fSMax Reitz /* We want to mirror from @bs, but keep implicit filters on top */
28583f072a7fSMax Reitz unfiltered_bs = bdrv_skip_implicit_filters(bs);
28593f072a7fSMax Reitz if (unfiltered_bs != bs) {
28603f072a7fSMax Reitz replaces = unfiltered_bs->node_name;
28613f072a7fSMax Reitz }
28623f072a7fSMax Reitz }
28633f072a7fSMax Reitz
286454fde4ffSMarkus Armbruster if (replaces) {
286574ce9e46SMax Reitz BlockDriverState *to_replace_bs;
286674ce9e46SMax Reitz int64_t bs_size, replace_size;
286774ce9e46SMax Reitz
286874ce9e46SMax Reitz bs_size = bdrv_getlength(bs);
286974ce9e46SMax Reitz if (bs_size < 0) {
287074ce9e46SMax Reitz error_setg_errno(errp, -bs_size, "Failed to query device's size");
287174ce9e46SMax Reitz return;
287274ce9e46SMax Reitz }
287374ce9e46SMax Reitz
287474ce9e46SMax Reitz to_replace_bs = check_to_replace_node(bs, replaces, errp);
287574ce9e46SMax Reitz if (!to_replace_bs) {
287674ce9e46SMax Reitz return;
287774ce9e46SMax Reitz }
287874ce9e46SMax Reitz
287974ce9e46SMax Reitz replace_size = bdrv_getlength(to_replace_bs);
288074ce9e46SMax Reitz
288174ce9e46SMax Reitz if (replace_size < 0) {
288274ce9e46SMax Reitz error_setg_errno(errp, -replace_size,
288374ce9e46SMax Reitz "Failed to query the replacement node's size");
288474ce9e46SMax Reitz return;
288574ce9e46SMax Reitz }
288674ce9e46SMax Reitz if (bs_size != replace_size) {
288774ce9e46SMax Reitz error_setg(errp, "cannot replace image with a mirror image of "
288874ce9e46SMax Reitz "different size");
288974ce9e46SMax Reitz return;
289074ce9e46SMax Reitz }
289174ce9e46SMax Reitz }
289274ce9e46SMax Reitz
28934193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling
28944193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion
28954193cdd7SFam Zheng */
289671aa9867SAlberto Garcia mirror_start(job_id, bs, target,
289754fde4ffSMarkus Armbruster replaces, job_flags,
2898cdf3bc93SMax Reitz speed, granularity, buf_size, sync, backing_mode, zero_target,
28996cdbceb1SKevin Wolf on_source_error, on_target_error, unmap, filter_node_name,
2900481debaaSMax Reitz copy_mode, errp);
29014193cdd7SFam Zheng }
29024193cdd7SFam Zheng
qmp_drive_mirror(DriveMirror * arg,Error ** errp)2903faecd40aSEric Blake void qmp_drive_mirror(DriveMirror *arg, Error **errp)
29044193cdd7SFam Zheng {
29054193cdd7SFam Zheng BlockDriverState *bs;
29063f072a7fSMax Reitz BlockDriverState *target_backing_bs, *target_bs;
29074193cdd7SFam Zheng AioContext *aio_context;
2908274fcceeSMax Reitz BlockMirrorBackingMode backing_mode;
29094193cdd7SFam Zheng Error *local_err = NULL;
29104193cdd7SFam Zheng QDict *options = NULL;
29114193cdd7SFam Zheng int flags;
29124193cdd7SFam Zheng int64_t size;
2913faecd40aSEric Blake const char *format = arg->format;
2914cdf3bc93SMax Reitz bool zero_target;
29158ed7d42fSKevin Wolf int ret;
29164193cdd7SFam Zheng
29170524e93aSKevin Wolf bs = qmp_get_root_bs(arg->device, errp);
29180524e93aSKevin Wolf if (!bs) {
2919d9b902dbSPaolo Bonzini return;
2920d9b902dbSPaolo Bonzini }
2921d9b902dbSPaolo Bonzini
2922cb2af917SPaolo Bonzini /* Early check to avoid creating target */
2923277f2007SKevin Wolf bdrv_graph_rdlock_main_loop();
2924cb2af917SPaolo Bonzini if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
2925277f2007SKevin Wolf bdrv_graph_rdunlock_main_loop();
2926cb2af917SPaolo Bonzini return;
2927cb2af917SPaolo Bonzini }
2928cb2af917SPaolo Bonzini
29290524e93aSKevin Wolf aio_context = bdrv_get_aio_context(bs);
29305a7e7a0bSStefan Hajnoczi
2931faecd40aSEric Blake if (!arg->has_mode) {
2932faecd40aSEric Blake arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
29334193cdd7SFam Zheng }
2934d9b902dbSPaolo Bonzini
293554fde4ffSMarkus Armbruster if (!arg->format) {
2936faecd40aSEric Blake format = (arg->mode == NEW_IMAGE_MODE_EXISTING
2937faecd40aSEric Blake ? NULL : bs->drv->format_name);
2938d9b902dbSPaolo Bonzini }
2939d9b902dbSPaolo Bonzini
294061de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR;
29413f072a7fSMax Reitz target_backing_bs = bdrv_cow_bs(bdrv_skip_filters(bs));
29423f072a7fSMax Reitz if (!target_backing_bs && arg->sync == MIRROR_SYNC_MODE_TOP) {
2943faecd40aSEric Blake arg->sync = MIRROR_SYNC_MODE_FULL;
2944d9b902dbSPaolo Bonzini }
2945faecd40aSEric Blake if (arg->sync == MIRROR_SYNC_MODE_NONE) {
29463f072a7fSMax Reitz target_backing_bs = bs;
2947117e0c82SMax Reitz }
2948ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop();
2949d9b902dbSPaolo Bonzini
2950ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs);
2951ac3c5d83SStefan Hajnoczi if (size < 0) {
2952ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed");
2953b49f4755SStefan Hajnoczi return;
2954ac3c5d83SStefan Hajnoczi }
2955ac3c5d83SStefan Hajnoczi
295654fde4ffSMarkus Armbruster if (arg->replaces) {
295754fde4ffSMarkus Armbruster if (!arg->node_name) {
295809158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a"
295909158f00SBenoît Canet " named node of the graph");
2960b49f4755SStefan Hajnoczi return;
296109158f00SBenoît Canet }
296209158f00SBenoît Canet }
296309158f00SBenoît Canet
2964faecd40aSEric Blake if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
2965274fcceeSMax Reitz backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
2966274fcceeSMax Reitz } else {
2967274fcceeSMax Reitz backing_mode = MIRROR_OPEN_BACKING_CHAIN;
2968274fcceeSMax Reitz }
2969274fcceeSMax Reitz
29702a32c6e8SJohn Snow /* Don't open backing image in create() */
29712a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING;
29722a32c6e8SJohn Snow
29733f072a7fSMax Reitz if ((arg->sync == MIRROR_SYNC_MODE_FULL || !target_backing_bs)
2974faecd40aSEric Blake && arg->mode != NEW_IMAGE_MODE_EXISTING)
297514526864SMax Reitz {
2976d9b902dbSPaolo Bonzini /* create new image w/o backing file */
2977e6641719SMax Reitz assert(format);
2978faecd40aSEric Blake bdrv_img_create(arg->target, format,
29799217283dSFam Zheng NULL, NULL, NULL, size, flags, false, &local_err);
2980d9b902dbSPaolo Bonzini } else {
2981430da832SKevin Wolf BlockDriverState *explicit_backing;
29823f072a7fSMax Reitz
2983faecd40aSEric Blake switch (arg->mode) {
2984d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING:
2985d9b902dbSPaolo Bonzini break;
2986d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2987430da832SKevin Wolf /*
2988430da832SKevin Wolf * Create new image with backing file.
2989430da832SKevin Wolf * Implicit filters should not appear in the filename.
2990430da832SKevin Wolf */
2991b7cfc7d5SKevin Wolf bdrv_graph_rdlock_main_loop();
2992430da832SKevin Wolf explicit_backing = bdrv_skip_implicit_filters(target_backing_bs);
29933f072a7fSMax Reitz bdrv_refresh_filename(explicit_backing);
2994b7cfc7d5SKevin Wolf bdrv_graph_rdunlock_main_loop();
2995b7cfc7d5SKevin Wolf
2996faecd40aSEric Blake bdrv_img_create(arg->target, format,
29973f072a7fSMax Reitz explicit_backing->filename,
29983f072a7fSMax Reitz explicit_backing->drv->format_name,
29999217283dSFam Zheng NULL, size, flags, false, &local_err);
3000d9b902dbSPaolo Bonzini break;
3001d9b902dbSPaolo Bonzini default:
3002d9b902dbSPaolo Bonzini abort();
3003d9b902dbSPaolo Bonzini }
3004d9b902dbSPaolo Bonzini }
3005d9b902dbSPaolo Bonzini
300684d18f06SMarkus Armbruster if (local_err) {
3007cf8f2426SLuiz Capitulino error_propagate(errp, local_err);
3008b49f4755SStefan Hajnoczi return;
3009d9b902dbSPaolo Bonzini }
3010d9b902dbSPaolo Bonzini
30114c828dc6SBenoît Canet options = qdict_new();
301254fde4ffSMarkus Armbruster if (arg->node_name) {
301346f5ac20SEric Blake qdict_put_str(options, "node-name", arg->node_name);
30144c828dc6SBenoît Canet }
3015e6641719SMax Reitz if (format) {
301646f5ac20SEric Blake qdict_put_str(options, "driver", format);
3017e6641719SMax Reitz }
30184c828dc6SBenoît Canet
3019b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing
3020b812f671SPaolo Bonzini * file.
3021b812f671SPaolo Bonzini */
30222a32c6e8SJohn Snow target_bs = bdrv_open(arg->target, NULL, options, flags, errp);
30235b363937SMax Reitz if (!target_bs) {
3024c6e0a6deSKevin Wolf return;
3025d9b902dbSPaolo Bonzini }
3026d9b902dbSPaolo Bonzini
302706717986SKevin Wolf bdrv_graph_rdlock_main_loop();
3028cdf3bc93SMax Reitz zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL &&
3029cdf3bc93SMax Reitz (arg->mode == NEW_IMAGE_MODE_EXISTING ||
3030cdf3bc93SMax Reitz !bdrv_has_zero_init(target_bs)));
303106717986SKevin Wolf bdrv_graph_rdunlock_main_loop();
3032cdf3bc93SMax Reitz
30333ea67e08SSergio Lopez
3034142e6907SEmanuele Giuseppe Esposito ret = bdrv_try_change_aio_context(target_bs, aio_context, NULL, errp);
30358ed7d42fSKevin Wolf if (ret < 0) {
30368ed7d42fSKevin Wolf bdrv_unref(target_bs);
30373ea67e08SSergio Lopez return;
30388ed7d42fSKevin Wolf }
30395a7e7a0bSStefan Hajnoczi
304054fde4ffSMarkus Armbruster blockdev_mirror_common(arg->job_id, bs, target_bs,
304154fde4ffSMarkus Armbruster arg->replaces, arg->sync,
3042cdf3bc93SMax Reitz backing_mode, zero_target,
3043cdf3bc93SMax Reitz arg->has_speed, arg->speed,
3044faecd40aSEric Blake arg->has_granularity, arg->granularity,
3045faecd40aSEric Blake arg->has_buf_size, arg->buf_size,
3046faecd40aSEric Blake arg->has_on_source_error, arg->on_source_error,
3047faecd40aSEric Blake arg->has_on_target_error, arg->on_target_error,
3048faecd40aSEric Blake arg->has_unmap, arg->unmap,
304954fde4ffSMarkus Armbruster NULL,
3050481debaaSMax Reitz arg->has_copy_mode, arg->copy_mode,
3051a6b58adeSJohn Snow arg->has_auto_finalize, arg->auto_finalize,
3052a6b58adeSJohn Snow arg->has_auto_dismiss, arg->auto_dismiss,
3053992861fbSMarkus Armbruster errp);
3054e253f4b8SKevin Wolf bdrv_unref(target_bs);
3055d9b902dbSPaolo Bonzini }
3056d9b902dbSPaolo Bonzini
qmp_blockdev_mirror(const char * job_id,const char * device,const char * target,const char * replaces,MirrorSyncMode sync,bool has_speed,int64_t speed,bool has_granularity,uint32_t granularity,bool has_buf_size,int64_t buf_size,bool has_on_source_error,BlockdevOnError on_source_error,bool has_on_target_error,BlockdevOnError on_target_error,const char * filter_node_name,bool has_copy_mode,MirrorCopyMode copy_mode,bool has_auto_finalize,bool auto_finalize,bool has_auto_dismiss,bool auto_dismiss,Error ** errp)305754fde4ffSMarkus Armbruster void qmp_blockdev_mirror(const char *job_id,
305871aa9867SAlberto Garcia const char *device, const char *target,
305954fde4ffSMarkus Armbruster const char *replaces,
3060df92562eSFam Zheng MirrorSyncMode sync,
3061df92562eSFam Zheng bool has_speed, int64_t speed,
3062df92562eSFam Zheng bool has_granularity, uint32_t granularity,
3063df92562eSFam Zheng bool has_buf_size, int64_t buf_size,
3064df92562eSFam Zheng bool has_on_source_error,
3065df92562eSFam Zheng BlockdevOnError on_source_error,
3066df92562eSFam Zheng bool has_on_target_error,
3067df92562eSFam Zheng BlockdevOnError on_target_error,
30686cdbceb1SKevin Wolf const char *filter_node_name,
3069481debaaSMax Reitz bool has_copy_mode, MirrorCopyMode copy_mode,
3070a6b58adeSJohn Snow bool has_auto_finalize, bool auto_finalize,
3071a6b58adeSJohn Snow bool has_auto_dismiss, bool auto_dismiss,
3072df92562eSFam Zheng Error **errp)
3073df92562eSFam Zheng {
3074df92562eSFam Zheng BlockDriverState *bs;
3075df92562eSFam Zheng BlockDriverState *target_bs;
3076df92562eSFam Zheng AioContext *aio_context;
3077274fcceeSMax Reitz BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
3078cdf3bc93SMax Reitz bool zero_target;
30798ed7d42fSKevin Wolf int ret;
3080df92562eSFam Zheng
308107eec652SKevin Wolf bs = qmp_get_root_bs(device, errp);
3082df92562eSFam Zheng if (!bs) {
3083df92562eSFam Zheng return;
3084df92562eSFam Zheng }
3085df92562eSFam Zheng
3086df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp);
3087df92562eSFam Zheng if (!target_bs) {
3088df92562eSFam Zheng return;
3089df92562eSFam Zheng }
3090df92562eSFam Zheng
3091cdf3bc93SMax Reitz zero_target = (sync == MIRROR_SYNC_MODE_FULL);
3092cdf3bc93SMax Reitz
3093df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs);
3094df92562eSFam Zheng
3095142e6907SEmanuele Giuseppe Esposito ret = bdrv_try_change_aio_context(target_bs, aio_context, NULL, errp);
30968ed7d42fSKevin Wolf if (ret < 0) {
3097b49f4755SStefan Hajnoczi return;
30988ed7d42fSKevin Wolf }
3099df92562eSFam Zheng
310054fde4ffSMarkus Armbruster blockdev_mirror_common(job_id, bs, target_bs,
310154fde4ffSMarkus Armbruster replaces, sync, backing_mode,
3102cdf3bc93SMax Reitz zero_target, has_speed, speed,
3103df92562eSFam Zheng has_granularity, granularity,
3104df92562eSFam Zheng has_buf_size, buf_size,
3105df92562eSFam Zheng has_on_source_error, on_source_error,
3106df92562eSFam Zheng has_on_target_error, on_target_error,
310754fde4ffSMarkus Armbruster true, true, filter_node_name,
3108481debaaSMax Reitz has_copy_mode, copy_mode,
3109a6b58adeSJohn Snow has_auto_finalize, auto_finalize,
3110a6b58adeSJohn Snow has_auto_dismiss, auto_dismiss,
3111992861fbSMarkus Armbruster errp);
3112df92562eSFam Zheng }
3113df92562eSFam Zheng
311496241124SEmanuele Giuseppe Esposito /*
31156f592e5aSEmanuele Giuseppe Esposito * Get a block job using its ID. Called with job_mutex held.
311696241124SEmanuele Giuseppe Esposito */
find_block_job_locked(const char * id,Error ** errp)31176f592e5aSEmanuele Giuseppe Esposito static BlockJob *find_block_job_locked(const char *id, Error **errp)
31182d47c6e9SStefan Hajnoczi {
31193ddf3efeSAlberto Garcia BlockJob *job;
31203ddf3efeSAlberto Garcia
31213ddf3efeSAlberto Garcia assert(id != NULL);
31222d47c6e9SStefan Hajnoczi
312396241124SEmanuele Giuseppe Esposito job = block_job_get_locked(id);
31243ddf3efeSAlberto Garcia
31253ddf3efeSAlberto Garcia if (!job) {
31263ddf3efeSAlberto Garcia error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
31273ddf3efeSAlberto Garcia "Block job '%s' not found", id);
31283ddf3efeSAlberto Garcia return NULL;
31292d47c6e9SStefan Hajnoczi }
31303d948cdfSStefan Hajnoczi
31313ddf3efeSAlberto Garcia return job;
31322d47c6e9SStefan Hajnoczi }
31332d47c6e9SStefan Hajnoczi
qmp_block_job_set_speed(const char * device,int64_t speed,Error ** errp)3134882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
31352d47c6e9SStefan Hajnoczi {
313696241124SEmanuele Giuseppe Esposito BlockJob *job;
313796241124SEmanuele Giuseppe Esposito
313896241124SEmanuele Giuseppe Esposito JOB_LOCK_GUARD();
31396f592e5aSEmanuele Giuseppe Esposito job = find_block_job_locked(device, errp);
31402d47c6e9SStefan Hajnoczi
31412d47c6e9SStefan Hajnoczi if (!job) {
31422d47c6e9SStefan Hajnoczi return;
31432d47c6e9SStefan Hajnoczi }
31442d47c6e9SStefan Hajnoczi
314596241124SEmanuele Giuseppe Esposito block_job_set_speed_locked(job, speed, errp);
31462d47c6e9SStefan Hajnoczi }
3147370521a1SStefan Hajnoczi
qmp_block_job_cancel(const char * device,bool has_force,bool force,Error ** errp)31486e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device,
31496e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp)
31506e37fb81SPaolo Bonzini {
315196241124SEmanuele Giuseppe Esposito BlockJob *job;
315296241124SEmanuele Giuseppe Esposito
315396241124SEmanuele Giuseppe Esposito JOB_LOCK_GUARD();
31546f592e5aSEmanuele Giuseppe Esposito job = find_block_job_locked(device, errp);
31556e37fb81SPaolo Bonzini
31566e37fb81SPaolo Bonzini if (!job) {
31576e37fb81SPaolo Bonzini return;
31586e37fb81SPaolo Bonzini }
31593d948cdfSStefan Hajnoczi
31603d948cdfSStefan Hajnoczi if (!has_force) {
31613d948cdfSStefan Hajnoczi force = false;
31623d948cdfSStefan Hajnoczi }
31633d948cdfSStefan Hajnoczi
316496241124SEmanuele Giuseppe Esposito if (job_user_paused_locked(&job->job) && !force) {
3165f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused",
3166f231b88dSCole Robinson device);
31676f592e5aSEmanuele Giuseppe Esposito return;
31686e37fb81SPaolo Bonzini }
31696e37fb81SPaolo Bonzini
31706e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job);
317196241124SEmanuele Giuseppe Esposito job_user_cancel_locked(&job->job, force, errp);
31726e37fb81SPaolo Bonzini }
31736e37fb81SPaolo Bonzini
qmp_block_job_pause(const char * device,Error ** errp)31746e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp)
3175370521a1SStefan Hajnoczi {
317696241124SEmanuele Giuseppe Esposito BlockJob *job;
317796241124SEmanuele Giuseppe Esposito
317896241124SEmanuele Giuseppe Esposito JOB_LOCK_GUARD();
31796f592e5aSEmanuele Giuseppe Esposito job = find_block_job_locked(device, errp);
3180370521a1SStefan Hajnoczi
31810ec4dfb8SJohn Snow if (!job) {
3182370521a1SStefan Hajnoczi return;
3183370521a1SStefan Hajnoczi }
31846e37fb81SPaolo Bonzini
31856e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job);
318696241124SEmanuele Giuseppe Esposito job_user_pause_locked(&job->job, errp);
31876e37fb81SPaolo Bonzini }
31886e37fb81SPaolo Bonzini
qmp_block_job_resume(const char * device,Error ** errp)31896e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp)
31906e37fb81SPaolo Bonzini {
319196241124SEmanuele Giuseppe Esposito BlockJob *job;
319296241124SEmanuele Giuseppe Esposito
319396241124SEmanuele Giuseppe Esposito JOB_LOCK_GUARD();
31946f592e5aSEmanuele Giuseppe Esposito job = find_block_job_locked(device, errp);
31956e37fb81SPaolo Bonzini
31960ec4dfb8SJohn Snow if (!job) {
31978acc72a4SPaolo Bonzini return;
31988acc72a4SPaolo Bonzini }
3199370521a1SStefan Hajnoczi
32006e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job);
320196241124SEmanuele Giuseppe Esposito job_user_resume_locked(&job->job, errp);
3202370521a1SStefan Hajnoczi }
3203fb5458cdSStefan Hajnoczi
qmp_block_job_complete(const char * device,Error ** errp)3204aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp)
3205aeae883bSPaolo Bonzini {
320696241124SEmanuele Giuseppe Esposito BlockJob *job;
320796241124SEmanuele Giuseppe Esposito
320896241124SEmanuele Giuseppe Esposito JOB_LOCK_GUARD();
32096f592e5aSEmanuele Giuseppe Esposito job = find_block_job_locked(device, errp);
3210aeae883bSPaolo Bonzini
3211aeae883bSPaolo Bonzini if (!job) {
3212aeae883bSPaolo Bonzini return;
3213aeae883bSPaolo Bonzini }
3214aeae883bSPaolo Bonzini
3215aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job);
321696241124SEmanuele Giuseppe Esposito job_complete_locked(&job->job, errp);
3217aeae883bSPaolo Bonzini }
3218aeae883bSPaolo Bonzini
qmp_block_job_finalize(const char * id,Error ** errp)321911b61fbcSJohn Snow void qmp_block_job_finalize(const char *id, Error **errp)
322011b61fbcSJohn Snow {
322196241124SEmanuele Giuseppe Esposito BlockJob *job;
322296241124SEmanuele Giuseppe Esposito
322396241124SEmanuele Giuseppe Esposito JOB_LOCK_GUARD();
32246f592e5aSEmanuele Giuseppe Esposito job = find_block_job_locked(id, errp);
322511b61fbcSJohn Snow
322611b61fbcSJohn Snow if (!job) {
322711b61fbcSJohn Snow return;
322811b61fbcSJohn Snow }
322911b61fbcSJohn Snow
323011b61fbcSJohn Snow trace_qmp_block_job_finalize(job);
323196241124SEmanuele Giuseppe Esposito job_ref_locked(&job->job);
323296241124SEmanuele Giuseppe Esposito job_finalize_locked(&job->job, errp);
3233b660a84bSStefan Reiter
323496241124SEmanuele Giuseppe Esposito job_unref_locked(&job->job);
323511b61fbcSJohn Snow }
323611b61fbcSJohn Snow
qmp_block_job_dismiss(const char * id,Error ** errp)323775f71059SJohn Snow void qmp_block_job_dismiss(const char *id, Error **errp)
323875f71059SJohn Snow {
323996241124SEmanuele Giuseppe Esposito BlockJob *bjob;
32405f9a6a08SKevin Wolf Job *job;
324175f71059SJohn Snow
324296241124SEmanuele Giuseppe Esposito JOB_LOCK_GUARD();
32436f592e5aSEmanuele Giuseppe Esposito bjob = find_block_job_locked(id, errp);
324496241124SEmanuele Giuseppe Esposito
32455f9a6a08SKevin Wolf if (!bjob) {
324675f71059SJohn Snow return;
324775f71059SJohn Snow }
324875f71059SJohn Snow
32495f9a6a08SKevin Wolf trace_qmp_block_job_dismiss(bjob);
32505f9a6a08SKevin Wolf job = &bjob->job;
325196241124SEmanuele Giuseppe Esposito job_dismiss_locked(&job, errp);
325275f71059SJohn Snow }
325375f71059SJohn Snow
qmp_block_job_change(BlockJobChangeOptions * opts,Error ** errp)325461a3a5a7SFiona Ebner void qmp_block_job_change(BlockJobChangeOptions *opts, Error **errp)
325561a3a5a7SFiona Ebner {
325661a3a5a7SFiona Ebner BlockJob *job;
325761a3a5a7SFiona Ebner
325861a3a5a7SFiona Ebner JOB_LOCK_GUARD();
325961a3a5a7SFiona Ebner job = find_block_job_locked(opts->id, errp);
326061a3a5a7SFiona Ebner
326161a3a5a7SFiona Ebner if (!job) {
326261a3a5a7SFiona Ebner return;
326361a3a5a7SFiona Ebner }
326461a3a5a7SFiona Ebner
326561a3a5a7SFiona Ebner block_job_change_locked(job, opts, errp);
326661a3a5a7SFiona Ebner }
326761a3a5a7SFiona Ebner
qmp_change_backing_file(const char * device,const char * image_node_name,const char * backing_file,Error ** errp)3268fa40e656SJeff Cody void qmp_change_backing_file(const char *device,
3269fa40e656SJeff Cody const char *image_node_name,
3270fa40e656SJeff Cody const char *backing_file,
3271fa40e656SJeff Cody Error **errp)
3272fa40e656SJeff Cody {
3273fa40e656SJeff Cody BlockDriverState *bs = NULL;
3274fa40e656SJeff Cody BlockDriverState *image_bs = NULL;
3275fa40e656SJeff Cody Error *local_err = NULL;
3276fa40e656SJeff Cody bool ro;
3277fa40e656SJeff Cody int ret;
3278fa40e656SJeff Cody
32797b5dca3fSKevin Wolf bs = qmp_get_root_bs(device, errp);
32807b5dca3fSKevin Wolf if (!bs) {
3281fa40e656SJeff Cody return;
3282fa40e656SJeff Cody }
3283fa40e656SJeff Cody
328479bb7627SKevin Wolf bdrv_graph_rdlock_main_loop();
328579bb7627SKevin Wolf
3286fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3287fa40e656SJeff Cody if (local_err) {
3288fa40e656SJeff Cody error_propagate(errp, local_err);
328979bb7627SKevin Wolf goto out_rdlock;
3290fa40e656SJeff Cody }
3291fa40e656SJeff Cody
3292fa40e656SJeff Cody if (!image_bs) {
3293fa40e656SJeff Cody error_setg(errp, "image file not found");
329479bb7627SKevin Wolf goto out_rdlock;
3295fa40e656SJeff Cody }
3296fa40e656SJeff Cody
3297fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) {
3298fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image "
3299fa40e656SJeff Cody "without a backing file");
330079bb7627SKevin Wolf goto out_rdlock;
3301fa40e656SJeff Cody }
3302fa40e656SJeff Cody
3303fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to
3304fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */
3305fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
330679bb7627SKevin Wolf goto out_rdlock;
3307fa40e656SJeff Cody }
3308fa40e656SJeff Cody
3309fa40e656SJeff Cody /* final sanity check */
3310fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) {
3311fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain",
3312fa40e656SJeff Cody device);
331379bb7627SKevin Wolf goto out_rdlock;
3314fa40e656SJeff Cody }
331579bb7627SKevin Wolf bdrv_graph_rdunlock_main_loop();
3316fa40e656SJeff Cody
3317fa40e656SJeff Cody /* if not r/w, reopen to make r/w */
3318fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs);
3319fa40e656SJeff Cody
3320fa40e656SJeff Cody if (ro) {
3321051a60f6SAlberto Garcia if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) {
3322b49f4755SStefan Hajnoczi return;
3323fa40e656SJeff Cody }
3324fa40e656SJeff Cody }
3325fa40e656SJeff Cody
3326fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file,
3327e54ee1b3SEric Blake image_bs->drv ? image_bs->drv->format_name : "",
3328e54ee1b3SEric Blake false);
3329fa40e656SJeff Cody
3330fa40e656SJeff Cody if (ret < 0) {
3331fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3332fa40e656SJeff Cody backing_file);
3333fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if
3334fa40e656SJeff Cody * appropriate */
3335fa40e656SJeff Cody }
3336fa40e656SJeff Cody
3337fa40e656SJeff Cody if (ro) {
3338992861fbSMarkus Armbruster bdrv_reopen_set_read_only(image_bs, true, errp);
3339fa40e656SJeff Cody }
334079bb7627SKevin Wolf return;
334179bb7627SKevin Wolf
334279bb7627SKevin Wolf out_rdlock:
334379bb7627SKevin Wolf bdrv_graph_rdunlock_main_loop();
3344fa40e656SJeff Cody }
3345fa40e656SJeff Cody
qmp_blockdev_add(BlockdevOptions * options,Error ** errp)3346d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3347d26c9a15SKevin Wolf {
3348be4b67bcSMax Reitz BlockDriverState *bs;
3349d26c9a15SKevin Wolf QObject *obj;
33507d5e199aSDaniel P. Berrange Visitor *v = qobject_output_visitor_new(&obj);
3351d26c9a15SKevin Wolf QDict *qdict;
3352d26c9a15SKevin Wolf
33531f584248SMarkus Armbruster visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
33543b098d56SEric Blake visit_complete(v, &obj);
33557dc847ebSMax Reitz qdict = qobject_to(QDict, obj);
3356d26c9a15SKevin Wolf
3357d26c9a15SKevin Wolf qdict_flatten(qdict);
3358d26c9a15SKevin Wolf
3359be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) {
33609ec8873eSKevin Wolf error_setg(errp, "'node-name' must be specified for the root node");
3361be4b67bcSMax Reitz goto fail;
3362be4b67bcSMax Reitz }
3363be4b67bcSMax Reitz
3364bd745e23SMax Reitz bs = bds_tree_init(qdict, errp);
3365bd745e23SMax Reitz if (!bs) {
3366be4b67bcSMax Reitz goto fail;
3367be4b67bcSMax Reitz }
33689c4218e9SMax Reitz
336989802d5aSMaxim Levitsky bdrv_set_monitor_owned(bs);
3370be4b67bcSMax Reitz
3371d26c9a15SKevin Wolf fail:
33723b098d56SEric Blake visit_free(v);
3373d26c9a15SKevin Wolf }
3374d26c9a15SKevin Wolf
qmp_blockdev_reopen(BlockdevOptionsList * reopen_list,Error ** errp)3375e60edf69SAlberto Garcia void qmp_blockdev_reopen(BlockdevOptionsList *reopen_list, Error **errp)
33761479c730SAlberto Garcia {
33773908b7a8SAlberto Garcia BlockReopenQueue *queue = NULL;
33783908b7a8SAlberto Garcia
33793908b7a8SAlberto Garcia /* Add each one of the BDS that we want to reopen to the queue */
33803908b7a8SAlberto Garcia for (; reopen_list != NULL; reopen_list = reopen_list->next) {
33813908b7a8SAlberto Garcia BlockdevOptions *options = reopen_list->value;
33821479c730SAlberto Garcia BlockDriverState *bs;
33831479c730SAlberto Garcia QObject *obj;
33843908b7a8SAlberto Garcia Visitor *v;
33851479c730SAlberto Garcia QDict *qdict;
33861479c730SAlberto Garcia
33871479c730SAlberto Garcia /* Check for the selected node name */
338854fde4ffSMarkus Armbruster if (!options->node_name) {
3389ef2e38a1SConnor Kuehl error_setg(errp, "node-name not specified");
33901479c730SAlberto Garcia goto fail;
33911479c730SAlberto Garcia }
33921479c730SAlberto Garcia
33931479c730SAlberto Garcia bs = bdrv_find_node(options->node_name);
33941479c730SAlberto Garcia if (!bs) {
3395ef2e38a1SConnor Kuehl error_setg(errp, "Failed to find node with node-name='%s'",
3396ef2e38a1SConnor Kuehl options->node_name);
33971479c730SAlberto Garcia goto fail;
33981479c730SAlberto Garcia }
33991479c730SAlberto Garcia
34001479c730SAlberto Garcia /* Put all options in a QDict and flatten it */
34013908b7a8SAlberto Garcia v = qobject_output_visitor_new(&obj);
34021f584248SMarkus Armbruster visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
34031479c730SAlberto Garcia visit_complete(v, &obj);
34043908b7a8SAlberto Garcia visit_free(v);
34053908b7a8SAlberto Garcia
34061479c730SAlberto Garcia qdict = qobject_to(QDict, obj);
34071479c730SAlberto Garcia
34081479c730SAlberto Garcia qdict_flatten(qdict);
34091479c730SAlberto Garcia
34103908b7a8SAlberto Garcia queue = bdrv_reopen_queue(queue, bs, qdict, false);
34113908b7a8SAlberto Garcia }
34123908b7a8SAlberto Garcia
34133908b7a8SAlberto Garcia /* Perform the reopen operation */
34141479c730SAlberto Garcia bdrv_reopen_multiple(queue, errp);
34153908b7a8SAlberto Garcia queue = NULL;
34161479c730SAlberto Garcia
34171479c730SAlberto Garcia fail:
34183908b7a8SAlberto Garcia bdrv_reopen_queue_free(queue);
34191479c730SAlberto Garcia }
34201479c730SAlberto Garcia
qmp_blockdev_del(const char * node_name,Error ** errp)342179b7a77eSMarkus Armbruster void qmp_blockdev_del(const char *node_name, Error **errp)
342281b936aeSAlberto Garcia {
342381b936aeSAlberto Garcia BlockDriverState *bs;
342481b936aeSAlberto Garcia
3425b4ad82aaSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE();
34262b3912f1SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
3427b4ad82aaSEmanuele Giuseppe Esposito
342881b936aeSAlberto Garcia bs = bdrv_find_node(node_name);
342981b936aeSAlberto Garcia if (!bs) {
3430ef2e38a1SConnor Kuehl error_setg(errp, "Failed to find node with node-name='%s'", node_name);
343181b936aeSAlberto Garcia return;
343281b936aeSAlberto Garcia }
34331f0c461bSKevin Wolf if (bdrv_has_blk(bs)) {
3434e467da7bSKevin Wolf error_setg(errp, "Node %s is in use", node_name);
343581b936aeSAlberto Garcia return;
343681b936aeSAlberto Garcia }
343781b936aeSAlberto Garcia
343881b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3439b49f4755SStefan Hajnoczi return;
344081b936aeSAlberto Garcia }
344181b936aeSAlberto Garcia
344270537ed5SPaolo Bonzini if (!QTAILQ_IN_USE(bs, monitor_list)) {
34439c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor",
34449c4218e9SMax Reitz bs->node_name);
3445b49f4755SStefan Hajnoczi return;
34469c4218e9SMax Reitz }
34479c4218e9SMax Reitz
34489c4218e9SMax Reitz if (bs->refcnt > 1) {
344981b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use",
345081b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs));
3451b49f4755SStefan Hajnoczi return;
345281b936aeSAlberto Garcia }
345381b936aeSAlberto Garcia
34549c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
345581b936aeSAlberto Garcia bdrv_unref(bs);
345681b936aeSAlberto Garcia }
345781b936aeSAlberto Garcia
34589def6082SKevin Wolf static BdrvChild * GRAPH_RDLOCK
bdrv_find_child(BlockDriverState * parent_bs,const char * child_name)34599def6082SKevin Wolf bdrv_find_child(BlockDriverState *parent_bs, const char *child_name)
34607f821597SWen Congyang {
34617f821597SWen Congyang BdrvChild *child;
34627f821597SWen Congyang
34637f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) {
34647f821597SWen Congyang if (strcmp(child->name, child_name) == 0) {
34657f821597SWen Congyang return child;
34667f821597SWen Congyang }
34677f821597SWen Congyang }
34687f821597SWen Congyang
34697f821597SWen Congyang return NULL;
34707f821597SWen Congyang }
34717f821597SWen Congyang
qmp_x_blockdev_change(const char * parent,const char * child,const char * node,Error ** errp)347254fde4ffSMarkus Armbruster void qmp_x_blockdev_change(const char *parent, const char *child,
34737f821597SWen Congyang const char *node, Error **errp)
34747f821597SWen Congyang {
34757f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL;
34767f821597SWen Congyang BdrvChild *p_child;
34777f821597SWen Congyang
34786bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
34799def6082SKevin Wolf
34807f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp);
34817f821597SWen Congyang if (!parent_bs) {
34829def6082SKevin Wolf goto out;
34837f821597SWen Congyang }
34847f821597SWen Congyang
348554fde4ffSMarkus Armbruster if (!child == !node) {
348654fde4ffSMarkus Armbruster if (child) {
34877f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict");
34887f821597SWen Congyang } else {
34897f821597SWen Congyang error_setg(errp, "Either child or node must be specified");
34907f821597SWen Congyang }
34919def6082SKevin Wolf goto out;
34927f821597SWen Congyang }
34937f821597SWen Congyang
349454fde4ffSMarkus Armbruster if (child) {
34957f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child);
34967f821597SWen Congyang if (!p_child) {
34977f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'",
34987f821597SWen Congyang parent, child);
34999def6082SKevin Wolf goto out;
35007f821597SWen Congyang }
35017f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp);
35027f821597SWen Congyang }
35037f821597SWen Congyang
350454fde4ffSMarkus Armbruster if (node) {
35057f821597SWen Congyang new_bs = bdrv_find_node(node);
35067f821597SWen Congyang if (!new_bs) {
35077f821597SWen Congyang error_setg(errp, "Node '%s' not found", node);
35089def6082SKevin Wolf goto out;
35097f821597SWen Congyang }
35107f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp);
35117f821597SWen Congyang }
35129def6082SKevin Wolf
35139def6082SKevin Wolf out:
35146bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
35157f821597SWen Congyang }
35167f821597SWen Congyang
qmp_query_block_jobs(Error ** errp)3517fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3518fb5458cdSStefan Hajnoczi {
3519c3033fd3SEric Blake BlockJobInfoList *head = NULL, **tail = &head;
3520f0f55dedSAlberto Garcia BlockJob *job;
3521fea68bb6SMarkus Armbruster
3522880eeec6SEmanuele Giuseppe Esposito JOB_LOCK_GUARD();
3523880eeec6SEmanuele Giuseppe Esposito
3524880eeec6SEmanuele Giuseppe Esposito for (job = block_job_next_locked(NULL); job;
3525880eeec6SEmanuele Giuseppe Esposito job = block_job_next_locked(job)) {
3526c3033fd3SEric Blake BlockJobInfo *value;
352769691e72SStefan Hajnoczi
3528559b935fSJohn Snow if (block_job_is_internal(job)) {
3529559b935fSJohn Snow continue;
3530559b935fSJohn Snow }
3531880eeec6SEmanuele Giuseppe Esposito value = block_job_query_locked(job, errp);
3532c3033fd3SEric Blake if (!value) {
3533559b935fSJohn Snow qapi_free_BlockJobInfoList(head);
3534559b935fSJohn Snow return NULL;
3535559b935fSJohn Snow }
3536c3033fd3SEric Blake QAPI_LIST_APPEND(tail, value);
3537fea68bb6SMarkus Armbruster }
353869691e72SStefan Hajnoczi
3539fea68bb6SMarkus Armbruster return head;
3540fb5458cdSStefan Hajnoczi }
35414d454574SPaolo Bonzini
qmp_x_blockdev_set_iothread(const char * node_name,StrOrNull * iothread,bool has_force,bool force,Error ** errp)3542ca00bbb1SStefan Hajnoczi void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
3543882e9b89SStefan Hajnoczi bool has_force, bool force, Error **errp)
3544ca00bbb1SStefan Hajnoczi {
3545ca00bbb1SStefan Hajnoczi AioContext *new_context;
3546ca00bbb1SStefan Hajnoczi BlockDriverState *bs;
3547ca00bbb1SStefan Hajnoczi
35482b3912f1SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
35492b3912f1SKevin Wolf
3550ca00bbb1SStefan Hajnoczi bs = bdrv_find_node(node_name);
3551ca00bbb1SStefan Hajnoczi if (!bs) {
3552ef2e38a1SConnor Kuehl error_setg(errp, "Failed to find node with node-name='%s'", node_name);
3553ca00bbb1SStefan Hajnoczi return;
3554ca00bbb1SStefan Hajnoczi }
3555ca00bbb1SStefan Hajnoczi
3556882e9b89SStefan Hajnoczi /* Protects against accidents. */
3557882e9b89SStefan Hajnoczi if (!(has_force && force) && bdrv_has_blk(bs)) {
3558882e9b89SStefan Hajnoczi error_setg(errp, "Node %s is associated with a BlockBackend and could "
3559882e9b89SStefan Hajnoczi "be in use (use force=true to override this check)",
3560882e9b89SStefan Hajnoczi node_name);
3561ca00bbb1SStefan Hajnoczi return;
3562ca00bbb1SStefan Hajnoczi }
3563ca00bbb1SStefan Hajnoczi
3564ca00bbb1SStefan Hajnoczi if (iothread->type == QTYPE_QSTRING) {
3565ca00bbb1SStefan Hajnoczi IOThread *obj = iothread_by_id(iothread->u.s);
3566ca00bbb1SStefan Hajnoczi if (!obj) {
3567ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find iothread %s", iothread->u.s);
3568ca00bbb1SStefan Hajnoczi return;
3569ca00bbb1SStefan Hajnoczi }
3570ca00bbb1SStefan Hajnoczi
3571ca00bbb1SStefan Hajnoczi new_context = iothread_get_aio_context(obj);
3572ca00bbb1SStefan Hajnoczi } else {
3573ca00bbb1SStefan Hajnoczi new_context = qemu_get_aio_context();
3574ca00bbb1SStefan Hajnoczi }
3575ca00bbb1SStefan Hajnoczi
3576142e6907SEmanuele Giuseppe Esposito bdrv_try_change_aio_context(bs, new_context, NULL, errp);
3577ca00bbb1SStefan Hajnoczi }
3578ca00bbb1SStefan Hajnoczi
35790006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = {
35804d454574SPaolo Bonzini .name = "drive",
35810006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
35824d454574SPaolo Bonzini .desc = {
35834d454574SPaolo Bonzini {
35844d454574SPaolo Bonzini .name = "snapshot",
35854d454574SPaolo Bonzini .type = QEMU_OPT_BOOL,
35864d454574SPaolo Bonzini .help = "enable/disable snapshot mode",
35874d454574SPaolo Bonzini },{
35884d454574SPaolo Bonzini .name = "aio",
35894d454574SPaolo Bonzini .type = QEMU_OPT_STRING,
3590f80f2673SAarushi Mehta .help = "host AIO implementation (threads, native, io_uring)",
35914d454574SPaolo Bonzini },{
3592e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB,
3593e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL,
3594e4b24b49SKevin Wolf .help = "Enable writeback mode",
3595e4b24b49SKevin Wolf },{
35964d454574SPaolo Bonzini .name = "format",
35974d454574SPaolo Bonzini .type = QEMU_OPT_STRING,
35984d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)",
35994d454574SPaolo Bonzini },{
36004d454574SPaolo Bonzini .name = "rerror",
36014d454574SPaolo Bonzini .type = QEMU_OPT_STRING,
36024d454574SPaolo Bonzini .help = "read error action",
36034d454574SPaolo Bonzini },{
36044d454574SPaolo Bonzini .name = "werror",
36054d454574SPaolo Bonzini .type = QEMU_OPT_STRING,
36064d454574SPaolo Bonzini .help = "write error action",
36074d454574SPaolo Bonzini },{
36084e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY,
36094d454574SPaolo Bonzini .type = QEMU_OPT_BOOL,
36104d454574SPaolo Bonzini .help = "open drive file as read-only",
3611a2a7862cSPradeep Jagadeesh },
3612a2a7862cSPradeep Jagadeesh
3613a2a7862cSPradeep Jagadeesh THROTTLE_OPTS,
3614a2a7862cSPradeep Jagadeesh
3615a2a7862cSPradeep Jagadeesh {
361676f4afb4SAlberto Garcia .name = "throttling.group",
361776f4afb4SAlberto Garcia .type = QEMU_OPT_STRING,
361876f4afb4SAlberto Garcia .help = "name of the block throttling group",
361976f4afb4SAlberto Garcia },{
36204d454574SPaolo Bonzini .name = "copy-on-read",
36214d454574SPaolo Bonzini .type = QEMU_OPT_BOOL,
36224d454574SPaolo Bonzini .help = "copy read data from backing file into image file",
3623465bee1dSPeter Lieven },{
3624465bee1dSPeter Lieven .name = "detect-zeroes",
3625465bee1dSPeter Lieven .type = QEMU_OPT_STRING,
3626465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)",
3627362e9299SAlberto Garcia },{
3628362e9299SAlberto Garcia .name = "stats-account-invalid",
3629362e9299SAlberto Garcia .type = QEMU_OPT_BOOL,
3630362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations "
3631362e9299SAlberto Garcia "in the statistics",
3632362e9299SAlberto Garcia },{
3633362e9299SAlberto Garcia .name = "stats-account-failed",
3634362e9299SAlberto Garcia .type = QEMU_OPT_BOOL,
3635362e9299SAlberto Garcia .help = "whether to account for failed I/O operations "
3636362e9299SAlberto Garcia "in the statistics",
36374d454574SPaolo Bonzini },
36384d454574SPaolo Bonzini { /* end of list */ }
36394d454574SPaolo Bonzini },
36404d454574SPaolo Bonzini };
36410006383eSKevin Wolf
36420006383eSKevin Wolf QemuOptsList qemu_drive_opts = {
36430006383eSKevin Wolf .name = "drive",
36440006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
36450006383eSKevin Wolf .desc = {
3646492fdc6fSKevin Wolf /*
3647492fdc6fSKevin Wolf * no elements => accept any params
3648492fdc6fSKevin Wolf * validation will happen later
3649492fdc6fSKevin Wolf */
36500006383eSKevin Wolf { /* end of list */ }
36510006383eSKevin Wolf },
36520006383eSKevin Wolf };
3653