xref: /openbmc/qemu/blockdev.c (revision 12d5ee3a7e83a42ba75a79b220a9db4dacd70a6e)
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"
3876f4afb4SAlberto Garcia #include "block/throttle-groups.h"
3983c9089eSPaolo Bonzini #include "monitor/monitor.h"
40d49b6836SMarkus Armbruster #include "qemu/error-report.h"
411de7afc9SPaolo Bonzini #include "qemu/option.h"
421de7afc9SPaolo Bonzini #include "qemu/config-file.h"
437b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h"
44d26c9a15SKevin Wolf #include "qapi-visit.h"
45cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h"
46d26c9a15SKevin Wolf #include "qapi/qmp-output-visitor.h"
479e7dac7cSPeter Lieven #include "qapi/util.h"
489c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
49737e150eSPaolo Bonzini #include "block/block_int.h"
50a4dea8a9SLuiz Capitulino #include "qmp-commands.h"
5112bd451fSStefan Hajnoczi #include "trace.h"
529c17d615SPaolo Bonzini #include "sysemu/arch_init.h"
53666daa68SMarkus Armbruster 
549c4218e9SMax Reitz static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
559c4218e9SMax Reitz     QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
569c4218e9SMax Reitz 
571960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = {
581960966dSMarkus Armbruster     [IF_NONE] = "none",
591960966dSMarkus Armbruster     [IF_IDE] = "ide",
601960966dSMarkus Armbruster     [IF_SCSI] = "scsi",
611960966dSMarkus Armbruster     [IF_FLOPPY] = "floppy",
621960966dSMarkus Armbruster     [IF_PFLASH] = "pflash",
631960966dSMarkus Armbruster     [IF_MTD] = "mtd",
641960966dSMarkus Armbruster     [IF_SD] = "sd",
651960966dSMarkus Armbruster     [IF_VIRTIO] = "virtio",
661960966dSMarkus Armbruster     [IF_XEN] = "xen",
671960966dSMarkus Armbruster };
681960966dSMarkus Armbruster 
6921dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = {
7027d6bf40SMarkus Armbruster     /*
7127d6bf40SMarkus Armbruster      * Do not change these numbers!  They govern how drive option
7227d6bf40SMarkus Armbruster      * index maps to unit and bus.  That mapping is ABI.
7327d6bf40SMarkus Armbruster      *
7427d6bf40SMarkus Armbruster      * All controllers used to imlement if=T drives need to support
7527d6bf40SMarkus Armbruster      * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
7627d6bf40SMarkus Armbruster      * Otherwise, some index values map to "impossible" bus, unit
7727d6bf40SMarkus Armbruster      * values.
7827d6bf40SMarkus Armbruster      *
7927d6bf40SMarkus Armbruster      * For instance, if you change [IF_SCSI] to 255, -drive
8027d6bf40SMarkus Armbruster      * if=scsi,index=12 no longer means bus=1,unit=5, but
8127d6bf40SMarkus Armbruster      * bus=0,unit=12.  With an lsi53c895a controller (7 units max),
8227d6bf40SMarkus Armbruster      * the drive can't be set up.  Regression.
8327d6bf40SMarkus Armbruster      */
8427d6bf40SMarkus Armbruster     [IF_IDE] = 2,
8527d6bf40SMarkus Armbruster     [IF_SCSI] = 7,
861960966dSMarkus Armbruster };
871960966dSMarkus Armbruster 
8821dff8cfSJohn Snow /**
8921dff8cfSJohn Snow  * Boards may call this to offer board-by-board overrides
9021dff8cfSJohn Snow  * of the default, global values.
9121dff8cfSJohn Snow  */
9221dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs)
9321dff8cfSJohn Snow {
9418e46a03SMarkus Armbruster     BlockBackend *blk;
9521dff8cfSJohn Snow     DriveInfo *dinfo;
9621dff8cfSJohn Snow 
9721dff8cfSJohn Snow     if (max_devs <= 0) {
9821dff8cfSJohn Snow         return;
9921dff8cfSJohn Snow     }
10021dff8cfSJohn Snow 
10118e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
10218e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
10321dff8cfSJohn Snow         if (dinfo->type == type) {
10421dff8cfSJohn Snow             fprintf(stderr, "Cannot override units-per-bus property of"
10521dff8cfSJohn Snow                     " the %s interface, because a drive of that type has"
10621dff8cfSJohn Snow                     " already been added.\n", if_name[type]);
10721dff8cfSJohn Snow             g_assert_not_reached();
10821dff8cfSJohn Snow         }
10921dff8cfSJohn Snow     }
11021dff8cfSJohn Snow 
11121dff8cfSJohn Snow     if_max_devs[type] = max_devs;
11221dff8cfSJohn Snow }
11321dff8cfSJohn Snow 
11414bafc54SMarkus Armbruster /*
11514bafc54SMarkus Armbruster  * We automatically delete the drive when a device using it gets
11614bafc54SMarkus Armbruster  * unplugged.  Questionable feature, but we can't just drop it.
11714bafc54SMarkus Armbruster  * Device models call blockdev_mark_auto_del() to schedule the
11814bafc54SMarkus Armbruster  * automatic deletion, and generic qdev code calls blockdev_auto_del()
11914bafc54SMarkus Armbruster  * when deletion is actually safe.
12014bafc54SMarkus Armbruster  */
1214be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk)
12214bafc54SMarkus Armbruster {
12318e46a03SMarkus Armbruster     DriveInfo *dinfo = blk_legacy_dinfo(blk);
1244be74634SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
12591fddb0dSStefan Hajnoczi     AioContext *aio_context;
12614bafc54SMarkus Armbruster 
12726f8b3a8SMarkus Armbruster     if (!dinfo) {
1282d246f01SKevin Wolf         return;
1292d246f01SKevin Wolf     }
1302d246f01SKevin Wolf 
1315433c24fSMax Reitz     if (bs) {
13291fddb0dSStefan Hajnoczi         aio_context = bdrv_get_aio_context(bs);
13391fddb0dSStefan Hajnoczi         aio_context_acquire(aio_context);
13491fddb0dSStefan Hajnoczi 
13512bde0eeSPaolo Bonzini         if (bs->job) {
13612bde0eeSPaolo Bonzini             block_job_cancel(bs->job);
13712bde0eeSPaolo Bonzini         }
13891fddb0dSStefan Hajnoczi 
13991fddb0dSStefan Hajnoczi         aio_context_release(aio_context);
1405433c24fSMax Reitz     }
14191fddb0dSStefan Hajnoczi 
14214bafc54SMarkus Armbruster     dinfo->auto_del = 1;
14314bafc54SMarkus Armbruster }
14414bafc54SMarkus Armbruster 
1454be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk)
14614bafc54SMarkus Armbruster {
14718e46a03SMarkus Armbruster     DriveInfo *dinfo = blk_legacy_dinfo(blk);
14814bafc54SMarkus Armbruster 
1490fc0f1faSRyan Harper     if (dinfo && dinfo->auto_del) {
150b9fe8a7aSMarkus Armbruster         blk_unref(blk);
15114bafc54SMarkus Armbruster     }
15214bafc54SMarkus Armbruster }
15314bafc54SMarkus Armbruster 
154d8f94e1bSJohn Snow /**
155d8f94e1bSJohn Snow  * Returns the current mapping of how many units per bus
156d8f94e1bSJohn Snow  * a particular interface can support.
157d8f94e1bSJohn Snow  *
158d8f94e1bSJohn Snow  *  A positive integer indicates n units per bus.
159d8f94e1bSJohn Snow  *  0 implies the mapping has not been established.
160d8f94e1bSJohn Snow  * -1 indicates an invalid BlockInterfaceType was given.
161d8f94e1bSJohn Snow  */
162d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type)
163d8f94e1bSJohn Snow {
164d8f94e1bSJohn Snow     if (type >= IF_IDE && type < IF_COUNT) {
165d8f94e1bSJohn Snow         return if_max_devs[type];
166d8f94e1bSJohn Snow     }
167d8f94e1bSJohn Snow 
168d8f94e1bSJohn Snow     return -1;
169d8f94e1bSJohn Snow }
170d8f94e1bSJohn Snow 
171505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index)
172505a7fb1SMarkus Armbruster {
173505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
174505a7fb1SMarkus Armbruster     return max_devs ? index / max_devs : 0;
175505a7fb1SMarkus Armbruster }
176505a7fb1SMarkus Armbruster 
177505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index)
178505a7fb1SMarkus Armbruster {
179505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
180505a7fb1SMarkus Armbruster     return max_devs ? index % max_devs : index;
181505a7fb1SMarkus Armbruster }
182505a7fb1SMarkus Armbruster 
1832292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr)
1842292ddaeSMarkus Armbruster {
18570b94331SMarkus Armbruster     return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
1862292ddaeSMarkus Armbruster }
1872292ddaeSMarkus Armbruster 
1882292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
1895645b0f4SMarkus Armbruster                     const char *optstr)
190666daa68SMarkus Armbruster {
191666daa68SMarkus Armbruster     QemuOpts *opts;
192666daa68SMarkus Armbruster 
1932292ddaeSMarkus Armbruster     opts = drive_def(optstr);
194666daa68SMarkus Armbruster     if (!opts) {
195666daa68SMarkus Armbruster         return NULL;
196666daa68SMarkus Armbruster     }
1972292ddaeSMarkus Armbruster     if (type != IF_DEFAULT) {
198f43e47dbSMarkus Armbruster         qemu_opt_set(opts, "if", if_name[type], &error_abort);
1992292ddaeSMarkus Armbruster     }
2002292ddaeSMarkus Armbruster     if (index >= 0) {
201a8b18f8fSMarkus Armbruster         qemu_opt_set_number(opts, "index", index, &error_abort);
2022292ddaeSMarkus Armbruster     }
203666daa68SMarkus Armbruster     if (file)
204f43e47dbSMarkus Armbruster         qemu_opt_set(opts, "file", file, &error_abort);
205666daa68SMarkus Armbruster     return opts;
206666daa68SMarkus Armbruster }
207666daa68SMarkus Armbruster 
208666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
209666daa68SMarkus Armbruster {
21018e46a03SMarkus Armbruster     BlockBackend *blk;
211666daa68SMarkus Armbruster     DriveInfo *dinfo;
212666daa68SMarkus Armbruster 
21318e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
21418e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
21518e46a03SMarkus Armbruster         if (dinfo && dinfo->type == type
21618e46a03SMarkus Armbruster             && dinfo->bus == bus && dinfo->unit == unit) {
217666daa68SMarkus Armbruster             return dinfo;
218666daa68SMarkus Armbruster         }
21918e46a03SMarkus Armbruster     }
220666daa68SMarkus Armbruster 
221666daa68SMarkus Armbruster     return NULL;
222666daa68SMarkus Armbruster }
223666daa68SMarkus Armbruster 
224a66c9dc7SJohn Snow bool drive_check_orphaned(void)
225a66c9dc7SJohn Snow {
22618e46a03SMarkus Armbruster     BlockBackend *blk;
227a66c9dc7SJohn Snow     DriveInfo *dinfo;
228a66c9dc7SJohn Snow     bool rs = false;
229a66c9dc7SJohn Snow 
23018e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
23118e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
232a66c9dc7SJohn Snow         /* If dinfo->bdrv->dev is NULL, it has no device attached. */
233a66c9dc7SJohn Snow         /* Unless this is a default drive, this may be an oversight. */
234a7f53e26SMarkus Armbruster         if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
235a66c9dc7SJohn Snow             dinfo->type != IF_NONE) {
236a66c9dc7SJohn Snow             fprintf(stderr, "Warning: Orphaned drive without device: "
237a66c9dc7SJohn Snow                     "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
2385433c24fSMax Reitz                     blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
2395433c24fSMax Reitz                     if_name[dinfo->type], dinfo->bus, dinfo->unit);
240a66c9dc7SJohn Snow             rs = true;
241a66c9dc7SJohn Snow         }
242a66c9dc7SJohn Snow     }
243a66c9dc7SJohn Snow 
244a66c9dc7SJohn Snow     return rs;
245a66c9dc7SJohn Snow }
246a66c9dc7SJohn Snow 
247f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
248f1bd51acSMarkus Armbruster {
249f1bd51acSMarkus Armbruster     return drive_get(type,
250f1bd51acSMarkus Armbruster                      drive_index_to_bus_id(type, index),
251f1bd51acSMarkus Armbruster                      drive_index_to_unit_id(type, index));
252f1bd51acSMarkus Armbruster }
253f1bd51acSMarkus Armbruster 
254666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type)
255666daa68SMarkus Armbruster {
256666daa68SMarkus Armbruster     int max_bus;
25718e46a03SMarkus Armbruster     BlockBackend *blk;
258666daa68SMarkus Armbruster     DriveInfo *dinfo;
259666daa68SMarkus Armbruster 
260666daa68SMarkus Armbruster     max_bus = -1;
26118e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
26218e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
26318e46a03SMarkus Armbruster         if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
264666daa68SMarkus Armbruster             max_bus = dinfo->bus;
265666daa68SMarkus Armbruster         }
26618e46a03SMarkus Armbruster     }
267666daa68SMarkus Armbruster     return max_bus;
268666daa68SMarkus Armbruster }
269666daa68SMarkus Armbruster 
27013839974SMarkus Armbruster /* Get a block device.  This should only be used for single-drive devices
27113839974SMarkus Armbruster    (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
27213839974SMarkus Armbruster    appropriate bus.  */
27313839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type)
27413839974SMarkus Armbruster {
27513839974SMarkus Armbruster     static int next_block_unit[IF_COUNT];
27613839974SMarkus Armbruster 
27713839974SMarkus Armbruster     return drive_get(type, 0, next_block_unit[type]++);
27813839974SMarkus Armbruster }
27913839974SMarkus Armbruster 
280666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name)
281666daa68SMarkus Armbruster {
282807105a7SMarkus Armbruster     error_printf(" %s", name);
283666daa68SMarkus Armbruster }
284666daa68SMarkus Armbruster 
285aa398a5cSStefan Hajnoczi typedef struct {
286aa398a5cSStefan Hajnoczi     QEMUBH *bh;
287fa510ebfSFam Zheng     BlockDriverState *bs;
288fa510ebfSFam Zheng } BDRVPutRefBH;
289aa398a5cSStefan Hajnoczi 
290b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
291666daa68SMarkus Armbruster {
292666daa68SMarkus Armbruster     if (!strcmp(buf, "ignore")) {
29392aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_IGNORE;
294666daa68SMarkus Armbruster     } else if (!is_read && !strcmp(buf, "enospc")) {
29592aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_ENOSPC;
296666daa68SMarkus Armbruster     } else if (!strcmp(buf, "stop")) {
29792aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_STOP;
298666daa68SMarkus Armbruster     } else if (!strcmp(buf, "report")) {
29992aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_REPORT;
300666daa68SMarkus Armbruster     } else {
301b681072dSKevin Wolf         error_setg(errp, "'%s' invalid %s error action",
302666daa68SMarkus Armbruster                    buf, is_read ? "read" : "write");
303666daa68SMarkus Armbruster         return -1;
304666daa68SMarkus Armbruster     }
305666daa68SMarkus Armbruster }
306666daa68SMarkus Armbruster 
30740119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
30840119effSAlberto Garcia                                   Error **errp)
30940119effSAlberto Garcia {
31040119effSAlberto Garcia     const QListEntry *entry;
31140119effSAlberto Garcia     for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
31240119effSAlberto Garcia         switch (qobject_type(entry->value)) {
31340119effSAlberto Garcia 
31440119effSAlberto Garcia         case QTYPE_QSTRING: {
31540119effSAlberto Garcia             unsigned long long length;
31640119effSAlberto Garcia             const char *str = qstring_get_str(qobject_to_qstring(entry->value));
31740119effSAlberto Garcia             if (parse_uint_full(str, &length, 10) == 0 &&
31840119effSAlberto Garcia                 length > 0 && length <= UINT_MAX) {
31940119effSAlberto Garcia                 block_acct_add_interval(stats, (unsigned) length);
32040119effSAlberto Garcia             } else {
32140119effSAlberto Garcia                 error_setg(errp, "Invalid interval length: %s", str);
32240119effSAlberto Garcia                 return false;
32340119effSAlberto Garcia             }
32440119effSAlberto Garcia             break;
32540119effSAlberto Garcia         }
32640119effSAlberto Garcia 
32740119effSAlberto Garcia         case QTYPE_QINT: {
32840119effSAlberto Garcia             int64_t length = qint_get_int(qobject_to_qint(entry->value));
32940119effSAlberto Garcia             if (length > 0 && length <= UINT_MAX) {
33040119effSAlberto Garcia                 block_acct_add_interval(stats, (unsigned) length);
33140119effSAlberto Garcia             } else {
33240119effSAlberto Garcia                 error_setg(errp, "Invalid interval length: %" PRId64, length);
33340119effSAlberto Garcia                 return false;
33440119effSAlberto Garcia             }
33540119effSAlberto Garcia             break;
33640119effSAlberto Garcia         }
33740119effSAlberto Garcia 
33840119effSAlberto Garcia         default:
33940119effSAlberto Garcia             error_setg(errp, "The specification of stats-intervals is invalid");
34040119effSAlberto Garcia             return false;
34140119effSAlberto Garcia         }
34240119effSAlberto Garcia     }
34340119effSAlberto Garcia     return true;
34440119effSAlberto Garcia }
34540119effSAlberto Garcia 
346cc0681c4SBenoît Canet static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
3470563e191SZhi Yong Wu {
348cc0681c4SBenoît Canet     if (throttle_conflicting(cfg)) {
349cc0681c4SBenoît Canet         error_setg(errp, "bps/iops/max total values and read/write values"
350c546194fSStefan Hajnoczi                          " cannot be used at the same time");
3510563e191SZhi Yong Wu         return false;
3520563e191SZhi Yong Wu     }
3530563e191SZhi Yong Wu 
354cc0681c4SBenoît Canet     if (!throttle_is_valid(cfg)) {
355972606c4SFam Zheng         error_setg(errp, "bps/iops/max values must be within [0, %lld]",
356972606c4SFam Zheng                    THROTTLE_VALUE_MAX);
3577d81c141SStefan Hajnoczi         return false;
3587d81c141SStefan Hajnoczi     }
3597d81c141SStefan Hajnoczi 
360ee2bdc33SStefan Hajnoczi     if (throttle_max_is_missing_limit(cfg)) {
361ee2bdc33SStefan Hajnoczi         error_setg(errp, "bps_max/iops_max require corresponding"
362ee2bdc33SStefan Hajnoczi                          " bps/iops values");
363ee2bdc33SStefan Hajnoczi         return false;
364ee2bdc33SStefan Hajnoczi     }
365ee2bdc33SStefan Hajnoczi 
3660563e191SZhi Yong Wu     return true;
3670563e191SZhi Yong Wu }
3680563e191SZhi Yong Wu 
36933cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
37033cb7dc8SKevin Wolf 
371fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */
372fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
373fbf8175eSMax Reitz     const char **throttling_group, ThrottleConfig *throttle_cfg,
374fbf8175eSMax Reitz     BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
375fbf8175eSMax Reitz {
376fbf8175eSMax Reitz     const char *discard;
377fbf8175eSMax Reitz     Error *local_error = NULL;
378fbf8175eSMax Reitz     const char *aio;
379fbf8175eSMax Reitz 
380fbf8175eSMax Reitz     if (bdrv_flags) {
381fbf8175eSMax Reitz         if (!qemu_opt_get_bool(opts, "read-only", false)) {
382fbf8175eSMax Reitz             *bdrv_flags |= BDRV_O_RDWR;
383fbf8175eSMax Reitz         }
384fbf8175eSMax Reitz         if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
385fbf8175eSMax Reitz             *bdrv_flags |= BDRV_O_COPY_ON_READ;
386fbf8175eSMax Reitz         }
387fbf8175eSMax Reitz 
388fbf8175eSMax Reitz         if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
389fbf8175eSMax Reitz             if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
390fbf8175eSMax Reitz                 error_setg(errp, "Invalid discard option");
391fbf8175eSMax Reitz                 return;
392fbf8175eSMax Reitz             }
393fbf8175eSMax Reitz         }
394fbf8175eSMax Reitz 
395fbf8175eSMax Reitz         if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
396fbf8175eSMax Reitz             if (!strcmp(aio, "native")) {
397fbf8175eSMax Reitz                 *bdrv_flags |= BDRV_O_NATIVE_AIO;
398fbf8175eSMax Reitz             } else if (!strcmp(aio, "threads")) {
399fbf8175eSMax Reitz                 /* this is the default */
400fbf8175eSMax Reitz             } else {
401fbf8175eSMax Reitz                error_setg(errp, "invalid aio option");
402fbf8175eSMax Reitz                return;
403fbf8175eSMax Reitz             }
404fbf8175eSMax Reitz         }
405fbf8175eSMax Reitz     }
406fbf8175eSMax Reitz 
407fbf8175eSMax Reitz     /* disk I/O throttling */
408fbf8175eSMax Reitz     if (throttling_group) {
409fbf8175eSMax Reitz         *throttling_group = qemu_opt_get(opts, "throttling.group");
410fbf8175eSMax Reitz     }
411fbf8175eSMax Reitz 
412fbf8175eSMax Reitz     if (throttle_cfg) {
413fbf8175eSMax Reitz         memset(throttle_cfg, 0, sizeof(*throttle_cfg));
414fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
415fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-total", 0);
416fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_READ].avg  =
417fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-read", 0);
418fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
419fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-write", 0);
420fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
421fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-total", 0);
422fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
423fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-read", 0);
424fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
425fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-write", 0);
426fbf8175eSMax Reitz 
427fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
428fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
429fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_READ].max  =
430fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
431fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
432fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
433fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
434fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
435fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_READ].max =
436fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
437fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
438fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
439fbf8175eSMax Reitz 
440fbf8175eSMax Reitz         throttle_cfg->op_size =
441fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-size", 0);
442fbf8175eSMax Reitz 
443fbf8175eSMax Reitz         if (!check_throttle_config(throttle_cfg, errp)) {
444fbf8175eSMax Reitz             return;
445fbf8175eSMax Reitz         }
446fbf8175eSMax Reitz     }
447fbf8175eSMax Reitz 
448fbf8175eSMax Reitz     if (detect_zeroes) {
449fbf8175eSMax Reitz         *detect_zeroes =
450fbf8175eSMax Reitz             qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
451fbf8175eSMax Reitz                             qemu_opt_get(opts, "detect-zeroes"),
4527fb1cf16SEric Blake                             BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
453fbf8175eSMax Reitz                             BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
454fbf8175eSMax Reitz                             &local_error);
455fbf8175eSMax Reitz         if (local_error) {
456fbf8175eSMax Reitz             error_propagate(errp, local_error);
457fbf8175eSMax Reitz             return;
458fbf8175eSMax Reitz         }
459fbf8175eSMax Reitz 
460fbf8175eSMax Reitz         if (bdrv_flags &&
461fbf8175eSMax Reitz             *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
462fbf8175eSMax Reitz             !(*bdrv_flags & BDRV_O_UNMAP))
463fbf8175eSMax Reitz         {
464fbf8175eSMax Reitz             error_setg(errp, "setting detect-zeroes to unmap is not allowed "
465fbf8175eSMax Reitz                              "without setting discard operation to unmap");
466fbf8175eSMax Reitz             return;
467fbf8175eSMax Reitz         }
468fbf8175eSMax Reitz     }
469fbf8175eSMax Reitz }
470fbf8175eSMax Reitz 
471f298d071SKevin Wolf /* Takes the ownership of bs_opts */
47218e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
473b681072dSKevin Wolf                                    Error **errp)
474666daa68SMarkus Armbruster {
475666daa68SMarkus Armbruster     const char *buf;
476666daa68SMarkus Armbruster     int bdrv_flags = 0;
477666daa68SMarkus Armbruster     int on_read_error, on_write_error;
478362e9299SAlberto Garcia     bool account_invalid, account_failed;
47926f54e9aSMarkus Armbruster     BlockBackend *blk;
480a0f1eab1SMarkus Armbruster     BlockDriverState *bs;
481cc0681c4SBenoît Canet     ThrottleConfig cfg;
482666daa68SMarkus Armbruster     int snapshot = 0;
483c546194fSStefan Hajnoczi     Error *error = NULL;
4840006383eSKevin Wolf     QemuOpts *opts;
48540119effSAlberto Garcia     QDict *interval_dict = NULL;
48640119effSAlberto Garcia     QList *interval_list = NULL;
4870006383eSKevin Wolf     const char *id;
488fbf8175eSMax Reitz     BlockdevDetectZeroesOptions detect_zeroes =
489fbf8175eSMax Reitz         BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
490fbf8175eSMax Reitz     const char *throttling_group = NULL;
491666daa68SMarkus Armbruster 
492f298d071SKevin Wolf     /* Check common options by copying from bs_opts to opts, all other options
493f298d071SKevin Wolf      * stay in bs_opts for processing by bdrv_open(). */
494f298d071SKevin Wolf     id = qdict_get_try_str(bs_opts, "id");
4950006383eSKevin Wolf     opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
49684d18f06SMarkus Armbruster     if (error) {
497b681072dSKevin Wolf         error_propagate(errp, error);
4986376f952SMarkus Armbruster         goto err_no_opts;
4990006383eSKevin Wolf     }
5000006383eSKevin Wolf 
5010006383eSKevin Wolf     qemu_opts_absorb_qdict(opts, bs_opts, &error);
50284d18f06SMarkus Armbruster     if (error) {
503b681072dSKevin Wolf         error_propagate(errp, error);
504ec9c10d2SStefan Hajnoczi         goto early_err;
5050006383eSKevin Wolf     }
5060006383eSKevin Wolf 
5070006383eSKevin Wolf     if (id) {
5080006383eSKevin Wolf         qdict_del(bs_opts, "id");
5090006383eSKevin Wolf     }
5100006383eSKevin Wolf 
511666daa68SMarkus Armbruster     /* extract parameters */
512666daa68SMarkus Armbruster     snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
513666daa68SMarkus Armbruster 
514362e9299SAlberto Garcia     account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
515362e9299SAlberto Garcia     account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
516362e9299SAlberto Garcia 
51740119effSAlberto Garcia     qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
51840119effSAlberto Garcia     qdict_array_split(interval_dict, &interval_list);
51940119effSAlberto Garcia 
52040119effSAlberto Garcia     if (qdict_size(interval_dict) != 0) {
52140119effSAlberto Garcia         error_setg(errp, "Invalid option stats-intervals.%s",
52240119effSAlberto Garcia                    qdict_first(interval_dict)->key);
52340119effSAlberto Garcia         goto early_err;
52440119effSAlberto Garcia     }
5252be5506fSAlberto Garcia 
526fbf8175eSMax Reitz     extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
527fbf8175eSMax Reitz                                     &detect_zeroes, &error);
528fbf8175eSMax Reitz     if (error) {
529fbf8175eSMax Reitz         error_propagate(errp, error);
530ec9c10d2SStefan Hajnoczi         goto early_err;
531a9384affSPaolo Bonzini     }
532666daa68SMarkus Armbruster 
533666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "format")) != NULL) {
534c8057f95SPeter Maydell         if (is_help_option(buf)) {
535807105a7SMarkus Armbruster             error_printf("Supported formats:");
536666daa68SMarkus Armbruster             bdrv_iterate_format(bdrv_format_print, NULL);
537807105a7SMarkus Armbruster             error_printf("\n");
538ec9c10d2SStefan Hajnoczi             goto early_err;
539666daa68SMarkus Armbruster         }
54074fe54f2SKevin Wolf 
541e4342ce5SMax Reitz         if (qdict_haskey(bs_opts, "driver")) {
542e4342ce5SMax Reitz             error_setg(errp, "Cannot specify both 'driver' and 'format'");
543ec9c10d2SStefan Hajnoczi             goto early_err;
5446db5f5d6SMike Qiu         }
545e4342ce5SMax Reitz         qdict_put(bs_opts, "driver", qstring_from_str(buf));
546666daa68SMarkus Armbruster     }
547666daa68SMarkus Armbruster 
54892aa5c6dSPaolo Bonzini     on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
549666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
550b681072dSKevin Wolf         on_write_error = parse_block_error_action(buf, 0, &error);
55184d18f06SMarkus Armbruster         if (error) {
552b681072dSKevin Wolf             error_propagate(errp, error);
553ec9c10d2SStefan Hajnoczi             goto early_err;
554666daa68SMarkus Armbruster         }
555666daa68SMarkus Armbruster     }
556666daa68SMarkus Armbruster 
55792aa5c6dSPaolo Bonzini     on_read_error = BLOCKDEV_ON_ERROR_REPORT;
558666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
559b681072dSKevin Wolf         on_read_error = parse_block_error_action(buf, 1, &error);
56084d18f06SMarkus Armbruster         if (error) {
561b681072dSKevin Wolf             error_propagate(errp, error);
562ec9c10d2SStefan Hajnoczi             goto early_err;
563666daa68SMarkus Armbruster         }
564666daa68SMarkus Armbruster     }
565666daa68SMarkus Armbruster 
566666daa68SMarkus Armbruster     if (snapshot) {
56791a097e7SKevin Wolf         bdrv_flags |= BDRV_O_SNAPSHOT;
568666daa68SMarkus Armbruster     }
569666daa68SMarkus Armbruster 
5705ec18f8cSMax Reitz     /* init */
57139c4ae94SKevin Wolf     if ((!file || !*file) && !qdict_size(bs_opts)) {
5725ec18f8cSMax Reitz         BlockBackendRootState *blk_rs;
5735ec18f8cSMax Reitz 
5745ec18f8cSMax Reitz         blk = blk_new(qemu_opts_id(opts), errp);
5755ec18f8cSMax Reitz         if (!blk) {
5765ec18f8cSMax Reitz             goto early_err;
5775ec18f8cSMax Reitz         }
5785ec18f8cSMax Reitz 
5795ec18f8cSMax Reitz         blk_rs = blk_get_root_state(blk);
5805ec18f8cSMax Reitz         blk_rs->open_flags    = bdrv_flags;
581fbf8175eSMax Reitz         blk_rs->read_only     = !(bdrv_flags & BDRV_O_RDWR);
5825ec18f8cSMax Reitz         blk_rs->detect_zeroes = detect_zeroes;
5835ec18f8cSMax Reitz 
5845ec18f8cSMax Reitz         if (throttle_enabled(&cfg)) {
5855ec18f8cSMax Reitz             if (!throttling_group) {
5865ec18f8cSMax Reitz                 throttling_group = blk_name(blk);
5875ec18f8cSMax Reitz             }
5885ec18f8cSMax Reitz             blk_rs->throttle_group = g_strdup(throttling_group);
5895ec18f8cSMax Reitz             blk_rs->throttle_state = throttle_group_incref(throttling_group);
5905ec18f8cSMax Reitz             blk_rs->throttle_state->cfg = cfg;
5915ec18f8cSMax Reitz         }
5925ec18f8cSMax Reitz 
5935ec18f8cSMax Reitz         QDECREF(bs_opts);
5945ec18f8cSMax Reitz     } else {
5955ec18f8cSMax Reitz         if (file && !*file) {
5965ec18f8cSMax Reitz             file = NULL;
5975ec18f8cSMax Reitz         }
5985ec18f8cSMax Reitz 
59991a097e7SKevin Wolf         /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
60091a097e7SKevin Wolf          * with other callers) rather than what we want as the real defaults.
60191a097e7SKevin Wolf          * Apply the defaults here instead. */
60291a097e7SKevin Wolf         qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_WB, "on");
60391a097e7SKevin Wolf         qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
60491a097e7SKevin Wolf         qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
60591a097e7SKevin Wolf 
60691a097e7SKevin Wolf         if (snapshot) {
60791a097e7SKevin Wolf             /* always use cache=unsafe with snapshot */
60891a097e7SKevin Wolf             qdict_put(bs_opts, BDRV_OPT_CACHE_WB, qstring_from_str("on"));
60991a097e7SKevin Wolf             qdict_put(bs_opts, BDRV_OPT_CACHE_DIRECT, qstring_from_str("off"));
61091a097e7SKevin Wolf             qdict_put(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, qstring_from_str("on"));
61191a097e7SKevin Wolf         }
61291a097e7SKevin Wolf 
613*12d5ee3aSKevin Wolf         if (runstate_check(RUN_STATE_INMIGRATE)) {
614*12d5ee3aSKevin Wolf             bdrv_flags |= BDRV_O_INACTIVE;
615*12d5ee3aSKevin Wolf         }
616*12d5ee3aSKevin Wolf 
617e4342ce5SMax Reitz         blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
618e4342ce5SMax Reitz                            errp);
619e4342ce5SMax Reitz         if (!blk) {
620e4342ce5SMax Reitz             goto err_no_bs_opts;
621e4342ce5SMax Reitz         }
622e4342ce5SMax Reitz         bs = blk_bs(blk);
6230006383eSKevin Wolf 
624e4342ce5SMax Reitz         bs->detect_zeroes = detect_zeroes;
625e4342ce5SMax Reitz 
626e4342ce5SMax Reitz         /* disk I/O throttling */
627e4342ce5SMax Reitz         if (throttle_enabled(&cfg)) {
62876f4afb4SAlberto Garcia             if (!throttling_group) {
62976f4afb4SAlberto Garcia                 throttling_group = blk_name(blk);
63076f4afb4SAlberto Garcia             }
63176f4afb4SAlberto Garcia             bdrv_io_limits_enable(bs, throttling_group);
632e4342ce5SMax Reitz             bdrv_set_io_limits(bs, &cfg);
633666daa68SMarkus Armbruster         }
634666daa68SMarkus Armbruster 
635a0f1eab1SMarkus Armbruster         if (bdrv_key_required(bs)) {
636666daa68SMarkus Armbruster             autostart = 0;
637a0f1eab1SMarkus Armbruster         }
638362e9299SAlberto Garcia 
639362e9299SAlberto Garcia         block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
6402be5506fSAlberto Garcia 
64140119effSAlberto Garcia         if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
6422be5506fSAlberto Garcia             blk_unref(blk);
6432be5506fSAlberto Garcia             blk = NULL;
6442be5506fSAlberto Garcia             goto err_no_bs_opts;
6452be5506fSAlberto Garcia         }
6462be5506fSAlberto Garcia     }
6475ec18f8cSMax Reitz 
6485ec18f8cSMax Reitz     blk_set_on_error(blk, on_read_error, on_write_error);
6490006383eSKevin Wolf 
650e4342ce5SMax Reitz err_no_bs_opts:
6510006383eSKevin Wolf     qemu_opts_del(opts);
65240119effSAlberto Garcia     QDECREF(interval_dict);
65340119effSAlberto Garcia     QDECREF(interval_list);
65418e46a03SMarkus Armbruster     return blk;
655a9ae2bffSMarkus Armbruster 
656ec9c10d2SStefan Hajnoczi early_err:
657ec9c10d2SStefan Hajnoczi     qemu_opts_del(opts);
65840119effSAlberto Garcia     QDECREF(interval_dict);
65940119effSAlberto Garcia     QDECREF(interval_list);
6606376f952SMarkus Armbruster err_no_opts:
6616376f952SMarkus Armbruster     QDECREF(bs_opts);
662a9ae2bffSMarkus Armbruster     return NULL;
663666daa68SMarkus Armbruster }
664666daa68SMarkus Armbruster 
665bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts;
666bd745e23SMax Reitz 
667bd745e23SMax Reitz /* Takes the ownership of bs_opts */
668bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
669bd745e23SMax Reitz {
670bd745e23SMax Reitz     BlockDriverState *bs;
671bd745e23SMax Reitz     QemuOpts *opts;
672bd745e23SMax Reitz     Error *local_error = NULL;
673bd745e23SMax Reitz     BlockdevDetectZeroesOptions detect_zeroes;
674bd745e23SMax Reitz     int ret;
675bd745e23SMax Reitz     int bdrv_flags = 0;
676bd745e23SMax Reitz 
677bd745e23SMax Reitz     opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
678bd745e23SMax Reitz     if (!opts) {
679bd745e23SMax Reitz         goto fail;
680bd745e23SMax Reitz     }
681bd745e23SMax Reitz 
682bd745e23SMax Reitz     qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
683bd745e23SMax Reitz     if (local_error) {
684bd745e23SMax Reitz         error_propagate(errp, local_error);
685bd745e23SMax Reitz         goto fail;
686bd745e23SMax Reitz     }
687bd745e23SMax Reitz 
688bd745e23SMax Reitz     extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
689bd745e23SMax Reitz                                     &detect_zeroes, &local_error);
690bd745e23SMax Reitz     if (local_error) {
691bd745e23SMax Reitz         error_propagate(errp, local_error);
692bd745e23SMax Reitz         goto fail;
693bd745e23SMax Reitz     }
694bd745e23SMax Reitz 
695*12d5ee3aSKevin Wolf     if (runstate_check(RUN_STATE_INMIGRATE)) {
696*12d5ee3aSKevin Wolf         bdrv_flags |= BDRV_O_INACTIVE;
697*12d5ee3aSKevin Wolf     }
698*12d5ee3aSKevin Wolf 
699bd745e23SMax Reitz     bs = NULL;
700bd745e23SMax Reitz     ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp);
701bd745e23SMax Reitz     if (ret < 0) {
702bd745e23SMax Reitz         goto fail_no_bs_opts;
703bd745e23SMax Reitz     }
704bd745e23SMax Reitz 
705bd745e23SMax Reitz     bs->detect_zeroes = detect_zeroes;
706bd745e23SMax Reitz 
707bd745e23SMax Reitz fail_no_bs_opts:
708bd745e23SMax Reitz     qemu_opts_del(opts);
709bd745e23SMax Reitz     return bs;
710bd745e23SMax Reitz 
711bd745e23SMax Reitz fail:
712bd745e23SMax Reitz     qemu_opts_del(opts);
713bd745e23SMax Reitz     QDECREF(bs_opts);
714bd745e23SMax Reitz     return NULL;
715bd745e23SMax Reitz }
716bd745e23SMax Reitz 
7179c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void)
7189c4218e9SMax Reitz {
7199c4218e9SMax Reitz     BlockDriverState *bs, *next_bs;
7209c4218e9SMax Reitz 
7219c4218e9SMax Reitz     QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
7229c4218e9SMax Reitz         AioContext *ctx = bdrv_get_aio_context(bs);
7239c4218e9SMax Reitz 
7249c4218e9SMax Reitz         aio_context_acquire(ctx);
7259c4218e9SMax Reitz         bdrv_unref(bs);
7269c4218e9SMax Reitz         aio_context_release(ctx);
7279c4218e9SMax Reitz     }
7289c4218e9SMax Reitz }
7299c4218e9SMax Reitz 
7305abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
7315abbf0eeSKevin Wolf                             Error **errp)
73257975222SKevin Wolf {
73357975222SKevin Wolf     const char *value;
73457975222SKevin Wolf 
73557975222SKevin Wolf     value = qemu_opt_get(opts, from);
73657975222SKevin Wolf     if (value) {
7375abbf0eeSKevin Wolf         if (qemu_opt_find(opts, to)) {
7385abbf0eeSKevin Wolf             error_setg(errp, "'%s' and its alias '%s' can't be used at the "
7395abbf0eeSKevin Wolf                        "same time", to, from);
7405abbf0eeSKevin Wolf             return;
7415abbf0eeSKevin Wolf         }
74220d6cd47SJun Li     }
74320d6cd47SJun Li 
74420d6cd47SJun Li     /* rename all items in opts */
74520d6cd47SJun Li     while ((value = qemu_opt_get(opts, from))) {
746f43e47dbSMarkus Armbruster         qemu_opt_set(opts, to, value, &error_abort);
74757975222SKevin Wolf         qemu_opt_unset(opts, from);
74857975222SKevin Wolf     }
74957975222SKevin Wolf }
75057975222SKevin Wolf 
75133cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = {
75233cb7dc8SKevin Wolf     .name = "drive",
75333cb7dc8SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
75433cb7dc8SKevin Wolf     .desc = {
75533cb7dc8SKevin Wolf         {
75687a899c5SKevin Wolf             .name = "bus",
75787a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
75887a899c5SKevin Wolf             .help = "bus number",
75987a899c5SKevin Wolf         },{
76087a899c5SKevin Wolf             .name = "unit",
76187a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
76287a899c5SKevin Wolf             .help = "unit number (i.e. lun for scsi)",
76387a899c5SKevin Wolf         },{
76487a899c5SKevin Wolf             .name = "index",
76587a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
76687a899c5SKevin Wolf             .help = "index number",
76787a899c5SKevin Wolf         },{
76833cb7dc8SKevin Wolf             .name = "media",
76933cb7dc8SKevin Wolf             .type = QEMU_OPT_STRING,
77033cb7dc8SKevin Wolf             .help = "media type (disk, cdrom)",
771593d464bSKevin Wolf         },{
772593d464bSKevin Wolf             .name = "if",
773593d464bSKevin Wolf             .type = QEMU_OPT_STRING,
774593d464bSKevin Wolf             .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
775b41a7338SKevin Wolf         },{
776b41a7338SKevin Wolf             .name = "cyls",
777b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
778b41a7338SKevin Wolf             .help = "number of cylinders (ide disk geometry)",
779b41a7338SKevin Wolf         },{
780b41a7338SKevin Wolf             .name = "heads",
781b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
782b41a7338SKevin Wolf             .help = "number of heads (ide disk geometry)",
783b41a7338SKevin Wolf         },{
784b41a7338SKevin Wolf             .name = "secs",
785b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
786b41a7338SKevin Wolf             .help = "number of sectors (ide disk geometry)",
787b41a7338SKevin Wolf         },{
788b41a7338SKevin Wolf             .name = "trans",
789b41a7338SKevin Wolf             .type = QEMU_OPT_STRING,
790b41a7338SKevin Wolf             .help = "chs translation (auto, lba, none)",
79126929298SKevin Wolf         },{
79226929298SKevin Wolf             .name = "boot",
79326929298SKevin Wolf             .type = QEMU_OPT_BOOL,
79426929298SKevin Wolf             .help = "(deprecated, ignored)",
795394c7d4dSKevin Wolf         },{
796394c7d4dSKevin Wolf             .name = "addr",
797394c7d4dSKevin Wolf             .type = QEMU_OPT_STRING,
798394c7d4dSKevin Wolf             .help = "pci address (virtio only)",
799d095b465SMax Reitz         },{
800bcf83158SKevin Wolf             .name = "serial",
801bcf83158SKevin Wolf             .type = QEMU_OPT_STRING,
802bcf83158SKevin Wolf             .help = "disk serial number",
803bcf83158SKevin Wolf         },{
804d095b465SMax Reitz             .name = "file",
805d095b465SMax Reitz             .type = QEMU_OPT_STRING,
806d095b465SMax Reitz             .help = "file name",
80733cb7dc8SKevin Wolf         },
8080ebd24e0SKevin Wolf 
8090ebd24e0SKevin Wolf         /* Options that are passed on, but have special semantics with -drive */
8100ebd24e0SKevin Wolf         {
8110ebd24e0SKevin Wolf             .name = "read-only",
8120ebd24e0SKevin Wolf             .type = QEMU_OPT_BOOL,
8130ebd24e0SKevin Wolf             .help = "open drive file as read-only",
8140ebd24e0SKevin Wolf         },{
815ee13ed1cSKevin Wolf             .name = "rerror",
816ee13ed1cSKevin Wolf             .type = QEMU_OPT_STRING,
817ee13ed1cSKevin Wolf             .help = "read error action",
818ee13ed1cSKevin Wolf         },{
819ee13ed1cSKevin Wolf             .name = "werror",
820ee13ed1cSKevin Wolf             .type = QEMU_OPT_STRING,
821ee13ed1cSKevin Wolf             .help = "write error action",
822ee13ed1cSKevin Wolf         },{
8230ebd24e0SKevin Wolf             .name = "copy-on-read",
8240ebd24e0SKevin Wolf             .type = QEMU_OPT_BOOL,
8250ebd24e0SKevin Wolf             .help = "copy read data from backing file into image file",
8260ebd24e0SKevin Wolf         },
8270ebd24e0SKevin Wolf 
82833cb7dc8SKevin Wolf         { /* end of list */ }
82933cb7dc8SKevin Wolf     },
83033cb7dc8SKevin Wolf };
83133cb7dc8SKevin Wolf 
83260e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
83357975222SKevin Wolf {
83429c4e2b5SKevin Wolf     const char *value;
83518e46a03SMarkus Armbruster     BlockBackend *blk;
83633cb7dc8SKevin Wolf     DriveInfo *dinfo = NULL;
837f298d071SKevin Wolf     QDict *bs_opts;
83833cb7dc8SKevin Wolf     QemuOpts *legacy_opts;
83933cb7dc8SKevin Wolf     DriveMediaType media = MEDIA_DISK;
840593d464bSKevin Wolf     BlockInterfaceType type;
841b41a7338SKevin Wolf     int cyls, heads, secs, translation;
84287a899c5SKevin Wolf     int max_devs, bus_id, unit_id, index;
843394c7d4dSKevin Wolf     const char *devaddr;
844ee13ed1cSKevin Wolf     const char *werror, *rerror;
845a7fdbcf0SFam Zheng     bool read_only = false;
846a7fdbcf0SFam Zheng     bool copy_on_read;
847bcf83158SKevin Wolf     const char *serial;
848d095b465SMax Reitz     const char *filename;
84933cb7dc8SKevin Wolf     Error *local_err = NULL;
850247147fbSKevin Wolf     int i;
85129c4e2b5SKevin Wolf 
85257975222SKevin Wolf     /* Change legacy command line options into QMP ones */
853247147fbSKevin Wolf     static const struct {
854247147fbSKevin Wolf         const char *from;
855247147fbSKevin Wolf         const char *to;
856247147fbSKevin Wolf     } opt_renames[] = {
857247147fbSKevin Wolf         { "iops",           "throttling.iops-total" },
858247147fbSKevin Wolf         { "iops_rd",        "throttling.iops-read" },
859247147fbSKevin Wolf         { "iops_wr",        "throttling.iops-write" },
86057975222SKevin Wolf 
861247147fbSKevin Wolf         { "bps",            "throttling.bps-total" },
862247147fbSKevin Wolf         { "bps_rd",         "throttling.bps-read" },
863247147fbSKevin Wolf         { "bps_wr",         "throttling.bps-write" },
86457975222SKevin Wolf 
865247147fbSKevin Wolf         { "iops_max",       "throttling.iops-total-max" },
866247147fbSKevin Wolf         { "iops_rd_max",    "throttling.iops-read-max" },
867247147fbSKevin Wolf         { "iops_wr_max",    "throttling.iops-write-max" },
8683e9fab69SBenoît Canet 
869247147fbSKevin Wolf         { "bps_max",        "throttling.bps-total-max" },
870247147fbSKevin Wolf         { "bps_rd_max",     "throttling.bps-read-max" },
871247147fbSKevin Wolf         { "bps_wr_max",     "throttling.bps-write-max" },
8723e9fab69SBenoît Canet 
873247147fbSKevin Wolf         { "iops_size",      "throttling.iops-size" },
8742024c1dfSBenoît Canet 
87576f4afb4SAlberto Garcia         { "group",          "throttling.group" },
87676f4afb4SAlberto Garcia 
877247147fbSKevin Wolf         { "readonly",       "read-only" },
878247147fbSKevin Wolf     };
879247147fbSKevin Wolf 
880247147fbSKevin Wolf     for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
8815abbf0eeSKevin Wolf         qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
8825abbf0eeSKevin Wolf                         &local_err);
8835abbf0eeSKevin Wolf         if (local_err) {
884565f65d2SMarkus Armbruster             error_report_err(local_err);
8855abbf0eeSKevin Wolf             return NULL;
8865abbf0eeSKevin Wolf         }
887247147fbSKevin Wolf     }
8880f227a94SKevin Wolf 
88929c4e2b5SKevin Wolf     value = qemu_opt_get(all_opts, "cache");
89029c4e2b5SKevin Wolf     if (value) {
89129c4e2b5SKevin Wolf         int flags = 0;
89229c4e2b5SKevin Wolf 
89329c4e2b5SKevin Wolf         if (bdrv_parse_cache_flags(value, &flags) != 0) {
89429c4e2b5SKevin Wolf             error_report("invalid cache option");
89529c4e2b5SKevin Wolf             return NULL;
89629c4e2b5SKevin Wolf         }
89729c4e2b5SKevin Wolf 
89829c4e2b5SKevin Wolf         /* Specific options take precedence */
89954861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
90054861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
901cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_CACHE_WB), &error_abort);
90229c4e2b5SKevin Wolf         }
90354861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
90454861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
905cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_NOCACHE), &error_abort);
90629c4e2b5SKevin Wolf         }
90754861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
90854861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
909cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_NO_FLUSH), &error_abort);
91029c4e2b5SKevin Wolf         }
91129c4e2b5SKevin Wolf         qemu_opt_unset(all_opts, "cache");
91229c4e2b5SKevin Wolf     }
91329c4e2b5SKevin Wolf 
914f298d071SKevin Wolf     /* Get a QDict for processing the options */
915f298d071SKevin Wolf     bs_opts = qdict_new();
916f298d071SKevin Wolf     qemu_opts_to_qdict(all_opts, bs_opts);
917f298d071SKevin Wolf 
91887ea75d5SPeter Crosthwaite     legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
91987ea75d5SPeter Crosthwaite                                    &error_abort);
92033cb7dc8SKevin Wolf     qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
92184d18f06SMarkus Armbruster     if (local_err) {
922565f65d2SMarkus Armbruster         error_report_err(local_err);
92333cb7dc8SKevin Wolf         goto fail;
92433cb7dc8SKevin Wolf     }
92533cb7dc8SKevin Wolf 
92626929298SKevin Wolf     /* Deprecated option boot=[on|off] */
92726929298SKevin Wolf     if (qemu_opt_get(legacy_opts, "boot") != NULL) {
92826929298SKevin Wolf         fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
92926929298SKevin Wolf                 "ignored. Future versions will reject this parameter. Please "
93026929298SKevin Wolf                 "update your scripts.\n");
93126929298SKevin Wolf     }
93226929298SKevin Wolf 
93333cb7dc8SKevin Wolf     /* Media type */
93433cb7dc8SKevin Wolf     value = qemu_opt_get(legacy_opts, "media");
93533cb7dc8SKevin Wolf     if (value) {
93633cb7dc8SKevin Wolf         if (!strcmp(value, "disk")) {
93733cb7dc8SKevin Wolf             media = MEDIA_DISK;
93833cb7dc8SKevin Wolf         } else if (!strcmp(value, "cdrom")) {
93933cb7dc8SKevin Wolf             media = MEDIA_CDROM;
940a7fdbcf0SFam Zheng             read_only = true;
94133cb7dc8SKevin Wolf         } else {
94233cb7dc8SKevin Wolf             error_report("'%s' invalid media", value);
94333cb7dc8SKevin Wolf             goto fail;
94433cb7dc8SKevin Wolf         }
94533cb7dc8SKevin Wolf     }
94633cb7dc8SKevin Wolf 
9470ebd24e0SKevin Wolf     /* copy-on-read is disabled with a warning for read-only devices */
948a7fdbcf0SFam Zheng     read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
9490ebd24e0SKevin Wolf     copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
9500ebd24e0SKevin Wolf 
9510ebd24e0SKevin Wolf     if (read_only && copy_on_read) {
9520ebd24e0SKevin Wolf         error_report("warning: disabling copy-on-read on read-only drive");
9530ebd24e0SKevin Wolf         copy_on_read = false;
9540ebd24e0SKevin Wolf     }
9550ebd24e0SKevin Wolf 
9560ebd24e0SKevin Wolf     qdict_put(bs_opts, "read-only",
9570ebd24e0SKevin Wolf               qstring_from_str(read_only ? "on" : "off"));
9580ebd24e0SKevin Wolf     qdict_put(bs_opts, "copy-on-read",
9590ebd24e0SKevin Wolf               qstring_from_str(copy_on_read ? "on" :"off"));
9600ebd24e0SKevin Wolf 
961593d464bSKevin Wolf     /* Controller type */
962593d464bSKevin Wolf     value = qemu_opt_get(legacy_opts, "if");
963593d464bSKevin Wolf     if (value) {
964593d464bSKevin Wolf         for (type = 0;
965593d464bSKevin Wolf              type < IF_COUNT && strcmp(value, if_name[type]);
966593d464bSKevin Wolf              type++) {
967593d464bSKevin Wolf         }
968593d464bSKevin Wolf         if (type == IF_COUNT) {
969593d464bSKevin Wolf             error_report("unsupported bus type '%s'", value);
970593d464bSKevin Wolf             goto fail;
971593d464bSKevin Wolf         }
972593d464bSKevin Wolf     } else {
973593d464bSKevin Wolf         type = block_default_type;
974593d464bSKevin Wolf     }
975593d464bSKevin Wolf 
976b41a7338SKevin Wolf     /* Geometry */
977b41a7338SKevin Wolf     cyls  = qemu_opt_get_number(legacy_opts, "cyls", 0);
978b41a7338SKevin Wolf     heads = qemu_opt_get_number(legacy_opts, "heads", 0);
979b41a7338SKevin Wolf     secs  = qemu_opt_get_number(legacy_opts, "secs", 0);
980b41a7338SKevin Wolf 
981b41a7338SKevin Wolf     if (cyls || heads || secs) {
982b41a7338SKevin Wolf         if (cyls < 1) {
983b41a7338SKevin Wolf             error_report("invalid physical cyls number");
984b41a7338SKevin Wolf             goto fail;
985b41a7338SKevin Wolf         }
986b41a7338SKevin Wolf         if (heads < 1) {
987b41a7338SKevin Wolf             error_report("invalid physical heads number");
988b41a7338SKevin Wolf             goto fail;
989b41a7338SKevin Wolf         }
990b41a7338SKevin Wolf         if (secs < 1) {
991b41a7338SKevin Wolf             error_report("invalid physical secs number");
992b41a7338SKevin Wolf             goto fail;
993b41a7338SKevin Wolf         }
994b41a7338SKevin Wolf     }
995b41a7338SKevin Wolf 
996b41a7338SKevin Wolf     translation = BIOS_ATA_TRANSLATION_AUTO;
997b41a7338SKevin Wolf     value = qemu_opt_get(legacy_opts, "trans");
998b41a7338SKevin Wolf     if (value != NULL) {
999b41a7338SKevin Wolf         if (!cyls) {
1000b41a7338SKevin Wolf             error_report("'%s' trans must be used with cyls, heads and secs",
1001b41a7338SKevin Wolf                          value);
1002b41a7338SKevin Wolf             goto fail;
1003b41a7338SKevin Wolf         }
1004b41a7338SKevin Wolf         if (!strcmp(value, "none")) {
1005b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_NONE;
1006b41a7338SKevin Wolf         } else if (!strcmp(value, "lba")) {
1007b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_LBA;
1008f31c41ffSPaolo Bonzini         } else if (!strcmp(value, "large")) {
1009f31c41ffSPaolo Bonzini             translation = BIOS_ATA_TRANSLATION_LARGE;
1010f31c41ffSPaolo Bonzini         } else if (!strcmp(value, "rechs")) {
1011f31c41ffSPaolo Bonzini             translation = BIOS_ATA_TRANSLATION_RECHS;
1012b41a7338SKevin Wolf         } else if (!strcmp(value, "auto")) {
1013b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_AUTO;
1014b41a7338SKevin Wolf         } else {
1015b41a7338SKevin Wolf             error_report("'%s' invalid translation type", value);
1016b41a7338SKevin Wolf             goto fail;
1017b41a7338SKevin Wolf         }
1018b41a7338SKevin Wolf     }
1019b41a7338SKevin Wolf 
1020b41a7338SKevin Wolf     if (media == MEDIA_CDROM) {
1021b41a7338SKevin Wolf         if (cyls || secs || heads) {
1022b41a7338SKevin Wolf             error_report("CHS can't be set with media=cdrom");
1023b41a7338SKevin Wolf             goto fail;
1024b41a7338SKevin Wolf         }
1025b41a7338SKevin Wolf     }
1026b41a7338SKevin Wolf 
102787a899c5SKevin Wolf     /* Device address specified by bus/unit or index.
102887a899c5SKevin Wolf      * If none was specified, try to find the first free one. */
102987a899c5SKevin Wolf     bus_id  = qemu_opt_get_number(legacy_opts, "bus", 0);
103087a899c5SKevin Wolf     unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
103187a899c5SKevin Wolf     index   = qemu_opt_get_number(legacy_opts, "index", -1);
103287a899c5SKevin Wolf 
103387a899c5SKevin Wolf     max_devs = if_max_devs[type];
103487a899c5SKevin Wolf 
103587a899c5SKevin Wolf     if (index != -1) {
103687a899c5SKevin Wolf         if (bus_id != 0 || unit_id != -1) {
103787a899c5SKevin Wolf             error_report("index cannot be used with bus and unit");
103887a899c5SKevin Wolf             goto fail;
103987a899c5SKevin Wolf         }
104087a899c5SKevin Wolf         bus_id = drive_index_to_bus_id(type, index);
104187a899c5SKevin Wolf         unit_id = drive_index_to_unit_id(type, index);
104287a899c5SKevin Wolf     }
104387a899c5SKevin Wolf 
104487a899c5SKevin Wolf     if (unit_id == -1) {
104587a899c5SKevin Wolf        unit_id = 0;
104687a899c5SKevin Wolf        while (drive_get(type, bus_id, unit_id) != NULL) {
104787a899c5SKevin Wolf            unit_id++;
104887a899c5SKevin Wolf            if (max_devs && unit_id >= max_devs) {
104987a899c5SKevin Wolf                unit_id -= max_devs;
105087a899c5SKevin Wolf                bus_id++;
105187a899c5SKevin Wolf            }
105287a899c5SKevin Wolf        }
105387a899c5SKevin Wolf     }
105487a899c5SKevin Wolf 
105587a899c5SKevin Wolf     if (max_devs && unit_id >= max_devs) {
105687a899c5SKevin Wolf         error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
105787a899c5SKevin Wolf         goto fail;
105887a899c5SKevin Wolf     }
105987a899c5SKevin Wolf 
106087a899c5SKevin Wolf     if (drive_get(type, bus_id, unit_id) != NULL) {
106187a899c5SKevin Wolf         error_report("drive with bus=%d, unit=%d (index=%d) exists",
106287a899c5SKevin Wolf                      bus_id, unit_id, index);
106387a899c5SKevin Wolf         goto fail;
106487a899c5SKevin Wolf     }
106587a899c5SKevin Wolf 
1066bcf83158SKevin Wolf     /* Serial number */
1067bcf83158SKevin Wolf     serial = qemu_opt_get(legacy_opts, "serial");
1068bcf83158SKevin Wolf 
106987a899c5SKevin Wolf     /* no id supplied -> create one */
107087a899c5SKevin Wolf     if (qemu_opts_id(all_opts) == NULL) {
107187a899c5SKevin Wolf         char *new_id;
107287a899c5SKevin Wolf         const char *mediastr = "";
107387a899c5SKevin Wolf         if (type == IF_IDE || type == IF_SCSI) {
107487a899c5SKevin Wolf             mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
107587a899c5SKevin Wolf         }
107687a899c5SKevin Wolf         if (max_devs) {
107787a899c5SKevin Wolf             new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
107887a899c5SKevin Wolf                                      mediastr, unit_id);
107987a899c5SKevin Wolf         } else {
108087a899c5SKevin Wolf             new_id = g_strdup_printf("%s%s%i", if_name[type],
108187a899c5SKevin Wolf                                      mediastr, unit_id);
108287a899c5SKevin Wolf         }
108387a899c5SKevin Wolf         qdict_put(bs_opts, "id", qstring_from_str(new_id));
108487a899c5SKevin Wolf         g_free(new_id);
108587a899c5SKevin Wolf     }
108687a899c5SKevin Wolf 
1087394c7d4dSKevin Wolf     /* Add virtio block device */
1088394c7d4dSKevin Wolf     devaddr = qemu_opt_get(legacy_opts, "addr");
1089394c7d4dSKevin Wolf     if (devaddr && type != IF_VIRTIO) {
1090394c7d4dSKevin Wolf         error_report("addr is not supported by this bus type");
1091394c7d4dSKevin Wolf         goto fail;
1092394c7d4dSKevin Wolf     }
1093394c7d4dSKevin Wolf 
1094394c7d4dSKevin Wolf     if (type == IF_VIRTIO) {
1095394c7d4dSKevin Wolf         QemuOpts *devopts;
109687ea75d5SPeter Crosthwaite         devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
109787ea75d5SPeter Crosthwaite                                    &error_abort);
1098394c7d4dSKevin Wolf         if (arch_type == QEMU_ARCH_S390X) {
10991f68f1d3SAlexander Graf             qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
1100394c7d4dSKevin Wolf         } else {
1101f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
1102394c7d4dSKevin Wolf         }
1103f43e47dbSMarkus Armbruster         qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1104f43e47dbSMarkus Armbruster                      &error_abort);
1105394c7d4dSKevin Wolf         if (devaddr) {
1106f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "addr", devaddr, &error_abort);
1107394c7d4dSKevin Wolf         }
1108394c7d4dSKevin Wolf     }
1109394c7d4dSKevin Wolf 
1110d095b465SMax Reitz     filename = qemu_opt_get(legacy_opts, "file");
1111d095b465SMax Reitz 
1112ee13ed1cSKevin Wolf     /* Check werror/rerror compatibility with if=... */
1113ee13ed1cSKevin Wolf     werror = qemu_opt_get(legacy_opts, "werror");
1114ee13ed1cSKevin Wolf     if (werror != NULL) {
1115ee13ed1cSKevin Wolf         if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1116ee13ed1cSKevin Wolf             type != IF_NONE) {
1117ee13ed1cSKevin Wolf             error_report("werror is not supported by this bus type");
1118ee13ed1cSKevin Wolf             goto fail;
1119ee13ed1cSKevin Wolf         }
1120ee13ed1cSKevin Wolf         qdict_put(bs_opts, "werror", qstring_from_str(werror));
1121ee13ed1cSKevin Wolf     }
1122ee13ed1cSKevin Wolf 
1123ee13ed1cSKevin Wolf     rerror = qemu_opt_get(legacy_opts, "rerror");
1124ee13ed1cSKevin Wolf     if (rerror != NULL) {
1125ee13ed1cSKevin Wolf         if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1126ee13ed1cSKevin Wolf             type != IF_NONE) {
1127ee13ed1cSKevin Wolf             error_report("rerror is not supported by this bus type");
1128ee13ed1cSKevin Wolf             goto fail;
1129ee13ed1cSKevin Wolf         }
1130ee13ed1cSKevin Wolf         qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1131ee13ed1cSKevin Wolf     }
1132ee13ed1cSKevin Wolf 
11332d246f01SKevin Wolf     /* Actual block device init: Functionality shared with blockdev-add */
113418e46a03SMarkus Armbruster     blk = blockdev_init(filename, bs_opts, &local_err);
11353cb0e25cSMarkus Armbruster     bs_opts = NULL;
113618e46a03SMarkus Armbruster     if (!blk) {
113784d18f06SMarkus Armbruster         if (local_err) {
1138565f65d2SMarkus Armbruster             error_report_err(local_err);
1139b681072dSKevin Wolf         }
11402d246f01SKevin Wolf         goto fail;
1141b681072dSKevin Wolf     } else {
114284d18f06SMarkus Armbruster         assert(!local_err);
11432d246f01SKevin Wolf     }
11442d246f01SKevin Wolf 
114526f8b3a8SMarkus Armbruster     /* Create legacy DriveInfo */
114626f8b3a8SMarkus Armbruster     dinfo = g_malloc0(sizeof(*dinfo));
1147f298d071SKevin Wolf     dinfo->opts = all_opts;
11482d246f01SKevin Wolf 
1149b41a7338SKevin Wolf     dinfo->cyls = cyls;
1150b41a7338SKevin Wolf     dinfo->heads = heads;
1151b41a7338SKevin Wolf     dinfo->secs = secs;
1152b41a7338SKevin Wolf     dinfo->trans = translation;
1153b41a7338SKevin Wolf 
1154ee13ed1cSKevin Wolf     dinfo->type = type;
115587a899c5SKevin Wolf     dinfo->bus = bus_id;
115687a899c5SKevin Wolf     dinfo->unit = unit_id;
1157394c7d4dSKevin Wolf     dinfo->devaddr = devaddr;
1158bcf83158SKevin Wolf     dinfo->serial = g_strdup(serial);
1159bcf83158SKevin Wolf 
116026f8b3a8SMarkus Armbruster     blk_set_legacy_dinfo(blk, dinfo);
116126f8b3a8SMarkus Armbruster 
1162e34ef046SKevin Wolf     switch(type) {
1163e34ef046SKevin Wolf     case IF_IDE:
1164e34ef046SKevin Wolf     case IF_SCSI:
1165e34ef046SKevin Wolf     case IF_XEN:
1166e34ef046SKevin Wolf     case IF_NONE:
1167e34ef046SKevin Wolf         dinfo->media_cd = media == MEDIA_CDROM;
1168e34ef046SKevin Wolf         break;
1169e34ef046SKevin Wolf     default:
1170e34ef046SKevin Wolf         break;
1171e34ef046SKevin Wolf     }
1172e34ef046SKevin Wolf 
11732d246f01SKevin Wolf fail:
117433cb7dc8SKevin Wolf     qemu_opts_del(legacy_opts);
11753cb0e25cSMarkus Armbruster     QDECREF(bs_opts);
11762d246f01SKevin Wolf     return dinfo;
117757975222SKevin Wolf }
117857975222SKevin Wolf 
11793e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict)
1180666daa68SMarkus Armbruster {
1181666daa68SMarkus Armbruster     const char *device = qdict_get_str(qdict, "device");
1182a0e8544cSFam Zheng     BlockBackend *blk;
11832d3735d3SStefan Hajnoczi     int ret;
11842d3735d3SStefan Hajnoczi 
1185e8877497SStefan Hajnoczi     if (!strcmp(device, "all")) {
1186e8877497SStefan Hajnoczi         ret = bdrv_commit_all();
1187e8877497SStefan Hajnoczi     } else {
118884aa0140SStefan Hajnoczi         BlockDriverState *bs;
118984aa0140SStefan Hajnoczi         AioContext *aio_context;
119084aa0140SStefan Hajnoczi 
1191a0e8544cSFam Zheng         blk = blk_by_name(device);
1192a0e8544cSFam Zheng         if (!blk) {
119358513bdeSJeff Cody             monitor_printf(mon, "Device '%s' not found\n", device);
1194ac59eb95SMarkus Armbruster             return;
11956ab4b5abSMarkus Armbruster         }
11965433c24fSMax Reitz         if (!blk_is_available(blk)) {
11975433c24fSMax Reitz             monitor_printf(mon, "Device '%s' has no medium\n", device);
11985433c24fSMax Reitz             return;
11995433c24fSMax Reitz         }
120084aa0140SStefan Hajnoczi 
120184aa0140SStefan Hajnoczi         bs = blk_bs(blk);
120284aa0140SStefan Hajnoczi         aio_context = bdrv_get_aio_context(bs);
120384aa0140SStefan Hajnoczi         aio_context_acquire(aio_context);
120484aa0140SStefan Hajnoczi 
120584aa0140SStefan Hajnoczi         ret = bdrv_commit(bs);
120684aa0140SStefan Hajnoczi 
120784aa0140SStefan Hajnoczi         aio_context_release(aio_context);
12082d3735d3SStefan Hajnoczi     }
120958513bdeSJeff Cody     if (ret < 0) {
121058513bdeSJeff Cody         monitor_printf(mon, "'commit' error for '%s': %s\n", device,
121158513bdeSJeff Cody                        strerror(-ret));
1212666daa68SMarkus Armbruster     }
1213666daa68SMarkus Armbruster }
1214666daa68SMarkus Armbruster 
12156a8f9661SEric Blake static void blockdev_do_action(TransactionActionKind type, void *data,
12166a8f9661SEric Blake                                Error **errp)
12176cc2a415SPaolo Bonzini {
1218c8a83e85SKevin Wolf     TransactionAction action;
1219c8a83e85SKevin Wolf     TransactionActionList list;
12206cc2a415SPaolo Bonzini 
12216a8f9661SEric Blake     action.type = type;
12226a8f9661SEric Blake     action.u.data = data;
12236cc2a415SPaolo Bonzini     list.value = &action;
12246cc2a415SPaolo Bonzini     list.next = NULL;
122594d16a64SJohn Snow     qmp_transaction(&list, false, NULL, errp);
12266cc2a415SPaolo Bonzini }
12276cc2a415SPaolo Bonzini 
12280901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
12290901f67eSBenoît Canet                                 bool has_node_name, const char *node_name,
12300901f67eSBenoît Canet                                 const char *snapshot_file,
12310901f67eSBenoît Canet                                 bool has_snapshot_node_name,
12320901f67eSBenoît Canet                                 const char *snapshot_node_name,
12336106e249SLuiz Capitulino                                 bool has_format, const char *format,
12340901f67eSBenoît Canet                                 bool has_mode, NewImageMode mode, Error **errp)
1235f8882568SJes Sorensen {
1236a911e6aeSAlberto Garcia     BlockdevSnapshotSync snapshot = {
12370901f67eSBenoît Canet         .has_device = has_device,
12386cc2a415SPaolo Bonzini         .device = (char *) device,
12390901f67eSBenoît Canet         .has_node_name = has_node_name,
12400901f67eSBenoît Canet         .node_name = (char *) node_name,
12416cc2a415SPaolo Bonzini         .snapshot_file = (char *) snapshot_file,
12420901f67eSBenoît Canet         .has_snapshot_node_name = has_snapshot_node_name,
12430901f67eSBenoît Canet         .snapshot_node_name = (char *) snapshot_node_name,
12446cc2a415SPaolo Bonzini         .has_format = has_format,
12456cc2a415SPaolo Bonzini         .format = (char *) format,
12466cc2a415SPaolo Bonzini         .has_mode = has_mode,
12476cc2a415SPaolo Bonzini         .mode = mode,
12486cc2a415SPaolo Bonzini     };
1249c8a83e85SKevin Wolf     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1250c8a83e85SKevin Wolf                        &snapshot, errp);
1251f8882568SJes Sorensen }
1252f8882568SJes Sorensen 
125343de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay,
125443de7e2dSAlberto Garcia                            Error **errp)
125543de7e2dSAlberto Garcia {
125643de7e2dSAlberto Garcia     BlockdevSnapshot snapshot_data = {
125743de7e2dSAlberto Garcia         .node = (char *) node,
125843de7e2dSAlberto Garcia         .overlay = (char *) overlay
125943de7e2dSAlberto Garcia     };
126043de7e2dSAlberto Garcia 
126143de7e2dSAlberto Garcia     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
126243de7e2dSAlberto Garcia                        &snapshot_data, errp);
126343de7e2dSAlberto Garcia }
126443de7e2dSAlberto Garcia 
1265f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device,
1266f323bc9eSWenchao Xia                                          const char *name,
1267f323bc9eSWenchao Xia                                          Error **errp)
1268f323bc9eSWenchao Xia {
1269f323bc9eSWenchao Xia     BlockdevSnapshotInternal snapshot = {
1270f323bc9eSWenchao Xia         .device = (char *) device,
1271f323bc9eSWenchao Xia         .name = (char *) name
1272f323bc9eSWenchao Xia     };
1273f323bc9eSWenchao Xia 
1274f323bc9eSWenchao Xia     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1275f323bc9eSWenchao Xia                        &snapshot, errp);
1276f323bc9eSWenchao Xia }
1277f323bc9eSWenchao Xia 
127844e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
127944e3e053SWenchao Xia                                                          bool has_id,
128044e3e053SWenchao Xia                                                          const char *id,
128144e3e053SWenchao Xia                                                          bool has_name,
128244e3e053SWenchao Xia                                                          const char *name,
128344e3e053SWenchao Xia                                                          Error **errp)
128444e3e053SWenchao Xia {
1285a0e8544cSFam Zheng     BlockDriverState *bs;
1286a0e8544cSFam Zheng     BlockBackend *blk;
12874ef3982aSStefan Hajnoczi     AioContext *aio_context;
128844e3e053SWenchao Xia     QEMUSnapshotInfo sn;
128944e3e053SWenchao Xia     Error *local_err = NULL;
129044e3e053SWenchao Xia     SnapshotInfo *info = NULL;
129144e3e053SWenchao Xia     int ret;
129244e3e053SWenchao Xia 
1293a0e8544cSFam Zheng     blk = blk_by_name(device);
1294a0e8544cSFam Zheng     if (!blk) {
129575158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
129675158ebbSMarkus Armbruster                   "Device '%s' not found", device);
129744e3e053SWenchao Xia         return NULL;
129844e3e053SWenchao Xia     }
12995433c24fSMax Reitz 
13005433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
13015433c24fSMax Reitz     aio_context_acquire(aio_context);
130244e3e053SWenchao Xia 
130344e3e053SWenchao Xia     if (!has_id) {
130444e3e053SWenchao Xia         id = NULL;
130544e3e053SWenchao Xia     }
130644e3e053SWenchao Xia 
130744e3e053SWenchao Xia     if (!has_name) {
130844e3e053SWenchao Xia         name = NULL;
130944e3e053SWenchao Xia     }
131044e3e053SWenchao Xia 
131144e3e053SWenchao Xia     if (!id && !name) {
131244e3e053SWenchao Xia         error_setg(errp, "Name or id must be provided");
13135433c24fSMax Reitz         goto out_aio_context;
131444e3e053SWenchao Xia     }
131544e3e053SWenchao Xia 
13165433c24fSMax Reitz     if (!blk_is_available(blk)) {
13175433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
13185433c24fSMax Reitz         goto out_aio_context;
13195433c24fSMax Reitz     }
13205433c24fSMax Reitz     bs = blk_bs(blk);
13214ef3982aSStefan Hajnoczi 
13220b928854SStefan Hajnoczi     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
13230b928854SStefan Hajnoczi         goto out_aio_context;
13240b928854SStefan Hajnoczi     }
13250b928854SStefan Hajnoczi 
132644e3e053SWenchao Xia     ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
132784d18f06SMarkus Armbruster     if (local_err) {
132844e3e053SWenchao Xia         error_propagate(errp, local_err);
13294ef3982aSStefan Hajnoczi         goto out_aio_context;
133044e3e053SWenchao Xia     }
133144e3e053SWenchao Xia     if (!ret) {
133244e3e053SWenchao Xia         error_setg(errp,
133344e3e053SWenchao Xia                    "Snapshot with id '%s' and name '%s' does not exist on "
133444e3e053SWenchao Xia                    "device '%s'",
133544e3e053SWenchao Xia                    STR_OR_NULL(id), STR_OR_NULL(name), device);
13364ef3982aSStefan Hajnoczi         goto out_aio_context;
133744e3e053SWenchao Xia     }
133844e3e053SWenchao Xia 
133944e3e053SWenchao Xia     bdrv_snapshot_delete(bs, id, name, &local_err);
134084d18f06SMarkus Armbruster     if (local_err) {
134144e3e053SWenchao Xia         error_propagate(errp, local_err);
13424ef3982aSStefan Hajnoczi         goto out_aio_context;
134344e3e053SWenchao Xia     }
134444e3e053SWenchao Xia 
13454ef3982aSStefan Hajnoczi     aio_context_release(aio_context);
13464ef3982aSStefan Hajnoczi 
13475839e53bSMarkus Armbruster     info = g_new0(SnapshotInfo, 1);
134844e3e053SWenchao Xia     info->id = g_strdup(sn.id_str);
134944e3e053SWenchao Xia     info->name = g_strdup(sn.name);
135044e3e053SWenchao Xia     info->date_nsec = sn.date_nsec;
135144e3e053SWenchao Xia     info->date_sec = sn.date_sec;
135244e3e053SWenchao Xia     info->vm_state_size = sn.vm_state_size;
135344e3e053SWenchao Xia     info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
135444e3e053SWenchao Xia     info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
135544e3e053SWenchao Xia 
135644e3e053SWenchao Xia     return info;
13574ef3982aSStefan Hajnoczi 
13584ef3982aSStefan Hajnoczi out_aio_context:
13594ef3982aSStefan Hajnoczi     aio_context_release(aio_context);
13604ef3982aSStefan Hajnoczi     return NULL;
136144e3e053SWenchao Xia }
13628802d1fdSJeff Cody 
1363341ebc2fSJohn Snow /**
1364341ebc2fSJohn Snow  * block_dirty_bitmap_lookup:
1365341ebc2fSJohn Snow  * Return a dirty bitmap (if present), after validating
1366341ebc2fSJohn Snow  * the node reference and bitmap names.
1367341ebc2fSJohn Snow  *
1368341ebc2fSJohn Snow  * @node: The name of the BDS node to search for bitmaps
1369341ebc2fSJohn Snow  * @name: The name of the bitmap to search for
1370341ebc2fSJohn Snow  * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1371341ebc2fSJohn Snow  * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1372341ebc2fSJohn Snow  * @errp: Output pointer for error information. Can be NULL.
1373341ebc2fSJohn Snow  *
1374341ebc2fSJohn Snow  * @return: A bitmap object on success, or NULL on failure.
1375341ebc2fSJohn Snow  */
1376341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1377341ebc2fSJohn Snow                                                   const char *name,
1378341ebc2fSJohn Snow                                                   BlockDriverState **pbs,
1379341ebc2fSJohn Snow                                                   AioContext **paio,
1380341ebc2fSJohn Snow                                                   Error **errp)
1381341ebc2fSJohn Snow {
1382341ebc2fSJohn Snow     BlockDriverState *bs;
1383341ebc2fSJohn Snow     BdrvDirtyBitmap *bitmap;
1384341ebc2fSJohn Snow     AioContext *aio_context;
1385341ebc2fSJohn Snow 
1386341ebc2fSJohn Snow     if (!node) {
1387341ebc2fSJohn Snow         error_setg(errp, "Node cannot be NULL");
1388341ebc2fSJohn Snow         return NULL;
1389341ebc2fSJohn Snow     }
1390341ebc2fSJohn Snow     if (!name) {
1391341ebc2fSJohn Snow         error_setg(errp, "Bitmap name cannot be NULL");
1392341ebc2fSJohn Snow         return NULL;
1393341ebc2fSJohn Snow     }
1394341ebc2fSJohn Snow     bs = bdrv_lookup_bs(node, node, NULL);
1395341ebc2fSJohn Snow     if (!bs) {
1396341ebc2fSJohn Snow         error_setg(errp, "Node '%s' not found", node);
1397341ebc2fSJohn Snow         return NULL;
1398341ebc2fSJohn Snow     }
1399341ebc2fSJohn Snow 
1400341ebc2fSJohn Snow     aio_context = bdrv_get_aio_context(bs);
1401341ebc2fSJohn Snow     aio_context_acquire(aio_context);
1402341ebc2fSJohn Snow 
1403341ebc2fSJohn Snow     bitmap = bdrv_find_dirty_bitmap(bs, name);
1404341ebc2fSJohn Snow     if (!bitmap) {
1405341ebc2fSJohn Snow         error_setg(errp, "Dirty bitmap '%s' not found", name);
1406341ebc2fSJohn Snow         goto fail;
1407341ebc2fSJohn Snow     }
1408341ebc2fSJohn Snow 
1409341ebc2fSJohn Snow     if (pbs) {
1410341ebc2fSJohn Snow         *pbs = bs;
1411341ebc2fSJohn Snow     }
1412341ebc2fSJohn Snow     if (paio) {
1413341ebc2fSJohn Snow         *paio = aio_context;
1414341ebc2fSJohn Snow     } else {
1415341ebc2fSJohn Snow         aio_context_release(aio_context);
1416341ebc2fSJohn Snow     }
1417341ebc2fSJohn Snow 
1418341ebc2fSJohn Snow     return bitmap;
1419341ebc2fSJohn Snow 
1420341ebc2fSJohn Snow  fail:
1421341ebc2fSJohn Snow     aio_context_release(aio_context);
1422341ebc2fSJohn Snow     return NULL;
1423341ebc2fSJohn Snow }
1424341ebc2fSJohn Snow 
1425b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */
1426ba0c86a3SWenchao Xia 
142750f43f0fSJohn Snow typedef struct BlkActionState BlkActionState;
1428ba0c86a3SWenchao Xia 
142950f43f0fSJohn Snow /**
143050f43f0fSJohn Snow  * BlkActionOps:
143150f43f0fSJohn Snow  * Table of operations that define an Action.
143250f43f0fSJohn Snow  *
143350f43f0fSJohn Snow  * @instance_size: Size of state struct, in bytes.
143450f43f0fSJohn Snow  * @prepare: Prepare the work, must NOT be NULL.
143550f43f0fSJohn Snow  * @commit: Commit the changes, can be NULL.
143650f43f0fSJohn Snow  * @abort: Abort the changes on fail, can be NULL.
143750f43f0fSJohn Snow  * @clean: Clean up resources after all transaction actions have called
143850f43f0fSJohn Snow  *         commit() or abort(). Can be NULL.
143950f43f0fSJohn Snow  *
144050f43f0fSJohn Snow  * Only prepare() may fail. In a single transaction, only one of commit() or
144150f43f0fSJohn Snow  * abort() will be called. clean() will always be called if it is present.
1442ba0c86a3SWenchao Xia  */
144350f43f0fSJohn Snow typedef struct BlkActionOps {
144450f43f0fSJohn Snow     size_t instance_size;
144550f43f0fSJohn Snow     void (*prepare)(BlkActionState *common, Error **errp);
144650f43f0fSJohn Snow     void (*commit)(BlkActionState *common);
144750f43f0fSJohn Snow     void (*abort)(BlkActionState *common);
144850f43f0fSJohn Snow     void (*clean)(BlkActionState *common);
144950f43f0fSJohn Snow } BlkActionOps;
145050f43f0fSJohn Snow 
145150f43f0fSJohn Snow /**
145250f43f0fSJohn Snow  * BlkActionState:
145350f43f0fSJohn Snow  * Describes one Action's state within a Transaction.
145450f43f0fSJohn Snow  *
145550f43f0fSJohn Snow  * @action: QAPI-defined enum identifying which Action to perform.
145650f43f0fSJohn Snow  * @ops: Table of ActionOps this Action can perform.
145794d16a64SJohn Snow  * @block_job_txn: Transaction which this action belongs to.
145850f43f0fSJohn Snow  * @entry: List membership for all Actions in this Transaction.
145950f43f0fSJohn Snow  *
146050f43f0fSJohn Snow  * This structure must be arranged as first member in a subclassed type,
146150f43f0fSJohn Snow  * assuming that the compiler will also arrange it to the same offsets as the
146250f43f0fSJohn Snow  * base class.
146350f43f0fSJohn Snow  */
146450f43f0fSJohn Snow struct BlkActionState {
1465c8a83e85SKevin Wolf     TransactionAction *action;
146650f43f0fSJohn Snow     const BlkActionOps *ops;
146794d16a64SJohn Snow     BlockJobTxn *block_job_txn;
146894d16a64SJohn Snow     TransactionProperties *txn_props;
146950f43f0fSJohn Snow     QSIMPLEQ_ENTRY(BlkActionState) entry;
1470ba0c86a3SWenchao Xia };
1471ba0c86a3SWenchao Xia 
1472bbe86010SWenchao Xia /* internal snapshot private data */
1473bbe86010SWenchao Xia typedef struct InternalSnapshotState {
147450f43f0fSJohn Snow     BlkActionState common;
1475bbe86010SWenchao Xia     BlockDriverState *bs;
14765d6e96efSStefan Hajnoczi     AioContext *aio_context;
1477bbe86010SWenchao Xia     QEMUSnapshotInfo sn;
1478507306ccSFam Zheng     bool created;
1479bbe86010SWenchao Xia } InternalSnapshotState;
1480bbe86010SWenchao Xia 
148194d16a64SJohn Snow 
148294d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp)
148394d16a64SJohn Snow {
148494d16a64SJohn Snow     if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
148594d16a64SJohn Snow         error_setg(errp,
148694d16a64SJohn Snow                    "Action '%s' does not support Transaction property "
148794d16a64SJohn Snow                    "completion-mode = %s",
148894d16a64SJohn Snow                    TransactionActionKind_lookup[s->action->type],
148994d16a64SJohn Snow                    ActionCompletionMode_lookup[s->txn_props->completion_mode]);
149094d16a64SJohn Snow         return -1;
149194d16a64SJohn Snow     }
149294d16a64SJohn Snow     return 0;
149394d16a64SJohn Snow }
149494d16a64SJohn Snow 
149550f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common,
1496bbe86010SWenchao Xia                                       Error **errp)
1497bbe86010SWenchao Xia {
1498f70edf99SMarkus Armbruster     Error *local_err = NULL;
1499bbe86010SWenchao Xia     const char *device;
1500bbe86010SWenchao Xia     const char *name;
1501a0e8544cSFam Zheng     BlockBackend *blk;
1502bbe86010SWenchao Xia     BlockDriverState *bs;
1503bbe86010SWenchao Xia     QEMUSnapshotInfo old_sn, *sn;
1504bbe86010SWenchao Xia     bool ret;
1505bbe86010SWenchao Xia     qemu_timeval tv;
1506bbe86010SWenchao Xia     BlockdevSnapshotInternal *internal;
1507bbe86010SWenchao Xia     InternalSnapshotState *state;
1508bbe86010SWenchao Xia     int ret1;
1509bbe86010SWenchao Xia 
15106a8f9661SEric Blake     g_assert(common->action->type ==
1511bbe86010SWenchao Xia              TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
15126a8f9661SEric Blake     internal = common->action->u.blockdev_snapshot_internal_sync;
1513bbe86010SWenchao Xia     state = DO_UPCAST(InternalSnapshotState, common, common);
1514bbe86010SWenchao Xia 
1515bbe86010SWenchao Xia     /* 1. parse input */
1516bbe86010SWenchao Xia     device = internal->device;
1517bbe86010SWenchao Xia     name = internal->name;
1518bbe86010SWenchao Xia 
1519bbe86010SWenchao Xia     /* 2. check for validation */
152094d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
152194d16a64SJohn Snow         return;
152294d16a64SJohn Snow     }
152394d16a64SJohn Snow 
1524a0e8544cSFam Zheng     blk = blk_by_name(device);
1525a0e8544cSFam Zheng     if (!blk) {
152675158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
152775158ebbSMarkus Armbruster                   "Device '%s' not found", device);
1528bbe86010SWenchao Xia         return;
1529bbe86010SWenchao Xia     }
1530bbe86010SWenchao Xia 
15315d6e96efSStefan Hajnoczi     /* AioContext is released in .clean() */
15325433c24fSMax Reitz     state->aio_context = blk_get_aio_context(blk);
15335d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
15345d6e96efSStefan Hajnoczi 
15355433c24fSMax Reitz     if (!blk_is_available(blk)) {
1536c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1537bbe86010SWenchao Xia         return;
1538bbe86010SWenchao Xia     }
15395433c24fSMax Reitz     bs = blk_bs(blk);
1540bbe86010SWenchao Xia 
1541507306ccSFam Zheng     state->bs = bs;
1542507306ccSFam Zheng     bdrv_drained_begin(bs);
1543507306ccSFam Zheng 
15443dc7ca3cSStefan Hajnoczi     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
15453dc7ca3cSStefan Hajnoczi         return;
15463dc7ca3cSStefan Hajnoczi     }
15473dc7ca3cSStefan Hajnoczi 
1548bbe86010SWenchao Xia     if (bdrv_is_read_only(bs)) {
154981e5f78aSAlberto Garcia         error_setg(errp, "Device '%s' is read only", device);
1550bbe86010SWenchao Xia         return;
1551bbe86010SWenchao Xia     }
1552bbe86010SWenchao Xia 
1553bbe86010SWenchao Xia     if (!bdrv_can_snapshot(bs)) {
155481e5f78aSAlberto Garcia         error_setg(errp, "Block format '%s' used by device '%s' "
155581e5f78aSAlberto Garcia                    "does not support internal snapshots",
155681e5f78aSAlberto Garcia                    bs->drv->format_name, device);
1557bbe86010SWenchao Xia         return;
1558bbe86010SWenchao Xia     }
1559bbe86010SWenchao Xia 
1560bbe86010SWenchao Xia     if (!strlen(name)) {
1561bbe86010SWenchao Xia         error_setg(errp, "Name is empty");
1562bbe86010SWenchao Xia         return;
1563bbe86010SWenchao Xia     }
1564bbe86010SWenchao Xia 
1565bbe86010SWenchao Xia     /* check whether a snapshot with name exist */
1566f70edf99SMarkus Armbruster     ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1567f70edf99SMarkus Armbruster                                             &local_err);
1568f70edf99SMarkus Armbruster     if (local_err) {
1569f70edf99SMarkus Armbruster         error_propagate(errp, local_err);
1570bbe86010SWenchao Xia         return;
1571bbe86010SWenchao Xia     } else if (ret) {
1572bbe86010SWenchao Xia         error_setg(errp,
1573bbe86010SWenchao Xia                    "Snapshot with name '%s' already exists on device '%s'",
1574bbe86010SWenchao Xia                    name, device);
1575bbe86010SWenchao Xia         return;
1576bbe86010SWenchao Xia     }
1577bbe86010SWenchao Xia 
1578bbe86010SWenchao Xia     /* 3. take the snapshot */
1579bbe86010SWenchao Xia     sn = &state->sn;
1580bbe86010SWenchao Xia     pstrcpy(sn->name, sizeof(sn->name), name);
1581bbe86010SWenchao Xia     qemu_gettimeofday(&tv);
1582bbe86010SWenchao Xia     sn->date_sec = tv.tv_sec;
1583bbe86010SWenchao Xia     sn->date_nsec = tv.tv_usec * 1000;
1584bbe86010SWenchao Xia     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1585bbe86010SWenchao Xia 
1586bbe86010SWenchao Xia     ret1 = bdrv_snapshot_create(bs, sn);
1587bbe86010SWenchao Xia     if (ret1 < 0) {
1588bbe86010SWenchao Xia         error_setg_errno(errp, -ret1,
1589bbe86010SWenchao Xia                          "Failed to create snapshot '%s' on device '%s'",
1590bbe86010SWenchao Xia                          name, device);
1591bbe86010SWenchao Xia         return;
1592bbe86010SWenchao Xia     }
1593bbe86010SWenchao Xia 
1594bbe86010SWenchao Xia     /* 4. succeed, mark a snapshot is created */
1595507306ccSFam Zheng     state->created = true;
1596bbe86010SWenchao Xia }
1597bbe86010SWenchao Xia 
159850f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common)
1599bbe86010SWenchao Xia {
1600bbe86010SWenchao Xia     InternalSnapshotState *state =
1601bbe86010SWenchao Xia                              DO_UPCAST(InternalSnapshotState, common, common);
1602bbe86010SWenchao Xia     BlockDriverState *bs = state->bs;
1603bbe86010SWenchao Xia     QEMUSnapshotInfo *sn = &state->sn;
1604bbe86010SWenchao Xia     Error *local_error = NULL;
1605bbe86010SWenchao Xia 
1606507306ccSFam Zheng     if (!state->created) {
1607bbe86010SWenchao Xia         return;
1608bbe86010SWenchao Xia     }
1609bbe86010SWenchao Xia 
1610bbe86010SWenchao Xia     if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1611c29b77f9SMarkus Armbruster         error_reportf_err(local_error,
1612c29b77f9SMarkus Armbruster                           "Failed to delete snapshot with id '%s' and "
1613c29b77f9SMarkus Armbruster                           "name '%s' on device '%s' in abort: ",
1614c29b77f9SMarkus Armbruster                           sn->id_str, sn->name,
1615c29b77f9SMarkus Armbruster                           bdrv_get_device_name(bs));
1616bbe86010SWenchao Xia     }
1617bbe86010SWenchao Xia }
1618bbe86010SWenchao Xia 
161950f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common)
16205d6e96efSStefan Hajnoczi {
16215d6e96efSStefan Hajnoczi     InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
16225d6e96efSStefan Hajnoczi                                              common, common);
16235d6e96efSStefan Hajnoczi 
16245d6e96efSStefan Hajnoczi     if (state->aio_context) {
1625507306ccSFam Zheng         if (state->bs) {
1626507306ccSFam Zheng             bdrv_drained_end(state->bs);
1627507306ccSFam Zheng         }
16285d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
16295d6e96efSStefan Hajnoczi     }
16305d6e96efSStefan Hajnoczi }
16315d6e96efSStefan Hajnoczi 
1632ba0c86a3SWenchao Xia /* external snapshot private data */
1633ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState {
163450f43f0fSJohn Snow     BlkActionState common;
16358802d1fdSJeff Cody     BlockDriverState *old_bs;
16368802d1fdSJeff Cody     BlockDriverState *new_bs;
16375d6e96efSStefan Hajnoczi     AioContext *aio_context;
1638ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState;
16398802d1fdSJeff Cody 
164050f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common,
16419b9877eeSWenchao Xia                                       Error **errp)
16429b9877eeSWenchao Xia {
164343de7e2dSAlberto Garcia     int flags = 0, ret;
164443de7e2dSAlberto Garcia     QDict *options = NULL;
16459b9877eeSWenchao Xia     Error *local_err = NULL;
164643de7e2dSAlberto Garcia     /* Device and node name of the image to generate the snapshot from */
1647e2a31e87SWenchao Xia     const char *device;
16480901f67eSBenoît Canet     const char *node_name;
164943de7e2dSAlberto Garcia     /* Reference to the new image (for 'blockdev-snapshot') */
165043de7e2dSAlberto Garcia     const char *snapshot_ref;
165143de7e2dSAlberto Garcia     /* File name of the new image (for 'blockdev-snapshot-sync') */
1652e2a31e87SWenchao Xia     const char *new_image_file;
1653ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1654ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1655c8a83e85SKevin Wolf     TransactionAction *action = common->action;
16569b9877eeSWenchao Xia 
165743de7e2dSAlberto Garcia     /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
165843de7e2dSAlberto Garcia      * purpose but a different set of parameters */
165943de7e2dSAlberto Garcia     switch (action->type) {
166043de7e2dSAlberto Garcia     case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
166143de7e2dSAlberto Garcia         {
166243de7e2dSAlberto Garcia             BlockdevSnapshot *s = action->u.blockdev_snapshot;
166343de7e2dSAlberto Garcia             device = s->node;
166443de7e2dSAlberto Garcia             node_name = s->node;
166543de7e2dSAlberto Garcia             new_image_file = NULL;
166643de7e2dSAlberto Garcia             snapshot_ref = s->overlay;
1667e2a31e87SWenchao Xia         }
166843de7e2dSAlberto Garcia         break;
166943de7e2dSAlberto Garcia     case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
167043de7e2dSAlberto Garcia         {
167143de7e2dSAlberto Garcia             BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
167243de7e2dSAlberto Garcia             device = s->has_device ? s->device : NULL;
167343de7e2dSAlberto Garcia             node_name = s->has_node_name ? s->node_name : NULL;
167443de7e2dSAlberto Garcia             new_image_file = s->snapshot_file;
167543de7e2dSAlberto Garcia             snapshot_ref = NULL;
167643de7e2dSAlberto Garcia         }
167743de7e2dSAlberto Garcia         break;
167843de7e2dSAlberto Garcia     default:
167943de7e2dSAlberto Garcia         g_assert_not_reached();
1680e2a31e87SWenchao Xia     }
1681e2a31e87SWenchao Xia 
1682e2a31e87SWenchao Xia     /* start processing */
168394d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
168494d16a64SJohn Snow         return;
168594d16a64SJohn Snow     }
168694d16a64SJohn Snow 
168743de7e2dSAlberto Garcia     state->old_bs = bdrv_lookup_bs(device, node_name, errp);
168843de7e2dSAlberto Garcia     if (!state->old_bs) {
16899b9877eeSWenchao Xia         return;
16909b9877eeSWenchao Xia     }
16919b9877eeSWenchao Xia 
16925d6e96efSStefan Hajnoczi     /* Acquire AioContext now so any threads operating on old_bs stop */
16935d6e96efSStefan Hajnoczi     state->aio_context = bdrv_get_aio_context(state->old_bs);
16945d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
1695da763e83SFam Zheng     bdrv_drained_begin(state->old_bs);
16965d6e96efSStefan Hajnoczi 
1697ba5d6ab6SStefan Hajnoczi     if (!bdrv_is_inserted(state->old_bs)) {
1698c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
16999b9877eeSWenchao Xia         return;
17009b9877eeSWenchao Xia     }
17019b9877eeSWenchao Xia 
17023718d8abSFam Zheng     if (bdrv_op_is_blocked(state->old_bs,
17033718d8abSFam Zheng                            BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
17049b9877eeSWenchao Xia         return;
17059b9877eeSWenchao Xia     }
17069b9877eeSWenchao Xia 
1707ba5d6ab6SStefan Hajnoczi     if (!bdrv_is_read_only(state->old_bs)) {
1708ba5d6ab6SStefan Hajnoczi         if (bdrv_flush(state->old_bs)) {
1709c6bd8c70SMarkus Armbruster             error_setg(errp, QERR_IO_ERROR);
17109b9877eeSWenchao Xia             return;
17119b9877eeSWenchao Xia         }
17129b9877eeSWenchao Xia     }
17139b9877eeSWenchao Xia 
1714212a5a8fSBenoît Canet     if (!bdrv_is_first_non_filter(state->old_bs)) {
1715c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1716f6186f49SBenoît Canet         return;
1717f6186f49SBenoît Canet     }
1718f6186f49SBenoît Canet 
171943de7e2dSAlberto Garcia     if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
172043de7e2dSAlberto Garcia         BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
172143de7e2dSAlberto Garcia         const char *format = s->has_format ? s->format : "qcow2";
172243de7e2dSAlberto Garcia         enum NewImageMode mode;
172343de7e2dSAlberto Garcia         const char *snapshot_node_name =
172443de7e2dSAlberto Garcia             s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
172543de7e2dSAlberto Garcia 
172643de7e2dSAlberto Garcia         if (node_name && !snapshot_node_name) {
172743de7e2dSAlberto Garcia             error_setg(errp, "New snapshot node name missing");
172843de7e2dSAlberto Garcia             return;
172943de7e2dSAlberto Garcia         }
173043de7e2dSAlberto Garcia 
173143de7e2dSAlberto Garcia         if (snapshot_node_name &&
173243de7e2dSAlberto Garcia             bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
173343de7e2dSAlberto Garcia             error_setg(errp, "New snapshot node name already in use");
173443de7e2dSAlberto Garcia             return;
173543de7e2dSAlberto Garcia         }
173643de7e2dSAlberto Garcia 
1737ba5d6ab6SStefan Hajnoczi         flags = state->old_bs->open_flags;
17389b9877eeSWenchao Xia 
17399b9877eeSWenchao Xia         /* create new image w/backing file */
174043de7e2dSAlberto Garcia         mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
17419b9877eeSWenchao Xia         if (mode != NEW_IMAGE_MODE_EXISTING) {
17429b9877eeSWenchao Xia             bdrv_img_create(new_image_file, format,
1743ba5d6ab6SStefan Hajnoczi                             state->old_bs->filename,
1744ba5d6ab6SStefan Hajnoczi                             state->old_bs->drv->format_name,
17459b9877eeSWenchao Xia                             NULL, -1, flags, &local_err, false);
174684d18f06SMarkus Armbruster             if (local_err) {
17479b9877eeSWenchao Xia                 error_propagate(errp, local_err);
17489b9877eeSWenchao Xia                 return;
17499b9877eeSWenchao Xia             }
17509b9877eeSWenchao Xia         }
17519b9877eeSWenchao Xia 
17520901f67eSBenoît Canet         options = qdict_new();
175343de7e2dSAlberto Garcia         if (s->has_snapshot_node_name) {
17540901f67eSBenoît Canet             qdict_put(options, "node-name",
17550901f67eSBenoît Canet                       qstring_from_str(snapshot_node_name));
17560901f67eSBenoît Canet         }
1757e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
17580901f67eSBenoît Canet 
175943de7e2dSAlberto Garcia         flags |= BDRV_O_NO_BACKING;
176043de7e2dSAlberto Garcia     }
176143de7e2dSAlberto Garcia 
1762f67503e5SMax Reitz     assert(state->new_bs == NULL);
176343de7e2dSAlberto Garcia     ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options,
176443de7e2dSAlberto Garcia                     flags, errp);
1765f67503e5SMax Reitz     /* We will manually add the backing_hd field to the bs later */
17669b9877eeSWenchao Xia     if (ret != 0) {
176743de7e2dSAlberto Garcia         return;
176843de7e2dSAlberto Garcia     }
176943de7e2dSAlberto Garcia 
177043de7e2dSAlberto Garcia     if (state->new_bs->blk != NULL) {
177143de7e2dSAlberto Garcia         error_setg(errp, "The snapshot is already in use by %s",
177243de7e2dSAlberto Garcia                    blk_name(state->new_bs->blk));
177343de7e2dSAlberto Garcia         return;
177443de7e2dSAlberto Garcia     }
177543de7e2dSAlberto Garcia 
177643de7e2dSAlberto Garcia     if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
177743de7e2dSAlberto Garcia                            errp)) {
177843de7e2dSAlberto Garcia         return;
177943de7e2dSAlberto Garcia     }
178043de7e2dSAlberto Garcia 
178143de7e2dSAlberto Garcia     if (state->new_bs->backing != NULL) {
178243de7e2dSAlberto Garcia         error_setg(errp, "The snapshot already has a backing image");
178308b24cfeSAlberto Garcia         return;
178408b24cfeSAlberto Garcia     }
178508b24cfeSAlberto Garcia 
178608b24cfeSAlberto Garcia     if (!state->new_bs->drv->supports_backing) {
178708b24cfeSAlberto Garcia         error_setg(errp, "The snapshot does not support backing images");
17889b9877eeSWenchao Xia     }
17899b9877eeSWenchao Xia }
17909b9877eeSWenchao Xia 
179150f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common)
17923b0047e8SWenchao Xia {
1793ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1794ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1795ba0c86a3SWenchao Xia 
17965d6e96efSStefan Hajnoczi     bdrv_set_aio_context(state->new_bs, state->aio_context);
17975d6e96efSStefan Hajnoczi 
1798ba5d6ab6SStefan Hajnoczi     /* This removes our old bs and adds the new bs */
1799ba5d6ab6SStefan Hajnoczi     bdrv_append(state->new_bs, state->old_bs);
18003b0047e8SWenchao Xia     /* We don't need (or want) to use the transactional
18013b0047e8SWenchao Xia      * bdrv_reopen_multiple() across all the entries at once, because we
18023b0047e8SWenchao Xia      * don't want to abort all of them if one of them fails the reopen */
1803dd62f1caSKevin Wolf     bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
18043b0047e8SWenchao Xia                 NULL);
18053b0047e8SWenchao Xia }
18063b0047e8SWenchao Xia 
180750f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common)
180896b86bf7SWenchao Xia {
1809ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1810ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1811ba5d6ab6SStefan Hajnoczi     if (state->new_bs) {
18124f6fd349SFam Zheng         bdrv_unref(state->new_bs);
181396b86bf7SWenchao Xia     }
1814da763e83SFam Zheng }
1815da763e83SFam Zheng 
181650f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common)
1817da763e83SFam Zheng {
1818da763e83SFam Zheng     ExternalSnapshotState *state =
1819da763e83SFam Zheng                              DO_UPCAST(ExternalSnapshotState, common, common);
18205d6e96efSStefan Hajnoczi     if (state->aio_context) {
1821da763e83SFam Zheng         bdrv_drained_end(state->old_bs);
18225d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
18235d6e96efSStefan Hajnoczi     }
182496b86bf7SWenchao Xia }
182596b86bf7SWenchao Xia 
18263037f364SStefan Hajnoczi typedef struct DriveBackupState {
182750f43f0fSJohn Snow     BlkActionState common;
18283037f364SStefan Hajnoczi     BlockDriverState *bs;
18295d6e96efSStefan Hajnoczi     AioContext *aio_context;
18303037f364SStefan Hajnoczi     BlockJob *job;
18313037f364SStefan Hajnoczi } DriveBackupState;
18323037f364SStefan Hajnoczi 
183378f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target,
183478f51fdeSJohn Snow                             bool has_format, const char *format,
183578f51fdeSJohn Snow                             enum MirrorSyncMode sync,
183678f51fdeSJohn Snow                             bool has_mode, enum NewImageMode mode,
183778f51fdeSJohn Snow                             bool has_speed, int64_t speed,
183878f51fdeSJohn Snow                             bool has_bitmap, const char *bitmap,
183978f51fdeSJohn Snow                             bool has_on_source_error,
184078f51fdeSJohn Snow                             BlockdevOnError on_source_error,
184178f51fdeSJohn Snow                             bool has_on_target_error,
184278f51fdeSJohn Snow                             BlockdevOnError on_target_error,
184378f51fdeSJohn Snow                             BlockJobTxn *txn, Error **errp);
184478f51fdeSJohn Snow 
184550f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp)
18463037f364SStefan Hajnoczi {
18473037f364SStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1848a0e8544cSFam Zheng     BlockBackend *blk;
18493037f364SStefan Hajnoczi     DriveBackup *backup;
18503037f364SStefan Hajnoczi     Error *local_err = NULL;
18513037f364SStefan Hajnoczi 
18526a8f9661SEric Blake     assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
18536a8f9661SEric Blake     backup = common->action->u.drive_backup;
18543037f364SStefan Hajnoczi 
1855a0e8544cSFam Zheng     blk = blk_by_name(backup->device);
1856a0e8544cSFam Zheng     if (!blk) {
185775158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
185875158ebbSMarkus Armbruster                   "Device '%s' not found", backup->device);
18595d6e96efSStefan Hajnoczi         return;
18605d6e96efSStefan Hajnoczi     }
18615d6e96efSStefan Hajnoczi 
18621fdd4b7bSFam Zheng     if (!blk_is_available(blk)) {
18631fdd4b7bSFam Zheng         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
18641fdd4b7bSFam Zheng         return;
18651fdd4b7bSFam Zheng     }
18661fdd4b7bSFam Zheng 
18675d6e96efSStefan Hajnoczi     /* AioContext is released in .clean() */
18685433c24fSMax Reitz     state->aio_context = blk_get_aio_context(blk);
18695d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
18701fdd4b7bSFam Zheng     bdrv_drained_begin(blk_bs(blk));
18711fdd4b7bSFam Zheng     state->bs = blk_bs(blk);
18725d6e96efSStefan Hajnoczi 
187378f51fdeSJohn Snow     do_drive_backup(backup->device, backup->target,
18743037f364SStefan Hajnoczi                     backup->has_format, backup->format,
1875b53169eaSStefan Hajnoczi                     backup->sync,
18763037f364SStefan Hajnoczi                     backup->has_mode, backup->mode,
18773037f364SStefan Hajnoczi                     backup->has_speed, backup->speed,
1878d58d8453SJohn Snow                     backup->has_bitmap, backup->bitmap,
18793037f364SStefan Hajnoczi                     backup->has_on_source_error, backup->on_source_error,
18803037f364SStefan Hajnoczi                     backup->has_on_target_error, backup->on_target_error,
188194d16a64SJohn Snow                     common->block_job_txn, &local_err);
188284d18f06SMarkus Armbruster     if (local_err) {
18833037f364SStefan Hajnoczi         error_propagate(errp, local_err);
18843037f364SStefan Hajnoczi         return;
18853037f364SStefan Hajnoczi     }
18863037f364SStefan Hajnoczi 
18873037f364SStefan Hajnoczi     state->job = state->bs->job;
18883037f364SStefan Hajnoczi }
18893037f364SStefan Hajnoczi 
189050f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common)
18913037f364SStefan Hajnoczi {
18923037f364SStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
18933037f364SStefan Hajnoczi     BlockDriverState *bs = state->bs;
18943037f364SStefan Hajnoczi 
18953037f364SStefan Hajnoczi     /* Only cancel if it's the job we started */
18963037f364SStefan Hajnoczi     if (bs && bs->job && bs->job == state->job) {
18973037f364SStefan Hajnoczi         block_job_cancel_sync(bs->job);
18983037f364SStefan Hajnoczi     }
18993037f364SStefan Hajnoczi }
19003037f364SStefan Hajnoczi 
190150f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common)
19025d6e96efSStefan Hajnoczi {
19035d6e96efSStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
19045d6e96efSStefan Hajnoczi 
19055d6e96efSStefan Hajnoczi     if (state->aio_context) {
19061fdd4b7bSFam Zheng         bdrv_drained_end(state->bs);
19075d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
19085d6e96efSStefan Hajnoczi     }
19095d6e96efSStefan Hajnoczi }
19105d6e96efSStefan Hajnoczi 
1911bd8baecdSFam Zheng typedef struct BlockdevBackupState {
191250f43f0fSJohn Snow     BlkActionState common;
1913bd8baecdSFam Zheng     BlockDriverState *bs;
1914bd8baecdSFam Zheng     BlockJob *job;
1915bd8baecdSFam Zheng     AioContext *aio_context;
1916bd8baecdSFam Zheng } BlockdevBackupState;
1917bd8baecdSFam Zheng 
191878f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target,
191978f51fdeSJohn Snow                                enum MirrorSyncMode sync,
192078f51fdeSJohn Snow                                bool has_speed, int64_t speed,
192178f51fdeSJohn Snow                                bool has_on_source_error,
192278f51fdeSJohn Snow                                BlockdevOnError on_source_error,
192378f51fdeSJohn Snow                                bool has_on_target_error,
192478f51fdeSJohn Snow                                BlockdevOnError on_target_error,
192578f51fdeSJohn Snow                                BlockJobTxn *txn, Error **errp);
192678f51fdeSJohn Snow 
192750f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1928bd8baecdSFam Zheng {
1929bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1930bd8baecdSFam Zheng     BlockdevBackup *backup;
19315433c24fSMax Reitz     BlockBackend *blk, *target;
1932bd8baecdSFam Zheng     Error *local_err = NULL;
1933bd8baecdSFam Zheng 
19346a8f9661SEric Blake     assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
19356a8f9661SEric Blake     backup = common->action->u.blockdev_backup;
1936bd8baecdSFam Zheng 
1937a0e8544cSFam Zheng     blk = blk_by_name(backup->device);
1938a0e8544cSFam Zheng     if (!blk) {
19395b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", backup->device);
1940bd8baecdSFam Zheng         return;
1941bd8baecdSFam Zheng     }
1942bd8baecdSFam Zheng 
1943ff52bf36SFam Zheng     if (!blk_is_available(blk)) {
1944ff52bf36SFam Zheng         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1945ff52bf36SFam Zheng         return;
1946ff52bf36SFam Zheng     }
1947ff52bf36SFam Zheng 
19485433c24fSMax Reitz     target = blk_by_name(backup->target);
19495433c24fSMax Reitz     if (!target) {
19505b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", backup->target);
1951bd8baecdSFam Zheng         return;
1952bd8baecdSFam Zheng     }
1953bd8baecdSFam Zheng 
1954bd8baecdSFam Zheng     /* AioContext is released in .clean() */
19555433c24fSMax Reitz     state->aio_context = blk_get_aio_context(blk);
19565433c24fSMax Reitz     if (state->aio_context != blk_get_aio_context(target)) {
1957bd8baecdSFam Zheng         state->aio_context = NULL;
1958bd8baecdSFam Zheng         error_setg(errp, "Backup between two IO threads is not implemented");
1959bd8baecdSFam Zheng         return;
1960bd8baecdSFam Zheng     }
1961bd8baecdSFam Zheng     aio_context_acquire(state->aio_context);
1962ff52bf36SFam Zheng     state->bs = blk_bs(blk);
1963ff52bf36SFam Zheng     bdrv_drained_begin(state->bs);
1964bd8baecdSFam Zheng 
196578f51fdeSJohn Snow     do_blockdev_backup(backup->device, backup->target,
1966bd8baecdSFam Zheng                        backup->sync,
1967bd8baecdSFam Zheng                        backup->has_speed, backup->speed,
1968bd8baecdSFam Zheng                        backup->has_on_source_error, backup->on_source_error,
1969bd8baecdSFam Zheng                        backup->has_on_target_error, backup->on_target_error,
197094d16a64SJohn Snow                        common->block_job_txn, &local_err);
1971bd8baecdSFam Zheng     if (local_err) {
1972bd8baecdSFam Zheng         error_propagate(errp, local_err);
1973bd8baecdSFam Zheng         return;
1974bd8baecdSFam Zheng     }
1975bd8baecdSFam Zheng 
1976bd8baecdSFam Zheng     state->job = state->bs->job;
1977bd8baecdSFam Zheng }
1978bd8baecdSFam Zheng 
197950f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common)
1980bd8baecdSFam Zheng {
1981bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1982bd8baecdSFam Zheng     BlockDriverState *bs = state->bs;
1983bd8baecdSFam Zheng 
1984bd8baecdSFam Zheng     /* Only cancel if it's the job we started */
1985bd8baecdSFam Zheng     if (bs && bs->job && bs->job == state->job) {
1986bd8baecdSFam Zheng         block_job_cancel_sync(bs->job);
1987bd8baecdSFam Zheng     }
1988bd8baecdSFam Zheng }
1989bd8baecdSFam Zheng 
199050f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common)
1991bd8baecdSFam Zheng {
1992bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1993bd8baecdSFam Zheng 
1994bd8baecdSFam Zheng     if (state->aio_context) {
1995ff52bf36SFam Zheng         bdrv_drained_end(state->bs);
1996bd8baecdSFam Zheng         aio_context_release(state->aio_context);
1997bd8baecdSFam Zheng     }
1998bd8baecdSFam Zheng }
1999bd8baecdSFam Zheng 
2000df9a681dSFam Zheng typedef struct BlockDirtyBitmapState {
200150f43f0fSJohn Snow     BlkActionState common;
2002df9a681dSFam Zheng     BdrvDirtyBitmap *bitmap;
2003df9a681dSFam Zheng     BlockDriverState *bs;
2004df9a681dSFam Zheng     AioContext *aio_context;
2005df9a681dSFam Zheng     HBitmap *backup;
2006df9a681dSFam Zheng     bool prepared;
2007df9a681dSFam Zheng } BlockDirtyBitmapState;
2008df9a681dSFam Zheng 
200950f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common,
2010df9a681dSFam Zheng                                            Error **errp)
2011df9a681dSFam Zheng {
2012df9a681dSFam Zheng     Error *local_err = NULL;
2013df9a681dSFam Zheng     BlockDirtyBitmapAdd *action;
2014df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2015df9a681dSFam Zheng                                              common, common);
2016df9a681dSFam Zheng 
201794d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
201894d16a64SJohn Snow         return;
201994d16a64SJohn Snow     }
202094d16a64SJohn Snow 
202150f43f0fSJohn Snow     action = common->action->u.block_dirty_bitmap_add;
2022df9a681dSFam Zheng     /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2023df9a681dSFam Zheng     qmp_block_dirty_bitmap_add(action->node, action->name,
2024df9a681dSFam Zheng                                action->has_granularity, action->granularity,
2025df9a681dSFam Zheng                                &local_err);
2026df9a681dSFam Zheng 
2027df9a681dSFam Zheng     if (!local_err) {
2028df9a681dSFam Zheng         state->prepared = true;
2029df9a681dSFam Zheng     } else {
2030df9a681dSFam Zheng         error_propagate(errp, local_err);
2031df9a681dSFam Zheng     }
2032df9a681dSFam Zheng }
2033df9a681dSFam Zheng 
203450f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common)
2035df9a681dSFam Zheng {
2036df9a681dSFam Zheng     BlockDirtyBitmapAdd *action;
2037df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2038df9a681dSFam Zheng                                              common, common);
2039df9a681dSFam Zheng 
204050f43f0fSJohn Snow     action = common->action->u.block_dirty_bitmap_add;
2041df9a681dSFam Zheng     /* Should not be able to fail: IF the bitmap was added via .prepare(),
2042df9a681dSFam Zheng      * then the node reference and bitmap name must have been valid.
2043df9a681dSFam Zheng      */
2044df9a681dSFam Zheng     if (state->prepared) {
2045df9a681dSFam Zheng         qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2046df9a681dSFam Zheng     }
2047df9a681dSFam Zheng }
2048df9a681dSFam Zheng 
204950f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
2050df9a681dSFam Zheng                                              Error **errp)
2051df9a681dSFam Zheng {
2052df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2053df9a681dSFam Zheng                                              common, common);
2054df9a681dSFam Zheng     BlockDirtyBitmap *action;
2055df9a681dSFam Zheng 
205694d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
205794d16a64SJohn Snow         return;
205894d16a64SJohn Snow     }
205994d16a64SJohn Snow 
206050f43f0fSJohn Snow     action = common->action->u.block_dirty_bitmap_clear;
2061df9a681dSFam Zheng     state->bitmap = block_dirty_bitmap_lookup(action->node,
2062df9a681dSFam Zheng                                               action->name,
2063df9a681dSFam Zheng                                               &state->bs,
2064df9a681dSFam Zheng                                               &state->aio_context,
2065df9a681dSFam Zheng                                               errp);
2066df9a681dSFam Zheng     if (!state->bitmap) {
2067df9a681dSFam Zheng         return;
2068df9a681dSFam Zheng     }
2069df9a681dSFam Zheng 
2070df9a681dSFam Zheng     if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2071df9a681dSFam Zheng         error_setg(errp, "Cannot modify a frozen bitmap");
2072df9a681dSFam Zheng         return;
2073df9a681dSFam Zheng     } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2074df9a681dSFam Zheng         error_setg(errp, "Cannot clear a disabled bitmap");
2075df9a681dSFam Zheng         return;
2076df9a681dSFam Zheng     }
2077df9a681dSFam Zheng 
2078df9a681dSFam Zheng     bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2079df9a681dSFam Zheng     /* AioContext is released in .clean() */
2080df9a681dSFam Zheng }
2081df9a681dSFam Zheng 
208250f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common)
2083df9a681dSFam Zheng {
2084df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2085df9a681dSFam Zheng                                              common, common);
2086df9a681dSFam Zheng 
2087df9a681dSFam Zheng     bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2088df9a681dSFam Zheng }
2089df9a681dSFam Zheng 
209050f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common)
2091df9a681dSFam Zheng {
2092df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2093df9a681dSFam Zheng                                              common, common);
2094df9a681dSFam Zheng 
2095df9a681dSFam Zheng     hbitmap_free(state->backup);
2096df9a681dSFam Zheng }
2097df9a681dSFam Zheng 
209850f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common)
2099df9a681dSFam Zheng {
2100df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2101df9a681dSFam Zheng                                              common, common);
2102df9a681dSFam Zheng 
2103df9a681dSFam Zheng     if (state->aio_context) {
2104df9a681dSFam Zheng         aio_context_release(state->aio_context);
2105df9a681dSFam Zheng     }
2106df9a681dSFam Zheng }
2107df9a681dSFam Zheng 
210850f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp)
210978b18b78SStefan Hajnoczi {
211078b18b78SStefan Hajnoczi     error_setg(errp, "Transaction aborted using Abort action");
211178b18b78SStefan Hajnoczi }
211278b18b78SStefan Hajnoczi 
211350f43f0fSJohn Snow static void abort_commit(BlkActionState *common)
211478b18b78SStefan Hajnoczi {
2115dfc6f865SStefan Weil     g_assert_not_reached(); /* this action never succeeds */
211678b18b78SStefan Hajnoczi }
211778b18b78SStefan Hajnoczi 
211850f43f0fSJohn Snow static const BlkActionOps actions[] = {
211943de7e2dSAlberto Garcia     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
212043de7e2dSAlberto Garcia         .instance_size = sizeof(ExternalSnapshotState),
212143de7e2dSAlberto Garcia         .prepare  = external_snapshot_prepare,
212243de7e2dSAlberto Garcia         .commit   = external_snapshot_commit,
212343de7e2dSAlberto Garcia         .abort = external_snapshot_abort,
21244ad6f3dbSAlberto Garcia         .clean = external_snapshot_clean,
212543de7e2dSAlberto Garcia     },
2126c8a83e85SKevin Wolf     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2127ba5d6ab6SStefan Hajnoczi         .instance_size = sizeof(ExternalSnapshotState),
2128ba0c86a3SWenchao Xia         .prepare  = external_snapshot_prepare,
2129ba0c86a3SWenchao Xia         .commit   = external_snapshot_commit,
2130ba0c86a3SWenchao Xia         .abort = external_snapshot_abort,
2131da763e83SFam Zheng         .clean = external_snapshot_clean,
2132ba0c86a3SWenchao Xia     },
21333037f364SStefan Hajnoczi     [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
21343037f364SStefan Hajnoczi         .instance_size = sizeof(DriveBackupState),
21353037f364SStefan Hajnoczi         .prepare = drive_backup_prepare,
21363037f364SStefan Hajnoczi         .abort = drive_backup_abort,
21375d6e96efSStefan Hajnoczi         .clean = drive_backup_clean,
21383037f364SStefan Hajnoczi     },
2139bd8baecdSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2140bd8baecdSFam Zheng         .instance_size = sizeof(BlockdevBackupState),
2141bd8baecdSFam Zheng         .prepare = blockdev_backup_prepare,
2142bd8baecdSFam Zheng         .abort = blockdev_backup_abort,
2143bd8baecdSFam Zheng         .clean = blockdev_backup_clean,
2144bd8baecdSFam Zheng     },
214578b18b78SStefan Hajnoczi     [TRANSACTION_ACTION_KIND_ABORT] = {
214650f43f0fSJohn Snow         .instance_size = sizeof(BlkActionState),
214778b18b78SStefan Hajnoczi         .prepare = abort_prepare,
214878b18b78SStefan Hajnoczi         .commit = abort_commit,
214978b18b78SStefan Hajnoczi     },
2150bbe86010SWenchao Xia     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2151bbe86010SWenchao Xia         .instance_size = sizeof(InternalSnapshotState),
2152bbe86010SWenchao Xia         .prepare  = internal_snapshot_prepare,
2153bbe86010SWenchao Xia         .abort = internal_snapshot_abort,
21545d6e96efSStefan Hajnoczi         .clean = internal_snapshot_clean,
2155bbe86010SWenchao Xia     },
2156df9a681dSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2157df9a681dSFam Zheng         .instance_size = sizeof(BlockDirtyBitmapState),
2158df9a681dSFam Zheng         .prepare = block_dirty_bitmap_add_prepare,
2159df9a681dSFam Zheng         .abort = block_dirty_bitmap_add_abort,
2160df9a681dSFam Zheng     },
2161df9a681dSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2162df9a681dSFam Zheng         .instance_size = sizeof(BlockDirtyBitmapState),
2163df9a681dSFam Zheng         .prepare = block_dirty_bitmap_clear_prepare,
2164df9a681dSFam Zheng         .commit = block_dirty_bitmap_clear_commit,
2165df9a681dSFam Zheng         .abort = block_dirty_bitmap_clear_abort,
2166df9a681dSFam Zheng         .clean = block_dirty_bitmap_clear_clean,
2167df9a681dSFam Zheng     }
2168ba0c86a3SWenchao Xia };
2169ba0c86a3SWenchao Xia 
217094d16a64SJohn Snow /**
217194d16a64SJohn Snow  * Allocate a TransactionProperties structure if necessary, and fill
217294d16a64SJohn Snow  * that structure with desired defaults if they are unset.
217394d16a64SJohn Snow  */
217494d16a64SJohn Snow static TransactionProperties *get_transaction_properties(
217594d16a64SJohn Snow     TransactionProperties *props)
217694d16a64SJohn Snow {
217794d16a64SJohn Snow     if (!props) {
217894d16a64SJohn Snow         props = g_new0(TransactionProperties, 1);
217994d16a64SJohn Snow     }
218094d16a64SJohn Snow 
218194d16a64SJohn Snow     if (!props->has_completion_mode) {
218294d16a64SJohn Snow         props->has_completion_mode = true;
218394d16a64SJohn Snow         props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
218494d16a64SJohn Snow     }
218594d16a64SJohn Snow 
218694d16a64SJohn Snow     return props;
218794d16a64SJohn Snow }
218894d16a64SJohn Snow 
21898802d1fdSJeff Cody /*
2190b756b9ceSStefan Hajnoczi  * 'Atomic' group operations.  The operations are performed as a set, and if
2191b756b9ceSStefan Hajnoczi  * any fail then we roll back all operations in the group.
21928802d1fdSJeff Cody  */
219394d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list,
219494d16a64SJohn Snow                      bool has_props,
219594d16a64SJohn Snow                      struct TransactionProperties *props,
219694d16a64SJohn Snow                      Error **errp)
21978802d1fdSJeff Cody {
2198c8a83e85SKevin Wolf     TransactionActionList *dev_entry = dev_list;
219994d16a64SJohn Snow     BlockJobTxn *block_job_txn = NULL;
220050f43f0fSJohn Snow     BlkActionState *state, *next;
220143e17041SLuiz Capitulino     Error *local_err = NULL;
22028802d1fdSJeff Cody 
220350f43f0fSJohn Snow     QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
22048802d1fdSJeff Cody     QSIMPLEQ_INIT(&snap_bdrv_states);
22058802d1fdSJeff Cody 
220694d16a64SJohn Snow     /* Does this transaction get canceled as a group on failure?
220794d16a64SJohn Snow      * If not, we don't really need to make a BlockJobTxn.
220894d16a64SJohn Snow      */
220994d16a64SJohn Snow     props = get_transaction_properties(props);
221094d16a64SJohn Snow     if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
221194d16a64SJohn Snow         block_job_txn = block_job_txn_new();
221294d16a64SJohn Snow     }
221394d16a64SJohn Snow 
2214b756b9ceSStefan Hajnoczi     /* drain all i/o before any operations */
22158802d1fdSJeff Cody     bdrv_drain_all();
22168802d1fdSJeff Cody 
2217b756b9ceSStefan Hajnoczi     /* We don't do anything in this loop that commits us to the operations */
22188802d1fdSJeff Cody     while (NULL != dev_entry) {
2219c8a83e85SKevin Wolf         TransactionAction *dev_info = NULL;
222050f43f0fSJohn Snow         const BlkActionOps *ops;
222152e7c241SPaolo Bonzini 
22228802d1fdSJeff Cody         dev_info = dev_entry->value;
22238802d1fdSJeff Cody         dev_entry = dev_entry->next;
22248802d1fdSJeff Cody 
22256a8f9661SEric Blake         assert(dev_info->type < ARRAY_SIZE(actions));
2226ba0c86a3SWenchao Xia 
22276a8f9661SEric Blake         ops = &actions[dev_info->type];
2228aa3fe714SMax Reitz         assert(ops->instance_size > 0);
2229aa3fe714SMax Reitz 
2230ba5d6ab6SStefan Hajnoczi         state = g_malloc0(ops->instance_size);
2231ba5d6ab6SStefan Hajnoczi         state->ops = ops;
2232ba5d6ab6SStefan Hajnoczi         state->action = dev_info;
223394d16a64SJohn Snow         state->block_job_txn = block_job_txn;
223494d16a64SJohn Snow         state->txn_props = props;
2235ba5d6ab6SStefan Hajnoczi         QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
22368802d1fdSJeff Cody 
2237ba5d6ab6SStefan Hajnoczi         state->ops->prepare(state, &local_err);
223884d18f06SMarkus Armbruster         if (local_err) {
22399b9877eeSWenchao Xia             error_propagate(errp, local_err);
22409b9877eeSWenchao Xia             goto delete_and_fail;
22419b9877eeSWenchao Xia         }
224252e7c241SPaolo Bonzini     }
22438802d1fdSJeff Cody 
2244ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2245f9ea81e8SStefan Hajnoczi         if (state->ops->commit) {
2246ba5d6ab6SStefan Hajnoczi             state->ops->commit(state);
22478802d1fdSJeff Cody         }
2248f9ea81e8SStefan Hajnoczi     }
22498802d1fdSJeff Cody 
22508802d1fdSJeff Cody     /* success */
22518802d1fdSJeff Cody     goto exit;
22528802d1fdSJeff Cody 
22538802d1fdSJeff Cody delete_and_fail:
2254b756b9ceSStefan Hajnoczi     /* failure, and it is all-or-none; roll back all operations */
2255ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2256ba5d6ab6SStefan Hajnoczi         if (state->ops->abort) {
2257ba5d6ab6SStefan Hajnoczi             state->ops->abort(state);
2258ba0c86a3SWenchao Xia         }
22598802d1fdSJeff Cody     }
22608802d1fdSJeff Cody exit:
2261ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2262ba5d6ab6SStefan Hajnoczi         if (state->ops->clean) {
2263ba5d6ab6SStefan Hajnoczi             state->ops->clean(state);
2264ba0c86a3SWenchao Xia         }
2265ba5d6ab6SStefan Hajnoczi         g_free(state);
22668802d1fdSJeff Cody     }
226794d16a64SJohn Snow     if (!has_props) {
226894d16a64SJohn Snow         qapi_free_TransactionProperties(props);
226994d16a64SJohn Snow     }
227094d16a64SJohn Snow     block_job_txn_unref(block_job_txn);
22718802d1fdSJeff Cody }
22728802d1fdSJeff Cody 
2273c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
2274666daa68SMarkus Armbruster {
227538f54bd1SMax Reitz     Error *local_err = NULL;
2276666daa68SMarkus Armbruster 
227738f54bd1SMax Reitz     qmp_blockdev_open_tray(device, has_force, force, &local_err);
227838f54bd1SMax Reitz     if (local_err) {
227938f54bd1SMax Reitz         error_propagate(errp, local_err);
2280c245b6a3SLuiz Capitulino         return;
2281666daa68SMarkus Armbruster     }
228292d48558SLuiz Capitulino 
22836e0abc25SMax Reitz     qmp_x_blockdev_remove_medium(device, errp);
2284666daa68SMarkus Armbruster }
2285666daa68SMarkus Armbruster 
228612d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device,
228712d3ba82SBenoît Canet                       bool has_node_name, const char *node_name,
228812d3ba82SBenoît Canet                       const char *password, Error **errp)
2289666daa68SMarkus Armbruster {
229012d3ba82SBenoît Canet     Error *local_err = NULL;
2291666daa68SMarkus Armbruster     BlockDriverState *bs;
2292e3442099SStefan Hajnoczi     AioContext *aio_context;
2293666daa68SMarkus Armbruster 
229412d3ba82SBenoît Canet     bs = bdrv_lookup_bs(has_device ? device : NULL,
229512d3ba82SBenoît Canet                         has_node_name ? node_name : NULL,
229612d3ba82SBenoît Canet                         &local_err);
229784d18f06SMarkus Armbruster     if (local_err) {
229812d3ba82SBenoît Canet         error_propagate(errp, local_err);
2299a4dea8a9SLuiz Capitulino         return;
2300666daa68SMarkus Armbruster     }
2301666daa68SMarkus Armbruster 
2302e3442099SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2303e3442099SStefan Hajnoczi     aio_context_acquire(aio_context);
2304e3442099SStefan Hajnoczi 
23054d2855a3SMarkus Armbruster     bdrv_add_key(bs, password, errp);
2306666daa68SMarkus Armbruster 
2307e3442099SStefan Hajnoczi     aio_context_release(aio_context);
2308e3442099SStefan Hajnoczi }
2309e3442099SStefan Hajnoczi 
23107d8a9f71SMax Reitz void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
23117d8a9f71SMax Reitz                             Error **errp)
23127d8a9f71SMax Reitz {
23137d8a9f71SMax Reitz     BlockBackend *blk;
23147d8a9f71SMax Reitz     bool locked;
23157d8a9f71SMax Reitz 
23167d8a9f71SMax Reitz     if (!has_force) {
23177d8a9f71SMax Reitz         force = false;
23187d8a9f71SMax Reitz     }
23197d8a9f71SMax Reitz 
23207d8a9f71SMax Reitz     blk = blk_by_name(device);
23217d8a9f71SMax Reitz     if (!blk) {
23227d8a9f71SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
23237d8a9f71SMax Reitz                   "Device '%s' not found", device);
23247d8a9f71SMax Reitz         return;
23257d8a9f71SMax Reitz     }
23267d8a9f71SMax Reitz 
23277d8a9f71SMax Reitz     if (!blk_dev_has_removable_media(blk)) {
23287d8a9f71SMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
23297d8a9f71SMax Reitz         return;
23307d8a9f71SMax Reitz     }
23317d8a9f71SMax Reitz 
233212c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
233312c7ec87SMax Reitz         /* Ignore this command on tray-less devices */
233412c7ec87SMax Reitz         return;
233512c7ec87SMax Reitz     }
233612c7ec87SMax Reitz 
23377d8a9f71SMax Reitz     if (blk_dev_is_tray_open(blk)) {
23387d8a9f71SMax Reitz         return;
23397d8a9f71SMax Reitz     }
23407d8a9f71SMax Reitz 
23417d8a9f71SMax Reitz     locked = blk_dev_is_medium_locked(blk);
23427d8a9f71SMax Reitz     if (locked) {
23437d8a9f71SMax Reitz         blk_dev_eject_request(blk, force);
23447d8a9f71SMax Reitz     }
23457d8a9f71SMax Reitz 
23467d8a9f71SMax Reitz     if (!locked || force) {
23477d8a9f71SMax Reitz         blk_dev_change_media_cb(blk, false);
23487d8a9f71SMax Reitz     }
23497d8a9f71SMax Reitz }
23507d8a9f71SMax Reitz 
2351abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp)
2352abaaf59dSMax Reitz {
2353abaaf59dSMax Reitz     BlockBackend *blk;
2354abaaf59dSMax Reitz 
2355abaaf59dSMax Reitz     blk = blk_by_name(device);
2356abaaf59dSMax Reitz     if (!blk) {
2357abaaf59dSMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2358abaaf59dSMax Reitz                   "Device '%s' not found", device);
2359abaaf59dSMax Reitz         return;
2360abaaf59dSMax Reitz     }
2361abaaf59dSMax Reitz 
2362abaaf59dSMax Reitz     if (!blk_dev_has_removable_media(blk)) {
2363abaaf59dSMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
2364abaaf59dSMax Reitz         return;
2365abaaf59dSMax Reitz     }
2366abaaf59dSMax Reitz 
236712c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
236812c7ec87SMax Reitz         /* Ignore this command on tray-less devices */
236912c7ec87SMax Reitz         return;
237012c7ec87SMax Reitz     }
237112c7ec87SMax Reitz 
2372abaaf59dSMax Reitz     if (!blk_dev_is_tray_open(blk)) {
2373abaaf59dSMax Reitz         return;
2374abaaf59dSMax Reitz     }
2375abaaf59dSMax Reitz 
2376abaaf59dSMax Reitz     blk_dev_change_media_cb(blk, true);
2377abaaf59dSMax Reitz }
2378abaaf59dSMax Reitz 
23796e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
23802814f672SMax Reitz {
23812814f672SMax Reitz     BlockBackend *blk;
23822814f672SMax Reitz     BlockDriverState *bs;
23832814f672SMax Reitz     AioContext *aio_context;
23842814f672SMax Reitz     bool has_device;
23852814f672SMax Reitz 
23862814f672SMax Reitz     blk = blk_by_name(device);
23872814f672SMax Reitz     if (!blk) {
23882814f672SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
23892814f672SMax Reitz                   "Device '%s' not found", device);
23902814f672SMax Reitz         return;
23912814f672SMax Reitz     }
23922814f672SMax Reitz 
23932814f672SMax Reitz     /* For BBs without a device, we can exchange the BDS tree at will */
23942814f672SMax Reitz     has_device = blk_get_attached_dev(blk);
23952814f672SMax Reitz 
23962814f672SMax Reitz     if (has_device && !blk_dev_has_removable_media(blk)) {
23972814f672SMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
23982814f672SMax Reitz         return;
23992814f672SMax Reitz     }
24002814f672SMax Reitz 
240112c7ec87SMax Reitz     if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
24022814f672SMax Reitz         error_setg(errp, "Tray of device '%s' is not open", device);
24032814f672SMax Reitz         return;
24042814f672SMax Reitz     }
24052814f672SMax Reitz 
24062814f672SMax Reitz     bs = blk_bs(blk);
24072814f672SMax Reitz     if (!bs) {
24082814f672SMax Reitz         return;
24092814f672SMax Reitz     }
24102814f672SMax Reitz 
24112814f672SMax Reitz     aio_context = bdrv_get_aio_context(bs);
24122814f672SMax Reitz     aio_context_acquire(aio_context);
24132814f672SMax Reitz 
24142814f672SMax Reitz     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
24152814f672SMax Reitz         goto out;
24162814f672SMax Reitz     }
24172814f672SMax Reitz 
24182814f672SMax Reitz     /* This follows the convention established by bdrv_make_anon() */
24192814f672SMax Reitz     if (bs->device_list.tqe_prev) {
2420f8aa905aSJeff Cody         bdrv_device_remove(bs);
24212814f672SMax Reitz     }
24222814f672SMax Reitz 
24232814f672SMax Reitz     blk_remove_bs(blk);
24242814f672SMax Reitz 
242512c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
242612c7ec87SMax Reitz         /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
242712c7ec87SMax Reitz          * called at all); therefore, the medium needs to be ejected here.
242812c7ec87SMax Reitz          * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
242912c7ec87SMax Reitz          * value passed here (i.e. false). */
243012c7ec87SMax Reitz         blk_dev_change_media_cb(blk, false);
243112c7ec87SMax Reitz     }
243212c7ec87SMax Reitz 
24332814f672SMax Reitz out:
24342814f672SMax Reitz     aio_context_release(aio_context);
24352814f672SMax Reitz }
24362814f672SMax Reitz 
2437d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device,
2438d1299882SMax Reitz                                             BlockDriverState *bs, Error **errp)
2439d1299882SMax Reitz {
2440d1299882SMax Reitz     BlockBackend *blk;
2441d1299882SMax Reitz     bool has_device;
2442d1299882SMax Reitz 
2443d1299882SMax Reitz     blk = blk_by_name(device);
2444d1299882SMax Reitz     if (!blk) {
2445d1299882SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2446d1299882SMax Reitz                   "Device '%s' not found", device);
2447d1299882SMax Reitz         return;
2448d1299882SMax Reitz     }
2449d1299882SMax Reitz 
2450d1299882SMax Reitz     /* For BBs without a device, we can exchange the BDS tree at will */
2451d1299882SMax Reitz     has_device = blk_get_attached_dev(blk);
2452d1299882SMax Reitz 
2453d1299882SMax Reitz     if (has_device && !blk_dev_has_removable_media(blk)) {
2454d1299882SMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
2455d1299882SMax Reitz         return;
2456d1299882SMax Reitz     }
2457d1299882SMax Reitz 
245812c7ec87SMax Reitz     if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2459d1299882SMax Reitz         error_setg(errp, "Tray of device '%s' is not open", device);
2460d1299882SMax Reitz         return;
2461d1299882SMax Reitz     }
2462d1299882SMax Reitz 
2463d1299882SMax Reitz     if (blk_bs(blk)) {
2464d1299882SMax Reitz         error_setg(errp, "There already is a medium in device '%s'", device);
2465d1299882SMax Reitz         return;
2466d1299882SMax Reitz     }
2467d1299882SMax Reitz 
2468d1299882SMax Reitz     blk_insert_bs(blk, bs);
2469d1299882SMax Reitz 
2470d1299882SMax Reitz     QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
247112c7ec87SMax Reitz 
247212c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
247312c7ec87SMax Reitz         /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
247412c7ec87SMax Reitz          * called at all); therefore, the medium needs to be pushed into the
247512c7ec87SMax Reitz          * slot here.
247612c7ec87SMax Reitz          * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
247712c7ec87SMax Reitz          * value passed here (i.e. true). */
247812c7ec87SMax Reitz         blk_dev_change_media_cb(blk, true);
247912c7ec87SMax Reitz     }
2480d1299882SMax Reitz }
2481d1299882SMax Reitz 
24826e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name,
2483d1299882SMax Reitz                                   Error **errp)
2484d1299882SMax Reitz {
2485d1299882SMax Reitz     BlockDriverState *bs;
2486d1299882SMax Reitz 
2487d1299882SMax Reitz     bs = bdrv_find_node(node_name);
2488d1299882SMax Reitz     if (!bs) {
2489d1299882SMax Reitz         error_setg(errp, "Node '%s' not found", node_name);
2490d1299882SMax Reitz         return;
2491d1299882SMax Reitz     }
2492d1299882SMax Reitz 
2493d1299882SMax Reitz     if (bs->blk) {
2494d1299882SMax Reitz         error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2495d1299882SMax Reitz                    blk_name(bs->blk));
2496d1299882SMax Reitz         return;
2497d1299882SMax Reitz     }
2498d1299882SMax Reitz 
2499d1299882SMax Reitz     qmp_blockdev_insert_anon_medium(device, bs, errp);
2500d1299882SMax Reitz }
2501d1299882SMax Reitz 
250224fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename,
250324fb4133SMax Reitz                                 bool has_format, const char *format,
250439ff43d9SMax Reitz                                 bool has_read_only,
250539ff43d9SMax Reitz                                 BlockdevChangeReadOnlyMode read_only,
250624fb4133SMax Reitz                                 Error **errp)
2507de2c6c05SMax Reitz {
2508de2c6c05SMax Reitz     BlockBackend *blk;
2509de2c6c05SMax Reitz     BlockDriverState *medium_bs = NULL;
2510de2c6c05SMax Reitz     int bdrv_flags, ret;
2511de2c6c05SMax Reitz     QDict *options = NULL;
2512de2c6c05SMax Reitz     Error *err = NULL;
2513de2c6c05SMax Reitz 
2514de2c6c05SMax Reitz     blk = blk_by_name(device);
2515de2c6c05SMax Reitz     if (!blk) {
2516de2c6c05SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2517de2c6c05SMax Reitz                   "Device '%s' not found", device);
2518de2c6c05SMax Reitz         goto fail;
2519de2c6c05SMax Reitz     }
2520de2c6c05SMax Reitz 
2521de2c6c05SMax Reitz     if (blk_bs(blk)) {
2522de2c6c05SMax Reitz         blk_update_root_state(blk);
2523de2c6c05SMax Reitz     }
2524de2c6c05SMax Reitz 
2525de2c6c05SMax Reitz     bdrv_flags = blk_get_open_flags_from_root_state(blk);
2526de2c6c05SMax Reitz 
252739ff43d9SMax Reitz     if (!has_read_only) {
252839ff43d9SMax Reitz         read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
252939ff43d9SMax Reitz     }
253039ff43d9SMax Reitz 
253139ff43d9SMax Reitz     switch (read_only) {
253239ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
253339ff43d9SMax Reitz         break;
253439ff43d9SMax Reitz 
253539ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
253639ff43d9SMax Reitz         bdrv_flags &= ~BDRV_O_RDWR;
253739ff43d9SMax Reitz         break;
253839ff43d9SMax Reitz 
253939ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
254039ff43d9SMax Reitz         bdrv_flags |= BDRV_O_RDWR;
254139ff43d9SMax Reitz         break;
254239ff43d9SMax Reitz 
254339ff43d9SMax Reitz     default:
254439ff43d9SMax Reitz         abort();
254539ff43d9SMax Reitz     }
254639ff43d9SMax Reitz 
254724fb4133SMax Reitz     if (has_format) {
2548de2c6c05SMax Reitz         options = qdict_new();
2549de2c6c05SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
2550de2c6c05SMax Reitz     }
2551de2c6c05SMax Reitz 
2552de2c6c05SMax Reitz     assert(!medium_bs);
2553de2c6c05SMax Reitz     ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2554de2c6c05SMax Reitz     if (ret < 0) {
2555de2c6c05SMax Reitz         goto fail;
2556de2c6c05SMax Reitz     }
2557de2c6c05SMax Reitz 
2558de2c6c05SMax Reitz     blk_apply_root_state(blk, medium_bs);
2559de2c6c05SMax Reitz 
2560de2c6c05SMax Reitz     bdrv_add_key(medium_bs, NULL, &err);
2561de2c6c05SMax Reitz     if (err) {
2562de2c6c05SMax Reitz         error_propagate(errp, err);
2563de2c6c05SMax Reitz         goto fail;
2564de2c6c05SMax Reitz     }
2565de2c6c05SMax Reitz 
2566de2c6c05SMax Reitz     qmp_blockdev_open_tray(device, false, false, &err);
2567de2c6c05SMax Reitz     if (err) {
2568de2c6c05SMax Reitz         error_propagate(errp, err);
2569de2c6c05SMax Reitz         goto fail;
2570de2c6c05SMax Reitz     }
2571de2c6c05SMax Reitz 
25726e0abc25SMax Reitz     qmp_x_blockdev_remove_medium(device, &err);
2573de2c6c05SMax Reitz     if (err) {
2574de2c6c05SMax Reitz         error_propagate(errp, err);
2575de2c6c05SMax Reitz         goto fail;
2576de2c6c05SMax Reitz     }
2577de2c6c05SMax Reitz 
2578de2c6c05SMax Reitz     qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2579de2c6c05SMax Reitz     if (err) {
2580de2c6c05SMax Reitz         error_propagate(errp, err);
2581de2c6c05SMax Reitz         goto fail;
2582de2c6c05SMax Reitz     }
2583de2c6c05SMax Reitz 
2584de2c6c05SMax Reitz     qmp_blockdev_close_tray(device, errp);
2585de2c6c05SMax Reitz 
2586de2c6c05SMax Reitz fail:
2587de2c6c05SMax Reitz     /* If the medium has been inserted, the device has its own reference, so
2588de2c6c05SMax Reitz      * ours must be relinquished; and if it has not been inserted successfully,
2589de2c6c05SMax Reitz      * the reference must be relinquished anyway */
2590de2c6c05SMax Reitz     bdrv_unref(medium_bs);
2591de2c6c05SMax Reitz }
2592de2c6c05SMax Reitz 
2593727f005eSZhi Yong Wu /* throttling disk I/O limits */
259480047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
25953e9fab69SBenoît Canet                                int64_t bps_wr,
25963e9fab69SBenoît Canet                                int64_t iops,
25973e9fab69SBenoît Canet                                int64_t iops_rd,
25983e9fab69SBenoît Canet                                int64_t iops_wr,
25993e9fab69SBenoît Canet                                bool has_bps_max,
26003e9fab69SBenoît Canet                                int64_t bps_max,
26013e9fab69SBenoît Canet                                bool has_bps_rd_max,
26023e9fab69SBenoît Canet                                int64_t bps_rd_max,
26033e9fab69SBenoît Canet                                bool has_bps_wr_max,
26043e9fab69SBenoît Canet                                int64_t bps_wr_max,
26053e9fab69SBenoît Canet                                bool has_iops_max,
26063e9fab69SBenoît Canet                                int64_t iops_max,
26073e9fab69SBenoît Canet                                bool has_iops_rd_max,
26083e9fab69SBenoît Canet                                int64_t iops_rd_max,
26093e9fab69SBenoît Canet                                bool has_iops_wr_max,
26102024c1dfSBenoît Canet                                int64_t iops_wr_max,
26112024c1dfSBenoît Canet                                bool has_iops_size,
261276f4afb4SAlberto Garcia                                int64_t iops_size,
261376f4afb4SAlberto Garcia                                bool has_group,
261476f4afb4SAlberto Garcia                                const char *group, Error **errp)
2615727f005eSZhi Yong Wu {
2616cc0681c4SBenoît Canet     ThrottleConfig cfg;
2617727f005eSZhi Yong Wu     BlockDriverState *bs;
2618a0e8544cSFam Zheng     BlockBackend *blk;
2619b15446fdSStefan Hajnoczi     AioContext *aio_context;
2620727f005eSZhi Yong Wu 
2621a0e8544cSFam Zheng     blk = blk_by_name(device);
2622a0e8544cSFam Zheng     if (!blk) {
262375158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
262475158ebbSMarkus Armbruster                   "Device '%s' not found", device);
262580047da5SLuiz Capitulino         return;
2626727f005eSZhi Yong Wu     }
26275433c24fSMax Reitz 
26285433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
26295433c24fSMax Reitz     aio_context_acquire(aio_context);
26305433c24fSMax Reitz 
2631a0e8544cSFam Zheng     bs = blk_bs(blk);
26325433c24fSMax Reitz     if (!bs) {
26335433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
26345433c24fSMax Reitz         goto out;
26355433c24fSMax Reitz     }
2636727f005eSZhi Yong Wu 
2637cc0681c4SBenoît Canet     memset(&cfg, 0, sizeof(cfg));
2638cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2639cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_READ].avg  = bps_rd;
2640cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2641727f005eSZhi Yong Wu 
2642cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2643cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_READ].avg  = iops_rd;
2644cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2645cc0681c4SBenoît Canet 
26463e9fab69SBenoît Canet     if (has_bps_max) {
26473e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
26483e9fab69SBenoît Canet     }
26493e9fab69SBenoît Canet     if (has_bps_rd_max) {
26503e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
26513e9fab69SBenoît Canet     }
26523e9fab69SBenoît Canet     if (has_bps_wr_max) {
26533e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
26543e9fab69SBenoît Canet     }
26553e9fab69SBenoît Canet     if (has_iops_max) {
26563e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
26573e9fab69SBenoît Canet     }
26583e9fab69SBenoît Canet     if (has_iops_rd_max) {
26593e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
26603e9fab69SBenoît Canet     }
26613e9fab69SBenoît Canet     if (has_iops_wr_max) {
26623e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
26633e9fab69SBenoît Canet     }
2664cc0681c4SBenoît Canet 
26652024c1dfSBenoît Canet     if (has_iops_size) {
26662024c1dfSBenoît Canet         cfg.op_size = iops_size;
26672024c1dfSBenoît Canet     }
2668cc0681c4SBenoît Canet 
2669cc0681c4SBenoît Canet     if (!check_throttle_config(&cfg, errp)) {
26705433c24fSMax Reitz         goto out;
2671727f005eSZhi Yong Wu     }
2672727f005eSZhi Yong Wu 
267376f4afb4SAlberto Garcia     if (throttle_enabled(&cfg)) {
267476f4afb4SAlberto Garcia         /* Enable I/O limits if they're not enabled yet, otherwise
267576f4afb4SAlberto Garcia          * just update the throttling group. */
2676a0d64a61SAlberto Garcia         if (!bs->throttle_state) {
267776f4afb4SAlberto Garcia             bdrv_io_limits_enable(bs, has_group ? group : device);
267876f4afb4SAlberto Garcia         } else if (has_group) {
267976f4afb4SAlberto Garcia             bdrv_io_limits_update_group(bs, group);
2680727f005eSZhi Yong Wu         }
268176f4afb4SAlberto Garcia         /* Set the new throttling configuration */
2682cc0681c4SBenoît Canet         bdrv_set_io_limits(bs, &cfg);
2683a0d64a61SAlberto Garcia     } else if (bs->throttle_state) {
268476f4afb4SAlberto Garcia         /* If all throttling settings are set to 0, disable I/O limits */
268576f4afb4SAlberto Garcia         bdrv_io_limits_disable(bs);
2686727f005eSZhi Yong Wu     }
2687b15446fdSStefan Hajnoczi 
26885433c24fSMax Reitz out:
2689b15446fdSStefan Hajnoczi     aio_context_release(aio_context);
2690727f005eSZhi Yong Wu }
2691727f005eSZhi Yong Wu 
2692341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2693341ebc2fSJohn Snow                                 bool has_granularity, uint32_t granularity,
2694341ebc2fSJohn Snow                                 Error **errp)
2695341ebc2fSJohn Snow {
2696341ebc2fSJohn Snow     AioContext *aio_context;
2697341ebc2fSJohn Snow     BlockDriverState *bs;
2698341ebc2fSJohn Snow 
2699341ebc2fSJohn Snow     if (!name || name[0] == '\0') {
2700341ebc2fSJohn Snow         error_setg(errp, "Bitmap name cannot be empty");
2701341ebc2fSJohn Snow         return;
2702341ebc2fSJohn Snow     }
2703341ebc2fSJohn Snow 
2704341ebc2fSJohn Snow     bs = bdrv_lookup_bs(node, node, errp);
2705341ebc2fSJohn Snow     if (!bs) {
2706341ebc2fSJohn Snow         return;
2707341ebc2fSJohn Snow     }
2708341ebc2fSJohn Snow 
2709341ebc2fSJohn Snow     aio_context = bdrv_get_aio_context(bs);
2710341ebc2fSJohn Snow     aio_context_acquire(aio_context);
2711341ebc2fSJohn Snow 
2712341ebc2fSJohn Snow     if (has_granularity) {
2713341ebc2fSJohn Snow         if (granularity < 512 || !is_power_of_2(granularity)) {
2714341ebc2fSJohn Snow             error_setg(errp, "Granularity must be power of 2 "
2715341ebc2fSJohn Snow                              "and at least 512");
2716341ebc2fSJohn Snow             goto out;
2717341ebc2fSJohn Snow         }
2718341ebc2fSJohn Snow     } else {
2719341ebc2fSJohn Snow         /* Default to cluster size, if available: */
2720341ebc2fSJohn Snow         granularity = bdrv_get_default_bitmap_granularity(bs);
2721341ebc2fSJohn Snow     }
2722341ebc2fSJohn Snow 
2723341ebc2fSJohn Snow     bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2724341ebc2fSJohn Snow 
2725341ebc2fSJohn Snow  out:
2726341ebc2fSJohn Snow     aio_context_release(aio_context);
2727341ebc2fSJohn Snow }
2728341ebc2fSJohn Snow 
2729341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2730341ebc2fSJohn Snow                                    Error **errp)
2731341ebc2fSJohn Snow {
2732341ebc2fSJohn Snow     AioContext *aio_context;
2733341ebc2fSJohn Snow     BlockDriverState *bs;
2734341ebc2fSJohn Snow     BdrvDirtyBitmap *bitmap;
2735341ebc2fSJohn Snow 
2736341ebc2fSJohn Snow     bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2737341ebc2fSJohn Snow     if (!bitmap || !bs) {
2738341ebc2fSJohn Snow         return;
2739341ebc2fSJohn Snow     }
2740341ebc2fSJohn Snow 
27419bd2b08fSJohn Snow     if (bdrv_dirty_bitmap_frozen(bitmap)) {
27429bd2b08fSJohn Snow         error_setg(errp,
27439bd2b08fSJohn Snow                    "Bitmap '%s' is currently frozen and cannot be removed",
27449bd2b08fSJohn Snow                    name);
27459bd2b08fSJohn Snow         goto out;
27469bd2b08fSJohn Snow     }
274720dca810SJohn Snow     bdrv_dirty_bitmap_make_anon(bitmap);
2748341ebc2fSJohn Snow     bdrv_release_dirty_bitmap(bs, bitmap);
2749341ebc2fSJohn Snow 
27509bd2b08fSJohn Snow  out:
2751341ebc2fSJohn Snow     aio_context_release(aio_context);
2752341ebc2fSJohn Snow }
2753341ebc2fSJohn Snow 
2754e74e6b78SJohn Snow /**
2755e74e6b78SJohn Snow  * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2756e74e6b78SJohn Snow  * immediately after a full backup operation.
2757e74e6b78SJohn Snow  */
2758e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2759e74e6b78SJohn Snow                                   Error **errp)
2760e74e6b78SJohn Snow {
2761e74e6b78SJohn Snow     AioContext *aio_context;
2762e74e6b78SJohn Snow     BdrvDirtyBitmap *bitmap;
2763e74e6b78SJohn Snow     BlockDriverState *bs;
2764e74e6b78SJohn Snow 
2765e74e6b78SJohn Snow     bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2766e74e6b78SJohn Snow     if (!bitmap || !bs) {
2767e74e6b78SJohn Snow         return;
2768e74e6b78SJohn Snow     }
2769e74e6b78SJohn Snow 
2770e74e6b78SJohn Snow     if (bdrv_dirty_bitmap_frozen(bitmap)) {
2771e74e6b78SJohn Snow         error_setg(errp,
2772e74e6b78SJohn Snow                    "Bitmap '%s' is currently frozen and cannot be modified",
2773e74e6b78SJohn Snow                    name);
2774e74e6b78SJohn Snow         goto out;
2775e74e6b78SJohn Snow     } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2776e74e6b78SJohn Snow         error_setg(errp,
2777e74e6b78SJohn Snow                    "Bitmap '%s' is currently disabled and cannot be cleared",
2778e74e6b78SJohn Snow                    name);
2779e74e6b78SJohn Snow         goto out;
2780e74e6b78SJohn Snow     }
2781e74e6b78SJohn Snow 
2782df9a681dSFam Zheng     bdrv_clear_dirty_bitmap(bitmap, NULL);
2783e74e6b78SJohn Snow 
2784e74e6b78SJohn Snow  out:
2785e74e6b78SJohn Snow     aio_context_release(aio_context);
2786e74e6b78SJohn Snow }
2787e74e6b78SJohn Snow 
2788072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict)
27899063f814SRyan Harper {
27909063f814SRyan Harper     const char *id = qdict_get_str(qdict, "id");
27917e7d56d9SMarkus Armbruster     BlockBackend *blk;
27929063f814SRyan Harper     BlockDriverState *bs;
27938ad4202bSStefan Hajnoczi     AioContext *aio_context;
27943718d8abSFam Zheng     Error *local_err = NULL;
27959063f814SRyan Harper 
27967e7d56d9SMarkus Armbruster     blk = blk_by_name(id);
27977e7d56d9SMarkus Armbruster     if (!blk) {
2798b1422f20SMarkus Armbruster         error_report("Device '%s' not found", id);
2799072ebe6bSMarkus Armbruster         return;
28009063f814SRyan Harper     }
28018ad4202bSStefan Hajnoczi 
280226f8b3a8SMarkus Armbruster     if (!blk_legacy_dinfo(blk)) {
280348f364ddSMarkus Armbruster         error_report("Deleting device added with blockdev-add"
280448f364ddSMarkus Armbruster                      " is not supported");
2805072ebe6bSMarkus Armbruster         return;
280648f364ddSMarkus Armbruster     }
280748f364ddSMarkus Armbruster 
28085433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
28098ad4202bSStefan Hajnoczi     aio_context_acquire(aio_context);
28108ad4202bSStefan Hajnoczi 
28115433c24fSMax Reitz     bs = blk_bs(blk);
28125433c24fSMax Reitz     if (bs) {
28133718d8abSFam Zheng         if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2814565f65d2SMarkus Armbruster             error_report_err(local_err);
28158ad4202bSStefan Hajnoczi             aio_context_release(aio_context);
2816072ebe6bSMarkus Armbruster             return;
28178591675fSMarcelo Tosatti         }
28189063f814SRyan Harper 
2819938abd43SMax Reitz         blk_remove_bs(blk);
28205433c24fSMax Reitz     }
28219063f814SRyan Harper 
2822fa879d62SMarkus Armbruster     /* if we have a device attached to this BlockDriverState
2823d22b2f41SRyan Harper      * then we need to make the drive anonymous until the device
2824d22b2f41SRyan Harper      * can be removed.  If this is a drive with no device backing
2825d22b2f41SRyan Harper      * then we can just get rid of the block driver state right here.
2826d22b2f41SRyan Harper      */
2827a7f53e26SMarkus Armbruster     if (blk_get_attached_dev(blk)) {
28283e5a50d6SMarkus Armbruster         blk_hide_on_behalf_of_hmp_drive_del(blk);
2829293c51a6SStefan Hajnoczi         /* Further I/O must not pause the guest */
2830373340b2SMax Reitz         blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2831293c51a6SStefan Hajnoczi                          BLOCKDEV_ON_ERROR_REPORT);
2832d22b2f41SRyan Harper     } else {
2833b9fe8a7aSMarkus Armbruster         blk_unref(blk);
2834d22b2f41SRyan Harper     }
28359063f814SRyan Harper 
28368ad4202bSStefan Hajnoczi     aio_context_release(aio_context);
28379063f814SRyan Harper }
28386d4a2b3aSChristoph Hellwig 
28393b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device,
28403b1dbd11SBenoît Canet                       bool has_node_name, const char *node_name,
28413b1dbd11SBenoît Canet                       int64_t size, Error **errp)
28426d4a2b3aSChristoph Hellwig {
28433b1dbd11SBenoît Canet     Error *local_err = NULL;
28446d4a2b3aSChristoph Hellwig     BlockDriverState *bs;
2845927e0e76SStefan Hajnoczi     AioContext *aio_context;
28468732901eSKevin Wolf     int ret;
28476d4a2b3aSChristoph Hellwig 
28483b1dbd11SBenoît Canet     bs = bdrv_lookup_bs(has_device ? device : NULL,
28493b1dbd11SBenoît Canet                         has_node_name ? node_name : NULL,
28503b1dbd11SBenoît Canet                         &local_err);
285184d18f06SMarkus Armbruster     if (local_err) {
28523b1dbd11SBenoît Canet         error_propagate(errp, local_err);
28533b1dbd11SBenoît Canet         return;
28543b1dbd11SBenoît Canet     }
28553b1dbd11SBenoît Canet 
2856927e0e76SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2857927e0e76SStefan Hajnoczi     aio_context_acquire(aio_context);
2858927e0e76SStefan Hajnoczi 
28593b1dbd11SBenoît Canet     if (!bdrv_is_first_non_filter(bs)) {
2860c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2861927e0e76SStefan Hajnoczi         goto out;
28626d4a2b3aSChristoph Hellwig     }
28636d4a2b3aSChristoph Hellwig 
28646d4a2b3aSChristoph Hellwig     if (size < 0) {
2865c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2866927e0e76SStefan Hajnoczi         goto out;
28676d4a2b3aSChristoph Hellwig     }
28686d4a2b3aSChristoph Hellwig 
28699c75e168SJeff Cody     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2870c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_IN_USE, device);
2871927e0e76SStefan Hajnoczi         goto out;
28729c75e168SJeff Cody     }
28739c75e168SJeff Cody 
287492b7a08dSPeter Lieven     /* complete all in-flight operations before resizing the device */
287592b7a08dSPeter Lieven     bdrv_drain_all();
287692b7a08dSPeter Lieven 
28778732901eSKevin Wolf     ret = bdrv_truncate(bs, size);
28788732901eSKevin Wolf     switch (ret) {
2879939a1cc3SStefan Hajnoczi     case 0:
2880939a1cc3SStefan Hajnoczi         break;
2881939a1cc3SStefan Hajnoczi     case -ENOMEDIUM:
2882c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2883939a1cc3SStefan Hajnoczi         break;
2884939a1cc3SStefan Hajnoczi     case -ENOTSUP:
2885c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_UNSUPPORTED);
2886939a1cc3SStefan Hajnoczi         break;
2887939a1cc3SStefan Hajnoczi     case -EACCES:
288881e5f78aSAlberto Garcia         error_setg(errp, "Device '%s' is read only", device);
2889939a1cc3SStefan Hajnoczi         break;
2890939a1cc3SStefan Hajnoczi     case -EBUSY:
2891c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_IN_USE, device);
2892939a1cc3SStefan Hajnoczi         break;
2893939a1cc3SStefan Hajnoczi     default:
28948732901eSKevin Wolf         error_setg_errno(errp, -ret, "Could not resize");
2895939a1cc3SStefan Hajnoczi         break;
28966d4a2b3aSChristoph Hellwig     }
2897927e0e76SStefan Hajnoczi 
2898927e0e76SStefan Hajnoczi out:
2899927e0e76SStefan Hajnoczi     aio_context_release(aio_context);
29006d4a2b3aSChristoph Hellwig }
290112bd451fSStefan Hajnoczi 
29029abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret)
290312bd451fSStefan Hajnoczi {
2904723c5d93SStefan Hajnoczi     /* Note that this function may be executed from another AioContext besides
2905723c5d93SStefan Hajnoczi      * the QEMU main loop.  If you need to access anything that assumes the
2906723c5d93SStefan Hajnoczi      * QEMU global mutex, use a BH or introduce a mutex.
2907723c5d93SStefan Hajnoczi      */
2908723c5d93SStefan Hajnoczi 
290912bd451fSStefan Hajnoczi     BlockDriverState *bs = opaque;
2910bcada37bSWenchao Xia     const char *msg = NULL;
291112bd451fSStefan Hajnoczi 
29129abf2dbaSJeff Cody     trace_block_job_cb(bs, bs->job, ret);
291312bd451fSStefan Hajnoczi 
291412bd451fSStefan Hajnoczi     assert(bs->job);
2915bcada37bSWenchao Xia 
291612bd451fSStefan Hajnoczi     if (ret < 0) {
2917bcada37bSWenchao Xia         msg = strerror(-ret);
291812bd451fSStefan Hajnoczi     }
291912bd451fSStefan Hajnoczi 
2920370521a1SStefan Hajnoczi     if (block_job_is_cancelled(bs->job)) {
2921bcada37bSWenchao Xia         block_job_event_cancelled(bs->job);
2922370521a1SStefan Hajnoczi     } else {
2923bcada37bSWenchao Xia         block_job_event_completed(bs->job, msg);
2924370521a1SStefan Hajnoczi     }
292512bd451fSStefan Hajnoczi }
292612bd451fSStefan Hajnoczi 
292713d8cc51SJeff Cody void qmp_block_stream(const char *device,
292813d8cc51SJeff Cody                       bool has_base, const char *base,
292913d8cc51SJeff Cody                       bool has_backing_file, const char *backing_file,
293013d8cc51SJeff Cody                       bool has_speed, int64_t speed,
29311d809098SPaolo Bonzini                       bool has_on_error, BlockdevOnError on_error,
29321d809098SPaolo Bonzini                       Error **errp)
293312bd451fSStefan Hajnoczi {
2934a0e8544cSFam Zheng     BlockBackend *blk;
293512bd451fSStefan Hajnoczi     BlockDriverState *bs;
2936c8c3080fSMarcelo Tosatti     BlockDriverState *base_bs = NULL;
2937f3e69bebSStefan Hajnoczi     AioContext *aio_context;
2938fd7f8c65SStefan Hajnoczi     Error *local_err = NULL;
293913d8cc51SJeff Cody     const char *base_name = NULL;
294012bd451fSStefan Hajnoczi 
29411d809098SPaolo Bonzini     if (!has_on_error) {
29421d809098SPaolo Bonzini         on_error = BLOCKDEV_ON_ERROR_REPORT;
29431d809098SPaolo Bonzini     }
29441d809098SPaolo Bonzini 
2945a0e8544cSFam Zheng     blk = blk_by_name(device);
2946a0e8544cSFam Zheng     if (!blk) {
294775158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
294875158ebbSMarkus Armbruster                   "Device '%s' not found", device);
294912bd451fSStefan Hajnoczi         return;
295012bd451fSStefan Hajnoczi     }
295112bd451fSStefan Hajnoczi 
29525433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
2953f3e69bebSStefan Hajnoczi     aio_context_acquire(aio_context);
2954f3e69bebSStefan Hajnoczi 
29555433c24fSMax Reitz     if (!blk_is_available(blk)) {
29565433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
29575433c24fSMax Reitz         goto out;
29585433c24fSMax Reitz     }
29595433c24fSMax Reitz     bs = blk_bs(blk);
29605433c24fSMax Reitz 
2961628ff683SFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2962f3e69bebSStefan Hajnoczi         goto out;
2963628ff683SFam Zheng     }
2964628ff683SFam Zheng 
296513d8cc51SJeff Cody     if (has_base) {
2966c8c3080fSMarcelo Tosatti         base_bs = bdrv_find_backing_image(bs, base);
2967c8c3080fSMarcelo Tosatti         if (base_bs == NULL) {
2968c6bd8c70SMarkus Armbruster             error_setg(errp, QERR_BASE_NOT_FOUND, base);
2969f3e69bebSStefan Hajnoczi             goto out;
297012bd451fSStefan Hajnoczi         }
2971f3e69bebSStefan Hajnoczi         assert(bdrv_get_aio_context(base_bs) == aio_context);
297213d8cc51SJeff Cody         base_name = base;
2973c8c3080fSMarcelo Tosatti     }
297412bd451fSStefan Hajnoczi 
297513d8cc51SJeff Cody     /* if we are streaming the entire chain, the result will have no backing
297613d8cc51SJeff Cody      * file, and specifying one is therefore an error */
297713d8cc51SJeff Cody     if (base_bs == NULL && has_backing_file) {
297813d8cc51SJeff Cody         error_setg(errp, "backing file specified, but streaming the "
297913d8cc51SJeff Cody                          "entire chain");
2980f3e69bebSStefan Hajnoczi         goto out;
298113d8cc51SJeff Cody     }
298213d8cc51SJeff Cody 
298313d8cc51SJeff Cody     /* backing_file string overrides base bs filename */
298413d8cc51SJeff Cody     base_name = has_backing_file ? backing_file : base_name;
298513d8cc51SJeff Cody 
298613d8cc51SJeff Cody     stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
29871d809098SPaolo Bonzini                  on_error, block_job_cb, bs, &local_err);
298884d18f06SMarkus Armbruster     if (local_err) {
2989fd7f8c65SStefan Hajnoczi         error_propagate(errp, local_err);
2990f3e69bebSStefan Hajnoczi         goto out;
299112bd451fSStefan Hajnoczi     }
299212bd451fSStefan Hajnoczi 
299312bd451fSStefan Hajnoczi     trace_qmp_block_stream(bs, bs->job);
2994f3e69bebSStefan Hajnoczi 
2995f3e69bebSStefan Hajnoczi out:
2996f3e69bebSStefan Hajnoczi     aio_context_release(aio_context);
299712bd451fSStefan Hajnoczi }
29982d47c6e9SStefan Hajnoczi 
2999ed61fc10SJeff Cody void qmp_block_commit(const char *device,
30007676e2c5SJeff Cody                       bool has_base, const char *base,
30017676e2c5SJeff Cody                       bool has_top, const char *top,
300254e26900SJeff Cody                       bool has_backing_file, const char *backing_file,
3003ed61fc10SJeff Cody                       bool has_speed, int64_t speed,
3004ed61fc10SJeff Cody                       Error **errp)
3005ed61fc10SJeff Cody {
3006a0e8544cSFam Zheng     BlockBackend *blk;
3007ed61fc10SJeff Cody     BlockDriverState *bs;
3008ed61fc10SJeff Cody     BlockDriverState *base_bs, *top_bs;
30099e85cd5cSStefan Hajnoczi     AioContext *aio_context;
3010ed61fc10SJeff Cody     Error *local_err = NULL;
3011ed61fc10SJeff Cody     /* This will be part of the QMP command, if/when the
3012ed61fc10SJeff Cody      * BlockdevOnError change for blkmirror makes it in
3013ed61fc10SJeff Cody      */
301492aa5c6dSPaolo Bonzini     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
3015ed61fc10SJeff Cody 
301654504663SMax Reitz     if (!has_speed) {
301754504663SMax Reitz         speed = 0;
301854504663SMax Reitz     }
301954504663SMax Reitz 
30207676e2c5SJeff Cody     /* Important Note:
30217676e2c5SJeff Cody      *  libvirt relies on the DeviceNotFound error class in order to probe for
30227676e2c5SJeff Cody      *  live commit feature versions; for this to work, we must make sure to
30237676e2c5SJeff Cody      *  perform the device lookup before any generic errors that may occur in a
30247676e2c5SJeff Cody      *  scenario in which all optional arguments are omitted. */
3025a0e8544cSFam Zheng     blk = blk_by_name(device);
3026a0e8544cSFam Zheng     if (!blk) {
302775158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
302875158ebbSMarkus Armbruster                   "Device '%s' not found", device);
3029ed61fc10SJeff Cody         return;
3030ed61fc10SJeff Cody     }
3031ed61fc10SJeff Cody 
30325433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
30339e85cd5cSStefan Hajnoczi     aio_context_acquire(aio_context);
30349e85cd5cSStefan Hajnoczi 
30355433c24fSMax Reitz     if (!blk_is_available(blk)) {
30365433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
30375433c24fSMax Reitz         goto out;
30385433c24fSMax Reitz     }
30395433c24fSMax Reitz     bs = blk_bs(blk);
30405433c24fSMax Reitz 
3041bb00021dSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
30429e85cd5cSStefan Hajnoczi         goto out;
3043628ff683SFam Zheng     }
3044628ff683SFam Zheng 
3045ed61fc10SJeff Cody     /* default top_bs is the active layer */
3046ed61fc10SJeff Cody     top_bs = bs;
3047ed61fc10SJeff Cody 
30487676e2c5SJeff Cody     if (has_top && top) {
3049ed61fc10SJeff Cody         if (strcmp(bs->filename, top) != 0) {
3050ed61fc10SJeff Cody             top_bs = bdrv_find_backing_image(bs, top);
3051ed61fc10SJeff Cody         }
3052ed61fc10SJeff Cody     }
3053ed61fc10SJeff Cody 
3054ed61fc10SJeff Cody     if (top_bs == NULL) {
3055ed61fc10SJeff Cody         error_setg(errp, "Top image file %s not found", top ? top : "NULL");
30569e85cd5cSStefan Hajnoczi         goto out;
3057ed61fc10SJeff Cody     }
3058ed61fc10SJeff Cody 
30599e85cd5cSStefan Hajnoczi     assert(bdrv_get_aio_context(top_bs) == aio_context);
30609e85cd5cSStefan Hajnoczi 
3061d5208c45SJeff Cody     if (has_base && base) {
3062d5208c45SJeff Cody         base_bs = bdrv_find_backing_image(top_bs, base);
3063d5208c45SJeff Cody     } else {
3064d5208c45SJeff Cody         base_bs = bdrv_find_base(top_bs);
3065d5208c45SJeff Cody     }
3066d5208c45SJeff Cody 
3067d5208c45SJeff Cody     if (base_bs == NULL) {
3068c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
30699e85cd5cSStefan Hajnoczi         goto out;
3070d5208c45SJeff Cody     }
3071d5208c45SJeff Cody 
30729e85cd5cSStefan Hajnoczi     assert(bdrv_get_aio_context(base_bs) == aio_context);
30739e85cd5cSStefan Hajnoczi 
3074bb00021dSFam Zheng     if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3075bb00021dSFam Zheng         goto out;
3076bb00021dSFam Zheng     }
3077bb00021dSFam Zheng 
30787676e2c5SJeff Cody     /* Do not allow attempts to commit an image into itself */
30797676e2c5SJeff Cody     if (top_bs == base_bs) {
30807676e2c5SJeff Cody         error_setg(errp, "cannot commit an image into itself");
30819e85cd5cSStefan Hajnoczi         goto out;
30827676e2c5SJeff Cody     }
30837676e2c5SJeff Cody 
308420a63d2cSFam Zheng     if (top_bs == bs) {
308554e26900SJeff Cody         if (has_backing_file) {
308654e26900SJeff Cody             error_setg(errp, "'backing-file' specified,"
308754e26900SJeff Cody                              " but 'top' is the active layer");
30889e85cd5cSStefan Hajnoczi             goto out;
308954e26900SJeff Cody         }
309020a63d2cSFam Zheng         commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
309120a63d2cSFam Zheng                             bs, &local_err);
309220a63d2cSFam Zheng     } else {
3093ed61fc10SJeff Cody         commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
309454e26900SJeff Cody                      has_backing_file ? backing_file : NULL, &local_err);
309520a63d2cSFam Zheng     }
3096ed61fc10SJeff Cody     if (local_err != NULL) {
3097ed61fc10SJeff Cody         error_propagate(errp, local_err);
30989e85cd5cSStefan Hajnoczi         goto out;
3099ed61fc10SJeff Cody     }
31009e85cd5cSStefan Hajnoczi 
31019e85cd5cSStefan Hajnoczi out:
31029e85cd5cSStefan Hajnoczi     aio_context_release(aio_context);
3103ed61fc10SJeff Cody }
3104ed61fc10SJeff Cody 
310578f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target,
310699a9addfSStefan Hajnoczi                             bool has_format, const char *format,
3107b53169eaSStefan Hajnoczi                             enum MirrorSyncMode sync,
310899a9addfSStefan Hajnoczi                             bool has_mode, enum NewImageMode mode,
310999a9addfSStefan Hajnoczi                             bool has_speed, int64_t speed,
3110d58d8453SJohn Snow                             bool has_bitmap, const char *bitmap,
311178f51fdeSJohn Snow                             bool has_on_source_error,
311278f51fdeSJohn Snow                             BlockdevOnError on_source_error,
311378f51fdeSJohn Snow                             bool has_on_target_error,
311478f51fdeSJohn Snow                             BlockdevOnError on_target_error,
311578f51fdeSJohn Snow                             BlockJobTxn *txn, Error **errp)
311699a9addfSStefan Hajnoczi {
3117a0e8544cSFam Zheng     BlockBackend *blk;
311899a9addfSStefan Hajnoczi     BlockDriverState *bs;
311999a9addfSStefan Hajnoczi     BlockDriverState *target_bs;
3120fc5d3f84SIan Main     BlockDriverState *source = NULL;
3121d58d8453SJohn Snow     BdrvDirtyBitmap *bmap = NULL;
3122761731b1SStefan Hajnoczi     AioContext *aio_context;
3123e6641719SMax Reitz     QDict *options = NULL;
312499a9addfSStefan Hajnoczi     Error *local_err = NULL;
312599a9addfSStefan Hajnoczi     int flags;
312699a9addfSStefan Hajnoczi     int64_t size;
312799a9addfSStefan Hajnoczi     int ret;
312899a9addfSStefan Hajnoczi 
312999a9addfSStefan Hajnoczi     if (!has_speed) {
313099a9addfSStefan Hajnoczi         speed = 0;
313199a9addfSStefan Hajnoczi     }
313299a9addfSStefan Hajnoczi     if (!has_on_source_error) {
313399a9addfSStefan Hajnoczi         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
313499a9addfSStefan Hajnoczi     }
313599a9addfSStefan Hajnoczi     if (!has_on_target_error) {
313699a9addfSStefan Hajnoczi         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
313799a9addfSStefan Hajnoczi     }
313899a9addfSStefan Hajnoczi     if (!has_mode) {
313999a9addfSStefan Hajnoczi         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
314099a9addfSStefan Hajnoczi     }
314199a9addfSStefan Hajnoczi 
3142a0e8544cSFam Zheng     blk = blk_by_name(device);
3143a0e8544cSFam Zheng     if (!blk) {
314475158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
314575158ebbSMarkus Armbruster                   "Device '%s' not found", device);
314699a9addfSStefan Hajnoczi         return;
314799a9addfSStefan Hajnoczi     }
314899a9addfSStefan Hajnoczi 
31495433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
3150761731b1SStefan Hajnoczi     aio_context_acquire(aio_context);
3151761731b1SStefan Hajnoczi 
3152c29c1dd3SFam Zheng     /* Although backup_run has this check too, we need to use bs->drv below, so
3153c29c1dd3SFam Zheng      * do an early check redundantly. */
31545433c24fSMax Reitz     if (!blk_is_available(blk)) {
3155c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3156761731b1SStefan Hajnoczi         goto out;
315799a9addfSStefan Hajnoczi     }
31585433c24fSMax Reitz     bs = blk_bs(blk);
315999a9addfSStefan Hajnoczi 
316099a9addfSStefan Hajnoczi     if (!has_format) {
316199a9addfSStefan Hajnoczi         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
316299a9addfSStefan Hajnoczi     }
316399a9addfSStefan Hajnoczi 
3164c29c1dd3SFam Zheng     /* Early check to avoid creating target */
31653718d8abSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
3166761731b1SStefan Hajnoczi         goto out;
316799a9addfSStefan Hajnoczi     }
316899a9addfSStefan Hajnoczi 
316999a9addfSStefan Hajnoczi     flags = bs->open_flags | BDRV_O_RDWR;
317099a9addfSStefan Hajnoczi 
3171fc5d3f84SIan Main     /* See if we have a backing HD we can use to create our new image
3172fc5d3f84SIan Main      * on top of. */
3173fc5d3f84SIan Main     if (sync == MIRROR_SYNC_MODE_TOP) {
3174760e0063SKevin Wolf         source = backing_bs(bs);
3175fc5d3f84SIan Main         if (!source) {
3176fc5d3f84SIan Main             sync = MIRROR_SYNC_MODE_FULL;
3177fc5d3f84SIan Main         }
3178fc5d3f84SIan Main     }
3179fc5d3f84SIan Main     if (sync == MIRROR_SYNC_MODE_NONE) {
3180fc5d3f84SIan Main         source = bs;
3181fc5d3f84SIan Main     }
3182fc5d3f84SIan Main 
318399a9addfSStefan Hajnoczi     size = bdrv_getlength(bs);
318499a9addfSStefan Hajnoczi     if (size < 0) {
318599a9addfSStefan Hajnoczi         error_setg_errno(errp, -size, "bdrv_getlength failed");
3186761731b1SStefan Hajnoczi         goto out;
318799a9addfSStefan Hajnoczi     }
318899a9addfSStefan Hajnoczi 
318999a9addfSStefan Hajnoczi     if (mode != NEW_IMAGE_MODE_EXISTING) {
3190e6641719SMax Reitz         assert(format);
3191fc5d3f84SIan Main         if (source) {
3192fc5d3f84SIan Main             bdrv_img_create(target, format, source->filename,
3193fc5d3f84SIan Main                             source->drv->format_name, NULL,
3194fc5d3f84SIan Main                             size, flags, &local_err, false);
3195fc5d3f84SIan Main         } else {
3196fc5d3f84SIan Main             bdrv_img_create(target, format, NULL, NULL, NULL,
3197fc5d3f84SIan Main                             size, flags, &local_err, false);
3198fc5d3f84SIan Main         }
319999a9addfSStefan Hajnoczi     }
320099a9addfSStefan Hajnoczi 
320184d18f06SMarkus Armbruster     if (local_err) {
320299a9addfSStefan Hajnoczi         error_propagate(errp, local_err);
3203761731b1SStefan Hajnoczi         goto out;
320499a9addfSStefan Hajnoczi     }
320599a9addfSStefan Hajnoczi 
3206e6641719SMax Reitz     if (format) {
3207e6641719SMax Reitz         options = qdict_new();
3208e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
3209e6641719SMax Reitz     }
3210e6641719SMax Reitz 
3211f67503e5SMax Reitz     target_bs = NULL;
32126ebf9aa2SMax Reitz     ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
321399a9addfSStefan Hajnoczi     if (ret < 0) {
321434b5d2c6SMax Reitz         error_propagate(errp, local_err);
3215761731b1SStefan Hajnoczi         goto out;
321699a9addfSStefan Hajnoczi     }
321799a9addfSStefan Hajnoczi 
3218761731b1SStefan Hajnoczi     bdrv_set_aio_context(target_bs, aio_context);
3219761731b1SStefan Hajnoczi 
3220d58d8453SJohn Snow     if (has_bitmap) {
3221d58d8453SJohn Snow         bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3222d58d8453SJohn Snow         if (!bmap) {
3223d58d8453SJohn Snow             error_setg(errp, "Bitmap '%s' could not be found", bitmap);
32240702d3d8SMax Reitz             bdrv_unref(target_bs);
3225d58d8453SJohn Snow             goto out;
3226d58d8453SJohn Snow         }
3227d58d8453SJohn Snow     }
3228d58d8453SJohn Snow 
3229d58d8453SJohn Snow     backup_start(bs, target_bs, speed, sync, bmap,
3230d58d8453SJohn Snow                  on_source_error, on_target_error,
323178f51fdeSJohn Snow                  block_job_cb, bs, txn, &local_err);
323299a9addfSStefan Hajnoczi     if (local_err != NULL) {
32334f6fd349SFam Zheng         bdrv_unref(target_bs);
323499a9addfSStefan Hajnoczi         error_propagate(errp, local_err);
3235761731b1SStefan Hajnoczi         goto out;
323699a9addfSStefan Hajnoczi     }
3237761731b1SStefan Hajnoczi 
3238761731b1SStefan Hajnoczi out:
3239761731b1SStefan Hajnoczi     aio_context_release(aio_context);
324099a9addfSStefan Hajnoczi }
324199a9addfSStefan Hajnoczi 
324278f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target,
324378f51fdeSJohn Snow                       bool has_format, const char *format,
324478f51fdeSJohn Snow                       enum MirrorSyncMode sync,
324578f51fdeSJohn Snow                       bool has_mode, enum NewImageMode mode,
324678f51fdeSJohn Snow                       bool has_speed, int64_t speed,
324778f51fdeSJohn Snow                       bool has_bitmap, const char *bitmap,
324878f51fdeSJohn Snow                       bool has_on_source_error, BlockdevOnError on_source_error,
324978f51fdeSJohn Snow                       bool has_on_target_error, BlockdevOnError on_target_error,
325078f51fdeSJohn Snow                       Error **errp)
325178f51fdeSJohn Snow {
325278f51fdeSJohn Snow     return do_drive_backup(device, target, has_format, format, sync,
325378f51fdeSJohn Snow                            has_mode, mode, has_speed, speed,
325478f51fdeSJohn Snow                            has_bitmap, bitmap,
325578f51fdeSJohn Snow                            has_on_source_error, on_source_error,
325678f51fdeSJohn Snow                            has_on_target_error, on_target_error,
325778f51fdeSJohn Snow                            NULL, errp);
325878f51fdeSJohn Snow }
325978f51fdeSJohn Snow 
3260c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3261c13163fbSBenoît Canet {
3262d5a8ee60SAlberto Garcia     return bdrv_named_nodes_list(errp);
3263c13163fbSBenoît Canet }
3264c13163fbSBenoît Canet 
326578f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target,
3266c29c1dd3SFam Zheng                          enum MirrorSyncMode sync,
3267c29c1dd3SFam Zheng                          bool has_speed, int64_t speed,
3268c29c1dd3SFam Zheng                          bool has_on_source_error,
3269c29c1dd3SFam Zheng                          BlockdevOnError on_source_error,
3270c29c1dd3SFam Zheng                          bool has_on_target_error,
3271c29c1dd3SFam Zheng                          BlockdevOnError on_target_error,
327278f51fdeSJohn Snow                          BlockJobTxn *txn, Error **errp)
3273c29c1dd3SFam Zheng {
32745433c24fSMax Reitz     BlockBackend *blk, *target_blk;
3275c29c1dd3SFam Zheng     BlockDriverState *bs;
3276c29c1dd3SFam Zheng     BlockDriverState *target_bs;
3277c29c1dd3SFam Zheng     Error *local_err = NULL;
3278c29c1dd3SFam Zheng     AioContext *aio_context;
3279c29c1dd3SFam Zheng 
3280c29c1dd3SFam Zheng     if (!has_speed) {
3281c29c1dd3SFam Zheng         speed = 0;
3282c29c1dd3SFam Zheng     }
3283c29c1dd3SFam Zheng     if (!has_on_source_error) {
3284c29c1dd3SFam Zheng         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3285c29c1dd3SFam Zheng     }
3286c29c1dd3SFam Zheng     if (!has_on_target_error) {
3287c29c1dd3SFam Zheng         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3288c29c1dd3SFam Zheng     }
3289c29c1dd3SFam Zheng 
3290a0e8544cSFam Zheng     blk = blk_by_name(device);
3291a0e8544cSFam Zheng     if (!blk) {
32925b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", device);
3293c29c1dd3SFam Zheng         return;
3294c29c1dd3SFam Zheng     }
3295c29c1dd3SFam Zheng 
32965433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
3297c29c1dd3SFam Zheng     aio_context_acquire(aio_context);
3298c29c1dd3SFam Zheng 
32995433c24fSMax Reitz     if (!blk_is_available(blk)) {
33005433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
33015433c24fSMax Reitz         goto out;
33025433c24fSMax Reitz     }
33035433c24fSMax Reitz     bs = blk_bs(blk);
33045433c24fSMax Reitz 
33055433c24fSMax Reitz     target_blk = blk_by_name(target);
33065433c24fSMax Reitz     if (!target_blk) {
33075b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", target);
3308c29c1dd3SFam Zheng         goto out;
3309c29c1dd3SFam Zheng     }
33105433c24fSMax Reitz 
33115433c24fSMax Reitz     if (!blk_is_available(target_blk)) {
33125433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", target);
33135433c24fSMax Reitz         goto out;
33145433c24fSMax Reitz     }
33155433c24fSMax Reitz     target_bs = blk_bs(target_blk);
3316c29c1dd3SFam Zheng 
3317c29c1dd3SFam Zheng     bdrv_ref(target_bs);
3318c29c1dd3SFam Zheng     bdrv_set_aio_context(target_bs, aio_context);
3319d58d8453SJohn Snow     backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
332078f51fdeSJohn Snow                  on_target_error, block_job_cb, bs, txn, &local_err);
3321c29c1dd3SFam Zheng     if (local_err != NULL) {
3322c29c1dd3SFam Zheng         bdrv_unref(target_bs);
3323c29c1dd3SFam Zheng         error_propagate(errp, local_err);
3324c29c1dd3SFam Zheng     }
3325c29c1dd3SFam Zheng out:
3326c29c1dd3SFam Zheng     aio_context_release(aio_context);
3327c29c1dd3SFam Zheng }
3328c29c1dd3SFam Zheng 
332978f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target,
333078f51fdeSJohn Snow                          enum MirrorSyncMode sync,
333178f51fdeSJohn Snow                          bool has_speed, int64_t speed,
333278f51fdeSJohn Snow                          bool has_on_source_error,
333378f51fdeSJohn Snow                          BlockdevOnError on_source_error,
333478f51fdeSJohn Snow                          bool has_on_target_error,
333578f51fdeSJohn Snow                          BlockdevOnError on_target_error,
333678f51fdeSJohn Snow                          Error **errp)
333778f51fdeSJohn Snow {
333878f51fdeSJohn Snow     do_blockdev_backup(device, target, sync, has_speed, speed,
333978f51fdeSJohn Snow                        has_on_source_error, on_source_error,
334078f51fdeSJohn Snow                        has_on_target_error, on_target_error,
334178f51fdeSJohn Snow                        NULL, errp);
334278f51fdeSJohn Snow }
334378f51fdeSJohn Snow 
33444193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring.
33454193cdd7SFam Zheng  * Caller should hold @device and @target's aio context (must be the same).
33464193cdd7SFam Zheng  **/
33474193cdd7SFam Zheng static void blockdev_mirror_common(BlockDriverState *bs,
33484193cdd7SFam Zheng                                    BlockDriverState *target,
334909158f00SBenoît Canet                                    bool has_replaces, const char *replaces,
3350d9b902dbSPaolo Bonzini                                    enum MirrorSyncMode sync,
3351b952b558SPaolo Bonzini                                    bool has_speed, int64_t speed,
3352eee13dfeSPaolo Bonzini                                    bool has_granularity, uint32_t granularity,
335308e4ed6cSPaolo Bonzini                                    bool has_buf_size, int64_t buf_size,
33544193cdd7SFam Zheng                                    bool has_on_source_error,
33554193cdd7SFam Zheng                                    BlockdevOnError on_source_error,
33564193cdd7SFam Zheng                                    bool has_on_target_error,
33574193cdd7SFam Zheng                                    BlockdevOnError on_target_error,
33580fc9f8eaSFam Zheng                                    bool has_unmap, bool unmap,
3359b952b558SPaolo Bonzini                                    Error **errp)
3360d9b902dbSPaolo Bonzini {
3361d9b902dbSPaolo Bonzini 
3362d9b902dbSPaolo Bonzini     if (!has_speed) {
3363d9b902dbSPaolo Bonzini         speed = 0;
3364d9b902dbSPaolo Bonzini     }
3365b952b558SPaolo Bonzini     if (!has_on_source_error) {
3366b952b558SPaolo Bonzini         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3367b952b558SPaolo Bonzini     }
3368b952b558SPaolo Bonzini     if (!has_on_target_error) {
3369b952b558SPaolo Bonzini         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3370b952b558SPaolo Bonzini     }
3371eee13dfeSPaolo Bonzini     if (!has_granularity) {
3372eee13dfeSPaolo Bonzini         granularity = 0;
3373eee13dfeSPaolo Bonzini     }
337408e4ed6cSPaolo Bonzini     if (!has_buf_size) {
337548ac0a4dSWen Congyang         buf_size = 0;
337608e4ed6cSPaolo Bonzini     }
33770fc9f8eaSFam Zheng     if (!has_unmap) {
33780fc9f8eaSFam Zheng         unmap = true;
33790fc9f8eaSFam Zheng     }
338008e4ed6cSPaolo Bonzini 
3381eee13dfeSPaolo Bonzini     if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3382c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
33833cbbe9fdSStefan Hajnoczi                    "a value in range [512B, 64MB]");
3384eee13dfeSPaolo Bonzini         return;
3385eee13dfeSPaolo Bonzini     }
3386eee13dfeSPaolo Bonzini     if (granularity & (granularity - 1)) {
3387c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3388c6bd8c70SMarkus Armbruster                    "power of 2");
3389eee13dfeSPaolo Bonzini         return;
3390eee13dfeSPaolo Bonzini     }
3391d9b902dbSPaolo Bonzini 
33924193cdd7SFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
33934193cdd7SFam Zheng         return;
33944193cdd7SFam Zheng     }
3395e40e5027SFam Zheng     if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3396e40e5027SFam Zheng         return;
3397e40e5027SFam Zheng     }
3398df92562eSFam Zheng     if (target->blk) {
3399df92562eSFam Zheng         error_setg(errp, "Cannot mirror to an attached block device");
3400df92562eSFam Zheng         return;
3401df92562eSFam Zheng     }
34024193cdd7SFam Zheng 
34034193cdd7SFam Zheng     if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
34044193cdd7SFam Zheng         sync = MIRROR_SYNC_MODE_FULL;
34054193cdd7SFam Zheng     }
34064193cdd7SFam Zheng 
34074193cdd7SFam Zheng     /* pass the node name to replace to mirror start since it's loose coupling
34084193cdd7SFam Zheng      * and will allow to check whether the node still exist at mirror completion
34094193cdd7SFam Zheng      */
34104193cdd7SFam Zheng     mirror_start(bs, target,
34114193cdd7SFam Zheng                  has_replaces ? replaces : NULL,
34124193cdd7SFam Zheng                  speed, granularity, buf_size, sync,
34134193cdd7SFam Zheng                  on_source_error, on_target_error, unmap,
34144193cdd7SFam Zheng                  block_job_cb, bs, errp);
34154193cdd7SFam Zheng }
34164193cdd7SFam Zheng 
34174193cdd7SFam Zheng void qmp_drive_mirror(const char *device, const char *target,
34184193cdd7SFam Zheng                       bool has_format, const char *format,
34194193cdd7SFam Zheng                       bool has_node_name, const char *node_name,
34204193cdd7SFam Zheng                       bool has_replaces, const char *replaces,
34214193cdd7SFam Zheng                       enum MirrorSyncMode sync,
34224193cdd7SFam Zheng                       bool has_mode, enum NewImageMode mode,
34234193cdd7SFam Zheng                       bool has_speed, int64_t speed,
34244193cdd7SFam Zheng                       bool has_granularity, uint32_t granularity,
34254193cdd7SFam Zheng                       bool has_buf_size, int64_t buf_size,
34264193cdd7SFam Zheng                       bool has_on_source_error, BlockdevOnError on_source_error,
34274193cdd7SFam Zheng                       bool has_on_target_error, BlockdevOnError on_target_error,
34284193cdd7SFam Zheng                       bool has_unmap, bool unmap,
34294193cdd7SFam Zheng                       Error **errp)
34304193cdd7SFam Zheng {
34314193cdd7SFam Zheng     BlockDriverState *bs;
34324193cdd7SFam Zheng     BlockBackend *blk;
34334193cdd7SFam Zheng     BlockDriverState *source, *target_bs;
34344193cdd7SFam Zheng     AioContext *aio_context;
34354193cdd7SFam Zheng     Error *local_err = NULL;
34364193cdd7SFam Zheng     QDict *options = NULL;
34374193cdd7SFam Zheng     int flags;
34384193cdd7SFam Zheng     int64_t size;
34394193cdd7SFam Zheng     int ret;
34404193cdd7SFam Zheng 
3441a0e8544cSFam Zheng     blk = blk_by_name(device);
3442a0e8544cSFam Zheng     if (!blk) {
344375158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
344475158ebbSMarkus Armbruster                   "Device '%s' not found", device);
3445d9b902dbSPaolo Bonzini         return;
3446d9b902dbSPaolo Bonzini     }
3447d9b902dbSPaolo Bonzini 
34485433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
34495a7e7a0bSStefan Hajnoczi     aio_context_acquire(aio_context);
34505a7e7a0bSStefan Hajnoczi 
34515433c24fSMax Reitz     if (!blk_is_available(blk)) {
3452c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
34535a7e7a0bSStefan Hajnoczi         goto out;
3454d9b902dbSPaolo Bonzini     }
34555433c24fSMax Reitz     bs = blk_bs(blk);
34564193cdd7SFam Zheng     if (!has_mode) {
34574193cdd7SFam Zheng         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
34584193cdd7SFam Zheng     }
3459d9b902dbSPaolo Bonzini 
3460d9b902dbSPaolo Bonzini     if (!has_format) {
3461d9b902dbSPaolo Bonzini         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3462d9b902dbSPaolo Bonzini     }
3463d9b902dbSPaolo Bonzini 
3464d9b902dbSPaolo Bonzini     flags = bs->open_flags | BDRV_O_RDWR;
3465760e0063SKevin Wolf     source = backing_bs(bs);
3466d9b902dbSPaolo Bonzini     if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3467d9b902dbSPaolo Bonzini         sync = MIRROR_SYNC_MODE_FULL;
3468d9b902dbSPaolo Bonzini     }
3469117e0c82SMax Reitz     if (sync == MIRROR_SYNC_MODE_NONE) {
3470117e0c82SMax Reitz         source = bs;
3471117e0c82SMax Reitz     }
3472d9b902dbSPaolo Bonzini 
3473ac3c5d83SStefan Hajnoczi     size = bdrv_getlength(bs);
3474ac3c5d83SStefan Hajnoczi     if (size < 0) {
3475ac3c5d83SStefan Hajnoczi         error_setg_errno(errp, -size, "bdrv_getlength failed");
34765a7e7a0bSStefan Hajnoczi         goto out;
3477ac3c5d83SStefan Hajnoczi     }
3478ac3c5d83SStefan Hajnoczi 
347909158f00SBenoît Canet     if (has_replaces) {
348009158f00SBenoît Canet         BlockDriverState *to_replace_bs;
34815a7e7a0bSStefan Hajnoczi         AioContext *replace_aio_context;
34825a7e7a0bSStefan Hajnoczi         int64_t replace_size;
348309158f00SBenoît Canet 
348409158f00SBenoît Canet         if (!has_node_name) {
348509158f00SBenoît Canet             error_setg(errp, "a node-name must be provided when replacing a"
348609158f00SBenoît Canet                              " named node of the graph");
34875a7e7a0bSStefan Hajnoczi             goto out;
348809158f00SBenoît Canet         }
348909158f00SBenoît Canet 
3490e12f3784SWen Congyang         to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
349109158f00SBenoît Canet 
349209158f00SBenoît Canet         if (!to_replace_bs) {
349309158f00SBenoît Canet             error_propagate(errp, local_err);
34945a7e7a0bSStefan Hajnoczi             goto out;
349509158f00SBenoît Canet         }
349609158f00SBenoît Canet 
34975a7e7a0bSStefan Hajnoczi         replace_aio_context = bdrv_get_aio_context(to_replace_bs);
34985a7e7a0bSStefan Hajnoczi         aio_context_acquire(replace_aio_context);
34995a7e7a0bSStefan Hajnoczi         replace_size = bdrv_getlength(to_replace_bs);
35005a7e7a0bSStefan Hajnoczi         aio_context_release(replace_aio_context);
35015a7e7a0bSStefan Hajnoczi 
35025a7e7a0bSStefan Hajnoczi         if (size != replace_size) {
350309158f00SBenoît Canet             error_setg(errp, "cannot replace image with a mirror image of "
350409158f00SBenoît Canet                              "different size");
35055a7e7a0bSStefan Hajnoczi             goto out;
350609158f00SBenoît Canet         }
350709158f00SBenoît Canet     }
350809158f00SBenoît Canet 
350914526864SMax Reitz     if ((sync == MIRROR_SYNC_MODE_FULL || !source)
351014526864SMax Reitz         && mode != NEW_IMAGE_MODE_EXISTING)
351114526864SMax Reitz     {
3512d9b902dbSPaolo Bonzini         /* create new image w/o backing file */
3513e6641719SMax Reitz         assert(format);
3514cf8f2426SLuiz Capitulino         bdrv_img_create(target, format,
3515f382d43aSMiroslav Rezanina                         NULL, NULL, NULL, size, flags, &local_err, false);
3516d9b902dbSPaolo Bonzini     } else {
3517d9b902dbSPaolo Bonzini         switch (mode) {
3518d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_EXISTING:
3519d9b902dbSPaolo Bonzini             break;
3520d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3521d9b902dbSPaolo Bonzini             /* create new image with backing file */
3522cf8f2426SLuiz Capitulino             bdrv_img_create(target, format,
3523d9b902dbSPaolo Bonzini                             source->filename,
3524d9b902dbSPaolo Bonzini                             source->drv->format_name,
3525f382d43aSMiroslav Rezanina                             NULL, size, flags, &local_err, false);
3526d9b902dbSPaolo Bonzini             break;
3527d9b902dbSPaolo Bonzini         default:
3528d9b902dbSPaolo Bonzini             abort();
3529d9b902dbSPaolo Bonzini         }
3530d9b902dbSPaolo Bonzini     }
3531d9b902dbSPaolo Bonzini 
353284d18f06SMarkus Armbruster     if (local_err) {
3533cf8f2426SLuiz Capitulino         error_propagate(errp, local_err);
35345a7e7a0bSStefan Hajnoczi         goto out;
3535d9b902dbSPaolo Bonzini     }
3536d9b902dbSPaolo Bonzini 
35374c828dc6SBenoît Canet     options = qdict_new();
3538e6641719SMax Reitz     if (has_node_name) {
35394c828dc6SBenoît Canet         qdict_put(options, "node-name", qstring_from_str(node_name));
35404c828dc6SBenoît Canet     }
3541e6641719SMax Reitz     if (format) {
3542e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
3543e6641719SMax Reitz     }
35444c828dc6SBenoît Canet 
3545b812f671SPaolo Bonzini     /* Mirroring takes care of copy-on-write using the source's backing
3546b812f671SPaolo Bonzini      * file.
3547b812f671SPaolo Bonzini      */
3548f67503e5SMax Reitz     target_bs = NULL;
35494c828dc6SBenoît Canet     ret = bdrv_open(&target_bs, target, NULL, options,
35506ebf9aa2SMax Reitz                     flags | BDRV_O_NO_BACKING, &local_err);
3551d9b902dbSPaolo Bonzini     if (ret < 0) {
355234b5d2c6SMax Reitz         error_propagate(errp, local_err);
35535a7e7a0bSStefan Hajnoczi         goto out;
3554d9b902dbSPaolo Bonzini     }
3555d9b902dbSPaolo Bonzini 
35565a7e7a0bSStefan Hajnoczi     bdrv_set_aio_context(target_bs, aio_context);
35575a7e7a0bSStefan Hajnoczi 
35584193cdd7SFam Zheng     blockdev_mirror_common(bs, target_bs,
35594193cdd7SFam Zheng                            has_replaces, replaces, sync,
35604193cdd7SFam Zheng                            has_speed, speed,
35614193cdd7SFam Zheng                            has_granularity, granularity,
35624193cdd7SFam Zheng                            has_buf_size, buf_size,
35634193cdd7SFam Zheng                            has_on_source_error, on_source_error,
35644193cdd7SFam Zheng                            has_on_target_error, on_target_error,
35654193cdd7SFam Zheng                            has_unmap, unmap,
35664193cdd7SFam Zheng                            &local_err);
35674193cdd7SFam Zheng     if (local_err) {
3568d9b902dbSPaolo Bonzini         error_propagate(errp, local_err);
35694193cdd7SFam Zheng         bdrv_unref(target_bs);
3570d9b902dbSPaolo Bonzini     }
35715a7e7a0bSStefan Hajnoczi out:
35725a7e7a0bSStefan Hajnoczi     aio_context_release(aio_context);
3573d9b902dbSPaolo Bonzini }
3574d9b902dbSPaolo Bonzini 
3575df92562eSFam Zheng void qmp_blockdev_mirror(const char *device, const char *target,
3576df92562eSFam Zheng                          bool has_replaces, const char *replaces,
3577df92562eSFam Zheng                          MirrorSyncMode sync,
3578df92562eSFam Zheng                          bool has_speed, int64_t speed,
3579df92562eSFam Zheng                          bool has_granularity, uint32_t granularity,
3580df92562eSFam Zheng                          bool has_buf_size, int64_t buf_size,
3581df92562eSFam Zheng                          bool has_on_source_error,
3582df92562eSFam Zheng                          BlockdevOnError on_source_error,
3583df92562eSFam Zheng                          bool has_on_target_error,
3584df92562eSFam Zheng                          BlockdevOnError on_target_error,
3585df92562eSFam Zheng                          Error **errp)
3586df92562eSFam Zheng {
3587df92562eSFam Zheng     BlockDriverState *bs;
3588df92562eSFam Zheng     BlockBackend *blk;
3589df92562eSFam Zheng     BlockDriverState *target_bs;
3590df92562eSFam Zheng     AioContext *aio_context;
3591df92562eSFam Zheng     Error *local_err = NULL;
3592df92562eSFam Zheng 
3593df92562eSFam Zheng     blk = blk_by_name(device);
3594df92562eSFam Zheng     if (!blk) {
3595df92562eSFam Zheng         error_setg(errp, "Device '%s' not found", device);
3596df92562eSFam Zheng         return;
3597df92562eSFam Zheng     }
3598df92562eSFam Zheng     bs = blk_bs(blk);
3599df92562eSFam Zheng 
3600df92562eSFam Zheng     if (!bs) {
3601df92562eSFam Zheng         error_setg(errp, "Device '%s' has no media", device);
3602df92562eSFam Zheng         return;
3603df92562eSFam Zheng     }
3604df92562eSFam Zheng 
3605df92562eSFam Zheng     target_bs = bdrv_lookup_bs(target, target, errp);
3606df92562eSFam Zheng     if (!target_bs) {
3607df92562eSFam Zheng         return;
3608df92562eSFam Zheng     }
3609df92562eSFam Zheng 
3610df92562eSFam Zheng     aio_context = bdrv_get_aio_context(bs);
3611df92562eSFam Zheng     aio_context_acquire(aio_context);
3612df92562eSFam Zheng 
3613df92562eSFam Zheng     bdrv_ref(target_bs);
3614df92562eSFam Zheng     bdrv_set_aio_context(target_bs, aio_context);
3615df92562eSFam Zheng 
3616df92562eSFam Zheng     blockdev_mirror_common(bs, target_bs,
3617df92562eSFam Zheng                            has_replaces, replaces, sync,
3618df92562eSFam Zheng                            has_speed, speed,
3619df92562eSFam Zheng                            has_granularity, granularity,
3620df92562eSFam Zheng                            has_buf_size, buf_size,
3621df92562eSFam Zheng                            has_on_source_error, on_source_error,
3622df92562eSFam Zheng                            has_on_target_error, on_target_error,
3623df92562eSFam Zheng                            true, true,
3624df92562eSFam Zheng                            &local_err);
3625df92562eSFam Zheng     if (local_err) {
3626df92562eSFam Zheng         error_propagate(errp, local_err);
3627df92562eSFam Zheng         bdrv_unref(target_bs);
3628df92562eSFam Zheng     }
3629df92562eSFam Zheng 
3630df92562eSFam Zheng     aio_context_release(aio_context);
3631df92562eSFam Zheng }
3632df92562eSFam Zheng 
36333d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */
363424d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context,
363524d6bffeSMarkus Armbruster                                 Error **errp)
36362d47c6e9SStefan Hajnoczi {
3637a0e8544cSFam Zheng     BlockBackend *blk;
36382d47c6e9SStefan Hajnoczi     BlockDriverState *bs;
36392d47c6e9SStefan Hajnoczi 
36405433c24fSMax Reitz     *aio_context = NULL;
36415433c24fSMax Reitz 
3642a0e8544cSFam Zheng     blk = blk_by_name(device);
3643a0e8544cSFam Zheng     if (!blk) {
36443d948cdfSStefan Hajnoczi         goto notfound;
36452d47c6e9SStefan Hajnoczi     }
36463d948cdfSStefan Hajnoczi 
36475433c24fSMax Reitz     *aio_context = blk_get_aio_context(blk);
36483d948cdfSStefan Hajnoczi     aio_context_acquire(*aio_context);
36493d948cdfSStefan Hajnoczi 
36505433c24fSMax Reitz     if (!blk_is_available(blk)) {
36515433c24fSMax Reitz         goto notfound;
36525433c24fSMax Reitz     }
36535433c24fSMax Reitz     bs = blk_bs(blk);
36545433c24fSMax Reitz 
36553d948cdfSStefan Hajnoczi     if (!bs->job) {
36563d948cdfSStefan Hajnoczi         goto notfound;
36573d948cdfSStefan Hajnoczi     }
36583d948cdfSStefan Hajnoczi 
36592d47c6e9SStefan Hajnoczi     return bs->job;
36603d948cdfSStefan Hajnoczi 
36613d948cdfSStefan Hajnoczi notfound:
36622e3a0266SMarkus Armbruster     error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
36632e3a0266SMarkus Armbruster               "No active block job on device '%s'", device);
36645433c24fSMax Reitz     if (*aio_context) {
36655433c24fSMax Reitz         aio_context_release(*aio_context);
36663d948cdfSStefan Hajnoczi         *aio_context = NULL;
36675433c24fSMax Reitz     }
36683d948cdfSStefan Hajnoczi     return NULL;
36692d47c6e9SStefan Hajnoczi }
36702d47c6e9SStefan Hajnoczi 
3671882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
36722d47c6e9SStefan Hajnoczi {
36733d948cdfSStefan Hajnoczi     AioContext *aio_context;
367424d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
36752d47c6e9SStefan Hajnoczi 
36762d47c6e9SStefan Hajnoczi     if (!job) {
36772d47c6e9SStefan Hajnoczi         return;
36782d47c6e9SStefan Hajnoczi     }
36792d47c6e9SStefan Hajnoczi 
3680882ec7ceSStefan Hajnoczi     block_job_set_speed(job, speed, errp);
36813d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
36822d47c6e9SStefan Hajnoczi }
3683370521a1SStefan Hajnoczi 
36846e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device,
36856e37fb81SPaolo Bonzini                           bool has_force, bool force, Error **errp)
36866e37fb81SPaolo Bonzini {
36873d948cdfSStefan Hajnoczi     AioContext *aio_context;
368824d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
36896e37fb81SPaolo Bonzini 
36906e37fb81SPaolo Bonzini     if (!job) {
36916e37fb81SPaolo Bonzini         return;
36926e37fb81SPaolo Bonzini     }
36933d948cdfSStefan Hajnoczi 
36943d948cdfSStefan Hajnoczi     if (!has_force) {
36953d948cdfSStefan Hajnoczi         force = false;
36963d948cdfSStefan Hajnoczi     }
36973d948cdfSStefan Hajnoczi 
3698751ebd76SFam Zheng     if (job->user_paused && !force) {
3699f231b88dSCole Robinson         error_setg(errp, "The block job for device '%s' is currently paused",
3700f231b88dSCole Robinson                    device);
37013d948cdfSStefan Hajnoczi         goto out;
37026e37fb81SPaolo Bonzini     }
37036e37fb81SPaolo Bonzini 
37046e37fb81SPaolo Bonzini     trace_qmp_block_job_cancel(job);
37056e37fb81SPaolo Bonzini     block_job_cancel(job);
37063d948cdfSStefan Hajnoczi out:
37073d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
37086e37fb81SPaolo Bonzini }
37096e37fb81SPaolo Bonzini 
37106e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp)
3711370521a1SStefan Hajnoczi {
37123d948cdfSStefan Hajnoczi     AioContext *aio_context;
371324d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
3714370521a1SStefan Hajnoczi 
3715751ebd76SFam Zheng     if (!job || job->user_paused) {
3716370521a1SStefan Hajnoczi         return;
3717370521a1SStefan Hajnoczi     }
37186e37fb81SPaolo Bonzini 
3719751ebd76SFam Zheng     job->user_paused = true;
37206e37fb81SPaolo Bonzini     trace_qmp_block_job_pause(job);
37216e37fb81SPaolo Bonzini     block_job_pause(job);
37223d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
37236e37fb81SPaolo Bonzini }
37246e37fb81SPaolo Bonzini 
37256e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp)
37266e37fb81SPaolo Bonzini {
37273d948cdfSStefan Hajnoczi     AioContext *aio_context;
372824d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
37296e37fb81SPaolo Bonzini 
3730751ebd76SFam Zheng     if (!job || !job->user_paused) {
37318acc72a4SPaolo Bonzini         return;
37328acc72a4SPaolo Bonzini     }
3733370521a1SStefan Hajnoczi 
3734751ebd76SFam Zheng     job->user_paused = false;
37356e37fb81SPaolo Bonzini     trace_qmp_block_job_resume(job);
37366e37fb81SPaolo Bonzini     block_job_resume(job);
37373d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
3738370521a1SStefan Hajnoczi }
3739fb5458cdSStefan Hajnoczi 
3740aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp)
3741aeae883bSPaolo Bonzini {
37423d948cdfSStefan Hajnoczi     AioContext *aio_context;
374324d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
3744aeae883bSPaolo Bonzini 
3745aeae883bSPaolo Bonzini     if (!job) {
3746aeae883bSPaolo Bonzini         return;
3747aeae883bSPaolo Bonzini     }
3748aeae883bSPaolo Bonzini 
3749aeae883bSPaolo Bonzini     trace_qmp_block_job_complete(job);
3750aeae883bSPaolo Bonzini     block_job_complete(job, errp);
37513d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
3752aeae883bSPaolo Bonzini }
3753aeae883bSPaolo Bonzini 
3754fa40e656SJeff Cody void qmp_change_backing_file(const char *device,
3755fa40e656SJeff Cody                              const char *image_node_name,
3756fa40e656SJeff Cody                              const char *backing_file,
3757fa40e656SJeff Cody                              Error **errp)
3758fa40e656SJeff Cody {
3759a0e8544cSFam Zheng     BlockBackend *blk;
3760fa40e656SJeff Cody     BlockDriverState *bs = NULL;
3761729962f6SStefan Hajnoczi     AioContext *aio_context;
3762fa40e656SJeff Cody     BlockDriverState *image_bs = NULL;
3763fa40e656SJeff Cody     Error *local_err = NULL;
3764fa40e656SJeff Cody     bool ro;
3765fa40e656SJeff Cody     int open_flags;
3766fa40e656SJeff Cody     int ret;
3767fa40e656SJeff Cody 
3768a0e8544cSFam Zheng     blk = blk_by_name(device);
3769a0e8544cSFam Zheng     if (!blk) {
377075158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
377175158ebbSMarkus Armbruster                   "Device '%s' not found", device);
3772fa40e656SJeff Cody         return;
3773fa40e656SJeff Cody     }
3774fa40e656SJeff Cody 
37755433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
3776729962f6SStefan Hajnoczi     aio_context_acquire(aio_context);
3777729962f6SStefan Hajnoczi 
37785433c24fSMax Reitz     if (!blk_is_available(blk)) {
37795433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
37805433c24fSMax Reitz         goto out;
37815433c24fSMax Reitz     }
37825433c24fSMax Reitz     bs = blk_bs(blk);
37835433c24fSMax Reitz 
3784fa40e656SJeff Cody     image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3785fa40e656SJeff Cody     if (local_err) {
3786fa40e656SJeff Cody         error_propagate(errp, local_err);
3787729962f6SStefan Hajnoczi         goto out;
3788fa40e656SJeff Cody     }
3789fa40e656SJeff Cody 
3790fa40e656SJeff Cody     if (!image_bs) {
3791fa40e656SJeff Cody         error_setg(errp, "image file not found");
3792729962f6SStefan Hajnoczi         goto out;
3793fa40e656SJeff Cody     }
3794fa40e656SJeff Cody 
3795fa40e656SJeff Cody     if (bdrv_find_base(image_bs) == image_bs) {
3796fa40e656SJeff Cody         error_setg(errp, "not allowing backing file change on an image "
3797fa40e656SJeff Cody                          "without a backing file");
3798729962f6SStefan Hajnoczi         goto out;
3799fa40e656SJeff Cody     }
3800fa40e656SJeff Cody 
3801fa40e656SJeff Cody     /* even though we are not necessarily operating on bs, we need it to
3802fa40e656SJeff Cody      * determine if block ops are currently prohibited on the chain */
3803fa40e656SJeff Cody     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3804729962f6SStefan Hajnoczi         goto out;
3805fa40e656SJeff Cody     }
3806fa40e656SJeff Cody 
3807fa40e656SJeff Cody     /* final sanity check */
3808fa40e656SJeff Cody     if (!bdrv_chain_contains(bs, image_bs)) {
3809fa40e656SJeff Cody         error_setg(errp, "'%s' and image file are not in the same chain",
3810fa40e656SJeff Cody                    device);
3811729962f6SStefan Hajnoczi         goto out;
3812fa40e656SJeff Cody     }
3813fa40e656SJeff Cody 
3814fa40e656SJeff Cody     /* if not r/w, reopen to make r/w */
3815fa40e656SJeff Cody     open_flags = image_bs->open_flags;
3816fa40e656SJeff Cody     ro = bdrv_is_read_only(image_bs);
3817fa40e656SJeff Cody 
3818fa40e656SJeff Cody     if (ro) {
3819fa40e656SJeff Cody         bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3820fa40e656SJeff Cody         if (local_err) {
3821fa40e656SJeff Cody             error_propagate(errp, local_err);
3822729962f6SStefan Hajnoczi             goto out;
3823fa40e656SJeff Cody         }
3824fa40e656SJeff Cody     }
3825fa40e656SJeff Cody 
3826fa40e656SJeff Cody     ret = bdrv_change_backing_file(image_bs, backing_file,
3827fa40e656SJeff Cody                                image_bs->drv ? image_bs->drv->format_name : "");
3828fa40e656SJeff Cody 
3829fa40e656SJeff Cody     if (ret < 0) {
3830fa40e656SJeff Cody         error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3831fa40e656SJeff Cody                          backing_file);
3832fa40e656SJeff Cody         /* don't exit here, so we can try to restore open flags if
3833fa40e656SJeff Cody          * appropriate */
3834fa40e656SJeff Cody     }
3835fa40e656SJeff Cody 
3836fa40e656SJeff Cody     if (ro) {
3837fa40e656SJeff Cody         bdrv_reopen(image_bs, open_flags, &local_err);
3838fa40e656SJeff Cody         if (local_err) {
3839fa40e656SJeff Cody             error_propagate(errp, local_err); /* will preserve prior errp */
3840fa40e656SJeff Cody         }
3841fa40e656SJeff Cody     }
3842729962f6SStefan Hajnoczi 
3843729962f6SStefan Hajnoczi out:
3844729962f6SStefan Hajnoczi     aio_context_release(aio_context);
3845fa40e656SJeff Cody }
3846fa40e656SJeff Cody 
3847d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3848d26c9a15SKevin Wolf {
3849d26c9a15SKevin Wolf     QmpOutputVisitor *ov = qmp_output_visitor_new();
3850be4b67bcSMax Reitz     BlockDriverState *bs;
3851be4b67bcSMax Reitz     BlockBackend *blk = NULL;
3852d26c9a15SKevin Wolf     QObject *obj;
3853d26c9a15SKevin Wolf     QDict *qdict;
3854d26c9a15SKevin Wolf     Error *local_err = NULL;
3855d26c9a15SKevin Wolf 
385660e19e06SMarkus Armbruster     /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3857d26c9a15SKevin Wolf      * cache.direct=false instead of silently switching to aio=threads, except
385860e19e06SMarkus Armbruster      * when called from drive_new().
3859d26c9a15SKevin Wolf      *
3860d26c9a15SKevin Wolf      * For now, simply forbidding the combination for all drivers will do. */
3861d26c9a15SKevin Wolf     if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3862c6e0bd9bSKevin Wolf         bool direct = options->has_cache &&
3863c6e0bd9bSKevin Wolf                       options->cache->has_direct &&
3864c6e0bd9bSKevin Wolf                       options->cache->direct;
3865c6e0bd9bSKevin Wolf         if (!direct) {
3866d26c9a15SKevin Wolf             error_setg(errp, "aio=native requires cache.direct=true");
3867d26c9a15SKevin Wolf             goto fail;
3868d26c9a15SKevin Wolf         }
3869d26c9a15SKevin Wolf     }
3870d26c9a15SKevin Wolf 
387151e72bc1SEric Blake     visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options,
387251e72bc1SEric Blake                                &local_err);
387384d18f06SMarkus Armbruster     if (local_err) {
3874d26c9a15SKevin Wolf         error_propagate(errp, local_err);
3875d26c9a15SKevin Wolf         goto fail;
3876d26c9a15SKevin Wolf     }
3877d26c9a15SKevin Wolf 
3878d26c9a15SKevin Wolf     obj = qmp_output_get_qobject(ov);
3879d26c9a15SKevin Wolf     qdict = qobject_to_qdict(obj);
3880d26c9a15SKevin Wolf 
3881d26c9a15SKevin Wolf     qdict_flatten(qdict);
3882d26c9a15SKevin Wolf 
3883be4b67bcSMax Reitz     if (options->has_id) {
388418e46a03SMarkus Armbruster         blk = blockdev_init(NULL, qdict, &local_err);
388584d18f06SMarkus Armbruster         if (local_err) {
3886b681072dSKevin Wolf             error_propagate(errp, local_err);
3887d26c9a15SKevin Wolf             goto fail;
3888d26c9a15SKevin Wolf         }
3889d26c9a15SKevin Wolf 
3890be4b67bcSMax Reitz         bs = blk_bs(blk);
3891be4b67bcSMax Reitz     } else {
3892be4b67bcSMax Reitz         if (!qdict_get_try_str(qdict, "node-name")) {
3893be4b67bcSMax Reitz             error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3894be4b67bcSMax Reitz                        "the root node");
3895be4b67bcSMax Reitz             goto fail;
3896be4b67bcSMax Reitz         }
3897be4b67bcSMax Reitz 
3898bd745e23SMax Reitz         bs = bds_tree_init(qdict, errp);
3899bd745e23SMax Reitz         if (!bs) {
3900be4b67bcSMax Reitz             goto fail;
3901be4b67bcSMax Reitz         }
39029c4218e9SMax Reitz 
39039c4218e9SMax Reitz         QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3904be4b67bcSMax Reitz     }
3905be4b67bcSMax Reitz 
3906be4b67bcSMax Reitz     if (bs && bdrv_key_required(bs)) {
3907be4b67bcSMax Reitz         if (blk) {
390818e46a03SMarkus Armbruster             blk_unref(blk);
3909be4b67bcSMax Reitz         } else {
39109c4218e9SMax Reitz             QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3911be4b67bcSMax Reitz             bdrv_unref(bs);
3912be4b67bcSMax Reitz         }
39138ae8e904SKevin Wolf         error_setg(errp, "blockdev-add doesn't support encrypted devices");
39148ae8e904SKevin Wolf         goto fail;
39158ae8e904SKevin Wolf     }
39168ae8e904SKevin Wolf 
3917d26c9a15SKevin Wolf fail:
3918d26c9a15SKevin Wolf     qmp_output_visitor_cleanup(ov);
3919d26c9a15SKevin Wolf }
3920d26c9a15SKevin Wolf 
392181b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id,
392281b936aeSAlberto Garcia                         bool has_node_name, const char *node_name, Error **errp)
392381b936aeSAlberto Garcia {
392481b936aeSAlberto Garcia     AioContext *aio_context;
392581b936aeSAlberto Garcia     BlockBackend *blk;
392681b936aeSAlberto Garcia     BlockDriverState *bs;
392781b936aeSAlberto Garcia 
392881b936aeSAlberto Garcia     if (has_id && has_node_name) {
392981b936aeSAlberto Garcia         error_setg(errp, "Only one of id and node-name must be specified");
393081b936aeSAlberto Garcia         return;
393181b936aeSAlberto Garcia     } else if (!has_id && !has_node_name) {
393281b936aeSAlberto Garcia         error_setg(errp, "No block device specified");
393381b936aeSAlberto Garcia         return;
393481b936aeSAlberto Garcia     }
393581b936aeSAlberto Garcia 
393681b936aeSAlberto Garcia     if (has_id) {
393781b936aeSAlberto Garcia         blk = blk_by_name(id);
393881b936aeSAlberto Garcia         if (!blk) {
393981b936aeSAlberto Garcia             error_setg(errp, "Cannot find block backend %s", id);
394081b936aeSAlberto Garcia             return;
394181b936aeSAlberto Garcia         }
394281b936aeSAlberto Garcia         if (blk_get_refcnt(blk) > 1) {
394381b936aeSAlberto Garcia             error_setg(errp, "Block backend %s is in use", id);
394481b936aeSAlberto Garcia             return;
394581b936aeSAlberto Garcia         }
394681b936aeSAlberto Garcia         bs = blk_bs(blk);
394781b936aeSAlberto Garcia         aio_context = blk_get_aio_context(blk);
394881b936aeSAlberto Garcia     } else {
394981b936aeSAlberto Garcia         bs = bdrv_find_node(node_name);
395081b936aeSAlberto Garcia         if (!bs) {
395181b936aeSAlberto Garcia             error_setg(errp, "Cannot find node %s", node_name);
395281b936aeSAlberto Garcia             return;
395381b936aeSAlberto Garcia         }
395481b936aeSAlberto Garcia         blk = bs->blk;
395581b936aeSAlberto Garcia         if (blk) {
395681b936aeSAlberto Garcia             error_setg(errp, "Node %s is in use by %s",
395781b936aeSAlberto Garcia                        node_name, blk_name(blk));
395881b936aeSAlberto Garcia             return;
395981b936aeSAlberto Garcia         }
396081b936aeSAlberto Garcia         aio_context = bdrv_get_aio_context(bs);
396181b936aeSAlberto Garcia     }
396281b936aeSAlberto Garcia 
396381b936aeSAlberto Garcia     aio_context_acquire(aio_context);
396481b936aeSAlberto Garcia 
396581b936aeSAlberto Garcia     if (bs) {
396681b936aeSAlberto Garcia         if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
396781b936aeSAlberto Garcia             goto out;
396881b936aeSAlberto Garcia         }
396981b936aeSAlberto Garcia 
39709c4218e9SMax Reitz         if (!blk && !bs->monitor_list.tqe_prev) {
39719c4218e9SMax Reitz             error_setg(errp, "Node %s is not owned by the monitor",
39729c4218e9SMax Reitz                        bs->node_name);
39739c4218e9SMax Reitz             goto out;
39749c4218e9SMax Reitz         }
39759c4218e9SMax Reitz 
39769c4218e9SMax Reitz         if (bs->refcnt > 1) {
397781b936aeSAlberto Garcia             error_setg(errp, "Block device %s is in use",
397881b936aeSAlberto Garcia                        bdrv_get_device_or_node_name(bs));
397981b936aeSAlberto Garcia             goto out;
398081b936aeSAlberto Garcia         }
398181b936aeSAlberto Garcia     }
398281b936aeSAlberto Garcia 
398381b936aeSAlberto Garcia     if (blk) {
398481b936aeSAlberto Garcia         blk_unref(blk);
398581b936aeSAlberto Garcia     } else {
39869c4218e9SMax Reitz         QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
398781b936aeSAlberto Garcia         bdrv_unref(bs);
398881b936aeSAlberto Garcia     }
398981b936aeSAlberto Garcia 
399081b936aeSAlberto Garcia out:
399181b936aeSAlberto Garcia     aio_context_release(aio_context);
399281b936aeSAlberto Garcia }
399381b936aeSAlberto Garcia 
3994fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3995fb5458cdSStefan Hajnoczi {
3996fea68bb6SMarkus Armbruster     BlockJobInfoList *head = NULL, **p_next = &head;
3997fea68bb6SMarkus Armbruster     BlockDriverState *bs;
3998fea68bb6SMarkus Armbruster 
3999fea68bb6SMarkus Armbruster     for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
400069691e72SStefan Hajnoczi         AioContext *aio_context = bdrv_get_aio_context(bs);
400169691e72SStefan Hajnoczi 
400269691e72SStefan Hajnoczi         aio_context_acquire(aio_context);
400369691e72SStefan Hajnoczi 
4004fea68bb6SMarkus Armbruster         if (bs->job) {
4005fea68bb6SMarkus Armbruster             BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
4006fea68bb6SMarkus Armbruster             elem->value = block_job_query(bs->job);
4007fea68bb6SMarkus Armbruster             *p_next = elem;
4008fea68bb6SMarkus Armbruster             p_next = &elem->next;
4009fea68bb6SMarkus Armbruster         }
401069691e72SStefan Hajnoczi 
401169691e72SStefan Hajnoczi         aio_context_release(aio_context);
4012fea68bb6SMarkus Armbruster     }
4013fea68bb6SMarkus Armbruster 
4014fea68bb6SMarkus Armbruster     return head;
4015fb5458cdSStefan Hajnoczi }
40164d454574SPaolo Bonzini 
40170006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = {
40184d454574SPaolo Bonzini     .name = "drive",
40190006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
40204d454574SPaolo Bonzini     .desc = {
40214d454574SPaolo Bonzini         {
40224d454574SPaolo Bonzini             .name = "snapshot",
40234d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
40244d454574SPaolo Bonzini             .help = "enable/disable snapshot mode",
40254d454574SPaolo Bonzini         },{
4026a9384affSPaolo Bonzini             .name = "discard",
4027a9384affSPaolo Bonzini             .type = QEMU_OPT_STRING,
4028a9384affSPaolo Bonzini             .help = "discard operation (ignore/off, unmap/on)",
4029a9384affSPaolo Bonzini         },{
40304d454574SPaolo Bonzini             .name = "aio",
40314d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40324d454574SPaolo Bonzini             .help = "host AIO implementation (threads, native)",
40334d454574SPaolo Bonzini         },{
40344d454574SPaolo Bonzini             .name = "format",
40354d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40364d454574SPaolo Bonzini             .help = "disk format (raw, qcow2, ...)",
40374d454574SPaolo Bonzini         },{
40384d454574SPaolo Bonzini             .name = "rerror",
40394d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40404d454574SPaolo Bonzini             .help = "read error action",
40414d454574SPaolo Bonzini         },{
40424d454574SPaolo Bonzini             .name = "werror",
40434d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40444d454574SPaolo Bonzini             .help = "write error action",
40454d454574SPaolo Bonzini         },{
40460f227a94SKevin Wolf             .name = "read-only",
40474d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
40484d454574SPaolo Bonzini             .help = "open drive file as read-only",
40494d454574SPaolo Bonzini         },{
405057975222SKevin Wolf             .name = "throttling.iops-total",
40514d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40524d454574SPaolo Bonzini             .help = "limit total I/O operations per second",
40534d454574SPaolo Bonzini         },{
405457975222SKevin Wolf             .name = "throttling.iops-read",
40554d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40564d454574SPaolo Bonzini             .help = "limit read operations per second",
40574d454574SPaolo Bonzini         },{
405857975222SKevin Wolf             .name = "throttling.iops-write",
40594d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40604d454574SPaolo Bonzini             .help = "limit write operations per second",
40614d454574SPaolo Bonzini         },{
406257975222SKevin Wolf             .name = "throttling.bps-total",
40634d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40644d454574SPaolo Bonzini             .help = "limit total bytes per second",
40654d454574SPaolo Bonzini         },{
406657975222SKevin Wolf             .name = "throttling.bps-read",
40674d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40684d454574SPaolo Bonzini             .help = "limit read bytes per second",
40694d454574SPaolo Bonzini         },{
407057975222SKevin Wolf             .name = "throttling.bps-write",
40714d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40724d454574SPaolo Bonzini             .help = "limit write bytes per second",
40734d454574SPaolo Bonzini         },{
40743e9fab69SBenoît Canet             .name = "throttling.iops-total-max",
40753e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40763e9fab69SBenoît Canet             .help = "I/O operations burst",
40773e9fab69SBenoît Canet         },{
40783e9fab69SBenoît Canet             .name = "throttling.iops-read-max",
40793e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40803e9fab69SBenoît Canet             .help = "I/O operations read burst",
40813e9fab69SBenoît Canet         },{
40823e9fab69SBenoît Canet             .name = "throttling.iops-write-max",
40833e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40843e9fab69SBenoît Canet             .help = "I/O operations write burst",
40853e9fab69SBenoît Canet         },{
40863e9fab69SBenoît Canet             .name = "throttling.bps-total-max",
40873e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40883e9fab69SBenoît Canet             .help = "total bytes burst",
40893e9fab69SBenoît Canet         },{
40903e9fab69SBenoît Canet             .name = "throttling.bps-read-max",
40913e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40923e9fab69SBenoît Canet             .help = "total bytes read burst",
40933e9fab69SBenoît Canet         },{
40943e9fab69SBenoît Canet             .name = "throttling.bps-write-max",
40953e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40963e9fab69SBenoît Canet             .help = "total bytes write burst",
40973e9fab69SBenoît Canet         },{
40982024c1dfSBenoît Canet             .name = "throttling.iops-size",
40992024c1dfSBenoît Canet             .type = QEMU_OPT_NUMBER,
41002024c1dfSBenoît Canet             .help = "when limiting by iops max size of an I/O in bytes",
41012024c1dfSBenoît Canet         },{
410276f4afb4SAlberto Garcia             .name = "throttling.group",
410376f4afb4SAlberto Garcia             .type = QEMU_OPT_STRING,
410476f4afb4SAlberto Garcia             .help = "name of the block throttling group",
410576f4afb4SAlberto Garcia         },{
41064d454574SPaolo Bonzini             .name = "copy-on-read",
41074d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
41084d454574SPaolo Bonzini             .help = "copy read data from backing file into image file",
4109465bee1dSPeter Lieven         },{
4110465bee1dSPeter Lieven             .name = "detect-zeroes",
4111465bee1dSPeter Lieven             .type = QEMU_OPT_STRING,
4112465bee1dSPeter Lieven             .help = "try to optimize zero writes (off, on, unmap)",
4113362e9299SAlberto Garcia         },{
4114362e9299SAlberto Garcia             .name = "stats-account-invalid",
4115362e9299SAlberto Garcia             .type = QEMU_OPT_BOOL,
4116362e9299SAlberto Garcia             .help = "whether to account for invalid I/O operations "
4117362e9299SAlberto Garcia                     "in the statistics",
4118362e9299SAlberto Garcia         },{
4119362e9299SAlberto Garcia             .name = "stats-account-failed",
4120362e9299SAlberto Garcia             .type = QEMU_OPT_BOOL,
4121362e9299SAlberto Garcia             .help = "whether to account for failed I/O operations "
4122362e9299SAlberto Garcia                     "in the statistics",
41234d454574SPaolo Bonzini         },
41244d454574SPaolo Bonzini         { /* end of list */ }
41254d454574SPaolo Bonzini     },
41264d454574SPaolo Bonzini };
41270006383eSKevin Wolf 
4128bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = {
4129bd745e23SMax Reitz     .name = "root-bds",
4130bd745e23SMax Reitz     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4131bd745e23SMax Reitz     .desc = {
4132bd745e23SMax Reitz         {
4133bd745e23SMax Reitz             .name = "discard",
4134bd745e23SMax Reitz             .type = QEMU_OPT_STRING,
4135bd745e23SMax Reitz             .help = "discard operation (ignore/off, unmap/on)",
4136bd745e23SMax Reitz         },{
4137bd745e23SMax Reitz             .name = "aio",
4138bd745e23SMax Reitz             .type = QEMU_OPT_STRING,
4139bd745e23SMax Reitz             .help = "host AIO implementation (threads, native)",
4140bd745e23SMax Reitz         },{
4141bd745e23SMax Reitz             .name = "read-only",
4142bd745e23SMax Reitz             .type = QEMU_OPT_BOOL,
4143bd745e23SMax Reitz             .help = "open drive file as read-only",
4144bd745e23SMax Reitz         },{
4145bd745e23SMax Reitz             .name = "copy-on-read",
4146bd745e23SMax Reitz             .type = QEMU_OPT_BOOL,
4147bd745e23SMax Reitz             .help = "copy read data from backing file into image file",
4148bd745e23SMax Reitz         },{
4149bd745e23SMax Reitz             .name = "detect-zeroes",
4150bd745e23SMax Reitz             .type = QEMU_OPT_STRING,
4151bd745e23SMax Reitz             .help = "try to optimize zero writes (off, on, unmap)",
4152bd745e23SMax Reitz         },
4153bd745e23SMax Reitz         { /* end of list */ }
4154bd745e23SMax Reitz     },
4155bd745e23SMax Reitz };
4156bd745e23SMax Reitz 
41570006383eSKevin Wolf QemuOptsList qemu_drive_opts = {
41580006383eSKevin Wolf     .name = "drive",
41590006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
41600006383eSKevin Wolf     .desc = {
4161492fdc6fSKevin Wolf         /*
4162492fdc6fSKevin Wolf          * no elements => accept any params
4163492fdc6fSKevin Wolf          * validation will happen later
4164492fdc6fSKevin Wolf          */
41650006383eSKevin Wolf         { /* end of list */ }
41660006383eSKevin Wolf     },
41670006383eSKevin Wolf };
4168