xref: /openbmc/qemu/blockdev.c (revision 51e72bc1dd6ace6e91d675f41a1f09bd00ab8043)
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 
613e4342ce5SMax Reitz         blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
614e4342ce5SMax Reitz                            errp);
615e4342ce5SMax Reitz         if (!blk) {
616e4342ce5SMax Reitz             goto err_no_bs_opts;
617e4342ce5SMax Reitz         }
618e4342ce5SMax Reitz         bs = blk_bs(blk);
6190006383eSKevin Wolf 
620e4342ce5SMax Reitz         bs->detect_zeroes = detect_zeroes;
621e4342ce5SMax Reitz 
622e4342ce5SMax Reitz         /* disk I/O throttling */
623e4342ce5SMax Reitz         if (throttle_enabled(&cfg)) {
62476f4afb4SAlberto Garcia             if (!throttling_group) {
62576f4afb4SAlberto Garcia                 throttling_group = blk_name(blk);
62676f4afb4SAlberto Garcia             }
62776f4afb4SAlberto Garcia             bdrv_io_limits_enable(bs, throttling_group);
628e4342ce5SMax Reitz             bdrv_set_io_limits(bs, &cfg);
629666daa68SMarkus Armbruster         }
630666daa68SMarkus Armbruster 
631a0f1eab1SMarkus Armbruster         if (bdrv_key_required(bs)) {
632666daa68SMarkus Armbruster             autostart = 0;
633a0f1eab1SMarkus Armbruster         }
634362e9299SAlberto Garcia 
635362e9299SAlberto Garcia         block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
6362be5506fSAlberto Garcia 
63740119effSAlberto Garcia         if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
6382be5506fSAlberto Garcia             blk_unref(blk);
6392be5506fSAlberto Garcia             blk = NULL;
6402be5506fSAlberto Garcia             goto err_no_bs_opts;
6412be5506fSAlberto Garcia         }
6422be5506fSAlberto Garcia     }
6435ec18f8cSMax Reitz 
6445ec18f8cSMax Reitz     blk_set_on_error(blk, on_read_error, on_write_error);
6450006383eSKevin Wolf 
646e4342ce5SMax Reitz err_no_bs_opts:
6470006383eSKevin Wolf     qemu_opts_del(opts);
64840119effSAlberto Garcia     QDECREF(interval_dict);
64940119effSAlberto Garcia     QDECREF(interval_list);
65018e46a03SMarkus Armbruster     return blk;
651a9ae2bffSMarkus Armbruster 
652ec9c10d2SStefan Hajnoczi early_err:
653ec9c10d2SStefan Hajnoczi     qemu_opts_del(opts);
65440119effSAlberto Garcia     QDECREF(interval_dict);
65540119effSAlberto Garcia     QDECREF(interval_list);
6566376f952SMarkus Armbruster err_no_opts:
6576376f952SMarkus Armbruster     QDECREF(bs_opts);
658a9ae2bffSMarkus Armbruster     return NULL;
659666daa68SMarkus Armbruster }
660666daa68SMarkus Armbruster 
661bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts;
662bd745e23SMax Reitz 
663bd745e23SMax Reitz /* Takes the ownership of bs_opts */
664bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
665bd745e23SMax Reitz {
666bd745e23SMax Reitz     BlockDriverState *bs;
667bd745e23SMax Reitz     QemuOpts *opts;
668bd745e23SMax Reitz     Error *local_error = NULL;
669bd745e23SMax Reitz     BlockdevDetectZeroesOptions detect_zeroes;
670bd745e23SMax Reitz     int ret;
671bd745e23SMax Reitz     int bdrv_flags = 0;
672bd745e23SMax Reitz 
673bd745e23SMax Reitz     opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
674bd745e23SMax Reitz     if (!opts) {
675bd745e23SMax Reitz         goto fail;
676bd745e23SMax Reitz     }
677bd745e23SMax Reitz 
678bd745e23SMax Reitz     qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
679bd745e23SMax Reitz     if (local_error) {
680bd745e23SMax Reitz         error_propagate(errp, local_error);
681bd745e23SMax Reitz         goto fail;
682bd745e23SMax Reitz     }
683bd745e23SMax Reitz 
684bd745e23SMax Reitz     extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
685bd745e23SMax Reitz                                     &detect_zeroes, &local_error);
686bd745e23SMax Reitz     if (local_error) {
687bd745e23SMax Reitz         error_propagate(errp, local_error);
688bd745e23SMax Reitz         goto fail;
689bd745e23SMax Reitz     }
690bd745e23SMax Reitz 
691bd745e23SMax Reitz     bs = NULL;
692bd745e23SMax Reitz     ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp);
693bd745e23SMax Reitz     if (ret < 0) {
694bd745e23SMax Reitz         goto fail_no_bs_opts;
695bd745e23SMax Reitz     }
696bd745e23SMax Reitz 
697bd745e23SMax Reitz     bs->detect_zeroes = detect_zeroes;
698bd745e23SMax Reitz 
699bd745e23SMax Reitz fail_no_bs_opts:
700bd745e23SMax Reitz     qemu_opts_del(opts);
701bd745e23SMax Reitz     return bs;
702bd745e23SMax Reitz 
703bd745e23SMax Reitz fail:
704bd745e23SMax Reitz     qemu_opts_del(opts);
705bd745e23SMax Reitz     QDECREF(bs_opts);
706bd745e23SMax Reitz     return NULL;
707bd745e23SMax Reitz }
708bd745e23SMax Reitz 
7099c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void)
7109c4218e9SMax Reitz {
7119c4218e9SMax Reitz     BlockDriverState *bs, *next_bs;
7129c4218e9SMax Reitz 
7139c4218e9SMax Reitz     QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
7149c4218e9SMax Reitz         AioContext *ctx = bdrv_get_aio_context(bs);
7159c4218e9SMax Reitz 
7169c4218e9SMax Reitz         aio_context_acquire(ctx);
7179c4218e9SMax Reitz         bdrv_unref(bs);
7189c4218e9SMax Reitz         aio_context_release(ctx);
7199c4218e9SMax Reitz     }
7209c4218e9SMax Reitz }
7219c4218e9SMax Reitz 
7225abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
7235abbf0eeSKevin Wolf                             Error **errp)
72457975222SKevin Wolf {
72557975222SKevin Wolf     const char *value;
72657975222SKevin Wolf 
72757975222SKevin Wolf     value = qemu_opt_get(opts, from);
72857975222SKevin Wolf     if (value) {
7295abbf0eeSKevin Wolf         if (qemu_opt_find(opts, to)) {
7305abbf0eeSKevin Wolf             error_setg(errp, "'%s' and its alias '%s' can't be used at the "
7315abbf0eeSKevin Wolf                        "same time", to, from);
7325abbf0eeSKevin Wolf             return;
7335abbf0eeSKevin Wolf         }
73420d6cd47SJun Li     }
73520d6cd47SJun Li 
73620d6cd47SJun Li     /* rename all items in opts */
73720d6cd47SJun Li     while ((value = qemu_opt_get(opts, from))) {
738f43e47dbSMarkus Armbruster         qemu_opt_set(opts, to, value, &error_abort);
73957975222SKevin Wolf         qemu_opt_unset(opts, from);
74057975222SKevin Wolf     }
74157975222SKevin Wolf }
74257975222SKevin Wolf 
74333cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = {
74433cb7dc8SKevin Wolf     .name = "drive",
74533cb7dc8SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
74633cb7dc8SKevin Wolf     .desc = {
74733cb7dc8SKevin Wolf         {
74887a899c5SKevin Wolf             .name = "bus",
74987a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
75087a899c5SKevin Wolf             .help = "bus number",
75187a899c5SKevin Wolf         },{
75287a899c5SKevin Wolf             .name = "unit",
75387a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
75487a899c5SKevin Wolf             .help = "unit number (i.e. lun for scsi)",
75587a899c5SKevin Wolf         },{
75687a899c5SKevin Wolf             .name = "index",
75787a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
75887a899c5SKevin Wolf             .help = "index number",
75987a899c5SKevin Wolf         },{
76033cb7dc8SKevin Wolf             .name = "media",
76133cb7dc8SKevin Wolf             .type = QEMU_OPT_STRING,
76233cb7dc8SKevin Wolf             .help = "media type (disk, cdrom)",
763593d464bSKevin Wolf         },{
764593d464bSKevin Wolf             .name = "if",
765593d464bSKevin Wolf             .type = QEMU_OPT_STRING,
766593d464bSKevin Wolf             .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
767b41a7338SKevin Wolf         },{
768b41a7338SKevin Wolf             .name = "cyls",
769b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
770b41a7338SKevin Wolf             .help = "number of cylinders (ide disk geometry)",
771b41a7338SKevin Wolf         },{
772b41a7338SKevin Wolf             .name = "heads",
773b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
774b41a7338SKevin Wolf             .help = "number of heads (ide disk geometry)",
775b41a7338SKevin Wolf         },{
776b41a7338SKevin Wolf             .name = "secs",
777b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
778b41a7338SKevin Wolf             .help = "number of sectors (ide disk geometry)",
779b41a7338SKevin Wolf         },{
780b41a7338SKevin Wolf             .name = "trans",
781b41a7338SKevin Wolf             .type = QEMU_OPT_STRING,
782b41a7338SKevin Wolf             .help = "chs translation (auto, lba, none)",
78326929298SKevin Wolf         },{
78426929298SKevin Wolf             .name = "boot",
78526929298SKevin Wolf             .type = QEMU_OPT_BOOL,
78626929298SKevin Wolf             .help = "(deprecated, ignored)",
787394c7d4dSKevin Wolf         },{
788394c7d4dSKevin Wolf             .name = "addr",
789394c7d4dSKevin Wolf             .type = QEMU_OPT_STRING,
790394c7d4dSKevin Wolf             .help = "pci address (virtio only)",
791d095b465SMax Reitz         },{
792bcf83158SKevin Wolf             .name = "serial",
793bcf83158SKevin Wolf             .type = QEMU_OPT_STRING,
794bcf83158SKevin Wolf             .help = "disk serial number",
795bcf83158SKevin Wolf         },{
796d095b465SMax Reitz             .name = "file",
797d095b465SMax Reitz             .type = QEMU_OPT_STRING,
798d095b465SMax Reitz             .help = "file name",
79933cb7dc8SKevin Wolf         },
8000ebd24e0SKevin Wolf 
8010ebd24e0SKevin Wolf         /* Options that are passed on, but have special semantics with -drive */
8020ebd24e0SKevin Wolf         {
8030ebd24e0SKevin Wolf             .name = "read-only",
8040ebd24e0SKevin Wolf             .type = QEMU_OPT_BOOL,
8050ebd24e0SKevin Wolf             .help = "open drive file as read-only",
8060ebd24e0SKevin Wolf         },{
807ee13ed1cSKevin Wolf             .name = "rerror",
808ee13ed1cSKevin Wolf             .type = QEMU_OPT_STRING,
809ee13ed1cSKevin Wolf             .help = "read error action",
810ee13ed1cSKevin Wolf         },{
811ee13ed1cSKevin Wolf             .name = "werror",
812ee13ed1cSKevin Wolf             .type = QEMU_OPT_STRING,
813ee13ed1cSKevin Wolf             .help = "write error action",
814ee13ed1cSKevin Wolf         },{
8150ebd24e0SKevin Wolf             .name = "copy-on-read",
8160ebd24e0SKevin Wolf             .type = QEMU_OPT_BOOL,
8170ebd24e0SKevin Wolf             .help = "copy read data from backing file into image file",
8180ebd24e0SKevin Wolf         },
8190ebd24e0SKevin Wolf 
82033cb7dc8SKevin Wolf         { /* end of list */ }
82133cb7dc8SKevin Wolf     },
82233cb7dc8SKevin Wolf };
82333cb7dc8SKevin Wolf 
82460e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
82557975222SKevin Wolf {
82629c4e2b5SKevin Wolf     const char *value;
82718e46a03SMarkus Armbruster     BlockBackend *blk;
82833cb7dc8SKevin Wolf     DriveInfo *dinfo = NULL;
829f298d071SKevin Wolf     QDict *bs_opts;
83033cb7dc8SKevin Wolf     QemuOpts *legacy_opts;
83133cb7dc8SKevin Wolf     DriveMediaType media = MEDIA_DISK;
832593d464bSKevin Wolf     BlockInterfaceType type;
833b41a7338SKevin Wolf     int cyls, heads, secs, translation;
83487a899c5SKevin Wolf     int max_devs, bus_id, unit_id, index;
835394c7d4dSKevin Wolf     const char *devaddr;
836ee13ed1cSKevin Wolf     const char *werror, *rerror;
837a7fdbcf0SFam Zheng     bool read_only = false;
838a7fdbcf0SFam Zheng     bool copy_on_read;
839bcf83158SKevin Wolf     const char *serial;
840d095b465SMax Reitz     const char *filename;
84133cb7dc8SKevin Wolf     Error *local_err = NULL;
842247147fbSKevin Wolf     int i;
84329c4e2b5SKevin Wolf 
84457975222SKevin Wolf     /* Change legacy command line options into QMP ones */
845247147fbSKevin Wolf     static const struct {
846247147fbSKevin Wolf         const char *from;
847247147fbSKevin Wolf         const char *to;
848247147fbSKevin Wolf     } opt_renames[] = {
849247147fbSKevin Wolf         { "iops",           "throttling.iops-total" },
850247147fbSKevin Wolf         { "iops_rd",        "throttling.iops-read" },
851247147fbSKevin Wolf         { "iops_wr",        "throttling.iops-write" },
85257975222SKevin Wolf 
853247147fbSKevin Wolf         { "bps",            "throttling.bps-total" },
854247147fbSKevin Wolf         { "bps_rd",         "throttling.bps-read" },
855247147fbSKevin Wolf         { "bps_wr",         "throttling.bps-write" },
85657975222SKevin Wolf 
857247147fbSKevin Wolf         { "iops_max",       "throttling.iops-total-max" },
858247147fbSKevin Wolf         { "iops_rd_max",    "throttling.iops-read-max" },
859247147fbSKevin Wolf         { "iops_wr_max",    "throttling.iops-write-max" },
8603e9fab69SBenoît Canet 
861247147fbSKevin Wolf         { "bps_max",        "throttling.bps-total-max" },
862247147fbSKevin Wolf         { "bps_rd_max",     "throttling.bps-read-max" },
863247147fbSKevin Wolf         { "bps_wr_max",     "throttling.bps-write-max" },
8643e9fab69SBenoît Canet 
865247147fbSKevin Wolf         { "iops_size",      "throttling.iops-size" },
8662024c1dfSBenoît Canet 
86776f4afb4SAlberto Garcia         { "group",          "throttling.group" },
86876f4afb4SAlberto Garcia 
869247147fbSKevin Wolf         { "readonly",       "read-only" },
870247147fbSKevin Wolf     };
871247147fbSKevin Wolf 
872247147fbSKevin Wolf     for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
8735abbf0eeSKevin Wolf         qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
8745abbf0eeSKevin Wolf                         &local_err);
8755abbf0eeSKevin Wolf         if (local_err) {
876565f65d2SMarkus Armbruster             error_report_err(local_err);
8775abbf0eeSKevin Wolf             return NULL;
8785abbf0eeSKevin Wolf         }
879247147fbSKevin Wolf     }
8800f227a94SKevin Wolf 
88129c4e2b5SKevin Wolf     value = qemu_opt_get(all_opts, "cache");
88229c4e2b5SKevin Wolf     if (value) {
88329c4e2b5SKevin Wolf         int flags = 0;
88429c4e2b5SKevin Wolf 
88529c4e2b5SKevin Wolf         if (bdrv_parse_cache_flags(value, &flags) != 0) {
88629c4e2b5SKevin Wolf             error_report("invalid cache option");
88729c4e2b5SKevin Wolf             return NULL;
88829c4e2b5SKevin Wolf         }
88929c4e2b5SKevin Wolf 
89029c4e2b5SKevin Wolf         /* Specific options take precedence */
89154861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
89254861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
893cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_CACHE_WB), &error_abort);
89429c4e2b5SKevin Wolf         }
89554861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
89654861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
897cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_NOCACHE), &error_abort);
89829c4e2b5SKevin Wolf         }
89954861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
90054861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
901cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_NO_FLUSH), &error_abort);
90229c4e2b5SKevin Wolf         }
90329c4e2b5SKevin Wolf         qemu_opt_unset(all_opts, "cache");
90429c4e2b5SKevin Wolf     }
90529c4e2b5SKevin Wolf 
906f298d071SKevin Wolf     /* Get a QDict for processing the options */
907f298d071SKevin Wolf     bs_opts = qdict_new();
908f298d071SKevin Wolf     qemu_opts_to_qdict(all_opts, bs_opts);
909f298d071SKevin Wolf 
91087ea75d5SPeter Crosthwaite     legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
91187ea75d5SPeter Crosthwaite                                    &error_abort);
91233cb7dc8SKevin Wolf     qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
91384d18f06SMarkus Armbruster     if (local_err) {
914565f65d2SMarkus Armbruster         error_report_err(local_err);
91533cb7dc8SKevin Wolf         goto fail;
91633cb7dc8SKevin Wolf     }
91733cb7dc8SKevin Wolf 
91826929298SKevin Wolf     /* Deprecated option boot=[on|off] */
91926929298SKevin Wolf     if (qemu_opt_get(legacy_opts, "boot") != NULL) {
92026929298SKevin Wolf         fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
92126929298SKevin Wolf                 "ignored. Future versions will reject this parameter. Please "
92226929298SKevin Wolf                 "update your scripts.\n");
92326929298SKevin Wolf     }
92426929298SKevin Wolf 
92533cb7dc8SKevin Wolf     /* Media type */
92633cb7dc8SKevin Wolf     value = qemu_opt_get(legacy_opts, "media");
92733cb7dc8SKevin Wolf     if (value) {
92833cb7dc8SKevin Wolf         if (!strcmp(value, "disk")) {
92933cb7dc8SKevin Wolf             media = MEDIA_DISK;
93033cb7dc8SKevin Wolf         } else if (!strcmp(value, "cdrom")) {
93133cb7dc8SKevin Wolf             media = MEDIA_CDROM;
932a7fdbcf0SFam Zheng             read_only = true;
93333cb7dc8SKevin Wolf         } else {
93433cb7dc8SKevin Wolf             error_report("'%s' invalid media", value);
93533cb7dc8SKevin Wolf             goto fail;
93633cb7dc8SKevin Wolf         }
93733cb7dc8SKevin Wolf     }
93833cb7dc8SKevin Wolf 
9390ebd24e0SKevin Wolf     /* copy-on-read is disabled with a warning for read-only devices */
940a7fdbcf0SFam Zheng     read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
9410ebd24e0SKevin Wolf     copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
9420ebd24e0SKevin Wolf 
9430ebd24e0SKevin Wolf     if (read_only && copy_on_read) {
9440ebd24e0SKevin Wolf         error_report("warning: disabling copy-on-read on read-only drive");
9450ebd24e0SKevin Wolf         copy_on_read = false;
9460ebd24e0SKevin Wolf     }
9470ebd24e0SKevin Wolf 
9480ebd24e0SKevin Wolf     qdict_put(bs_opts, "read-only",
9490ebd24e0SKevin Wolf               qstring_from_str(read_only ? "on" : "off"));
9500ebd24e0SKevin Wolf     qdict_put(bs_opts, "copy-on-read",
9510ebd24e0SKevin Wolf               qstring_from_str(copy_on_read ? "on" :"off"));
9520ebd24e0SKevin Wolf 
953593d464bSKevin Wolf     /* Controller type */
954593d464bSKevin Wolf     value = qemu_opt_get(legacy_opts, "if");
955593d464bSKevin Wolf     if (value) {
956593d464bSKevin Wolf         for (type = 0;
957593d464bSKevin Wolf              type < IF_COUNT && strcmp(value, if_name[type]);
958593d464bSKevin Wolf              type++) {
959593d464bSKevin Wolf         }
960593d464bSKevin Wolf         if (type == IF_COUNT) {
961593d464bSKevin Wolf             error_report("unsupported bus type '%s'", value);
962593d464bSKevin Wolf             goto fail;
963593d464bSKevin Wolf         }
964593d464bSKevin Wolf     } else {
965593d464bSKevin Wolf         type = block_default_type;
966593d464bSKevin Wolf     }
967593d464bSKevin Wolf 
968b41a7338SKevin Wolf     /* Geometry */
969b41a7338SKevin Wolf     cyls  = qemu_opt_get_number(legacy_opts, "cyls", 0);
970b41a7338SKevin Wolf     heads = qemu_opt_get_number(legacy_opts, "heads", 0);
971b41a7338SKevin Wolf     secs  = qemu_opt_get_number(legacy_opts, "secs", 0);
972b41a7338SKevin Wolf 
973b41a7338SKevin Wolf     if (cyls || heads || secs) {
974b41a7338SKevin Wolf         if (cyls < 1) {
975b41a7338SKevin Wolf             error_report("invalid physical cyls number");
976b41a7338SKevin Wolf             goto fail;
977b41a7338SKevin Wolf         }
978b41a7338SKevin Wolf         if (heads < 1) {
979b41a7338SKevin Wolf             error_report("invalid physical heads number");
980b41a7338SKevin Wolf             goto fail;
981b41a7338SKevin Wolf         }
982b41a7338SKevin Wolf         if (secs < 1) {
983b41a7338SKevin Wolf             error_report("invalid physical secs number");
984b41a7338SKevin Wolf             goto fail;
985b41a7338SKevin Wolf         }
986b41a7338SKevin Wolf     }
987b41a7338SKevin Wolf 
988b41a7338SKevin Wolf     translation = BIOS_ATA_TRANSLATION_AUTO;
989b41a7338SKevin Wolf     value = qemu_opt_get(legacy_opts, "trans");
990b41a7338SKevin Wolf     if (value != NULL) {
991b41a7338SKevin Wolf         if (!cyls) {
992b41a7338SKevin Wolf             error_report("'%s' trans must be used with cyls, heads and secs",
993b41a7338SKevin Wolf                          value);
994b41a7338SKevin Wolf             goto fail;
995b41a7338SKevin Wolf         }
996b41a7338SKevin Wolf         if (!strcmp(value, "none")) {
997b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_NONE;
998b41a7338SKevin Wolf         } else if (!strcmp(value, "lba")) {
999b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_LBA;
1000f31c41ffSPaolo Bonzini         } else if (!strcmp(value, "large")) {
1001f31c41ffSPaolo Bonzini             translation = BIOS_ATA_TRANSLATION_LARGE;
1002f31c41ffSPaolo Bonzini         } else if (!strcmp(value, "rechs")) {
1003f31c41ffSPaolo Bonzini             translation = BIOS_ATA_TRANSLATION_RECHS;
1004b41a7338SKevin Wolf         } else if (!strcmp(value, "auto")) {
1005b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_AUTO;
1006b41a7338SKevin Wolf         } else {
1007b41a7338SKevin Wolf             error_report("'%s' invalid translation type", value);
1008b41a7338SKevin Wolf             goto fail;
1009b41a7338SKevin Wolf         }
1010b41a7338SKevin Wolf     }
1011b41a7338SKevin Wolf 
1012b41a7338SKevin Wolf     if (media == MEDIA_CDROM) {
1013b41a7338SKevin Wolf         if (cyls || secs || heads) {
1014b41a7338SKevin Wolf             error_report("CHS can't be set with media=cdrom");
1015b41a7338SKevin Wolf             goto fail;
1016b41a7338SKevin Wolf         }
1017b41a7338SKevin Wolf     }
1018b41a7338SKevin Wolf 
101987a899c5SKevin Wolf     /* Device address specified by bus/unit or index.
102087a899c5SKevin Wolf      * If none was specified, try to find the first free one. */
102187a899c5SKevin Wolf     bus_id  = qemu_opt_get_number(legacy_opts, "bus", 0);
102287a899c5SKevin Wolf     unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
102387a899c5SKevin Wolf     index   = qemu_opt_get_number(legacy_opts, "index", -1);
102487a899c5SKevin Wolf 
102587a899c5SKevin Wolf     max_devs = if_max_devs[type];
102687a899c5SKevin Wolf 
102787a899c5SKevin Wolf     if (index != -1) {
102887a899c5SKevin Wolf         if (bus_id != 0 || unit_id != -1) {
102987a899c5SKevin Wolf             error_report("index cannot be used with bus and unit");
103087a899c5SKevin Wolf             goto fail;
103187a899c5SKevin Wolf         }
103287a899c5SKevin Wolf         bus_id = drive_index_to_bus_id(type, index);
103387a899c5SKevin Wolf         unit_id = drive_index_to_unit_id(type, index);
103487a899c5SKevin Wolf     }
103587a899c5SKevin Wolf 
103687a899c5SKevin Wolf     if (unit_id == -1) {
103787a899c5SKevin Wolf        unit_id = 0;
103887a899c5SKevin Wolf        while (drive_get(type, bus_id, unit_id) != NULL) {
103987a899c5SKevin Wolf            unit_id++;
104087a899c5SKevin Wolf            if (max_devs && unit_id >= max_devs) {
104187a899c5SKevin Wolf                unit_id -= max_devs;
104287a899c5SKevin Wolf                bus_id++;
104387a899c5SKevin Wolf            }
104487a899c5SKevin Wolf        }
104587a899c5SKevin Wolf     }
104687a899c5SKevin Wolf 
104787a899c5SKevin Wolf     if (max_devs && unit_id >= max_devs) {
104887a899c5SKevin Wolf         error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
104987a899c5SKevin Wolf         goto fail;
105087a899c5SKevin Wolf     }
105187a899c5SKevin Wolf 
105287a899c5SKevin Wolf     if (drive_get(type, bus_id, unit_id) != NULL) {
105387a899c5SKevin Wolf         error_report("drive with bus=%d, unit=%d (index=%d) exists",
105487a899c5SKevin Wolf                      bus_id, unit_id, index);
105587a899c5SKevin Wolf         goto fail;
105687a899c5SKevin Wolf     }
105787a899c5SKevin Wolf 
1058bcf83158SKevin Wolf     /* Serial number */
1059bcf83158SKevin Wolf     serial = qemu_opt_get(legacy_opts, "serial");
1060bcf83158SKevin Wolf 
106187a899c5SKevin Wolf     /* no id supplied -> create one */
106287a899c5SKevin Wolf     if (qemu_opts_id(all_opts) == NULL) {
106387a899c5SKevin Wolf         char *new_id;
106487a899c5SKevin Wolf         const char *mediastr = "";
106587a899c5SKevin Wolf         if (type == IF_IDE || type == IF_SCSI) {
106687a899c5SKevin Wolf             mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
106787a899c5SKevin Wolf         }
106887a899c5SKevin Wolf         if (max_devs) {
106987a899c5SKevin Wolf             new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
107087a899c5SKevin Wolf                                      mediastr, unit_id);
107187a899c5SKevin Wolf         } else {
107287a899c5SKevin Wolf             new_id = g_strdup_printf("%s%s%i", if_name[type],
107387a899c5SKevin Wolf                                      mediastr, unit_id);
107487a899c5SKevin Wolf         }
107587a899c5SKevin Wolf         qdict_put(bs_opts, "id", qstring_from_str(new_id));
107687a899c5SKevin Wolf         g_free(new_id);
107787a899c5SKevin Wolf     }
107887a899c5SKevin Wolf 
1079394c7d4dSKevin Wolf     /* Add virtio block device */
1080394c7d4dSKevin Wolf     devaddr = qemu_opt_get(legacy_opts, "addr");
1081394c7d4dSKevin Wolf     if (devaddr && type != IF_VIRTIO) {
1082394c7d4dSKevin Wolf         error_report("addr is not supported by this bus type");
1083394c7d4dSKevin Wolf         goto fail;
1084394c7d4dSKevin Wolf     }
1085394c7d4dSKevin Wolf 
1086394c7d4dSKevin Wolf     if (type == IF_VIRTIO) {
1087394c7d4dSKevin Wolf         QemuOpts *devopts;
108887ea75d5SPeter Crosthwaite         devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
108987ea75d5SPeter Crosthwaite                                    &error_abort);
1090394c7d4dSKevin Wolf         if (arch_type == QEMU_ARCH_S390X) {
10911f68f1d3SAlexander Graf             qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
1092394c7d4dSKevin Wolf         } else {
1093f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
1094394c7d4dSKevin Wolf         }
1095f43e47dbSMarkus Armbruster         qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1096f43e47dbSMarkus Armbruster                      &error_abort);
1097394c7d4dSKevin Wolf         if (devaddr) {
1098f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "addr", devaddr, &error_abort);
1099394c7d4dSKevin Wolf         }
1100394c7d4dSKevin Wolf     }
1101394c7d4dSKevin Wolf 
1102d095b465SMax Reitz     filename = qemu_opt_get(legacy_opts, "file");
1103d095b465SMax Reitz 
1104ee13ed1cSKevin Wolf     /* Check werror/rerror compatibility with if=... */
1105ee13ed1cSKevin Wolf     werror = qemu_opt_get(legacy_opts, "werror");
1106ee13ed1cSKevin Wolf     if (werror != NULL) {
1107ee13ed1cSKevin Wolf         if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1108ee13ed1cSKevin Wolf             type != IF_NONE) {
1109ee13ed1cSKevin Wolf             error_report("werror is not supported by this bus type");
1110ee13ed1cSKevin Wolf             goto fail;
1111ee13ed1cSKevin Wolf         }
1112ee13ed1cSKevin Wolf         qdict_put(bs_opts, "werror", qstring_from_str(werror));
1113ee13ed1cSKevin Wolf     }
1114ee13ed1cSKevin Wolf 
1115ee13ed1cSKevin Wolf     rerror = qemu_opt_get(legacy_opts, "rerror");
1116ee13ed1cSKevin Wolf     if (rerror != NULL) {
1117ee13ed1cSKevin Wolf         if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1118ee13ed1cSKevin Wolf             type != IF_NONE) {
1119ee13ed1cSKevin Wolf             error_report("rerror is not supported by this bus type");
1120ee13ed1cSKevin Wolf             goto fail;
1121ee13ed1cSKevin Wolf         }
1122ee13ed1cSKevin Wolf         qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1123ee13ed1cSKevin Wolf     }
1124ee13ed1cSKevin Wolf 
11252d246f01SKevin Wolf     /* Actual block device init: Functionality shared with blockdev-add */
112618e46a03SMarkus Armbruster     blk = blockdev_init(filename, bs_opts, &local_err);
11273cb0e25cSMarkus Armbruster     bs_opts = NULL;
112818e46a03SMarkus Armbruster     if (!blk) {
112984d18f06SMarkus Armbruster         if (local_err) {
1130565f65d2SMarkus Armbruster             error_report_err(local_err);
1131b681072dSKevin Wolf         }
11322d246f01SKevin Wolf         goto fail;
1133b681072dSKevin Wolf     } else {
113484d18f06SMarkus Armbruster         assert(!local_err);
11352d246f01SKevin Wolf     }
11362d246f01SKevin Wolf 
113726f8b3a8SMarkus Armbruster     /* Create legacy DriveInfo */
113826f8b3a8SMarkus Armbruster     dinfo = g_malloc0(sizeof(*dinfo));
1139f298d071SKevin Wolf     dinfo->opts = all_opts;
11402d246f01SKevin Wolf 
1141b41a7338SKevin Wolf     dinfo->cyls = cyls;
1142b41a7338SKevin Wolf     dinfo->heads = heads;
1143b41a7338SKevin Wolf     dinfo->secs = secs;
1144b41a7338SKevin Wolf     dinfo->trans = translation;
1145b41a7338SKevin Wolf 
1146ee13ed1cSKevin Wolf     dinfo->type = type;
114787a899c5SKevin Wolf     dinfo->bus = bus_id;
114887a899c5SKevin Wolf     dinfo->unit = unit_id;
1149394c7d4dSKevin Wolf     dinfo->devaddr = devaddr;
1150bcf83158SKevin Wolf     dinfo->serial = g_strdup(serial);
1151bcf83158SKevin Wolf 
115226f8b3a8SMarkus Armbruster     blk_set_legacy_dinfo(blk, dinfo);
115326f8b3a8SMarkus Armbruster 
1154e34ef046SKevin Wolf     switch(type) {
1155e34ef046SKevin Wolf     case IF_IDE:
1156e34ef046SKevin Wolf     case IF_SCSI:
1157e34ef046SKevin Wolf     case IF_XEN:
1158e34ef046SKevin Wolf     case IF_NONE:
1159e34ef046SKevin Wolf         dinfo->media_cd = media == MEDIA_CDROM;
1160e34ef046SKevin Wolf         break;
1161e34ef046SKevin Wolf     default:
1162e34ef046SKevin Wolf         break;
1163e34ef046SKevin Wolf     }
1164e34ef046SKevin Wolf 
11652d246f01SKevin Wolf fail:
116633cb7dc8SKevin Wolf     qemu_opts_del(legacy_opts);
11673cb0e25cSMarkus Armbruster     QDECREF(bs_opts);
11682d246f01SKevin Wolf     return dinfo;
116957975222SKevin Wolf }
117057975222SKevin Wolf 
11713e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict)
1172666daa68SMarkus Armbruster {
1173666daa68SMarkus Armbruster     const char *device = qdict_get_str(qdict, "device");
1174a0e8544cSFam Zheng     BlockBackend *blk;
11752d3735d3SStefan Hajnoczi     int ret;
11762d3735d3SStefan Hajnoczi 
1177e8877497SStefan Hajnoczi     if (!strcmp(device, "all")) {
1178e8877497SStefan Hajnoczi         ret = bdrv_commit_all();
1179e8877497SStefan Hajnoczi     } else {
118084aa0140SStefan Hajnoczi         BlockDriverState *bs;
118184aa0140SStefan Hajnoczi         AioContext *aio_context;
118284aa0140SStefan Hajnoczi 
1183a0e8544cSFam Zheng         blk = blk_by_name(device);
1184a0e8544cSFam Zheng         if (!blk) {
118558513bdeSJeff Cody             monitor_printf(mon, "Device '%s' not found\n", device);
1186ac59eb95SMarkus Armbruster             return;
11876ab4b5abSMarkus Armbruster         }
11885433c24fSMax Reitz         if (!blk_is_available(blk)) {
11895433c24fSMax Reitz             monitor_printf(mon, "Device '%s' has no medium\n", device);
11905433c24fSMax Reitz             return;
11915433c24fSMax Reitz         }
119284aa0140SStefan Hajnoczi 
119384aa0140SStefan Hajnoczi         bs = blk_bs(blk);
119484aa0140SStefan Hajnoczi         aio_context = bdrv_get_aio_context(bs);
119584aa0140SStefan Hajnoczi         aio_context_acquire(aio_context);
119684aa0140SStefan Hajnoczi 
119784aa0140SStefan Hajnoczi         ret = bdrv_commit(bs);
119884aa0140SStefan Hajnoczi 
119984aa0140SStefan Hajnoczi         aio_context_release(aio_context);
12002d3735d3SStefan Hajnoczi     }
120158513bdeSJeff Cody     if (ret < 0) {
120258513bdeSJeff Cody         monitor_printf(mon, "'commit' error for '%s': %s\n", device,
120358513bdeSJeff Cody                        strerror(-ret));
1204666daa68SMarkus Armbruster     }
1205666daa68SMarkus Armbruster }
1206666daa68SMarkus Armbruster 
12076a8f9661SEric Blake static void blockdev_do_action(TransactionActionKind type, void *data,
12086a8f9661SEric Blake                                Error **errp)
12096cc2a415SPaolo Bonzini {
1210c8a83e85SKevin Wolf     TransactionAction action;
1211c8a83e85SKevin Wolf     TransactionActionList list;
12126cc2a415SPaolo Bonzini 
12136a8f9661SEric Blake     action.type = type;
12146a8f9661SEric Blake     action.u.data = data;
12156cc2a415SPaolo Bonzini     list.value = &action;
12166cc2a415SPaolo Bonzini     list.next = NULL;
121794d16a64SJohn Snow     qmp_transaction(&list, false, NULL, errp);
12186cc2a415SPaolo Bonzini }
12196cc2a415SPaolo Bonzini 
12200901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
12210901f67eSBenoît Canet                                 bool has_node_name, const char *node_name,
12220901f67eSBenoît Canet                                 const char *snapshot_file,
12230901f67eSBenoît Canet                                 bool has_snapshot_node_name,
12240901f67eSBenoît Canet                                 const char *snapshot_node_name,
12256106e249SLuiz Capitulino                                 bool has_format, const char *format,
12260901f67eSBenoît Canet                                 bool has_mode, NewImageMode mode, Error **errp)
1227f8882568SJes Sorensen {
1228a911e6aeSAlberto Garcia     BlockdevSnapshotSync snapshot = {
12290901f67eSBenoît Canet         .has_device = has_device,
12306cc2a415SPaolo Bonzini         .device = (char *) device,
12310901f67eSBenoît Canet         .has_node_name = has_node_name,
12320901f67eSBenoît Canet         .node_name = (char *) node_name,
12336cc2a415SPaolo Bonzini         .snapshot_file = (char *) snapshot_file,
12340901f67eSBenoît Canet         .has_snapshot_node_name = has_snapshot_node_name,
12350901f67eSBenoît Canet         .snapshot_node_name = (char *) snapshot_node_name,
12366cc2a415SPaolo Bonzini         .has_format = has_format,
12376cc2a415SPaolo Bonzini         .format = (char *) format,
12386cc2a415SPaolo Bonzini         .has_mode = has_mode,
12396cc2a415SPaolo Bonzini         .mode = mode,
12406cc2a415SPaolo Bonzini     };
1241c8a83e85SKevin Wolf     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1242c8a83e85SKevin Wolf                        &snapshot, errp);
1243f8882568SJes Sorensen }
1244f8882568SJes Sorensen 
124543de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay,
124643de7e2dSAlberto Garcia                            Error **errp)
124743de7e2dSAlberto Garcia {
124843de7e2dSAlberto Garcia     BlockdevSnapshot snapshot_data = {
124943de7e2dSAlberto Garcia         .node = (char *) node,
125043de7e2dSAlberto Garcia         .overlay = (char *) overlay
125143de7e2dSAlberto Garcia     };
125243de7e2dSAlberto Garcia 
125343de7e2dSAlberto Garcia     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
125443de7e2dSAlberto Garcia                        &snapshot_data, errp);
125543de7e2dSAlberto Garcia }
125643de7e2dSAlberto Garcia 
1257f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device,
1258f323bc9eSWenchao Xia                                          const char *name,
1259f323bc9eSWenchao Xia                                          Error **errp)
1260f323bc9eSWenchao Xia {
1261f323bc9eSWenchao Xia     BlockdevSnapshotInternal snapshot = {
1262f323bc9eSWenchao Xia         .device = (char *) device,
1263f323bc9eSWenchao Xia         .name = (char *) name
1264f323bc9eSWenchao Xia     };
1265f323bc9eSWenchao Xia 
1266f323bc9eSWenchao Xia     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1267f323bc9eSWenchao Xia                        &snapshot, errp);
1268f323bc9eSWenchao Xia }
1269f323bc9eSWenchao Xia 
127044e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
127144e3e053SWenchao Xia                                                          bool has_id,
127244e3e053SWenchao Xia                                                          const char *id,
127344e3e053SWenchao Xia                                                          bool has_name,
127444e3e053SWenchao Xia                                                          const char *name,
127544e3e053SWenchao Xia                                                          Error **errp)
127644e3e053SWenchao Xia {
1277a0e8544cSFam Zheng     BlockDriverState *bs;
1278a0e8544cSFam Zheng     BlockBackend *blk;
12794ef3982aSStefan Hajnoczi     AioContext *aio_context;
128044e3e053SWenchao Xia     QEMUSnapshotInfo sn;
128144e3e053SWenchao Xia     Error *local_err = NULL;
128244e3e053SWenchao Xia     SnapshotInfo *info = NULL;
128344e3e053SWenchao Xia     int ret;
128444e3e053SWenchao Xia 
1285a0e8544cSFam Zheng     blk = blk_by_name(device);
1286a0e8544cSFam Zheng     if (!blk) {
128775158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
128875158ebbSMarkus Armbruster                   "Device '%s' not found", device);
128944e3e053SWenchao Xia         return NULL;
129044e3e053SWenchao Xia     }
12915433c24fSMax Reitz 
12925433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
12935433c24fSMax Reitz     aio_context_acquire(aio_context);
129444e3e053SWenchao Xia 
129544e3e053SWenchao Xia     if (!has_id) {
129644e3e053SWenchao Xia         id = NULL;
129744e3e053SWenchao Xia     }
129844e3e053SWenchao Xia 
129944e3e053SWenchao Xia     if (!has_name) {
130044e3e053SWenchao Xia         name = NULL;
130144e3e053SWenchao Xia     }
130244e3e053SWenchao Xia 
130344e3e053SWenchao Xia     if (!id && !name) {
130444e3e053SWenchao Xia         error_setg(errp, "Name or id must be provided");
13055433c24fSMax Reitz         goto out_aio_context;
130644e3e053SWenchao Xia     }
130744e3e053SWenchao Xia 
13085433c24fSMax Reitz     if (!blk_is_available(blk)) {
13095433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
13105433c24fSMax Reitz         goto out_aio_context;
13115433c24fSMax Reitz     }
13125433c24fSMax Reitz     bs = blk_bs(blk);
13134ef3982aSStefan Hajnoczi 
13140b928854SStefan Hajnoczi     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
13150b928854SStefan Hajnoczi         goto out_aio_context;
13160b928854SStefan Hajnoczi     }
13170b928854SStefan Hajnoczi 
131844e3e053SWenchao Xia     ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
131984d18f06SMarkus Armbruster     if (local_err) {
132044e3e053SWenchao Xia         error_propagate(errp, local_err);
13214ef3982aSStefan Hajnoczi         goto out_aio_context;
132244e3e053SWenchao Xia     }
132344e3e053SWenchao Xia     if (!ret) {
132444e3e053SWenchao Xia         error_setg(errp,
132544e3e053SWenchao Xia                    "Snapshot with id '%s' and name '%s' does not exist on "
132644e3e053SWenchao Xia                    "device '%s'",
132744e3e053SWenchao Xia                    STR_OR_NULL(id), STR_OR_NULL(name), device);
13284ef3982aSStefan Hajnoczi         goto out_aio_context;
132944e3e053SWenchao Xia     }
133044e3e053SWenchao Xia 
133144e3e053SWenchao Xia     bdrv_snapshot_delete(bs, id, name, &local_err);
133284d18f06SMarkus Armbruster     if (local_err) {
133344e3e053SWenchao Xia         error_propagate(errp, local_err);
13344ef3982aSStefan Hajnoczi         goto out_aio_context;
133544e3e053SWenchao Xia     }
133644e3e053SWenchao Xia 
13374ef3982aSStefan Hajnoczi     aio_context_release(aio_context);
13384ef3982aSStefan Hajnoczi 
13395839e53bSMarkus Armbruster     info = g_new0(SnapshotInfo, 1);
134044e3e053SWenchao Xia     info->id = g_strdup(sn.id_str);
134144e3e053SWenchao Xia     info->name = g_strdup(sn.name);
134244e3e053SWenchao Xia     info->date_nsec = sn.date_nsec;
134344e3e053SWenchao Xia     info->date_sec = sn.date_sec;
134444e3e053SWenchao Xia     info->vm_state_size = sn.vm_state_size;
134544e3e053SWenchao Xia     info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
134644e3e053SWenchao Xia     info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
134744e3e053SWenchao Xia 
134844e3e053SWenchao Xia     return info;
13494ef3982aSStefan Hajnoczi 
13504ef3982aSStefan Hajnoczi out_aio_context:
13514ef3982aSStefan Hajnoczi     aio_context_release(aio_context);
13524ef3982aSStefan Hajnoczi     return NULL;
135344e3e053SWenchao Xia }
13548802d1fdSJeff Cody 
1355341ebc2fSJohn Snow /**
1356341ebc2fSJohn Snow  * block_dirty_bitmap_lookup:
1357341ebc2fSJohn Snow  * Return a dirty bitmap (if present), after validating
1358341ebc2fSJohn Snow  * the node reference and bitmap names.
1359341ebc2fSJohn Snow  *
1360341ebc2fSJohn Snow  * @node: The name of the BDS node to search for bitmaps
1361341ebc2fSJohn Snow  * @name: The name of the bitmap to search for
1362341ebc2fSJohn Snow  * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1363341ebc2fSJohn Snow  * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1364341ebc2fSJohn Snow  * @errp: Output pointer for error information. Can be NULL.
1365341ebc2fSJohn Snow  *
1366341ebc2fSJohn Snow  * @return: A bitmap object on success, or NULL on failure.
1367341ebc2fSJohn Snow  */
1368341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1369341ebc2fSJohn Snow                                                   const char *name,
1370341ebc2fSJohn Snow                                                   BlockDriverState **pbs,
1371341ebc2fSJohn Snow                                                   AioContext **paio,
1372341ebc2fSJohn Snow                                                   Error **errp)
1373341ebc2fSJohn Snow {
1374341ebc2fSJohn Snow     BlockDriverState *bs;
1375341ebc2fSJohn Snow     BdrvDirtyBitmap *bitmap;
1376341ebc2fSJohn Snow     AioContext *aio_context;
1377341ebc2fSJohn Snow 
1378341ebc2fSJohn Snow     if (!node) {
1379341ebc2fSJohn Snow         error_setg(errp, "Node cannot be NULL");
1380341ebc2fSJohn Snow         return NULL;
1381341ebc2fSJohn Snow     }
1382341ebc2fSJohn Snow     if (!name) {
1383341ebc2fSJohn Snow         error_setg(errp, "Bitmap name cannot be NULL");
1384341ebc2fSJohn Snow         return NULL;
1385341ebc2fSJohn Snow     }
1386341ebc2fSJohn Snow     bs = bdrv_lookup_bs(node, node, NULL);
1387341ebc2fSJohn Snow     if (!bs) {
1388341ebc2fSJohn Snow         error_setg(errp, "Node '%s' not found", node);
1389341ebc2fSJohn Snow         return NULL;
1390341ebc2fSJohn Snow     }
1391341ebc2fSJohn Snow 
1392341ebc2fSJohn Snow     aio_context = bdrv_get_aio_context(bs);
1393341ebc2fSJohn Snow     aio_context_acquire(aio_context);
1394341ebc2fSJohn Snow 
1395341ebc2fSJohn Snow     bitmap = bdrv_find_dirty_bitmap(bs, name);
1396341ebc2fSJohn Snow     if (!bitmap) {
1397341ebc2fSJohn Snow         error_setg(errp, "Dirty bitmap '%s' not found", name);
1398341ebc2fSJohn Snow         goto fail;
1399341ebc2fSJohn Snow     }
1400341ebc2fSJohn Snow 
1401341ebc2fSJohn Snow     if (pbs) {
1402341ebc2fSJohn Snow         *pbs = bs;
1403341ebc2fSJohn Snow     }
1404341ebc2fSJohn Snow     if (paio) {
1405341ebc2fSJohn Snow         *paio = aio_context;
1406341ebc2fSJohn Snow     } else {
1407341ebc2fSJohn Snow         aio_context_release(aio_context);
1408341ebc2fSJohn Snow     }
1409341ebc2fSJohn Snow 
1410341ebc2fSJohn Snow     return bitmap;
1411341ebc2fSJohn Snow 
1412341ebc2fSJohn Snow  fail:
1413341ebc2fSJohn Snow     aio_context_release(aio_context);
1414341ebc2fSJohn Snow     return NULL;
1415341ebc2fSJohn Snow }
1416341ebc2fSJohn Snow 
1417b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */
1418ba0c86a3SWenchao Xia 
141950f43f0fSJohn Snow typedef struct BlkActionState BlkActionState;
1420ba0c86a3SWenchao Xia 
142150f43f0fSJohn Snow /**
142250f43f0fSJohn Snow  * BlkActionOps:
142350f43f0fSJohn Snow  * Table of operations that define an Action.
142450f43f0fSJohn Snow  *
142550f43f0fSJohn Snow  * @instance_size: Size of state struct, in bytes.
142650f43f0fSJohn Snow  * @prepare: Prepare the work, must NOT be NULL.
142750f43f0fSJohn Snow  * @commit: Commit the changes, can be NULL.
142850f43f0fSJohn Snow  * @abort: Abort the changes on fail, can be NULL.
142950f43f0fSJohn Snow  * @clean: Clean up resources after all transaction actions have called
143050f43f0fSJohn Snow  *         commit() or abort(). Can be NULL.
143150f43f0fSJohn Snow  *
143250f43f0fSJohn Snow  * Only prepare() may fail. In a single transaction, only one of commit() or
143350f43f0fSJohn Snow  * abort() will be called. clean() will always be called if it is present.
1434ba0c86a3SWenchao Xia  */
143550f43f0fSJohn Snow typedef struct BlkActionOps {
143650f43f0fSJohn Snow     size_t instance_size;
143750f43f0fSJohn Snow     void (*prepare)(BlkActionState *common, Error **errp);
143850f43f0fSJohn Snow     void (*commit)(BlkActionState *common);
143950f43f0fSJohn Snow     void (*abort)(BlkActionState *common);
144050f43f0fSJohn Snow     void (*clean)(BlkActionState *common);
144150f43f0fSJohn Snow } BlkActionOps;
144250f43f0fSJohn Snow 
144350f43f0fSJohn Snow /**
144450f43f0fSJohn Snow  * BlkActionState:
144550f43f0fSJohn Snow  * Describes one Action's state within a Transaction.
144650f43f0fSJohn Snow  *
144750f43f0fSJohn Snow  * @action: QAPI-defined enum identifying which Action to perform.
144850f43f0fSJohn Snow  * @ops: Table of ActionOps this Action can perform.
144994d16a64SJohn Snow  * @block_job_txn: Transaction which this action belongs to.
145050f43f0fSJohn Snow  * @entry: List membership for all Actions in this Transaction.
145150f43f0fSJohn Snow  *
145250f43f0fSJohn Snow  * This structure must be arranged as first member in a subclassed type,
145350f43f0fSJohn Snow  * assuming that the compiler will also arrange it to the same offsets as the
145450f43f0fSJohn Snow  * base class.
145550f43f0fSJohn Snow  */
145650f43f0fSJohn Snow struct BlkActionState {
1457c8a83e85SKevin Wolf     TransactionAction *action;
145850f43f0fSJohn Snow     const BlkActionOps *ops;
145994d16a64SJohn Snow     BlockJobTxn *block_job_txn;
146094d16a64SJohn Snow     TransactionProperties *txn_props;
146150f43f0fSJohn Snow     QSIMPLEQ_ENTRY(BlkActionState) entry;
1462ba0c86a3SWenchao Xia };
1463ba0c86a3SWenchao Xia 
1464bbe86010SWenchao Xia /* internal snapshot private data */
1465bbe86010SWenchao Xia typedef struct InternalSnapshotState {
146650f43f0fSJohn Snow     BlkActionState common;
1467bbe86010SWenchao Xia     BlockDriverState *bs;
14685d6e96efSStefan Hajnoczi     AioContext *aio_context;
1469bbe86010SWenchao Xia     QEMUSnapshotInfo sn;
1470507306ccSFam Zheng     bool created;
1471bbe86010SWenchao Xia } InternalSnapshotState;
1472bbe86010SWenchao Xia 
147394d16a64SJohn Snow 
147494d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp)
147594d16a64SJohn Snow {
147694d16a64SJohn Snow     if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
147794d16a64SJohn Snow         error_setg(errp,
147894d16a64SJohn Snow                    "Action '%s' does not support Transaction property "
147994d16a64SJohn Snow                    "completion-mode = %s",
148094d16a64SJohn Snow                    TransactionActionKind_lookup[s->action->type],
148194d16a64SJohn Snow                    ActionCompletionMode_lookup[s->txn_props->completion_mode]);
148294d16a64SJohn Snow         return -1;
148394d16a64SJohn Snow     }
148494d16a64SJohn Snow     return 0;
148594d16a64SJohn Snow }
148694d16a64SJohn Snow 
148750f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common,
1488bbe86010SWenchao Xia                                       Error **errp)
1489bbe86010SWenchao Xia {
1490f70edf99SMarkus Armbruster     Error *local_err = NULL;
1491bbe86010SWenchao Xia     const char *device;
1492bbe86010SWenchao Xia     const char *name;
1493a0e8544cSFam Zheng     BlockBackend *blk;
1494bbe86010SWenchao Xia     BlockDriverState *bs;
1495bbe86010SWenchao Xia     QEMUSnapshotInfo old_sn, *sn;
1496bbe86010SWenchao Xia     bool ret;
1497bbe86010SWenchao Xia     qemu_timeval tv;
1498bbe86010SWenchao Xia     BlockdevSnapshotInternal *internal;
1499bbe86010SWenchao Xia     InternalSnapshotState *state;
1500bbe86010SWenchao Xia     int ret1;
1501bbe86010SWenchao Xia 
15026a8f9661SEric Blake     g_assert(common->action->type ==
1503bbe86010SWenchao Xia              TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
15046a8f9661SEric Blake     internal = common->action->u.blockdev_snapshot_internal_sync;
1505bbe86010SWenchao Xia     state = DO_UPCAST(InternalSnapshotState, common, common);
1506bbe86010SWenchao Xia 
1507bbe86010SWenchao Xia     /* 1. parse input */
1508bbe86010SWenchao Xia     device = internal->device;
1509bbe86010SWenchao Xia     name = internal->name;
1510bbe86010SWenchao Xia 
1511bbe86010SWenchao Xia     /* 2. check for validation */
151294d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
151394d16a64SJohn Snow         return;
151494d16a64SJohn Snow     }
151594d16a64SJohn Snow 
1516a0e8544cSFam Zheng     blk = blk_by_name(device);
1517a0e8544cSFam Zheng     if (!blk) {
151875158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
151975158ebbSMarkus Armbruster                   "Device '%s' not found", device);
1520bbe86010SWenchao Xia         return;
1521bbe86010SWenchao Xia     }
1522bbe86010SWenchao Xia 
15235d6e96efSStefan Hajnoczi     /* AioContext is released in .clean() */
15245433c24fSMax Reitz     state->aio_context = blk_get_aio_context(blk);
15255d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
15265d6e96efSStefan Hajnoczi 
15275433c24fSMax Reitz     if (!blk_is_available(blk)) {
1528c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1529bbe86010SWenchao Xia         return;
1530bbe86010SWenchao Xia     }
15315433c24fSMax Reitz     bs = blk_bs(blk);
1532bbe86010SWenchao Xia 
1533507306ccSFam Zheng     state->bs = bs;
1534507306ccSFam Zheng     bdrv_drained_begin(bs);
1535507306ccSFam Zheng 
15363dc7ca3cSStefan Hajnoczi     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
15373dc7ca3cSStefan Hajnoczi         return;
15383dc7ca3cSStefan Hajnoczi     }
15393dc7ca3cSStefan Hajnoczi 
1540bbe86010SWenchao Xia     if (bdrv_is_read_only(bs)) {
154181e5f78aSAlberto Garcia         error_setg(errp, "Device '%s' is read only", device);
1542bbe86010SWenchao Xia         return;
1543bbe86010SWenchao Xia     }
1544bbe86010SWenchao Xia 
1545bbe86010SWenchao Xia     if (!bdrv_can_snapshot(bs)) {
154681e5f78aSAlberto Garcia         error_setg(errp, "Block format '%s' used by device '%s' "
154781e5f78aSAlberto Garcia                    "does not support internal snapshots",
154881e5f78aSAlberto Garcia                    bs->drv->format_name, device);
1549bbe86010SWenchao Xia         return;
1550bbe86010SWenchao Xia     }
1551bbe86010SWenchao Xia 
1552bbe86010SWenchao Xia     if (!strlen(name)) {
1553bbe86010SWenchao Xia         error_setg(errp, "Name is empty");
1554bbe86010SWenchao Xia         return;
1555bbe86010SWenchao Xia     }
1556bbe86010SWenchao Xia 
1557bbe86010SWenchao Xia     /* check whether a snapshot with name exist */
1558f70edf99SMarkus Armbruster     ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1559f70edf99SMarkus Armbruster                                             &local_err);
1560f70edf99SMarkus Armbruster     if (local_err) {
1561f70edf99SMarkus Armbruster         error_propagate(errp, local_err);
1562bbe86010SWenchao Xia         return;
1563bbe86010SWenchao Xia     } else if (ret) {
1564bbe86010SWenchao Xia         error_setg(errp,
1565bbe86010SWenchao Xia                    "Snapshot with name '%s' already exists on device '%s'",
1566bbe86010SWenchao Xia                    name, device);
1567bbe86010SWenchao Xia         return;
1568bbe86010SWenchao Xia     }
1569bbe86010SWenchao Xia 
1570bbe86010SWenchao Xia     /* 3. take the snapshot */
1571bbe86010SWenchao Xia     sn = &state->sn;
1572bbe86010SWenchao Xia     pstrcpy(sn->name, sizeof(sn->name), name);
1573bbe86010SWenchao Xia     qemu_gettimeofday(&tv);
1574bbe86010SWenchao Xia     sn->date_sec = tv.tv_sec;
1575bbe86010SWenchao Xia     sn->date_nsec = tv.tv_usec * 1000;
1576bbe86010SWenchao Xia     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1577bbe86010SWenchao Xia 
1578bbe86010SWenchao Xia     ret1 = bdrv_snapshot_create(bs, sn);
1579bbe86010SWenchao Xia     if (ret1 < 0) {
1580bbe86010SWenchao Xia         error_setg_errno(errp, -ret1,
1581bbe86010SWenchao Xia                          "Failed to create snapshot '%s' on device '%s'",
1582bbe86010SWenchao Xia                          name, device);
1583bbe86010SWenchao Xia         return;
1584bbe86010SWenchao Xia     }
1585bbe86010SWenchao Xia 
1586bbe86010SWenchao Xia     /* 4. succeed, mark a snapshot is created */
1587507306ccSFam Zheng     state->created = true;
1588bbe86010SWenchao Xia }
1589bbe86010SWenchao Xia 
159050f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common)
1591bbe86010SWenchao Xia {
1592bbe86010SWenchao Xia     InternalSnapshotState *state =
1593bbe86010SWenchao Xia                              DO_UPCAST(InternalSnapshotState, common, common);
1594bbe86010SWenchao Xia     BlockDriverState *bs = state->bs;
1595bbe86010SWenchao Xia     QEMUSnapshotInfo *sn = &state->sn;
1596bbe86010SWenchao Xia     Error *local_error = NULL;
1597bbe86010SWenchao Xia 
1598507306ccSFam Zheng     if (!state->created) {
1599bbe86010SWenchao Xia         return;
1600bbe86010SWenchao Xia     }
1601bbe86010SWenchao Xia 
1602bbe86010SWenchao Xia     if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1603c29b77f9SMarkus Armbruster         error_reportf_err(local_error,
1604c29b77f9SMarkus Armbruster                           "Failed to delete snapshot with id '%s' and "
1605c29b77f9SMarkus Armbruster                           "name '%s' on device '%s' in abort: ",
1606c29b77f9SMarkus Armbruster                           sn->id_str, sn->name,
1607c29b77f9SMarkus Armbruster                           bdrv_get_device_name(bs));
1608bbe86010SWenchao Xia     }
1609bbe86010SWenchao Xia }
1610bbe86010SWenchao Xia 
161150f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common)
16125d6e96efSStefan Hajnoczi {
16135d6e96efSStefan Hajnoczi     InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
16145d6e96efSStefan Hajnoczi                                              common, common);
16155d6e96efSStefan Hajnoczi 
16165d6e96efSStefan Hajnoczi     if (state->aio_context) {
1617507306ccSFam Zheng         if (state->bs) {
1618507306ccSFam Zheng             bdrv_drained_end(state->bs);
1619507306ccSFam Zheng         }
16205d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
16215d6e96efSStefan Hajnoczi     }
16225d6e96efSStefan Hajnoczi }
16235d6e96efSStefan Hajnoczi 
1624ba0c86a3SWenchao Xia /* external snapshot private data */
1625ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState {
162650f43f0fSJohn Snow     BlkActionState common;
16278802d1fdSJeff Cody     BlockDriverState *old_bs;
16288802d1fdSJeff Cody     BlockDriverState *new_bs;
16295d6e96efSStefan Hajnoczi     AioContext *aio_context;
1630ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState;
16318802d1fdSJeff Cody 
163250f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common,
16339b9877eeSWenchao Xia                                       Error **errp)
16349b9877eeSWenchao Xia {
163543de7e2dSAlberto Garcia     int flags = 0, ret;
163643de7e2dSAlberto Garcia     QDict *options = NULL;
16379b9877eeSWenchao Xia     Error *local_err = NULL;
163843de7e2dSAlberto Garcia     /* Device and node name of the image to generate the snapshot from */
1639e2a31e87SWenchao Xia     const char *device;
16400901f67eSBenoît Canet     const char *node_name;
164143de7e2dSAlberto Garcia     /* Reference to the new image (for 'blockdev-snapshot') */
164243de7e2dSAlberto Garcia     const char *snapshot_ref;
164343de7e2dSAlberto Garcia     /* File name of the new image (for 'blockdev-snapshot-sync') */
1644e2a31e87SWenchao Xia     const char *new_image_file;
1645ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1646ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1647c8a83e85SKevin Wolf     TransactionAction *action = common->action;
16489b9877eeSWenchao Xia 
164943de7e2dSAlberto Garcia     /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
165043de7e2dSAlberto Garcia      * purpose but a different set of parameters */
165143de7e2dSAlberto Garcia     switch (action->type) {
165243de7e2dSAlberto Garcia     case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
165343de7e2dSAlberto Garcia         {
165443de7e2dSAlberto Garcia             BlockdevSnapshot *s = action->u.blockdev_snapshot;
165543de7e2dSAlberto Garcia             device = s->node;
165643de7e2dSAlberto Garcia             node_name = s->node;
165743de7e2dSAlberto Garcia             new_image_file = NULL;
165843de7e2dSAlberto Garcia             snapshot_ref = s->overlay;
1659e2a31e87SWenchao Xia         }
166043de7e2dSAlberto Garcia         break;
166143de7e2dSAlberto Garcia     case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
166243de7e2dSAlberto Garcia         {
166343de7e2dSAlberto Garcia             BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
166443de7e2dSAlberto Garcia             device = s->has_device ? s->device : NULL;
166543de7e2dSAlberto Garcia             node_name = s->has_node_name ? s->node_name : NULL;
166643de7e2dSAlberto Garcia             new_image_file = s->snapshot_file;
166743de7e2dSAlberto Garcia             snapshot_ref = NULL;
166843de7e2dSAlberto Garcia         }
166943de7e2dSAlberto Garcia         break;
167043de7e2dSAlberto Garcia     default:
167143de7e2dSAlberto Garcia         g_assert_not_reached();
1672e2a31e87SWenchao Xia     }
1673e2a31e87SWenchao Xia 
1674e2a31e87SWenchao Xia     /* start processing */
167594d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
167694d16a64SJohn Snow         return;
167794d16a64SJohn Snow     }
167894d16a64SJohn Snow 
167943de7e2dSAlberto Garcia     state->old_bs = bdrv_lookup_bs(device, node_name, errp);
168043de7e2dSAlberto Garcia     if (!state->old_bs) {
16819b9877eeSWenchao Xia         return;
16829b9877eeSWenchao Xia     }
16839b9877eeSWenchao Xia 
16845d6e96efSStefan Hajnoczi     /* Acquire AioContext now so any threads operating on old_bs stop */
16855d6e96efSStefan Hajnoczi     state->aio_context = bdrv_get_aio_context(state->old_bs);
16865d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
1687da763e83SFam Zheng     bdrv_drained_begin(state->old_bs);
16885d6e96efSStefan Hajnoczi 
1689ba5d6ab6SStefan Hajnoczi     if (!bdrv_is_inserted(state->old_bs)) {
1690c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
16919b9877eeSWenchao Xia         return;
16929b9877eeSWenchao Xia     }
16939b9877eeSWenchao Xia 
16943718d8abSFam Zheng     if (bdrv_op_is_blocked(state->old_bs,
16953718d8abSFam Zheng                            BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
16969b9877eeSWenchao Xia         return;
16979b9877eeSWenchao Xia     }
16989b9877eeSWenchao Xia 
1699ba5d6ab6SStefan Hajnoczi     if (!bdrv_is_read_only(state->old_bs)) {
1700ba5d6ab6SStefan Hajnoczi         if (bdrv_flush(state->old_bs)) {
1701c6bd8c70SMarkus Armbruster             error_setg(errp, QERR_IO_ERROR);
17029b9877eeSWenchao Xia             return;
17039b9877eeSWenchao Xia         }
17049b9877eeSWenchao Xia     }
17059b9877eeSWenchao Xia 
1706212a5a8fSBenoît Canet     if (!bdrv_is_first_non_filter(state->old_bs)) {
1707c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1708f6186f49SBenoît Canet         return;
1709f6186f49SBenoît Canet     }
1710f6186f49SBenoît Canet 
171143de7e2dSAlberto Garcia     if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
171243de7e2dSAlberto Garcia         BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
171343de7e2dSAlberto Garcia         const char *format = s->has_format ? s->format : "qcow2";
171443de7e2dSAlberto Garcia         enum NewImageMode mode;
171543de7e2dSAlberto Garcia         const char *snapshot_node_name =
171643de7e2dSAlberto Garcia             s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
171743de7e2dSAlberto Garcia 
171843de7e2dSAlberto Garcia         if (node_name && !snapshot_node_name) {
171943de7e2dSAlberto Garcia             error_setg(errp, "New snapshot node name missing");
172043de7e2dSAlberto Garcia             return;
172143de7e2dSAlberto Garcia         }
172243de7e2dSAlberto Garcia 
172343de7e2dSAlberto Garcia         if (snapshot_node_name &&
172443de7e2dSAlberto Garcia             bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
172543de7e2dSAlberto Garcia             error_setg(errp, "New snapshot node name already in use");
172643de7e2dSAlberto Garcia             return;
172743de7e2dSAlberto Garcia         }
172843de7e2dSAlberto Garcia 
1729ba5d6ab6SStefan Hajnoczi         flags = state->old_bs->open_flags;
17309b9877eeSWenchao Xia 
17319b9877eeSWenchao Xia         /* create new image w/backing file */
173243de7e2dSAlberto Garcia         mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
17339b9877eeSWenchao Xia         if (mode != NEW_IMAGE_MODE_EXISTING) {
17349b9877eeSWenchao Xia             bdrv_img_create(new_image_file, format,
1735ba5d6ab6SStefan Hajnoczi                             state->old_bs->filename,
1736ba5d6ab6SStefan Hajnoczi                             state->old_bs->drv->format_name,
17379b9877eeSWenchao Xia                             NULL, -1, flags, &local_err, false);
173884d18f06SMarkus Armbruster             if (local_err) {
17399b9877eeSWenchao Xia                 error_propagate(errp, local_err);
17409b9877eeSWenchao Xia                 return;
17419b9877eeSWenchao Xia             }
17429b9877eeSWenchao Xia         }
17439b9877eeSWenchao Xia 
17440901f67eSBenoît Canet         options = qdict_new();
174543de7e2dSAlberto Garcia         if (s->has_snapshot_node_name) {
17460901f67eSBenoît Canet             qdict_put(options, "node-name",
17470901f67eSBenoît Canet                       qstring_from_str(snapshot_node_name));
17480901f67eSBenoît Canet         }
1749e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
17500901f67eSBenoît Canet 
175143de7e2dSAlberto Garcia         flags |= BDRV_O_NO_BACKING;
175243de7e2dSAlberto Garcia     }
175343de7e2dSAlberto Garcia 
1754f67503e5SMax Reitz     assert(state->new_bs == NULL);
175543de7e2dSAlberto Garcia     ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options,
175643de7e2dSAlberto Garcia                     flags, errp);
1757f67503e5SMax Reitz     /* We will manually add the backing_hd field to the bs later */
17589b9877eeSWenchao Xia     if (ret != 0) {
175943de7e2dSAlberto Garcia         return;
176043de7e2dSAlberto Garcia     }
176143de7e2dSAlberto Garcia 
176243de7e2dSAlberto Garcia     if (state->new_bs->blk != NULL) {
176343de7e2dSAlberto Garcia         error_setg(errp, "The snapshot is already in use by %s",
176443de7e2dSAlberto Garcia                    blk_name(state->new_bs->blk));
176543de7e2dSAlberto Garcia         return;
176643de7e2dSAlberto Garcia     }
176743de7e2dSAlberto Garcia 
176843de7e2dSAlberto Garcia     if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
176943de7e2dSAlberto Garcia                            errp)) {
177043de7e2dSAlberto Garcia         return;
177143de7e2dSAlberto Garcia     }
177243de7e2dSAlberto Garcia 
177343de7e2dSAlberto Garcia     if (state->new_bs->backing != NULL) {
177443de7e2dSAlberto Garcia         error_setg(errp, "The snapshot already has a backing image");
177508b24cfeSAlberto Garcia         return;
177608b24cfeSAlberto Garcia     }
177708b24cfeSAlberto Garcia 
177808b24cfeSAlberto Garcia     if (!state->new_bs->drv->supports_backing) {
177908b24cfeSAlberto Garcia         error_setg(errp, "The snapshot does not support backing images");
17809b9877eeSWenchao Xia     }
17819b9877eeSWenchao Xia }
17829b9877eeSWenchao Xia 
178350f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common)
17843b0047e8SWenchao Xia {
1785ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1786ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1787ba0c86a3SWenchao Xia 
17885d6e96efSStefan Hajnoczi     bdrv_set_aio_context(state->new_bs, state->aio_context);
17895d6e96efSStefan Hajnoczi 
1790ba5d6ab6SStefan Hajnoczi     /* This removes our old bs and adds the new bs */
1791ba5d6ab6SStefan Hajnoczi     bdrv_append(state->new_bs, state->old_bs);
17923b0047e8SWenchao Xia     /* We don't need (or want) to use the transactional
17933b0047e8SWenchao Xia      * bdrv_reopen_multiple() across all the entries at once, because we
17943b0047e8SWenchao Xia      * don't want to abort all of them if one of them fails the reopen */
1795dd62f1caSKevin Wolf     bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
17963b0047e8SWenchao Xia                 NULL);
17973b0047e8SWenchao Xia }
17983b0047e8SWenchao Xia 
179950f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common)
180096b86bf7SWenchao Xia {
1801ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1802ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1803ba5d6ab6SStefan Hajnoczi     if (state->new_bs) {
18044f6fd349SFam Zheng         bdrv_unref(state->new_bs);
180596b86bf7SWenchao Xia     }
1806da763e83SFam Zheng }
1807da763e83SFam Zheng 
180850f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common)
1809da763e83SFam Zheng {
1810da763e83SFam Zheng     ExternalSnapshotState *state =
1811da763e83SFam Zheng                              DO_UPCAST(ExternalSnapshotState, common, common);
18125d6e96efSStefan Hajnoczi     if (state->aio_context) {
1813da763e83SFam Zheng         bdrv_drained_end(state->old_bs);
18145d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
18155d6e96efSStefan Hajnoczi     }
181696b86bf7SWenchao Xia }
181796b86bf7SWenchao Xia 
18183037f364SStefan Hajnoczi typedef struct DriveBackupState {
181950f43f0fSJohn Snow     BlkActionState common;
18203037f364SStefan Hajnoczi     BlockDriverState *bs;
18215d6e96efSStefan Hajnoczi     AioContext *aio_context;
18223037f364SStefan Hajnoczi     BlockJob *job;
18233037f364SStefan Hajnoczi } DriveBackupState;
18243037f364SStefan Hajnoczi 
182578f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target,
182678f51fdeSJohn Snow                             bool has_format, const char *format,
182778f51fdeSJohn Snow                             enum MirrorSyncMode sync,
182878f51fdeSJohn Snow                             bool has_mode, enum NewImageMode mode,
182978f51fdeSJohn Snow                             bool has_speed, int64_t speed,
183078f51fdeSJohn Snow                             bool has_bitmap, const char *bitmap,
183178f51fdeSJohn Snow                             bool has_on_source_error,
183278f51fdeSJohn Snow                             BlockdevOnError on_source_error,
183378f51fdeSJohn Snow                             bool has_on_target_error,
183478f51fdeSJohn Snow                             BlockdevOnError on_target_error,
183578f51fdeSJohn Snow                             BlockJobTxn *txn, Error **errp);
183678f51fdeSJohn Snow 
183750f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp)
18383037f364SStefan Hajnoczi {
18393037f364SStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1840a0e8544cSFam Zheng     BlockBackend *blk;
18413037f364SStefan Hajnoczi     DriveBackup *backup;
18423037f364SStefan Hajnoczi     Error *local_err = NULL;
18433037f364SStefan Hajnoczi 
18446a8f9661SEric Blake     assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
18456a8f9661SEric Blake     backup = common->action->u.drive_backup;
18463037f364SStefan Hajnoczi 
1847a0e8544cSFam Zheng     blk = blk_by_name(backup->device);
1848a0e8544cSFam Zheng     if (!blk) {
184975158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
185075158ebbSMarkus Armbruster                   "Device '%s' not found", backup->device);
18515d6e96efSStefan Hajnoczi         return;
18525d6e96efSStefan Hajnoczi     }
18535d6e96efSStefan Hajnoczi 
18541fdd4b7bSFam Zheng     if (!blk_is_available(blk)) {
18551fdd4b7bSFam Zheng         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
18561fdd4b7bSFam Zheng         return;
18571fdd4b7bSFam Zheng     }
18581fdd4b7bSFam Zheng 
18595d6e96efSStefan Hajnoczi     /* AioContext is released in .clean() */
18605433c24fSMax Reitz     state->aio_context = blk_get_aio_context(blk);
18615d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
18621fdd4b7bSFam Zheng     bdrv_drained_begin(blk_bs(blk));
18631fdd4b7bSFam Zheng     state->bs = blk_bs(blk);
18645d6e96efSStefan Hajnoczi 
186578f51fdeSJohn Snow     do_drive_backup(backup->device, backup->target,
18663037f364SStefan Hajnoczi                     backup->has_format, backup->format,
1867b53169eaSStefan Hajnoczi                     backup->sync,
18683037f364SStefan Hajnoczi                     backup->has_mode, backup->mode,
18693037f364SStefan Hajnoczi                     backup->has_speed, backup->speed,
1870d58d8453SJohn Snow                     backup->has_bitmap, backup->bitmap,
18713037f364SStefan Hajnoczi                     backup->has_on_source_error, backup->on_source_error,
18723037f364SStefan Hajnoczi                     backup->has_on_target_error, backup->on_target_error,
187394d16a64SJohn Snow                     common->block_job_txn, &local_err);
187484d18f06SMarkus Armbruster     if (local_err) {
18753037f364SStefan Hajnoczi         error_propagate(errp, local_err);
18763037f364SStefan Hajnoczi         return;
18773037f364SStefan Hajnoczi     }
18783037f364SStefan Hajnoczi 
18793037f364SStefan Hajnoczi     state->job = state->bs->job;
18803037f364SStefan Hajnoczi }
18813037f364SStefan Hajnoczi 
188250f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common)
18833037f364SStefan Hajnoczi {
18843037f364SStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
18853037f364SStefan Hajnoczi     BlockDriverState *bs = state->bs;
18863037f364SStefan Hajnoczi 
18873037f364SStefan Hajnoczi     /* Only cancel if it's the job we started */
18883037f364SStefan Hajnoczi     if (bs && bs->job && bs->job == state->job) {
18893037f364SStefan Hajnoczi         block_job_cancel_sync(bs->job);
18903037f364SStefan Hajnoczi     }
18913037f364SStefan Hajnoczi }
18923037f364SStefan Hajnoczi 
189350f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common)
18945d6e96efSStefan Hajnoczi {
18955d6e96efSStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
18965d6e96efSStefan Hajnoczi 
18975d6e96efSStefan Hajnoczi     if (state->aio_context) {
18981fdd4b7bSFam Zheng         bdrv_drained_end(state->bs);
18995d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
19005d6e96efSStefan Hajnoczi     }
19015d6e96efSStefan Hajnoczi }
19025d6e96efSStefan Hajnoczi 
1903bd8baecdSFam Zheng typedef struct BlockdevBackupState {
190450f43f0fSJohn Snow     BlkActionState common;
1905bd8baecdSFam Zheng     BlockDriverState *bs;
1906bd8baecdSFam Zheng     BlockJob *job;
1907bd8baecdSFam Zheng     AioContext *aio_context;
1908bd8baecdSFam Zheng } BlockdevBackupState;
1909bd8baecdSFam Zheng 
191078f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target,
191178f51fdeSJohn Snow                                enum MirrorSyncMode sync,
191278f51fdeSJohn Snow                                bool has_speed, int64_t speed,
191378f51fdeSJohn Snow                                bool has_on_source_error,
191478f51fdeSJohn Snow                                BlockdevOnError on_source_error,
191578f51fdeSJohn Snow                                bool has_on_target_error,
191678f51fdeSJohn Snow                                BlockdevOnError on_target_error,
191778f51fdeSJohn Snow                                BlockJobTxn *txn, Error **errp);
191878f51fdeSJohn Snow 
191950f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1920bd8baecdSFam Zheng {
1921bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1922bd8baecdSFam Zheng     BlockdevBackup *backup;
19235433c24fSMax Reitz     BlockBackend *blk, *target;
1924bd8baecdSFam Zheng     Error *local_err = NULL;
1925bd8baecdSFam Zheng 
19266a8f9661SEric Blake     assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
19276a8f9661SEric Blake     backup = common->action->u.blockdev_backup;
1928bd8baecdSFam Zheng 
1929a0e8544cSFam Zheng     blk = blk_by_name(backup->device);
1930a0e8544cSFam Zheng     if (!blk) {
19315b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", backup->device);
1932bd8baecdSFam Zheng         return;
1933bd8baecdSFam Zheng     }
1934bd8baecdSFam Zheng 
1935ff52bf36SFam Zheng     if (!blk_is_available(blk)) {
1936ff52bf36SFam Zheng         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1937ff52bf36SFam Zheng         return;
1938ff52bf36SFam Zheng     }
1939ff52bf36SFam Zheng 
19405433c24fSMax Reitz     target = blk_by_name(backup->target);
19415433c24fSMax Reitz     if (!target) {
19425b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", backup->target);
1943bd8baecdSFam Zheng         return;
1944bd8baecdSFam Zheng     }
1945bd8baecdSFam Zheng 
1946bd8baecdSFam Zheng     /* AioContext is released in .clean() */
19475433c24fSMax Reitz     state->aio_context = blk_get_aio_context(blk);
19485433c24fSMax Reitz     if (state->aio_context != blk_get_aio_context(target)) {
1949bd8baecdSFam Zheng         state->aio_context = NULL;
1950bd8baecdSFam Zheng         error_setg(errp, "Backup between two IO threads is not implemented");
1951bd8baecdSFam Zheng         return;
1952bd8baecdSFam Zheng     }
1953bd8baecdSFam Zheng     aio_context_acquire(state->aio_context);
1954ff52bf36SFam Zheng     state->bs = blk_bs(blk);
1955ff52bf36SFam Zheng     bdrv_drained_begin(state->bs);
1956bd8baecdSFam Zheng 
195778f51fdeSJohn Snow     do_blockdev_backup(backup->device, backup->target,
1958bd8baecdSFam Zheng                        backup->sync,
1959bd8baecdSFam Zheng                        backup->has_speed, backup->speed,
1960bd8baecdSFam Zheng                        backup->has_on_source_error, backup->on_source_error,
1961bd8baecdSFam Zheng                        backup->has_on_target_error, backup->on_target_error,
196294d16a64SJohn Snow                        common->block_job_txn, &local_err);
1963bd8baecdSFam Zheng     if (local_err) {
1964bd8baecdSFam Zheng         error_propagate(errp, local_err);
1965bd8baecdSFam Zheng         return;
1966bd8baecdSFam Zheng     }
1967bd8baecdSFam Zheng 
1968bd8baecdSFam Zheng     state->job = state->bs->job;
1969bd8baecdSFam Zheng }
1970bd8baecdSFam Zheng 
197150f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common)
1972bd8baecdSFam Zheng {
1973bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1974bd8baecdSFam Zheng     BlockDriverState *bs = state->bs;
1975bd8baecdSFam Zheng 
1976bd8baecdSFam Zheng     /* Only cancel if it's the job we started */
1977bd8baecdSFam Zheng     if (bs && bs->job && bs->job == state->job) {
1978bd8baecdSFam Zheng         block_job_cancel_sync(bs->job);
1979bd8baecdSFam Zheng     }
1980bd8baecdSFam Zheng }
1981bd8baecdSFam Zheng 
198250f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common)
1983bd8baecdSFam Zheng {
1984bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1985bd8baecdSFam Zheng 
1986bd8baecdSFam Zheng     if (state->aio_context) {
1987ff52bf36SFam Zheng         bdrv_drained_end(state->bs);
1988bd8baecdSFam Zheng         aio_context_release(state->aio_context);
1989bd8baecdSFam Zheng     }
1990bd8baecdSFam Zheng }
1991bd8baecdSFam Zheng 
1992df9a681dSFam Zheng typedef struct BlockDirtyBitmapState {
199350f43f0fSJohn Snow     BlkActionState common;
1994df9a681dSFam Zheng     BdrvDirtyBitmap *bitmap;
1995df9a681dSFam Zheng     BlockDriverState *bs;
1996df9a681dSFam Zheng     AioContext *aio_context;
1997df9a681dSFam Zheng     HBitmap *backup;
1998df9a681dSFam Zheng     bool prepared;
1999df9a681dSFam Zheng } BlockDirtyBitmapState;
2000df9a681dSFam Zheng 
200150f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common,
2002df9a681dSFam Zheng                                            Error **errp)
2003df9a681dSFam Zheng {
2004df9a681dSFam Zheng     Error *local_err = NULL;
2005df9a681dSFam Zheng     BlockDirtyBitmapAdd *action;
2006df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2007df9a681dSFam Zheng                                              common, common);
2008df9a681dSFam Zheng 
200994d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
201094d16a64SJohn Snow         return;
201194d16a64SJohn Snow     }
201294d16a64SJohn Snow 
201350f43f0fSJohn Snow     action = common->action->u.block_dirty_bitmap_add;
2014df9a681dSFam Zheng     /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2015df9a681dSFam Zheng     qmp_block_dirty_bitmap_add(action->node, action->name,
2016df9a681dSFam Zheng                                action->has_granularity, action->granularity,
2017df9a681dSFam Zheng                                &local_err);
2018df9a681dSFam Zheng 
2019df9a681dSFam Zheng     if (!local_err) {
2020df9a681dSFam Zheng         state->prepared = true;
2021df9a681dSFam Zheng     } else {
2022df9a681dSFam Zheng         error_propagate(errp, local_err);
2023df9a681dSFam Zheng     }
2024df9a681dSFam Zheng }
2025df9a681dSFam Zheng 
202650f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common)
2027df9a681dSFam Zheng {
2028df9a681dSFam Zheng     BlockDirtyBitmapAdd *action;
2029df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2030df9a681dSFam Zheng                                              common, common);
2031df9a681dSFam Zheng 
203250f43f0fSJohn Snow     action = common->action->u.block_dirty_bitmap_add;
2033df9a681dSFam Zheng     /* Should not be able to fail: IF the bitmap was added via .prepare(),
2034df9a681dSFam Zheng      * then the node reference and bitmap name must have been valid.
2035df9a681dSFam Zheng      */
2036df9a681dSFam Zheng     if (state->prepared) {
2037df9a681dSFam Zheng         qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2038df9a681dSFam Zheng     }
2039df9a681dSFam Zheng }
2040df9a681dSFam Zheng 
204150f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
2042df9a681dSFam Zheng                                              Error **errp)
2043df9a681dSFam Zheng {
2044df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2045df9a681dSFam Zheng                                              common, common);
2046df9a681dSFam Zheng     BlockDirtyBitmap *action;
2047df9a681dSFam Zheng 
204894d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
204994d16a64SJohn Snow         return;
205094d16a64SJohn Snow     }
205194d16a64SJohn Snow 
205250f43f0fSJohn Snow     action = common->action->u.block_dirty_bitmap_clear;
2053df9a681dSFam Zheng     state->bitmap = block_dirty_bitmap_lookup(action->node,
2054df9a681dSFam Zheng                                               action->name,
2055df9a681dSFam Zheng                                               &state->bs,
2056df9a681dSFam Zheng                                               &state->aio_context,
2057df9a681dSFam Zheng                                               errp);
2058df9a681dSFam Zheng     if (!state->bitmap) {
2059df9a681dSFam Zheng         return;
2060df9a681dSFam Zheng     }
2061df9a681dSFam Zheng 
2062df9a681dSFam Zheng     if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2063df9a681dSFam Zheng         error_setg(errp, "Cannot modify a frozen bitmap");
2064df9a681dSFam Zheng         return;
2065df9a681dSFam Zheng     } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2066df9a681dSFam Zheng         error_setg(errp, "Cannot clear a disabled bitmap");
2067df9a681dSFam Zheng         return;
2068df9a681dSFam Zheng     }
2069df9a681dSFam Zheng 
2070df9a681dSFam Zheng     bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2071df9a681dSFam Zheng     /* AioContext is released in .clean() */
2072df9a681dSFam Zheng }
2073df9a681dSFam Zheng 
207450f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common)
2075df9a681dSFam Zheng {
2076df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2077df9a681dSFam Zheng                                              common, common);
2078df9a681dSFam Zheng 
2079df9a681dSFam Zheng     bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2080df9a681dSFam Zheng }
2081df9a681dSFam Zheng 
208250f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common)
2083df9a681dSFam Zheng {
2084df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2085df9a681dSFam Zheng                                              common, common);
2086df9a681dSFam Zheng 
2087df9a681dSFam Zheng     hbitmap_free(state->backup);
2088df9a681dSFam Zheng }
2089df9a681dSFam Zheng 
209050f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common)
2091df9a681dSFam Zheng {
2092df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2093df9a681dSFam Zheng                                              common, common);
2094df9a681dSFam Zheng 
2095df9a681dSFam Zheng     if (state->aio_context) {
2096df9a681dSFam Zheng         aio_context_release(state->aio_context);
2097df9a681dSFam Zheng     }
2098df9a681dSFam Zheng }
2099df9a681dSFam Zheng 
210050f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp)
210178b18b78SStefan Hajnoczi {
210278b18b78SStefan Hajnoczi     error_setg(errp, "Transaction aborted using Abort action");
210378b18b78SStefan Hajnoczi }
210478b18b78SStefan Hajnoczi 
210550f43f0fSJohn Snow static void abort_commit(BlkActionState *common)
210678b18b78SStefan Hajnoczi {
2107dfc6f865SStefan Weil     g_assert_not_reached(); /* this action never succeeds */
210878b18b78SStefan Hajnoczi }
210978b18b78SStefan Hajnoczi 
211050f43f0fSJohn Snow static const BlkActionOps actions[] = {
211143de7e2dSAlberto Garcia     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
211243de7e2dSAlberto Garcia         .instance_size = sizeof(ExternalSnapshotState),
211343de7e2dSAlberto Garcia         .prepare  = external_snapshot_prepare,
211443de7e2dSAlberto Garcia         .commit   = external_snapshot_commit,
211543de7e2dSAlberto Garcia         .abort = external_snapshot_abort,
21164ad6f3dbSAlberto Garcia         .clean = external_snapshot_clean,
211743de7e2dSAlberto Garcia     },
2118c8a83e85SKevin Wolf     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2119ba5d6ab6SStefan Hajnoczi         .instance_size = sizeof(ExternalSnapshotState),
2120ba0c86a3SWenchao Xia         .prepare  = external_snapshot_prepare,
2121ba0c86a3SWenchao Xia         .commit   = external_snapshot_commit,
2122ba0c86a3SWenchao Xia         .abort = external_snapshot_abort,
2123da763e83SFam Zheng         .clean = external_snapshot_clean,
2124ba0c86a3SWenchao Xia     },
21253037f364SStefan Hajnoczi     [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
21263037f364SStefan Hajnoczi         .instance_size = sizeof(DriveBackupState),
21273037f364SStefan Hajnoczi         .prepare = drive_backup_prepare,
21283037f364SStefan Hajnoczi         .abort = drive_backup_abort,
21295d6e96efSStefan Hajnoczi         .clean = drive_backup_clean,
21303037f364SStefan Hajnoczi     },
2131bd8baecdSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2132bd8baecdSFam Zheng         .instance_size = sizeof(BlockdevBackupState),
2133bd8baecdSFam Zheng         .prepare = blockdev_backup_prepare,
2134bd8baecdSFam Zheng         .abort = blockdev_backup_abort,
2135bd8baecdSFam Zheng         .clean = blockdev_backup_clean,
2136bd8baecdSFam Zheng     },
213778b18b78SStefan Hajnoczi     [TRANSACTION_ACTION_KIND_ABORT] = {
213850f43f0fSJohn Snow         .instance_size = sizeof(BlkActionState),
213978b18b78SStefan Hajnoczi         .prepare = abort_prepare,
214078b18b78SStefan Hajnoczi         .commit = abort_commit,
214178b18b78SStefan Hajnoczi     },
2142bbe86010SWenchao Xia     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2143bbe86010SWenchao Xia         .instance_size = sizeof(InternalSnapshotState),
2144bbe86010SWenchao Xia         .prepare  = internal_snapshot_prepare,
2145bbe86010SWenchao Xia         .abort = internal_snapshot_abort,
21465d6e96efSStefan Hajnoczi         .clean = internal_snapshot_clean,
2147bbe86010SWenchao Xia     },
2148df9a681dSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2149df9a681dSFam Zheng         .instance_size = sizeof(BlockDirtyBitmapState),
2150df9a681dSFam Zheng         .prepare = block_dirty_bitmap_add_prepare,
2151df9a681dSFam Zheng         .abort = block_dirty_bitmap_add_abort,
2152df9a681dSFam Zheng     },
2153df9a681dSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2154df9a681dSFam Zheng         .instance_size = sizeof(BlockDirtyBitmapState),
2155df9a681dSFam Zheng         .prepare = block_dirty_bitmap_clear_prepare,
2156df9a681dSFam Zheng         .commit = block_dirty_bitmap_clear_commit,
2157df9a681dSFam Zheng         .abort = block_dirty_bitmap_clear_abort,
2158df9a681dSFam Zheng         .clean = block_dirty_bitmap_clear_clean,
2159df9a681dSFam Zheng     }
2160ba0c86a3SWenchao Xia };
2161ba0c86a3SWenchao Xia 
216294d16a64SJohn Snow /**
216394d16a64SJohn Snow  * Allocate a TransactionProperties structure if necessary, and fill
216494d16a64SJohn Snow  * that structure with desired defaults if they are unset.
216594d16a64SJohn Snow  */
216694d16a64SJohn Snow static TransactionProperties *get_transaction_properties(
216794d16a64SJohn Snow     TransactionProperties *props)
216894d16a64SJohn Snow {
216994d16a64SJohn Snow     if (!props) {
217094d16a64SJohn Snow         props = g_new0(TransactionProperties, 1);
217194d16a64SJohn Snow     }
217294d16a64SJohn Snow 
217394d16a64SJohn Snow     if (!props->has_completion_mode) {
217494d16a64SJohn Snow         props->has_completion_mode = true;
217594d16a64SJohn Snow         props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
217694d16a64SJohn Snow     }
217794d16a64SJohn Snow 
217894d16a64SJohn Snow     return props;
217994d16a64SJohn Snow }
218094d16a64SJohn Snow 
21818802d1fdSJeff Cody /*
2182b756b9ceSStefan Hajnoczi  * 'Atomic' group operations.  The operations are performed as a set, and if
2183b756b9ceSStefan Hajnoczi  * any fail then we roll back all operations in the group.
21848802d1fdSJeff Cody  */
218594d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list,
218694d16a64SJohn Snow                      bool has_props,
218794d16a64SJohn Snow                      struct TransactionProperties *props,
218894d16a64SJohn Snow                      Error **errp)
21898802d1fdSJeff Cody {
2190c8a83e85SKevin Wolf     TransactionActionList *dev_entry = dev_list;
219194d16a64SJohn Snow     BlockJobTxn *block_job_txn = NULL;
219250f43f0fSJohn Snow     BlkActionState *state, *next;
219343e17041SLuiz Capitulino     Error *local_err = NULL;
21948802d1fdSJeff Cody 
219550f43f0fSJohn Snow     QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
21968802d1fdSJeff Cody     QSIMPLEQ_INIT(&snap_bdrv_states);
21978802d1fdSJeff Cody 
219894d16a64SJohn Snow     /* Does this transaction get canceled as a group on failure?
219994d16a64SJohn Snow      * If not, we don't really need to make a BlockJobTxn.
220094d16a64SJohn Snow      */
220194d16a64SJohn Snow     props = get_transaction_properties(props);
220294d16a64SJohn Snow     if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
220394d16a64SJohn Snow         block_job_txn = block_job_txn_new();
220494d16a64SJohn Snow     }
220594d16a64SJohn Snow 
2206b756b9ceSStefan Hajnoczi     /* drain all i/o before any operations */
22078802d1fdSJeff Cody     bdrv_drain_all();
22088802d1fdSJeff Cody 
2209b756b9ceSStefan Hajnoczi     /* We don't do anything in this loop that commits us to the operations */
22108802d1fdSJeff Cody     while (NULL != dev_entry) {
2211c8a83e85SKevin Wolf         TransactionAction *dev_info = NULL;
221250f43f0fSJohn Snow         const BlkActionOps *ops;
221352e7c241SPaolo Bonzini 
22148802d1fdSJeff Cody         dev_info = dev_entry->value;
22158802d1fdSJeff Cody         dev_entry = dev_entry->next;
22168802d1fdSJeff Cody 
22176a8f9661SEric Blake         assert(dev_info->type < ARRAY_SIZE(actions));
2218ba0c86a3SWenchao Xia 
22196a8f9661SEric Blake         ops = &actions[dev_info->type];
2220aa3fe714SMax Reitz         assert(ops->instance_size > 0);
2221aa3fe714SMax Reitz 
2222ba5d6ab6SStefan Hajnoczi         state = g_malloc0(ops->instance_size);
2223ba5d6ab6SStefan Hajnoczi         state->ops = ops;
2224ba5d6ab6SStefan Hajnoczi         state->action = dev_info;
222594d16a64SJohn Snow         state->block_job_txn = block_job_txn;
222694d16a64SJohn Snow         state->txn_props = props;
2227ba5d6ab6SStefan Hajnoczi         QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
22288802d1fdSJeff Cody 
2229ba5d6ab6SStefan Hajnoczi         state->ops->prepare(state, &local_err);
223084d18f06SMarkus Armbruster         if (local_err) {
22319b9877eeSWenchao Xia             error_propagate(errp, local_err);
22329b9877eeSWenchao Xia             goto delete_and_fail;
22339b9877eeSWenchao Xia         }
223452e7c241SPaolo Bonzini     }
22358802d1fdSJeff Cody 
2236ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2237f9ea81e8SStefan Hajnoczi         if (state->ops->commit) {
2238ba5d6ab6SStefan Hajnoczi             state->ops->commit(state);
22398802d1fdSJeff Cody         }
2240f9ea81e8SStefan Hajnoczi     }
22418802d1fdSJeff Cody 
22428802d1fdSJeff Cody     /* success */
22438802d1fdSJeff Cody     goto exit;
22448802d1fdSJeff Cody 
22458802d1fdSJeff Cody delete_and_fail:
2246b756b9ceSStefan Hajnoczi     /* failure, and it is all-or-none; roll back all operations */
2247ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2248ba5d6ab6SStefan Hajnoczi         if (state->ops->abort) {
2249ba5d6ab6SStefan Hajnoczi             state->ops->abort(state);
2250ba0c86a3SWenchao Xia         }
22518802d1fdSJeff Cody     }
22528802d1fdSJeff Cody exit:
2253ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2254ba5d6ab6SStefan Hajnoczi         if (state->ops->clean) {
2255ba5d6ab6SStefan Hajnoczi             state->ops->clean(state);
2256ba0c86a3SWenchao Xia         }
2257ba5d6ab6SStefan Hajnoczi         g_free(state);
22588802d1fdSJeff Cody     }
225994d16a64SJohn Snow     if (!has_props) {
226094d16a64SJohn Snow         qapi_free_TransactionProperties(props);
226194d16a64SJohn Snow     }
226294d16a64SJohn Snow     block_job_txn_unref(block_job_txn);
22638802d1fdSJeff Cody }
22648802d1fdSJeff Cody 
2265c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
2266666daa68SMarkus Armbruster {
226738f54bd1SMax Reitz     Error *local_err = NULL;
2268666daa68SMarkus Armbruster 
226938f54bd1SMax Reitz     qmp_blockdev_open_tray(device, has_force, force, &local_err);
227038f54bd1SMax Reitz     if (local_err) {
227138f54bd1SMax Reitz         error_propagate(errp, local_err);
2272c245b6a3SLuiz Capitulino         return;
2273666daa68SMarkus Armbruster     }
227492d48558SLuiz Capitulino 
22756e0abc25SMax Reitz     qmp_x_blockdev_remove_medium(device, errp);
2276666daa68SMarkus Armbruster }
2277666daa68SMarkus Armbruster 
227812d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device,
227912d3ba82SBenoît Canet                       bool has_node_name, const char *node_name,
228012d3ba82SBenoît Canet                       const char *password, Error **errp)
2281666daa68SMarkus Armbruster {
228212d3ba82SBenoît Canet     Error *local_err = NULL;
2283666daa68SMarkus Armbruster     BlockDriverState *bs;
2284e3442099SStefan Hajnoczi     AioContext *aio_context;
2285666daa68SMarkus Armbruster 
228612d3ba82SBenoît Canet     bs = bdrv_lookup_bs(has_device ? device : NULL,
228712d3ba82SBenoît Canet                         has_node_name ? node_name : NULL,
228812d3ba82SBenoît Canet                         &local_err);
228984d18f06SMarkus Armbruster     if (local_err) {
229012d3ba82SBenoît Canet         error_propagate(errp, local_err);
2291a4dea8a9SLuiz Capitulino         return;
2292666daa68SMarkus Armbruster     }
2293666daa68SMarkus Armbruster 
2294e3442099SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2295e3442099SStefan Hajnoczi     aio_context_acquire(aio_context);
2296e3442099SStefan Hajnoczi 
22974d2855a3SMarkus Armbruster     bdrv_add_key(bs, password, errp);
2298666daa68SMarkus Armbruster 
2299e3442099SStefan Hajnoczi     aio_context_release(aio_context);
2300e3442099SStefan Hajnoczi }
2301e3442099SStefan Hajnoczi 
23027d8a9f71SMax Reitz void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
23037d8a9f71SMax Reitz                             Error **errp)
23047d8a9f71SMax Reitz {
23057d8a9f71SMax Reitz     BlockBackend *blk;
23067d8a9f71SMax Reitz     bool locked;
23077d8a9f71SMax Reitz 
23087d8a9f71SMax Reitz     if (!has_force) {
23097d8a9f71SMax Reitz         force = false;
23107d8a9f71SMax Reitz     }
23117d8a9f71SMax Reitz 
23127d8a9f71SMax Reitz     blk = blk_by_name(device);
23137d8a9f71SMax Reitz     if (!blk) {
23147d8a9f71SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
23157d8a9f71SMax Reitz                   "Device '%s' not found", device);
23167d8a9f71SMax Reitz         return;
23177d8a9f71SMax Reitz     }
23187d8a9f71SMax Reitz 
23197d8a9f71SMax Reitz     if (!blk_dev_has_removable_media(blk)) {
23207d8a9f71SMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
23217d8a9f71SMax Reitz         return;
23227d8a9f71SMax Reitz     }
23237d8a9f71SMax Reitz 
232412c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
232512c7ec87SMax Reitz         /* Ignore this command on tray-less devices */
232612c7ec87SMax Reitz         return;
232712c7ec87SMax Reitz     }
232812c7ec87SMax Reitz 
23297d8a9f71SMax Reitz     if (blk_dev_is_tray_open(blk)) {
23307d8a9f71SMax Reitz         return;
23317d8a9f71SMax Reitz     }
23327d8a9f71SMax Reitz 
23337d8a9f71SMax Reitz     locked = blk_dev_is_medium_locked(blk);
23347d8a9f71SMax Reitz     if (locked) {
23357d8a9f71SMax Reitz         blk_dev_eject_request(blk, force);
23367d8a9f71SMax Reitz     }
23377d8a9f71SMax Reitz 
23387d8a9f71SMax Reitz     if (!locked || force) {
23397d8a9f71SMax Reitz         blk_dev_change_media_cb(blk, false);
23407d8a9f71SMax Reitz     }
23417d8a9f71SMax Reitz }
23427d8a9f71SMax Reitz 
2343abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp)
2344abaaf59dSMax Reitz {
2345abaaf59dSMax Reitz     BlockBackend *blk;
2346abaaf59dSMax Reitz 
2347abaaf59dSMax Reitz     blk = blk_by_name(device);
2348abaaf59dSMax Reitz     if (!blk) {
2349abaaf59dSMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2350abaaf59dSMax Reitz                   "Device '%s' not found", device);
2351abaaf59dSMax Reitz         return;
2352abaaf59dSMax Reitz     }
2353abaaf59dSMax Reitz 
2354abaaf59dSMax Reitz     if (!blk_dev_has_removable_media(blk)) {
2355abaaf59dSMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
2356abaaf59dSMax Reitz         return;
2357abaaf59dSMax Reitz     }
2358abaaf59dSMax Reitz 
235912c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
236012c7ec87SMax Reitz         /* Ignore this command on tray-less devices */
236112c7ec87SMax Reitz         return;
236212c7ec87SMax Reitz     }
236312c7ec87SMax Reitz 
2364abaaf59dSMax Reitz     if (!blk_dev_is_tray_open(blk)) {
2365abaaf59dSMax Reitz         return;
2366abaaf59dSMax Reitz     }
2367abaaf59dSMax Reitz 
2368abaaf59dSMax Reitz     blk_dev_change_media_cb(blk, true);
2369abaaf59dSMax Reitz }
2370abaaf59dSMax Reitz 
23716e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
23722814f672SMax Reitz {
23732814f672SMax Reitz     BlockBackend *blk;
23742814f672SMax Reitz     BlockDriverState *bs;
23752814f672SMax Reitz     AioContext *aio_context;
23762814f672SMax Reitz     bool has_device;
23772814f672SMax Reitz 
23782814f672SMax Reitz     blk = blk_by_name(device);
23792814f672SMax Reitz     if (!blk) {
23802814f672SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
23812814f672SMax Reitz                   "Device '%s' not found", device);
23822814f672SMax Reitz         return;
23832814f672SMax Reitz     }
23842814f672SMax Reitz 
23852814f672SMax Reitz     /* For BBs without a device, we can exchange the BDS tree at will */
23862814f672SMax Reitz     has_device = blk_get_attached_dev(blk);
23872814f672SMax Reitz 
23882814f672SMax Reitz     if (has_device && !blk_dev_has_removable_media(blk)) {
23892814f672SMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
23902814f672SMax Reitz         return;
23912814f672SMax Reitz     }
23922814f672SMax Reitz 
239312c7ec87SMax Reitz     if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
23942814f672SMax Reitz         error_setg(errp, "Tray of device '%s' is not open", device);
23952814f672SMax Reitz         return;
23962814f672SMax Reitz     }
23972814f672SMax Reitz 
23982814f672SMax Reitz     bs = blk_bs(blk);
23992814f672SMax Reitz     if (!bs) {
24002814f672SMax Reitz         return;
24012814f672SMax Reitz     }
24022814f672SMax Reitz 
24032814f672SMax Reitz     aio_context = bdrv_get_aio_context(bs);
24042814f672SMax Reitz     aio_context_acquire(aio_context);
24052814f672SMax Reitz 
24062814f672SMax Reitz     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
24072814f672SMax Reitz         goto out;
24082814f672SMax Reitz     }
24092814f672SMax Reitz 
24102814f672SMax Reitz     /* This follows the convention established by bdrv_make_anon() */
24112814f672SMax Reitz     if (bs->device_list.tqe_prev) {
2412f8aa905aSJeff Cody         bdrv_device_remove(bs);
24132814f672SMax Reitz     }
24142814f672SMax Reitz 
24152814f672SMax Reitz     blk_remove_bs(blk);
24162814f672SMax Reitz 
241712c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
241812c7ec87SMax Reitz         /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
241912c7ec87SMax Reitz          * called at all); therefore, the medium needs to be ejected here.
242012c7ec87SMax Reitz          * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
242112c7ec87SMax Reitz          * value passed here (i.e. false). */
242212c7ec87SMax Reitz         blk_dev_change_media_cb(blk, false);
242312c7ec87SMax Reitz     }
242412c7ec87SMax Reitz 
24252814f672SMax Reitz out:
24262814f672SMax Reitz     aio_context_release(aio_context);
24272814f672SMax Reitz }
24282814f672SMax Reitz 
2429d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device,
2430d1299882SMax Reitz                                             BlockDriverState *bs, Error **errp)
2431d1299882SMax Reitz {
2432d1299882SMax Reitz     BlockBackend *blk;
2433d1299882SMax Reitz     bool has_device;
2434d1299882SMax Reitz 
2435d1299882SMax Reitz     blk = blk_by_name(device);
2436d1299882SMax Reitz     if (!blk) {
2437d1299882SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2438d1299882SMax Reitz                   "Device '%s' not found", device);
2439d1299882SMax Reitz         return;
2440d1299882SMax Reitz     }
2441d1299882SMax Reitz 
2442d1299882SMax Reitz     /* For BBs without a device, we can exchange the BDS tree at will */
2443d1299882SMax Reitz     has_device = blk_get_attached_dev(blk);
2444d1299882SMax Reitz 
2445d1299882SMax Reitz     if (has_device && !blk_dev_has_removable_media(blk)) {
2446d1299882SMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
2447d1299882SMax Reitz         return;
2448d1299882SMax Reitz     }
2449d1299882SMax Reitz 
245012c7ec87SMax Reitz     if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2451d1299882SMax Reitz         error_setg(errp, "Tray of device '%s' is not open", device);
2452d1299882SMax Reitz         return;
2453d1299882SMax Reitz     }
2454d1299882SMax Reitz 
2455d1299882SMax Reitz     if (blk_bs(blk)) {
2456d1299882SMax Reitz         error_setg(errp, "There already is a medium in device '%s'", device);
2457d1299882SMax Reitz         return;
2458d1299882SMax Reitz     }
2459d1299882SMax Reitz 
2460d1299882SMax Reitz     blk_insert_bs(blk, bs);
2461d1299882SMax Reitz 
2462d1299882SMax Reitz     QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
246312c7ec87SMax Reitz 
246412c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
246512c7ec87SMax Reitz         /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
246612c7ec87SMax Reitz          * called at all); therefore, the medium needs to be pushed into the
246712c7ec87SMax Reitz          * slot here.
246812c7ec87SMax Reitz          * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
246912c7ec87SMax Reitz          * value passed here (i.e. true). */
247012c7ec87SMax Reitz         blk_dev_change_media_cb(blk, true);
247112c7ec87SMax Reitz     }
2472d1299882SMax Reitz }
2473d1299882SMax Reitz 
24746e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name,
2475d1299882SMax Reitz                                   Error **errp)
2476d1299882SMax Reitz {
2477d1299882SMax Reitz     BlockDriverState *bs;
2478d1299882SMax Reitz 
2479d1299882SMax Reitz     bs = bdrv_find_node(node_name);
2480d1299882SMax Reitz     if (!bs) {
2481d1299882SMax Reitz         error_setg(errp, "Node '%s' not found", node_name);
2482d1299882SMax Reitz         return;
2483d1299882SMax Reitz     }
2484d1299882SMax Reitz 
2485d1299882SMax Reitz     if (bs->blk) {
2486d1299882SMax Reitz         error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2487d1299882SMax Reitz                    blk_name(bs->blk));
2488d1299882SMax Reitz         return;
2489d1299882SMax Reitz     }
2490d1299882SMax Reitz 
2491d1299882SMax Reitz     qmp_blockdev_insert_anon_medium(device, bs, errp);
2492d1299882SMax Reitz }
2493d1299882SMax Reitz 
249424fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename,
249524fb4133SMax Reitz                                 bool has_format, const char *format,
249639ff43d9SMax Reitz                                 bool has_read_only,
249739ff43d9SMax Reitz                                 BlockdevChangeReadOnlyMode read_only,
249824fb4133SMax Reitz                                 Error **errp)
2499de2c6c05SMax Reitz {
2500de2c6c05SMax Reitz     BlockBackend *blk;
2501de2c6c05SMax Reitz     BlockDriverState *medium_bs = NULL;
2502de2c6c05SMax Reitz     int bdrv_flags, ret;
2503de2c6c05SMax Reitz     QDict *options = NULL;
2504de2c6c05SMax Reitz     Error *err = NULL;
2505de2c6c05SMax Reitz 
2506de2c6c05SMax Reitz     blk = blk_by_name(device);
2507de2c6c05SMax Reitz     if (!blk) {
2508de2c6c05SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2509de2c6c05SMax Reitz                   "Device '%s' not found", device);
2510de2c6c05SMax Reitz         goto fail;
2511de2c6c05SMax Reitz     }
2512de2c6c05SMax Reitz 
2513de2c6c05SMax Reitz     if (blk_bs(blk)) {
2514de2c6c05SMax Reitz         blk_update_root_state(blk);
2515de2c6c05SMax Reitz     }
2516de2c6c05SMax Reitz 
2517de2c6c05SMax Reitz     bdrv_flags = blk_get_open_flags_from_root_state(blk);
2518de2c6c05SMax Reitz 
251939ff43d9SMax Reitz     if (!has_read_only) {
252039ff43d9SMax Reitz         read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
252139ff43d9SMax Reitz     }
252239ff43d9SMax Reitz 
252339ff43d9SMax Reitz     switch (read_only) {
252439ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
252539ff43d9SMax Reitz         break;
252639ff43d9SMax Reitz 
252739ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
252839ff43d9SMax Reitz         bdrv_flags &= ~BDRV_O_RDWR;
252939ff43d9SMax Reitz         break;
253039ff43d9SMax Reitz 
253139ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
253239ff43d9SMax Reitz         bdrv_flags |= BDRV_O_RDWR;
253339ff43d9SMax Reitz         break;
253439ff43d9SMax Reitz 
253539ff43d9SMax Reitz     default:
253639ff43d9SMax Reitz         abort();
253739ff43d9SMax Reitz     }
253839ff43d9SMax Reitz 
253924fb4133SMax Reitz     if (has_format) {
2540de2c6c05SMax Reitz         options = qdict_new();
2541de2c6c05SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
2542de2c6c05SMax Reitz     }
2543de2c6c05SMax Reitz 
2544de2c6c05SMax Reitz     assert(!medium_bs);
2545de2c6c05SMax Reitz     ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2546de2c6c05SMax Reitz     if (ret < 0) {
2547de2c6c05SMax Reitz         goto fail;
2548de2c6c05SMax Reitz     }
2549de2c6c05SMax Reitz 
2550de2c6c05SMax Reitz     blk_apply_root_state(blk, medium_bs);
2551de2c6c05SMax Reitz 
2552de2c6c05SMax Reitz     bdrv_add_key(medium_bs, NULL, &err);
2553de2c6c05SMax Reitz     if (err) {
2554de2c6c05SMax Reitz         error_propagate(errp, err);
2555de2c6c05SMax Reitz         goto fail;
2556de2c6c05SMax Reitz     }
2557de2c6c05SMax Reitz 
2558de2c6c05SMax Reitz     qmp_blockdev_open_tray(device, false, false, &err);
2559de2c6c05SMax Reitz     if (err) {
2560de2c6c05SMax Reitz         error_propagate(errp, err);
2561de2c6c05SMax Reitz         goto fail;
2562de2c6c05SMax Reitz     }
2563de2c6c05SMax Reitz 
25646e0abc25SMax Reitz     qmp_x_blockdev_remove_medium(device, &err);
2565de2c6c05SMax Reitz     if (err) {
2566de2c6c05SMax Reitz         error_propagate(errp, err);
2567de2c6c05SMax Reitz         goto fail;
2568de2c6c05SMax Reitz     }
2569de2c6c05SMax Reitz 
2570de2c6c05SMax Reitz     qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2571de2c6c05SMax Reitz     if (err) {
2572de2c6c05SMax Reitz         error_propagate(errp, err);
2573de2c6c05SMax Reitz         goto fail;
2574de2c6c05SMax Reitz     }
2575de2c6c05SMax Reitz 
2576de2c6c05SMax Reitz     qmp_blockdev_close_tray(device, errp);
2577de2c6c05SMax Reitz 
2578de2c6c05SMax Reitz fail:
2579de2c6c05SMax Reitz     /* If the medium has been inserted, the device has its own reference, so
2580de2c6c05SMax Reitz      * ours must be relinquished; and if it has not been inserted successfully,
2581de2c6c05SMax Reitz      * the reference must be relinquished anyway */
2582de2c6c05SMax Reitz     bdrv_unref(medium_bs);
2583de2c6c05SMax Reitz }
2584de2c6c05SMax Reitz 
2585727f005eSZhi Yong Wu /* throttling disk I/O limits */
258680047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
25873e9fab69SBenoît Canet                                int64_t bps_wr,
25883e9fab69SBenoît Canet                                int64_t iops,
25893e9fab69SBenoît Canet                                int64_t iops_rd,
25903e9fab69SBenoît Canet                                int64_t iops_wr,
25913e9fab69SBenoît Canet                                bool has_bps_max,
25923e9fab69SBenoît Canet                                int64_t bps_max,
25933e9fab69SBenoît Canet                                bool has_bps_rd_max,
25943e9fab69SBenoît Canet                                int64_t bps_rd_max,
25953e9fab69SBenoît Canet                                bool has_bps_wr_max,
25963e9fab69SBenoît Canet                                int64_t bps_wr_max,
25973e9fab69SBenoît Canet                                bool has_iops_max,
25983e9fab69SBenoît Canet                                int64_t iops_max,
25993e9fab69SBenoît Canet                                bool has_iops_rd_max,
26003e9fab69SBenoît Canet                                int64_t iops_rd_max,
26013e9fab69SBenoît Canet                                bool has_iops_wr_max,
26022024c1dfSBenoît Canet                                int64_t iops_wr_max,
26032024c1dfSBenoît Canet                                bool has_iops_size,
260476f4afb4SAlberto Garcia                                int64_t iops_size,
260576f4afb4SAlberto Garcia                                bool has_group,
260676f4afb4SAlberto Garcia                                const char *group, Error **errp)
2607727f005eSZhi Yong Wu {
2608cc0681c4SBenoît Canet     ThrottleConfig cfg;
2609727f005eSZhi Yong Wu     BlockDriverState *bs;
2610a0e8544cSFam Zheng     BlockBackend *blk;
2611b15446fdSStefan Hajnoczi     AioContext *aio_context;
2612727f005eSZhi Yong Wu 
2613a0e8544cSFam Zheng     blk = blk_by_name(device);
2614a0e8544cSFam Zheng     if (!blk) {
261575158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
261675158ebbSMarkus Armbruster                   "Device '%s' not found", device);
261780047da5SLuiz Capitulino         return;
2618727f005eSZhi Yong Wu     }
26195433c24fSMax Reitz 
26205433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
26215433c24fSMax Reitz     aio_context_acquire(aio_context);
26225433c24fSMax Reitz 
2623a0e8544cSFam Zheng     bs = blk_bs(blk);
26245433c24fSMax Reitz     if (!bs) {
26255433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
26265433c24fSMax Reitz         goto out;
26275433c24fSMax Reitz     }
2628727f005eSZhi Yong Wu 
2629cc0681c4SBenoît Canet     memset(&cfg, 0, sizeof(cfg));
2630cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2631cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_READ].avg  = bps_rd;
2632cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2633727f005eSZhi Yong Wu 
2634cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2635cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_READ].avg  = iops_rd;
2636cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2637cc0681c4SBenoît Canet 
26383e9fab69SBenoît Canet     if (has_bps_max) {
26393e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
26403e9fab69SBenoît Canet     }
26413e9fab69SBenoît Canet     if (has_bps_rd_max) {
26423e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
26433e9fab69SBenoît Canet     }
26443e9fab69SBenoît Canet     if (has_bps_wr_max) {
26453e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
26463e9fab69SBenoît Canet     }
26473e9fab69SBenoît Canet     if (has_iops_max) {
26483e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
26493e9fab69SBenoît Canet     }
26503e9fab69SBenoît Canet     if (has_iops_rd_max) {
26513e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
26523e9fab69SBenoît Canet     }
26533e9fab69SBenoît Canet     if (has_iops_wr_max) {
26543e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
26553e9fab69SBenoît Canet     }
2656cc0681c4SBenoît Canet 
26572024c1dfSBenoît Canet     if (has_iops_size) {
26582024c1dfSBenoît Canet         cfg.op_size = iops_size;
26592024c1dfSBenoît Canet     }
2660cc0681c4SBenoît Canet 
2661cc0681c4SBenoît Canet     if (!check_throttle_config(&cfg, errp)) {
26625433c24fSMax Reitz         goto out;
2663727f005eSZhi Yong Wu     }
2664727f005eSZhi Yong Wu 
266576f4afb4SAlberto Garcia     if (throttle_enabled(&cfg)) {
266676f4afb4SAlberto Garcia         /* Enable I/O limits if they're not enabled yet, otherwise
266776f4afb4SAlberto Garcia          * just update the throttling group. */
2668a0d64a61SAlberto Garcia         if (!bs->throttle_state) {
266976f4afb4SAlberto Garcia             bdrv_io_limits_enable(bs, has_group ? group : device);
267076f4afb4SAlberto Garcia         } else if (has_group) {
267176f4afb4SAlberto Garcia             bdrv_io_limits_update_group(bs, group);
2672727f005eSZhi Yong Wu         }
267376f4afb4SAlberto Garcia         /* Set the new throttling configuration */
2674cc0681c4SBenoît Canet         bdrv_set_io_limits(bs, &cfg);
2675a0d64a61SAlberto Garcia     } else if (bs->throttle_state) {
267676f4afb4SAlberto Garcia         /* If all throttling settings are set to 0, disable I/O limits */
267776f4afb4SAlberto Garcia         bdrv_io_limits_disable(bs);
2678727f005eSZhi Yong Wu     }
2679b15446fdSStefan Hajnoczi 
26805433c24fSMax Reitz out:
2681b15446fdSStefan Hajnoczi     aio_context_release(aio_context);
2682727f005eSZhi Yong Wu }
2683727f005eSZhi Yong Wu 
2684341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2685341ebc2fSJohn Snow                                 bool has_granularity, uint32_t granularity,
2686341ebc2fSJohn Snow                                 Error **errp)
2687341ebc2fSJohn Snow {
2688341ebc2fSJohn Snow     AioContext *aio_context;
2689341ebc2fSJohn Snow     BlockDriverState *bs;
2690341ebc2fSJohn Snow 
2691341ebc2fSJohn Snow     if (!name || name[0] == '\0') {
2692341ebc2fSJohn Snow         error_setg(errp, "Bitmap name cannot be empty");
2693341ebc2fSJohn Snow         return;
2694341ebc2fSJohn Snow     }
2695341ebc2fSJohn Snow 
2696341ebc2fSJohn Snow     bs = bdrv_lookup_bs(node, node, errp);
2697341ebc2fSJohn Snow     if (!bs) {
2698341ebc2fSJohn Snow         return;
2699341ebc2fSJohn Snow     }
2700341ebc2fSJohn Snow 
2701341ebc2fSJohn Snow     aio_context = bdrv_get_aio_context(bs);
2702341ebc2fSJohn Snow     aio_context_acquire(aio_context);
2703341ebc2fSJohn Snow 
2704341ebc2fSJohn Snow     if (has_granularity) {
2705341ebc2fSJohn Snow         if (granularity < 512 || !is_power_of_2(granularity)) {
2706341ebc2fSJohn Snow             error_setg(errp, "Granularity must be power of 2 "
2707341ebc2fSJohn Snow                              "and at least 512");
2708341ebc2fSJohn Snow             goto out;
2709341ebc2fSJohn Snow         }
2710341ebc2fSJohn Snow     } else {
2711341ebc2fSJohn Snow         /* Default to cluster size, if available: */
2712341ebc2fSJohn Snow         granularity = bdrv_get_default_bitmap_granularity(bs);
2713341ebc2fSJohn Snow     }
2714341ebc2fSJohn Snow 
2715341ebc2fSJohn Snow     bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2716341ebc2fSJohn Snow 
2717341ebc2fSJohn Snow  out:
2718341ebc2fSJohn Snow     aio_context_release(aio_context);
2719341ebc2fSJohn Snow }
2720341ebc2fSJohn Snow 
2721341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2722341ebc2fSJohn Snow                                    Error **errp)
2723341ebc2fSJohn Snow {
2724341ebc2fSJohn Snow     AioContext *aio_context;
2725341ebc2fSJohn Snow     BlockDriverState *bs;
2726341ebc2fSJohn Snow     BdrvDirtyBitmap *bitmap;
2727341ebc2fSJohn Snow 
2728341ebc2fSJohn Snow     bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2729341ebc2fSJohn Snow     if (!bitmap || !bs) {
2730341ebc2fSJohn Snow         return;
2731341ebc2fSJohn Snow     }
2732341ebc2fSJohn Snow 
27339bd2b08fSJohn Snow     if (bdrv_dirty_bitmap_frozen(bitmap)) {
27349bd2b08fSJohn Snow         error_setg(errp,
27359bd2b08fSJohn Snow                    "Bitmap '%s' is currently frozen and cannot be removed",
27369bd2b08fSJohn Snow                    name);
27379bd2b08fSJohn Snow         goto out;
27389bd2b08fSJohn Snow     }
273920dca810SJohn Snow     bdrv_dirty_bitmap_make_anon(bitmap);
2740341ebc2fSJohn Snow     bdrv_release_dirty_bitmap(bs, bitmap);
2741341ebc2fSJohn Snow 
27429bd2b08fSJohn Snow  out:
2743341ebc2fSJohn Snow     aio_context_release(aio_context);
2744341ebc2fSJohn Snow }
2745341ebc2fSJohn Snow 
2746e74e6b78SJohn Snow /**
2747e74e6b78SJohn Snow  * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2748e74e6b78SJohn Snow  * immediately after a full backup operation.
2749e74e6b78SJohn Snow  */
2750e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2751e74e6b78SJohn Snow                                   Error **errp)
2752e74e6b78SJohn Snow {
2753e74e6b78SJohn Snow     AioContext *aio_context;
2754e74e6b78SJohn Snow     BdrvDirtyBitmap *bitmap;
2755e74e6b78SJohn Snow     BlockDriverState *bs;
2756e74e6b78SJohn Snow 
2757e74e6b78SJohn Snow     bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2758e74e6b78SJohn Snow     if (!bitmap || !bs) {
2759e74e6b78SJohn Snow         return;
2760e74e6b78SJohn Snow     }
2761e74e6b78SJohn Snow 
2762e74e6b78SJohn Snow     if (bdrv_dirty_bitmap_frozen(bitmap)) {
2763e74e6b78SJohn Snow         error_setg(errp,
2764e74e6b78SJohn Snow                    "Bitmap '%s' is currently frozen and cannot be modified",
2765e74e6b78SJohn Snow                    name);
2766e74e6b78SJohn Snow         goto out;
2767e74e6b78SJohn Snow     } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2768e74e6b78SJohn Snow         error_setg(errp,
2769e74e6b78SJohn Snow                    "Bitmap '%s' is currently disabled and cannot be cleared",
2770e74e6b78SJohn Snow                    name);
2771e74e6b78SJohn Snow         goto out;
2772e74e6b78SJohn Snow     }
2773e74e6b78SJohn Snow 
2774df9a681dSFam Zheng     bdrv_clear_dirty_bitmap(bitmap, NULL);
2775e74e6b78SJohn Snow 
2776e74e6b78SJohn Snow  out:
2777e74e6b78SJohn Snow     aio_context_release(aio_context);
2778e74e6b78SJohn Snow }
2779e74e6b78SJohn Snow 
2780072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict)
27819063f814SRyan Harper {
27829063f814SRyan Harper     const char *id = qdict_get_str(qdict, "id");
27837e7d56d9SMarkus Armbruster     BlockBackend *blk;
27849063f814SRyan Harper     BlockDriverState *bs;
27858ad4202bSStefan Hajnoczi     AioContext *aio_context;
27863718d8abSFam Zheng     Error *local_err = NULL;
27879063f814SRyan Harper 
27887e7d56d9SMarkus Armbruster     blk = blk_by_name(id);
27897e7d56d9SMarkus Armbruster     if (!blk) {
2790b1422f20SMarkus Armbruster         error_report("Device '%s' not found", id);
2791072ebe6bSMarkus Armbruster         return;
27929063f814SRyan Harper     }
27938ad4202bSStefan Hajnoczi 
279426f8b3a8SMarkus Armbruster     if (!blk_legacy_dinfo(blk)) {
279548f364ddSMarkus Armbruster         error_report("Deleting device added with blockdev-add"
279648f364ddSMarkus Armbruster                      " is not supported");
2797072ebe6bSMarkus Armbruster         return;
279848f364ddSMarkus Armbruster     }
279948f364ddSMarkus Armbruster 
28005433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
28018ad4202bSStefan Hajnoczi     aio_context_acquire(aio_context);
28028ad4202bSStefan Hajnoczi 
28035433c24fSMax Reitz     bs = blk_bs(blk);
28045433c24fSMax Reitz     if (bs) {
28053718d8abSFam Zheng         if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2806565f65d2SMarkus Armbruster             error_report_err(local_err);
28078ad4202bSStefan Hajnoczi             aio_context_release(aio_context);
2808072ebe6bSMarkus Armbruster             return;
28098591675fSMarcelo Tosatti         }
28109063f814SRyan Harper 
2811938abd43SMax Reitz         blk_remove_bs(blk);
28125433c24fSMax Reitz     }
28139063f814SRyan Harper 
2814fa879d62SMarkus Armbruster     /* if we have a device attached to this BlockDriverState
2815d22b2f41SRyan Harper      * then we need to make the drive anonymous until the device
2816d22b2f41SRyan Harper      * can be removed.  If this is a drive with no device backing
2817d22b2f41SRyan Harper      * then we can just get rid of the block driver state right here.
2818d22b2f41SRyan Harper      */
2819a7f53e26SMarkus Armbruster     if (blk_get_attached_dev(blk)) {
28203e5a50d6SMarkus Armbruster         blk_hide_on_behalf_of_hmp_drive_del(blk);
2821293c51a6SStefan Hajnoczi         /* Further I/O must not pause the guest */
2822373340b2SMax Reitz         blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2823293c51a6SStefan Hajnoczi                          BLOCKDEV_ON_ERROR_REPORT);
2824d22b2f41SRyan Harper     } else {
2825b9fe8a7aSMarkus Armbruster         blk_unref(blk);
2826d22b2f41SRyan Harper     }
28279063f814SRyan Harper 
28288ad4202bSStefan Hajnoczi     aio_context_release(aio_context);
28299063f814SRyan Harper }
28306d4a2b3aSChristoph Hellwig 
28313b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device,
28323b1dbd11SBenoît Canet                       bool has_node_name, const char *node_name,
28333b1dbd11SBenoît Canet                       int64_t size, Error **errp)
28346d4a2b3aSChristoph Hellwig {
28353b1dbd11SBenoît Canet     Error *local_err = NULL;
28366d4a2b3aSChristoph Hellwig     BlockDriverState *bs;
2837927e0e76SStefan Hajnoczi     AioContext *aio_context;
28388732901eSKevin Wolf     int ret;
28396d4a2b3aSChristoph Hellwig 
28403b1dbd11SBenoît Canet     bs = bdrv_lookup_bs(has_device ? device : NULL,
28413b1dbd11SBenoît Canet                         has_node_name ? node_name : NULL,
28423b1dbd11SBenoît Canet                         &local_err);
284384d18f06SMarkus Armbruster     if (local_err) {
28443b1dbd11SBenoît Canet         error_propagate(errp, local_err);
28453b1dbd11SBenoît Canet         return;
28463b1dbd11SBenoît Canet     }
28473b1dbd11SBenoît Canet 
2848927e0e76SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2849927e0e76SStefan Hajnoczi     aio_context_acquire(aio_context);
2850927e0e76SStefan Hajnoczi 
28513b1dbd11SBenoît Canet     if (!bdrv_is_first_non_filter(bs)) {
2852c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2853927e0e76SStefan Hajnoczi         goto out;
28546d4a2b3aSChristoph Hellwig     }
28556d4a2b3aSChristoph Hellwig 
28566d4a2b3aSChristoph Hellwig     if (size < 0) {
2857c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2858927e0e76SStefan Hajnoczi         goto out;
28596d4a2b3aSChristoph Hellwig     }
28606d4a2b3aSChristoph Hellwig 
28619c75e168SJeff Cody     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2862c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_IN_USE, device);
2863927e0e76SStefan Hajnoczi         goto out;
28649c75e168SJeff Cody     }
28659c75e168SJeff Cody 
286692b7a08dSPeter Lieven     /* complete all in-flight operations before resizing the device */
286792b7a08dSPeter Lieven     bdrv_drain_all();
286892b7a08dSPeter Lieven 
28698732901eSKevin Wolf     ret = bdrv_truncate(bs, size);
28708732901eSKevin Wolf     switch (ret) {
2871939a1cc3SStefan Hajnoczi     case 0:
2872939a1cc3SStefan Hajnoczi         break;
2873939a1cc3SStefan Hajnoczi     case -ENOMEDIUM:
2874c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2875939a1cc3SStefan Hajnoczi         break;
2876939a1cc3SStefan Hajnoczi     case -ENOTSUP:
2877c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_UNSUPPORTED);
2878939a1cc3SStefan Hajnoczi         break;
2879939a1cc3SStefan Hajnoczi     case -EACCES:
288081e5f78aSAlberto Garcia         error_setg(errp, "Device '%s' is read only", device);
2881939a1cc3SStefan Hajnoczi         break;
2882939a1cc3SStefan Hajnoczi     case -EBUSY:
2883c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_IN_USE, device);
2884939a1cc3SStefan Hajnoczi         break;
2885939a1cc3SStefan Hajnoczi     default:
28868732901eSKevin Wolf         error_setg_errno(errp, -ret, "Could not resize");
2887939a1cc3SStefan Hajnoczi         break;
28886d4a2b3aSChristoph Hellwig     }
2889927e0e76SStefan Hajnoczi 
2890927e0e76SStefan Hajnoczi out:
2891927e0e76SStefan Hajnoczi     aio_context_release(aio_context);
28926d4a2b3aSChristoph Hellwig }
289312bd451fSStefan Hajnoczi 
28949abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret)
289512bd451fSStefan Hajnoczi {
2896723c5d93SStefan Hajnoczi     /* Note that this function may be executed from another AioContext besides
2897723c5d93SStefan Hajnoczi      * the QEMU main loop.  If you need to access anything that assumes the
2898723c5d93SStefan Hajnoczi      * QEMU global mutex, use a BH or introduce a mutex.
2899723c5d93SStefan Hajnoczi      */
2900723c5d93SStefan Hajnoczi 
290112bd451fSStefan Hajnoczi     BlockDriverState *bs = opaque;
2902bcada37bSWenchao Xia     const char *msg = NULL;
290312bd451fSStefan Hajnoczi 
29049abf2dbaSJeff Cody     trace_block_job_cb(bs, bs->job, ret);
290512bd451fSStefan Hajnoczi 
290612bd451fSStefan Hajnoczi     assert(bs->job);
2907bcada37bSWenchao Xia 
290812bd451fSStefan Hajnoczi     if (ret < 0) {
2909bcada37bSWenchao Xia         msg = strerror(-ret);
291012bd451fSStefan Hajnoczi     }
291112bd451fSStefan Hajnoczi 
2912370521a1SStefan Hajnoczi     if (block_job_is_cancelled(bs->job)) {
2913bcada37bSWenchao Xia         block_job_event_cancelled(bs->job);
2914370521a1SStefan Hajnoczi     } else {
2915bcada37bSWenchao Xia         block_job_event_completed(bs->job, msg);
2916370521a1SStefan Hajnoczi     }
291712bd451fSStefan Hajnoczi }
291812bd451fSStefan Hajnoczi 
291913d8cc51SJeff Cody void qmp_block_stream(const char *device,
292013d8cc51SJeff Cody                       bool has_base, const char *base,
292113d8cc51SJeff Cody                       bool has_backing_file, const char *backing_file,
292213d8cc51SJeff Cody                       bool has_speed, int64_t speed,
29231d809098SPaolo Bonzini                       bool has_on_error, BlockdevOnError on_error,
29241d809098SPaolo Bonzini                       Error **errp)
292512bd451fSStefan Hajnoczi {
2926a0e8544cSFam Zheng     BlockBackend *blk;
292712bd451fSStefan Hajnoczi     BlockDriverState *bs;
2928c8c3080fSMarcelo Tosatti     BlockDriverState *base_bs = NULL;
2929f3e69bebSStefan Hajnoczi     AioContext *aio_context;
2930fd7f8c65SStefan Hajnoczi     Error *local_err = NULL;
293113d8cc51SJeff Cody     const char *base_name = NULL;
293212bd451fSStefan Hajnoczi 
29331d809098SPaolo Bonzini     if (!has_on_error) {
29341d809098SPaolo Bonzini         on_error = BLOCKDEV_ON_ERROR_REPORT;
29351d809098SPaolo Bonzini     }
29361d809098SPaolo Bonzini 
2937a0e8544cSFam Zheng     blk = blk_by_name(device);
2938a0e8544cSFam Zheng     if (!blk) {
293975158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
294075158ebbSMarkus Armbruster                   "Device '%s' not found", device);
294112bd451fSStefan Hajnoczi         return;
294212bd451fSStefan Hajnoczi     }
294312bd451fSStefan Hajnoczi 
29445433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
2945f3e69bebSStefan Hajnoczi     aio_context_acquire(aio_context);
2946f3e69bebSStefan Hajnoczi 
29475433c24fSMax Reitz     if (!blk_is_available(blk)) {
29485433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
29495433c24fSMax Reitz         goto out;
29505433c24fSMax Reitz     }
29515433c24fSMax Reitz     bs = blk_bs(blk);
29525433c24fSMax Reitz 
2953628ff683SFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2954f3e69bebSStefan Hajnoczi         goto out;
2955628ff683SFam Zheng     }
2956628ff683SFam Zheng 
295713d8cc51SJeff Cody     if (has_base) {
2958c8c3080fSMarcelo Tosatti         base_bs = bdrv_find_backing_image(bs, base);
2959c8c3080fSMarcelo Tosatti         if (base_bs == NULL) {
2960c6bd8c70SMarkus Armbruster             error_setg(errp, QERR_BASE_NOT_FOUND, base);
2961f3e69bebSStefan Hajnoczi             goto out;
296212bd451fSStefan Hajnoczi         }
2963f3e69bebSStefan Hajnoczi         assert(bdrv_get_aio_context(base_bs) == aio_context);
296413d8cc51SJeff Cody         base_name = base;
2965c8c3080fSMarcelo Tosatti     }
296612bd451fSStefan Hajnoczi 
296713d8cc51SJeff Cody     /* if we are streaming the entire chain, the result will have no backing
296813d8cc51SJeff Cody      * file, and specifying one is therefore an error */
296913d8cc51SJeff Cody     if (base_bs == NULL && has_backing_file) {
297013d8cc51SJeff Cody         error_setg(errp, "backing file specified, but streaming the "
297113d8cc51SJeff Cody                          "entire chain");
2972f3e69bebSStefan Hajnoczi         goto out;
297313d8cc51SJeff Cody     }
297413d8cc51SJeff Cody 
297513d8cc51SJeff Cody     /* backing_file string overrides base bs filename */
297613d8cc51SJeff Cody     base_name = has_backing_file ? backing_file : base_name;
297713d8cc51SJeff Cody 
297813d8cc51SJeff Cody     stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
29791d809098SPaolo Bonzini                  on_error, block_job_cb, bs, &local_err);
298084d18f06SMarkus Armbruster     if (local_err) {
2981fd7f8c65SStefan Hajnoczi         error_propagate(errp, local_err);
2982f3e69bebSStefan Hajnoczi         goto out;
298312bd451fSStefan Hajnoczi     }
298412bd451fSStefan Hajnoczi 
298512bd451fSStefan Hajnoczi     trace_qmp_block_stream(bs, bs->job);
2986f3e69bebSStefan Hajnoczi 
2987f3e69bebSStefan Hajnoczi out:
2988f3e69bebSStefan Hajnoczi     aio_context_release(aio_context);
298912bd451fSStefan Hajnoczi }
29902d47c6e9SStefan Hajnoczi 
2991ed61fc10SJeff Cody void qmp_block_commit(const char *device,
29927676e2c5SJeff Cody                       bool has_base, const char *base,
29937676e2c5SJeff Cody                       bool has_top, const char *top,
299454e26900SJeff Cody                       bool has_backing_file, const char *backing_file,
2995ed61fc10SJeff Cody                       bool has_speed, int64_t speed,
2996ed61fc10SJeff Cody                       Error **errp)
2997ed61fc10SJeff Cody {
2998a0e8544cSFam Zheng     BlockBackend *blk;
2999ed61fc10SJeff Cody     BlockDriverState *bs;
3000ed61fc10SJeff Cody     BlockDriverState *base_bs, *top_bs;
30019e85cd5cSStefan Hajnoczi     AioContext *aio_context;
3002ed61fc10SJeff Cody     Error *local_err = NULL;
3003ed61fc10SJeff Cody     /* This will be part of the QMP command, if/when the
3004ed61fc10SJeff Cody      * BlockdevOnError change for blkmirror makes it in
3005ed61fc10SJeff Cody      */
300692aa5c6dSPaolo Bonzini     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
3007ed61fc10SJeff Cody 
300854504663SMax Reitz     if (!has_speed) {
300954504663SMax Reitz         speed = 0;
301054504663SMax Reitz     }
301154504663SMax Reitz 
30127676e2c5SJeff Cody     /* Important Note:
30137676e2c5SJeff Cody      *  libvirt relies on the DeviceNotFound error class in order to probe for
30147676e2c5SJeff Cody      *  live commit feature versions; for this to work, we must make sure to
30157676e2c5SJeff Cody      *  perform the device lookup before any generic errors that may occur in a
30167676e2c5SJeff Cody      *  scenario in which all optional arguments are omitted. */
3017a0e8544cSFam Zheng     blk = blk_by_name(device);
3018a0e8544cSFam Zheng     if (!blk) {
301975158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
302075158ebbSMarkus Armbruster                   "Device '%s' not found", device);
3021ed61fc10SJeff Cody         return;
3022ed61fc10SJeff Cody     }
3023ed61fc10SJeff Cody 
30245433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
30259e85cd5cSStefan Hajnoczi     aio_context_acquire(aio_context);
30269e85cd5cSStefan Hajnoczi 
30275433c24fSMax Reitz     if (!blk_is_available(blk)) {
30285433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
30295433c24fSMax Reitz         goto out;
30305433c24fSMax Reitz     }
30315433c24fSMax Reitz     bs = blk_bs(blk);
30325433c24fSMax Reitz 
3033bb00021dSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
30349e85cd5cSStefan Hajnoczi         goto out;
3035628ff683SFam Zheng     }
3036628ff683SFam Zheng 
3037ed61fc10SJeff Cody     /* default top_bs is the active layer */
3038ed61fc10SJeff Cody     top_bs = bs;
3039ed61fc10SJeff Cody 
30407676e2c5SJeff Cody     if (has_top && top) {
3041ed61fc10SJeff Cody         if (strcmp(bs->filename, top) != 0) {
3042ed61fc10SJeff Cody             top_bs = bdrv_find_backing_image(bs, top);
3043ed61fc10SJeff Cody         }
3044ed61fc10SJeff Cody     }
3045ed61fc10SJeff Cody 
3046ed61fc10SJeff Cody     if (top_bs == NULL) {
3047ed61fc10SJeff Cody         error_setg(errp, "Top image file %s not found", top ? top : "NULL");
30489e85cd5cSStefan Hajnoczi         goto out;
3049ed61fc10SJeff Cody     }
3050ed61fc10SJeff Cody 
30519e85cd5cSStefan Hajnoczi     assert(bdrv_get_aio_context(top_bs) == aio_context);
30529e85cd5cSStefan Hajnoczi 
3053d5208c45SJeff Cody     if (has_base && base) {
3054d5208c45SJeff Cody         base_bs = bdrv_find_backing_image(top_bs, base);
3055d5208c45SJeff Cody     } else {
3056d5208c45SJeff Cody         base_bs = bdrv_find_base(top_bs);
3057d5208c45SJeff Cody     }
3058d5208c45SJeff Cody 
3059d5208c45SJeff Cody     if (base_bs == NULL) {
3060c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
30619e85cd5cSStefan Hajnoczi         goto out;
3062d5208c45SJeff Cody     }
3063d5208c45SJeff Cody 
30649e85cd5cSStefan Hajnoczi     assert(bdrv_get_aio_context(base_bs) == aio_context);
30659e85cd5cSStefan Hajnoczi 
3066bb00021dSFam Zheng     if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3067bb00021dSFam Zheng         goto out;
3068bb00021dSFam Zheng     }
3069bb00021dSFam Zheng 
30707676e2c5SJeff Cody     /* Do not allow attempts to commit an image into itself */
30717676e2c5SJeff Cody     if (top_bs == base_bs) {
30727676e2c5SJeff Cody         error_setg(errp, "cannot commit an image into itself");
30739e85cd5cSStefan Hajnoczi         goto out;
30747676e2c5SJeff Cody     }
30757676e2c5SJeff Cody 
307620a63d2cSFam Zheng     if (top_bs == bs) {
307754e26900SJeff Cody         if (has_backing_file) {
307854e26900SJeff Cody             error_setg(errp, "'backing-file' specified,"
307954e26900SJeff Cody                              " but 'top' is the active layer");
30809e85cd5cSStefan Hajnoczi             goto out;
308154e26900SJeff Cody         }
308220a63d2cSFam Zheng         commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
308320a63d2cSFam Zheng                             bs, &local_err);
308420a63d2cSFam Zheng     } else {
3085ed61fc10SJeff Cody         commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
308654e26900SJeff Cody                      has_backing_file ? backing_file : NULL, &local_err);
308720a63d2cSFam Zheng     }
3088ed61fc10SJeff Cody     if (local_err != NULL) {
3089ed61fc10SJeff Cody         error_propagate(errp, local_err);
30909e85cd5cSStefan Hajnoczi         goto out;
3091ed61fc10SJeff Cody     }
30929e85cd5cSStefan Hajnoczi 
30939e85cd5cSStefan Hajnoczi out:
30949e85cd5cSStefan Hajnoczi     aio_context_release(aio_context);
3095ed61fc10SJeff Cody }
3096ed61fc10SJeff Cody 
309778f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target,
309899a9addfSStefan Hajnoczi                             bool has_format, const char *format,
3099b53169eaSStefan Hajnoczi                             enum MirrorSyncMode sync,
310099a9addfSStefan Hajnoczi                             bool has_mode, enum NewImageMode mode,
310199a9addfSStefan Hajnoczi                             bool has_speed, int64_t speed,
3102d58d8453SJohn Snow                             bool has_bitmap, const char *bitmap,
310378f51fdeSJohn Snow                             bool has_on_source_error,
310478f51fdeSJohn Snow                             BlockdevOnError on_source_error,
310578f51fdeSJohn Snow                             bool has_on_target_error,
310678f51fdeSJohn Snow                             BlockdevOnError on_target_error,
310778f51fdeSJohn Snow                             BlockJobTxn *txn, Error **errp)
310899a9addfSStefan Hajnoczi {
3109a0e8544cSFam Zheng     BlockBackend *blk;
311099a9addfSStefan Hajnoczi     BlockDriverState *bs;
311199a9addfSStefan Hajnoczi     BlockDriverState *target_bs;
3112fc5d3f84SIan Main     BlockDriverState *source = NULL;
3113d58d8453SJohn Snow     BdrvDirtyBitmap *bmap = NULL;
3114761731b1SStefan Hajnoczi     AioContext *aio_context;
3115e6641719SMax Reitz     QDict *options = NULL;
311699a9addfSStefan Hajnoczi     Error *local_err = NULL;
311799a9addfSStefan Hajnoczi     int flags;
311899a9addfSStefan Hajnoczi     int64_t size;
311999a9addfSStefan Hajnoczi     int ret;
312099a9addfSStefan Hajnoczi 
312199a9addfSStefan Hajnoczi     if (!has_speed) {
312299a9addfSStefan Hajnoczi         speed = 0;
312399a9addfSStefan Hajnoczi     }
312499a9addfSStefan Hajnoczi     if (!has_on_source_error) {
312599a9addfSStefan Hajnoczi         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
312699a9addfSStefan Hajnoczi     }
312799a9addfSStefan Hajnoczi     if (!has_on_target_error) {
312899a9addfSStefan Hajnoczi         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
312999a9addfSStefan Hajnoczi     }
313099a9addfSStefan Hajnoczi     if (!has_mode) {
313199a9addfSStefan Hajnoczi         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
313299a9addfSStefan Hajnoczi     }
313399a9addfSStefan Hajnoczi 
3134a0e8544cSFam Zheng     blk = blk_by_name(device);
3135a0e8544cSFam Zheng     if (!blk) {
313675158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
313775158ebbSMarkus Armbruster                   "Device '%s' not found", device);
313899a9addfSStefan Hajnoczi         return;
313999a9addfSStefan Hajnoczi     }
314099a9addfSStefan Hajnoczi 
31415433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
3142761731b1SStefan Hajnoczi     aio_context_acquire(aio_context);
3143761731b1SStefan Hajnoczi 
3144c29c1dd3SFam Zheng     /* Although backup_run has this check too, we need to use bs->drv below, so
3145c29c1dd3SFam Zheng      * do an early check redundantly. */
31465433c24fSMax Reitz     if (!blk_is_available(blk)) {
3147c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3148761731b1SStefan Hajnoczi         goto out;
314999a9addfSStefan Hajnoczi     }
31505433c24fSMax Reitz     bs = blk_bs(blk);
315199a9addfSStefan Hajnoczi 
315299a9addfSStefan Hajnoczi     if (!has_format) {
315399a9addfSStefan Hajnoczi         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
315499a9addfSStefan Hajnoczi     }
315599a9addfSStefan Hajnoczi 
3156c29c1dd3SFam Zheng     /* Early check to avoid creating target */
31573718d8abSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
3158761731b1SStefan Hajnoczi         goto out;
315999a9addfSStefan Hajnoczi     }
316099a9addfSStefan Hajnoczi 
316199a9addfSStefan Hajnoczi     flags = bs->open_flags | BDRV_O_RDWR;
316299a9addfSStefan Hajnoczi 
3163fc5d3f84SIan Main     /* See if we have a backing HD we can use to create our new image
3164fc5d3f84SIan Main      * on top of. */
3165fc5d3f84SIan Main     if (sync == MIRROR_SYNC_MODE_TOP) {
3166760e0063SKevin Wolf         source = backing_bs(bs);
3167fc5d3f84SIan Main         if (!source) {
3168fc5d3f84SIan Main             sync = MIRROR_SYNC_MODE_FULL;
3169fc5d3f84SIan Main         }
3170fc5d3f84SIan Main     }
3171fc5d3f84SIan Main     if (sync == MIRROR_SYNC_MODE_NONE) {
3172fc5d3f84SIan Main         source = bs;
3173fc5d3f84SIan Main     }
3174fc5d3f84SIan Main 
317599a9addfSStefan Hajnoczi     size = bdrv_getlength(bs);
317699a9addfSStefan Hajnoczi     if (size < 0) {
317799a9addfSStefan Hajnoczi         error_setg_errno(errp, -size, "bdrv_getlength failed");
3178761731b1SStefan Hajnoczi         goto out;
317999a9addfSStefan Hajnoczi     }
318099a9addfSStefan Hajnoczi 
318199a9addfSStefan Hajnoczi     if (mode != NEW_IMAGE_MODE_EXISTING) {
3182e6641719SMax Reitz         assert(format);
3183fc5d3f84SIan Main         if (source) {
3184fc5d3f84SIan Main             bdrv_img_create(target, format, source->filename,
3185fc5d3f84SIan Main                             source->drv->format_name, NULL,
3186fc5d3f84SIan Main                             size, flags, &local_err, false);
3187fc5d3f84SIan Main         } else {
3188fc5d3f84SIan Main             bdrv_img_create(target, format, NULL, NULL, NULL,
3189fc5d3f84SIan Main                             size, flags, &local_err, false);
3190fc5d3f84SIan Main         }
319199a9addfSStefan Hajnoczi     }
319299a9addfSStefan Hajnoczi 
319384d18f06SMarkus Armbruster     if (local_err) {
319499a9addfSStefan Hajnoczi         error_propagate(errp, local_err);
3195761731b1SStefan Hajnoczi         goto out;
319699a9addfSStefan Hajnoczi     }
319799a9addfSStefan Hajnoczi 
3198e6641719SMax Reitz     if (format) {
3199e6641719SMax Reitz         options = qdict_new();
3200e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
3201e6641719SMax Reitz     }
3202e6641719SMax Reitz 
3203f67503e5SMax Reitz     target_bs = NULL;
32046ebf9aa2SMax Reitz     ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
320599a9addfSStefan Hajnoczi     if (ret < 0) {
320634b5d2c6SMax Reitz         error_propagate(errp, local_err);
3207761731b1SStefan Hajnoczi         goto out;
320899a9addfSStefan Hajnoczi     }
320999a9addfSStefan Hajnoczi 
3210761731b1SStefan Hajnoczi     bdrv_set_aio_context(target_bs, aio_context);
3211761731b1SStefan Hajnoczi 
3212d58d8453SJohn Snow     if (has_bitmap) {
3213d58d8453SJohn Snow         bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3214d58d8453SJohn Snow         if (!bmap) {
3215d58d8453SJohn Snow             error_setg(errp, "Bitmap '%s' could not be found", bitmap);
32160702d3d8SMax Reitz             bdrv_unref(target_bs);
3217d58d8453SJohn Snow             goto out;
3218d58d8453SJohn Snow         }
3219d58d8453SJohn Snow     }
3220d58d8453SJohn Snow 
3221d58d8453SJohn Snow     backup_start(bs, target_bs, speed, sync, bmap,
3222d58d8453SJohn Snow                  on_source_error, on_target_error,
322378f51fdeSJohn Snow                  block_job_cb, bs, txn, &local_err);
322499a9addfSStefan Hajnoczi     if (local_err != NULL) {
32254f6fd349SFam Zheng         bdrv_unref(target_bs);
322699a9addfSStefan Hajnoczi         error_propagate(errp, local_err);
3227761731b1SStefan Hajnoczi         goto out;
322899a9addfSStefan Hajnoczi     }
3229761731b1SStefan Hajnoczi 
3230761731b1SStefan Hajnoczi out:
3231761731b1SStefan Hajnoczi     aio_context_release(aio_context);
323299a9addfSStefan Hajnoczi }
323399a9addfSStefan Hajnoczi 
323478f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target,
323578f51fdeSJohn Snow                       bool has_format, const char *format,
323678f51fdeSJohn Snow                       enum MirrorSyncMode sync,
323778f51fdeSJohn Snow                       bool has_mode, enum NewImageMode mode,
323878f51fdeSJohn Snow                       bool has_speed, int64_t speed,
323978f51fdeSJohn Snow                       bool has_bitmap, const char *bitmap,
324078f51fdeSJohn Snow                       bool has_on_source_error, BlockdevOnError on_source_error,
324178f51fdeSJohn Snow                       bool has_on_target_error, BlockdevOnError on_target_error,
324278f51fdeSJohn Snow                       Error **errp)
324378f51fdeSJohn Snow {
324478f51fdeSJohn Snow     return do_drive_backup(device, target, has_format, format, sync,
324578f51fdeSJohn Snow                            has_mode, mode, has_speed, speed,
324678f51fdeSJohn Snow                            has_bitmap, bitmap,
324778f51fdeSJohn Snow                            has_on_source_error, on_source_error,
324878f51fdeSJohn Snow                            has_on_target_error, on_target_error,
324978f51fdeSJohn Snow                            NULL, errp);
325078f51fdeSJohn Snow }
325178f51fdeSJohn Snow 
3252c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3253c13163fbSBenoît Canet {
3254d5a8ee60SAlberto Garcia     return bdrv_named_nodes_list(errp);
3255c13163fbSBenoît Canet }
3256c13163fbSBenoît Canet 
325778f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target,
3258c29c1dd3SFam Zheng                          enum MirrorSyncMode sync,
3259c29c1dd3SFam Zheng                          bool has_speed, int64_t speed,
3260c29c1dd3SFam Zheng                          bool has_on_source_error,
3261c29c1dd3SFam Zheng                          BlockdevOnError on_source_error,
3262c29c1dd3SFam Zheng                          bool has_on_target_error,
3263c29c1dd3SFam Zheng                          BlockdevOnError on_target_error,
326478f51fdeSJohn Snow                          BlockJobTxn *txn, Error **errp)
3265c29c1dd3SFam Zheng {
32665433c24fSMax Reitz     BlockBackend *blk, *target_blk;
3267c29c1dd3SFam Zheng     BlockDriverState *bs;
3268c29c1dd3SFam Zheng     BlockDriverState *target_bs;
3269c29c1dd3SFam Zheng     Error *local_err = NULL;
3270c29c1dd3SFam Zheng     AioContext *aio_context;
3271c29c1dd3SFam Zheng 
3272c29c1dd3SFam Zheng     if (!has_speed) {
3273c29c1dd3SFam Zheng         speed = 0;
3274c29c1dd3SFam Zheng     }
3275c29c1dd3SFam Zheng     if (!has_on_source_error) {
3276c29c1dd3SFam Zheng         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3277c29c1dd3SFam Zheng     }
3278c29c1dd3SFam Zheng     if (!has_on_target_error) {
3279c29c1dd3SFam Zheng         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3280c29c1dd3SFam Zheng     }
3281c29c1dd3SFam Zheng 
3282a0e8544cSFam Zheng     blk = blk_by_name(device);
3283a0e8544cSFam Zheng     if (!blk) {
32845b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", device);
3285c29c1dd3SFam Zheng         return;
3286c29c1dd3SFam Zheng     }
3287c29c1dd3SFam Zheng 
32885433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
3289c29c1dd3SFam Zheng     aio_context_acquire(aio_context);
3290c29c1dd3SFam Zheng 
32915433c24fSMax Reitz     if (!blk_is_available(blk)) {
32925433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
32935433c24fSMax Reitz         goto out;
32945433c24fSMax Reitz     }
32955433c24fSMax Reitz     bs = blk_bs(blk);
32965433c24fSMax Reitz 
32975433c24fSMax Reitz     target_blk = blk_by_name(target);
32985433c24fSMax Reitz     if (!target_blk) {
32995b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", target);
3300c29c1dd3SFam Zheng         goto out;
3301c29c1dd3SFam Zheng     }
33025433c24fSMax Reitz 
33035433c24fSMax Reitz     if (!blk_is_available(target_blk)) {
33045433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", target);
33055433c24fSMax Reitz         goto out;
33065433c24fSMax Reitz     }
33075433c24fSMax Reitz     target_bs = blk_bs(target_blk);
3308c29c1dd3SFam Zheng 
3309c29c1dd3SFam Zheng     bdrv_ref(target_bs);
3310c29c1dd3SFam Zheng     bdrv_set_aio_context(target_bs, aio_context);
3311d58d8453SJohn Snow     backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
331278f51fdeSJohn Snow                  on_target_error, block_job_cb, bs, txn, &local_err);
3313c29c1dd3SFam Zheng     if (local_err != NULL) {
3314c29c1dd3SFam Zheng         bdrv_unref(target_bs);
3315c29c1dd3SFam Zheng         error_propagate(errp, local_err);
3316c29c1dd3SFam Zheng     }
3317c29c1dd3SFam Zheng out:
3318c29c1dd3SFam Zheng     aio_context_release(aio_context);
3319c29c1dd3SFam Zheng }
3320c29c1dd3SFam Zheng 
332178f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target,
332278f51fdeSJohn Snow                          enum MirrorSyncMode sync,
332378f51fdeSJohn Snow                          bool has_speed, int64_t speed,
332478f51fdeSJohn Snow                          bool has_on_source_error,
332578f51fdeSJohn Snow                          BlockdevOnError on_source_error,
332678f51fdeSJohn Snow                          bool has_on_target_error,
332778f51fdeSJohn Snow                          BlockdevOnError on_target_error,
332878f51fdeSJohn Snow                          Error **errp)
332978f51fdeSJohn Snow {
333078f51fdeSJohn Snow     do_blockdev_backup(device, target, sync, has_speed, speed,
333178f51fdeSJohn Snow                        has_on_source_error, on_source_error,
333278f51fdeSJohn Snow                        has_on_target_error, on_target_error,
333378f51fdeSJohn Snow                        NULL, errp);
333478f51fdeSJohn Snow }
333578f51fdeSJohn Snow 
33364193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring.
33374193cdd7SFam Zheng  * Caller should hold @device and @target's aio context (must be the same).
33384193cdd7SFam Zheng  **/
33394193cdd7SFam Zheng static void blockdev_mirror_common(BlockDriverState *bs,
33404193cdd7SFam Zheng                                    BlockDriverState *target,
334109158f00SBenoît Canet                                    bool has_replaces, const char *replaces,
3342d9b902dbSPaolo Bonzini                                    enum MirrorSyncMode sync,
3343b952b558SPaolo Bonzini                                    bool has_speed, int64_t speed,
3344eee13dfeSPaolo Bonzini                                    bool has_granularity, uint32_t granularity,
334508e4ed6cSPaolo Bonzini                                    bool has_buf_size, int64_t buf_size,
33464193cdd7SFam Zheng                                    bool has_on_source_error,
33474193cdd7SFam Zheng                                    BlockdevOnError on_source_error,
33484193cdd7SFam Zheng                                    bool has_on_target_error,
33494193cdd7SFam Zheng                                    BlockdevOnError on_target_error,
33500fc9f8eaSFam Zheng                                    bool has_unmap, bool unmap,
3351b952b558SPaolo Bonzini                                    Error **errp)
3352d9b902dbSPaolo Bonzini {
3353d9b902dbSPaolo Bonzini 
3354d9b902dbSPaolo Bonzini     if (!has_speed) {
3355d9b902dbSPaolo Bonzini         speed = 0;
3356d9b902dbSPaolo Bonzini     }
3357b952b558SPaolo Bonzini     if (!has_on_source_error) {
3358b952b558SPaolo Bonzini         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3359b952b558SPaolo Bonzini     }
3360b952b558SPaolo Bonzini     if (!has_on_target_error) {
3361b952b558SPaolo Bonzini         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3362b952b558SPaolo Bonzini     }
3363eee13dfeSPaolo Bonzini     if (!has_granularity) {
3364eee13dfeSPaolo Bonzini         granularity = 0;
3365eee13dfeSPaolo Bonzini     }
336608e4ed6cSPaolo Bonzini     if (!has_buf_size) {
336748ac0a4dSWen Congyang         buf_size = 0;
336808e4ed6cSPaolo Bonzini     }
33690fc9f8eaSFam Zheng     if (!has_unmap) {
33700fc9f8eaSFam Zheng         unmap = true;
33710fc9f8eaSFam Zheng     }
337208e4ed6cSPaolo Bonzini 
3373eee13dfeSPaolo Bonzini     if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3374c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
33753cbbe9fdSStefan Hajnoczi                    "a value in range [512B, 64MB]");
3376eee13dfeSPaolo Bonzini         return;
3377eee13dfeSPaolo Bonzini     }
3378eee13dfeSPaolo Bonzini     if (granularity & (granularity - 1)) {
3379c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3380c6bd8c70SMarkus Armbruster                    "power of 2");
3381eee13dfeSPaolo Bonzini         return;
3382eee13dfeSPaolo Bonzini     }
3383d9b902dbSPaolo Bonzini 
33844193cdd7SFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
33854193cdd7SFam Zheng         return;
33864193cdd7SFam Zheng     }
3387e40e5027SFam Zheng     if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3388e40e5027SFam Zheng         return;
3389e40e5027SFam Zheng     }
3390df92562eSFam Zheng     if (target->blk) {
3391df92562eSFam Zheng         error_setg(errp, "Cannot mirror to an attached block device");
3392df92562eSFam Zheng         return;
3393df92562eSFam Zheng     }
33944193cdd7SFam Zheng 
33954193cdd7SFam Zheng     if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
33964193cdd7SFam Zheng         sync = MIRROR_SYNC_MODE_FULL;
33974193cdd7SFam Zheng     }
33984193cdd7SFam Zheng 
33994193cdd7SFam Zheng     /* pass the node name to replace to mirror start since it's loose coupling
34004193cdd7SFam Zheng      * and will allow to check whether the node still exist at mirror completion
34014193cdd7SFam Zheng      */
34024193cdd7SFam Zheng     mirror_start(bs, target,
34034193cdd7SFam Zheng                  has_replaces ? replaces : NULL,
34044193cdd7SFam Zheng                  speed, granularity, buf_size, sync,
34054193cdd7SFam Zheng                  on_source_error, on_target_error, unmap,
34064193cdd7SFam Zheng                  block_job_cb, bs, errp);
34074193cdd7SFam Zheng }
34084193cdd7SFam Zheng 
34094193cdd7SFam Zheng void qmp_drive_mirror(const char *device, const char *target,
34104193cdd7SFam Zheng                       bool has_format, const char *format,
34114193cdd7SFam Zheng                       bool has_node_name, const char *node_name,
34124193cdd7SFam Zheng                       bool has_replaces, const char *replaces,
34134193cdd7SFam Zheng                       enum MirrorSyncMode sync,
34144193cdd7SFam Zheng                       bool has_mode, enum NewImageMode mode,
34154193cdd7SFam Zheng                       bool has_speed, int64_t speed,
34164193cdd7SFam Zheng                       bool has_granularity, uint32_t granularity,
34174193cdd7SFam Zheng                       bool has_buf_size, int64_t buf_size,
34184193cdd7SFam Zheng                       bool has_on_source_error, BlockdevOnError on_source_error,
34194193cdd7SFam Zheng                       bool has_on_target_error, BlockdevOnError on_target_error,
34204193cdd7SFam Zheng                       bool has_unmap, bool unmap,
34214193cdd7SFam Zheng                       Error **errp)
34224193cdd7SFam Zheng {
34234193cdd7SFam Zheng     BlockDriverState *bs;
34244193cdd7SFam Zheng     BlockBackend *blk;
34254193cdd7SFam Zheng     BlockDriverState *source, *target_bs;
34264193cdd7SFam Zheng     AioContext *aio_context;
34274193cdd7SFam Zheng     Error *local_err = NULL;
34284193cdd7SFam Zheng     QDict *options = NULL;
34294193cdd7SFam Zheng     int flags;
34304193cdd7SFam Zheng     int64_t size;
34314193cdd7SFam Zheng     int ret;
34324193cdd7SFam Zheng 
3433a0e8544cSFam Zheng     blk = blk_by_name(device);
3434a0e8544cSFam Zheng     if (!blk) {
343575158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
343675158ebbSMarkus Armbruster                   "Device '%s' not found", device);
3437d9b902dbSPaolo Bonzini         return;
3438d9b902dbSPaolo Bonzini     }
3439d9b902dbSPaolo Bonzini 
34405433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
34415a7e7a0bSStefan Hajnoczi     aio_context_acquire(aio_context);
34425a7e7a0bSStefan Hajnoczi 
34435433c24fSMax Reitz     if (!blk_is_available(blk)) {
3444c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
34455a7e7a0bSStefan Hajnoczi         goto out;
3446d9b902dbSPaolo Bonzini     }
34475433c24fSMax Reitz     bs = blk_bs(blk);
34484193cdd7SFam Zheng     if (!has_mode) {
34494193cdd7SFam Zheng         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
34504193cdd7SFam Zheng     }
3451d9b902dbSPaolo Bonzini 
3452d9b902dbSPaolo Bonzini     if (!has_format) {
3453d9b902dbSPaolo Bonzini         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3454d9b902dbSPaolo Bonzini     }
3455d9b902dbSPaolo Bonzini 
3456d9b902dbSPaolo Bonzini     flags = bs->open_flags | BDRV_O_RDWR;
3457760e0063SKevin Wolf     source = backing_bs(bs);
3458d9b902dbSPaolo Bonzini     if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3459d9b902dbSPaolo Bonzini         sync = MIRROR_SYNC_MODE_FULL;
3460d9b902dbSPaolo Bonzini     }
3461117e0c82SMax Reitz     if (sync == MIRROR_SYNC_MODE_NONE) {
3462117e0c82SMax Reitz         source = bs;
3463117e0c82SMax Reitz     }
3464d9b902dbSPaolo Bonzini 
3465ac3c5d83SStefan Hajnoczi     size = bdrv_getlength(bs);
3466ac3c5d83SStefan Hajnoczi     if (size < 0) {
3467ac3c5d83SStefan Hajnoczi         error_setg_errno(errp, -size, "bdrv_getlength failed");
34685a7e7a0bSStefan Hajnoczi         goto out;
3469ac3c5d83SStefan Hajnoczi     }
3470ac3c5d83SStefan Hajnoczi 
347109158f00SBenoît Canet     if (has_replaces) {
347209158f00SBenoît Canet         BlockDriverState *to_replace_bs;
34735a7e7a0bSStefan Hajnoczi         AioContext *replace_aio_context;
34745a7e7a0bSStefan Hajnoczi         int64_t replace_size;
347509158f00SBenoît Canet 
347609158f00SBenoît Canet         if (!has_node_name) {
347709158f00SBenoît Canet             error_setg(errp, "a node-name must be provided when replacing a"
347809158f00SBenoît Canet                              " named node of the graph");
34795a7e7a0bSStefan Hajnoczi             goto out;
348009158f00SBenoît Canet         }
348109158f00SBenoît Canet 
3482e12f3784SWen Congyang         to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
348309158f00SBenoît Canet 
348409158f00SBenoît Canet         if (!to_replace_bs) {
348509158f00SBenoît Canet             error_propagate(errp, local_err);
34865a7e7a0bSStefan Hajnoczi             goto out;
348709158f00SBenoît Canet         }
348809158f00SBenoît Canet 
34895a7e7a0bSStefan Hajnoczi         replace_aio_context = bdrv_get_aio_context(to_replace_bs);
34905a7e7a0bSStefan Hajnoczi         aio_context_acquire(replace_aio_context);
34915a7e7a0bSStefan Hajnoczi         replace_size = bdrv_getlength(to_replace_bs);
34925a7e7a0bSStefan Hajnoczi         aio_context_release(replace_aio_context);
34935a7e7a0bSStefan Hajnoczi 
34945a7e7a0bSStefan Hajnoczi         if (size != replace_size) {
349509158f00SBenoît Canet             error_setg(errp, "cannot replace image with a mirror image of "
349609158f00SBenoît Canet                              "different size");
34975a7e7a0bSStefan Hajnoczi             goto out;
349809158f00SBenoît Canet         }
349909158f00SBenoît Canet     }
350009158f00SBenoît Canet 
350114526864SMax Reitz     if ((sync == MIRROR_SYNC_MODE_FULL || !source)
350214526864SMax Reitz         && mode != NEW_IMAGE_MODE_EXISTING)
350314526864SMax Reitz     {
3504d9b902dbSPaolo Bonzini         /* create new image w/o backing file */
3505e6641719SMax Reitz         assert(format);
3506cf8f2426SLuiz Capitulino         bdrv_img_create(target, format,
3507f382d43aSMiroslav Rezanina                         NULL, NULL, NULL, size, flags, &local_err, false);
3508d9b902dbSPaolo Bonzini     } else {
3509d9b902dbSPaolo Bonzini         switch (mode) {
3510d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_EXISTING:
3511d9b902dbSPaolo Bonzini             break;
3512d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3513d9b902dbSPaolo Bonzini             /* create new image with backing file */
3514cf8f2426SLuiz Capitulino             bdrv_img_create(target, format,
3515d9b902dbSPaolo Bonzini                             source->filename,
3516d9b902dbSPaolo Bonzini                             source->drv->format_name,
3517f382d43aSMiroslav Rezanina                             NULL, size, flags, &local_err, false);
3518d9b902dbSPaolo Bonzini             break;
3519d9b902dbSPaolo Bonzini         default:
3520d9b902dbSPaolo Bonzini             abort();
3521d9b902dbSPaolo Bonzini         }
3522d9b902dbSPaolo Bonzini     }
3523d9b902dbSPaolo Bonzini 
352484d18f06SMarkus Armbruster     if (local_err) {
3525cf8f2426SLuiz Capitulino         error_propagate(errp, local_err);
35265a7e7a0bSStefan Hajnoczi         goto out;
3527d9b902dbSPaolo Bonzini     }
3528d9b902dbSPaolo Bonzini 
35294c828dc6SBenoît Canet     options = qdict_new();
3530e6641719SMax Reitz     if (has_node_name) {
35314c828dc6SBenoît Canet         qdict_put(options, "node-name", qstring_from_str(node_name));
35324c828dc6SBenoît Canet     }
3533e6641719SMax Reitz     if (format) {
3534e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
3535e6641719SMax Reitz     }
35364c828dc6SBenoît Canet 
3537b812f671SPaolo Bonzini     /* Mirroring takes care of copy-on-write using the source's backing
3538b812f671SPaolo Bonzini      * file.
3539b812f671SPaolo Bonzini      */
3540f67503e5SMax Reitz     target_bs = NULL;
35414c828dc6SBenoît Canet     ret = bdrv_open(&target_bs, target, NULL, options,
35426ebf9aa2SMax Reitz                     flags | BDRV_O_NO_BACKING, &local_err);
3543d9b902dbSPaolo Bonzini     if (ret < 0) {
354434b5d2c6SMax Reitz         error_propagate(errp, local_err);
35455a7e7a0bSStefan Hajnoczi         goto out;
3546d9b902dbSPaolo Bonzini     }
3547d9b902dbSPaolo Bonzini 
35485a7e7a0bSStefan Hajnoczi     bdrv_set_aio_context(target_bs, aio_context);
35495a7e7a0bSStefan Hajnoczi 
35504193cdd7SFam Zheng     blockdev_mirror_common(bs, target_bs,
35514193cdd7SFam Zheng                            has_replaces, replaces, sync,
35524193cdd7SFam Zheng                            has_speed, speed,
35534193cdd7SFam Zheng                            has_granularity, granularity,
35544193cdd7SFam Zheng                            has_buf_size, buf_size,
35554193cdd7SFam Zheng                            has_on_source_error, on_source_error,
35564193cdd7SFam Zheng                            has_on_target_error, on_target_error,
35574193cdd7SFam Zheng                            has_unmap, unmap,
35584193cdd7SFam Zheng                            &local_err);
35594193cdd7SFam Zheng     if (local_err) {
3560d9b902dbSPaolo Bonzini         error_propagate(errp, local_err);
35614193cdd7SFam Zheng         bdrv_unref(target_bs);
3562d9b902dbSPaolo Bonzini     }
35635a7e7a0bSStefan Hajnoczi out:
35645a7e7a0bSStefan Hajnoczi     aio_context_release(aio_context);
3565d9b902dbSPaolo Bonzini }
3566d9b902dbSPaolo Bonzini 
3567df92562eSFam Zheng void qmp_blockdev_mirror(const char *device, const char *target,
3568df92562eSFam Zheng                          bool has_replaces, const char *replaces,
3569df92562eSFam Zheng                          MirrorSyncMode sync,
3570df92562eSFam Zheng                          bool has_speed, int64_t speed,
3571df92562eSFam Zheng                          bool has_granularity, uint32_t granularity,
3572df92562eSFam Zheng                          bool has_buf_size, int64_t buf_size,
3573df92562eSFam Zheng                          bool has_on_source_error,
3574df92562eSFam Zheng                          BlockdevOnError on_source_error,
3575df92562eSFam Zheng                          bool has_on_target_error,
3576df92562eSFam Zheng                          BlockdevOnError on_target_error,
3577df92562eSFam Zheng                          Error **errp)
3578df92562eSFam Zheng {
3579df92562eSFam Zheng     BlockDriverState *bs;
3580df92562eSFam Zheng     BlockBackend *blk;
3581df92562eSFam Zheng     BlockDriverState *target_bs;
3582df92562eSFam Zheng     AioContext *aio_context;
3583df92562eSFam Zheng     Error *local_err = NULL;
3584df92562eSFam Zheng 
3585df92562eSFam Zheng     blk = blk_by_name(device);
3586df92562eSFam Zheng     if (!blk) {
3587df92562eSFam Zheng         error_setg(errp, "Device '%s' not found", device);
3588df92562eSFam Zheng         return;
3589df92562eSFam Zheng     }
3590df92562eSFam Zheng     bs = blk_bs(blk);
3591df92562eSFam Zheng 
3592df92562eSFam Zheng     if (!bs) {
3593df92562eSFam Zheng         error_setg(errp, "Device '%s' has no media", device);
3594df92562eSFam Zheng         return;
3595df92562eSFam Zheng     }
3596df92562eSFam Zheng 
3597df92562eSFam Zheng     target_bs = bdrv_lookup_bs(target, target, errp);
3598df92562eSFam Zheng     if (!target_bs) {
3599df92562eSFam Zheng         return;
3600df92562eSFam Zheng     }
3601df92562eSFam Zheng 
3602df92562eSFam Zheng     aio_context = bdrv_get_aio_context(bs);
3603df92562eSFam Zheng     aio_context_acquire(aio_context);
3604df92562eSFam Zheng 
3605df92562eSFam Zheng     bdrv_ref(target_bs);
3606df92562eSFam Zheng     bdrv_set_aio_context(target_bs, aio_context);
3607df92562eSFam Zheng 
3608df92562eSFam Zheng     blockdev_mirror_common(bs, target_bs,
3609df92562eSFam Zheng                            has_replaces, replaces, sync,
3610df92562eSFam Zheng                            has_speed, speed,
3611df92562eSFam Zheng                            has_granularity, granularity,
3612df92562eSFam Zheng                            has_buf_size, buf_size,
3613df92562eSFam Zheng                            has_on_source_error, on_source_error,
3614df92562eSFam Zheng                            has_on_target_error, on_target_error,
3615df92562eSFam Zheng                            true, true,
3616df92562eSFam Zheng                            &local_err);
3617df92562eSFam Zheng     if (local_err) {
3618df92562eSFam Zheng         error_propagate(errp, local_err);
3619df92562eSFam Zheng         bdrv_unref(target_bs);
3620df92562eSFam Zheng     }
3621df92562eSFam Zheng 
3622df92562eSFam Zheng     aio_context_release(aio_context);
3623df92562eSFam Zheng }
3624df92562eSFam Zheng 
36253d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */
362624d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context,
362724d6bffeSMarkus Armbruster                                 Error **errp)
36282d47c6e9SStefan Hajnoczi {
3629a0e8544cSFam Zheng     BlockBackend *blk;
36302d47c6e9SStefan Hajnoczi     BlockDriverState *bs;
36312d47c6e9SStefan Hajnoczi 
36325433c24fSMax Reitz     *aio_context = NULL;
36335433c24fSMax Reitz 
3634a0e8544cSFam Zheng     blk = blk_by_name(device);
3635a0e8544cSFam Zheng     if (!blk) {
36363d948cdfSStefan Hajnoczi         goto notfound;
36372d47c6e9SStefan Hajnoczi     }
36383d948cdfSStefan Hajnoczi 
36395433c24fSMax Reitz     *aio_context = blk_get_aio_context(blk);
36403d948cdfSStefan Hajnoczi     aio_context_acquire(*aio_context);
36413d948cdfSStefan Hajnoczi 
36425433c24fSMax Reitz     if (!blk_is_available(blk)) {
36435433c24fSMax Reitz         goto notfound;
36445433c24fSMax Reitz     }
36455433c24fSMax Reitz     bs = blk_bs(blk);
36465433c24fSMax Reitz 
36473d948cdfSStefan Hajnoczi     if (!bs->job) {
36483d948cdfSStefan Hajnoczi         goto notfound;
36493d948cdfSStefan Hajnoczi     }
36503d948cdfSStefan Hajnoczi 
36512d47c6e9SStefan Hajnoczi     return bs->job;
36523d948cdfSStefan Hajnoczi 
36533d948cdfSStefan Hajnoczi notfound:
36542e3a0266SMarkus Armbruster     error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
36552e3a0266SMarkus Armbruster               "No active block job on device '%s'", device);
36565433c24fSMax Reitz     if (*aio_context) {
36575433c24fSMax Reitz         aio_context_release(*aio_context);
36583d948cdfSStefan Hajnoczi         *aio_context = NULL;
36595433c24fSMax Reitz     }
36603d948cdfSStefan Hajnoczi     return NULL;
36612d47c6e9SStefan Hajnoczi }
36622d47c6e9SStefan Hajnoczi 
3663882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
36642d47c6e9SStefan Hajnoczi {
36653d948cdfSStefan Hajnoczi     AioContext *aio_context;
366624d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
36672d47c6e9SStefan Hajnoczi 
36682d47c6e9SStefan Hajnoczi     if (!job) {
36692d47c6e9SStefan Hajnoczi         return;
36702d47c6e9SStefan Hajnoczi     }
36712d47c6e9SStefan Hajnoczi 
3672882ec7ceSStefan Hajnoczi     block_job_set_speed(job, speed, errp);
36733d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
36742d47c6e9SStefan Hajnoczi }
3675370521a1SStefan Hajnoczi 
36766e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device,
36776e37fb81SPaolo Bonzini                           bool has_force, bool force, Error **errp)
36786e37fb81SPaolo Bonzini {
36793d948cdfSStefan Hajnoczi     AioContext *aio_context;
368024d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
36816e37fb81SPaolo Bonzini 
36826e37fb81SPaolo Bonzini     if (!job) {
36836e37fb81SPaolo Bonzini         return;
36846e37fb81SPaolo Bonzini     }
36853d948cdfSStefan Hajnoczi 
36863d948cdfSStefan Hajnoczi     if (!has_force) {
36873d948cdfSStefan Hajnoczi         force = false;
36883d948cdfSStefan Hajnoczi     }
36893d948cdfSStefan Hajnoczi 
3690751ebd76SFam Zheng     if (job->user_paused && !force) {
3691f231b88dSCole Robinson         error_setg(errp, "The block job for device '%s' is currently paused",
3692f231b88dSCole Robinson                    device);
36933d948cdfSStefan Hajnoczi         goto out;
36946e37fb81SPaolo Bonzini     }
36956e37fb81SPaolo Bonzini 
36966e37fb81SPaolo Bonzini     trace_qmp_block_job_cancel(job);
36976e37fb81SPaolo Bonzini     block_job_cancel(job);
36983d948cdfSStefan Hajnoczi out:
36993d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
37006e37fb81SPaolo Bonzini }
37016e37fb81SPaolo Bonzini 
37026e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp)
3703370521a1SStefan Hajnoczi {
37043d948cdfSStefan Hajnoczi     AioContext *aio_context;
370524d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
3706370521a1SStefan Hajnoczi 
3707751ebd76SFam Zheng     if (!job || job->user_paused) {
3708370521a1SStefan Hajnoczi         return;
3709370521a1SStefan Hajnoczi     }
37106e37fb81SPaolo Bonzini 
3711751ebd76SFam Zheng     job->user_paused = true;
37126e37fb81SPaolo Bonzini     trace_qmp_block_job_pause(job);
37136e37fb81SPaolo Bonzini     block_job_pause(job);
37143d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
37156e37fb81SPaolo Bonzini }
37166e37fb81SPaolo Bonzini 
37176e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp)
37186e37fb81SPaolo Bonzini {
37193d948cdfSStefan Hajnoczi     AioContext *aio_context;
372024d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
37216e37fb81SPaolo Bonzini 
3722751ebd76SFam Zheng     if (!job || !job->user_paused) {
37238acc72a4SPaolo Bonzini         return;
37248acc72a4SPaolo Bonzini     }
3725370521a1SStefan Hajnoczi 
3726751ebd76SFam Zheng     job->user_paused = false;
37276e37fb81SPaolo Bonzini     trace_qmp_block_job_resume(job);
37286e37fb81SPaolo Bonzini     block_job_resume(job);
37293d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
3730370521a1SStefan Hajnoczi }
3731fb5458cdSStefan Hajnoczi 
3732aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp)
3733aeae883bSPaolo Bonzini {
37343d948cdfSStefan Hajnoczi     AioContext *aio_context;
373524d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
3736aeae883bSPaolo Bonzini 
3737aeae883bSPaolo Bonzini     if (!job) {
3738aeae883bSPaolo Bonzini         return;
3739aeae883bSPaolo Bonzini     }
3740aeae883bSPaolo Bonzini 
3741aeae883bSPaolo Bonzini     trace_qmp_block_job_complete(job);
3742aeae883bSPaolo Bonzini     block_job_complete(job, errp);
37433d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
3744aeae883bSPaolo Bonzini }
3745aeae883bSPaolo Bonzini 
3746fa40e656SJeff Cody void qmp_change_backing_file(const char *device,
3747fa40e656SJeff Cody                              const char *image_node_name,
3748fa40e656SJeff Cody                              const char *backing_file,
3749fa40e656SJeff Cody                              Error **errp)
3750fa40e656SJeff Cody {
3751a0e8544cSFam Zheng     BlockBackend *blk;
3752fa40e656SJeff Cody     BlockDriverState *bs = NULL;
3753729962f6SStefan Hajnoczi     AioContext *aio_context;
3754fa40e656SJeff Cody     BlockDriverState *image_bs = NULL;
3755fa40e656SJeff Cody     Error *local_err = NULL;
3756fa40e656SJeff Cody     bool ro;
3757fa40e656SJeff Cody     int open_flags;
3758fa40e656SJeff Cody     int ret;
3759fa40e656SJeff Cody 
3760a0e8544cSFam Zheng     blk = blk_by_name(device);
3761a0e8544cSFam Zheng     if (!blk) {
376275158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
376375158ebbSMarkus Armbruster                   "Device '%s' not found", device);
3764fa40e656SJeff Cody         return;
3765fa40e656SJeff Cody     }
3766fa40e656SJeff Cody 
37675433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
3768729962f6SStefan Hajnoczi     aio_context_acquire(aio_context);
3769729962f6SStefan Hajnoczi 
37705433c24fSMax Reitz     if (!blk_is_available(blk)) {
37715433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
37725433c24fSMax Reitz         goto out;
37735433c24fSMax Reitz     }
37745433c24fSMax Reitz     bs = blk_bs(blk);
37755433c24fSMax Reitz 
3776fa40e656SJeff Cody     image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3777fa40e656SJeff Cody     if (local_err) {
3778fa40e656SJeff Cody         error_propagate(errp, local_err);
3779729962f6SStefan Hajnoczi         goto out;
3780fa40e656SJeff Cody     }
3781fa40e656SJeff Cody 
3782fa40e656SJeff Cody     if (!image_bs) {
3783fa40e656SJeff Cody         error_setg(errp, "image file not found");
3784729962f6SStefan Hajnoczi         goto out;
3785fa40e656SJeff Cody     }
3786fa40e656SJeff Cody 
3787fa40e656SJeff Cody     if (bdrv_find_base(image_bs) == image_bs) {
3788fa40e656SJeff Cody         error_setg(errp, "not allowing backing file change on an image "
3789fa40e656SJeff Cody                          "without a backing file");
3790729962f6SStefan Hajnoczi         goto out;
3791fa40e656SJeff Cody     }
3792fa40e656SJeff Cody 
3793fa40e656SJeff Cody     /* even though we are not necessarily operating on bs, we need it to
3794fa40e656SJeff Cody      * determine if block ops are currently prohibited on the chain */
3795fa40e656SJeff Cody     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3796729962f6SStefan Hajnoczi         goto out;
3797fa40e656SJeff Cody     }
3798fa40e656SJeff Cody 
3799fa40e656SJeff Cody     /* final sanity check */
3800fa40e656SJeff Cody     if (!bdrv_chain_contains(bs, image_bs)) {
3801fa40e656SJeff Cody         error_setg(errp, "'%s' and image file are not in the same chain",
3802fa40e656SJeff Cody                    device);
3803729962f6SStefan Hajnoczi         goto out;
3804fa40e656SJeff Cody     }
3805fa40e656SJeff Cody 
3806fa40e656SJeff Cody     /* if not r/w, reopen to make r/w */
3807fa40e656SJeff Cody     open_flags = image_bs->open_flags;
3808fa40e656SJeff Cody     ro = bdrv_is_read_only(image_bs);
3809fa40e656SJeff Cody 
3810fa40e656SJeff Cody     if (ro) {
3811fa40e656SJeff Cody         bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3812fa40e656SJeff Cody         if (local_err) {
3813fa40e656SJeff Cody             error_propagate(errp, local_err);
3814729962f6SStefan Hajnoczi             goto out;
3815fa40e656SJeff Cody         }
3816fa40e656SJeff Cody     }
3817fa40e656SJeff Cody 
3818fa40e656SJeff Cody     ret = bdrv_change_backing_file(image_bs, backing_file,
3819fa40e656SJeff Cody                                image_bs->drv ? image_bs->drv->format_name : "");
3820fa40e656SJeff Cody 
3821fa40e656SJeff Cody     if (ret < 0) {
3822fa40e656SJeff Cody         error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3823fa40e656SJeff Cody                          backing_file);
3824fa40e656SJeff Cody         /* don't exit here, so we can try to restore open flags if
3825fa40e656SJeff Cody          * appropriate */
3826fa40e656SJeff Cody     }
3827fa40e656SJeff Cody 
3828fa40e656SJeff Cody     if (ro) {
3829fa40e656SJeff Cody         bdrv_reopen(image_bs, open_flags, &local_err);
3830fa40e656SJeff Cody         if (local_err) {
3831fa40e656SJeff Cody             error_propagate(errp, local_err); /* will preserve prior errp */
3832fa40e656SJeff Cody         }
3833fa40e656SJeff Cody     }
3834729962f6SStefan Hajnoczi 
3835729962f6SStefan Hajnoczi out:
3836729962f6SStefan Hajnoczi     aio_context_release(aio_context);
3837fa40e656SJeff Cody }
3838fa40e656SJeff Cody 
3839d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3840d26c9a15SKevin Wolf {
3841d26c9a15SKevin Wolf     QmpOutputVisitor *ov = qmp_output_visitor_new();
3842be4b67bcSMax Reitz     BlockDriverState *bs;
3843be4b67bcSMax Reitz     BlockBackend *blk = NULL;
3844d26c9a15SKevin Wolf     QObject *obj;
3845d26c9a15SKevin Wolf     QDict *qdict;
3846d26c9a15SKevin Wolf     Error *local_err = NULL;
3847d26c9a15SKevin Wolf 
384860e19e06SMarkus Armbruster     /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3849d26c9a15SKevin Wolf      * cache.direct=false instead of silently switching to aio=threads, except
385060e19e06SMarkus Armbruster      * when called from drive_new().
3851d26c9a15SKevin Wolf      *
3852d26c9a15SKevin Wolf      * For now, simply forbidding the combination for all drivers will do. */
3853d26c9a15SKevin Wolf     if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3854c6e0bd9bSKevin Wolf         bool direct = options->has_cache &&
3855c6e0bd9bSKevin Wolf                       options->cache->has_direct &&
3856c6e0bd9bSKevin Wolf                       options->cache->direct;
3857c6e0bd9bSKevin Wolf         if (!direct) {
3858d26c9a15SKevin Wolf             error_setg(errp, "aio=native requires cache.direct=true");
3859d26c9a15SKevin Wolf             goto fail;
3860d26c9a15SKevin Wolf         }
3861d26c9a15SKevin Wolf     }
3862d26c9a15SKevin Wolf 
3863*51e72bc1SEric Blake     visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options,
3864*51e72bc1SEric Blake                                &local_err);
386584d18f06SMarkus Armbruster     if (local_err) {
3866d26c9a15SKevin Wolf         error_propagate(errp, local_err);
3867d26c9a15SKevin Wolf         goto fail;
3868d26c9a15SKevin Wolf     }
3869d26c9a15SKevin Wolf 
3870d26c9a15SKevin Wolf     obj = qmp_output_get_qobject(ov);
3871d26c9a15SKevin Wolf     qdict = qobject_to_qdict(obj);
3872d26c9a15SKevin Wolf 
3873d26c9a15SKevin Wolf     qdict_flatten(qdict);
3874d26c9a15SKevin Wolf 
3875be4b67bcSMax Reitz     if (options->has_id) {
387618e46a03SMarkus Armbruster         blk = blockdev_init(NULL, qdict, &local_err);
387784d18f06SMarkus Armbruster         if (local_err) {
3878b681072dSKevin Wolf             error_propagate(errp, local_err);
3879d26c9a15SKevin Wolf             goto fail;
3880d26c9a15SKevin Wolf         }
3881d26c9a15SKevin Wolf 
3882be4b67bcSMax Reitz         bs = blk_bs(blk);
3883be4b67bcSMax Reitz     } else {
3884be4b67bcSMax Reitz         if (!qdict_get_try_str(qdict, "node-name")) {
3885be4b67bcSMax Reitz             error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3886be4b67bcSMax Reitz                        "the root node");
3887be4b67bcSMax Reitz             goto fail;
3888be4b67bcSMax Reitz         }
3889be4b67bcSMax Reitz 
3890bd745e23SMax Reitz         bs = bds_tree_init(qdict, errp);
3891bd745e23SMax Reitz         if (!bs) {
3892be4b67bcSMax Reitz             goto fail;
3893be4b67bcSMax Reitz         }
38949c4218e9SMax Reitz 
38959c4218e9SMax Reitz         QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3896be4b67bcSMax Reitz     }
3897be4b67bcSMax Reitz 
3898be4b67bcSMax Reitz     if (bs && bdrv_key_required(bs)) {
3899be4b67bcSMax Reitz         if (blk) {
390018e46a03SMarkus Armbruster             blk_unref(blk);
3901be4b67bcSMax Reitz         } else {
39029c4218e9SMax Reitz             QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3903be4b67bcSMax Reitz             bdrv_unref(bs);
3904be4b67bcSMax Reitz         }
39058ae8e904SKevin Wolf         error_setg(errp, "blockdev-add doesn't support encrypted devices");
39068ae8e904SKevin Wolf         goto fail;
39078ae8e904SKevin Wolf     }
39088ae8e904SKevin Wolf 
3909d26c9a15SKevin Wolf fail:
3910d26c9a15SKevin Wolf     qmp_output_visitor_cleanup(ov);
3911d26c9a15SKevin Wolf }
3912d26c9a15SKevin Wolf 
391381b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id,
391481b936aeSAlberto Garcia                         bool has_node_name, const char *node_name, Error **errp)
391581b936aeSAlberto Garcia {
391681b936aeSAlberto Garcia     AioContext *aio_context;
391781b936aeSAlberto Garcia     BlockBackend *blk;
391881b936aeSAlberto Garcia     BlockDriverState *bs;
391981b936aeSAlberto Garcia 
392081b936aeSAlberto Garcia     if (has_id && has_node_name) {
392181b936aeSAlberto Garcia         error_setg(errp, "Only one of id and node-name must be specified");
392281b936aeSAlberto Garcia         return;
392381b936aeSAlberto Garcia     } else if (!has_id && !has_node_name) {
392481b936aeSAlberto Garcia         error_setg(errp, "No block device specified");
392581b936aeSAlberto Garcia         return;
392681b936aeSAlberto Garcia     }
392781b936aeSAlberto Garcia 
392881b936aeSAlberto Garcia     if (has_id) {
392981b936aeSAlberto Garcia         blk = blk_by_name(id);
393081b936aeSAlberto Garcia         if (!blk) {
393181b936aeSAlberto Garcia             error_setg(errp, "Cannot find block backend %s", id);
393281b936aeSAlberto Garcia             return;
393381b936aeSAlberto Garcia         }
393481b936aeSAlberto Garcia         if (blk_get_refcnt(blk) > 1) {
393581b936aeSAlberto Garcia             error_setg(errp, "Block backend %s is in use", id);
393681b936aeSAlberto Garcia             return;
393781b936aeSAlberto Garcia         }
393881b936aeSAlberto Garcia         bs = blk_bs(blk);
393981b936aeSAlberto Garcia         aio_context = blk_get_aio_context(blk);
394081b936aeSAlberto Garcia     } else {
394181b936aeSAlberto Garcia         bs = bdrv_find_node(node_name);
394281b936aeSAlberto Garcia         if (!bs) {
394381b936aeSAlberto Garcia             error_setg(errp, "Cannot find node %s", node_name);
394481b936aeSAlberto Garcia             return;
394581b936aeSAlberto Garcia         }
394681b936aeSAlberto Garcia         blk = bs->blk;
394781b936aeSAlberto Garcia         if (blk) {
394881b936aeSAlberto Garcia             error_setg(errp, "Node %s is in use by %s",
394981b936aeSAlberto Garcia                        node_name, blk_name(blk));
395081b936aeSAlberto Garcia             return;
395181b936aeSAlberto Garcia         }
395281b936aeSAlberto Garcia         aio_context = bdrv_get_aio_context(bs);
395381b936aeSAlberto Garcia     }
395481b936aeSAlberto Garcia 
395581b936aeSAlberto Garcia     aio_context_acquire(aio_context);
395681b936aeSAlberto Garcia 
395781b936aeSAlberto Garcia     if (bs) {
395881b936aeSAlberto Garcia         if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
395981b936aeSAlberto Garcia             goto out;
396081b936aeSAlberto Garcia         }
396181b936aeSAlberto Garcia 
39629c4218e9SMax Reitz         if (!blk && !bs->monitor_list.tqe_prev) {
39639c4218e9SMax Reitz             error_setg(errp, "Node %s is not owned by the monitor",
39649c4218e9SMax Reitz                        bs->node_name);
39659c4218e9SMax Reitz             goto out;
39669c4218e9SMax Reitz         }
39679c4218e9SMax Reitz 
39689c4218e9SMax Reitz         if (bs->refcnt > 1) {
396981b936aeSAlberto Garcia             error_setg(errp, "Block device %s is in use",
397081b936aeSAlberto Garcia                        bdrv_get_device_or_node_name(bs));
397181b936aeSAlberto Garcia             goto out;
397281b936aeSAlberto Garcia         }
397381b936aeSAlberto Garcia     }
397481b936aeSAlberto Garcia 
397581b936aeSAlberto Garcia     if (blk) {
397681b936aeSAlberto Garcia         blk_unref(blk);
397781b936aeSAlberto Garcia     } else {
39789c4218e9SMax Reitz         QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
397981b936aeSAlberto Garcia         bdrv_unref(bs);
398081b936aeSAlberto Garcia     }
398181b936aeSAlberto Garcia 
398281b936aeSAlberto Garcia out:
398381b936aeSAlberto Garcia     aio_context_release(aio_context);
398481b936aeSAlberto Garcia }
398581b936aeSAlberto Garcia 
3986fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3987fb5458cdSStefan Hajnoczi {
3988fea68bb6SMarkus Armbruster     BlockJobInfoList *head = NULL, **p_next = &head;
3989fea68bb6SMarkus Armbruster     BlockDriverState *bs;
3990fea68bb6SMarkus Armbruster 
3991fea68bb6SMarkus Armbruster     for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
399269691e72SStefan Hajnoczi         AioContext *aio_context = bdrv_get_aio_context(bs);
399369691e72SStefan Hajnoczi 
399469691e72SStefan Hajnoczi         aio_context_acquire(aio_context);
399569691e72SStefan Hajnoczi 
3996fea68bb6SMarkus Armbruster         if (bs->job) {
3997fea68bb6SMarkus Armbruster             BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3998fea68bb6SMarkus Armbruster             elem->value = block_job_query(bs->job);
3999fea68bb6SMarkus Armbruster             *p_next = elem;
4000fea68bb6SMarkus Armbruster             p_next = &elem->next;
4001fea68bb6SMarkus Armbruster         }
400269691e72SStefan Hajnoczi 
400369691e72SStefan Hajnoczi         aio_context_release(aio_context);
4004fea68bb6SMarkus Armbruster     }
4005fea68bb6SMarkus Armbruster 
4006fea68bb6SMarkus Armbruster     return head;
4007fb5458cdSStefan Hajnoczi }
40084d454574SPaolo Bonzini 
40090006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = {
40104d454574SPaolo Bonzini     .name = "drive",
40110006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
40124d454574SPaolo Bonzini     .desc = {
40134d454574SPaolo Bonzini         {
40144d454574SPaolo Bonzini             .name = "snapshot",
40154d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
40164d454574SPaolo Bonzini             .help = "enable/disable snapshot mode",
40174d454574SPaolo Bonzini         },{
4018a9384affSPaolo Bonzini             .name = "discard",
4019a9384affSPaolo Bonzini             .type = QEMU_OPT_STRING,
4020a9384affSPaolo Bonzini             .help = "discard operation (ignore/off, unmap/on)",
4021a9384affSPaolo Bonzini         },{
40224d454574SPaolo Bonzini             .name = "aio",
40234d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40244d454574SPaolo Bonzini             .help = "host AIO implementation (threads, native)",
40254d454574SPaolo Bonzini         },{
40264d454574SPaolo Bonzini             .name = "format",
40274d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40284d454574SPaolo Bonzini             .help = "disk format (raw, qcow2, ...)",
40294d454574SPaolo Bonzini         },{
40304d454574SPaolo Bonzini             .name = "rerror",
40314d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40324d454574SPaolo Bonzini             .help = "read error action",
40334d454574SPaolo Bonzini         },{
40344d454574SPaolo Bonzini             .name = "werror",
40354d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40364d454574SPaolo Bonzini             .help = "write error action",
40374d454574SPaolo Bonzini         },{
40380f227a94SKevin Wolf             .name = "read-only",
40394d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
40404d454574SPaolo Bonzini             .help = "open drive file as read-only",
40414d454574SPaolo Bonzini         },{
404257975222SKevin Wolf             .name = "throttling.iops-total",
40434d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40444d454574SPaolo Bonzini             .help = "limit total I/O operations per second",
40454d454574SPaolo Bonzini         },{
404657975222SKevin Wolf             .name = "throttling.iops-read",
40474d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40484d454574SPaolo Bonzini             .help = "limit read operations per second",
40494d454574SPaolo Bonzini         },{
405057975222SKevin Wolf             .name = "throttling.iops-write",
40514d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40524d454574SPaolo Bonzini             .help = "limit write operations per second",
40534d454574SPaolo Bonzini         },{
405457975222SKevin Wolf             .name = "throttling.bps-total",
40554d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40564d454574SPaolo Bonzini             .help = "limit total bytes per second",
40574d454574SPaolo Bonzini         },{
405857975222SKevin Wolf             .name = "throttling.bps-read",
40594d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40604d454574SPaolo Bonzini             .help = "limit read bytes per second",
40614d454574SPaolo Bonzini         },{
406257975222SKevin Wolf             .name = "throttling.bps-write",
40634d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40644d454574SPaolo Bonzini             .help = "limit write bytes per second",
40654d454574SPaolo Bonzini         },{
40663e9fab69SBenoît Canet             .name = "throttling.iops-total-max",
40673e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40683e9fab69SBenoît Canet             .help = "I/O operations burst",
40693e9fab69SBenoît Canet         },{
40703e9fab69SBenoît Canet             .name = "throttling.iops-read-max",
40713e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40723e9fab69SBenoît Canet             .help = "I/O operations read burst",
40733e9fab69SBenoît Canet         },{
40743e9fab69SBenoît Canet             .name = "throttling.iops-write-max",
40753e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40763e9fab69SBenoît Canet             .help = "I/O operations write burst",
40773e9fab69SBenoît Canet         },{
40783e9fab69SBenoît Canet             .name = "throttling.bps-total-max",
40793e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40803e9fab69SBenoît Canet             .help = "total bytes burst",
40813e9fab69SBenoît Canet         },{
40823e9fab69SBenoît Canet             .name = "throttling.bps-read-max",
40833e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40843e9fab69SBenoît Canet             .help = "total bytes read burst",
40853e9fab69SBenoît Canet         },{
40863e9fab69SBenoît Canet             .name = "throttling.bps-write-max",
40873e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40883e9fab69SBenoît Canet             .help = "total bytes write burst",
40893e9fab69SBenoît Canet         },{
40902024c1dfSBenoît Canet             .name = "throttling.iops-size",
40912024c1dfSBenoît Canet             .type = QEMU_OPT_NUMBER,
40922024c1dfSBenoît Canet             .help = "when limiting by iops max size of an I/O in bytes",
40932024c1dfSBenoît Canet         },{
409476f4afb4SAlberto Garcia             .name = "throttling.group",
409576f4afb4SAlberto Garcia             .type = QEMU_OPT_STRING,
409676f4afb4SAlberto Garcia             .help = "name of the block throttling group",
409776f4afb4SAlberto Garcia         },{
40984d454574SPaolo Bonzini             .name = "copy-on-read",
40994d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
41004d454574SPaolo Bonzini             .help = "copy read data from backing file into image file",
4101465bee1dSPeter Lieven         },{
4102465bee1dSPeter Lieven             .name = "detect-zeroes",
4103465bee1dSPeter Lieven             .type = QEMU_OPT_STRING,
4104465bee1dSPeter Lieven             .help = "try to optimize zero writes (off, on, unmap)",
4105362e9299SAlberto Garcia         },{
4106362e9299SAlberto Garcia             .name = "stats-account-invalid",
4107362e9299SAlberto Garcia             .type = QEMU_OPT_BOOL,
4108362e9299SAlberto Garcia             .help = "whether to account for invalid I/O operations "
4109362e9299SAlberto Garcia                     "in the statistics",
4110362e9299SAlberto Garcia         },{
4111362e9299SAlberto Garcia             .name = "stats-account-failed",
4112362e9299SAlberto Garcia             .type = QEMU_OPT_BOOL,
4113362e9299SAlberto Garcia             .help = "whether to account for failed I/O operations "
4114362e9299SAlberto Garcia                     "in the statistics",
41154d454574SPaolo Bonzini         },
41164d454574SPaolo Bonzini         { /* end of list */ }
41174d454574SPaolo Bonzini     },
41184d454574SPaolo Bonzini };
41190006383eSKevin Wolf 
4120bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = {
4121bd745e23SMax Reitz     .name = "root-bds",
4122bd745e23SMax Reitz     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4123bd745e23SMax Reitz     .desc = {
4124bd745e23SMax Reitz         {
4125bd745e23SMax Reitz             .name = "discard",
4126bd745e23SMax Reitz             .type = QEMU_OPT_STRING,
4127bd745e23SMax Reitz             .help = "discard operation (ignore/off, unmap/on)",
4128bd745e23SMax Reitz         },{
4129bd745e23SMax Reitz             .name = "aio",
4130bd745e23SMax Reitz             .type = QEMU_OPT_STRING,
4131bd745e23SMax Reitz             .help = "host AIO implementation (threads, native)",
4132bd745e23SMax Reitz         },{
4133bd745e23SMax Reitz             .name = "read-only",
4134bd745e23SMax Reitz             .type = QEMU_OPT_BOOL,
4135bd745e23SMax Reitz             .help = "open drive file as read-only",
4136bd745e23SMax Reitz         },{
4137bd745e23SMax Reitz             .name = "copy-on-read",
4138bd745e23SMax Reitz             .type = QEMU_OPT_BOOL,
4139bd745e23SMax Reitz             .help = "copy read data from backing file into image file",
4140bd745e23SMax Reitz         },{
4141bd745e23SMax Reitz             .name = "detect-zeroes",
4142bd745e23SMax Reitz             .type = QEMU_OPT_STRING,
4143bd745e23SMax Reitz             .help = "try to optimize zero writes (off, on, unmap)",
4144bd745e23SMax Reitz         },
4145bd745e23SMax Reitz         { /* end of list */ }
4146bd745e23SMax Reitz     },
4147bd745e23SMax Reitz };
4148bd745e23SMax Reitz 
41490006383eSKevin Wolf QemuOptsList qemu_drive_opts = {
41500006383eSKevin Wolf     .name = "drive",
41510006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
41520006383eSKevin Wolf     .desc = {
4153492fdc6fSKevin Wolf         /*
4154492fdc6fSKevin Wolf          * no elements => accept any params
4155492fdc6fSKevin Wolf          * validation will happen later
4156492fdc6fSKevin Wolf          */
41570006383eSKevin Wolf         { /* end of list */ }
41580006383eSKevin Wolf     },
41590006383eSKevin Wolf };
4160