xref: /openbmc/qemu/blockdev.c (revision 74e1ae7c0b66d85d2b04c153ffdf6294e6a40798)
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"
53f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
54f348b6d1SVeronia Bahaa #include "qemu/help_option.h"
55666daa68SMarkus Armbruster 
569c4218e9SMax Reitz static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
579c4218e9SMax Reitz     QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
589c4218e9SMax Reitz 
59b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id,
60b33945cfSKevin Wolf                         bool force, Error **errp);
61bf18bee5SColin Lord 
621960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = {
631960966dSMarkus Armbruster     [IF_NONE] = "none",
641960966dSMarkus Armbruster     [IF_IDE] = "ide",
651960966dSMarkus Armbruster     [IF_SCSI] = "scsi",
661960966dSMarkus Armbruster     [IF_FLOPPY] = "floppy",
671960966dSMarkus Armbruster     [IF_PFLASH] = "pflash",
681960966dSMarkus Armbruster     [IF_MTD] = "mtd",
691960966dSMarkus Armbruster     [IF_SD] = "sd",
701960966dSMarkus Armbruster     [IF_VIRTIO] = "virtio",
711960966dSMarkus Armbruster     [IF_XEN] = "xen",
721960966dSMarkus Armbruster };
731960966dSMarkus Armbruster 
7421dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = {
7527d6bf40SMarkus Armbruster     /*
7627d6bf40SMarkus Armbruster      * Do not change these numbers!  They govern how drive option
7727d6bf40SMarkus Armbruster      * index maps to unit and bus.  That mapping is ABI.
7827d6bf40SMarkus Armbruster      *
79547cb157SWei Jiangang      * All controllers used to implement if=T drives need to support
8027d6bf40SMarkus Armbruster      * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
8127d6bf40SMarkus Armbruster      * Otherwise, some index values map to "impossible" bus, unit
8227d6bf40SMarkus Armbruster      * values.
8327d6bf40SMarkus Armbruster      *
8427d6bf40SMarkus Armbruster      * For instance, if you change [IF_SCSI] to 255, -drive
8527d6bf40SMarkus Armbruster      * if=scsi,index=12 no longer means bus=1,unit=5, but
8627d6bf40SMarkus Armbruster      * bus=0,unit=12.  With an lsi53c895a controller (7 units max),
8727d6bf40SMarkus Armbruster      * the drive can't be set up.  Regression.
8827d6bf40SMarkus Armbruster      */
8927d6bf40SMarkus Armbruster     [IF_IDE] = 2,
9027d6bf40SMarkus Armbruster     [IF_SCSI] = 7,
911960966dSMarkus Armbruster };
921960966dSMarkus Armbruster 
9321dff8cfSJohn Snow /**
9421dff8cfSJohn Snow  * Boards may call this to offer board-by-board overrides
9521dff8cfSJohn Snow  * of the default, global values.
9621dff8cfSJohn Snow  */
9721dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs)
9821dff8cfSJohn Snow {
9918e46a03SMarkus Armbruster     BlockBackend *blk;
10021dff8cfSJohn Snow     DriveInfo *dinfo;
10121dff8cfSJohn Snow 
10221dff8cfSJohn Snow     if (max_devs <= 0) {
10321dff8cfSJohn Snow         return;
10421dff8cfSJohn Snow     }
10521dff8cfSJohn Snow 
10618e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
10718e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
10821dff8cfSJohn Snow         if (dinfo->type == type) {
10921dff8cfSJohn Snow             fprintf(stderr, "Cannot override units-per-bus property of"
11021dff8cfSJohn Snow                     " the %s interface, because a drive of that type has"
11121dff8cfSJohn Snow                     " already been added.\n", if_name[type]);
11221dff8cfSJohn Snow             g_assert_not_reached();
11321dff8cfSJohn Snow         }
11421dff8cfSJohn Snow     }
11521dff8cfSJohn Snow 
11621dff8cfSJohn Snow     if_max_devs[type] = max_devs;
11721dff8cfSJohn Snow }
11821dff8cfSJohn Snow 
11914bafc54SMarkus Armbruster /*
12014bafc54SMarkus Armbruster  * We automatically delete the drive when a device using it gets
12114bafc54SMarkus Armbruster  * unplugged.  Questionable feature, but we can't just drop it.
12214bafc54SMarkus Armbruster  * Device models call blockdev_mark_auto_del() to schedule the
12314bafc54SMarkus Armbruster  * automatic deletion, and generic qdev code calls blockdev_auto_del()
12414bafc54SMarkus Armbruster  * when deletion is actually safe.
12514bafc54SMarkus Armbruster  */
1264be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk)
12714bafc54SMarkus Armbruster {
12818e46a03SMarkus Armbruster     DriveInfo *dinfo = blk_legacy_dinfo(blk);
1294be74634SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
13091fddb0dSStefan Hajnoczi     AioContext *aio_context;
13114bafc54SMarkus Armbruster 
13226f8b3a8SMarkus Armbruster     if (!dinfo) {
1332d246f01SKevin Wolf         return;
1342d246f01SKevin Wolf     }
1352d246f01SKevin Wolf 
1365433c24fSMax Reitz     if (bs) {
13791fddb0dSStefan Hajnoczi         aio_context = bdrv_get_aio_context(bs);
13891fddb0dSStefan Hajnoczi         aio_context_acquire(aio_context);
13991fddb0dSStefan Hajnoczi 
14012bde0eeSPaolo Bonzini         if (bs->job) {
14112bde0eeSPaolo Bonzini             block_job_cancel(bs->job);
14212bde0eeSPaolo Bonzini         }
14391fddb0dSStefan Hajnoczi 
14491fddb0dSStefan Hajnoczi         aio_context_release(aio_context);
1455433c24fSMax Reitz     }
14691fddb0dSStefan Hajnoczi 
14714bafc54SMarkus Armbruster     dinfo->auto_del = 1;
14814bafc54SMarkus Armbruster }
14914bafc54SMarkus Armbruster 
1504be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk)
15114bafc54SMarkus Armbruster {
15218e46a03SMarkus Armbruster     DriveInfo *dinfo = blk_legacy_dinfo(blk);
15314bafc54SMarkus Armbruster 
1540fc0f1faSRyan Harper     if (dinfo && dinfo->auto_del) {
155efaa7c4eSMax Reitz         monitor_remove_blk(blk);
156b9fe8a7aSMarkus Armbruster         blk_unref(blk);
15714bafc54SMarkus Armbruster     }
15814bafc54SMarkus Armbruster }
15914bafc54SMarkus Armbruster 
160d8f94e1bSJohn Snow /**
161d8f94e1bSJohn Snow  * Returns the current mapping of how many units per bus
162d8f94e1bSJohn Snow  * a particular interface can support.
163d8f94e1bSJohn Snow  *
164d8f94e1bSJohn Snow  *  A positive integer indicates n units per bus.
165d8f94e1bSJohn Snow  *  0 implies the mapping has not been established.
166d8f94e1bSJohn Snow  * -1 indicates an invalid BlockInterfaceType was given.
167d8f94e1bSJohn Snow  */
168d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type)
169d8f94e1bSJohn Snow {
170d8f94e1bSJohn Snow     if (type >= IF_IDE && type < IF_COUNT) {
171d8f94e1bSJohn Snow         return if_max_devs[type];
172d8f94e1bSJohn Snow     }
173d8f94e1bSJohn Snow 
174d8f94e1bSJohn Snow     return -1;
175d8f94e1bSJohn Snow }
176d8f94e1bSJohn Snow 
177505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index)
178505a7fb1SMarkus Armbruster {
179505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
180505a7fb1SMarkus Armbruster     return max_devs ? index / max_devs : 0;
181505a7fb1SMarkus Armbruster }
182505a7fb1SMarkus Armbruster 
183505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index)
184505a7fb1SMarkus Armbruster {
185505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
186505a7fb1SMarkus Armbruster     return max_devs ? index % max_devs : index;
187505a7fb1SMarkus Armbruster }
188505a7fb1SMarkus Armbruster 
1892292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr)
1902292ddaeSMarkus Armbruster {
19170b94331SMarkus Armbruster     return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
1922292ddaeSMarkus Armbruster }
1932292ddaeSMarkus Armbruster 
1942292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
1955645b0f4SMarkus Armbruster                     const char *optstr)
196666daa68SMarkus Armbruster {
197666daa68SMarkus Armbruster     QemuOpts *opts;
198666daa68SMarkus Armbruster 
1992292ddaeSMarkus Armbruster     opts = drive_def(optstr);
200666daa68SMarkus Armbruster     if (!opts) {
201666daa68SMarkus Armbruster         return NULL;
202666daa68SMarkus Armbruster     }
2032292ddaeSMarkus Armbruster     if (type != IF_DEFAULT) {
204f43e47dbSMarkus Armbruster         qemu_opt_set(opts, "if", if_name[type], &error_abort);
2052292ddaeSMarkus Armbruster     }
2062292ddaeSMarkus Armbruster     if (index >= 0) {
207a8b18f8fSMarkus Armbruster         qemu_opt_set_number(opts, "index", index, &error_abort);
2082292ddaeSMarkus Armbruster     }
209666daa68SMarkus Armbruster     if (file)
210f43e47dbSMarkus Armbruster         qemu_opt_set(opts, "file", file, &error_abort);
211666daa68SMarkus Armbruster     return opts;
212666daa68SMarkus Armbruster }
213666daa68SMarkus Armbruster 
214666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
215666daa68SMarkus Armbruster {
21618e46a03SMarkus Armbruster     BlockBackend *blk;
217666daa68SMarkus Armbruster     DriveInfo *dinfo;
218666daa68SMarkus Armbruster 
21918e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
22018e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
22118e46a03SMarkus Armbruster         if (dinfo && dinfo->type == type
22218e46a03SMarkus Armbruster             && dinfo->bus == bus && dinfo->unit == unit) {
223666daa68SMarkus Armbruster             return dinfo;
224666daa68SMarkus Armbruster         }
22518e46a03SMarkus Armbruster     }
226666daa68SMarkus Armbruster 
227666daa68SMarkus Armbruster     return NULL;
228666daa68SMarkus Armbruster }
229666daa68SMarkus Armbruster 
230a66c9dc7SJohn Snow bool drive_check_orphaned(void)
231a66c9dc7SJohn Snow {
23218e46a03SMarkus Armbruster     BlockBackend *blk;
233a66c9dc7SJohn Snow     DriveInfo *dinfo;
234a66c9dc7SJohn Snow     bool rs = false;
235a66c9dc7SJohn Snow 
23618e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
23718e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
238a66c9dc7SJohn Snow         /* If dinfo->bdrv->dev is NULL, it has no device attached. */
239a66c9dc7SJohn Snow         /* Unless this is a default drive, this may be an oversight. */
240a7f53e26SMarkus Armbruster         if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
241a66c9dc7SJohn Snow             dinfo->type != IF_NONE) {
242a66c9dc7SJohn Snow             fprintf(stderr, "Warning: Orphaned drive without device: "
243a66c9dc7SJohn Snow                     "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
2445433c24fSMax Reitz                     blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
2455433c24fSMax Reitz                     if_name[dinfo->type], dinfo->bus, dinfo->unit);
246a66c9dc7SJohn Snow             rs = true;
247a66c9dc7SJohn Snow         }
248a66c9dc7SJohn Snow     }
249a66c9dc7SJohn Snow 
250a66c9dc7SJohn Snow     return rs;
251a66c9dc7SJohn Snow }
252a66c9dc7SJohn Snow 
253f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
254f1bd51acSMarkus Armbruster {
255f1bd51acSMarkus Armbruster     return drive_get(type,
256f1bd51acSMarkus Armbruster                      drive_index_to_bus_id(type, index),
257f1bd51acSMarkus Armbruster                      drive_index_to_unit_id(type, index));
258f1bd51acSMarkus Armbruster }
259f1bd51acSMarkus Armbruster 
260666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type)
261666daa68SMarkus Armbruster {
262666daa68SMarkus Armbruster     int max_bus;
26318e46a03SMarkus Armbruster     BlockBackend *blk;
264666daa68SMarkus Armbruster     DriveInfo *dinfo;
265666daa68SMarkus Armbruster 
266666daa68SMarkus Armbruster     max_bus = -1;
26718e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
26818e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
26918e46a03SMarkus Armbruster         if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
270666daa68SMarkus Armbruster             max_bus = dinfo->bus;
271666daa68SMarkus Armbruster         }
27218e46a03SMarkus Armbruster     }
273666daa68SMarkus Armbruster     return max_bus;
274666daa68SMarkus Armbruster }
275666daa68SMarkus Armbruster 
27613839974SMarkus Armbruster /* Get a block device.  This should only be used for single-drive devices
27713839974SMarkus Armbruster    (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
27813839974SMarkus Armbruster    appropriate bus.  */
27913839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type)
28013839974SMarkus Armbruster {
28113839974SMarkus Armbruster     static int next_block_unit[IF_COUNT];
28213839974SMarkus Armbruster 
28313839974SMarkus Armbruster     return drive_get(type, 0, next_block_unit[type]++);
28413839974SMarkus Armbruster }
28513839974SMarkus Armbruster 
286666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name)
287666daa68SMarkus Armbruster {
288807105a7SMarkus Armbruster     error_printf(" %s", name);
289666daa68SMarkus Armbruster }
290666daa68SMarkus Armbruster 
291aa398a5cSStefan Hajnoczi typedef struct {
292aa398a5cSStefan Hajnoczi     QEMUBH *bh;
293fa510ebfSFam Zheng     BlockDriverState *bs;
294fa510ebfSFam Zheng } BDRVPutRefBH;
295aa398a5cSStefan Hajnoczi 
296b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
297666daa68SMarkus Armbruster {
298666daa68SMarkus Armbruster     if (!strcmp(buf, "ignore")) {
29992aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_IGNORE;
300666daa68SMarkus Armbruster     } else if (!is_read && !strcmp(buf, "enospc")) {
30192aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_ENOSPC;
302666daa68SMarkus Armbruster     } else if (!strcmp(buf, "stop")) {
30392aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_STOP;
304666daa68SMarkus Armbruster     } else if (!strcmp(buf, "report")) {
30592aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_REPORT;
306666daa68SMarkus Armbruster     } else {
307b681072dSKevin Wolf         error_setg(errp, "'%s' invalid %s error action",
308666daa68SMarkus Armbruster                    buf, is_read ? "read" : "write");
309666daa68SMarkus Armbruster         return -1;
310666daa68SMarkus Armbruster     }
311666daa68SMarkus Armbruster }
312666daa68SMarkus Armbruster 
31340119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
31440119effSAlberto Garcia                                   Error **errp)
31540119effSAlberto Garcia {
31640119effSAlberto Garcia     const QListEntry *entry;
31740119effSAlberto Garcia     for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
31840119effSAlberto Garcia         switch (qobject_type(entry->value)) {
31940119effSAlberto Garcia 
32040119effSAlberto Garcia         case QTYPE_QSTRING: {
32140119effSAlberto Garcia             unsigned long long length;
32240119effSAlberto Garcia             const char *str = qstring_get_str(qobject_to_qstring(entry->value));
32340119effSAlberto Garcia             if (parse_uint_full(str, &length, 10) == 0 &&
32440119effSAlberto Garcia                 length > 0 && length <= UINT_MAX) {
32540119effSAlberto Garcia                 block_acct_add_interval(stats, (unsigned) length);
32640119effSAlberto Garcia             } else {
32740119effSAlberto Garcia                 error_setg(errp, "Invalid interval length: %s", str);
32840119effSAlberto Garcia                 return false;
32940119effSAlberto Garcia             }
33040119effSAlberto Garcia             break;
33140119effSAlberto Garcia         }
33240119effSAlberto Garcia 
33340119effSAlberto Garcia         case QTYPE_QINT: {
33440119effSAlberto Garcia             int64_t length = qint_get_int(qobject_to_qint(entry->value));
33540119effSAlberto Garcia             if (length > 0 && length <= UINT_MAX) {
33640119effSAlberto Garcia                 block_acct_add_interval(stats, (unsigned) length);
33740119effSAlberto Garcia             } else {
33840119effSAlberto Garcia                 error_setg(errp, "Invalid interval length: %" PRId64, length);
33940119effSAlberto Garcia                 return false;
34040119effSAlberto Garcia             }
34140119effSAlberto Garcia             break;
34240119effSAlberto Garcia         }
34340119effSAlberto Garcia 
34440119effSAlberto Garcia         default:
34540119effSAlberto Garcia             error_setg(errp, "The specification of stats-intervals is invalid");
34640119effSAlberto Garcia             return false;
34740119effSAlberto Garcia         }
34840119effSAlberto Garcia     }
34940119effSAlberto Garcia     return true;
35040119effSAlberto Garcia }
35140119effSAlberto Garcia 
35233cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
35333cb7dc8SKevin Wolf 
354fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */
355fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
356fbf8175eSMax Reitz     const char **throttling_group, ThrottleConfig *throttle_cfg,
357fbf8175eSMax Reitz     BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
358fbf8175eSMax Reitz {
359fbf8175eSMax Reitz     Error *local_error = NULL;
360fbf8175eSMax Reitz     const char *aio;
361fbf8175eSMax Reitz 
362fbf8175eSMax Reitz     if (bdrv_flags) {
363fbf8175eSMax Reitz         if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
364fbf8175eSMax Reitz             *bdrv_flags |= BDRV_O_COPY_ON_READ;
365fbf8175eSMax Reitz         }
366fbf8175eSMax Reitz 
367fbf8175eSMax Reitz         if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
368fbf8175eSMax Reitz             if (!strcmp(aio, "native")) {
369fbf8175eSMax Reitz                 *bdrv_flags |= BDRV_O_NATIVE_AIO;
370fbf8175eSMax Reitz             } else if (!strcmp(aio, "threads")) {
371fbf8175eSMax Reitz                 /* this is the default */
372fbf8175eSMax Reitz             } else {
373fbf8175eSMax Reitz                error_setg(errp, "invalid aio option");
374fbf8175eSMax Reitz                return;
375fbf8175eSMax Reitz             }
376fbf8175eSMax Reitz         }
377fbf8175eSMax Reitz     }
378fbf8175eSMax Reitz 
379fbf8175eSMax Reitz     /* disk I/O throttling */
380fbf8175eSMax Reitz     if (throttling_group) {
381fbf8175eSMax Reitz         *throttling_group = qemu_opt_get(opts, "throttling.group");
382fbf8175eSMax Reitz     }
383fbf8175eSMax Reitz 
384fbf8175eSMax Reitz     if (throttle_cfg) {
3851588ab5dSAlberto Garcia         throttle_config_init(throttle_cfg);
386fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
387fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-total", 0);
388fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_READ].avg  =
389fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-read", 0);
390fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
391fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-write", 0);
392fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
393fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-total", 0);
394fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
395fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-read", 0);
396fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
397fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-write", 0);
398fbf8175eSMax Reitz 
399fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
400fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
401fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_READ].max  =
402fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
403fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
404fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
405fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
406fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
407fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_READ].max =
408fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
409fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
410fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
411fbf8175eSMax Reitz 
4128a0fc18dSAlberto Garcia         throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
4138a0fc18dSAlberto Garcia             qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
4148a0fc18dSAlberto Garcia         throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length  =
4158a0fc18dSAlberto Garcia             qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
4168a0fc18dSAlberto Garcia         throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
4178a0fc18dSAlberto Garcia             qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
4188a0fc18dSAlberto Garcia         throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
4198a0fc18dSAlberto Garcia             qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
4208a0fc18dSAlberto Garcia         throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
4218a0fc18dSAlberto Garcia             qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
4228a0fc18dSAlberto Garcia         throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
4238a0fc18dSAlberto Garcia             qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
4248a0fc18dSAlberto Garcia 
425fbf8175eSMax Reitz         throttle_cfg->op_size =
426fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-size", 0);
427fbf8175eSMax Reitz 
428d5851089SAlberto Garcia         if (!throttle_is_valid(throttle_cfg, errp)) {
429fbf8175eSMax Reitz             return;
430fbf8175eSMax Reitz         }
431fbf8175eSMax Reitz     }
432fbf8175eSMax Reitz 
433fbf8175eSMax Reitz     if (detect_zeroes) {
434fbf8175eSMax Reitz         *detect_zeroes =
435fbf8175eSMax Reitz             qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
436fbf8175eSMax Reitz                             qemu_opt_get(opts, "detect-zeroes"),
4377fb1cf16SEric Blake                             BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
438fbf8175eSMax Reitz                             BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
439fbf8175eSMax Reitz                             &local_error);
440fbf8175eSMax Reitz         if (local_error) {
441fbf8175eSMax Reitz             error_propagate(errp, local_error);
442fbf8175eSMax Reitz             return;
443fbf8175eSMax Reitz         }
444fbf8175eSMax Reitz     }
445fbf8175eSMax Reitz }
446fbf8175eSMax Reitz 
447f298d071SKevin Wolf /* Takes the ownership of bs_opts */
44818e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
449b681072dSKevin Wolf                                    Error **errp)
450666daa68SMarkus Armbruster {
451666daa68SMarkus Armbruster     const char *buf;
452666daa68SMarkus Armbruster     int bdrv_flags = 0;
453666daa68SMarkus Armbruster     int on_read_error, on_write_error;
454362e9299SAlberto Garcia     bool account_invalid, account_failed;
455f87a0e29SAlberto Garcia     bool writethrough, read_only;
45626f54e9aSMarkus Armbruster     BlockBackend *blk;
457a0f1eab1SMarkus Armbruster     BlockDriverState *bs;
458cc0681c4SBenoît Canet     ThrottleConfig cfg;
459666daa68SMarkus Armbruster     int snapshot = 0;
460c546194fSStefan Hajnoczi     Error *error = NULL;
4610006383eSKevin Wolf     QemuOpts *opts;
46240119effSAlberto Garcia     QDict *interval_dict = NULL;
46340119effSAlberto Garcia     QList *interval_list = NULL;
4640006383eSKevin Wolf     const char *id;
465fbf8175eSMax Reitz     BlockdevDetectZeroesOptions detect_zeroes =
466fbf8175eSMax Reitz         BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
467fbf8175eSMax Reitz     const char *throttling_group = NULL;
468666daa68SMarkus Armbruster 
469f298d071SKevin Wolf     /* Check common options by copying from bs_opts to opts, all other options
470f298d071SKevin Wolf      * stay in bs_opts for processing by bdrv_open(). */
471f298d071SKevin Wolf     id = qdict_get_try_str(bs_opts, "id");
4720006383eSKevin Wolf     opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
47384d18f06SMarkus Armbruster     if (error) {
474b681072dSKevin Wolf         error_propagate(errp, error);
4756376f952SMarkus Armbruster         goto err_no_opts;
4760006383eSKevin Wolf     }
4770006383eSKevin Wolf 
4780006383eSKevin Wolf     qemu_opts_absorb_qdict(opts, bs_opts, &error);
47984d18f06SMarkus Armbruster     if (error) {
480b681072dSKevin Wolf         error_propagate(errp, error);
481ec9c10d2SStefan Hajnoczi         goto early_err;
4820006383eSKevin Wolf     }
4830006383eSKevin Wolf 
4840006383eSKevin Wolf     if (id) {
4850006383eSKevin Wolf         qdict_del(bs_opts, "id");
4860006383eSKevin Wolf     }
4870006383eSKevin Wolf 
488666daa68SMarkus Armbruster     /* extract parameters */
489666daa68SMarkus Armbruster     snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
490666daa68SMarkus Armbruster 
491362e9299SAlberto Garcia     account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
492362e9299SAlberto Garcia     account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
493362e9299SAlberto Garcia 
494e4b24b49SKevin Wolf     writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
495e4b24b49SKevin Wolf 
496ff356ee4SAlberto Garcia     id = qemu_opts_id(opts);
497ff356ee4SAlberto Garcia 
49840119effSAlberto Garcia     qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
49940119effSAlberto Garcia     qdict_array_split(interval_dict, &interval_list);
50040119effSAlberto Garcia 
50140119effSAlberto Garcia     if (qdict_size(interval_dict) != 0) {
50240119effSAlberto Garcia         error_setg(errp, "Invalid option stats-intervals.%s",
50340119effSAlberto Garcia                    qdict_first(interval_dict)->key);
50440119effSAlberto Garcia         goto early_err;
50540119effSAlberto Garcia     }
5062be5506fSAlberto Garcia 
507fbf8175eSMax Reitz     extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
508fbf8175eSMax Reitz                                     &detect_zeroes, &error);
509fbf8175eSMax Reitz     if (error) {
510fbf8175eSMax Reitz         error_propagate(errp, error);
511ec9c10d2SStefan Hajnoczi         goto early_err;
512a9384affSPaolo Bonzini     }
513666daa68SMarkus Armbruster 
514666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "format")) != NULL) {
515c8057f95SPeter Maydell         if (is_help_option(buf)) {
516807105a7SMarkus Armbruster             error_printf("Supported formats:");
517666daa68SMarkus Armbruster             bdrv_iterate_format(bdrv_format_print, NULL);
518807105a7SMarkus Armbruster             error_printf("\n");
519ec9c10d2SStefan Hajnoczi             goto early_err;
520666daa68SMarkus Armbruster         }
52174fe54f2SKevin Wolf 
522e4342ce5SMax Reitz         if (qdict_haskey(bs_opts, "driver")) {
523e4342ce5SMax Reitz             error_setg(errp, "Cannot specify both 'driver' and 'format'");
524ec9c10d2SStefan Hajnoczi             goto early_err;
5256db5f5d6SMike Qiu         }
526e4342ce5SMax Reitz         qdict_put(bs_opts, "driver", qstring_from_str(buf));
527666daa68SMarkus Armbruster     }
528666daa68SMarkus Armbruster 
52992aa5c6dSPaolo Bonzini     on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
530666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
531b681072dSKevin Wolf         on_write_error = parse_block_error_action(buf, 0, &error);
53284d18f06SMarkus Armbruster         if (error) {
533b681072dSKevin Wolf             error_propagate(errp, error);
534ec9c10d2SStefan Hajnoczi             goto early_err;
535666daa68SMarkus Armbruster         }
536666daa68SMarkus Armbruster     }
537666daa68SMarkus Armbruster 
53892aa5c6dSPaolo Bonzini     on_read_error = BLOCKDEV_ON_ERROR_REPORT;
539666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
540b681072dSKevin Wolf         on_read_error = parse_block_error_action(buf, 1, &error);
54184d18f06SMarkus Armbruster         if (error) {
542b681072dSKevin Wolf             error_propagate(errp, error);
543ec9c10d2SStefan Hajnoczi             goto early_err;
544666daa68SMarkus Armbruster         }
545666daa68SMarkus Armbruster     }
546666daa68SMarkus Armbruster 
547666daa68SMarkus Armbruster     if (snapshot) {
54891a097e7SKevin Wolf         bdrv_flags |= BDRV_O_SNAPSHOT;
549666daa68SMarkus Armbruster     }
550666daa68SMarkus Armbruster 
551f87a0e29SAlberto Garcia     read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
552f87a0e29SAlberto Garcia 
5535ec18f8cSMax Reitz     /* init */
55439c4ae94SKevin Wolf     if ((!file || !*file) && !qdict_size(bs_opts)) {
5555ec18f8cSMax Reitz         BlockBackendRootState *blk_rs;
5565ec18f8cSMax Reitz 
557109525adSMax Reitz         blk = blk_new();
5585ec18f8cSMax Reitz         blk_rs = blk_get_root_state(blk);
5595ec18f8cSMax Reitz         blk_rs->open_flags    = bdrv_flags;
560f87a0e29SAlberto Garcia         blk_rs->read_only     = read_only;
5615ec18f8cSMax Reitz         blk_rs->detect_zeroes = detect_zeroes;
5625ec18f8cSMax Reitz 
5635ec18f8cSMax Reitz         QDECREF(bs_opts);
5645ec18f8cSMax Reitz     } else {
5655ec18f8cSMax Reitz         if (file && !*file) {
5665ec18f8cSMax Reitz             file = NULL;
5675ec18f8cSMax Reitz         }
5685ec18f8cSMax Reitz 
56991a097e7SKevin Wolf         /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
57091a097e7SKevin Wolf          * with other callers) rather than what we want as the real defaults.
57191a097e7SKevin Wolf          * Apply the defaults here instead. */
57291a097e7SKevin Wolf         qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
57391a097e7SKevin Wolf         qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
574f87a0e29SAlberto Garcia         qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
575f87a0e29SAlberto Garcia                               read_only ? "on" : "off");
57672e775c7SKevin Wolf         assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
57791a097e7SKevin Wolf 
57812d5ee3aSKevin Wolf         if (runstate_check(RUN_STATE_INMIGRATE)) {
57912d5ee3aSKevin Wolf             bdrv_flags |= BDRV_O_INACTIVE;
58012d5ee3aSKevin Wolf         }
58112d5ee3aSKevin Wolf 
582efaa7c4eSMax Reitz         blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
583e4342ce5SMax Reitz         if (!blk) {
584e4342ce5SMax Reitz             goto err_no_bs_opts;
585e4342ce5SMax Reitz         }
586e4342ce5SMax Reitz         bs = blk_bs(blk);
5870006383eSKevin Wolf 
588e4342ce5SMax Reitz         bs->detect_zeroes = detect_zeroes;
589e4342ce5SMax Reitz 
590a0f1eab1SMarkus Armbruster         if (bdrv_key_required(bs)) {
591666daa68SMarkus Armbruster             autostart = 0;
592a0f1eab1SMarkus Armbruster         }
593362e9299SAlberto Garcia 
594362e9299SAlberto Garcia         block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
5952be5506fSAlberto Garcia 
59640119effSAlberto Garcia         if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
5972be5506fSAlberto Garcia             blk_unref(blk);
5982be5506fSAlberto Garcia             blk = NULL;
5992be5506fSAlberto Garcia             goto err_no_bs_opts;
6002be5506fSAlberto Garcia         }
6012be5506fSAlberto Garcia     }
6025ec18f8cSMax Reitz 
6037ca7f0f6SKevin Wolf     /* disk I/O throttling */
6047ca7f0f6SKevin Wolf     if (throttle_enabled(&cfg)) {
6057ca7f0f6SKevin Wolf         if (!throttling_group) {
606ff356ee4SAlberto Garcia             throttling_group = id;
6077ca7f0f6SKevin Wolf         }
6087ca7f0f6SKevin Wolf         blk_io_limits_enable(blk, throttling_group);
6097ca7f0f6SKevin Wolf         blk_set_io_limits(blk, &cfg);
6107ca7f0f6SKevin Wolf     }
6117ca7f0f6SKevin Wolf 
612e4b24b49SKevin Wolf     blk_set_enable_write_cache(blk, !writethrough);
6135ec18f8cSMax Reitz     blk_set_on_error(blk, on_read_error, on_write_error);
6140006383eSKevin Wolf 
615ff356ee4SAlberto Garcia     if (!monitor_add_blk(blk, id, errp)) {
616efaa7c4eSMax Reitz         blk_unref(blk);
617efaa7c4eSMax Reitz         blk = NULL;
618efaa7c4eSMax Reitz         goto err_no_bs_opts;
619efaa7c4eSMax Reitz     }
620efaa7c4eSMax Reitz 
621e4342ce5SMax Reitz err_no_bs_opts:
6220006383eSKevin Wolf     qemu_opts_del(opts);
62340119effSAlberto Garcia     QDECREF(interval_dict);
62440119effSAlberto Garcia     QDECREF(interval_list);
62518e46a03SMarkus Armbruster     return blk;
626a9ae2bffSMarkus Armbruster 
627ec9c10d2SStefan Hajnoczi early_err:
628ec9c10d2SStefan Hajnoczi     qemu_opts_del(opts);
62940119effSAlberto Garcia     QDECREF(interval_dict);
63040119effSAlberto Garcia     QDECREF(interval_list);
6316376f952SMarkus Armbruster err_no_opts:
6326376f952SMarkus Armbruster     QDECREF(bs_opts);
633a9ae2bffSMarkus Armbruster     return NULL;
634666daa68SMarkus Armbruster }
635666daa68SMarkus Armbruster 
636bd745e23SMax Reitz /* Takes the ownership of bs_opts */
637bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
638bd745e23SMax Reitz {
639bd745e23SMax Reitz     int bdrv_flags = 0;
640bd745e23SMax Reitz 
641a81d6164SKevin Wolf     /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
642a81d6164SKevin Wolf      * with other callers) rather than what we want as the real defaults.
643a81d6164SKevin Wolf      * Apply the defaults here instead. */
644a81d6164SKevin Wolf     qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
645a81d6164SKevin Wolf     qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
646f87a0e29SAlberto Garcia     qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
647a81d6164SKevin Wolf 
64812d5ee3aSKevin Wolf     if (runstate_check(RUN_STATE_INMIGRATE)) {
64912d5ee3aSKevin Wolf         bdrv_flags |= BDRV_O_INACTIVE;
65012d5ee3aSKevin Wolf     }
65112d5ee3aSKevin Wolf 
652*74e1ae7cSKevin Wolf     return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
653bd745e23SMax Reitz }
654bd745e23SMax Reitz 
6559c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void)
6569c4218e9SMax Reitz {
6579c4218e9SMax Reitz     BlockDriverState *bs, *next_bs;
6589c4218e9SMax Reitz 
6599c4218e9SMax Reitz     QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
6609c4218e9SMax Reitz         AioContext *ctx = bdrv_get_aio_context(bs);
6619c4218e9SMax Reitz 
6629c4218e9SMax Reitz         aio_context_acquire(ctx);
6639c4218e9SMax Reitz         bdrv_unref(bs);
6649c4218e9SMax Reitz         aio_context_release(ctx);
6659c4218e9SMax Reitz     }
6669c4218e9SMax Reitz }
6679c4218e9SMax Reitz 
668262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */
669262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
670262b4e8fSMax Reitz {
671262b4e8fSMax Reitz     return bs ? QTAILQ_NEXT(bs, monitor_list)
672262b4e8fSMax Reitz               : QTAILQ_FIRST(&monitor_bdrv_states);
673262b4e8fSMax Reitz }
674262b4e8fSMax Reitz 
6755abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
6765abbf0eeSKevin Wolf                             Error **errp)
67757975222SKevin Wolf {
67857975222SKevin Wolf     const char *value;
67957975222SKevin Wolf 
68057975222SKevin Wolf     value = qemu_opt_get(opts, from);
68157975222SKevin Wolf     if (value) {
6825abbf0eeSKevin Wolf         if (qemu_opt_find(opts, to)) {
6835abbf0eeSKevin Wolf             error_setg(errp, "'%s' and its alias '%s' can't be used at the "
6845abbf0eeSKevin Wolf                        "same time", to, from);
6855abbf0eeSKevin Wolf             return;
6865abbf0eeSKevin Wolf         }
68720d6cd47SJun Li     }
68820d6cd47SJun Li 
68920d6cd47SJun Li     /* rename all items in opts */
69020d6cd47SJun Li     while ((value = qemu_opt_get(opts, from))) {
691f43e47dbSMarkus Armbruster         qemu_opt_set(opts, to, value, &error_abort);
69257975222SKevin Wolf         qemu_opt_unset(opts, from);
69357975222SKevin Wolf     }
69457975222SKevin Wolf }
69557975222SKevin Wolf 
69633cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = {
69733cb7dc8SKevin Wolf     .name = "drive",
69833cb7dc8SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
69933cb7dc8SKevin Wolf     .desc = {
70033cb7dc8SKevin Wolf         {
70187a899c5SKevin Wolf             .name = "bus",
70287a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
70387a899c5SKevin Wolf             .help = "bus number",
70487a899c5SKevin Wolf         },{
70587a899c5SKevin Wolf             .name = "unit",
70687a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
70787a899c5SKevin Wolf             .help = "unit number (i.e. lun for scsi)",
70887a899c5SKevin Wolf         },{
70987a899c5SKevin Wolf             .name = "index",
71087a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
71187a899c5SKevin Wolf             .help = "index number",
71287a899c5SKevin Wolf         },{
71333cb7dc8SKevin Wolf             .name = "media",
71433cb7dc8SKevin Wolf             .type = QEMU_OPT_STRING,
71533cb7dc8SKevin Wolf             .help = "media type (disk, cdrom)",
716593d464bSKevin Wolf         },{
717593d464bSKevin Wolf             .name = "if",
718593d464bSKevin Wolf             .type = QEMU_OPT_STRING,
719593d464bSKevin Wolf             .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
720b41a7338SKevin Wolf         },{
721b41a7338SKevin Wolf             .name = "cyls",
722b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
723b41a7338SKevin Wolf             .help = "number of cylinders (ide disk geometry)",
724b41a7338SKevin Wolf         },{
725b41a7338SKevin Wolf             .name = "heads",
726b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
727b41a7338SKevin Wolf             .help = "number of heads (ide disk geometry)",
728b41a7338SKevin Wolf         },{
729b41a7338SKevin Wolf             .name = "secs",
730b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
731b41a7338SKevin Wolf             .help = "number of sectors (ide disk geometry)",
732b41a7338SKevin Wolf         },{
733b41a7338SKevin Wolf             .name = "trans",
734b41a7338SKevin Wolf             .type = QEMU_OPT_STRING,
735b41a7338SKevin Wolf             .help = "chs translation (auto, lba, none)",
73626929298SKevin Wolf         },{
73726929298SKevin Wolf             .name = "boot",
73826929298SKevin Wolf             .type = QEMU_OPT_BOOL,
73926929298SKevin Wolf             .help = "(deprecated, ignored)",
740394c7d4dSKevin Wolf         },{
741394c7d4dSKevin Wolf             .name = "addr",
742394c7d4dSKevin Wolf             .type = QEMU_OPT_STRING,
743394c7d4dSKevin Wolf             .help = "pci address (virtio only)",
744d095b465SMax Reitz         },{
745bcf83158SKevin Wolf             .name = "serial",
746bcf83158SKevin Wolf             .type = QEMU_OPT_STRING,
747bcf83158SKevin Wolf             .help = "disk serial number",
748bcf83158SKevin Wolf         },{
749d095b465SMax Reitz             .name = "file",
750d095b465SMax Reitz             .type = QEMU_OPT_STRING,
751d095b465SMax Reitz             .help = "file name",
75233cb7dc8SKevin Wolf         },
7530ebd24e0SKevin Wolf 
7540ebd24e0SKevin Wolf         /* Options that are passed on, but have special semantics with -drive */
7550ebd24e0SKevin Wolf         {
7564e200cf8SAlberto Garcia             .name = BDRV_OPT_READ_ONLY,
7570ebd24e0SKevin Wolf             .type = QEMU_OPT_BOOL,
7580ebd24e0SKevin Wolf             .help = "open drive file as read-only",
7590ebd24e0SKevin Wolf         },{
760ee13ed1cSKevin Wolf             .name = "rerror",
761ee13ed1cSKevin Wolf             .type = QEMU_OPT_STRING,
762ee13ed1cSKevin Wolf             .help = "read error action",
763ee13ed1cSKevin Wolf         },{
764ee13ed1cSKevin Wolf             .name = "werror",
765ee13ed1cSKevin Wolf             .type = QEMU_OPT_STRING,
766ee13ed1cSKevin Wolf             .help = "write error action",
767ee13ed1cSKevin Wolf         },{
7680ebd24e0SKevin Wolf             .name = "copy-on-read",
7690ebd24e0SKevin Wolf             .type = QEMU_OPT_BOOL,
7700ebd24e0SKevin Wolf             .help = "copy read data from backing file into image file",
7710ebd24e0SKevin Wolf         },
7720ebd24e0SKevin Wolf 
77333cb7dc8SKevin Wolf         { /* end of list */ }
77433cb7dc8SKevin Wolf     },
77533cb7dc8SKevin Wolf };
77633cb7dc8SKevin Wolf 
77760e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
77857975222SKevin Wolf {
77929c4e2b5SKevin Wolf     const char *value;
78018e46a03SMarkus Armbruster     BlockBackend *blk;
78133cb7dc8SKevin Wolf     DriveInfo *dinfo = NULL;
782f298d071SKevin Wolf     QDict *bs_opts;
78333cb7dc8SKevin Wolf     QemuOpts *legacy_opts;
78433cb7dc8SKevin Wolf     DriveMediaType media = MEDIA_DISK;
785593d464bSKevin Wolf     BlockInterfaceType type;
786b41a7338SKevin Wolf     int cyls, heads, secs, translation;
78787a899c5SKevin Wolf     int max_devs, bus_id, unit_id, index;
788394c7d4dSKevin Wolf     const char *devaddr;
789ee13ed1cSKevin Wolf     const char *werror, *rerror;
790a7fdbcf0SFam Zheng     bool read_only = false;
791a7fdbcf0SFam Zheng     bool copy_on_read;
792bcf83158SKevin Wolf     const char *serial;
793d095b465SMax Reitz     const char *filename;
79433cb7dc8SKevin Wolf     Error *local_err = NULL;
795247147fbSKevin Wolf     int i;
79629c4e2b5SKevin Wolf 
79757975222SKevin Wolf     /* Change legacy command line options into QMP ones */
798247147fbSKevin Wolf     static const struct {
799247147fbSKevin Wolf         const char *from;
800247147fbSKevin Wolf         const char *to;
801247147fbSKevin Wolf     } opt_renames[] = {
802247147fbSKevin Wolf         { "iops",           "throttling.iops-total" },
803247147fbSKevin Wolf         { "iops_rd",        "throttling.iops-read" },
804247147fbSKevin Wolf         { "iops_wr",        "throttling.iops-write" },
80557975222SKevin Wolf 
806247147fbSKevin Wolf         { "bps",            "throttling.bps-total" },
807247147fbSKevin Wolf         { "bps_rd",         "throttling.bps-read" },
808247147fbSKevin Wolf         { "bps_wr",         "throttling.bps-write" },
80957975222SKevin Wolf 
810247147fbSKevin Wolf         { "iops_max",       "throttling.iops-total-max" },
811247147fbSKevin Wolf         { "iops_rd_max",    "throttling.iops-read-max" },
812247147fbSKevin Wolf         { "iops_wr_max",    "throttling.iops-write-max" },
8133e9fab69SBenoît Canet 
814247147fbSKevin Wolf         { "bps_max",        "throttling.bps-total-max" },
815247147fbSKevin Wolf         { "bps_rd_max",     "throttling.bps-read-max" },
816247147fbSKevin Wolf         { "bps_wr_max",     "throttling.bps-write-max" },
8173e9fab69SBenoît Canet 
818247147fbSKevin Wolf         { "iops_size",      "throttling.iops-size" },
8192024c1dfSBenoît Canet 
82076f4afb4SAlberto Garcia         { "group",          "throttling.group" },
82176f4afb4SAlberto Garcia 
8224e200cf8SAlberto Garcia         { "readonly",       BDRV_OPT_READ_ONLY },
823247147fbSKevin Wolf     };
824247147fbSKevin Wolf 
825247147fbSKevin Wolf     for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
8265abbf0eeSKevin Wolf         qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
8275abbf0eeSKevin Wolf                         &local_err);
8285abbf0eeSKevin Wolf         if (local_err) {
829565f65d2SMarkus Armbruster             error_report_err(local_err);
8305abbf0eeSKevin Wolf             return NULL;
8315abbf0eeSKevin Wolf         }
832247147fbSKevin Wolf     }
8330f227a94SKevin Wolf 
83429c4e2b5SKevin Wolf     value = qemu_opt_get(all_opts, "cache");
83529c4e2b5SKevin Wolf     if (value) {
83629c4e2b5SKevin Wolf         int flags = 0;
83704feb4a5SKevin Wolf         bool writethrough;
83829c4e2b5SKevin Wolf 
83904feb4a5SKevin Wolf         if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
84029c4e2b5SKevin Wolf             error_report("invalid cache option");
84129c4e2b5SKevin Wolf             return NULL;
84229c4e2b5SKevin Wolf         }
84329c4e2b5SKevin Wolf 
84429c4e2b5SKevin Wolf         /* Specific options take precedence */
84554861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
84654861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
84704feb4a5SKevin Wolf                               !writethrough, &error_abort);
84829c4e2b5SKevin Wolf         }
84954861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
85054861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
851cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_NOCACHE), &error_abort);
85229c4e2b5SKevin Wolf         }
85354861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
85454861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
855cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_NO_FLUSH), &error_abort);
85629c4e2b5SKevin Wolf         }
85729c4e2b5SKevin Wolf         qemu_opt_unset(all_opts, "cache");
85829c4e2b5SKevin Wolf     }
85929c4e2b5SKevin Wolf 
860f298d071SKevin Wolf     /* Get a QDict for processing the options */
861f298d071SKevin Wolf     bs_opts = qdict_new();
862f298d071SKevin Wolf     qemu_opts_to_qdict(all_opts, bs_opts);
863f298d071SKevin Wolf 
86487ea75d5SPeter Crosthwaite     legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
86587ea75d5SPeter Crosthwaite                                    &error_abort);
86633cb7dc8SKevin Wolf     qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
86784d18f06SMarkus Armbruster     if (local_err) {
868565f65d2SMarkus Armbruster         error_report_err(local_err);
86933cb7dc8SKevin Wolf         goto fail;
87033cb7dc8SKevin Wolf     }
87133cb7dc8SKevin Wolf 
87226929298SKevin Wolf     /* Deprecated option boot=[on|off] */
87326929298SKevin Wolf     if (qemu_opt_get(legacy_opts, "boot") != NULL) {
87426929298SKevin Wolf         fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
87526929298SKevin Wolf                 "ignored. Future versions will reject this parameter. Please "
87626929298SKevin Wolf                 "update your scripts.\n");
87726929298SKevin Wolf     }
87826929298SKevin Wolf 
87933cb7dc8SKevin Wolf     /* Media type */
88033cb7dc8SKevin Wolf     value = qemu_opt_get(legacy_opts, "media");
88133cb7dc8SKevin Wolf     if (value) {
88233cb7dc8SKevin Wolf         if (!strcmp(value, "disk")) {
88333cb7dc8SKevin Wolf             media = MEDIA_DISK;
88433cb7dc8SKevin Wolf         } else if (!strcmp(value, "cdrom")) {
88533cb7dc8SKevin Wolf             media = MEDIA_CDROM;
886a7fdbcf0SFam Zheng             read_only = true;
88733cb7dc8SKevin Wolf         } else {
88833cb7dc8SKevin Wolf             error_report("'%s' invalid media", value);
88933cb7dc8SKevin Wolf             goto fail;
89033cb7dc8SKevin Wolf         }
89133cb7dc8SKevin Wolf     }
89233cb7dc8SKevin Wolf 
8930ebd24e0SKevin Wolf     /* copy-on-read is disabled with a warning for read-only devices */
8944e200cf8SAlberto Garcia     read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
8950ebd24e0SKevin Wolf     copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
8960ebd24e0SKevin Wolf 
8970ebd24e0SKevin Wolf     if (read_only && copy_on_read) {
8980ebd24e0SKevin Wolf         error_report("warning: disabling copy-on-read on read-only drive");
8990ebd24e0SKevin Wolf         copy_on_read = false;
9000ebd24e0SKevin Wolf     }
9010ebd24e0SKevin Wolf 
9024e200cf8SAlberto Garcia     qdict_put(bs_opts, BDRV_OPT_READ_ONLY,
9030ebd24e0SKevin Wolf               qstring_from_str(read_only ? "on" : "off"));
9040ebd24e0SKevin Wolf     qdict_put(bs_opts, "copy-on-read",
9050ebd24e0SKevin Wolf               qstring_from_str(copy_on_read ? "on" :"off"));
9060ebd24e0SKevin Wolf 
907593d464bSKevin Wolf     /* Controller type */
908593d464bSKevin Wolf     value = qemu_opt_get(legacy_opts, "if");
909593d464bSKevin Wolf     if (value) {
910593d464bSKevin Wolf         for (type = 0;
911593d464bSKevin Wolf              type < IF_COUNT && strcmp(value, if_name[type]);
912593d464bSKevin Wolf              type++) {
913593d464bSKevin Wolf         }
914593d464bSKevin Wolf         if (type == IF_COUNT) {
915593d464bSKevin Wolf             error_report("unsupported bus type '%s'", value);
916593d464bSKevin Wolf             goto fail;
917593d464bSKevin Wolf         }
918593d464bSKevin Wolf     } else {
919593d464bSKevin Wolf         type = block_default_type;
920593d464bSKevin Wolf     }
921593d464bSKevin Wolf 
922b41a7338SKevin Wolf     /* Geometry */
923b41a7338SKevin Wolf     cyls  = qemu_opt_get_number(legacy_opts, "cyls", 0);
924b41a7338SKevin Wolf     heads = qemu_opt_get_number(legacy_opts, "heads", 0);
925b41a7338SKevin Wolf     secs  = qemu_opt_get_number(legacy_opts, "secs", 0);
926b41a7338SKevin Wolf 
927b41a7338SKevin Wolf     if (cyls || heads || secs) {
928b41a7338SKevin Wolf         if (cyls < 1) {
929b41a7338SKevin Wolf             error_report("invalid physical cyls number");
930b41a7338SKevin Wolf             goto fail;
931b41a7338SKevin Wolf         }
932b41a7338SKevin Wolf         if (heads < 1) {
933b41a7338SKevin Wolf             error_report("invalid physical heads number");
934b41a7338SKevin Wolf             goto fail;
935b41a7338SKevin Wolf         }
936b41a7338SKevin Wolf         if (secs < 1) {
937b41a7338SKevin Wolf             error_report("invalid physical secs number");
938b41a7338SKevin Wolf             goto fail;
939b41a7338SKevin Wolf         }
940b41a7338SKevin Wolf     }
941b41a7338SKevin Wolf 
942b41a7338SKevin Wolf     translation = BIOS_ATA_TRANSLATION_AUTO;
943b41a7338SKevin Wolf     value = qemu_opt_get(legacy_opts, "trans");
944b41a7338SKevin Wolf     if (value != NULL) {
945b41a7338SKevin Wolf         if (!cyls) {
946b41a7338SKevin Wolf             error_report("'%s' trans must be used with cyls, heads and secs",
947b41a7338SKevin Wolf                          value);
948b41a7338SKevin Wolf             goto fail;
949b41a7338SKevin Wolf         }
950b41a7338SKevin Wolf         if (!strcmp(value, "none")) {
951b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_NONE;
952b41a7338SKevin Wolf         } else if (!strcmp(value, "lba")) {
953b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_LBA;
954f31c41ffSPaolo Bonzini         } else if (!strcmp(value, "large")) {
955f31c41ffSPaolo Bonzini             translation = BIOS_ATA_TRANSLATION_LARGE;
956f31c41ffSPaolo Bonzini         } else if (!strcmp(value, "rechs")) {
957f31c41ffSPaolo Bonzini             translation = BIOS_ATA_TRANSLATION_RECHS;
958b41a7338SKevin Wolf         } else if (!strcmp(value, "auto")) {
959b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_AUTO;
960b41a7338SKevin Wolf         } else {
961b41a7338SKevin Wolf             error_report("'%s' invalid translation type", value);
962b41a7338SKevin Wolf             goto fail;
963b41a7338SKevin Wolf         }
964b41a7338SKevin Wolf     }
965b41a7338SKevin Wolf 
966b41a7338SKevin Wolf     if (media == MEDIA_CDROM) {
967b41a7338SKevin Wolf         if (cyls || secs || heads) {
968b41a7338SKevin Wolf             error_report("CHS can't be set with media=cdrom");
969b41a7338SKevin Wolf             goto fail;
970b41a7338SKevin Wolf         }
971b41a7338SKevin Wolf     }
972b41a7338SKevin Wolf 
97387a899c5SKevin Wolf     /* Device address specified by bus/unit or index.
97487a899c5SKevin Wolf      * If none was specified, try to find the first free one. */
97587a899c5SKevin Wolf     bus_id  = qemu_opt_get_number(legacy_opts, "bus", 0);
97687a899c5SKevin Wolf     unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
97787a899c5SKevin Wolf     index   = qemu_opt_get_number(legacy_opts, "index", -1);
97887a899c5SKevin Wolf 
97987a899c5SKevin Wolf     max_devs = if_max_devs[type];
98087a899c5SKevin Wolf 
98187a899c5SKevin Wolf     if (index != -1) {
98287a899c5SKevin Wolf         if (bus_id != 0 || unit_id != -1) {
98387a899c5SKevin Wolf             error_report("index cannot be used with bus and unit");
98487a899c5SKevin Wolf             goto fail;
98587a899c5SKevin Wolf         }
98687a899c5SKevin Wolf         bus_id = drive_index_to_bus_id(type, index);
98787a899c5SKevin Wolf         unit_id = drive_index_to_unit_id(type, index);
98887a899c5SKevin Wolf     }
98987a899c5SKevin Wolf 
99087a899c5SKevin Wolf     if (unit_id == -1) {
99187a899c5SKevin Wolf        unit_id = 0;
99287a899c5SKevin Wolf        while (drive_get(type, bus_id, unit_id) != NULL) {
99387a899c5SKevin Wolf            unit_id++;
99487a899c5SKevin Wolf            if (max_devs && unit_id >= max_devs) {
99587a899c5SKevin Wolf                unit_id -= max_devs;
99687a899c5SKevin Wolf                bus_id++;
99787a899c5SKevin Wolf            }
99887a899c5SKevin Wolf        }
99987a899c5SKevin Wolf     }
100087a899c5SKevin Wolf 
100187a899c5SKevin Wolf     if (max_devs && unit_id >= max_devs) {
100287a899c5SKevin Wolf         error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
100387a899c5SKevin Wolf         goto fail;
100487a899c5SKevin Wolf     }
100587a899c5SKevin Wolf 
100687a899c5SKevin Wolf     if (drive_get(type, bus_id, unit_id) != NULL) {
100787a899c5SKevin Wolf         error_report("drive with bus=%d, unit=%d (index=%d) exists",
100887a899c5SKevin Wolf                      bus_id, unit_id, index);
100987a899c5SKevin Wolf         goto fail;
101087a899c5SKevin Wolf     }
101187a899c5SKevin Wolf 
1012bcf83158SKevin Wolf     /* Serial number */
1013bcf83158SKevin Wolf     serial = qemu_opt_get(legacy_opts, "serial");
1014bcf83158SKevin Wolf 
101587a899c5SKevin Wolf     /* no id supplied -> create one */
101687a899c5SKevin Wolf     if (qemu_opts_id(all_opts) == NULL) {
101787a899c5SKevin Wolf         char *new_id;
101887a899c5SKevin Wolf         const char *mediastr = "";
101987a899c5SKevin Wolf         if (type == IF_IDE || type == IF_SCSI) {
102087a899c5SKevin Wolf             mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
102187a899c5SKevin Wolf         }
102287a899c5SKevin Wolf         if (max_devs) {
102387a899c5SKevin Wolf             new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
102487a899c5SKevin Wolf                                      mediastr, unit_id);
102587a899c5SKevin Wolf         } else {
102687a899c5SKevin Wolf             new_id = g_strdup_printf("%s%s%i", if_name[type],
102787a899c5SKevin Wolf                                      mediastr, unit_id);
102887a899c5SKevin Wolf         }
102987a899c5SKevin Wolf         qdict_put(bs_opts, "id", qstring_from_str(new_id));
103087a899c5SKevin Wolf         g_free(new_id);
103187a899c5SKevin Wolf     }
103287a899c5SKevin Wolf 
1033394c7d4dSKevin Wolf     /* Add virtio block device */
1034394c7d4dSKevin Wolf     devaddr = qemu_opt_get(legacy_opts, "addr");
1035394c7d4dSKevin Wolf     if (devaddr && type != IF_VIRTIO) {
1036394c7d4dSKevin Wolf         error_report("addr is not supported by this bus type");
1037394c7d4dSKevin Wolf         goto fail;
1038394c7d4dSKevin Wolf     }
1039394c7d4dSKevin Wolf 
1040394c7d4dSKevin Wolf     if (type == IF_VIRTIO) {
1041394c7d4dSKevin Wolf         QemuOpts *devopts;
104287ea75d5SPeter Crosthwaite         devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
104387ea75d5SPeter Crosthwaite                                    &error_abort);
1044394c7d4dSKevin Wolf         if (arch_type == QEMU_ARCH_S390X) {
10451f68f1d3SAlexander Graf             qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
1046394c7d4dSKevin Wolf         } else {
1047f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
1048394c7d4dSKevin Wolf         }
1049f43e47dbSMarkus Armbruster         qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1050f43e47dbSMarkus Armbruster                      &error_abort);
1051394c7d4dSKevin Wolf         if (devaddr) {
1052f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "addr", devaddr, &error_abort);
1053394c7d4dSKevin Wolf         }
1054394c7d4dSKevin Wolf     }
1055394c7d4dSKevin Wolf 
1056d095b465SMax Reitz     filename = qemu_opt_get(legacy_opts, "file");
1057d095b465SMax Reitz 
1058ee13ed1cSKevin Wolf     /* Check werror/rerror compatibility with if=... */
1059ee13ed1cSKevin Wolf     werror = qemu_opt_get(legacy_opts, "werror");
1060ee13ed1cSKevin Wolf     if (werror != NULL) {
1061ee13ed1cSKevin Wolf         if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1062ee13ed1cSKevin Wolf             type != IF_NONE) {
1063ee13ed1cSKevin Wolf             error_report("werror is not supported by this bus type");
1064ee13ed1cSKevin Wolf             goto fail;
1065ee13ed1cSKevin Wolf         }
1066ee13ed1cSKevin Wolf         qdict_put(bs_opts, "werror", qstring_from_str(werror));
1067ee13ed1cSKevin Wolf     }
1068ee13ed1cSKevin Wolf 
1069ee13ed1cSKevin Wolf     rerror = qemu_opt_get(legacy_opts, "rerror");
1070ee13ed1cSKevin Wolf     if (rerror != NULL) {
1071ee13ed1cSKevin Wolf         if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1072ee13ed1cSKevin Wolf             type != IF_NONE) {
1073ee13ed1cSKevin Wolf             error_report("rerror is not supported by this bus type");
1074ee13ed1cSKevin Wolf             goto fail;
1075ee13ed1cSKevin Wolf         }
1076ee13ed1cSKevin Wolf         qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1077ee13ed1cSKevin Wolf     }
1078ee13ed1cSKevin Wolf 
10792d246f01SKevin Wolf     /* Actual block device init: Functionality shared with blockdev-add */
108018e46a03SMarkus Armbruster     blk = blockdev_init(filename, bs_opts, &local_err);
10813cb0e25cSMarkus Armbruster     bs_opts = NULL;
108218e46a03SMarkus Armbruster     if (!blk) {
108384d18f06SMarkus Armbruster         if (local_err) {
1084565f65d2SMarkus Armbruster             error_report_err(local_err);
1085b681072dSKevin Wolf         }
10862d246f01SKevin Wolf         goto fail;
1087b681072dSKevin Wolf     } else {
108884d18f06SMarkus Armbruster         assert(!local_err);
10892d246f01SKevin Wolf     }
10902d246f01SKevin Wolf 
109126f8b3a8SMarkus Armbruster     /* Create legacy DriveInfo */
109226f8b3a8SMarkus Armbruster     dinfo = g_malloc0(sizeof(*dinfo));
1093f298d071SKevin Wolf     dinfo->opts = all_opts;
10942d246f01SKevin Wolf 
1095b41a7338SKevin Wolf     dinfo->cyls = cyls;
1096b41a7338SKevin Wolf     dinfo->heads = heads;
1097b41a7338SKevin Wolf     dinfo->secs = secs;
1098b41a7338SKevin Wolf     dinfo->trans = translation;
1099b41a7338SKevin Wolf 
1100ee13ed1cSKevin Wolf     dinfo->type = type;
110187a899c5SKevin Wolf     dinfo->bus = bus_id;
110287a899c5SKevin Wolf     dinfo->unit = unit_id;
1103394c7d4dSKevin Wolf     dinfo->devaddr = devaddr;
1104bcf83158SKevin Wolf     dinfo->serial = g_strdup(serial);
1105bcf83158SKevin Wolf 
110626f8b3a8SMarkus Armbruster     blk_set_legacy_dinfo(blk, dinfo);
110726f8b3a8SMarkus Armbruster 
1108e34ef046SKevin Wolf     switch(type) {
1109e34ef046SKevin Wolf     case IF_IDE:
1110e34ef046SKevin Wolf     case IF_SCSI:
1111e34ef046SKevin Wolf     case IF_XEN:
1112e34ef046SKevin Wolf     case IF_NONE:
1113e34ef046SKevin Wolf         dinfo->media_cd = media == MEDIA_CDROM;
1114e34ef046SKevin Wolf         break;
1115e34ef046SKevin Wolf     default:
1116e34ef046SKevin Wolf         break;
1117e34ef046SKevin Wolf     }
1118e34ef046SKevin Wolf 
11192d246f01SKevin Wolf fail:
112033cb7dc8SKevin Wolf     qemu_opts_del(legacy_opts);
11213cb0e25cSMarkus Armbruster     QDECREF(bs_opts);
11222d246f01SKevin Wolf     return dinfo;
112357975222SKevin Wolf }
112457975222SKevin Wolf 
1125b6c1bae5SKevin Wolf static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1126b6c1bae5SKevin Wolf {
1127b6c1bae5SKevin Wolf     BlockDriverState *bs;
1128b6c1bae5SKevin Wolf 
1129b6c1bae5SKevin Wolf     bs = bdrv_lookup_bs(name, name, errp);
1130b6c1bae5SKevin Wolf     if (bs == NULL) {
1131b6c1bae5SKevin Wolf         return NULL;
1132b6c1bae5SKevin Wolf     }
1133b6c1bae5SKevin Wolf 
1134b6c1bae5SKevin Wolf     if (!bdrv_is_root_node(bs)) {
1135b6c1bae5SKevin Wolf         error_setg(errp, "Need a root block node");
1136b6c1bae5SKevin Wolf         return NULL;
1137b6c1bae5SKevin Wolf     }
1138b6c1bae5SKevin Wolf 
1139b6c1bae5SKevin Wolf     if (!bdrv_is_inserted(bs)) {
1140b6c1bae5SKevin Wolf         error_setg(errp, "Device has no medium");
1141b6c1bae5SKevin Wolf         return NULL;
1142b6c1bae5SKevin Wolf     }
1143b6c1bae5SKevin Wolf 
1144b6c1bae5SKevin Wolf     return bs;
1145b6c1bae5SKevin Wolf }
1146b6c1bae5SKevin Wolf 
1147b33945cfSKevin Wolf static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id,
1148b33945cfSKevin Wolf                                  Error **errp)
1149b33945cfSKevin Wolf {
1150b33945cfSKevin Wolf     BlockBackend *blk;
1151b33945cfSKevin Wolf 
1152b33945cfSKevin Wolf     if (!blk_name == !qdev_id) {
1153b33945cfSKevin Wolf         error_setg(errp, "Need exactly one of 'device' and 'id'");
1154b33945cfSKevin Wolf         return NULL;
1155b33945cfSKevin Wolf     }
1156b33945cfSKevin Wolf 
1157b33945cfSKevin Wolf     if (qdev_id) {
1158b33945cfSKevin Wolf         blk = blk_by_qdev_id(qdev_id, errp);
1159b33945cfSKevin Wolf     } else {
1160b33945cfSKevin Wolf         blk = blk_by_name(blk_name);
1161b33945cfSKevin Wolf         if (blk == NULL) {
1162b33945cfSKevin Wolf             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1163b33945cfSKevin Wolf                       "Device '%s' not found", blk_name);
1164b33945cfSKevin Wolf         }
1165b33945cfSKevin Wolf     }
1166b33945cfSKevin Wolf 
1167b33945cfSKevin Wolf     return blk;
1168b33945cfSKevin Wolf }
1169b33945cfSKevin Wolf 
11703e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict)
1171666daa68SMarkus Armbruster {
1172666daa68SMarkus Armbruster     const char *device = qdict_get_str(qdict, "device");
1173a0e8544cSFam Zheng     BlockBackend *blk;
11742d3735d3SStefan Hajnoczi     int ret;
11752d3735d3SStefan Hajnoczi 
1176e8877497SStefan Hajnoczi     if (!strcmp(device, "all")) {
1177da31d594SMax Reitz         ret = blk_commit_all();
1178e8877497SStefan Hajnoczi     } else {
117984aa0140SStefan Hajnoczi         BlockDriverState *bs;
118084aa0140SStefan Hajnoczi         AioContext *aio_context;
118184aa0140SStefan Hajnoczi 
1182a0e8544cSFam Zheng         blk = blk_by_name(device);
1183a0e8544cSFam Zheng         if (!blk) {
118458513bdeSJeff Cody             monitor_printf(mon, "Device '%s' not found\n", device);
1185ac59eb95SMarkus Armbruster             return;
11866ab4b5abSMarkus Armbruster         }
11875433c24fSMax Reitz         if (!blk_is_available(blk)) {
11885433c24fSMax Reitz             monitor_printf(mon, "Device '%s' has no medium\n", device);
11895433c24fSMax Reitz             return;
11905433c24fSMax Reitz         }
119184aa0140SStefan Hajnoczi 
119284aa0140SStefan Hajnoczi         bs = blk_bs(blk);
119384aa0140SStefan Hajnoczi         aio_context = bdrv_get_aio_context(bs);
119484aa0140SStefan Hajnoczi         aio_context_acquire(aio_context);
119584aa0140SStefan Hajnoczi 
119684aa0140SStefan Hajnoczi         ret = bdrv_commit(bs);
119784aa0140SStefan Hajnoczi 
119884aa0140SStefan Hajnoczi         aio_context_release(aio_context);
11992d3735d3SStefan Hajnoczi     }
120058513bdeSJeff Cody     if (ret < 0) {
120158513bdeSJeff Cody         monitor_printf(mon, "'commit' error for '%s': %s\n", device,
120258513bdeSJeff Cody                        strerror(-ret));
1203666daa68SMarkus Armbruster     }
1204666daa68SMarkus Armbruster }
1205666daa68SMarkus Armbruster 
120610f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp)
12076cc2a415SPaolo Bonzini {
1208c8a83e85SKevin Wolf     TransactionActionList list;
12096cc2a415SPaolo Bonzini 
121010f75907SEric Blake     list.value = action;
12116cc2a415SPaolo Bonzini     list.next = NULL;
121294d16a64SJohn Snow     qmp_transaction(&list, false, NULL, errp);
12136cc2a415SPaolo Bonzini }
12146cc2a415SPaolo Bonzini 
12150901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
12160901f67eSBenoît Canet                                 bool has_node_name, const char *node_name,
12170901f67eSBenoît Canet                                 const char *snapshot_file,
12180901f67eSBenoît Canet                                 bool has_snapshot_node_name,
12190901f67eSBenoît Canet                                 const char *snapshot_node_name,
12206106e249SLuiz Capitulino                                 bool has_format, const char *format,
12210901f67eSBenoît Canet                                 bool has_mode, NewImageMode mode, Error **errp)
1222f8882568SJes Sorensen {
1223a911e6aeSAlberto Garcia     BlockdevSnapshotSync snapshot = {
12240901f67eSBenoît Canet         .has_device = has_device,
12256cc2a415SPaolo Bonzini         .device = (char *) device,
12260901f67eSBenoît Canet         .has_node_name = has_node_name,
12270901f67eSBenoît Canet         .node_name = (char *) node_name,
12286cc2a415SPaolo Bonzini         .snapshot_file = (char *) snapshot_file,
12290901f67eSBenoît Canet         .has_snapshot_node_name = has_snapshot_node_name,
12300901f67eSBenoît Canet         .snapshot_node_name = (char *) snapshot_node_name,
12316cc2a415SPaolo Bonzini         .has_format = has_format,
12326cc2a415SPaolo Bonzini         .format = (char *) format,
12336cc2a415SPaolo Bonzini         .has_mode = has_mode,
12346cc2a415SPaolo Bonzini         .mode = mode,
12356cc2a415SPaolo Bonzini     };
123610f75907SEric Blake     TransactionAction action = {
123710f75907SEric Blake         .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
123832bafa8fSEric Blake         .u.blockdev_snapshot_sync.data = &snapshot,
123910f75907SEric Blake     };
124010f75907SEric Blake     blockdev_do_action(&action, errp);
1241f8882568SJes Sorensen }
1242f8882568SJes Sorensen 
124343de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay,
124443de7e2dSAlberto Garcia                            Error **errp)
124543de7e2dSAlberto Garcia {
124643de7e2dSAlberto Garcia     BlockdevSnapshot snapshot_data = {
124743de7e2dSAlberto Garcia         .node = (char *) node,
124843de7e2dSAlberto Garcia         .overlay = (char *) overlay
124943de7e2dSAlberto Garcia     };
125010f75907SEric Blake     TransactionAction action = {
125110f75907SEric Blake         .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
125232bafa8fSEric Blake         .u.blockdev_snapshot.data = &snapshot_data,
125310f75907SEric Blake     };
125410f75907SEric Blake     blockdev_do_action(&action, 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     };
126510f75907SEric Blake     TransactionAction action = {
126610f75907SEric Blake         .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
126732bafa8fSEric Blake         .u.blockdev_snapshot_internal_sync.data = &snapshot,
126810f75907SEric Blake     };
126910f75907SEric Blake     blockdev_do_action(&action, errp);
1270f323bc9eSWenchao Xia }
1271f323bc9eSWenchao Xia 
127244e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
127344e3e053SWenchao Xia                                                          bool has_id,
127444e3e053SWenchao Xia                                                          const char *id,
127544e3e053SWenchao Xia                                                          bool has_name,
127644e3e053SWenchao Xia                                                          const char *name,
127744e3e053SWenchao Xia                                                          Error **errp)
127844e3e053SWenchao Xia {
1279a0e8544cSFam Zheng     BlockDriverState *bs;
12804ef3982aSStefan Hajnoczi     AioContext *aio_context;
128144e3e053SWenchao Xia     QEMUSnapshotInfo sn;
128244e3e053SWenchao Xia     Error *local_err = NULL;
128344e3e053SWenchao Xia     SnapshotInfo *info = NULL;
128444e3e053SWenchao Xia     int ret;
128544e3e053SWenchao Xia 
12862dfb4c03SKevin Wolf     bs = qmp_get_root_bs(device, errp);
12872dfb4c03SKevin Wolf     if (!bs) {
128844e3e053SWenchao Xia         return NULL;
128944e3e053SWenchao Xia     }
12902dfb4c03SKevin Wolf     aio_context = bdrv_get_aio_context(bs);
12915433c24fSMax Reitz     aio_context_acquire(aio_context);
129244e3e053SWenchao Xia 
129344e3e053SWenchao Xia     if (!has_id) {
129444e3e053SWenchao Xia         id = NULL;
129544e3e053SWenchao Xia     }
129644e3e053SWenchao Xia 
129744e3e053SWenchao Xia     if (!has_name) {
129844e3e053SWenchao Xia         name = NULL;
129944e3e053SWenchao Xia     }
130044e3e053SWenchao Xia 
130144e3e053SWenchao Xia     if (!id && !name) {
130244e3e053SWenchao Xia         error_setg(errp, "Name or id must be provided");
13035433c24fSMax Reitz         goto out_aio_context;
130444e3e053SWenchao Xia     }
130544e3e053SWenchao Xia 
13060b928854SStefan Hajnoczi     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
13070b928854SStefan Hajnoczi         goto out_aio_context;
13080b928854SStefan Hajnoczi     }
13090b928854SStefan Hajnoczi 
131044e3e053SWenchao Xia     ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
131184d18f06SMarkus Armbruster     if (local_err) {
131244e3e053SWenchao Xia         error_propagate(errp, local_err);
13134ef3982aSStefan Hajnoczi         goto out_aio_context;
131444e3e053SWenchao Xia     }
131544e3e053SWenchao Xia     if (!ret) {
131644e3e053SWenchao Xia         error_setg(errp,
131744e3e053SWenchao Xia                    "Snapshot with id '%s' and name '%s' does not exist on "
131844e3e053SWenchao Xia                    "device '%s'",
131944e3e053SWenchao Xia                    STR_OR_NULL(id), STR_OR_NULL(name), device);
13204ef3982aSStefan Hajnoczi         goto out_aio_context;
132144e3e053SWenchao Xia     }
132244e3e053SWenchao Xia 
132344e3e053SWenchao Xia     bdrv_snapshot_delete(bs, id, name, &local_err);
132484d18f06SMarkus Armbruster     if (local_err) {
132544e3e053SWenchao Xia         error_propagate(errp, local_err);
13264ef3982aSStefan Hajnoczi         goto out_aio_context;
132744e3e053SWenchao Xia     }
132844e3e053SWenchao Xia 
13294ef3982aSStefan Hajnoczi     aio_context_release(aio_context);
13304ef3982aSStefan Hajnoczi 
13315839e53bSMarkus Armbruster     info = g_new0(SnapshotInfo, 1);
133244e3e053SWenchao Xia     info->id = g_strdup(sn.id_str);
133344e3e053SWenchao Xia     info->name = g_strdup(sn.name);
133444e3e053SWenchao Xia     info->date_nsec = sn.date_nsec;
133544e3e053SWenchao Xia     info->date_sec = sn.date_sec;
133644e3e053SWenchao Xia     info->vm_state_size = sn.vm_state_size;
133744e3e053SWenchao Xia     info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
133844e3e053SWenchao Xia     info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
133944e3e053SWenchao Xia 
134044e3e053SWenchao Xia     return info;
13414ef3982aSStefan Hajnoczi 
13424ef3982aSStefan Hajnoczi out_aio_context:
13434ef3982aSStefan Hajnoczi     aio_context_release(aio_context);
13444ef3982aSStefan Hajnoczi     return NULL;
134544e3e053SWenchao Xia }
13468802d1fdSJeff Cody 
1347341ebc2fSJohn Snow /**
1348341ebc2fSJohn Snow  * block_dirty_bitmap_lookup:
1349341ebc2fSJohn Snow  * Return a dirty bitmap (if present), after validating
1350341ebc2fSJohn Snow  * the node reference and bitmap names.
1351341ebc2fSJohn Snow  *
1352341ebc2fSJohn Snow  * @node: The name of the BDS node to search for bitmaps
1353341ebc2fSJohn Snow  * @name: The name of the bitmap to search for
1354341ebc2fSJohn Snow  * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1355341ebc2fSJohn Snow  * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1356341ebc2fSJohn Snow  * @errp: Output pointer for error information. Can be NULL.
1357341ebc2fSJohn Snow  *
1358341ebc2fSJohn Snow  * @return: A bitmap object on success, or NULL on failure.
1359341ebc2fSJohn Snow  */
1360341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1361341ebc2fSJohn Snow                                                   const char *name,
1362341ebc2fSJohn Snow                                                   BlockDriverState **pbs,
1363341ebc2fSJohn Snow                                                   AioContext **paio,
1364341ebc2fSJohn Snow                                                   Error **errp)
1365341ebc2fSJohn Snow {
1366341ebc2fSJohn Snow     BlockDriverState *bs;
1367341ebc2fSJohn Snow     BdrvDirtyBitmap *bitmap;
1368341ebc2fSJohn Snow     AioContext *aio_context;
1369341ebc2fSJohn Snow 
1370341ebc2fSJohn Snow     if (!node) {
1371341ebc2fSJohn Snow         error_setg(errp, "Node cannot be NULL");
1372341ebc2fSJohn Snow         return NULL;
1373341ebc2fSJohn Snow     }
1374341ebc2fSJohn Snow     if (!name) {
1375341ebc2fSJohn Snow         error_setg(errp, "Bitmap name cannot be NULL");
1376341ebc2fSJohn Snow         return NULL;
1377341ebc2fSJohn Snow     }
1378341ebc2fSJohn Snow     bs = bdrv_lookup_bs(node, node, NULL);
1379341ebc2fSJohn Snow     if (!bs) {
1380341ebc2fSJohn Snow         error_setg(errp, "Node '%s' not found", node);
1381341ebc2fSJohn Snow         return NULL;
1382341ebc2fSJohn Snow     }
1383341ebc2fSJohn Snow 
1384341ebc2fSJohn Snow     aio_context = bdrv_get_aio_context(bs);
1385341ebc2fSJohn Snow     aio_context_acquire(aio_context);
1386341ebc2fSJohn Snow 
1387341ebc2fSJohn Snow     bitmap = bdrv_find_dirty_bitmap(bs, name);
1388341ebc2fSJohn Snow     if (!bitmap) {
1389341ebc2fSJohn Snow         error_setg(errp, "Dirty bitmap '%s' not found", name);
1390341ebc2fSJohn Snow         goto fail;
1391341ebc2fSJohn Snow     }
1392341ebc2fSJohn Snow 
1393341ebc2fSJohn Snow     if (pbs) {
1394341ebc2fSJohn Snow         *pbs = bs;
1395341ebc2fSJohn Snow     }
1396341ebc2fSJohn Snow     if (paio) {
1397341ebc2fSJohn Snow         *paio = aio_context;
1398341ebc2fSJohn Snow     } else {
1399341ebc2fSJohn Snow         aio_context_release(aio_context);
1400341ebc2fSJohn Snow     }
1401341ebc2fSJohn Snow 
1402341ebc2fSJohn Snow     return bitmap;
1403341ebc2fSJohn Snow 
1404341ebc2fSJohn Snow  fail:
1405341ebc2fSJohn Snow     aio_context_release(aio_context);
1406341ebc2fSJohn Snow     return NULL;
1407341ebc2fSJohn Snow }
1408341ebc2fSJohn Snow 
1409b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */
1410ba0c86a3SWenchao Xia 
141150f43f0fSJohn Snow typedef struct BlkActionState BlkActionState;
1412ba0c86a3SWenchao Xia 
141350f43f0fSJohn Snow /**
141450f43f0fSJohn Snow  * BlkActionOps:
141550f43f0fSJohn Snow  * Table of operations that define an Action.
141650f43f0fSJohn Snow  *
141750f43f0fSJohn Snow  * @instance_size: Size of state struct, in bytes.
141850f43f0fSJohn Snow  * @prepare: Prepare the work, must NOT be NULL.
141950f43f0fSJohn Snow  * @commit: Commit the changes, can be NULL.
142050f43f0fSJohn Snow  * @abort: Abort the changes on fail, can be NULL.
142150f43f0fSJohn Snow  * @clean: Clean up resources after all transaction actions have called
142250f43f0fSJohn Snow  *         commit() or abort(). Can be NULL.
142350f43f0fSJohn Snow  *
142450f43f0fSJohn Snow  * Only prepare() may fail. In a single transaction, only one of commit() or
142550f43f0fSJohn Snow  * abort() will be called. clean() will always be called if it is present.
1426ba0c86a3SWenchao Xia  */
142750f43f0fSJohn Snow typedef struct BlkActionOps {
142850f43f0fSJohn Snow     size_t instance_size;
142950f43f0fSJohn Snow     void (*prepare)(BlkActionState *common, Error **errp);
143050f43f0fSJohn Snow     void (*commit)(BlkActionState *common);
143150f43f0fSJohn Snow     void (*abort)(BlkActionState *common);
143250f43f0fSJohn Snow     void (*clean)(BlkActionState *common);
143350f43f0fSJohn Snow } BlkActionOps;
143450f43f0fSJohn Snow 
143550f43f0fSJohn Snow /**
143650f43f0fSJohn Snow  * BlkActionState:
143750f43f0fSJohn Snow  * Describes one Action's state within a Transaction.
143850f43f0fSJohn Snow  *
143950f43f0fSJohn Snow  * @action: QAPI-defined enum identifying which Action to perform.
144050f43f0fSJohn Snow  * @ops: Table of ActionOps this Action can perform.
144194d16a64SJohn Snow  * @block_job_txn: Transaction which this action belongs to.
144250f43f0fSJohn Snow  * @entry: List membership for all Actions in this Transaction.
144350f43f0fSJohn Snow  *
144450f43f0fSJohn Snow  * This structure must be arranged as first member in a subclassed type,
144550f43f0fSJohn Snow  * assuming that the compiler will also arrange it to the same offsets as the
144650f43f0fSJohn Snow  * base class.
144750f43f0fSJohn Snow  */
144850f43f0fSJohn Snow struct BlkActionState {
1449c8a83e85SKevin Wolf     TransactionAction *action;
145050f43f0fSJohn Snow     const BlkActionOps *ops;
145194d16a64SJohn Snow     BlockJobTxn *block_job_txn;
145294d16a64SJohn Snow     TransactionProperties *txn_props;
145350f43f0fSJohn Snow     QSIMPLEQ_ENTRY(BlkActionState) entry;
1454ba0c86a3SWenchao Xia };
1455ba0c86a3SWenchao Xia 
1456bbe86010SWenchao Xia /* internal snapshot private data */
1457bbe86010SWenchao Xia typedef struct InternalSnapshotState {
145850f43f0fSJohn Snow     BlkActionState common;
1459bbe86010SWenchao Xia     BlockDriverState *bs;
14605d6e96efSStefan Hajnoczi     AioContext *aio_context;
1461bbe86010SWenchao Xia     QEMUSnapshotInfo sn;
1462507306ccSFam Zheng     bool created;
1463bbe86010SWenchao Xia } InternalSnapshotState;
1464bbe86010SWenchao Xia 
146594d16a64SJohn Snow 
146694d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp)
146794d16a64SJohn Snow {
146894d16a64SJohn Snow     if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
146994d16a64SJohn Snow         error_setg(errp,
147094d16a64SJohn Snow                    "Action '%s' does not support Transaction property "
147194d16a64SJohn Snow                    "completion-mode = %s",
147294d16a64SJohn Snow                    TransactionActionKind_lookup[s->action->type],
147394d16a64SJohn Snow                    ActionCompletionMode_lookup[s->txn_props->completion_mode]);
147494d16a64SJohn Snow         return -1;
147594d16a64SJohn Snow     }
147694d16a64SJohn Snow     return 0;
147794d16a64SJohn Snow }
147894d16a64SJohn Snow 
147950f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common,
1480bbe86010SWenchao Xia                                       Error **errp)
1481bbe86010SWenchao Xia {
1482f70edf99SMarkus Armbruster     Error *local_err = NULL;
1483bbe86010SWenchao Xia     const char *device;
1484bbe86010SWenchao Xia     const char *name;
1485bbe86010SWenchao Xia     BlockDriverState *bs;
1486bbe86010SWenchao Xia     QEMUSnapshotInfo old_sn, *sn;
1487bbe86010SWenchao Xia     bool ret;
1488bbe86010SWenchao Xia     qemu_timeval tv;
1489bbe86010SWenchao Xia     BlockdevSnapshotInternal *internal;
1490bbe86010SWenchao Xia     InternalSnapshotState *state;
1491bbe86010SWenchao Xia     int ret1;
1492bbe86010SWenchao Xia 
14936a8f9661SEric Blake     g_assert(common->action->type ==
1494bbe86010SWenchao Xia              TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
149532bafa8fSEric Blake     internal = common->action->u.blockdev_snapshot_internal_sync.data;
1496bbe86010SWenchao Xia     state = DO_UPCAST(InternalSnapshotState, common, common);
1497bbe86010SWenchao Xia 
1498bbe86010SWenchao Xia     /* 1. parse input */
1499bbe86010SWenchao Xia     device = internal->device;
1500bbe86010SWenchao Xia     name = internal->name;
1501bbe86010SWenchao Xia 
1502bbe86010SWenchao Xia     /* 2. check for validation */
150394d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
150494d16a64SJohn Snow         return;
150594d16a64SJohn Snow     }
150694d16a64SJohn Snow 
150775dfd402SKevin Wolf     bs = qmp_get_root_bs(device, errp);
150875dfd402SKevin Wolf     if (!bs) {
1509bbe86010SWenchao Xia         return;
1510bbe86010SWenchao Xia     }
1511bbe86010SWenchao Xia 
15125d6e96efSStefan Hajnoczi     /* AioContext is released in .clean() */
151375dfd402SKevin Wolf     state->aio_context = bdrv_get_aio_context(bs);
15145d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
15155d6e96efSStefan Hajnoczi 
1516507306ccSFam Zheng     state->bs = bs;
1517507306ccSFam Zheng     bdrv_drained_begin(bs);
1518507306ccSFam Zheng 
15193dc7ca3cSStefan Hajnoczi     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
15203dc7ca3cSStefan Hajnoczi         return;
15213dc7ca3cSStefan Hajnoczi     }
15223dc7ca3cSStefan Hajnoczi 
1523bbe86010SWenchao Xia     if (bdrv_is_read_only(bs)) {
152481e5f78aSAlberto Garcia         error_setg(errp, "Device '%s' is read only", device);
1525bbe86010SWenchao Xia         return;
1526bbe86010SWenchao Xia     }
1527bbe86010SWenchao Xia 
1528bbe86010SWenchao Xia     if (!bdrv_can_snapshot(bs)) {
152981e5f78aSAlberto Garcia         error_setg(errp, "Block format '%s' used by device '%s' "
153081e5f78aSAlberto Garcia                    "does not support internal snapshots",
153181e5f78aSAlberto Garcia                    bs->drv->format_name, device);
1532bbe86010SWenchao Xia         return;
1533bbe86010SWenchao Xia     }
1534bbe86010SWenchao Xia 
1535bbe86010SWenchao Xia     if (!strlen(name)) {
1536bbe86010SWenchao Xia         error_setg(errp, "Name is empty");
1537bbe86010SWenchao Xia         return;
1538bbe86010SWenchao Xia     }
1539bbe86010SWenchao Xia 
1540bbe86010SWenchao Xia     /* check whether a snapshot with name exist */
1541f70edf99SMarkus Armbruster     ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1542f70edf99SMarkus Armbruster                                             &local_err);
1543f70edf99SMarkus Armbruster     if (local_err) {
1544f70edf99SMarkus Armbruster         error_propagate(errp, local_err);
1545bbe86010SWenchao Xia         return;
1546bbe86010SWenchao Xia     } else if (ret) {
1547bbe86010SWenchao Xia         error_setg(errp,
1548bbe86010SWenchao Xia                    "Snapshot with name '%s' already exists on device '%s'",
1549bbe86010SWenchao Xia                    name, device);
1550bbe86010SWenchao Xia         return;
1551bbe86010SWenchao Xia     }
1552bbe86010SWenchao Xia 
1553bbe86010SWenchao Xia     /* 3. take the snapshot */
1554bbe86010SWenchao Xia     sn = &state->sn;
1555bbe86010SWenchao Xia     pstrcpy(sn->name, sizeof(sn->name), name);
1556bbe86010SWenchao Xia     qemu_gettimeofday(&tv);
1557bbe86010SWenchao Xia     sn->date_sec = tv.tv_sec;
1558bbe86010SWenchao Xia     sn->date_nsec = tv.tv_usec * 1000;
1559bbe86010SWenchao Xia     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1560bbe86010SWenchao Xia 
1561bbe86010SWenchao Xia     ret1 = bdrv_snapshot_create(bs, sn);
1562bbe86010SWenchao Xia     if (ret1 < 0) {
1563bbe86010SWenchao Xia         error_setg_errno(errp, -ret1,
1564bbe86010SWenchao Xia                          "Failed to create snapshot '%s' on device '%s'",
1565bbe86010SWenchao Xia                          name, device);
1566bbe86010SWenchao Xia         return;
1567bbe86010SWenchao Xia     }
1568bbe86010SWenchao Xia 
1569bbe86010SWenchao Xia     /* 4. succeed, mark a snapshot is created */
1570507306ccSFam Zheng     state->created = true;
1571bbe86010SWenchao Xia }
1572bbe86010SWenchao Xia 
157350f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common)
1574bbe86010SWenchao Xia {
1575bbe86010SWenchao Xia     InternalSnapshotState *state =
1576bbe86010SWenchao Xia                              DO_UPCAST(InternalSnapshotState, common, common);
1577bbe86010SWenchao Xia     BlockDriverState *bs = state->bs;
1578bbe86010SWenchao Xia     QEMUSnapshotInfo *sn = &state->sn;
1579bbe86010SWenchao Xia     Error *local_error = NULL;
1580bbe86010SWenchao Xia 
1581507306ccSFam Zheng     if (!state->created) {
1582bbe86010SWenchao Xia         return;
1583bbe86010SWenchao Xia     }
1584bbe86010SWenchao Xia 
1585bbe86010SWenchao Xia     if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1586c29b77f9SMarkus Armbruster         error_reportf_err(local_error,
1587c29b77f9SMarkus Armbruster                           "Failed to delete snapshot with id '%s' and "
1588c29b77f9SMarkus Armbruster                           "name '%s' on device '%s' in abort: ",
1589c29b77f9SMarkus Armbruster                           sn->id_str, sn->name,
1590c29b77f9SMarkus Armbruster                           bdrv_get_device_name(bs));
1591bbe86010SWenchao Xia     }
1592bbe86010SWenchao Xia }
1593bbe86010SWenchao Xia 
159450f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common)
15955d6e96efSStefan Hajnoczi {
15965d6e96efSStefan Hajnoczi     InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
15975d6e96efSStefan Hajnoczi                                              common, common);
15985d6e96efSStefan Hajnoczi 
15995d6e96efSStefan Hajnoczi     if (state->aio_context) {
1600507306ccSFam Zheng         if (state->bs) {
1601507306ccSFam Zheng             bdrv_drained_end(state->bs);
1602507306ccSFam Zheng         }
16035d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
16045d6e96efSStefan Hajnoczi     }
16055d6e96efSStefan Hajnoczi }
16065d6e96efSStefan Hajnoczi 
1607ba0c86a3SWenchao Xia /* external snapshot private data */
1608ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState {
160950f43f0fSJohn Snow     BlkActionState common;
16108802d1fdSJeff Cody     BlockDriverState *old_bs;
16118802d1fdSJeff Cody     BlockDriverState *new_bs;
16125d6e96efSStefan Hajnoczi     AioContext *aio_context;
1613ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState;
16148802d1fdSJeff Cody 
161550f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common,
16169b9877eeSWenchao Xia                                       Error **errp)
16179b9877eeSWenchao Xia {
16185b363937SMax Reitz     int flags = 0;
161943de7e2dSAlberto Garcia     QDict *options = NULL;
16209b9877eeSWenchao Xia     Error *local_err = NULL;
162143de7e2dSAlberto Garcia     /* Device and node name of the image to generate the snapshot from */
1622e2a31e87SWenchao Xia     const char *device;
16230901f67eSBenoît Canet     const char *node_name;
162443de7e2dSAlberto Garcia     /* Reference to the new image (for 'blockdev-snapshot') */
162543de7e2dSAlberto Garcia     const char *snapshot_ref;
162643de7e2dSAlberto Garcia     /* File name of the new image (for 'blockdev-snapshot-sync') */
1627e2a31e87SWenchao Xia     const char *new_image_file;
1628ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1629ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1630c8a83e85SKevin Wolf     TransactionAction *action = common->action;
16319b9877eeSWenchao Xia 
163243de7e2dSAlberto Garcia     /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
163343de7e2dSAlberto Garcia      * purpose but a different set of parameters */
163443de7e2dSAlberto Garcia     switch (action->type) {
163543de7e2dSAlberto Garcia     case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
163643de7e2dSAlberto Garcia         {
163732bafa8fSEric Blake             BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
163843de7e2dSAlberto Garcia             device = s->node;
163943de7e2dSAlberto Garcia             node_name = s->node;
164043de7e2dSAlberto Garcia             new_image_file = NULL;
164143de7e2dSAlberto Garcia             snapshot_ref = s->overlay;
1642e2a31e87SWenchao Xia         }
164343de7e2dSAlberto Garcia         break;
164443de7e2dSAlberto Garcia     case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
164543de7e2dSAlberto Garcia         {
164632bafa8fSEric Blake             BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
164743de7e2dSAlberto Garcia             device = s->has_device ? s->device : NULL;
164843de7e2dSAlberto Garcia             node_name = s->has_node_name ? s->node_name : NULL;
164943de7e2dSAlberto Garcia             new_image_file = s->snapshot_file;
165043de7e2dSAlberto Garcia             snapshot_ref = NULL;
165143de7e2dSAlberto Garcia         }
165243de7e2dSAlberto Garcia         break;
165343de7e2dSAlberto Garcia     default:
165443de7e2dSAlberto Garcia         g_assert_not_reached();
1655e2a31e87SWenchao Xia     }
1656e2a31e87SWenchao Xia 
1657e2a31e87SWenchao Xia     /* start processing */
165894d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
165994d16a64SJohn Snow         return;
166094d16a64SJohn Snow     }
166194d16a64SJohn Snow 
166243de7e2dSAlberto Garcia     state->old_bs = bdrv_lookup_bs(device, node_name, errp);
166343de7e2dSAlberto Garcia     if (!state->old_bs) {
16649b9877eeSWenchao Xia         return;
16659b9877eeSWenchao Xia     }
16669b9877eeSWenchao Xia 
16675d6e96efSStefan Hajnoczi     /* Acquire AioContext now so any threads operating on old_bs stop */
16685d6e96efSStefan Hajnoczi     state->aio_context = bdrv_get_aio_context(state->old_bs);
16695d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
1670da763e83SFam Zheng     bdrv_drained_begin(state->old_bs);
16715d6e96efSStefan Hajnoczi 
1672ba5d6ab6SStefan Hajnoczi     if (!bdrv_is_inserted(state->old_bs)) {
1673c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
16749b9877eeSWenchao Xia         return;
16759b9877eeSWenchao Xia     }
16769b9877eeSWenchao Xia 
16773718d8abSFam Zheng     if (bdrv_op_is_blocked(state->old_bs,
16783718d8abSFam Zheng                            BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
16799b9877eeSWenchao Xia         return;
16809b9877eeSWenchao Xia     }
16819b9877eeSWenchao Xia 
1682ba5d6ab6SStefan Hajnoczi     if (!bdrv_is_read_only(state->old_bs)) {
1683ba5d6ab6SStefan Hajnoczi         if (bdrv_flush(state->old_bs)) {
1684c6bd8c70SMarkus Armbruster             error_setg(errp, QERR_IO_ERROR);
16859b9877eeSWenchao Xia             return;
16869b9877eeSWenchao Xia         }
16879b9877eeSWenchao Xia     }
16889b9877eeSWenchao Xia 
1689212a5a8fSBenoît Canet     if (!bdrv_is_first_non_filter(state->old_bs)) {
1690c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1691f6186f49SBenoît Canet         return;
1692f6186f49SBenoît Canet     }
1693f6186f49SBenoît Canet 
169443de7e2dSAlberto Garcia     if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
169532bafa8fSEric Blake         BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
169643de7e2dSAlberto Garcia         const char *format = s->has_format ? s->format : "qcow2";
169743de7e2dSAlberto Garcia         enum NewImageMode mode;
169843de7e2dSAlberto Garcia         const char *snapshot_node_name =
169943de7e2dSAlberto Garcia             s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
170043de7e2dSAlberto Garcia 
170143de7e2dSAlberto Garcia         if (node_name && !snapshot_node_name) {
170243de7e2dSAlberto Garcia             error_setg(errp, "New snapshot node name missing");
170343de7e2dSAlberto Garcia             return;
170443de7e2dSAlberto Garcia         }
170543de7e2dSAlberto Garcia 
170643de7e2dSAlberto Garcia         if (snapshot_node_name &&
170743de7e2dSAlberto Garcia             bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
170843de7e2dSAlberto Garcia             error_setg(errp, "New snapshot node name already in use");
170943de7e2dSAlberto Garcia             return;
171043de7e2dSAlberto Garcia         }
171143de7e2dSAlberto Garcia 
1712ba5d6ab6SStefan Hajnoczi         flags = state->old_bs->open_flags;
17134c844983SKevin Wolf         flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
17149b9877eeSWenchao Xia 
17159b9877eeSWenchao Xia         /* create new image w/backing file */
171643de7e2dSAlberto Garcia         mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
17179b9877eeSWenchao Xia         if (mode != NEW_IMAGE_MODE_EXISTING) {
1718f86b8b58SKevin Wolf             int64_t size = bdrv_getlength(state->old_bs);
1719f86b8b58SKevin Wolf             if (size < 0) {
1720f86b8b58SKevin Wolf                 error_setg_errno(errp, -size, "bdrv_getlength failed");
1721f86b8b58SKevin Wolf                 return;
1722f86b8b58SKevin Wolf             }
17239b9877eeSWenchao Xia             bdrv_img_create(new_image_file, format,
1724ba5d6ab6SStefan Hajnoczi                             state->old_bs->filename,
1725ba5d6ab6SStefan Hajnoczi                             state->old_bs->drv->format_name,
1726f86b8b58SKevin Wolf                             NULL, size, flags, &local_err, false);
172784d18f06SMarkus Armbruster             if (local_err) {
17289b9877eeSWenchao Xia                 error_propagate(errp, local_err);
17299b9877eeSWenchao Xia                 return;
17309b9877eeSWenchao Xia             }
17319b9877eeSWenchao Xia         }
17329b9877eeSWenchao Xia 
17330901f67eSBenoît Canet         options = qdict_new();
173443de7e2dSAlberto Garcia         if (s->has_snapshot_node_name) {
17350901f67eSBenoît Canet             qdict_put(options, "node-name",
17360901f67eSBenoît Canet                       qstring_from_str(snapshot_node_name));
17370901f67eSBenoît Canet         }
1738e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
17390901f67eSBenoît Canet 
174043de7e2dSAlberto Garcia         flags |= BDRV_O_NO_BACKING;
174143de7e2dSAlberto Garcia     }
174243de7e2dSAlberto Garcia 
17435b363937SMax Reitz     state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
17445b363937SMax Reitz                               errp);
1745f67503e5SMax Reitz     /* We will manually add the backing_hd field to the bs later */
17465b363937SMax Reitz     if (!state->new_bs) {
174743de7e2dSAlberto Garcia         return;
174843de7e2dSAlberto Garcia     }
174943de7e2dSAlberto Garcia 
17501f0c461bSKevin Wolf     if (bdrv_has_blk(state->new_bs)) {
1751e467da7bSKevin Wolf         error_setg(errp, "The snapshot is already in use");
175243de7e2dSAlberto Garcia         return;
175343de7e2dSAlberto Garcia     }
175443de7e2dSAlberto Garcia 
175543de7e2dSAlberto Garcia     if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
175643de7e2dSAlberto Garcia                            errp)) {
175743de7e2dSAlberto Garcia         return;
175843de7e2dSAlberto Garcia     }
175943de7e2dSAlberto Garcia 
176043de7e2dSAlberto Garcia     if (state->new_bs->backing != NULL) {
176143de7e2dSAlberto Garcia         error_setg(errp, "The snapshot already has a backing image");
176208b24cfeSAlberto Garcia         return;
176308b24cfeSAlberto Garcia     }
176408b24cfeSAlberto Garcia 
176508b24cfeSAlberto Garcia     if (!state->new_bs->drv->supports_backing) {
176608b24cfeSAlberto Garcia         error_setg(errp, "The snapshot does not support backing images");
17679b9877eeSWenchao Xia     }
17689b9877eeSWenchao Xia }
17699b9877eeSWenchao Xia 
177050f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common)
17713b0047e8SWenchao Xia {
1772ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1773ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1774ba0c86a3SWenchao Xia 
17755d6e96efSStefan Hajnoczi     bdrv_set_aio_context(state->new_bs, state->aio_context);
17765d6e96efSStefan Hajnoczi 
1777ba5d6ab6SStefan Hajnoczi     /* This removes our old bs and adds the new bs */
1778ba5d6ab6SStefan Hajnoczi     bdrv_append(state->new_bs, state->old_bs);
17793b0047e8SWenchao Xia     /* We don't need (or want) to use the transactional
17803b0047e8SWenchao Xia      * bdrv_reopen_multiple() across all the entries at once, because we
17813b0047e8SWenchao Xia      * don't want to abort all of them if one of them fails the reopen */
17824c844983SKevin Wolf     if (!state->old_bs->copy_on_read) {
1783dd62f1caSKevin Wolf         bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
17843b0047e8SWenchao Xia                     NULL);
17853b0047e8SWenchao Xia     }
17864c844983SKevin Wolf }
17873b0047e8SWenchao Xia 
178850f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common)
178996b86bf7SWenchao Xia {
1790ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1791ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1792ba5d6ab6SStefan Hajnoczi     if (state->new_bs) {
17934f6fd349SFam Zheng         bdrv_unref(state->new_bs);
179496b86bf7SWenchao Xia     }
1795da763e83SFam Zheng }
1796da763e83SFam Zheng 
179750f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common)
1798da763e83SFam Zheng {
1799da763e83SFam Zheng     ExternalSnapshotState *state =
1800da763e83SFam Zheng                              DO_UPCAST(ExternalSnapshotState, common, common);
18015d6e96efSStefan Hajnoczi     if (state->aio_context) {
1802da763e83SFam Zheng         bdrv_drained_end(state->old_bs);
18035d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
18045d6e96efSStefan Hajnoczi     }
180596b86bf7SWenchao Xia }
180696b86bf7SWenchao Xia 
18073037f364SStefan Hajnoczi typedef struct DriveBackupState {
180850f43f0fSJohn Snow     BlkActionState common;
18093037f364SStefan Hajnoczi     BlockDriverState *bs;
18105d6e96efSStefan Hajnoczi     AioContext *aio_context;
18113037f364SStefan Hajnoczi     BlockJob *job;
18123037f364SStefan Hajnoczi } DriveBackupState;
18133037f364SStefan Hajnoczi 
181481206a89SPavel Butsykin static void do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
181581206a89SPavel Butsykin                             Error **errp);
181678f51fdeSJohn Snow 
181750f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp)
18183037f364SStefan Hajnoczi {
18193037f364SStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1820b7e4fa22SKevin Wolf     BlockDriverState *bs;
18213037f364SStefan Hajnoczi     DriveBackup *backup;
18223037f364SStefan Hajnoczi     Error *local_err = NULL;
18233037f364SStefan Hajnoczi 
18246a8f9661SEric Blake     assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
182532bafa8fSEric Blake     backup = common->action->u.drive_backup.data;
18263037f364SStefan Hajnoczi 
1827b7e4fa22SKevin Wolf     bs = qmp_get_root_bs(backup->device, errp);
1828b7e4fa22SKevin Wolf     if (!bs) {
18291fdd4b7bSFam Zheng         return;
18301fdd4b7bSFam Zheng     }
18311fdd4b7bSFam Zheng 
18325d6e96efSStefan Hajnoczi     /* AioContext is released in .clean() */
1833b7e4fa22SKevin Wolf     state->aio_context = bdrv_get_aio_context(bs);
18345d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
1835b7e4fa22SKevin Wolf     bdrv_drained_begin(bs);
1836b7e4fa22SKevin Wolf     state->bs = bs;
18375d6e96efSStefan Hajnoczi 
183881206a89SPavel Butsykin     do_drive_backup(backup, common->block_job_txn, &local_err);
183984d18f06SMarkus Armbruster     if (local_err) {
18403037f364SStefan Hajnoczi         error_propagate(errp, local_err);
18413037f364SStefan Hajnoczi         return;
18423037f364SStefan Hajnoczi     }
18433037f364SStefan Hajnoczi 
18443037f364SStefan Hajnoczi     state->job = state->bs->job;
18453037f364SStefan Hajnoczi }
18463037f364SStefan Hajnoczi 
184750f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common)
18483037f364SStefan Hajnoczi {
18493037f364SStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
18503037f364SStefan Hajnoczi     BlockDriverState *bs = state->bs;
18513037f364SStefan Hajnoczi 
18523037f364SStefan Hajnoczi     /* Only cancel if it's the job we started */
18533037f364SStefan Hajnoczi     if (bs && bs->job && bs->job == state->job) {
18543037f364SStefan Hajnoczi         block_job_cancel_sync(bs->job);
18553037f364SStefan Hajnoczi     }
18563037f364SStefan Hajnoczi }
18573037f364SStefan Hajnoczi 
185850f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common)
18595d6e96efSStefan Hajnoczi {
18605d6e96efSStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
18615d6e96efSStefan Hajnoczi 
18625d6e96efSStefan Hajnoczi     if (state->aio_context) {
18631fdd4b7bSFam Zheng         bdrv_drained_end(state->bs);
18645d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
18655d6e96efSStefan Hajnoczi     }
18665d6e96efSStefan Hajnoczi }
18675d6e96efSStefan Hajnoczi 
1868bd8baecdSFam Zheng typedef struct BlockdevBackupState {
186950f43f0fSJohn Snow     BlkActionState common;
1870bd8baecdSFam Zheng     BlockDriverState *bs;
1871bd8baecdSFam Zheng     BlockJob *job;
1872bd8baecdSFam Zheng     AioContext *aio_context;
1873bd8baecdSFam Zheng } BlockdevBackupState;
1874bd8baecdSFam Zheng 
1875dc7a4a9eSPavel Butsykin static void do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
1876dc7a4a9eSPavel Butsykin                                Error **errp);
187778f51fdeSJohn Snow 
187850f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1879bd8baecdSFam Zheng {
1880bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1881bd8baecdSFam Zheng     BlockdevBackup *backup;
1882cef34eebSKevin Wolf     BlockDriverState *bs, *target;
1883bd8baecdSFam Zheng     Error *local_err = NULL;
1884bd8baecdSFam Zheng 
18856a8f9661SEric Blake     assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
188632bafa8fSEric Blake     backup = common->action->u.blockdev_backup.data;
1887bd8baecdSFam Zheng 
1888cef34eebSKevin Wolf     bs = qmp_get_root_bs(backup->device, errp);
1889cef34eebSKevin Wolf     if (!bs) {
1890ff52bf36SFam Zheng         return;
1891ff52bf36SFam Zheng     }
1892ff52bf36SFam Zheng 
189339d990acSKevin Wolf     target = bdrv_lookup_bs(backup->target, backup->target, errp);
18945433c24fSMax Reitz     if (!target) {
1895bd8baecdSFam Zheng         return;
1896bd8baecdSFam Zheng     }
1897bd8baecdSFam Zheng 
1898bd8baecdSFam Zheng     /* AioContext is released in .clean() */
1899cef34eebSKevin Wolf     state->aio_context = bdrv_get_aio_context(bs);
190039d990acSKevin Wolf     if (state->aio_context != bdrv_get_aio_context(target)) {
1901bd8baecdSFam Zheng         state->aio_context = NULL;
1902bd8baecdSFam Zheng         error_setg(errp, "Backup between two IO threads is not implemented");
1903bd8baecdSFam Zheng         return;
1904bd8baecdSFam Zheng     }
1905bd8baecdSFam Zheng     aio_context_acquire(state->aio_context);
1906cef34eebSKevin Wolf     state->bs = bs;
1907ff52bf36SFam Zheng     bdrv_drained_begin(state->bs);
1908bd8baecdSFam Zheng 
1909dc7a4a9eSPavel Butsykin     do_blockdev_backup(backup, common->block_job_txn, &local_err);
1910bd8baecdSFam Zheng     if (local_err) {
1911bd8baecdSFam Zheng         error_propagate(errp, local_err);
1912bd8baecdSFam Zheng         return;
1913bd8baecdSFam Zheng     }
1914bd8baecdSFam Zheng 
1915bd8baecdSFam Zheng     state->job = state->bs->job;
1916bd8baecdSFam Zheng }
1917bd8baecdSFam Zheng 
191850f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common)
1919bd8baecdSFam Zheng {
1920bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1921bd8baecdSFam Zheng     BlockDriverState *bs = state->bs;
1922bd8baecdSFam Zheng 
1923bd8baecdSFam Zheng     /* Only cancel if it's the job we started */
1924bd8baecdSFam Zheng     if (bs && bs->job && bs->job == state->job) {
1925bd8baecdSFam Zheng         block_job_cancel_sync(bs->job);
1926bd8baecdSFam Zheng     }
1927bd8baecdSFam Zheng }
1928bd8baecdSFam Zheng 
192950f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common)
1930bd8baecdSFam Zheng {
1931bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1932bd8baecdSFam Zheng 
1933bd8baecdSFam Zheng     if (state->aio_context) {
1934ff52bf36SFam Zheng         bdrv_drained_end(state->bs);
1935bd8baecdSFam Zheng         aio_context_release(state->aio_context);
1936bd8baecdSFam Zheng     }
1937bd8baecdSFam Zheng }
1938bd8baecdSFam Zheng 
1939df9a681dSFam Zheng typedef struct BlockDirtyBitmapState {
194050f43f0fSJohn Snow     BlkActionState common;
1941df9a681dSFam Zheng     BdrvDirtyBitmap *bitmap;
1942df9a681dSFam Zheng     BlockDriverState *bs;
1943df9a681dSFam Zheng     AioContext *aio_context;
1944df9a681dSFam Zheng     HBitmap *backup;
1945df9a681dSFam Zheng     bool prepared;
1946df9a681dSFam Zheng } BlockDirtyBitmapState;
1947df9a681dSFam Zheng 
194850f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common,
1949df9a681dSFam Zheng                                            Error **errp)
1950df9a681dSFam Zheng {
1951df9a681dSFam Zheng     Error *local_err = NULL;
1952df9a681dSFam Zheng     BlockDirtyBitmapAdd *action;
1953df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1954df9a681dSFam Zheng                                              common, common);
1955df9a681dSFam Zheng 
195694d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
195794d16a64SJohn Snow         return;
195894d16a64SJohn Snow     }
195994d16a64SJohn Snow 
196032bafa8fSEric Blake     action = common->action->u.block_dirty_bitmap_add.data;
1961df9a681dSFam Zheng     /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1962df9a681dSFam Zheng     qmp_block_dirty_bitmap_add(action->node, action->name,
1963df9a681dSFam Zheng                                action->has_granularity, action->granularity,
1964df9a681dSFam Zheng                                &local_err);
1965df9a681dSFam Zheng 
1966df9a681dSFam Zheng     if (!local_err) {
1967df9a681dSFam Zheng         state->prepared = true;
1968df9a681dSFam Zheng     } else {
1969df9a681dSFam Zheng         error_propagate(errp, local_err);
1970df9a681dSFam Zheng     }
1971df9a681dSFam Zheng }
1972df9a681dSFam Zheng 
197350f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common)
1974df9a681dSFam Zheng {
1975df9a681dSFam Zheng     BlockDirtyBitmapAdd *action;
1976df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1977df9a681dSFam Zheng                                              common, common);
1978df9a681dSFam Zheng 
197932bafa8fSEric Blake     action = common->action->u.block_dirty_bitmap_add.data;
1980df9a681dSFam Zheng     /* Should not be able to fail: IF the bitmap was added via .prepare(),
1981df9a681dSFam Zheng      * then the node reference and bitmap name must have been valid.
1982df9a681dSFam Zheng      */
1983df9a681dSFam Zheng     if (state->prepared) {
1984df9a681dSFam Zheng         qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
1985df9a681dSFam Zheng     }
1986df9a681dSFam Zheng }
1987df9a681dSFam Zheng 
198850f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
1989df9a681dSFam Zheng                                              Error **errp)
1990df9a681dSFam Zheng {
1991df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1992df9a681dSFam Zheng                                              common, common);
1993df9a681dSFam Zheng     BlockDirtyBitmap *action;
1994df9a681dSFam Zheng 
199594d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
199694d16a64SJohn Snow         return;
199794d16a64SJohn Snow     }
199894d16a64SJohn Snow 
199932bafa8fSEric Blake     action = common->action->u.block_dirty_bitmap_clear.data;
2000df9a681dSFam Zheng     state->bitmap = block_dirty_bitmap_lookup(action->node,
2001df9a681dSFam Zheng                                               action->name,
2002df9a681dSFam Zheng                                               &state->bs,
2003df9a681dSFam Zheng                                               &state->aio_context,
2004df9a681dSFam Zheng                                               errp);
2005df9a681dSFam Zheng     if (!state->bitmap) {
2006df9a681dSFam Zheng         return;
2007df9a681dSFam Zheng     }
2008df9a681dSFam Zheng 
2009df9a681dSFam Zheng     if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2010df9a681dSFam Zheng         error_setg(errp, "Cannot modify a frozen bitmap");
2011df9a681dSFam Zheng         return;
2012df9a681dSFam Zheng     } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2013df9a681dSFam Zheng         error_setg(errp, "Cannot clear a disabled bitmap");
2014df9a681dSFam Zheng         return;
2015df9a681dSFam Zheng     }
2016df9a681dSFam Zheng 
2017df9a681dSFam Zheng     bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2018df9a681dSFam Zheng     /* AioContext is released in .clean() */
2019df9a681dSFam Zheng }
2020df9a681dSFam Zheng 
202150f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common)
2022df9a681dSFam Zheng {
2023df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2024df9a681dSFam Zheng                                              common, common);
2025df9a681dSFam Zheng 
2026df9a681dSFam Zheng     bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2027df9a681dSFam Zheng }
2028df9a681dSFam Zheng 
202950f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common)
2030df9a681dSFam Zheng {
2031df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2032df9a681dSFam Zheng                                              common, common);
2033df9a681dSFam Zheng 
2034df9a681dSFam Zheng     hbitmap_free(state->backup);
2035df9a681dSFam Zheng }
2036df9a681dSFam Zheng 
203750f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common)
2038df9a681dSFam Zheng {
2039df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2040df9a681dSFam Zheng                                              common, common);
2041df9a681dSFam Zheng 
2042df9a681dSFam Zheng     if (state->aio_context) {
2043df9a681dSFam Zheng         aio_context_release(state->aio_context);
2044df9a681dSFam Zheng     }
2045df9a681dSFam Zheng }
2046df9a681dSFam Zheng 
204750f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp)
204878b18b78SStefan Hajnoczi {
204978b18b78SStefan Hajnoczi     error_setg(errp, "Transaction aborted using Abort action");
205078b18b78SStefan Hajnoczi }
205178b18b78SStefan Hajnoczi 
205250f43f0fSJohn Snow static void abort_commit(BlkActionState *common)
205378b18b78SStefan Hajnoczi {
2054dfc6f865SStefan Weil     g_assert_not_reached(); /* this action never succeeds */
205578b18b78SStefan Hajnoczi }
205678b18b78SStefan Hajnoczi 
205750f43f0fSJohn Snow static const BlkActionOps actions[] = {
205843de7e2dSAlberto Garcia     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
205943de7e2dSAlberto Garcia         .instance_size = sizeof(ExternalSnapshotState),
206043de7e2dSAlberto Garcia         .prepare  = external_snapshot_prepare,
206143de7e2dSAlberto Garcia         .commit   = external_snapshot_commit,
206243de7e2dSAlberto Garcia         .abort = external_snapshot_abort,
20634ad6f3dbSAlberto Garcia         .clean = external_snapshot_clean,
206443de7e2dSAlberto Garcia     },
2065c8a83e85SKevin Wolf     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2066ba5d6ab6SStefan Hajnoczi         .instance_size = sizeof(ExternalSnapshotState),
2067ba0c86a3SWenchao Xia         .prepare  = external_snapshot_prepare,
2068ba0c86a3SWenchao Xia         .commit   = external_snapshot_commit,
2069ba0c86a3SWenchao Xia         .abort = external_snapshot_abort,
2070da763e83SFam Zheng         .clean = external_snapshot_clean,
2071ba0c86a3SWenchao Xia     },
20723037f364SStefan Hajnoczi     [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
20733037f364SStefan Hajnoczi         .instance_size = sizeof(DriveBackupState),
20743037f364SStefan Hajnoczi         .prepare = drive_backup_prepare,
20753037f364SStefan Hajnoczi         .abort = drive_backup_abort,
20765d6e96efSStefan Hajnoczi         .clean = drive_backup_clean,
20773037f364SStefan Hajnoczi     },
2078bd8baecdSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2079bd8baecdSFam Zheng         .instance_size = sizeof(BlockdevBackupState),
2080bd8baecdSFam Zheng         .prepare = blockdev_backup_prepare,
2081bd8baecdSFam Zheng         .abort = blockdev_backup_abort,
2082bd8baecdSFam Zheng         .clean = blockdev_backup_clean,
2083bd8baecdSFam Zheng     },
208478b18b78SStefan Hajnoczi     [TRANSACTION_ACTION_KIND_ABORT] = {
208550f43f0fSJohn Snow         .instance_size = sizeof(BlkActionState),
208678b18b78SStefan Hajnoczi         .prepare = abort_prepare,
208778b18b78SStefan Hajnoczi         .commit = abort_commit,
208878b18b78SStefan Hajnoczi     },
2089bbe86010SWenchao Xia     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2090bbe86010SWenchao Xia         .instance_size = sizeof(InternalSnapshotState),
2091bbe86010SWenchao Xia         .prepare  = internal_snapshot_prepare,
2092bbe86010SWenchao Xia         .abort = internal_snapshot_abort,
20935d6e96efSStefan Hajnoczi         .clean = internal_snapshot_clean,
2094bbe86010SWenchao Xia     },
2095df9a681dSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2096df9a681dSFam Zheng         .instance_size = sizeof(BlockDirtyBitmapState),
2097df9a681dSFam Zheng         .prepare = block_dirty_bitmap_add_prepare,
2098df9a681dSFam Zheng         .abort = block_dirty_bitmap_add_abort,
2099df9a681dSFam Zheng     },
2100df9a681dSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2101df9a681dSFam Zheng         .instance_size = sizeof(BlockDirtyBitmapState),
2102df9a681dSFam Zheng         .prepare = block_dirty_bitmap_clear_prepare,
2103df9a681dSFam Zheng         .commit = block_dirty_bitmap_clear_commit,
2104df9a681dSFam Zheng         .abort = block_dirty_bitmap_clear_abort,
2105df9a681dSFam Zheng         .clean = block_dirty_bitmap_clear_clean,
2106df9a681dSFam Zheng     }
2107ba0c86a3SWenchao Xia };
2108ba0c86a3SWenchao Xia 
210994d16a64SJohn Snow /**
211094d16a64SJohn Snow  * Allocate a TransactionProperties structure if necessary, and fill
211194d16a64SJohn Snow  * that structure with desired defaults if they are unset.
211294d16a64SJohn Snow  */
211394d16a64SJohn Snow static TransactionProperties *get_transaction_properties(
211494d16a64SJohn Snow     TransactionProperties *props)
211594d16a64SJohn Snow {
211694d16a64SJohn Snow     if (!props) {
211794d16a64SJohn Snow         props = g_new0(TransactionProperties, 1);
211894d16a64SJohn Snow     }
211994d16a64SJohn Snow 
212094d16a64SJohn Snow     if (!props->has_completion_mode) {
212194d16a64SJohn Snow         props->has_completion_mode = true;
212294d16a64SJohn Snow         props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
212394d16a64SJohn Snow     }
212494d16a64SJohn Snow 
212594d16a64SJohn Snow     return props;
212694d16a64SJohn Snow }
212794d16a64SJohn Snow 
21288802d1fdSJeff Cody /*
2129b756b9ceSStefan Hajnoczi  * 'Atomic' group operations.  The operations are performed as a set, and if
2130b756b9ceSStefan Hajnoczi  * any fail then we roll back all operations in the group.
21318802d1fdSJeff Cody  */
213294d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list,
213394d16a64SJohn Snow                      bool has_props,
213494d16a64SJohn Snow                      struct TransactionProperties *props,
213594d16a64SJohn Snow                      Error **errp)
21368802d1fdSJeff Cody {
2137c8a83e85SKevin Wolf     TransactionActionList *dev_entry = dev_list;
213894d16a64SJohn Snow     BlockJobTxn *block_job_txn = NULL;
213950f43f0fSJohn Snow     BlkActionState *state, *next;
214043e17041SLuiz Capitulino     Error *local_err = NULL;
21418802d1fdSJeff Cody 
214250f43f0fSJohn Snow     QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
21438802d1fdSJeff Cody     QSIMPLEQ_INIT(&snap_bdrv_states);
21448802d1fdSJeff Cody 
214594d16a64SJohn Snow     /* Does this transaction get canceled as a group on failure?
214694d16a64SJohn Snow      * If not, we don't really need to make a BlockJobTxn.
214794d16a64SJohn Snow      */
214894d16a64SJohn Snow     props = get_transaction_properties(props);
214994d16a64SJohn Snow     if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
215094d16a64SJohn Snow         block_job_txn = block_job_txn_new();
215194d16a64SJohn Snow     }
215294d16a64SJohn Snow 
2153b756b9ceSStefan Hajnoczi     /* drain all i/o before any operations */
21548802d1fdSJeff Cody     bdrv_drain_all();
21558802d1fdSJeff Cody 
2156b756b9ceSStefan Hajnoczi     /* We don't do anything in this loop that commits us to the operations */
21578802d1fdSJeff Cody     while (NULL != dev_entry) {
2158c8a83e85SKevin Wolf         TransactionAction *dev_info = NULL;
215950f43f0fSJohn Snow         const BlkActionOps *ops;
216052e7c241SPaolo Bonzini 
21618802d1fdSJeff Cody         dev_info = dev_entry->value;
21628802d1fdSJeff Cody         dev_entry = dev_entry->next;
21638802d1fdSJeff Cody 
21646a8f9661SEric Blake         assert(dev_info->type < ARRAY_SIZE(actions));
2165ba0c86a3SWenchao Xia 
21666a8f9661SEric Blake         ops = &actions[dev_info->type];
2167aa3fe714SMax Reitz         assert(ops->instance_size > 0);
2168aa3fe714SMax Reitz 
2169ba5d6ab6SStefan Hajnoczi         state = g_malloc0(ops->instance_size);
2170ba5d6ab6SStefan Hajnoczi         state->ops = ops;
2171ba5d6ab6SStefan Hajnoczi         state->action = dev_info;
217294d16a64SJohn Snow         state->block_job_txn = block_job_txn;
217394d16a64SJohn Snow         state->txn_props = props;
2174ba5d6ab6SStefan Hajnoczi         QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
21758802d1fdSJeff Cody 
2176ba5d6ab6SStefan Hajnoczi         state->ops->prepare(state, &local_err);
217784d18f06SMarkus Armbruster         if (local_err) {
21789b9877eeSWenchao Xia             error_propagate(errp, local_err);
21799b9877eeSWenchao Xia             goto delete_and_fail;
21809b9877eeSWenchao Xia         }
218152e7c241SPaolo Bonzini     }
21828802d1fdSJeff Cody 
2183ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2184f9ea81e8SStefan Hajnoczi         if (state->ops->commit) {
2185ba5d6ab6SStefan Hajnoczi             state->ops->commit(state);
21868802d1fdSJeff Cody         }
2187f9ea81e8SStefan Hajnoczi     }
21888802d1fdSJeff Cody 
21898802d1fdSJeff Cody     /* success */
21908802d1fdSJeff Cody     goto exit;
21918802d1fdSJeff Cody 
21928802d1fdSJeff Cody delete_and_fail:
2193b756b9ceSStefan Hajnoczi     /* failure, and it is all-or-none; roll back all operations */
2194ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2195ba5d6ab6SStefan Hajnoczi         if (state->ops->abort) {
2196ba5d6ab6SStefan Hajnoczi             state->ops->abort(state);
2197ba0c86a3SWenchao Xia         }
21988802d1fdSJeff Cody     }
21998802d1fdSJeff Cody exit:
2200ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2201ba5d6ab6SStefan Hajnoczi         if (state->ops->clean) {
2202ba5d6ab6SStefan Hajnoczi             state->ops->clean(state);
2203ba0c86a3SWenchao Xia         }
2204ba5d6ab6SStefan Hajnoczi         g_free(state);
22058802d1fdSJeff Cody     }
220694d16a64SJohn Snow     if (!has_props) {
220794d16a64SJohn Snow         qapi_free_TransactionProperties(props);
220894d16a64SJohn Snow     }
220994d16a64SJohn Snow     block_job_txn_unref(block_job_txn);
22108802d1fdSJeff Cody }
22118802d1fdSJeff Cody 
2212fbe2d816SKevin Wolf void qmp_eject(bool has_device, const char *device,
2213fbe2d816SKevin Wolf                bool has_id, const char *id,
2214fbe2d816SKevin Wolf                bool has_force, bool force, Error **errp)
2215666daa68SMarkus Armbruster {
221638f54bd1SMax Reitz     Error *local_err = NULL;
22173a3086b7SJohn Snow     int rc;
2218666daa68SMarkus Armbruster 
22193a3086b7SJohn Snow     if (!has_force) {
22203a3086b7SJohn Snow         force = false;
22213a3086b7SJohn Snow     }
22223a3086b7SJohn Snow 
2223fbe2d816SKevin Wolf     rc = do_open_tray(has_device ? device : NULL,
2224fbe2d816SKevin Wolf                       has_id ? id : NULL,
2225fbe2d816SKevin Wolf                       force, &local_err);
2226bf18bee5SColin Lord     if (rc && rc != -ENOSYS) {
222738f54bd1SMax Reitz         error_propagate(errp, local_err);
2228c245b6a3SLuiz Capitulino         return;
2229666daa68SMarkus Armbruster     }
2230bf18bee5SColin Lord     error_free(local_err);
22313a3086b7SJohn Snow 
2232fbe2d816SKevin Wolf     qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp);
2233666daa68SMarkus Armbruster }
2234666daa68SMarkus Armbruster 
223512d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device,
223612d3ba82SBenoît Canet                       bool has_node_name, const char *node_name,
223712d3ba82SBenoît Canet                       const char *password, Error **errp)
2238666daa68SMarkus Armbruster {
223912d3ba82SBenoît Canet     Error *local_err = NULL;
2240666daa68SMarkus Armbruster     BlockDriverState *bs;
2241e3442099SStefan Hajnoczi     AioContext *aio_context;
2242666daa68SMarkus Armbruster 
224312d3ba82SBenoît Canet     bs = bdrv_lookup_bs(has_device ? device : NULL,
224412d3ba82SBenoît Canet                         has_node_name ? node_name : NULL,
224512d3ba82SBenoît Canet                         &local_err);
224684d18f06SMarkus Armbruster     if (local_err) {
224712d3ba82SBenoît Canet         error_propagate(errp, local_err);
2248a4dea8a9SLuiz Capitulino         return;
2249666daa68SMarkus Armbruster     }
2250666daa68SMarkus Armbruster 
2251e3442099SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2252e3442099SStefan Hajnoczi     aio_context_acquire(aio_context);
2253e3442099SStefan Hajnoczi 
22544d2855a3SMarkus Armbruster     bdrv_add_key(bs, password, errp);
2255666daa68SMarkus Armbruster 
2256e3442099SStefan Hajnoczi     aio_context_release(aio_context);
2257e3442099SStefan Hajnoczi }
2258e3442099SStefan Hajnoczi 
2259bf18bee5SColin Lord /*
2260bf18bee5SColin Lord  * Attempt to open the tray of @device.
2261bf18bee5SColin Lord  * If @force, ignore its tray lock.
2262bf18bee5SColin Lord  * Else, if the tray is locked, don't open it, but ask the guest to open it.
2263bf18bee5SColin Lord  * On error, store an error through @errp and return -errno.
2264bf18bee5SColin Lord  * If @device does not exist, return -ENODEV.
2265bf18bee5SColin Lord  * If it has no removable media, return -ENOTSUP.
2266bf18bee5SColin Lord  * If it has no tray, return -ENOSYS.
2267bf18bee5SColin Lord  * If the guest was asked to open the tray, return -EINPROGRESS.
2268bf18bee5SColin Lord  * Else, return 0.
22693a3086b7SJohn Snow  */
2270b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id,
2271b33945cfSKevin Wolf                         bool force, Error **errp)
22727d8a9f71SMax Reitz {
22737d8a9f71SMax Reitz     BlockBackend *blk;
2274b33945cfSKevin Wolf     const char *device = qdev_id ?: blk_name;
22757d8a9f71SMax Reitz     bool locked;
22767d8a9f71SMax Reitz 
2277b33945cfSKevin Wolf     blk = qmp_get_blk(blk_name, qdev_id, errp);
22787d8a9f71SMax Reitz     if (!blk) {
22793a3086b7SJohn Snow         return -ENODEV;
22807d8a9f71SMax Reitz     }
22817d8a9f71SMax Reitz 
22827d8a9f71SMax Reitz     if (!blk_dev_has_removable_media(blk)) {
22837d8a9f71SMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
22843a3086b7SJohn Snow         return -ENOTSUP;
22857d8a9f71SMax Reitz     }
22867d8a9f71SMax Reitz 
228712c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
2288bf18bee5SColin Lord         error_setg(errp, "Device '%s' does not have a tray", device);
2289bf18bee5SColin Lord         return -ENOSYS;
229012c7ec87SMax Reitz     }
229112c7ec87SMax Reitz 
22927d8a9f71SMax Reitz     if (blk_dev_is_tray_open(blk)) {
22933a3086b7SJohn Snow         return 0;
22947d8a9f71SMax Reitz     }
22957d8a9f71SMax Reitz 
22967d8a9f71SMax Reitz     locked = blk_dev_is_medium_locked(blk);
22977d8a9f71SMax Reitz     if (locked) {
22987d8a9f71SMax Reitz         blk_dev_eject_request(blk, force);
22997d8a9f71SMax Reitz     }
23007d8a9f71SMax Reitz 
23017d8a9f71SMax Reitz     if (!locked || force) {
23027d8a9f71SMax Reitz         blk_dev_change_media_cb(blk, false);
23037d8a9f71SMax Reitz     }
23043a3086b7SJohn Snow 
23053a3086b7SJohn Snow     if (locked && !force) {
2306bf18bee5SColin Lord         error_setg(errp, "Device '%s' is locked and force was not specified, "
2307bf18bee5SColin Lord                    "wait for tray to open and try again", device);
2308bf18bee5SColin Lord         return -EINPROGRESS;
23093a3086b7SJohn Snow     }
23103a3086b7SJohn Snow 
23113a3086b7SJohn Snow     return 0;
23123a3086b7SJohn Snow }
23133a3086b7SJohn Snow 
2314b33945cfSKevin Wolf void qmp_blockdev_open_tray(bool has_device, const char *device,
2315b33945cfSKevin Wolf                             bool has_id, const char *id,
2316b33945cfSKevin Wolf                             bool has_force, bool force,
23173a3086b7SJohn Snow                             Error **errp)
23183a3086b7SJohn Snow {
2319bf18bee5SColin Lord     Error *local_err = NULL;
2320bf18bee5SColin Lord     int rc;
2321bf18bee5SColin Lord 
23223a3086b7SJohn Snow     if (!has_force) {
23233a3086b7SJohn Snow         force = false;
23243a3086b7SJohn Snow     }
2325b33945cfSKevin Wolf     rc = do_open_tray(has_device ? device : NULL,
2326b33945cfSKevin Wolf                       has_id ? id : NULL,
2327b33945cfSKevin Wolf                       force, &local_err);
2328bf18bee5SColin Lord     if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2329bf18bee5SColin Lord         error_propagate(errp, local_err);
2330bf18bee5SColin Lord         return;
2331bf18bee5SColin Lord     }
2332bf18bee5SColin Lord     error_free(local_err);
23337d8a9f71SMax Reitz }
23347d8a9f71SMax Reitz 
2335b33945cfSKevin Wolf void qmp_blockdev_close_tray(bool has_device, const char *device,
2336b33945cfSKevin Wolf                              bool has_id, const char *id,
2337b33945cfSKevin Wolf                              Error **errp)
2338abaaf59dSMax Reitz {
2339abaaf59dSMax Reitz     BlockBackend *blk;
2340abaaf59dSMax Reitz 
2341b33945cfSKevin Wolf     device = has_device ? device : NULL;
2342b33945cfSKevin Wolf     id = has_id ? id : NULL;
2343b33945cfSKevin Wolf 
2344b33945cfSKevin Wolf     blk = qmp_get_blk(device, id, errp);
2345abaaf59dSMax Reitz     if (!blk) {
2346abaaf59dSMax Reitz         return;
2347abaaf59dSMax Reitz     }
2348abaaf59dSMax Reitz 
2349abaaf59dSMax Reitz     if (!blk_dev_has_removable_media(blk)) {
2350b33945cfSKevin Wolf         error_setg(errp, "Device '%s' is not removable", device ?: id);
2351abaaf59dSMax Reitz         return;
2352abaaf59dSMax Reitz     }
2353abaaf59dSMax Reitz 
235412c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
235512c7ec87SMax Reitz         /* Ignore this command on tray-less devices */
235612c7ec87SMax Reitz         return;
235712c7ec87SMax Reitz     }
235812c7ec87SMax Reitz 
2359abaaf59dSMax Reitz     if (!blk_dev_is_tray_open(blk)) {
2360abaaf59dSMax Reitz         return;
2361abaaf59dSMax Reitz     }
2362abaaf59dSMax Reitz 
2363abaaf59dSMax Reitz     blk_dev_change_media_cb(blk, true);
2364abaaf59dSMax Reitz }
2365abaaf59dSMax Reitz 
236600949babSKevin Wolf void qmp_x_blockdev_remove_medium(bool has_device, const char *device,
236700949babSKevin Wolf                                   bool has_id, const char *id, Error **errp)
23682814f672SMax Reitz {
23692814f672SMax Reitz     BlockBackend *blk;
23702814f672SMax Reitz     BlockDriverState *bs;
23712814f672SMax Reitz     AioContext *aio_context;
237200949babSKevin Wolf     bool has_attached_device;
23732814f672SMax Reitz 
237400949babSKevin Wolf     device = has_device ? device : NULL;
237500949babSKevin Wolf     id = has_id ? id : NULL;
237600949babSKevin Wolf 
237700949babSKevin Wolf     blk = qmp_get_blk(device, id, errp);
23782814f672SMax Reitz     if (!blk) {
23792814f672SMax Reitz         return;
23802814f672SMax Reitz     }
23812814f672SMax Reitz 
23822814f672SMax Reitz     /* For BBs without a device, we can exchange the BDS tree at will */
238300949babSKevin Wolf     has_attached_device = blk_get_attached_dev(blk);
23842814f672SMax Reitz 
238500949babSKevin Wolf     if (has_attached_device && !blk_dev_has_removable_media(blk)) {
238600949babSKevin Wolf         error_setg(errp, "Device '%s' is not removable", device ?: id);
23872814f672SMax Reitz         return;
23882814f672SMax Reitz     }
23892814f672SMax Reitz 
239000949babSKevin Wolf     if (has_attached_device && blk_dev_has_tray(blk) &&
239100949babSKevin Wolf         !blk_dev_is_tray_open(blk))
239200949babSKevin Wolf     {
239300949babSKevin Wolf         error_setg(errp, "Tray of device '%s' is not open", device ?: id);
23942814f672SMax Reitz         return;
23952814f672SMax Reitz     }
23962814f672SMax Reitz 
23972814f672SMax Reitz     bs = blk_bs(blk);
23982814f672SMax Reitz     if (!bs) {
23992814f672SMax Reitz         return;
24002814f672SMax Reitz     }
24012814f672SMax Reitz 
24022814f672SMax Reitz     aio_context = bdrv_get_aio_context(bs);
24032814f672SMax Reitz     aio_context_acquire(aio_context);
24042814f672SMax Reitz 
24052814f672SMax Reitz     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
24062814f672SMax Reitz         goto out;
24072814f672SMax Reitz     }
24082814f672SMax Reitz 
24092814f672SMax Reitz     blk_remove_bs(blk);
24102814f672SMax Reitz 
241112c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
241212c7ec87SMax Reitz         /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
241312c7ec87SMax Reitz          * called at all); therefore, the medium needs to be ejected here.
241412c7ec87SMax Reitz          * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
241512c7ec87SMax Reitz          * value passed here (i.e. false). */
241612c7ec87SMax Reitz         blk_dev_change_media_cb(blk, false);
241712c7ec87SMax Reitz     }
241812c7ec87SMax Reitz 
24192814f672SMax Reitz out:
24202814f672SMax Reitz     aio_context_release(aio_context);
24212814f672SMax Reitz }
24222814f672SMax Reitz 
2423716df217SKevin Wolf static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
2424d1299882SMax Reitz                                             BlockDriverState *bs, Error **errp)
2425d1299882SMax Reitz {
2426d1299882SMax Reitz     bool has_device;
2427d1299882SMax Reitz 
2428d1299882SMax Reitz     /* For BBs without a device, we can exchange the BDS tree at will */
2429d1299882SMax Reitz     has_device = blk_get_attached_dev(blk);
2430d1299882SMax Reitz 
2431d1299882SMax Reitz     if (has_device && !blk_dev_has_removable_media(blk)) {
2432716df217SKevin Wolf         error_setg(errp, "Device is not removable");
2433d1299882SMax Reitz         return;
2434d1299882SMax Reitz     }
2435d1299882SMax Reitz 
243612c7ec87SMax Reitz     if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2437716df217SKevin Wolf         error_setg(errp, "Tray of the device is not open");
2438d1299882SMax Reitz         return;
2439d1299882SMax Reitz     }
2440d1299882SMax Reitz 
2441d1299882SMax Reitz     if (blk_bs(blk)) {
2442716df217SKevin Wolf         error_setg(errp, "There already is a medium in the device");
2443d1299882SMax Reitz         return;
2444d1299882SMax Reitz     }
2445d1299882SMax Reitz 
2446d1299882SMax Reitz     blk_insert_bs(blk, bs);
2447d1299882SMax Reitz 
244812c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
244912c7ec87SMax Reitz         /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
245012c7ec87SMax Reitz          * called at all); therefore, the medium needs to be pushed into the
245112c7ec87SMax Reitz          * slot here.
245212c7ec87SMax Reitz          * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
245312c7ec87SMax Reitz          * value passed here (i.e. true). */
245412c7ec87SMax Reitz         blk_dev_change_media_cb(blk, true);
245512c7ec87SMax Reitz     }
2456d1299882SMax Reitz }
2457d1299882SMax Reitz 
2458716df217SKevin Wolf void qmp_x_blockdev_insert_medium(bool has_device, const char *device,
2459716df217SKevin Wolf                                   bool has_id, const char *id,
2460716df217SKevin Wolf                                   const char *node_name, Error **errp)
2461d1299882SMax Reitz {
2462716df217SKevin Wolf     BlockBackend *blk;
2463d1299882SMax Reitz     BlockDriverState *bs;
2464d1299882SMax Reitz 
2465716df217SKevin Wolf     blk = qmp_get_blk(has_device ? device : NULL,
2466716df217SKevin Wolf                       has_id ? id : NULL,
2467716df217SKevin Wolf                       errp);
2468716df217SKevin Wolf     if (!blk) {
2469716df217SKevin Wolf         return;
2470716df217SKevin Wolf     }
2471716df217SKevin Wolf 
2472d1299882SMax Reitz     bs = bdrv_find_node(node_name);
2473d1299882SMax Reitz     if (!bs) {
2474d1299882SMax Reitz         error_setg(errp, "Node '%s' not found", node_name);
2475d1299882SMax Reitz         return;
2476d1299882SMax Reitz     }
2477d1299882SMax Reitz 
24781f0c461bSKevin Wolf     if (bdrv_has_blk(bs)) {
2479e467da7bSKevin Wolf         error_setg(errp, "Node '%s' is already in use", node_name);
2480d1299882SMax Reitz         return;
2481d1299882SMax Reitz     }
2482d1299882SMax Reitz 
2483716df217SKevin Wolf     qmp_blockdev_insert_anon_medium(blk, bs, errp);
2484d1299882SMax Reitz }
2485d1299882SMax Reitz 
248670e2cb3bSKevin Wolf void qmp_blockdev_change_medium(bool has_device, const char *device,
248770e2cb3bSKevin Wolf                                 bool has_id, const char *id,
248870e2cb3bSKevin Wolf                                 const char *filename,
248924fb4133SMax Reitz                                 bool has_format, const char *format,
249039ff43d9SMax Reitz                                 bool has_read_only,
249139ff43d9SMax Reitz                                 BlockdevChangeReadOnlyMode read_only,
249224fb4133SMax Reitz                                 Error **errp)
2493de2c6c05SMax Reitz {
2494de2c6c05SMax Reitz     BlockBackend *blk;
2495de2c6c05SMax Reitz     BlockDriverState *medium_bs = NULL;
24965b363937SMax Reitz     int bdrv_flags;
2497b85114f8SKevin Wolf     bool detect_zeroes;
249838a53d50SColin Lord     int rc;
2499de2c6c05SMax Reitz     QDict *options = NULL;
2500de2c6c05SMax Reitz     Error *err = NULL;
2501de2c6c05SMax Reitz 
250270e2cb3bSKevin Wolf     blk = qmp_get_blk(has_device ? device : NULL,
250370e2cb3bSKevin Wolf                       has_id ? id : NULL,
250470e2cb3bSKevin Wolf                       errp);
2505de2c6c05SMax Reitz     if (!blk) {
2506de2c6c05SMax Reitz         goto fail;
2507de2c6c05SMax Reitz     }
2508de2c6c05SMax Reitz 
2509de2c6c05SMax Reitz     if (blk_bs(blk)) {
2510de2c6c05SMax Reitz         blk_update_root_state(blk);
2511de2c6c05SMax Reitz     }
2512de2c6c05SMax Reitz 
2513de2c6c05SMax Reitz     bdrv_flags = blk_get_open_flags_from_root_state(blk);
2514156abc2fSAlyssa Milburn     bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2515156abc2fSAlyssa Milburn         BDRV_O_PROTOCOL);
2516de2c6c05SMax Reitz 
251739ff43d9SMax Reitz     if (!has_read_only) {
251839ff43d9SMax Reitz         read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
251939ff43d9SMax Reitz     }
252039ff43d9SMax Reitz 
252139ff43d9SMax Reitz     switch (read_only) {
252239ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
252339ff43d9SMax Reitz         break;
252439ff43d9SMax Reitz 
252539ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
252639ff43d9SMax Reitz         bdrv_flags &= ~BDRV_O_RDWR;
252739ff43d9SMax Reitz         break;
252839ff43d9SMax Reitz 
252939ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
253039ff43d9SMax Reitz         bdrv_flags |= BDRV_O_RDWR;
253139ff43d9SMax Reitz         break;
253239ff43d9SMax Reitz 
253339ff43d9SMax Reitz     default:
253439ff43d9SMax Reitz         abort();
253539ff43d9SMax Reitz     }
253639ff43d9SMax Reitz 
2537de2c6c05SMax Reitz     options = qdict_new();
2538b85114f8SKevin Wolf     detect_zeroes = blk_get_detect_zeroes_from_root_state(blk);
2539b85114f8SKevin Wolf     qdict_put(options, "detect-zeroes",
2540b85114f8SKevin Wolf               qstring_from_str(detect_zeroes ? "on" : "off"));
2541b85114f8SKevin Wolf 
2542b85114f8SKevin Wolf     if (has_format) {
2543de2c6c05SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
2544de2c6c05SMax Reitz     }
2545de2c6c05SMax Reitz 
25465b363937SMax Reitz     medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
25475b363937SMax Reitz     if (!medium_bs) {
2548de2c6c05SMax Reitz         goto fail;
2549de2c6c05SMax Reitz     }
2550de2c6c05SMax Reitz 
2551de2c6c05SMax Reitz     bdrv_add_key(medium_bs, NULL, &err);
2552de2c6c05SMax Reitz     if (err) {
2553de2c6c05SMax Reitz         error_propagate(errp, err);
2554de2c6c05SMax Reitz         goto fail;
2555de2c6c05SMax Reitz     }
2556de2c6c05SMax Reitz 
255770e2cb3bSKevin Wolf     rc = do_open_tray(has_device ? device : NULL,
255870e2cb3bSKevin Wolf                       has_id ? id : NULL,
255970e2cb3bSKevin Wolf                       false, &err);
256038a53d50SColin Lord     if (rc && rc != -ENOSYS) {
2561de2c6c05SMax Reitz         error_propagate(errp, err);
2562de2c6c05SMax Reitz         goto fail;
2563de2c6c05SMax Reitz     }
256438a53d50SColin Lord     error_free(err);
256538a53d50SColin Lord     err = NULL;
2566de2c6c05SMax Reitz 
256724df38b0SKevin Wolf     qmp_x_blockdev_remove_medium(has_device, device, has_id, id, &err);
2568de2c6c05SMax Reitz     if (err) {
2569de2c6c05SMax Reitz         error_propagate(errp, err);
2570de2c6c05SMax Reitz         goto fail;
2571de2c6c05SMax Reitz     }
2572de2c6c05SMax Reitz 
2573716df217SKevin Wolf     qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
2574de2c6c05SMax Reitz     if (err) {
2575de2c6c05SMax Reitz         error_propagate(errp, err);
2576de2c6c05SMax Reitz         goto fail;
2577de2c6c05SMax Reitz     }
2578de2c6c05SMax Reitz 
257970e2cb3bSKevin Wolf     qmp_blockdev_close_tray(has_device, device, has_id, id, errp);
2580de2c6c05SMax Reitz 
2581de2c6c05SMax Reitz fail:
2582de2c6c05SMax Reitz     /* If the medium has been inserted, the device has its own reference, so
2583de2c6c05SMax Reitz      * ours must be relinquished; and if it has not been inserted successfully,
2584de2c6c05SMax Reitz      * the reference must be relinquished anyway */
2585de2c6c05SMax Reitz     bdrv_unref(medium_bs);
2586de2c6c05SMax Reitz }
2587de2c6c05SMax Reitz 
2588727f005eSZhi Yong Wu /* throttling disk I/O limits */
25894dc9397bSEric Blake void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
2590727f005eSZhi Yong Wu {
2591cc0681c4SBenoît Canet     ThrottleConfig cfg;
2592727f005eSZhi Yong Wu     BlockDriverState *bs;
2593a0e8544cSFam Zheng     BlockBackend *blk;
2594b15446fdSStefan Hajnoczi     AioContext *aio_context;
2595727f005eSZhi Yong Wu 
25967a9877a0SKevin Wolf     blk = qmp_get_blk(arg->has_device ? arg->device : NULL,
25977a9877a0SKevin Wolf                       arg->has_id ? arg->id : NULL,
25987a9877a0SKevin Wolf                       errp);
2599a0e8544cSFam Zheng     if (!blk) {
260080047da5SLuiz Capitulino         return;
2601727f005eSZhi Yong Wu     }
26025433c24fSMax Reitz 
26035433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
26045433c24fSMax Reitz     aio_context_acquire(aio_context);
26055433c24fSMax Reitz 
2606a0e8544cSFam Zheng     bs = blk_bs(blk);
26075433c24fSMax Reitz     if (!bs) {
26087a9877a0SKevin Wolf         error_setg(errp, "Device has no medium");
26095433c24fSMax Reitz         goto out;
26105433c24fSMax Reitz     }
2611727f005eSZhi Yong Wu 
26121588ab5dSAlberto Garcia     throttle_config_init(&cfg);
26134dc9397bSEric Blake     cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
26144dc9397bSEric Blake     cfg.buckets[THROTTLE_BPS_READ].avg  = arg->bps_rd;
26154dc9397bSEric Blake     cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
2616727f005eSZhi Yong Wu 
26174dc9397bSEric Blake     cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
26184dc9397bSEric Blake     cfg.buckets[THROTTLE_OPS_READ].avg  = arg->iops_rd;
26194dc9397bSEric Blake     cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
2620cc0681c4SBenoît Canet 
26214dc9397bSEric Blake     if (arg->has_bps_max) {
26224dc9397bSEric Blake         cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
26233e9fab69SBenoît Canet     }
26244dc9397bSEric Blake     if (arg->has_bps_rd_max) {
26254dc9397bSEric Blake         cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
26263e9fab69SBenoît Canet     }
26274dc9397bSEric Blake     if (arg->has_bps_wr_max) {
26284dc9397bSEric Blake         cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
26293e9fab69SBenoît Canet     }
26304dc9397bSEric Blake     if (arg->has_iops_max) {
26314dc9397bSEric Blake         cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
26323e9fab69SBenoît Canet     }
26334dc9397bSEric Blake     if (arg->has_iops_rd_max) {
26344dc9397bSEric Blake         cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
26353e9fab69SBenoît Canet     }
26364dc9397bSEric Blake     if (arg->has_iops_wr_max) {
26374dc9397bSEric Blake         cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
26383e9fab69SBenoît Canet     }
2639cc0681c4SBenoît Canet 
26404dc9397bSEric Blake     if (arg->has_bps_max_length) {
26414dc9397bSEric Blake         cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
2642dce13204SAlberto Garcia     }
26434dc9397bSEric Blake     if (arg->has_bps_rd_max_length) {
26444dc9397bSEric Blake         cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
2645dce13204SAlberto Garcia     }
26464dc9397bSEric Blake     if (arg->has_bps_wr_max_length) {
26474dc9397bSEric Blake         cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
2648dce13204SAlberto Garcia     }
26494dc9397bSEric Blake     if (arg->has_iops_max_length) {
26504dc9397bSEric Blake         cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
2651dce13204SAlberto Garcia     }
26524dc9397bSEric Blake     if (arg->has_iops_rd_max_length) {
26534dc9397bSEric Blake         cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
2654dce13204SAlberto Garcia     }
26554dc9397bSEric Blake     if (arg->has_iops_wr_max_length) {
26564dc9397bSEric Blake         cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
2657dce13204SAlberto Garcia     }
2658dce13204SAlberto Garcia 
26594dc9397bSEric Blake     if (arg->has_iops_size) {
26604dc9397bSEric Blake         cfg.op_size = arg->iops_size;
26612024c1dfSBenoît Canet     }
2662cc0681c4SBenoît Canet 
2663d5851089SAlberto Garcia     if (!throttle_is_valid(&cfg, errp)) {
26645433c24fSMax Reitz         goto out;
2665727f005eSZhi Yong Wu     }
2666727f005eSZhi Yong Wu 
266776f4afb4SAlberto Garcia     if (throttle_enabled(&cfg)) {
266876f4afb4SAlberto Garcia         /* Enable I/O limits if they're not enabled yet, otherwise
266976f4afb4SAlberto Garcia          * just update the throttling group. */
267097148076SKevin Wolf         if (!blk_get_public(blk)->throttle_state) {
26714dc9397bSEric Blake             blk_io_limits_enable(blk,
26727a9877a0SKevin Wolf                                  arg->has_group ? arg->group :
26737a9877a0SKevin Wolf                                  arg->has_device ? arg->device :
26747a9877a0SKevin Wolf                                  arg->id);
26754dc9397bSEric Blake         } else if (arg->has_group) {
26764dc9397bSEric Blake             blk_io_limits_update_group(blk, arg->group);
2677727f005eSZhi Yong Wu         }
267876f4afb4SAlberto Garcia         /* Set the new throttling configuration */
267997148076SKevin Wolf         blk_set_io_limits(blk, &cfg);
268097148076SKevin Wolf     } else if (blk_get_public(blk)->throttle_state) {
268176f4afb4SAlberto Garcia         /* If all throttling settings are set to 0, disable I/O limits */
268297148076SKevin Wolf         blk_io_limits_disable(blk);
2683727f005eSZhi Yong Wu     }
2684b15446fdSStefan Hajnoczi 
26855433c24fSMax Reitz out:
2686b15446fdSStefan Hajnoczi     aio_context_release(aio_context);
2687727f005eSZhi Yong Wu }
2688727f005eSZhi Yong Wu 
2689341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2690341ebc2fSJohn Snow                                 bool has_granularity, uint32_t granularity,
2691341ebc2fSJohn Snow                                 Error **errp)
2692341ebc2fSJohn Snow {
2693341ebc2fSJohn Snow     AioContext *aio_context;
2694341ebc2fSJohn Snow     BlockDriverState *bs;
2695341ebc2fSJohn Snow 
2696341ebc2fSJohn Snow     if (!name || name[0] == '\0') {
2697341ebc2fSJohn Snow         error_setg(errp, "Bitmap name cannot be empty");
2698341ebc2fSJohn Snow         return;
2699341ebc2fSJohn Snow     }
2700341ebc2fSJohn Snow 
2701341ebc2fSJohn Snow     bs = bdrv_lookup_bs(node, node, errp);
2702341ebc2fSJohn Snow     if (!bs) {
2703341ebc2fSJohn Snow         return;
2704341ebc2fSJohn Snow     }
2705341ebc2fSJohn Snow 
2706341ebc2fSJohn Snow     aio_context = bdrv_get_aio_context(bs);
2707341ebc2fSJohn Snow     aio_context_acquire(aio_context);
2708341ebc2fSJohn Snow 
2709341ebc2fSJohn Snow     if (has_granularity) {
2710341ebc2fSJohn Snow         if (granularity < 512 || !is_power_of_2(granularity)) {
2711341ebc2fSJohn Snow             error_setg(errp, "Granularity must be power of 2 "
2712341ebc2fSJohn Snow                              "and at least 512");
2713341ebc2fSJohn Snow             goto out;
2714341ebc2fSJohn Snow         }
2715341ebc2fSJohn Snow     } else {
2716341ebc2fSJohn Snow         /* Default to cluster size, if available: */
2717341ebc2fSJohn Snow         granularity = bdrv_get_default_bitmap_granularity(bs);
2718341ebc2fSJohn Snow     }
2719341ebc2fSJohn Snow 
2720341ebc2fSJohn Snow     bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2721341ebc2fSJohn Snow 
2722341ebc2fSJohn Snow  out:
2723341ebc2fSJohn Snow     aio_context_release(aio_context);
2724341ebc2fSJohn Snow }
2725341ebc2fSJohn Snow 
2726341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2727341ebc2fSJohn Snow                                    Error **errp)
2728341ebc2fSJohn Snow {
2729341ebc2fSJohn Snow     AioContext *aio_context;
2730341ebc2fSJohn Snow     BlockDriverState *bs;
2731341ebc2fSJohn Snow     BdrvDirtyBitmap *bitmap;
2732341ebc2fSJohn Snow 
2733341ebc2fSJohn Snow     bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2734341ebc2fSJohn Snow     if (!bitmap || !bs) {
2735341ebc2fSJohn Snow         return;
2736341ebc2fSJohn Snow     }
2737341ebc2fSJohn Snow 
27389bd2b08fSJohn Snow     if (bdrv_dirty_bitmap_frozen(bitmap)) {
27399bd2b08fSJohn Snow         error_setg(errp,
27409bd2b08fSJohn Snow                    "Bitmap '%s' is currently frozen and cannot be removed",
27419bd2b08fSJohn Snow                    name);
27429bd2b08fSJohn Snow         goto out;
27439bd2b08fSJohn Snow     }
274420dca810SJohn Snow     bdrv_dirty_bitmap_make_anon(bitmap);
2745341ebc2fSJohn Snow     bdrv_release_dirty_bitmap(bs, bitmap);
2746341ebc2fSJohn Snow 
27479bd2b08fSJohn Snow  out:
2748341ebc2fSJohn Snow     aio_context_release(aio_context);
2749341ebc2fSJohn Snow }
2750341ebc2fSJohn Snow 
2751e74e6b78SJohn Snow /**
2752e74e6b78SJohn Snow  * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2753e74e6b78SJohn Snow  * immediately after a full backup operation.
2754e74e6b78SJohn Snow  */
2755e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2756e74e6b78SJohn Snow                                   Error **errp)
2757e74e6b78SJohn Snow {
2758e74e6b78SJohn Snow     AioContext *aio_context;
2759e74e6b78SJohn Snow     BdrvDirtyBitmap *bitmap;
2760e74e6b78SJohn Snow     BlockDriverState *bs;
2761e74e6b78SJohn Snow 
2762e74e6b78SJohn Snow     bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2763e74e6b78SJohn Snow     if (!bitmap || !bs) {
2764e74e6b78SJohn Snow         return;
2765e74e6b78SJohn Snow     }
2766e74e6b78SJohn Snow 
2767e74e6b78SJohn Snow     if (bdrv_dirty_bitmap_frozen(bitmap)) {
2768e74e6b78SJohn Snow         error_setg(errp,
2769e74e6b78SJohn Snow                    "Bitmap '%s' is currently frozen and cannot be modified",
2770e74e6b78SJohn Snow                    name);
2771e74e6b78SJohn Snow         goto out;
2772e74e6b78SJohn Snow     } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2773e74e6b78SJohn Snow         error_setg(errp,
2774e74e6b78SJohn Snow                    "Bitmap '%s' is currently disabled and cannot be cleared",
2775e74e6b78SJohn Snow                    name);
2776e74e6b78SJohn Snow         goto out;
2777e74e6b78SJohn Snow     }
2778e74e6b78SJohn Snow 
2779df9a681dSFam Zheng     bdrv_clear_dirty_bitmap(bitmap, NULL);
2780e74e6b78SJohn Snow 
2781e74e6b78SJohn Snow  out:
2782e74e6b78SJohn Snow     aio_context_release(aio_context);
2783e74e6b78SJohn Snow }
2784e74e6b78SJohn Snow 
2785072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict)
27869063f814SRyan Harper {
27879063f814SRyan Harper     const char *id = qdict_get_str(qdict, "id");
27887e7d56d9SMarkus Armbruster     BlockBackend *blk;
27899063f814SRyan Harper     BlockDriverState *bs;
27908ad4202bSStefan Hajnoczi     AioContext *aio_context;
27913718d8abSFam Zheng     Error *local_err = NULL;
27929063f814SRyan Harper 
27932073d410SKevin Wolf     bs = bdrv_find_node(id);
27942073d410SKevin Wolf     if (bs) {
27959ec8873eSKevin Wolf         qmp_x_blockdev_del(id, &local_err);
27962073d410SKevin Wolf         if (local_err) {
27972073d410SKevin Wolf             error_report_err(local_err);
27982073d410SKevin Wolf         }
27992073d410SKevin Wolf         return;
28002073d410SKevin Wolf     }
28012073d410SKevin Wolf 
28027e7d56d9SMarkus Armbruster     blk = blk_by_name(id);
28037e7d56d9SMarkus Armbruster     if (!blk) {
2804b1422f20SMarkus Armbruster         error_report("Device '%s' not found", id);
2805072ebe6bSMarkus Armbruster         return;
28069063f814SRyan Harper     }
28078ad4202bSStefan Hajnoczi 
280826f8b3a8SMarkus Armbruster     if (!blk_legacy_dinfo(blk)) {
280948f364ddSMarkus Armbruster         error_report("Deleting device added with blockdev-add"
281048f364ddSMarkus Armbruster                      " is not supported");
2811072ebe6bSMarkus Armbruster         return;
281248f364ddSMarkus Armbruster     }
281348f364ddSMarkus Armbruster 
28145433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
28158ad4202bSStefan Hajnoczi     aio_context_acquire(aio_context);
28168ad4202bSStefan Hajnoczi 
28175433c24fSMax Reitz     bs = blk_bs(blk);
28185433c24fSMax Reitz     if (bs) {
28193718d8abSFam Zheng         if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2820565f65d2SMarkus Armbruster             error_report_err(local_err);
28218ad4202bSStefan Hajnoczi             aio_context_release(aio_context);
2822072ebe6bSMarkus Armbruster             return;
28238591675fSMarcelo Tosatti         }
28249063f814SRyan Harper 
2825938abd43SMax Reitz         blk_remove_bs(blk);
28265433c24fSMax Reitz     }
28279063f814SRyan Harper 
28287c735873SMax Reitz     /* Make the BlockBackend and the attached BlockDriverState anonymous */
2829efaa7c4eSMax Reitz     monitor_remove_blk(blk);
2830efaa7c4eSMax Reitz 
28317c735873SMax Reitz     /* If this BlockBackend has a device attached to it, its refcount will be
28327c735873SMax Reitz      * decremented when the device is removed; otherwise we have to do so here.
2833d22b2f41SRyan Harper      */
2834a7f53e26SMarkus Armbruster     if (blk_get_attached_dev(blk)) {
2835293c51a6SStefan Hajnoczi         /* Further I/O must not pause the guest */
2836373340b2SMax Reitz         blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2837293c51a6SStefan Hajnoczi                          BLOCKDEV_ON_ERROR_REPORT);
2838d22b2f41SRyan Harper     } else {
2839b9fe8a7aSMarkus Armbruster         blk_unref(blk);
2840d22b2f41SRyan Harper     }
28419063f814SRyan Harper 
28428ad4202bSStefan Hajnoczi     aio_context_release(aio_context);
28439063f814SRyan Harper }
28446d4a2b3aSChristoph Hellwig 
28453b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device,
28463b1dbd11SBenoît Canet                       bool has_node_name, const char *node_name,
28473b1dbd11SBenoît Canet                       int64_t size, Error **errp)
28486d4a2b3aSChristoph Hellwig {
28493b1dbd11SBenoît Canet     Error *local_err = NULL;
28506d4a2b3aSChristoph Hellwig     BlockDriverState *bs;
2851927e0e76SStefan Hajnoczi     AioContext *aio_context;
28528732901eSKevin Wolf     int ret;
28536d4a2b3aSChristoph Hellwig 
28543b1dbd11SBenoît Canet     bs = bdrv_lookup_bs(has_device ? device : NULL,
28553b1dbd11SBenoît Canet                         has_node_name ? node_name : NULL,
28563b1dbd11SBenoît Canet                         &local_err);
285784d18f06SMarkus Armbruster     if (local_err) {
28583b1dbd11SBenoît Canet         error_propagate(errp, local_err);
28593b1dbd11SBenoît Canet         return;
28603b1dbd11SBenoît Canet     }
28613b1dbd11SBenoît Canet 
2862927e0e76SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2863927e0e76SStefan Hajnoczi     aio_context_acquire(aio_context);
2864927e0e76SStefan Hajnoczi 
28653b1dbd11SBenoît Canet     if (!bdrv_is_first_non_filter(bs)) {
2866c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2867927e0e76SStefan Hajnoczi         goto out;
28686d4a2b3aSChristoph Hellwig     }
28696d4a2b3aSChristoph Hellwig 
28706d4a2b3aSChristoph Hellwig     if (size < 0) {
2871c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2872927e0e76SStefan Hajnoczi         goto out;
28736d4a2b3aSChristoph Hellwig     }
28746d4a2b3aSChristoph Hellwig 
28759c75e168SJeff Cody     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2876c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_IN_USE, device);
2877927e0e76SStefan Hajnoczi         goto out;
28789c75e168SJeff Cody     }
28799c75e168SJeff Cody 
288092b7a08dSPeter Lieven     /* complete all in-flight operations before resizing the device */
288192b7a08dSPeter Lieven     bdrv_drain_all();
288292b7a08dSPeter Lieven 
28838732901eSKevin Wolf     ret = bdrv_truncate(bs, size);
28848732901eSKevin Wolf     switch (ret) {
2885939a1cc3SStefan Hajnoczi     case 0:
2886939a1cc3SStefan Hajnoczi         break;
2887939a1cc3SStefan Hajnoczi     case -ENOMEDIUM:
2888c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2889939a1cc3SStefan Hajnoczi         break;
2890939a1cc3SStefan Hajnoczi     case -ENOTSUP:
2891c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_UNSUPPORTED);
2892939a1cc3SStefan Hajnoczi         break;
2893939a1cc3SStefan Hajnoczi     case -EACCES:
289481e5f78aSAlberto Garcia         error_setg(errp, "Device '%s' is read only", device);
2895939a1cc3SStefan Hajnoczi         break;
2896939a1cc3SStefan Hajnoczi     case -EBUSY:
2897c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_IN_USE, device);
2898939a1cc3SStefan Hajnoczi         break;
2899939a1cc3SStefan Hajnoczi     default:
29008732901eSKevin Wolf         error_setg_errno(errp, -ret, "Could not resize");
2901939a1cc3SStefan Hajnoczi         break;
29026d4a2b3aSChristoph Hellwig     }
2903927e0e76SStefan Hajnoczi 
2904927e0e76SStefan Hajnoczi out:
2905927e0e76SStefan Hajnoczi     aio_context_release(aio_context);
29066d4a2b3aSChristoph Hellwig }
290712bd451fSStefan Hajnoczi 
29089abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret)
290912bd451fSStefan Hajnoczi {
2910723c5d93SStefan Hajnoczi     /* Note that this function may be executed from another AioContext besides
2911723c5d93SStefan Hajnoczi      * the QEMU main loop.  If you need to access anything that assumes the
2912723c5d93SStefan Hajnoczi      * QEMU global mutex, use a BH or introduce a mutex.
2913723c5d93SStefan Hajnoczi      */
2914723c5d93SStefan Hajnoczi 
291512bd451fSStefan Hajnoczi     BlockDriverState *bs = opaque;
2916bcada37bSWenchao Xia     const char *msg = NULL;
291712bd451fSStefan Hajnoczi 
29189abf2dbaSJeff Cody     trace_block_job_cb(bs, bs->job, ret);
291912bd451fSStefan Hajnoczi 
292012bd451fSStefan Hajnoczi     assert(bs->job);
2921bcada37bSWenchao Xia 
292212bd451fSStefan Hajnoczi     if (ret < 0) {
2923bcada37bSWenchao Xia         msg = strerror(-ret);
292412bd451fSStefan Hajnoczi     }
292512bd451fSStefan Hajnoczi 
2926370521a1SStefan Hajnoczi     if (block_job_is_cancelled(bs->job)) {
2927bcada37bSWenchao Xia         block_job_event_cancelled(bs->job);
2928370521a1SStefan Hajnoczi     } else {
2929bcada37bSWenchao Xia         block_job_event_completed(bs->job, msg);
2930370521a1SStefan Hajnoczi     }
293112bd451fSStefan Hajnoczi }
293212bd451fSStefan Hajnoczi 
29332323322eSAlberto Garcia void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
293413d8cc51SJeff Cody                       bool has_base, const char *base,
293513d8cc51SJeff Cody                       bool has_backing_file, const char *backing_file,
293613d8cc51SJeff Cody                       bool has_speed, int64_t speed,
29371d809098SPaolo Bonzini                       bool has_on_error, BlockdevOnError on_error,
29381d809098SPaolo Bonzini                       Error **errp)
293912bd451fSStefan Hajnoczi {
294012bd451fSStefan Hajnoczi     BlockDriverState *bs;
2941c8c3080fSMarcelo Tosatti     BlockDriverState *base_bs = NULL;
2942f3e69bebSStefan Hajnoczi     AioContext *aio_context;
2943fd7f8c65SStefan Hajnoczi     Error *local_err = NULL;
294413d8cc51SJeff Cody     const char *base_name = NULL;
294512bd451fSStefan Hajnoczi 
29461d809098SPaolo Bonzini     if (!has_on_error) {
29471d809098SPaolo Bonzini         on_error = BLOCKDEV_ON_ERROR_REPORT;
29481d809098SPaolo Bonzini     }
29491d809098SPaolo Bonzini 
2950b6c1bae5SKevin Wolf     bs = qmp_get_root_bs(device, errp);
2951b6c1bae5SKevin Wolf     if (!bs) {
295212bd451fSStefan Hajnoczi         return;
295312bd451fSStefan Hajnoczi     }
295412bd451fSStefan Hajnoczi 
2955b6c1bae5SKevin Wolf     aio_context = bdrv_get_aio_context(bs);
2956f3e69bebSStefan Hajnoczi     aio_context_acquire(aio_context);
2957f3e69bebSStefan Hajnoczi 
2958628ff683SFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2959f3e69bebSStefan Hajnoczi         goto out;
2960628ff683SFam Zheng     }
2961628ff683SFam Zheng 
296213d8cc51SJeff Cody     if (has_base) {
2963c8c3080fSMarcelo Tosatti         base_bs = bdrv_find_backing_image(bs, base);
2964c8c3080fSMarcelo Tosatti         if (base_bs == NULL) {
2965c6bd8c70SMarkus Armbruster             error_setg(errp, QERR_BASE_NOT_FOUND, base);
2966f3e69bebSStefan Hajnoczi             goto out;
296712bd451fSStefan Hajnoczi         }
2968f3e69bebSStefan Hajnoczi         assert(bdrv_get_aio_context(base_bs) == aio_context);
296913d8cc51SJeff Cody         base_name = base;
2970c8c3080fSMarcelo Tosatti     }
297112bd451fSStefan Hajnoczi 
297213d8cc51SJeff Cody     /* if we are streaming the entire chain, the result will have no backing
297313d8cc51SJeff Cody      * file, and specifying one is therefore an error */
297413d8cc51SJeff Cody     if (base_bs == NULL && has_backing_file) {
297513d8cc51SJeff Cody         error_setg(errp, "backing file specified, but streaming the "
297613d8cc51SJeff Cody                          "entire chain");
2977f3e69bebSStefan Hajnoczi         goto out;
297813d8cc51SJeff Cody     }
297913d8cc51SJeff Cody 
298013d8cc51SJeff Cody     /* backing_file string overrides base bs filename */
298113d8cc51SJeff Cody     base_name = has_backing_file ? backing_file : base_name;
298213d8cc51SJeff Cody 
29832323322eSAlberto Garcia     stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
29842323322eSAlberto Garcia                  has_speed ? speed : 0, on_error, block_job_cb, bs, &local_err);
298584d18f06SMarkus Armbruster     if (local_err) {
2986fd7f8c65SStefan Hajnoczi         error_propagate(errp, local_err);
2987f3e69bebSStefan Hajnoczi         goto out;
298812bd451fSStefan Hajnoczi     }
298912bd451fSStefan Hajnoczi 
299012bd451fSStefan Hajnoczi     trace_qmp_block_stream(bs, bs->job);
2991f3e69bebSStefan Hajnoczi 
2992f3e69bebSStefan Hajnoczi out:
2993f3e69bebSStefan Hajnoczi     aio_context_release(aio_context);
299412bd451fSStefan Hajnoczi }
29952d47c6e9SStefan Hajnoczi 
2996fd62c609SAlberto Garcia void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
29977676e2c5SJeff Cody                       bool has_base, const char *base,
29987676e2c5SJeff Cody                       bool has_top, const char *top,
299954e26900SJeff Cody                       bool has_backing_file, const char *backing_file,
3000ed61fc10SJeff Cody                       bool has_speed, int64_t speed,
3001ed61fc10SJeff Cody                       Error **errp)
3002ed61fc10SJeff Cody {
3003ed61fc10SJeff Cody     BlockDriverState *bs;
3004ed61fc10SJeff Cody     BlockDriverState *base_bs, *top_bs;
30059e85cd5cSStefan Hajnoczi     AioContext *aio_context;
3006ed61fc10SJeff Cody     Error *local_err = NULL;
3007ed61fc10SJeff Cody     /* This will be part of the QMP command, if/when the
3008ed61fc10SJeff Cody      * BlockdevOnError change for blkmirror makes it in
3009ed61fc10SJeff Cody      */
301092aa5c6dSPaolo Bonzini     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
3011ed61fc10SJeff Cody 
301254504663SMax Reitz     if (!has_speed) {
301354504663SMax Reitz         speed = 0;
301454504663SMax Reitz     }
301554504663SMax Reitz 
30167676e2c5SJeff Cody     /* Important Note:
30177676e2c5SJeff Cody      *  libvirt relies on the DeviceNotFound error class in order to probe for
30187676e2c5SJeff Cody      *  live commit feature versions; for this to work, we must make sure to
30197676e2c5SJeff Cody      *  perform the device lookup before any generic errors that may occur in a
30207676e2c5SJeff Cody      *  scenario in which all optional arguments are omitted. */
30211d13b167SKevin Wolf     bs = qmp_get_root_bs(device, &local_err);
30221d13b167SKevin Wolf     if (!bs) {
30231d13b167SKevin Wolf         bs = bdrv_lookup_bs(device, device, NULL);
30241d13b167SKevin Wolf         if (!bs) {
30251d13b167SKevin Wolf             error_free(local_err);
302675158ebbSMarkus Armbruster             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
302775158ebbSMarkus Armbruster                       "Device '%s' not found", device);
30281d13b167SKevin Wolf         } else {
30291d13b167SKevin Wolf             error_propagate(errp, local_err);
30301d13b167SKevin Wolf         }
3031ed61fc10SJeff Cody         return;
3032ed61fc10SJeff Cody     }
3033ed61fc10SJeff Cody 
30341d13b167SKevin Wolf     aio_context = bdrv_get_aio_context(bs);
30359e85cd5cSStefan Hajnoczi     aio_context_acquire(aio_context);
30369e85cd5cSStefan Hajnoczi 
3037bb00021dSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
30389e85cd5cSStefan Hajnoczi         goto out;
3039628ff683SFam Zheng     }
3040628ff683SFam Zheng 
3041ed61fc10SJeff Cody     /* default top_bs is the active layer */
3042ed61fc10SJeff Cody     top_bs = bs;
3043ed61fc10SJeff Cody 
30447676e2c5SJeff Cody     if (has_top && top) {
3045ed61fc10SJeff Cody         if (strcmp(bs->filename, top) != 0) {
3046ed61fc10SJeff Cody             top_bs = bdrv_find_backing_image(bs, top);
3047ed61fc10SJeff Cody         }
3048ed61fc10SJeff Cody     }
3049ed61fc10SJeff Cody 
3050ed61fc10SJeff Cody     if (top_bs == NULL) {
3051ed61fc10SJeff Cody         error_setg(errp, "Top image file %s not found", top ? top : "NULL");
30529e85cd5cSStefan Hajnoczi         goto out;
3053ed61fc10SJeff Cody     }
3054ed61fc10SJeff Cody 
30559e85cd5cSStefan Hajnoczi     assert(bdrv_get_aio_context(top_bs) == aio_context);
30569e85cd5cSStefan Hajnoczi 
3057d5208c45SJeff Cody     if (has_base && base) {
3058d5208c45SJeff Cody         base_bs = bdrv_find_backing_image(top_bs, base);
3059d5208c45SJeff Cody     } else {
3060d5208c45SJeff Cody         base_bs = bdrv_find_base(top_bs);
3061d5208c45SJeff Cody     }
3062d5208c45SJeff Cody 
3063d5208c45SJeff Cody     if (base_bs == NULL) {
3064c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
30659e85cd5cSStefan Hajnoczi         goto out;
3066d5208c45SJeff Cody     }
3067d5208c45SJeff Cody 
30689e85cd5cSStefan Hajnoczi     assert(bdrv_get_aio_context(base_bs) == aio_context);
30699e85cd5cSStefan Hajnoczi 
3070bb00021dSFam Zheng     if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3071bb00021dSFam Zheng         goto out;
3072bb00021dSFam Zheng     }
3073bb00021dSFam Zheng 
30747676e2c5SJeff Cody     /* Do not allow attempts to commit an image into itself */
30757676e2c5SJeff Cody     if (top_bs == base_bs) {
30767676e2c5SJeff Cody         error_setg(errp, "cannot commit an image into itself");
30779e85cd5cSStefan Hajnoczi         goto out;
30787676e2c5SJeff Cody     }
30797676e2c5SJeff Cody 
308020a63d2cSFam Zheng     if (top_bs == bs) {
308154e26900SJeff Cody         if (has_backing_file) {
308254e26900SJeff Cody             error_setg(errp, "'backing-file' specified,"
308354e26900SJeff Cody                              " but 'top' is the active layer");
30849e85cd5cSStefan Hajnoczi             goto out;
308554e26900SJeff Cody         }
3086fd62c609SAlberto Garcia         commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, speed,
3087b49f7eadSWen Congyang                             on_error, block_job_cb, bs, &local_err, false);
308820a63d2cSFam Zheng     } else {
3089fd62c609SAlberto Garcia         commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed,
3090fd62c609SAlberto Garcia                      on_error, block_job_cb, bs,
309154e26900SJeff Cody                      has_backing_file ? backing_file : NULL, &local_err);
309220a63d2cSFam Zheng     }
3093ed61fc10SJeff Cody     if (local_err != NULL) {
3094ed61fc10SJeff Cody         error_propagate(errp, local_err);
30959e85cd5cSStefan Hajnoczi         goto out;
3096ed61fc10SJeff Cody     }
30979e85cd5cSStefan Hajnoczi 
30989e85cd5cSStefan Hajnoczi out:
30999e85cd5cSStefan Hajnoczi     aio_context_release(aio_context);
3100ed61fc10SJeff Cody }
3101ed61fc10SJeff Cody 
310281206a89SPavel Butsykin static void do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, Error **errp)
310399a9addfSStefan Hajnoczi {
310499a9addfSStefan Hajnoczi     BlockDriverState *bs;
310599a9addfSStefan Hajnoczi     BlockDriverState *target_bs;
3106fc5d3f84SIan Main     BlockDriverState *source = NULL;
3107d58d8453SJohn Snow     BdrvDirtyBitmap *bmap = NULL;
3108761731b1SStefan Hajnoczi     AioContext *aio_context;
3109e6641719SMax Reitz     QDict *options = NULL;
311099a9addfSStefan Hajnoczi     Error *local_err = NULL;
311199a9addfSStefan Hajnoczi     int flags;
311299a9addfSStefan Hajnoczi     int64_t size;
311399a9addfSStefan Hajnoczi 
311481206a89SPavel Butsykin     if (!backup->has_speed) {
311581206a89SPavel Butsykin         backup->speed = 0;
311699a9addfSStefan Hajnoczi     }
311781206a89SPavel Butsykin     if (!backup->has_on_source_error) {
311881206a89SPavel Butsykin         backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
311999a9addfSStefan Hajnoczi     }
312081206a89SPavel Butsykin     if (!backup->has_on_target_error) {
312181206a89SPavel Butsykin         backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
312299a9addfSStefan Hajnoczi     }
312381206a89SPavel Butsykin     if (!backup->has_mode) {
312481206a89SPavel Butsykin         backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
312581206a89SPavel Butsykin     }
312681206a89SPavel Butsykin     if (!backup->has_job_id) {
312781206a89SPavel Butsykin         backup->job_id = NULL;
312899a9addfSStefan Hajnoczi     }
312913b9414bSPavel Butsykin     if (!backup->has_compress) {
313013b9414bSPavel Butsykin         backup->compress = false;
313113b9414bSPavel Butsykin     }
313299a9addfSStefan Hajnoczi 
313381206a89SPavel Butsykin     bs = qmp_get_root_bs(backup->device, errp);
3134b7e4fa22SKevin Wolf     if (!bs) {
313599a9addfSStefan Hajnoczi         return;
313699a9addfSStefan Hajnoczi     }
313799a9addfSStefan Hajnoczi 
3138b7e4fa22SKevin Wolf     aio_context = bdrv_get_aio_context(bs);
3139761731b1SStefan Hajnoczi     aio_context_acquire(aio_context);
3140761731b1SStefan Hajnoczi 
314181206a89SPavel Butsykin     if (!backup->has_format) {
314281206a89SPavel Butsykin         backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
314381206a89SPavel Butsykin                          NULL : (char*) bs->drv->format_name;
314499a9addfSStefan Hajnoczi     }
314599a9addfSStefan Hajnoczi 
3146c29c1dd3SFam Zheng     /* Early check to avoid creating target */
31473718d8abSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
3148761731b1SStefan Hajnoczi         goto out;
314999a9addfSStefan Hajnoczi     }
315099a9addfSStefan Hajnoczi 
315161de4c68SKevin Wolf     flags = bs->open_flags | BDRV_O_RDWR;
315299a9addfSStefan Hajnoczi 
3153fc5d3f84SIan Main     /* See if we have a backing HD we can use to create our new image
3154fc5d3f84SIan Main      * on top of. */
315581206a89SPavel Butsykin     if (backup->sync == MIRROR_SYNC_MODE_TOP) {
3156760e0063SKevin Wolf         source = backing_bs(bs);
3157fc5d3f84SIan Main         if (!source) {
315881206a89SPavel Butsykin             backup->sync = MIRROR_SYNC_MODE_FULL;
3159fc5d3f84SIan Main         }
3160fc5d3f84SIan Main     }
316181206a89SPavel Butsykin     if (backup->sync == MIRROR_SYNC_MODE_NONE) {
3162fc5d3f84SIan Main         source = bs;
3163fc5d3f84SIan Main     }
3164fc5d3f84SIan Main 
316599a9addfSStefan Hajnoczi     size = bdrv_getlength(bs);
316699a9addfSStefan Hajnoczi     if (size < 0) {
316799a9addfSStefan Hajnoczi         error_setg_errno(errp, -size, "bdrv_getlength failed");
3168761731b1SStefan Hajnoczi         goto out;
316999a9addfSStefan Hajnoczi     }
317099a9addfSStefan Hajnoczi 
317181206a89SPavel Butsykin     if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
317281206a89SPavel Butsykin         assert(backup->format);
3173fc5d3f84SIan Main         if (source) {
317481206a89SPavel Butsykin             bdrv_img_create(backup->target, backup->format, source->filename,
3175fc5d3f84SIan Main                             source->drv->format_name, NULL,
3176fc5d3f84SIan Main                             size, flags, &local_err, false);
3177fc5d3f84SIan Main         } else {
317881206a89SPavel Butsykin             bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
3179fc5d3f84SIan Main                             size, flags, &local_err, false);
3180fc5d3f84SIan Main         }
318199a9addfSStefan Hajnoczi     }
318299a9addfSStefan Hajnoczi 
318384d18f06SMarkus Armbruster     if (local_err) {
318499a9addfSStefan Hajnoczi         error_propagate(errp, local_err);
3185761731b1SStefan Hajnoczi         goto out;
318699a9addfSStefan Hajnoczi     }
318799a9addfSStefan Hajnoczi 
318881206a89SPavel Butsykin     if (backup->format) {
3189e6641719SMax Reitz         options = qdict_new();
319081206a89SPavel Butsykin         qdict_put(options, "driver", qstring_from_str(backup->format));
3191e6641719SMax Reitz     }
3192e6641719SMax Reitz 
319381206a89SPavel Butsykin     target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
31945b363937SMax Reitz     if (!target_bs) {
3195761731b1SStefan Hajnoczi         goto out;
319699a9addfSStefan Hajnoczi     }
319799a9addfSStefan Hajnoczi 
3198761731b1SStefan Hajnoczi     bdrv_set_aio_context(target_bs, aio_context);
3199761731b1SStefan Hajnoczi 
320081206a89SPavel Butsykin     if (backup->has_bitmap) {
320181206a89SPavel Butsykin         bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
3202d58d8453SJohn Snow         if (!bmap) {
320381206a89SPavel Butsykin             error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
32040702d3d8SMax Reitz             bdrv_unref(target_bs);
3205d58d8453SJohn Snow             goto out;
3206d58d8453SJohn Snow         }
3207d58d8453SJohn Snow     }
3208d58d8453SJohn Snow 
320981206a89SPavel Butsykin     backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync,
321013b9414bSPavel Butsykin                  bmap, backup->compress, backup->on_source_error,
321113b9414bSPavel Butsykin                  backup->on_target_error, block_job_cb, bs, txn, &local_err);
32124f6fd349SFam Zheng     bdrv_unref(target_bs);
32135c438bc6SKevin Wolf     if (local_err != NULL) {
321499a9addfSStefan Hajnoczi         error_propagate(errp, local_err);
3215761731b1SStefan Hajnoczi         goto out;
321699a9addfSStefan Hajnoczi     }
3217761731b1SStefan Hajnoczi 
3218761731b1SStefan Hajnoczi out:
3219761731b1SStefan Hajnoczi     aio_context_release(aio_context);
322099a9addfSStefan Hajnoczi }
322199a9addfSStefan Hajnoczi 
322281206a89SPavel Butsykin void qmp_drive_backup(DriveBackup *arg, Error **errp)
322378f51fdeSJohn Snow {
322481206a89SPavel Butsykin     return do_drive_backup(arg, NULL, errp);
322578f51fdeSJohn Snow }
322678f51fdeSJohn Snow 
3227c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3228c13163fbSBenoît Canet {
3229d5a8ee60SAlberto Garcia     return bdrv_named_nodes_list(errp);
3230c13163fbSBenoît Canet }
3231c13163fbSBenoît Canet 
3232dc7a4a9eSPavel Butsykin void do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, Error **errp)
3233c29c1dd3SFam Zheng {
3234c29c1dd3SFam Zheng     BlockDriverState *bs;
3235c29c1dd3SFam Zheng     BlockDriverState *target_bs;
3236c29c1dd3SFam Zheng     Error *local_err = NULL;
3237c29c1dd3SFam Zheng     AioContext *aio_context;
3238c29c1dd3SFam Zheng 
3239dc7a4a9eSPavel Butsykin     if (!backup->has_speed) {
3240dc7a4a9eSPavel Butsykin         backup->speed = 0;
3241c29c1dd3SFam Zheng     }
3242dc7a4a9eSPavel Butsykin     if (!backup->has_on_source_error) {
3243dc7a4a9eSPavel Butsykin         backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3244c29c1dd3SFam Zheng     }
3245dc7a4a9eSPavel Butsykin     if (!backup->has_on_target_error) {
3246dc7a4a9eSPavel Butsykin         backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3247dc7a4a9eSPavel Butsykin     }
3248dc7a4a9eSPavel Butsykin     if (!backup->has_job_id) {
3249dc7a4a9eSPavel Butsykin         backup->job_id = NULL;
3250c29c1dd3SFam Zheng     }
32513b7b1236SPavel Butsykin     if (!backup->has_compress) {
32523b7b1236SPavel Butsykin         backup->compress = false;
32533b7b1236SPavel Butsykin     }
3254c29c1dd3SFam Zheng 
3255dc7a4a9eSPavel Butsykin     bs = qmp_get_root_bs(backup->device, errp);
3256cef34eebSKevin Wolf     if (!bs) {
3257c29c1dd3SFam Zheng         return;
3258c29c1dd3SFam Zheng     }
3259c29c1dd3SFam Zheng 
3260cef34eebSKevin Wolf     aio_context = bdrv_get_aio_context(bs);
3261c29c1dd3SFam Zheng     aio_context_acquire(aio_context);
3262c29c1dd3SFam Zheng 
3263dc7a4a9eSPavel Butsykin     target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
32640d978913SFam Zheng     if (!target_bs) {
3265c29c1dd3SFam Zheng         goto out;
3266c29c1dd3SFam Zheng     }
32675433c24fSMax Reitz 
3268efd75567SFam Zheng     if (bdrv_get_aio_context(target_bs) != aio_context) {
3269efd75567SFam Zheng         if (!bdrv_has_blk(target_bs)) {
3270efd75567SFam Zheng             /* The target BDS is not attached, we can safely move it to another
3271efd75567SFam Zheng              * AioContext. */
3272c29c1dd3SFam Zheng             bdrv_set_aio_context(target_bs, aio_context);
3273efd75567SFam Zheng         } else {
3274efd75567SFam Zheng             error_setg(errp, "Target is attached to a different thread from "
3275efd75567SFam Zheng                              "source.");
3276efd75567SFam Zheng             goto out;
3277efd75567SFam Zheng         }
3278efd75567SFam Zheng     }
3279dc7a4a9eSPavel Butsykin     backup_start(backup->job_id, bs, target_bs, backup->speed, backup->sync,
32803b7b1236SPavel Butsykin                  NULL, backup->compress, backup->on_source_error,
32813b7b1236SPavel Butsykin                  backup->on_target_error, block_job_cb, bs, txn, &local_err);
3282c29c1dd3SFam Zheng     if (local_err != NULL) {
3283c29c1dd3SFam Zheng         error_propagate(errp, local_err);
3284c29c1dd3SFam Zheng     }
3285c29c1dd3SFam Zheng out:
3286c29c1dd3SFam Zheng     aio_context_release(aio_context);
3287c29c1dd3SFam Zheng }
3288c29c1dd3SFam Zheng 
3289dc7a4a9eSPavel Butsykin void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp)
329078f51fdeSJohn Snow {
3291dc7a4a9eSPavel Butsykin     do_blockdev_backup(arg, NULL, errp);
329278f51fdeSJohn Snow }
329378f51fdeSJohn Snow 
32944193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring.
32954193cdd7SFam Zheng  * Caller should hold @device and @target's aio context (must be the same).
32964193cdd7SFam Zheng  **/
329771aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
32984193cdd7SFam Zheng                                    BlockDriverState *target,
329909158f00SBenoît Canet                                    bool has_replaces, const char *replaces,
3300d9b902dbSPaolo Bonzini                                    enum MirrorSyncMode sync,
3301274fcceeSMax Reitz                                    BlockMirrorBackingMode backing_mode,
3302b952b558SPaolo Bonzini                                    bool has_speed, int64_t speed,
3303eee13dfeSPaolo Bonzini                                    bool has_granularity, uint32_t granularity,
330408e4ed6cSPaolo Bonzini                                    bool has_buf_size, int64_t buf_size,
33054193cdd7SFam Zheng                                    bool has_on_source_error,
33064193cdd7SFam Zheng                                    BlockdevOnError on_source_error,
33074193cdd7SFam Zheng                                    bool has_on_target_error,
33084193cdd7SFam Zheng                                    BlockdevOnError on_target_error,
33090fc9f8eaSFam Zheng                                    bool has_unmap, bool unmap,
3310b952b558SPaolo Bonzini                                    Error **errp)
3311d9b902dbSPaolo Bonzini {
3312d9b902dbSPaolo Bonzini 
3313d9b902dbSPaolo Bonzini     if (!has_speed) {
3314d9b902dbSPaolo Bonzini         speed = 0;
3315d9b902dbSPaolo Bonzini     }
3316b952b558SPaolo Bonzini     if (!has_on_source_error) {
3317b952b558SPaolo Bonzini         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3318b952b558SPaolo Bonzini     }
3319b952b558SPaolo Bonzini     if (!has_on_target_error) {
3320b952b558SPaolo Bonzini         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3321b952b558SPaolo Bonzini     }
3322eee13dfeSPaolo Bonzini     if (!has_granularity) {
3323eee13dfeSPaolo Bonzini         granularity = 0;
3324eee13dfeSPaolo Bonzini     }
332508e4ed6cSPaolo Bonzini     if (!has_buf_size) {
332648ac0a4dSWen Congyang         buf_size = 0;
332708e4ed6cSPaolo Bonzini     }
33280fc9f8eaSFam Zheng     if (!has_unmap) {
33290fc9f8eaSFam Zheng         unmap = true;
33300fc9f8eaSFam Zheng     }
333108e4ed6cSPaolo Bonzini 
3332eee13dfeSPaolo Bonzini     if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3333c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
33343cbbe9fdSStefan Hajnoczi                    "a value in range [512B, 64MB]");
3335eee13dfeSPaolo Bonzini         return;
3336eee13dfeSPaolo Bonzini     }
3337eee13dfeSPaolo Bonzini     if (granularity & (granularity - 1)) {
3338c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3339c6bd8c70SMarkus Armbruster                    "power of 2");
3340eee13dfeSPaolo Bonzini         return;
3341eee13dfeSPaolo Bonzini     }
3342d9b902dbSPaolo Bonzini 
33434193cdd7SFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
33444193cdd7SFam Zheng         return;
33454193cdd7SFam Zheng     }
3346e40e5027SFam Zheng     if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3347e40e5027SFam Zheng         return;
3348e40e5027SFam Zheng     }
33494193cdd7SFam Zheng 
33504193cdd7SFam Zheng     if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
33514193cdd7SFam Zheng         sync = MIRROR_SYNC_MODE_FULL;
33524193cdd7SFam Zheng     }
33534193cdd7SFam Zheng 
33544193cdd7SFam Zheng     /* pass the node name to replace to mirror start since it's loose coupling
33554193cdd7SFam Zheng      * and will allow to check whether the node still exist at mirror completion
33564193cdd7SFam Zheng      */
335771aa9867SAlberto Garcia     mirror_start(job_id, bs, target,
33584193cdd7SFam Zheng                  has_replaces ? replaces : NULL,
3359274fcceeSMax Reitz                  speed, granularity, buf_size, sync, backing_mode,
33604193cdd7SFam Zheng                  on_source_error, on_target_error, unmap,
33614193cdd7SFam Zheng                  block_job_cb, bs, errp);
33624193cdd7SFam Zheng }
33634193cdd7SFam Zheng 
3364faecd40aSEric Blake void qmp_drive_mirror(DriveMirror *arg, Error **errp)
33654193cdd7SFam Zheng {
33664193cdd7SFam Zheng     BlockDriverState *bs;
33674193cdd7SFam Zheng     BlockDriverState *source, *target_bs;
33684193cdd7SFam Zheng     AioContext *aio_context;
3369274fcceeSMax Reitz     BlockMirrorBackingMode backing_mode;
33704193cdd7SFam Zheng     Error *local_err = NULL;
33714193cdd7SFam Zheng     QDict *options = NULL;
33724193cdd7SFam Zheng     int flags;
33734193cdd7SFam Zheng     int64_t size;
3374faecd40aSEric Blake     const char *format = arg->format;
33754193cdd7SFam Zheng 
33760524e93aSKevin Wolf     bs = qmp_get_root_bs(arg->device, errp);
33770524e93aSKevin Wolf     if (!bs) {
3378d9b902dbSPaolo Bonzini         return;
3379d9b902dbSPaolo Bonzini     }
3380d9b902dbSPaolo Bonzini 
33810524e93aSKevin Wolf     aio_context = bdrv_get_aio_context(bs);
33825a7e7a0bSStefan Hajnoczi     aio_context_acquire(aio_context);
33835a7e7a0bSStefan Hajnoczi 
3384faecd40aSEric Blake     if (!arg->has_mode) {
3385faecd40aSEric Blake         arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
33864193cdd7SFam Zheng     }
3387d9b902dbSPaolo Bonzini 
3388faecd40aSEric Blake     if (!arg->has_format) {
3389faecd40aSEric Blake         format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3390faecd40aSEric Blake                   ? NULL : bs->drv->format_name);
3391d9b902dbSPaolo Bonzini     }
3392d9b902dbSPaolo Bonzini 
339361de4c68SKevin Wolf     flags = bs->open_flags | BDRV_O_RDWR;
3394760e0063SKevin Wolf     source = backing_bs(bs);
3395faecd40aSEric Blake     if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3396faecd40aSEric Blake         arg->sync = MIRROR_SYNC_MODE_FULL;
3397d9b902dbSPaolo Bonzini     }
3398faecd40aSEric Blake     if (arg->sync == MIRROR_SYNC_MODE_NONE) {
3399117e0c82SMax Reitz         source = bs;
3400117e0c82SMax Reitz     }
3401d9b902dbSPaolo Bonzini 
3402ac3c5d83SStefan Hajnoczi     size = bdrv_getlength(bs);
3403ac3c5d83SStefan Hajnoczi     if (size < 0) {
3404ac3c5d83SStefan Hajnoczi         error_setg_errno(errp, -size, "bdrv_getlength failed");
34055a7e7a0bSStefan Hajnoczi         goto out;
3406ac3c5d83SStefan Hajnoczi     }
3407ac3c5d83SStefan Hajnoczi 
3408faecd40aSEric Blake     if (arg->has_replaces) {
340909158f00SBenoît Canet         BlockDriverState *to_replace_bs;
34105a7e7a0bSStefan Hajnoczi         AioContext *replace_aio_context;
34115a7e7a0bSStefan Hajnoczi         int64_t replace_size;
341209158f00SBenoît Canet 
3413faecd40aSEric Blake         if (!arg->has_node_name) {
341409158f00SBenoît Canet             error_setg(errp, "a node-name must be provided when replacing a"
341509158f00SBenoît Canet                              " named node of the graph");
34165a7e7a0bSStefan Hajnoczi             goto out;
341709158f00SBenoît Canet         }
341809158f00SBenoît Canet 
3419faecd40aSEric Blake         to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err);
342009158f00SBenoît Canet 
342109158f00SBenoît Canet         if (!to_replace_bs) {
342209158f00SBenoît Canet             error_propagate(errp, local_err);
34235a7e7a0bSStefan Hajnoczi             goto out;
342409158f00SBenoît Canet         }
342509158f00SBenoît Canet 
34265a7e7a0bSStefan Hajnoczi         replace_aio_context = bdrv_get_aio_context(to_replace_bs);
34275a7e7a0bSStefan Hajnoczi         aio_context_acquire(replace_aio_context);
34285a7e7a0bSStefan Hajnoczi         replace_size = bdrv_getlength(to_replace_bs);
34295a7e7a0bSStefan Hajnoczi         aio_context_release(replace_aio_context);
34305a7e7a0bSStefan Hajnoczi 
34315a7e7a0bSStefan Hajnoczi         if (size != replace_size) {
343209158f00SBenoît Canet             error_setg(errp, "cannot replace image with a mirror image of "
343309158f00SBenoît Canet                              "different size");
34345a7e7a0bSStefan Hajnoczi             goto out;
343509158f00SBenoît Canet         }
343609158f00SBenoît Canet     }
343709158f00SBenoît Canet 
3438faecd40aSEric Blake     if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
3439274fcceeSMax Reitz         backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3440274fcceeSMax Reitz     } else {
3441274fcceeSMax Reitz         backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3442274fcceeSMax Reitz     }
3443274fcceeSMax Reitz 
3444faecd40aSEric Blake     if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3445faecd40aSEric Blake         && arg->mode != NEW_IMAGE_MODE_EXISTING)
344614526864SMax Reitz     {
3447d9b902dbSPaolo Bonzini         /* create new image w/o backing file */
3448e6641719SMax Reitz         assert(format);
3449faecd40aSEric Blake         bdrv_img_create(arg->target, format,
3450f382d43aSMiroslav Rezanina                         NULL, NULL, NULL, size, flags, &local_err, false);
3451d9b902dbSPaolo Bonzini     } else {
3452faecd40aSEric Blake         switch (arg->mode) {
3453d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_EXISTING:
3454d9b902dbSPaolo Bonzini             break;
3455d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3456d9b902dbSPaolo Bonzini             /* create new image with backing file */
3457faecd40aSEric Blake             bdrv_img_create(arg->target, format,
3458d9b902dbSPaolo Bonzini                             source->filename,
3459d9b902dbSPaolo Bonzini                             source->drv->format_name,
3460f382d43aSMiroslav Rezanina                             NULL, size, flags, &local_err, false);
3461d9b902dbSPaolo Bonzini             break;
3462d9b902dbSPaolo Bonzini         default:
3463d9b902dbSPaolo Bonzini             abort();
3464d9b902dbSPaolo Bonzini         }
3465d9b902dbSPaolo Bonzini     }
3466d9b902dbSPaolo Bonzini 
346784d18f06SMarkus Armbruster     if (local_err) {
3468cf8f2426SLuiz Capitulino         error_propagate(errp, local_err);
34695a7e7a0bSStefan Hajnoczi         goto out;
3470d9b902dbSPaolo Bonzini     }
3471d9b902dbSPaolo Bonzini 
34724c828dc6SBenoît Canet     options = qdict_new();
3473faecd40aSEric Blake     if (arg->has_node_name) {
3474faecd40aSEric Blake         qdict_put(options, "node-name", qstring_from_str(arg->node_name));
34754c828dc6SBenoît Canet     }
3476e6641719SMax Reitz     if (format) {
3477e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
3478e6641719SMax Reitz     }
34794c828dc6SBenoît Canet 
3480b812f671SPaolo Bonzini     /* Mirroring takes care of copy-on-write using the source's backing
3481b812f671SPaolo Bonzini      * file.
3482b812f671SPaolo Bonzini      */
3483faecd40aSEric Blake     target_bs = bdrv_open(arg->target, NULL, options,
3484faecd40aSEric Blake                           flags | BDRV_O_NO_BACKING, errp);
34855b363937SMax Reitz     if (!target_bs) {
34865a7e7a0bSStefan Hajnoczi         goto out;
3487d9b902dbSPaolo Bonzini     }
3488d9b902dbSPaolo Bonzini 
34895a7e7a0bSStefan Hajnoczi     bdrv_set_aio_context(target_bs, aio_context);
34905a7e7a0bSStefan Hajnoczi 
3491faecd40aSEric Blake     blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3492faecd40aSEric Blake                            arg->has_replaces, arg->replaces, arg->sync,
3493faecd40aSEric Blake                            backing_mode, arg->has_speed, arg->speed,
3494faecd40aSEric Blake                            arg->has_granularity, arg->granularity,
3495faecd40aSEric Blake                            arg->has_buf_size, arg->buf_size,
3496faecd40aSEric Blake                            arg->has_on_source_error, arg->on_source_error,
3497faecd40aSEric Blake                            arg->has_on_target_error, arg->on_target_error,
3498faecd40aSEric Blake                            arg->has_unmap, arg->unmap,
34994193cdd7SFam Zheng                            &local_err);
3500e253f4b8SKevin Wolf     bdrv_unref(target_bs);
3501d9b902dbSPaolo Bonzini     error_propagate(errp, local_err);
35025a7e7a0bSStefan Hajnoczi out:
35035a7e7a0bSStefan Hajnoczi     aio_context_release(aio_context);
3504d9b902dbSPaolo Bonzini }
3505d9b902dbSPaolo Bonzini 
350671aa9867SAlberto Garcia void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
350771aa9867SAlberto Garcia                          const char *device, const char *target,
3508df92562eSFam Zheng                          bool has_replaces, const char *replaces,
3509df92562eSFam Zheng                          MirrorSyncMode sync,
3510df92562eSFam Zheng                          bool has_speed, int64_t speed,
3511df92562eSFam Zheng                          bool has_granularity, uint32_t granularity,
3512df92562eSFam Zheng                          bool has_buf_size, int64_t buf_size,
3513df92562eSFam Zheng                          bool has_on_source_error,
3514df92562eSFam Zheng                          BlockdevOnError on_source_error,
3515df92562eSFam Zheng                          bool has_on_target_error,
3516df92562eSFam Zheng                          BlockdevOnError on_target_error,
3517df92562eSFam Zheng                          Error **errp)
3518df92562eSFam Zheng {
3519df92562eSFam Zheng     BlockDriverState *bs;
3520df92562eSFam Zheng     BlockDriverState *target_bs;
3521df92562eSFam Zheng     AioContext *aio_context;
3522274fcceeSMax Reitz     BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
3523df92562eSFam Zheng     Error *local_err = NULL;
3524df92562eSFam Zheng 
352507eec652SKevin Wolf     bs = qmp_get_root_bs(device, errp);
3526df92562eSFam Zheng     if (!bs) {
3527df92562eSFam Zheng         return;
3528df92562eSFam Zheng     }
3529df92562eSFam Zheng 
3530df92562eSFam Zheng     target_bs = bdrv_lookup_bs(target, target, errp);
3531df92562eSFam Zheng     if (!target_bs) {
3532df92562eSFam Zheng         return;
3533df92562eSFam Zheng     }
3534df92562eSFam Zheng 
3535df92562eSFam Zheng     aio_context = bdrv_get_aio_context(bs);
3536df92562eSFam Zheng     aio_context_acquire(aio_context);
3537df92562eSFam Zheng 
3538df92562eSFam Zheng     bdrv_set_aio_context(target_bs, aio_context);
3539df92562eSFam Zheng 
354071aa9867SAlberto Garcia     blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
3541274fcceeSMax Reitz                            has_replaces, replaces, sync, backing_mode,
3542df92562eSFam Zheng                            has_speed, speed,
3543df92562eSFam Zheng                            has_granularity, granularity,
3544df92562eSFam Zheng                            has_buf_size, buf_size,
3545df92562eSFam Zheng                            has_on_source_error, on_source_error,
3546df92562eSFam Zheng                            has_on_target_error, on_target_error,
3547df92562eSFam Zheng                            true, true,
3548df92562eSFam Zheng                            &local_err);
3549df92562eSFam Zheng     error_propagate(errp, local_err);
3550df92562eSFam Zheng 
3551df92562eSFam Zheng     aio_context_release(aio_context);
3552df92562eSFam Zheng }
3553df92562eSFam Zheng 
35543ddf3efeSAlberto Garcia /* Get a block job using its ID and acquire its AioContext */
35553ddf3efeSAlberto Garcia static BlockJob *find_block_job(const char *id, AioContext **aio_context,
355624d6bffeSMarkus Armbruster                                 Error **errp)
35572d47c6e9SStefan Hajnoczi {
35583ddf3efeSAlberto Garcia     BlockJob *job;
35593ddf3efeSAlberto Garcia 
35603ddf3efeSAlberto Garcia     assert(id != NULL);
35612d47c6e9SStefan Hajnoczi 
35625433c24fSMax Reitz     *aio_context = NULL;
35635433c24fSMax Reitz 
35643ddf3efeSAlberto Garcia     job = block_job_get(id);
35653ddf3efeSAlberto Garcia 
35663ddf3efeSAlberto Garcia     if (!job) {
35673ddf3efeSAlberto Garcia         error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
35683ddf3efeSAlberto Garcia                   "Block job '%s' not found", id);
35693ddf3efeSAlberto Garcia         return NULL;
35702d47c6e9SStefan Hajnoczi     }
35713d948cdfSStefan Hajnoczi 
35723ddf3efeSAlberto Garcia     *aio_context = blk_get_aio_context(job->blk);
35733d948cdfSStefan Hajnoczi     aio_context_acquire(*aio_context);
35743d948cdfSStefan Hajnoczi 
35753ddf3efeSAlberto Garcia     return job;
35762d47c6e9SStefan Hajnoczi }
35772d47c6e9SStefan Hajnoczi 
3578882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
35792d47c6e9SStefan Hajnoczi {
35803d948cdfSStefan Hajnoczi     AioContext *aio_context;
358124d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
35822d47c6e9SStefan Hajnoczi 
35832d47c6e9SStefan Hajnoczi     if (!job) {
35842d47c6e9SStefan Hajnoczi         return;
35852d47c6e9SStefan Hajnoczi     }
35862d47c6e9SStefan Hajnoczi 
3587882ec7ceSStefan Hajnoczi     block_job_set_speed(job, speed, errp);
35883d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
35892d47c6e9SStefan Hajnoczi }
3590370521a1SStefan Hajnoczi 
35916e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device,
35926e37fb81SPaolo Bonzini                           bool has_force, bool force, Error **errp)
35936e37fb81SPaolo Bonzini {
35943d948cdfSStefan Hajnoczi     AioContext *aio_context;
359524d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
35966e37fb81SPaolo Bonzini 
35976e37fb81SPaolo Bonzini     if (!job) {
35986e37fb81SPaolo Bonzini         return;
35996e37fb81SPaolo Bonzini     }
36003d948cdfSStefan Hajnoczi 
36013d948cdfSStefan Hajnoczi     if (!has_force) {
36023d948cdfSStefan Hajnoczi         force = false;
36033d948cdfSStefan Hajnoczi     }
36043d948cdfSStefan Hajnoczi 
3605751ebd76SFam Zheng     if (job->user_paused && !force) {
3606f231b88dSCole Robinson         error_setg(errp, "The block job for device '%s' is currently paused",
3607f231b88dSCole Robinson                    device);
36083d948cdfSStefan Hajnoczi         goto out;
36096e37fb81SPaolo Bonzini     }
36106e37fb81SPaolo Bonzini 
36116e37fb81SPaolo Bonzini     trace_qmp_block_job_cancel(job);
36126e37fb81SPaolo Bonzini     block_job_cancel(job);
36133d948cdfSStefan Hajnoczi out:
36143d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
36156e37fb81SPaolo Bonzini }
36166e37fb81SPaolo Bonzini 
36176e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp)
3618370521a1SStefan Hajnoczi {
36193d948cdfSStefan Hajnoczi     AioContext *aio_context;
362024d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
3621370521a1SStefan Hajnoczi 
3622751ebd76SFam Zheng     if (!job || job->user_paused) {
3623370521a1SStefan Hajnoczi         return;
3624370521a1SStefan Hajnoczi     }
36256e37fb81SPaolo Bonzini 
3626751ebd76SFam Zheng     job->user_paused = true;
36276e37fb81SPaolo Bonzini     trace_qmp_block_job_pause(job);
36286e37fb81SPaolo Bonzini     block_job_pause(job);
36293d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
36306e37fb81SPaolo Bonzini }
36316e37fb81SPaolo Bonzini 
36326e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp)
36336e37fb81SPaolo Bonzini {
36343d948cdfSStefan Hajnoczi     AioContext *aio_context;
363524d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
36366e37fb81SPaolo Bonzini 
3637751ebd76SFam Zheng     if (!job || !job->user_paused) {
36388acc72a4SPaolo Bonzini         return;
36398acc72a4SPaolo Bonzini     }
3640370521a1SStefan Hajnoczi 
3641751ebd76SFam Zheng     job->user_paused = false;
36426e37fb81SPaolo Bonzini     trace_qmp_block_job_resume(job);
364317bd51f9SStefan Hajnoczi     block_job_iostatus_reset(job);
36446e37fb81SPaolo Bonzini     block_job_resume(job);
36453d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
3646370521a1SStefan Hajnoczi }
3647fb5458cdSStefan Hajnoczi 
3648aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp)
3649aeae883bSPaolo Bonzini {
36503d948cdfSStefan Hajnoczi     AioContext *aio_context;
365124d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
3652aeae883bSPaolo Bonzini 
3653aeae883bSPaolo Bonzini     if (!job) {
3654aeae883bSPaolo Bonzini         return;
3655aeae883bSPaolo Bonzini     }
3656aeae883bSPaolo Bonzini 
3657aeae883bSPaolo Bonzini     trace_qmp_block_job_complete(job);
3658aeae883bSPaolo Bonzini     block_job_complete(job, errp);
36593d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
3660aeae883bSPaolo Bonzini }
3661aeae883bSPaolo Bonzini 
3662fa40e656SJeff Cody void qmp_change_backing_file(const char *device,
3663fa40e656SJeff Cody                              const char *image_node_name,
3664fa40e656SJeff Cody                              const char *backing_file,
3665fa40e656SJeff Cody                              Error **errp)
3666fa40e656SJeff Cody {
3667fa40e656SJeff Cody     BlockDriverState *bs = NULL;
3668729962f6SStefan Hajnoczi     AioContext *aio_context;
3669fa40e656SJeff Cody     BlockDriverState *image_bs = NULL;
3670fa40e656SJeff Cody     Error *local_err = NULL;
3671fa40e656SJeff Cody     bool ro;
3672fa40e656SJeff Cody     int open_flags;
3673fa40e656SJeff Cody     int ret;
3674fa40e656SJeff Cody 
36757b5dca3fSKevin Wolf     bs = qmp_get_root_bs(device, errp);
36767b5dca3fSKevin Wolf     if (!bs) {
3677fa40e656SJeff Cody         return;
3678fa40e656SJeff Cody     }
3679fa40e656SJeff Cody 
36807b5dca3fSKevin Wolf     aio_context = bdrv_get_aio_context(bs);
3681729962f6SStefan Hajnoczi     aio_context_acquire(aio_context);
3682729962f6SStefan Hajnoczi 
3683fa40e656SJeff Cody     image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3684fa40e656SJeff Cody     if (local_err) {
3685fa40e656SJeff Cody         error_propagate(errp, local_err);
3686729962f6SStefan Hajnoczi         goto out;
3687fa40e656SJeff Cody     }
3688fa40e656SJeff Cody 
3689fa40e656SJeff Cody     if (!image_bs) {
3690fa40e656SJeff Cody         error_setg(errp, "image file not found");
3691729962f6SStefan Hajnoczi         goto out;
3692fa40e656SJeff Cody     }
3693fa40e656SJeff Cody 
3694fa40e656SJeff Cody     if (bdrv_find_base(image_bs) == image_bs) {
3695fa40e656SJeff Cody         error_setg(errp, "not allowing backing file change on an image "
3696fa40e656SJeff Cody                          "without a backing file");
3697729962f6SStefan Hajnoczi         goto out;
3698fa40e656SJeff Cody     }
3699fa40e656SJeff Cody 
3700fa40e656SJeff Cody     /* even though we are not necessarily operating on bs, we need it to
3701fa40e656SJeff Cody      * determine if block ops are currently prohibited on the chain */
3702fa40e656SJeff Cody     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3703729962f6SStefan Hajnoczi         goto out;
3704fa40e656SJeff Cody     }
3705fa40e656SJeff Cody 
3706fa40e656SJeff Cody     /* final sanity check */
3707fa40e656SJeff Cody     if (!bdrv_chain_contains(bs, image_bs)) {
3708fa40e656SJeff Cody         error_setg(errp, "'%s' and image file are not in the same chain",
3709fa40e656SJeff Cody                    device);
3710729962f6SStefan Hajnoczi         goto out;
3711fa40e656SJeff Cody     }
3712fa40e656SJeff Cody 
3713fa40e656SJeff Cody     /* if not r/w, reopen to make r/w */
3714fa40e656SJeff Cody     open_flags = image_bs->open_flags;
3715fa40e656SJeff Cody     ro = bdrv_is_read_only(image_bs);
3716fa40e656SJeff Cody 
3717fa40e656SJeff Cody     if (ro) {
3718fa40e656SJeff Cody         bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3719fa40e656SJeff Cody         if (local_err) {
3720fa40e656SJeff Cody             error_propagate(errp, local_err);
3721729962f6SStefan Hajnoczi             goto out;
3722fa40e656SJeff Cody         }
3723fa40e656SJeff Cody     }
3724fa40e656SJeff Cody 
3725fa40e656SJeff Cody     ret = bdrv_change_backing_file(image_bs, backing_file,
3726fa40e656SJeff Cody                                image_bs->drv ? image_bs->drv->format_name : "");
3727fa40e656SJeff Cody 
3728fa40e656SJeff Cody     if (ret < 0) {
3729fa40e656SJeff Cody         error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3730fa40e656SJeff Cody                          backing_file);
3731fa40e656SJeff Cody         /* don't exit here, so we can try to restore open flags if
3732fa40e656SJeff Cody          * appropriate */
3733fa40e656SJeff Cody     }
3734fa40e656SJeff Cody 
3735fa40e656SJeff Cody     if (ro) {
3736fa40e656SJeff Cody         bdrv_reopen(image_bs, open_flags, &local_err);
3737621ff94dSEduardo Habkost         error_propagate(errp, local_err);
3738fa40e656SJeff Cody     }
3739729962f6SStefan Hajnoczi 
3740729962f6SStefan Hajnoczi out:
3741729962f6SStefan Hajnoczi     aio_context_release(aio_context);
3742fa40e656SJeff Cody }
3743fa40e656SJeff Cody 
3744abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr)
3745abb21ac3SKevin Wolf {
3746abb21ac3SKevin Wolf     QemuOpts *opts;
3747abb21ac3SKevin Wolf     QDict *qdict;
3748abb21ac3SKevin Wolf     Error *local_err = NULL;
3749abb21ac3SKevin Wolf 
3750abb21ac3SKevin Wolf     opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
3751abb21ac3SKevin Wolf     if (!opts) {
3752abb21ac3SKevin Wolf         return;
3753abb21ac3SKevin Wolf     }
3754abb21ac3SKevin Wolf 
3755abb21ac3SKevin Wolf     qdict = qemu_opts_to_qdict(opts, NULL);
3756abb21ac3SKevin Wolf 
3757abb21ac3SKevin Wolf     if (!qdict_get_try_str(qdict, "node-name")) {
3758f8746fb8SKevin Wolf         QDECREF(qdict);
3759abb21ac3SKevin Wolf         error_report("'node-name' needs to be specified");
3760abb21ac3SKevin Wolf         goto out;
3761abb21ac3SKevin Wolf     }
3762abb21ac3SKevin Wolf 
3763abb21ac3SKevin Wolf     BlockDriverState *bs = bds_tree_init(qdict, &local_err);
3764abb21ac3SKevin Wolf     if (!bs) {
3765abb21ac3SKevin Wolf         error_report_err(local_err);
3766abb21ac3SKevin Wolf         goto out;
3767abb21ac3SKevin Wolf     }
3768abb21ac3SKevin Wolf 
3769abb21ac3SKevin Wolf     QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3770abb21ac3SKevin Wolf 
3771abb21ac3SKevin Wolf out:
3772abb21ac3SKevin Wolf     qemu_opts_del(opts);
3773abb21ac3SKevin Wolf }
3774abb21ac3SKevin Wolf 
3775d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3776d26c9a15SKevin Wolf {
3777be4b67bcSMax Reitz     BlockDriverState *bs;
3778d26c9a15SKevin Wolf     QObject *obj;
37793b098d56SEric Blake     Visitor *v = qmp_output_visitor_new(&obj);
3780d26c9a15SKevin Wolf     QDict *qdict;
3781d26c9a15SKevin Wolf     Error *local_err = NULL;
3782d26c9a15SKevin Wolf 
37833b098d56SEric Blake     visit_type_BlockdevOptions(v, NULL, &options, &local_err);
378484d18f06SMarkus Armbruster     if (local_err) {
3785d26c9a15SKevin Wolf         error_propagate(errp, local_err);
3786d26c9a15SKevin Wolf         goto fail;
3787d26c9a15SKevin Wolf     }
3788d26c9a15SKevin Wolf 
37893b098d56SEric Blake     visit_complete(v, &obj);
3790d26c9a15SKevin Wolf     qdict = qobject_to_qdict(obj);
3791d26c9a15SKevin Wolf 
3792d26c9a15SKevin Wolf     qdict_flatten(qdict);
3793d26c9a15SKevin Wolf 
3794be4b67bcSMax Reitz     if (!qdict_get_try_str(qdict, "node-name")) {
37959ec8873eSKevin Wolf         error_setg(errp, "'node-name' must be specified for the root node");
3796be4b67bcSMax Reitz         goto fail;
3797be4b67bcSMax Reitz     }
3798be4b67bcSMax Reitz 
3799bd745e23SMax Reitz     bs = bds_tree_init(qdict, errp);
3800bd745e23SMax Reitz     if (!bs) {
3801be4b67bcSMax Reitz         goto fail;
3802be4b67bcSMax Reitz     }
38039c4218e9SMax Reitz 
38049c4218e9SMax Reitz     QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3805be4b67bcSMax Reitz 
3806be4b67bcSMax Reitz     if (bs && bdrv_key_required(bs)) {
38079c4218e9SMax Reitz         QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3808be4b67bcSMax Reitz         bdrv_unref(bs);
38098ae8e904SKevin Wolf         error_setg(errp, "blockdev-add doesn't support encrypted devices");
38108ae8e904SKevin Wolf         goto fail;
38118ae8e904SKevin Wolf     }
38128ae8e904SKevin Wolf 
3813d26c9a15SKevin Wolf fail:
38143b098d56SEric Blake     visit_free(v);
3815d26c9a15SKevin Wolf }
3816d26c9a15SKevin Wolf 
38179ec8873eSKevin Wolf void qmp_x_blockdev_del(const char *node_name, Error **errp)
381881b936aeSAlberto Garcia {
381981b936aeSAlberto Garcia     AioContext *aio_context;
382081b936aeSAlberto Garcia     BlockDriverState *bs;
382181b936aeSAlberto Garcia 
382281b936aeSAlberto Garcia     bs = bdrv_find_node(node_name);
382381b936aeSAlberto Garcia     if (!bs) {
382481b936aeSAlberto Garcia         error_setg(errp, "Cannot find node %s", node_name);
382581b936aeSAlberto Garcia         return;
382681b936aeSAlberto Garcia     }
38271f0c461bSKevin Wolf     if (bdrv_has_blk(bs)) {
3828e467da7bSKevin Wolf         error_setg(errp, "Node %s is in use", node_name);
382981b936aeSAlberto Garcia         return;
383081b936aeSAlberto Garcia     }
383181b936aeSAlberto Garcia     aio_context = bdrv_get_aio_context(bs);
383281b936aeSAlberto Garcia     aio_context_acquire(aio_context);
383381b936aeSAlberto Garcia 
383481b936aeSAlberto Garcia     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
383581b936aeSAlberto Garcia         goto out;
383681b936aeSAlberto Garcia     }
383781b936aeSAlberto Garcia 
38389ec8873eSKevin Wolf     if (!bs->monitor_list.tqe_prev) {
38399c4218e9SMax Reitz         error_setg(errp, "Node %s is not owned by the monitor",
38409c4218e9SMax Reitz                    bs->node_name);
38419c4218e9SMax Reitz         goto out;
38429c4218e9SMax Reitz     }
38439c4218e9SMax Reitz 
38449c4218e9SMax Reitz     if (bs->refcnt > 1) {
384581b936aeSAlberto Garcia         error_setg(errp, "Block device %s is in use",
384681b936aeSAlberto Garcia                    bdrv_get_device_or_node_name(bs));
384781b936aeSAlberto Garcia         goto out;
384881b936aeSAlberto Garcia     }
384981b936aeSAlberto Garcia 
38509c4218e9SMax Reitz     QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
385181b936aeSAlberto Garcia     bdrv_unref(bs);
385281b936aeSAlberto Garcia 
385381b936aeSAlberto Garcia out:
385481b936aeSAlberto Garcia     aio_context_release(aio_context);
385581b936aeSAlberto Garcia }
385681b936aeSAlberto Garcia 
38577f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
38587f821597SWen Congyang                                   const char *child_name)
38597f821597SWen Congyang {
38607f821597SWen Congyang     BdrvChild *child;
38617f821597SWen Congyang 
38627f821597SWen Congyang     QLIST_FOREACH(child, &parent_bs->children, next) {
38637f821597SWen Congyang         if (strcmp(child->name, child_name) == 0) {
38647f821597SWen Congyang             return child;
38657f821597SWen Congyang         }
38667f821597SWen Congyang     }
38677f821597SWen Congyang 
38687f821597SWen Congyang     return NULL;
38697f821597SWen Congyang }
38707f821597SWen Congyang 
38717f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child,
38727f821597SWen Congyang                            const char *child, bool has_node,
38737f821597SWen Congyang                            const char *node, Error **errp)
38747f821597SWen Congyang {
38757f821597SWen Congyang     BlockDriverState *parent_bs, *new_bs = NULL;
38767f821597SWen Congyang     BdrvChild *p_child;
38777f821597SWen Congyang 
38787f821597SWen Congyang     parent_bs = bdrv_lookup_bs(parent, parent, errp);
38797f821597SWen Congyang     if (!parent_bs) {
38807f821597SWen Congyang         return;
38817f821597SWen Congyang     }
38827f821597SWen Congyang 
38837f821597SWen Congyang     if (has_child == has_node) {
38847f821597SWen Congyang         if (has_child) {
38857f821597SWen Congyang             error_setg(errp, "The parameters child and node are in conflict");
38867f821597SWen Congyang         } else {
38877f821597SWen Congyang             error_setg(errp, "Either child or node must be specified");
38887f821597SWen Congyang         }
38897f821597SWen Congyang         return;
38907f821597SWen Congyang     }
38917f821597SWen Congyang 
38927f821597SWen Congyang     if (has_child) {
38937f821597SWen Congyang         p_child = bdrv_find_child(parent_bs, child);
38947f821597SWen Congyang         if (!p_child) {
38957f821597SWen Congyang             error_setg(errp, "Node '%s' does not have child '%s'",
38967f821597SWen Congyang                        parent, child);
38977f821597SWen Congyang             return;
38987f821597SWen Congyang         }
38997f821597SWen Congyang         bdrv_del_child(parent_bs, p_child, errp);
39007f821597SWen Congyang     }
39017f821597SWen Congyang 
39027f821597SWen Congyang     if (has_node) {
39037f821597SWen Congyang         new_bs = bdrv_find_node(node);
39047f821597SWen Congyang         if (!new_bs) {
39057f821597SWen Congyang             error_setg(errp, "Node '%s' not found", node);
39067f821597SWen Congyang             return;
39077f821597SWen Congyang         }
39087f821597SWen Congyang         bdrv_add_child(parent_bs, new_bs, errp);
39097f821597SWen Congyang     }
39107f821597SWen Congyang }
39117f821597SWen Congyang 
3912fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3913fb5458cdSStefan Hajnoczi {
3914fea68bb6SMarkus Armbruster     BlockJobInfoList *head = NULL, **p_next = &head;
3915f0f55dedSAlberto Garcia     BlockJob *job;
3916fea68bb6SMarkus Armbruster 
3917f0f55dedSAlberto Garcia     for (job = block_job_next(NULL); job; job = block_job_next(job)) {
3918f0f55dedSAlberto Garcia         BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3919f0f55dedSAlberto Garcia         AioContext *aio_context = blk_get_aio_context(job->blk);
392069691e72SStefan Hajnoczi 
392169691e72SStefan Hajnoczi         aio_context_acquire(aio_context);
3922f0f55dedSAlberto Garcia         elem->value = block_job_query(job);
3923f0f55dedSAlberto Garcia         aio_context_release(aio_context);
392469691e72SStefan Hajnoczi 
3925fea68bb6SMarkus Armbruster         *p_next = elem;
3926fea68bb6SMarkus Armbruster         p_next = &elem->next;
3927fea68bb6SMarkus Armbruster     }
392869691e72SStefan Hajnoczi 
3929fea68bb6SMarkus Armbruster     return head;
3930fb5458cdSStefan Hajnoczi }
39314d454574SPaolo Bonzini 
39320006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = {
39334d454574SPaolo Bonzini     .name = "drive",
39340006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
39354d454574SPaolo Bonzini     .desc = {
39364d454574SPaolo Bonzini         {
39374d454574SPaolo Bonzini             .name = "snapshot",
39384d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
39394d454574SPaolo Bonzini             .help = "enable/disable snapshot mode",
39404d454574SPaolo Bonzini         },{
39414d454574SPaolo Bonzini             .name = "aio",
39424d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
39434d454574SPaolo Bonzini             .help = "host AIO implementation (threads, native)",
39444d454574SPaolo Bonzini         },{
3945e4b24b49SKevin Wolf             .name = BDRV_OPT_CACHE_WB,
3946e4b24b49SKevin Wolf             .type = QEMU_OPT_BOOL,
3947e4b24b49SKevin Wolf             .help = "Enable writeback mode",
3948e4b24b49SKevin Wolf         },{
39494d454574SPaolo Bonzini             .name = "format",
39504d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
39514d454574SPaolo Bonzini             .help = "disk format (raw, qcow2, ...)",
39524d454574SPaolo Bonzini         },{
39534d454574SPaolo Bonzini             .name = "rerror",
39544d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
39554d454574SPaolo Bonzini             .help = "read error action",
39564d454574SPaolo Bonzini         },{
39574d454574SPaolo Bonzini             .name = "werror",
39584d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
39594d454574SPaolo Bonzini             .help = "write error action",
39604d454574SPaolo Bonzini         },{
39614e200cf8SAlberto Garcia             .name = BDRV_OPT_READ_ONLY,
39624d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
39634d454574SPaolo Bonzini             .help = "open drive file as read-only",
39644d454574SPaolo Bonzini         },{
396557975222SKevin Wolf             .name = "throttling.iops-total",
39664d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
39674d454574SPaolo Bonzini             .help = "limit total I/O operations per second",
39684d454574SPaolo Bonzini         },{
396957975222SKevin Wolf             .name = "throttling.iops-read",
39704d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
39714d454574SPaolo Bonzini             .help = "limit read operations per second",
39724d454574SPaolo Bonzini         },{
397357975222SKevin Wolf             .name = "throttling.iops-write",
39744d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
39754d454574SPaolo Bonzini             .help = "limit write operations per second",
39764d454574SPaolo Bonzini         },{
397757975222SKevin Wolf             .name = "throttling.bps-total",
39784d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
39794d454574SPaolo Bonzini             .help = "limit total bytes per second",
39804d454574SPaolo Bonzini         },{
398157975222SKevin Wolf             .name = "throttling.bps-read",
39824d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
39834d454574SPaolo Bonzini             .help = "limit read bytes per second",
39844d454574SPaolo Bonzini         },{
398557975222SKevin Wolf             .name = "throttling.bps-write",
39864d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
39874d454574SPaolo Bonzini             .help = "limit write bytes per second",
39884d454574SPaolo Bonzini         },{
39893e9fab69SBenoît Canet             .name = "throttling.iops-total-max",
39903e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
39913e9fab69SBenoît Canet             .help = "I/O operations burst",
39923e9fab69SBenoît Canet         },{
39933e9fab69SBenoît Canet             .name = "throttling.iops-read-max",
39943e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
39953e9fab69SBenoît Canet             .help = "I/O operations read burst",
39963e9fab69SBenoît Canet         },{
39973e9fab69SBenoît Canet             .name = "throttling.iops-write-max",
39983e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
39993e9fab69SBenoît Canet             .help = "I/O operations write burst",
40003e9fab69SBenoît Canet         },{
40013e9fab69SBenoît Canet             .name = "throttling.bps-total-max",
40023e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40033e9fab69SBenoît Canet             .help = "total bytes burst",
40043e9fab69SBenoît Canet         },{
40053e9fab69SBenoît Canet             .name = "throttling.bps-read-max",
40063e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40073e9fab69SBenoît Canet             .help = "total bytes read burst",
40083e9fab69SBenoît Canet         },{
40093e9fab69SBenoît Canet             .name = "throttling.bps-write-max",
40103e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40113e9fab69SBenoît Canet             .help = "total bytes write burst",
40123e9fab69SBenoît Canet         },{
40138a0fc18dSAlberto Garcia             .name = "throttling.iops-total-max-length",
40148a0fc18dSAlberto Garcia             .type = QEMU_OPT_NUMBER,
40158a0fc18dSAlberto Garcia             .help = "length of the iops-total-max burst period, in seconds",
40168a0fc18dSAlberto Garcia         },{
40178a0fc18dSAlberto Garcia             .name = "throttling.iops-read-max-length",
40188a0fc18dSAlberto Garcia             .type = QEMU_OPT_NUMBER,
40198a0fc18dSAlberto Garcia             .help = "length of the iops-read-max burst period, in seconds",
40208a0fc18dSAlberto Garcia         },{
40218a0fc18dSAlberto Garcia             .name = "throttling.iops-write-max-length",
40228a0fc18dSAlberto Garcia             .type = QEMU_OPT_NUMBER,
40238a0fc18dSAlberto Garcia             .help = "length of the iops-write-max burst period, in seconds",
40248a0fc18dSAlberto Garcia         },{
40258a0fc18dSAlberto Garcia             .name = "throttling.bps-total-max-length",
40268a0fc18dSAlberto Garcia             .type = QEMU_OPT_NUMBER,
40278a0fc18dSAlberto Garcia             .help = "length of the bps-total-max burst period, in seconds",
40288a0fc18dSAlberto Garcia         },{
40298a0fc18dSAlberto Garcia             .name = "throttling.bps-read-max-length",
40308a0fc18dSAlberto Garcia             .type = QEMU_OPT_NUMBER,
40318a0fc18dSAlberto Garcia             .help = "length of the bps-read-max burst period, in seconds",
40328a0fc18dSAlberto Garcia         },{
40338a0fc18dSAlberto Garcia             .name = "throttling.bps-write-max-length",
40348a0fc18dSAlberto Garcia             .type = QEMU_OPT_NUMBER,
40358a0fc18dSAlberto Garcia             .help = "length of the bps-write-max burst period, in seconds",
40368a0fc18dSAlberto Garcia         },{
40372024c1dfSBenoît Canet             .name = "throttling.iops-size",
40382024c1dfSBenoît Canet             .type = QEMU_OPT_NUMBER,
40392024c1dfSBenoît Canet             .help = "when limiting by iops max size of an I/O in bytes",
40402024c1dfSBenoît Canet         },{
404176f4afb4SAlberto Garcia             .name = "throttling.group",
404276f4afb4SAlberto Garcia             .type = QEMU_OPT_STRING,
404376f4afb4SAlberto Garcia             .help = "name of the block throttling group",
404476f4afb4SAlberto Garcia         },{
40454d454574SPaolo Bonzini             .name = "copy-on-read",
40464d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
40474d454574SPaolo Bonzini             .help = "copy read data from backing file into image file",
4048465bee1dSPeter Lieven         },{
4049465bee1dSPeter Lieven             .name = "detect-zeroes",
4050465bee1dSPeter Lieven             .type = QEMU_OPT_STRING,
4051465bee1dSPeter Lieven             .help = "try to optimize zero writes (off, on, unmap)",
4052362e9299SAlberto Garcia         },{
4053362e9299SAlberto Garcia             .name = "stats-account-invalid",
4054362e9299SAlberto Garcia             .type = QEMU_OPT_BOOL,
4055362e9299SAlberto Garcia             .help = "whether to account for invalid I/O operations "
4056362e9299SAlberto Garcia                     "in the statistics",
4057362e9299SAlberto Garcia         },{
4058362e9299SAlberto Garcia             .name = "stats-account-failed",
4059362e9299SAlberto Garcia             .type = QEMU_OPT_BOOL,
4060362e9299SAlberto Garcia             .help = "whether to account for failed I/O operations "
4061362e9299SAlberto Garcia                     "in the statistics",
40624d454574SPaolo Bonzini         },
40634d454574SPaolo Bonzini         { /* end of list */ }
40644d454574SPaolo Bonzini     },
40654d454574SPaolo Bonzini };
40660006383eSKevin Wolf 
40670006383eSKevin Wolf QemuOptsList qemu_drive_opts = {
40680006383eSKevin Wolf     .name = "drive",
40690006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
40700006383eSKevin Wolf     .desc = {
4071492fdc6fSKevin Wolf         /*
4072492fdc6fSKevin Wolf          * no elements => accept any params
4073492fdc6fSKevin Wolf          * validation will happen later
4074492fdc6fSKevin Wolf          */
40750006383eSKevin Wolf         { /* end of list */ }
40760006383eSKevin Wolf     },
40770006383eSKevin Wolf };
4078