xref: /openbmc/qemu/blockdev.c (revision d5851089a8a77d5c23e8d5fffb5b99265009ba62)
1666daa68SMarkus Armbruster /*
2666daa68SMarkus Armbruster  * QEMU host block devices
3666daa68SMarkus Armbruster  *
4666daa68SMarkus Armbruster  * Copyright (c) 2003-2008 Fabrice Bellard
5666daa68SMarkus Armbruster  *
6666daa68SMarkus Armbruster  * This work is licensed under the terms of the GNU GPL, version 2 or
7666daa68SMarkus Armbruster  * later.  See the COPYING file in the top-level directory.
83618a094SMarkus Armbruster  *
93618a094SMarkus Armbruster  * This file incorporates work covered by the following copyright and
103618a094SMarkus Armbruster  * permission notice:
113618a094SMarkus Armbruster  *
123618a094SMarkus Armbruster  * Copyright (c) 2003-2008 Fabrice Bellard
133618a094SMarkus Armbruster  *
143618a094SMarkus Armbruster  * Permission is hereby granted, free of charge, to any person obtaining a copy
153618a094SMarkus Armbruster  * of this software and associated documentation files (the "Software"), to deal
163618a094SMarkus Armbruster  * in the Software without restriction, including without limitation the rights
173618a094SMarkus Armbruster  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
183618a094SMarkus Armbruster  * copies of the Software, and to permit persons to whom the Software is
193618a094SMarkus Armbruster  * furnished to do so, subject to the following conditions:
203618a094SMarkus Armbruster  *
213618a094SMarkus Armbruster  * The above copyright notice and this permission notice shall be included in
223618a094SMarkus Armbruster  * all copies or substantial portions of the Software.
233618a094SMarkus Armbruster  *
243618a094SMarkus Armbruster  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
253618a094SMarkus Armbruster  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
263618a094SMarkus Armbruster  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
273618a094SMarkus Armbruster  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
283618a094SMarkus Armbruster  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
293618a094SMarkus Armbruster  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
303618a094SMarkus Armbruster  * THE SOFTWARE.
31666daa68SMarkus Armbruster  */
32666daa68SMarkus Armbruster 
33d38ea87aSPeter Maydell #include "qemu/osdep.h"
3426f54e9aSMarkus Armbruster #include "sysemu/block-backend.h"
359c17d615SPaolo Bonzini #include "sysemu/blockdev.h"
360d09e41aSPaolo Bonzini #include "hw/block/block.h"
37737e150eSPaolo Bonzini #include "block/blockjob.h"
3876f4afb4SAlberto Garcia #include "block/throttle-groups.h"
3983c9089eSPaolo Bonzini #include "monitor/monitor.h"
40d49b6836SMarkus Armbruster #include "qemu/error-report.h"
411de7afc9SPaolo Bonzini #include "qemu/option.h"
421de7afc9SPaolo Bonzini #include "qemu/config-file.h"
437b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h"
44d26c9a15SKevin Wolf #include "qapi-visit.h"
45cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h"
46d26c9a15SKevin Wolf #include "qapi/qmp-output-visitor.h"
479e7dac7cSPeter Lieven #include "qapi/util.h"
489c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
49737e150eSPaolo Bonzini #include "block/block_int.h"
50a4dea8a9SLuiz Capitulino #include "qmp-commands.h"
5112bd451fSStefan Hajnoczi #include "trace.h"
529c17d615SPaolo Bonzini #include "sysemu/arch_init.h"
53666daa68SMarkus Armbruster 
549c4218e9SMax Reitz static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
559c4218e9SMax Reitz     QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
569c4218e9SMax Reitz 
571960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = {
581960966dSMarkus Armbruster     [IF_NONE] = "none",
591960966dSMarkus Armbruster     [IF_IDE] = "ide",
601960966dSMarkus Armbruster     [IF_SCSI] = "scsi",
611960966dSMarkus Armbruster     [IF_FLOPPY] = "floppy",
621960966dSMarkus Armbruster     [IF_PFLASH] = "pflash",
631960966dSMarkus Armbruster     [IF_MTD] = "mtd",
641960966dSMarkus Armbruster     [IF_SD] = "sd",
651960966dSMarkus Armbruster     [IF_VIRTIO] = "virtio",
661960966dSMarkus Armbruster     [IF_XEN] = "xen",
671960966dSMarkus Armbruster };
681960966dSMarkus Armbruster 
6921dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = {
7027d6bf40SMarkus Armbruster     /*
7127d6bf40SMarkus Armbruster      * Do not change these numbers!  They govern how drive option
7227d6bf40SMarkus Armbruster      * index maps to unit and bus.  That mapping is ABI.
7327d6bf40SMarkus Armbruster      *
7427d6bf40SMarkus Armbruster      * All controllers used to imlement if=T drives need to support
7527d6bf40SMarkus Armbruster      * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
7627d6bf40SMarkus Armbruster      * Otherwise, some index values map to "impossible" bus, unit
7727d6bf40SMarkus Armbruster      * values.
7827d6bf40SMarkus Armbruster      *
7927d6bf40SMarkus Armbruster      * For instance, if you change [IF_SCSI] to 255, -drive
8027d6bf40SMarkus Armbruster      * if=scsi,index=12 no longer means bus=1,unit=5, but
8127d6bf40SMarkus Armbruster      * bus=0,unit=12.  With an lsi53c895a controller (7 units max),
8227d6bf40SMarkus Armbruster      * the drive can't be set up.  Regression.
8327d6bf40SMarkus Armbruster      */
8427d6bf40SMarkus Armbruster     [IF_IDE] = 2,
8527d6bf40SMarkus Armbruster     [IF_SCSI] = 7,
861960966dSMarkus Armbruster };
871960966dSMarkus Armbruster 
8821dff8cfSJohn Snow /**
8921dff8cfSJohn Snow  * Boards may call this to offer board-by-board overrides
9021dff8cfSJohn Snow  * of the default, global values.
9121dff8cfSJohn Snow  */
9221dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs)
9321dff8cfSJohn Snow {
9418e46a03SMarkus Armbruster     BlockBackend *blk;
9521dff8cfSJohn Snow     DriveInfo *dinfo;
9621dff8cfSJohn Snow 
9721dff8cfSJohn Snow     if (max_devs <= 0) {
9821dff8cfSJohn Snow         return;
9921dff8cfSJohn Snow     }
10021dff8cfSJohn Snow 
10118e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
10218e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
10321dff8cfSJohn Snow         if (dinfo->type == type) {
10421dff8cfSJohn Snow             fprintf(stderr, "Cannot override units-per-bus property of"
10521dff8cfSJohn Snow                     " the %s interface, because a drive of that type has"
10621dff8cfSJohn Snow                     " already been added.\n", if_name[type]);
10721dff8cfSJohn Snow             g_assert_not_reached();
10821dff8cfSJohn Snow         }
10921dff8cfSJohn Snow     }
11021dff8cfSJohn Snow 
11121dff8cfSJohn Snow     if_max_devs[type] = max_devs;
11221dff8cfSJohn Snow }
11321dff8cfSJohn Snow 
11414bafc54SMarkus Armbruster /*
11514bafc54SMarkus Armbruster  * We automatically delete the drive when a device using it gets
11614bafc54SMarkus Armbruster  * unplugged.  Questionable feature, but we can't just drop it.
11714bafc54SMarkus Armbruster  * Device models call blockdev_mark_auto_del() to schedule the
11814bafc54SMarkus Armbruster  * automatic deletion, and generic qdev code calls blockdev_auto_del()
11914bafc54SMarkus Armbruster  * when deletion is actually safe.
12014bafc54SMarkus Armbruster  */
1214be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk)
12214bafc54SMarkus Armbruster {
12318e46a03SMarkus Armbruster     DriveInfo *dinfo = blk_legacy_dinfo(blk);
1244be74634SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
12591fddb0dSStefan Hajnoczi     AioContext *aio_context;
12614bafc54SMarkus Armbruster 
12726f8b3a8SMarkus Armbruster     if (!dinfo) {
1282d246f01SKevin Wolf         return;
1292d246f01SKevin Wolf     }
1302d246f01SKevin Wolf 
1315433c24fSMax Reitz     if (bs) {
13291fddb0dSStefan Hajnoczi         aio_context = bdrv_get_aio_context(bs);
13391fddb0dSStefan Hajnoczi         aio_context_acquire(aio_context);
13491fddb0dSStefan Hajnoczi 
13512bde0eeSPaolo Bonzini         if (bs->job) {
13612bde0eeSPaolo Bonzini             block_job_cancel(bs->job);
13712bde0eeSPaolo Bonzini         }
13891fddb0dSStefan Hajnoczi 
13991fddb0dSStefan Hajnoczi         aio_context_release(aio_context);
1405433c24fSMax Reitz     }
14191fddb0dSStefan Hajnoczi 
14214bafc54SMarkus Armbruster     dinfo->auto_del = 1;
14314bafc54SMarkus Armbruster }
14414bafc54SMarkus Armbruster 
1454be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk)
14614bafc54SMarkus Armbruster {
14718e46a03SMarkus Armbruster     DriveInfo *dinfo = blk_legacy_dinfo(blk);
14814bafc54SMarkus Armbruster 
1490fc0f1faSRyan Harper     if (dinfo && dinfo->auto_del) {
150b9fe8a7aSMarkus Armbruster         blk_unref(blk);
15114bafc54SMarkus Armbruster     }
15214bafc54SMarkus Armbruster }
15314bafc54SMarkus Armbruster 
154d8f94e1bSJohn Snow /**
155d8f94e1bSJohn Snow  * Returns the current mapping of how many units per bus
156d8f94e1bSJohn Snow  * a particular interface can support.
157d8f94e1bSJohn Snow  *
158d8f94e1bSJohn Snow  *  A positive integer indicates n units per bus.
159d8f94e1bSJohn Snow  *  0 implies the mapping has not been established.
160d8f94e1bSJohn Snow  * -1 indicates an invalid BlockInterfaceType was given.
161d8f94e1bSJohn Snow  */
162d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type)
163d8f94e1bSJohn Snow {
164d8f94e1bSJohn Snow     if (type >= IF_IDE && type < IF_COUNT) {
165d8f94e1bSJohn Snow         return if_max_devs[type];
166d8f94e1bSJohn Snow     }
167d8f94e1bSJohn Snow 
168d8f94e1bSJohn Snow     return -1;
169d8f94e1bSJohn Snow }
170d8f94e1bSJohn Snow 
171505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index)
172505a7fb1SMarkus Armbruster {
173505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
174505a7fb1SMarkus Armbruster     return max_devs ? index / max_devs : 0;
175505a7fb1SMarkus Armbruster }
176505a7fb1SMarkus Armbruster 
177505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index)
178505a7fb1SMarkus Armbruster {
179505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
180505a7fb1SMarkus Armbruster     return max_devs ? index % max_devs : index;
181505a7fb1SMarkus Armbruster }
182505a7fb1SMarkus Armbruster 
1832292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr)
1842292ddaeSMarkus Armbruster {
18570b94331SMarkus Armbruster     return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
1862292ddaeSMarkus Armbruster }
1872292ddaeSMarkus Armbruster 
1882292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
1895645b0f4SMarkus Armbruster                     const char *optstr)
190666daa68SMarkus Armbruster {
191666daa68SMarkus Armbruster     QemuOpts *opts;
192666daa68SMarkus Armbruster 
1932292ddaeSMarkus Armbruster     opts = drive_def(optstr);
194666daa68SMarkus Armbruster     if (!opts) {
195666daa68SMarkus Armbruster         return NULL;
196666daa68SMarkus Armbruster     }
1972292ddaeSMarkus Armbruster     if (type != IF_DEFAULT) {
198f43e47dbSMarkus Armbruster         qemu_opt_set(opts, "if", if_name[type], &error_abort);
1992292ddaeSMarkus Armbruster     }
2002292ddaeSMarkus Armbruster     if (index >= 0) {
201a8b18f8fSMarkus Armbruster         qemu_opt_set_number(opts, "index", index, &error_abort);
2022292ddaeSMarkus Armbruster     }
203666daa68SMarkus Armbruster     if (file)
204f43e47dbSMarkus Armbruster         qemu_opt_set(opts, "file", file, &error_abort);
205666daa68SMarkus Armbruster     return opts;
206666daa68SMarkus Armbruster }
207666daa68SMarkus Armbruster 
208666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
209666daa68SMarkus Armbruster {
21018e46a03SMarkus Armbruster     BlockBackend *blk;
211666daa68SMarkus Armbruster     DriveInfo *dinfo;
212666daa68SMarkus Armbruster 
21318e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
21418e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
21518e46a03SMarkus Armbruster         if (dinfo && dinfo->type == type
21618e46a03SMarkus Armbruster             && dinfo->bus == bus && dinfo->unit == unit) {
217666daa68SMarkus Armbruster             return dinfo;
218666daa68SMarkus Armbruster         }
21918e46a03SMarkus Armbruster     }
220666daa68SMarkus Armbruster 
221666daa68SMarkus Armbruster     return NULL;
222666daa68SMarkus Armbruster }
223666daa68SMarkus Armbruster 
224a66c9dc7SJohn Snow bool drive_check_orphaned(void)
225a66c9dc7SJohn Snow {
22618e46a03SMarkus Armbruster     BlockBackend *blk;
227a66c9dc7SJohn Snow     DriveInfo *dinfo;
228a66c9dc7SJohn Snow     bool rs = false;
229a66c9dc7SJohn Snow 
23018e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
23118e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
232a66c9dc7SJohn Snow         /* If dinfo->bdrv->dev is NULL, it has no device attached. */
233a66c9dc7SJohn Snow         /* Unless this is a default drive, this may be an oversight. */
234a7f53e26SMarkus Armbruster         if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
235a66c9dc7SJohn Snow             dinfo->type != IF_NONE) {
236a66c9dc7SJohn Snow             fprintf(stderr, "Warning: Orphaned drive without device: "
237a66c9dc7SJohn Snow                     "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
2385433c24fSMax Reitz                     blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
2395433c24fSMax Reitz                     if_name[dinfo->type], dinfo->bus, dinfo->unit);
240a66c9dc7SJohn Snow             rs = true;
241a66c9dc7SJohn Snow         }
242a66c9dc7SJohn Snow     }
243a66c9dc7SJohn Snow 
244a66c9dc7SJohn Snow     return rs;
245a66c9dc7SJohn Snow }
246a66c9dc7SJohn Snow 
247f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
248f1bd51acSMarkus Armbruster {
249f1bd51acSMarkus Armbruster     return drive_get(type,
250f1bd51acSMarkus Armbruster                      drive_index_to_bus_id(type, index),
251f1bd51acSMarkus Armbruster                      drive_index_to_unit_id(type, index));
252f1bd51acSMarkus Armbruster }
253f1bd51acSMarkus Armbruster 
254666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type)
255666daa68SMarkus Armbruster {
256666daa68SMarkus Armbruster     int max_bus;
25718e46a03SMarkus Armbruster     BlockBackend *blk;
258666daa68SMarkus Armbruster     DriveInfo *dinfo;
259666daa68SMarkus Armbruster 
260666daa68SMarkus Armbruster     max_bus = -1;
26118e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
26218e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
26318e46a03SMarkus Armbruster         if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
264666daa68SMarkus Armbruster             max_bus = dinfo->bus;
265666daa68SMarkus Armbruster         }
26618e46a03SMarkus Armbruster     }
267666daa68SMarkus Armbruster     return max_bus;
268666daa68SMarkus Armbruster }
269666daa68SMarkus Armbruster 
27013839974SMarkus Armbruster /* Get a block device.  This should only be used for single-drive devices
27113839974SMarkus Armbruster    (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
27213839974SMarkus Armbruster    appropriate bus.  */
27313839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type)
27413839974SMarkus Armbruster {
27513839974SMarkus Armbruster     static int next_block_unit[IF_COUNT];
27613839974SMarkus Armbruster 
27713839974SMarkus Armbruster     return drive_get(type, 0, next_block_unit[type]++);
27813839974SMarkus Armbruster }
27913839974SMarkus Armbruster 
280666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name)
281666daa68SMarkus Armbruster {
282807105a7SMarkus Armbruster     error_printf(" %s", name);
283666daa68SMarkus Armbruster }
284666daa68SMarkus Armbruster 
285aa398a5cSStefan Hajnoczi typedef struct {
286aa398a5cSStefan Hajnoczi     QEMUBH *bh;
287fa510ebfSFam Zheng     BlockDriverState *bs;
288fa510ebfSFam Zheng } BDRVPutRefBH;
289aa398a5cSStefan Hajnoczi 
290b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
291666daa68SMarkus Armbruster {
292666daa68SMarkus Armbruster     if (!strcmp(buf, "ignore")) {
29392aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_IGNORE;
294666daa68SMarkus Armbruster     } else if (!is_read && !strcmp(buf, "enospc")) {
29592aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_ENOSPC;
296666daa68SMarkus Armbruster     } else if (!strcmp(buf, "stop")) {
29792aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_STOP;
298666daa68SMarkus Armbruster     } else if (!strcmp(buf, "report")) {
29992aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_REPORT;
300666daa68SMarkus Armbruster     } else {
301b681072dSKevin Wolf         error_setg(errp, "'%s' invalid %s error action",
302666daa68SMarkus Armbruster                    buf, is_read ? "read" : "write");
303666daa68SMarkus Armbruster         return -1;
304666daa68SMarkus Armbruster     }
305666daa68SMarkus Armbruster }
306666daa68SMarkus Armbruster 
30740119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
30840119effSAlberto Garcia                                   Error **errp)
30940119effSAlberto Garcia {
31040119effSAlberto Garcia     const QListEntry *entry;
31140119effSAlberto Garcia     for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
31240119effSAlberto Garcia         switch (qobject_type(entry->value)) {
31340119effSAlberto Garcia 
31440119effSAlberto Garcia         case QTYPE_QSTRING: {
31540119effSAlberto Garcia             unsigned long long length;
31640119effSAlberto Garcia             const char *str = qstring_get_str(qobject_to_qstring(entry->value));
31740119effSAlberto Garcia             if (parse_uint_full(str, &length, 10) == 0 &&
31840119effSAlberto Garcia                 length > 0 && length <= UINT_MAX) {
31940119effSAlberto Garcia                 block_acct_add_interval(stats, (unsigned) length);
32040119effSAlberto Garcia             } else {
32140119effSAlberto Garcia                 error_setg(errp, "Invalid interval length: %s", str);
32240119effSAlberto Garcia                 return false;
32340119effSAlberto Garcia             }
32440119effSAlberto Garcia             break;
32540119effSAlberto Garcia         }
32640119effSAlberto Garcia 
32740119effSAlberto Garcia         case QTYPE_QINT: {
32840119effSAlberto Garcia             int64_t length = qint_get_int(qobject_to_qint(entry->value));
32940119effSAlberto Garcia             if (length > 0 && length <= UINT_MAX) {
33040119effSAlberto Garcia                 block_acct_add_interval(stats, (unsigned) length);
33140119effSAlberto Garcia             } else {
33240119effSAlberto Garcia                 error_setg(errp, "Invalid interval length: %" PRId64, length);
33340119effSAlberto Garcia                 return false;
33440119effSAlberto Garcia             }
33540119effSAlberto Garcia             break;
33640119effSAlberto Garcia         }
33740119effSAlberto Garcia 
33840119effSAlberto Garcia         default:
33940119effSAlberto Garcia             error_setg(errp, "The specification of stats-intervals is invalid");
34040119effSAlberto Garcia             return false;
34140119effSAlberto Garcia         }
34240119effSAlberto Garcia     }
34340119effSAlberto Garcia     return true;
34440119effSAlberto Garcia }
34540119effSAlberto Garcia 
34633cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
34733cb7dc8SKevin Wolf 
348fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */
349fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
350fbf8175eSMax Reitz     const char **throttling_group, ThrottleConfig *throttle_cfg,
351fbf8175eSMax Reitz     BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
352fbf8175eSMax Reitz {
353fbf8175eSMax Reitz     const char *discard;
354fbf8175eSMax Reitz     Error *local_error = NULL;
355fbf8175eSMax Reitz     const char *aio;
356fbf8175eSMax Reitz 
357fbf8175eSMax Reitz     if (bdrv_flags) {
358fbf8175eSMax Reitz         if (!qemu_opt_get_bool(opts, "read-only", false)) {
359fbf8175eSMax Reitz             *bdrv_flags |= BDRV_O_RDWR;
360fbf8175eSMax Reitz         }
361fbf8175eSMax Reitz         if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
362fbf8175eSMax Reitz             *bdrv_flags |= BDRV_O_COPY_ON_READ;
363fbf8175eSMax Reitz         }
364fbf8175eSMax Reitz 
365fbf8175eSMax Reitz         if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
366fbf8175eSMax Reitz             if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
367fbf8175eSMax Reitz                 error_setg(errp, "Invalid discard option");
368fbf8175eSMax Reitz                 return;
369fbf8175eSMax Reitz             }
370fbf8175eSMax Reitz         }
371fbf8175eSMax Reitz 
372fbf8175eSMax Reitz         if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
373fbf8175eSMax Reitz             if (!strcmp(aio, "native")) {
374fbf8175eSMax Reitz                 *bdrv_flags |= BDRV_O_NATIVE_AIO;
375fbf8175eSMax Reitz             } else if (!strcmp(aio, "threads")) {
376fbf8175eSMax Reitz                 /* this is the default */
377fbf8175eSMax Reitz             } else {
378fbf8175eSMax Reitz                error_setg(errp, "invalid aio option");
379fbf8175eSMax Reitz                return;
380fbf8175eSMax Reitz             }
381fbf8175eSMax Reitz         }
382fbf8175eSMax Reitz     }
383fbf8175eSMax Reitz 
384fbf8175eSMax Reitz     /* disk I/O throttling */
385fbf8175eSMax Reitz     if (throttling_group) {
386fbf8175eSMax Reitz         *throttling_group = qemu_opt_get(opts, "throttling.group");
387fbf8175eSMax Reitz     }
388fbf8175eSMax Reitz 
389fbf8175eSMax Reitz     if (throttle_cfg) {
390fbf8175eSMax Reitz         memset(throttle_cfg, 0, sizeof(*throttle_cfg));
391fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
392fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-total", 0);
393fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_READ].avg  =
394fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-read", 0);
395fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
396fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-write", 0);
397fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
398fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-total", 0);
399fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
400fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-read", 0);
401fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
402fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-write", 0);
403fbf8175eSMax Reitz 
404fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
405fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
406fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_READ].max  =
407fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
408fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
409fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
410fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
411fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
412fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_READ].max =
413fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
414fbf8175eSMax Reitz         throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
415fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
416fbf8175eSMax Reitz 
417fbf8175eSMax Reitz         throttle_cfg->op_size =
418fbf8175eSMax Reitz             qemu_opt_get_number(opts, "throttling.iops-size", 0);
419fbf8175eSMax Reitz 
420*d5851089SAlberto Garcia         if (!throttle_is_valid(throttle_cfg, errp)) {
421fbf8175eSMax Reitz             return;
422fbf8175eSMax Reitz         }
423fbf8175eSMax Reitz     }
424fbf8175eSMax Reitz 
425fbf8175eSMax Reitz     if (detect_zeroes) {
426fbf8175eSMax Reitz         *detect_zeroes =
427fbf8175eSMax Reitz             qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
428fbf8175eSMax Reitz                             qemu_opt_get(opts, "detect-zeroes"),
4297fb1cf16SEric Blake                             BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
430fbf8175eSMax Reitz                             BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
431fbf8175eSMax Reitz                             &local_error);
432fbf8175eSMax Reitz         if (local_error) {
433fbf8175eSMax Reitz             error_propagate(errp, local_error);
434fbf8175eSMax Reitz             return;
435fbf8175eSMax Reitz         }
436fbf8175eSMax Reitz 
437fbf8175eSMax Reitz         if (bdrv_flags &&
438fbf8175eSMax Reitz             *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
439fbf8175eSMax Reitz             !(*bdrv_flags & BDRV_O_UNMAP))
440fbf8175eSMax Reitz         {
441fbf8175eSMax Reitz             error_setg(errp, "setting detect-zeroes to unmap is not allowed "
442fbf8175eSMax Reitz                              "without setting discard operation to unmap");
443fbf8175eSMax Reitz             return;
444fbf8175eSMax Reitz         }
445fbf8175eSMax Reitz     }
446fbf8175eSMax Reitz }
447fbf8175eSMax Reitz 
448f298d071SKevin Wolf /* Takes the ownership of bs_opts */
44918e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
450b681072dSKevin Wolf                                    Error **errp)
451666daa68SMarkus Armbruster {
452666daa68SMarkus Armbruster     const char *buf;
453666daa68SMarkus Armbruster     int bdrv_flags = 0;
454666daa68SMarkus Armbruster     int on_read_error, on_write_error;
455362e9299SAlberto Garcia     bool account_invalid, account_failed;
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 
49440119effSAlberto Garcia     qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
49540119effSAlberto Garcia     qdict_array_split(interval_dict, &interval_list);
49640119effSAlberto Garcia 
49740119effSAlberto Garcia     if (qdict_size(interval_dict) != 0) {
49840119effSAlberto Garcia         error_setg(errp, "Invalid option stats-intervals.%s",
49940119effSAlberto Garcia                    qdict_first(interval_dict)->key);
50040119effSAlberto Garcia         goto early_err;
50140119effSAlberto Garcia     }
5022be5506fSAlberto Garcia 
503fbf8175eSMax Reitz     extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
504fbf8175eSMax Reitz                                     &detect_zeroes, &error);
505fbf8175eSMax Reitz     if (error) {
506fbf8175eSMax Reitz         error_propagate(errp, error);
507ec9c10d2SStefan Hajnoczi         goto early_err;
508a9384affSPaolo Bonzini     }
509666daa68SMarkus Armbruster 
510666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "format")) != NULL) {
511c8057f95SPeter Maydell         if (is_help_option(buf)) {
512807105a7SMarkus Armbruster             error_printf("Supported formats:");
513666daa68SMarkus Armbruster             bdrv_iterate_format(bdrv_format_print, NULL);
514807105a7SMarkus Armbruster             error_printf("\n");
515ec9c10d2SStefan Hajnoczi             goto early_err;
516666daa68SMarkus Armbruster         }
51774fe54f2SKevin Wolf 
518e4342ce5SMax Reitz         if (qdict_haskey(bs_opts, "driver")) {
519e4342ce5SMax Reitz             error_setg(errp, "Cannot specify both 'driver' and 'format'");
520ec9c10d2SStefan Hajnoczi             goto early_err;
5216db5f5d6SMike Qiu         }
522e4342ce5SMax Reitz         qdict_put(bs_opts, "driver", qstring_from_str(buf));
523666daa68SMarkus Armbruster     }
524666daa68SMarkus Armbruster 
52592aa5c6dSPaolo Bonzini     on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
526666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
527b681072dSKevin Wolf         on_write_error = parse_block_error_action(buf, 0, &error);
52884d18f06SMarkus Armbruster         if (error) {
529b681072dSKevin Wolf             error_propagate(errp, error);
530ec9c10d2SStefan Hajnoczi             goto early_err;
531666daa68SMarkus Armbruster         }
532666daa68SMarkus Armbruster     }
533666daa68SMarkus Armbruster 
53492aa5c6dSPaolo Bonzini     on_read_error = BLOCKDEV_ON_ERROR_REPORT;
535666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
536b681072dSKevin Wolf         on_read_error = parse_block_error_action(buf, 1, &error);
53784d18f06SMarkus Armbruster         if (error) {
538b681072dSKevin Wolf             error_propagate(errp, error);
539ec9c10d2SStefan Hajnoczi             goto early_err;
540666daa68SMarkus Armbruster         }
541666daa68SMarkus Armbruster     }
542666daa68SMarkus Armbruster 
543666daa68SMarkus Armbruster     if (snapshot) {
54491a097e7SKevin Wolf         bdrv_flags |= BDRV_O_SNAPSHOT;
545666daa68SMarkus Armbruster     }
546666daa68SMarkus Armbruster 
5475ec18f8cSMax Reitz     /* init */
54839c4ae94SKevin Wolf     if ((!file || !*file) && !qdict_size(bs_opts)) {
5495ec18f8cSMax Reitz         BlockBackendRootState *blk_rs;
5505ec18f8cSMax Reitz 
5515ec18f8cSMax Reitz         blk = blk_new(qemu_opts_id(opts), errp);
5525ec18f8cSMax Reitz         if (!blk) {
5535ec18f8cSMax Reitz             goto early_err;
5545ec18f8cSMax Reitz         }
5555ec18f8cSMax Reitz 
5565ec18f8cSMax Reitz         blk_rs = blk_get_root_state(blk);
5575ec18f8cSMax Reitz         blk_rs->open_flags    = bdrv_flags;
558fbf8175eSMax Reitz         blk_rs->read_only     = !(bdrv_flags & BDRV_O_RDWR);
5595ec18f8cSMax Reitz         blk_rs->detect_zeroes = detect_zeroes;
5605ec18f8cSMax Reitz 
5615ec18f8cSMax Reitz         if (throttle_enabled(&cfg)) {
5625ec18f8cSMax Reitz             if (!throttling_group) {
5635ec18f8cSMax Reitz                 throttling_group = blk_name(blk);
5645ec18f8cSMax Reitz             }
5655ec18f8cSMax Reitz             blk_rs->throttle_group = g_strdup(throttling_group);
5665ec18f8cSMax Reitz             blk_rs->throttle_state = throttle_group_incref(throttling_group);
5675ec18f8cSMax Reitz             blk_rs->throttle_state->cfg = cfg;
5685ec18f8cSMax Reitz         }
5695ec18f8cSMax Reitz 
5705ec18f8cSMax Reitz         QDECREF(bs_opts);
5715ec18f8cSMax Reitz     } else {
5725ec18f8cSMax Reitz         if (file && !*file) {
5735ec18f8cSMax Reitz             file = NULL;
5745ec18f8cSMax Reitz         }
5755ec18f8cSMax Reitz 
57691a097e7SKevin Wolf         /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
57791a097e7SKevin Wolf          * with other callers) rather than what we want as the real defaults.
57891a097e7SKevin Wolf          * Apply the defaults here instead. */
57991a097e7SKevin Wolf         qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_WB, "on");
58091a097e7SKevin Wolf         qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
58191a097e7SKevin Wolf         qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
58291a097e7SKevin Wolf 
58391a097e7SKevin Wolf         if (snapshot) {
58491a097e7SKevin Wolf             /* always use cache=unsafe with snapshot */
58591a097e7SKevin Wolf             qdict_put(bs_opts, BDRV_OPT_CACHE_WB, qstring_from_str("on"));
58691a097e7SKevin Wolf             qdict_put(bs_opts, BDRV_OPT_CACHE_DIRECT, qstring_from_str("off"));
58791a097e7SKevin Wolf             qdict_put(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, qstring_from_str("on"));
58891a097e7SKevin Wolf         }
58991a097e7SKevin Wolf 
59012d5ee3aSKevin Wolf         if (runstate_check(RUN_STATE_INMIGRATE)) {
59112d5ee3aSKevin Wolf             bdrv_flags |= BDRV_O_INACTIVE;
59212d5ee3aSKevin Wolf         }
59312d5ee3aSKevin Wolf 
594e4342ce5SMax Reitz         blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
595e4342ce5SMax Reitz                            errp);
596e4342ce5SMax Reitz         if (!blk) {
597e4342ce5SMax Reitz             goto err_no_bs_opts;
598e4342ce5SMax Reitz         }
599e4342ce5SMax Reitz         bs = blk_bs(blk);
6000006383eSKevin Wolf 
601e4342ce5SMax Reitz         bs->detect_zeroes = detect_zeroes;
602e4342ce5SMax Reitz 
603e4342ce5SMax Reitz         /* disk I/O throttling */
604e4342ce5SMax Reitz         if (throttle_enabled(&cfg)) {
60576f4afb4SAlberto Garcia             if (!throttling_group) {
60676f4afb4SAlberto Garcia                 throttling_group = blk_name(blk);
60776f4afb4SAlberto Garcia             }
60876f4afb4SAlberto Garcia             bdrv_io_limits_enable(bs, throttling_group);
609e4342ce5SMax Reitz             bdrv_set_io_limits(bs, &cfg);
610666daa68SMarkus Armbruster         }
611666daa68SMarkus Armbruster 
612a0f1eab1SMarkus Armbruster         if (bdrv_key_required(bs)) {
613666daa68SMarkus Armbruster             autostart = 0;
614a0f1eab1SMarkus Armbruster         }
615362e9299SAlberto Garcia 
616362e9299SAlberto Garcia         block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
6172be5506fSAlberto Garcia 
61840119effSAlberto Garcia         if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
6192be5506fSAlberto Garcia             blk_unref(blk);
6202be5506fSAlberto Garcia             blk = NULL;
6212be5506fSAlberto Garcia             goto err_no_bs_opts;
6222be5506fSAlberto Garcia         }
6232be5506fSAlberto Garcia     }
6245ec18f8cSMax Reitz 
6255ec18f8cSMax Reitz     blk_set_on_error(blk, on_read_error, on_write_error);
6260006383eSKevin Wolf 
627e4342ce5SMax Reitz err_no_bs_opts:
6280006383eSKevin Wolf     qemu_opts_del(opts);
62940119effSAlberto Garcia     QDECREF(interval_dict);
63040119effSAlberto Garcia     QDECREF(interval_list);
63118e46a03SMarkus Armbruster     return blk;
632a9ae2bffSMarkus Armbruster 
633ec9c10d2SStefan Hajnoczi early_err:
634ec9c10d2SStefan Hajnoczi     qemu_opts_del(opts);
63540119effSAlberto Garcia     QDECREF(interval_dict);
63640119effSAlberto Garcia     QDECREF(interval_list);
6376376f952SMarkus Armbruster err_no_opts:
6386376f952SMarkus Armbruster     QDECREF(bs_opts);
639a9ae2bffSMarkus Armbruster     return NULL;
640666daa68SMarkus Armbruster }
641666daa68SMarkus Armbruster 
642bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts;
643bd745e23SMax Reitz 
644bd745e23SMax Reitz /* Takes the ownership of bs_opts */
645bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
646bd745e23SMax Reitz {
647bd745e23SMax Reitz     BlockDriverState *bs;
648bd745e23SMax Reitz     QemuOpts *opts;
649bd745e23SMax Reitz     Error *local_error = NULL;
650bd745e23SMax Reitz     BlockdevDetectZeroesOptions detect_zeroes;
651bd745e23SMax Reitz     int ret;
652bd745e23SMax Reitz     int bdrv_flags = 0;
653bd745e23SMax Reitz 
654bd745e23SMax Reitz     opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
655bd745e23SMax Reitz     if (!opts) {
656bd745e23SMax Reitz         goto fail;
657bd745e23SMax Reitz     }
658bd745e23SMax Reitz 
659bd745e23SMax Reitz     qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
660bd745e23SMax Reitz     if (local_error) {
661bd745e23SMax Reitz         error_propagate(errp, local_error);
662bd745e23SMax Reitz         goto fail;
663bd745e23SMax Reitz     }
664bd745e23SMax Reitz 
665bd745e23SMax Reitz     extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
666bd745e23SMax Reitz                                     &detect_zeroes, &local_error);
667bd745e23SMax Reitz     if (local_error) {
668bd745e23SMax Reitz         error_propagate(errp, local_error);
669bd745e23SMax Reitz         goto fail;
670bd745e23SMax Reitz     }
671bd745e23SMax Reitz 
67212d5ee3aSKevin Wolf     if (runstate_check(RUN_STATE_INMIGRATE)) {
67312d5ee3aSKevin Wolf         bdrv_flags |= BDRV_O_INACTIVE;
67412d5ee3aSKevin Wolf     }
67512d5ee3aSKevin Wolf 
676bd745e23SMax Reitz     bs = NULL;
677bd745e23SMax Reitz     ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp);
678bd745e23SMax Reitz     if (ret < 0) {
679bd745e23SMax Reitz         goto fail_no_bs_opts;
680bd745e23SMax Reitz     }
681bd745e23SMax Reitz 
682bd745e23SMax Reitz     bs->detect_zeroes = detect_zeroes;
683bd745e23SMax Reitz 
684bd745e23SMax Reitz fail_no_bs_opts:
685bd745e23SMax Reitz     qemu_opts_del(opts);
686bd745e23SMax Reitz     return bs;
687bd745e23SMax Reitz 
688bd745e23SMax Reitz fail:
689bd745e23SMax Reitz     qemu_opts_del(opts);
690bd745e23SMax Reitz     QDECREF(bs_opts);
691bd745e23SMax Reitz     return NULL;
692bd745e23SMax Reitz }
693bd745e23SMax Reitz 
6949c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void)
6959c4218e9SMax Reitz {
6969c4218e9SMax Reitz     BlockDriverState *bs, *next_bs;
6979c4218e9SMax Reitz 
6989c4218e9SMax Reitz     QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
6999c4218e9SMax Reitz         AioContext *ctx = bdrv_get_aio_context(bs);
7009c4218e9SMax Reitz 
7019c4218e9SMax Reitz         aio_context_acquire(ctx);
7029c4218e9SMax Reitz         bdrv_unref(bs);
7039c4218e9SMax Reitz         aio_context_release(ctx);
7049c4218e9SMax Reitz     }
7059c4218e9SMax Reitz }
7069c4218e9SMax Reitz 
7075abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
7085abbf0eeSKevin Wolf                             Error **errp)
70957975222SKevin Wolf {
71057975222SKevin Wolf     const char *value;
71157975222SKevin Wolf 
71257975222SKevin Wolf     value = qemu_opt_get(opts, from);
71357975222SKevin Wolf     if (value) {
7145abbf0eeSKevin Wolf         if (qemu_opt_find(opts, to)) {
7155abbf0eeSKevin Wolf             error_setg(errp, "'%s' and its alias '%s' can't be used at the "
7165abbf0eeSKevin Wolf                        "same time", to, from);
7175abbf0eeSKevin Wolf             return;
7185abbf0eeSKevin Wolf         }
71920d6cd47SJun Li     }
72020d6cd47SJun Li 
72120d6cd47SJun Li     /* rename all items in opts */
72220d6cd47SJun Li     while ((value = qemu_opt_get(opts, from))) {
723f43e47dbSMarkus Armbruster         qemu_opt_set(opts, to, value, &error_abort);
72457975222SKevin Wolf         qemu_opt_unset(opts, from);
72557975222SKevin Wolf     }
72657975222SKevin Wolf }
72757975222SKevin Wolf 
72833cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = {
72933cb7dc8SKevin Wolf     .name = "drive",
73033cb7dc8SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
73133cb7dc8SKevin Wolf     .desc = {
73233cb7dc8SKevin Wolf         {
73387a899c5SKevin Wolf             .name = "bus",
73487a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
73587a899c5SKevin Wolf             .help = "bus number",
73687a899c5SKevin Wolf         },{
73787a899c5SKevin Wolf             .name = "unit",
73887a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
73987a899c5SKevin Wolf             .help = "unit number (i.e. lun for scsi)",
74087a899c5SKevin Wolf         },{
74187a899c5SKevin Wolf             .name = "index",
74287a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
74387a899c5SKevin Wolf             .help = "index number",
74487a899c5SKevin Wolf         },{
74533cb7dc8SKevin Wolf             .name = "media",
74633cb7dc8SKevin Wolf             .type = QEMU_OPT_STRING,
74733cb7dc8SKevin Wolf             .help = "media type (disk, cdrom)",
748593d464bSKevin Wolf         },{
749593d464bSKevin Wolf             .name = "if",
750593d464bSKevin Wolf             .type = QEMU_OPT_STRING,
751593d464bSKevin Wolf             .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
752b41a7338SKevin Wolf         },{
753b41a7338SKevin Wolf             .name = "cyls",
754b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
755b41a7338SKevin Wolf             .help = "number of cylinders (ide disk geometry)",
756b41a7338SKevin Wolf         },{
757b41a7338SKevin Wolf             .name = "heads",
758b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
759b41a7338SKevin Wolf             .help = "number of heads (ide disk geometry)",
760b41a7338SKevin Wolf         },{
761b41a7338SKevin Wolf             .name = "secs",
762b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
763b41a7338SKevin Wolf             .help = "number of sectors (ide disk geometry)",
764b41a7338SKevin Wolf         },{
765b41a7338SKevin Wolf             .name = "trans",
766b41a7338SKevin Wolf             .type = QEMU_OPT_STRING,
767b41a7338SKevin Wolf             .help = "chs translation (auto, lba, none)",
76826929298SKevin Wolf         },{
76926929298SKevin Wolf             .name = "boot",
77026929298SKevin Wolf             .type = QEMU_OPT_BOOL,
77126929298SKevin Wolf             .help = "(deprecated, ignored)",
772394c7d4dSKevin Wolf         },{
773394c7d4dSKevin Wolf             .name = "addr",
774394c7d4dSKevin Wolf             .type = QEMU_OPT_STRING,
775394c7d4dSKevin Wolf             .help = "pci address (virtio only)",
776d095b465SMax Reitz         },{
777bcf83158SKevin Wolf             .name = "serial",
778bcf83158SKevin Wolf             .type = QEMU_OPT_STRING,
779bcf83158SKevin Wolf             .help = "disk serial number",
780bcf83158SKevin Wolf         },{
781d095b465SMax Reitz             .name = "file",
782d095b465SMax Reitz             .type = QEMU_OPT_STRING,
783d095b465SMax Reitz             .help = "file name",
78433cb7dc8SKevin Wolf         },
7850ebd24e0SKevin Wolf 
7860ebd24e0SKevin Wolf         /* Options that are passed on, but have special semantics with -drive */
7870ebd24e0SKevin Wolf         {
7880ebd24e0SKevin Wolf             .name = "read-only",
7890ebd24e0SKevin Wolf             .type = QEMU_OPT_BOOL,
7900ebd24e0SKevin Wolf             .help = "open drive file as read-only",
7910ebd24e0SKevin Wolf         },{
792ee13ed1cSKevin Wolf             .name = "rerror",
793ee13ed1cSKevin Wolf             .type = QEMU_OPT_STRING,
794ee13ed1cSKevin Wolf             .help = "read error action",
795ee13ed1cSKevin Wolf         },{
796ee13ed1cSKevin Wolf             .name = "werror",
797ee13ed1cSKevin Wolf             .type = QEMU_OPT_STRING,
798ee13ed1cSKevin Wolf             .help = "write error action",
799ee13ed1cSKevin Wolf         },{
8000ebd24e0SKevin Wolf             .name = "copy-on-read",
8010ebd24e0SKevin Wolf             .type = QEMU_OPT_BOOL,
8020ebd24e0SKevin Wolf             .help = "copy read data from backing file into image file",
8030ebd24e0SKevin Wolf         },
8040ebd24e0SKevin Wolf 
80533cb7dc8SKevin Wolf         { /* end of list */ }
80633cb7dc8SKevin Wolf     },
80733cb7dc8SKevin Wolf };
80833cb7dc8SKevin Wolf 
80960e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
81057975222SKevin Wolf {
81129c4e2b5SKevin Wolf     const char *value;
81218e46a03SMarkus Armbruster     BlockBackend *blk;
81333cb7dc8SKevin Wolf     DriveInfo *dinfo = NULL;
814f298d071SKevin Wolf     QDict *bs_opts;
81533cb7dc8SKevin Wolf     QemuOpts *legacy_opts;
81633cb7dc8SKevin Wolf     DriveMediaType media = MEDIA_DISK;
817593d464bSKevin Wolf     BlockInterfaceType type;
818b41a7338SKevin Wolf     int cyls, heads, secs, translation;
81987a899c5SKevin Wolf     int max_devs, bus_id, unit_id, index;
820394c7d4dSKevin Wolf     const char *devaddr;
821ee13ed1cSKevin Wolf     const char *werror, *rerror;
822a7fdbcf0SFam Zheng     bool read_only = false;
823a7fdbcf0SFam Zheng     bool copy_on_read;
824bcf83158SKevin Wolf     const char *serial;
825d095b465SMax Reitz     const char *filename;
82633cb7dc8SKevin Wolf     Error *local_err = NULL;
827247147fbSKevin Wolf     int i;
82829c4e2b5SKevin Wolf 
82957975222SKevin Wolf     /* Change legacy command line options into QMP ones */
830247147fbSKevin Wolf     static const struct {
831247147fbSKevin Wolf         const char *from;
832247147fbSKevin Wolf         const char *to;
833247147fbSKevin Wolf     } opt_renames[] = {
834247147fbSKevin Wolf         { "iops",           "throttling.iops-total" },
835247147fbSKevin Wolf         { "iops_rd",        "throttling.iops-read" },
836247147fbSKevin Wolf         { "iops_wr",        "throttling.iops-write" },
83757975222SKevin Wolf 
838247147fbSKevin Wolf         { "bps",            "throttling.bps-total" },
839247147fbSKevin Wolf         { "bps_rd",         "throttling.bps-read" },
840247147fbSKevin Wolf         { "bps_wr",         "throttling.bps-write" },
84157975222SKevin Wolf 
842247147fbSKevin Wolf         { "iops_max",       "throttling.iops-total-max" },
843247147fbSKevin Wolf         { "iops_rd_max",    "throttling.iops-read-max" },
844247147fbSKevin Wolf         { "iops_wr_max",    "throttling.iops-write-max" },
8453e9fab69SBenoît Canet 
846247147fbSKevin Wolf         { "bps_max",        "throttling.bps-total-max" },
847247147fbSKevin Wolf         { "bps_rd_max",     "throttling.bps-read-max" },
848247147fbSKevin Wolf         { "bps_wr_max",     "throttling.bps-write-max" },
8493e9fab69SBenoît Canet 
850247147fbSKevin Wolf         { "iops_size",      "throttling.iops-size" },
8512024c1dfSBenoît Canet 
85276f4afb4SAlberto Garcia         { "group",          "throttling.group" },
85376f4afb4SAlberto Garcia 
854247147fbSKevin Wolf         { "readonly",       "read-only" },
855247147fbSKevin Wolf     };
856247147fbSKevin Wolf 
857247147fbSKevin Wolf     for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
8585abbf0eeSKevin Wolf         qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
8595abbf0eeSKevin Wolf                         &local_err);
8605abbf0eeSKevin Wolf         if (local_err) {
861565f65d2SMarkus Armbruster             error_report_err(local_err);
8625abbf0eeSKevin Wolf             return NULL;
8635abbf0eeSKevin Wolf         }
864247147fbSKevin Wolf     }
8650f227a94SKevin Wolf 
86629c4e2b5SKevin Wolf     value = qemu_opt_get(all_opts, "cache");
86729c4e2b5SKevin Wolf     if (value) {
86829c4e2b5SKevin Wolf         int flags = 0;
86929c4e2b5SKevin Wolf 
87029c4e2b5SKevin Wolf         if (bdrv_parse_cache_flags(value, &flags) != 0) {
87129c4e2b5SKevin Wolf             error_report("invalid cache option");
87229c4e2b5SKevin Wolf             return NULL;
87329c4e2b5SKevin Wolf         }
87429c4e2b5SKevin Wolf 
87529c4e2b5SKevin Wolf         /* Specific options take precedence */
87654861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
87754861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
878cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_CACHE_WB), &error_abort);
87929c4e2b5SKevin Wolf         }
88054861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
88154861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
882cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_NOCACHE), &error_abort);
88329c4e2b5SKevin Wolf         }
88454861b92SKevin Wolf         if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
88554861b92SKevin Wolf             qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
886cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_NO_FLUSH), &error_abort);
88729c4e2b5SKevin Wolf         }
88829c4e2b5SKevin Wolf         qemu_opt_unset(all_opts, "cache");
88929c4e2b5SKevin Wolf     }
89029c4e2b5SKevin Wolf 
891f298d071SKevin Wolf     /* Get a QDict for processing the options */
892f298d071SKevin Wolf     bs_opts = qdict_new();
893f298d071SKevin Wolf     qemu_opts_to_qdict(all_opts, bs_opts);
894f298d071SKevin Wolf 
89587ea75d5SPeter Crosthwaite     legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
89687ea75d5SPeter Crosthwaite                                    &error_abort);
89733cb7dc8SKevin Wolf     qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
89884d18f06SMarkus Armbruster     if (local_err) {
899565f65d2SMarkus Armbruster         error_report_err(local_err);
90033cb7dc8SKevin Wolf         goto fail;
90133cb7dc8SKevin Wolf     }
90233cb7dc8SKevin Wolf 
90326929298SKevin Wolf     /* Deprecated option boot=[on|off] */
90426929298SKevin Wolf     if (qemu_opt_get(legacy_opts, "boot") != NULL) {
90526929298SKevin Wolf         fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
90626929298SKevin Wolf                 "ignored. Future versions will reject this parameter. Please "
90726929298SKevin Wolf                 "update your scripts.\n");
90826929298SKevin Wolf     }
90926929298SKevin Wolf 
91033cb7dc8SKevin Wolf     /* Media type */
91133cb7dc8SKevin Wolf     value = qemu_opt_get(legacy_opts, "media");
91233cb7dc8SKevin Wolf     if (value) {
91333cb7dc8SKevin Wolf         if (!strcmp(value, "disk")) {
91433cb7dc8SKevin Wolf             media = MEDIA_DISK;
91533cb7dc8SKevin Wolf         } else if (!strcmp(value, "cdrom")) {
91633cb7dc8SKevin Wolf             media = MEDIA_CDROM;
917a7fdbcf0SFam Zheng             read_only = true;
91833cb7dc8SKevin Wolf         } else {
91933cb7dc8SKevin Wolf             error_report("'%s' invalid media", value);
92033cb7dc8SKevin Wolf             goto fail;
92133cb7dc8SKevin Wolf         }
92233cb7dc8SKevin Wolf     }
92333cb7dc8SKevin Wolf 
9240ebd24e0SKevin Wolf     /* copy-on-read is disabled with a warning for read-only devices */
925a7fdbcf0SFam Zheng     read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
9260ebd24e0SKevin Wolf     copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
9270ebd24e0SKevin Wolf 
9280ebd24e0SKevin Wolf     if (read_only && copy_on_read) {
9290ebd24e0SKevin Wolf         error_report("warning: disabling copy-on-read on read-only drive");
9300ebd24e0SKevin Wolf         copy_on_read = false;
9310ebd24e0SKevin Wolf     }
9320ebd24e0SKevin Wolf 
9330ebd24e0SKevin Wolf     qdict_put(bs_opts, "read-only",
9340ebd24e0SKevin Wolf               qstring_from_str(read_only ? "on" : "off"));
9350ebd24e0SKevin Wolf     qdict_put(bs_opts, "copy-on-read",
9360ebd24e0SKevin Wolf               qstring_from_str(copy_on_read ? "on" :"off"));
9370ebd24e0SKevin Wolf 
938593d464bSKevin Wolf     /* Controller type */
939593d464bSKevin Wolf     value = qemu_opt_get(legacy_opts, "if");
940593d464bSKevin Wolf     if (value) {
941593d464bSKevin Wolf         for (type = 0;
942593d464bSKevin Wolf              type < IF_COUNT && strcmp(value, if_name[type]);
943593d464bSKevin Wolf              type++) {
944593d464bSKevin Wolf         }
945593d464bSKevin Wolf         if (type == IF_COUNT) {
946593d464bSKevin Wolf             error_report("unsupported bus type '%s'", value);
947593d464bSKevin Wolf             goto fail;
948593d464bSKevin Wolf         }
949593d464bSKevin Wolf     } else {
950593d464bSKevin Wolf         type = block_default_type;
951593d464bSKevin Wolf     }
952593d464bSKevin Wolf 
953b41a7338SKevin Wolf     /* Geometry */
954b41a7338SKevin Wolf     cyls  = qemu_opt_get_number(legacy_opts, "cyls", 0);
955b41a7338SKevin Wolf     heads = qemu_opt_get_number(legacy_opts, "heads", 0);
956b41a7338SKevin Wolf     secs  = qemu_opt_get_number(legacy_opts, "secs", 0);
957b41a7338SKevin Wolf 
958b41a7338SKevin Wolf     if (cyls || heads || secs) {
959b41a7338SKevin Wolf         if (cyls < 1) {
960b41a7338SKevin Wolf             error_report("invalid physical cyls number");
961b41a7338SKevin Wolf             goto fail;
962b41a7338SKevin Wolf         }
963b41a7338SKevin Wolf         if (heads < 1) {
964b41a7338SKevin Wolf             error_report("invalid physical heads number");
965b41a7338SKevin Wolf             goto fail;
966b41a7338SKevin Wolf         }
967b41a7338SKevin Wolf         if (secs < 1) {
968b41a7338SKevin Wolf             error_report("invalid physical secs number");
969b41a7338SKevin Wolf             goto fail;
970b41a7338SKevin Wolf         }
971b41a7338SKevin Wolf     }
972b41a7338SKevin Wolf 
973b41a7338SKevin Wolf     translation = BIOS_ATA_TRANSLATION_AUTO;
974b41a7338SKevin Wolf     value = qemu_opt_get(legacy_opts, "trans");
975b41a7338SKevin Wolf     if (value != NULL) {
976b41a7338SKevin Wolf         if (!cyls) {
977b41a7338SKevin Wolf             error_report("'%s' trans must be used with cyls, heads and secs",
978b41a7338SKevin Wolf                          value);
979b41a7338SKevin Wolf             goto fail;
980b41a7338SKevin Wolf         }
981b41a7338SKevin Wolf         if (!strcmp(value, "none")) {
982b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_NONE;
983b41a7338SKevin Wolf         } else if (!strcmp(value, "lba")) {
984b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_LBA;
985f31c41ffSPaolo Bonzini         } else if (!strcmp(value, "large")) {
986f31c41ffSPaolo Bonzini             translation = BIOS_ATA_TRANSLATION_LARGE;
987f31c41ffSPaolo Bonzini         } else if (!strcmp(value, "rechs")) {
988f31c41ffSPaolo Bonzini             translation = BIOS_ATA_TRANSLATION_RECHS;
989b41a7338SKevin Wolf         } else if (!strcmp(value, "auto")) {
990b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_AUTO;
991b41a7338SKevin Wolf         } else {
992b41a7338SKevin Wolf             error_report("'%s' invalid translation type", value);
993b41a7338SKevin Wolf             goto fail;
994b41a7338SKevin Wolf         }
995b41a7338SKevin Wolf     }
996b41a7338SKevin Wolf 
997b41a7338SKevin Wolf     if (media == MEDIA_CDROM) {
998b41a7338SKevin Wolf         if (cyls || secs || heads) {
999b41a7338SKevin Wolf             error_report("CHS can't be set with media=cdrom");
1000b41a7338SKevin Wolf             goto fail;
1001b41a7338SKevin Wolf         }
1002b41a7338SKevin Wolf     }
1003b41a7338SKevin Wolf 
100487a899c5SKevin Wolf     /* Device address specified by bus/unit or index.
100587a899c5SKevin Wolf      * If none was specified, try to find the first free one. */
100687a899c5SKevin Wolf     bus_id  = qemu_opt_get_number(legacy_opts, "bus", 0);
100787a899c5SKevin Wolf     unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
100887a899c5SKevin Wolf     index   = qemu_opt_get_number(legacy_opts, "index", -1);
100987a899c5SKevin Wolf 
101087a899c5SKevin Wolf     max_devs = if_max_devs[type];
101187a899c5SKevin Wolf 
101287a899c5SKevin Wolf     if (index != -1) {
101387a899c5SKevin Wolf         if (bus_id != 0 || unit_id != -1) {
101487a899c5SKevin Wolf             error_report("index cannot be used with bus and unit");
101587a899c5SKevin Wolf             goto fail;
101687a899c5SKevin Wolf         }
101787a899c5SKevin Wolf         bus_id = drive_index_to_bus_id(type, index);
101887a899c5SKevin Wolf         unit_id = drive_index_to_unit_id(type, index);
101987a899c5SKevin Wolf     }
102087a899c5SKevin Wolf 
102187a899c5SKevin Wolf     if (unit_id == -1) {
102287a899c5SKevin Wolf        unit_id = 0;
102387a899c5SKevin Wolf        while (drive_get(type, bus_id, unit_id) != NULL) {
102487a899c5SKevin Wolf            unit_id++;
102587a899c5SKevin Wolf            if (max_devs && unit_id >= max_devs) {
102687a899c5SKevin Wolf                unit_id -= max_devs;
102787a899c5SKevin Wolf                bus_id++;
102887a899c5SKevin Wolf            }
102987a899c5SKevin Wolf        }
103087a899c5SKevin Wolf     }
103187a899c5SKevin Wolf 
103287a899c5SKevin Wolf     if (max_devs && unit_id >= max_devs) {
103387a899c5SKevin Wolf         error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
103487a899c5SKevin Wolf         goto fail;
103587a899c5SKevin Wolf     }
103687a899c5SKevin Wolf 
103787a899c5SKevin Wolf     if (drive_get(type, bus_id, unit_id) != NULL) {
103887a899c5SKevin Wolf         error_report("drive with bus=%d, unit=%d (index=%d) exists",
103987a899c5SKevin Wolf                      bus_id, unit_id, index);
104087a899c5SKevin Wolf         goto fail;
104187a899c5SKevin Wolf     }
104287a899c5SKevin Wolf 
1043bcf83158SKevin Wolf     /* Serial number */
1044bcf83158SKevin Wolf     serial = qemu_opt_get(legacy_opts, "serial");
1045bcf83158SKevin Wolf 
104687a899c5SKevin Wolf     /* no id supplied -> create one */
104787a899c5SKevin Wolf     if (qemu_opts_id(all_opts) == NULL) {
104887a899c5SKevin Wolf         char *new_id;
104987a899c5SKevin Wolf         const char *mediastr = "";
105087a899c5SKevin Wolf         if (type == IF_IDE || type == IF_SCSI) {
105187a899c5SKevin Wolf             mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
105287a899c5SKevin Wolf         }
105387a899c5SKevin Wolf         if (max_devs) {
105487a899c5SKevin Wolf             new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
105587a899c5SKevin Wolf                                      mediastr, unit_id);
105687a899c5SKevin Wolf         } else {
105787a899c5SKevin Wolf             new_id = g_strdup_printf("%s%s%i", if_name[type],
105887a899c5SKevin Wolf                                      mediastr, unit_id);
105987a899c5SKevin Wolf         }
106087a899c5SKevin Wolf         qdict_put(bs_opts, "id", qstring_from_str(new_id));
106187a899c5SKevin Wolf         g_free(new_id);
106287a899c5SKevin Wolf     }
106387a899c5SKevin Wolf 
1064394c7d4dSKevin Wolf     /* Add virtio block device */
1065394c7d4dSKevin Wolf     devaddr = qemu_opt_get(legacy_opts, "addr");
1066394c7d4dSKevin Wolf     if (devaddr && type != IF_VIRTIO) {
1067394c7d4dSKevin Wolf         error_report("addr is not supported by this bus type");
1068394c7d4dSKevin Wolf         goto fail;
1069394c7d4dSKevin Wolf     }
1070394c7d4dSKevin Wolf 
1071394c7d4dSKevin Wolf     if (type == IF_VIRTIO) {
1072394c7d4dSKevin Wolf         QemuOpts *devopts;
107387ea75d5SPeter Crosthwaite         devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
107487ea75d5SPeter Crosthwaite                                    &error_abort);
1075394c7d4dSKevin Wolf         if (arch_type == QEMU_ARCH_S390X) {
10761f68f1d3SAlexander Graf             qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
1077394c7d4dSKevin Wolf         } else {
1078f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
1079394c7d4dSKevin Wolf         }
1080f43e47dbSMarkus Armbruster         qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1081f43e47dbSMarkus Armbruster                      &error_abort);
1082394c7d4dSKevin Wolf         if (devaddr) {
1083f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "addr", devaddr, &error_abort);
1084394c7d4dSKevin Wolf         }
1085394c7d4dSKevin Wolf     }
1086394c7d4dSKevin Wolf 
1087d095b465SMax Reitz     filename = qemu_opt_get(legacy_opts, "file");
1088d095b465SMax Reitz 
1089ee13ed1cSKevin Wolf     /* Check werror/rerror compatibility with if=... */
1090ee13ed1cSKevin Wolf     werror = qemu_opt_get(legacy_opts, "werror");
1091ee13ed1cSKevin Wolf     if (werror != NULL) {
1092ee13ed1cSKevin Wolf         if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1093ee13ed1cSKevin Wolf             type != IF_NONE) {
1094ee13ed1cSKevin Wolf             error_report("werror is not supported by this bus type");
1095ee13ed1cSKevin Wolf             goto fail;
1096ee13ed1cSKevin Wolf         }
1097ee13ed1cSKevin Wolf         qdict_put(bs_opts, "werror", qstring_from_str(werror));
1098ee13ed1cSKevin Wolf     }
1099ee13ed1cSKevin Wolf 
1100ee13ed1cSKevin Wolf     rerror = qemu_opt_get(legacy_opts, "rerror");
1101ee13ed1cSKevin Wolf     if (rerror != NULL) {
1102ee13ed1cSKevin Wolf         if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1103ee13ed1cSKevin Wolf             type != IF_NONE) {
1104ee13ed1cSKevin Wolf             error_report("rerror is not supported by this bus type");
1105ee13ed1cSKevin Wolf             goto fail;
1106ee13ed1cSKevin Wolf         }
1107ee13ed1cSKevin Wolf         qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1108ee13ed1cSKevin Wolf     }
1109ee13ed1cSKevin Wolf 
11102d246f01SKevin Wolf     /* Actual block device init: Functionality shared with blockdev-add */
111118e46a03SMarkus Armbruster     blk = blockdev_init(filename, bs_opts, &local_err);
11123cb0e25cSMarkus Armbruster     bs_opts = NULL;
111318e46a03SMarkus Armbruster     if (!blk) {
111484d18f06SMarkus Armbruster         if (local_err) {
1115565f65d2SMarkus Armbruster             error_report_err(local_err);
1116b681072dSKevin Wolf         }
11172d246f01SKevin Wolf         goto fail;
1118b681072dSKevin Wolf     } else {
111984d18f06SMarkus Armbruster         assert(!local_err);
11202d246f01SKevin Wolf     }
11212d246f01SKevin Wolf 
112226f8b3a8SMarkus Armbruster     /* Create legacy DriveInfo */
112326f8b3a8SMarkus Armbruster     dinfo = g_malloc0(sizeof(*dinfo));
1124f298d071SKevin Wolf     dinfo->opts = all_opts;
11252d246f01SKevin Wolf 
1126b41a7338SKevin Wolf     dinfo->cyls = cyls;
1127b41a7338SKevin Wolf     dinfo->heads = heads;
1128b41a7338SKevin Wolf     dinfo->secs = secs;
1129b41a7338SKevin Wolf     dinfo->trans = translation;
1130b41a7338SKevin Wolf 
1131ee13ed1cSKevin Wolf     dinfo->type = type;
113287a899c5SKevin Wolf     dinfo->bus = bus_id;
113387a899c5SKevin Wolf     dinfo->unit = unit_id;
1134394c7d4dSKevin Wolf     dinfo->devaddr = devaddr;
1135bcf83158SKevin Wolf     dinfo->serial = g_strdup(serial);
1136bcf83158SKevin Wolf 
113726f8b3a8SMarkus Armbruster     blk_set_legacy_dinfo(blk, dinfo);
113826f8b3a8SMarkus Armbruster 
1139e34ef046SKevin Wolf     switch(type) {
1140e34ef046SKevin Wolf     case IF_IDE:
1141e34ef046SKevin Wolf     case IF_SCSI:
1142e34ef046SKevin Wolf     case IF_XEN:
1143e34ef046SKevin Wolf     case IF_NONE:
1144e34ef046SKevin Wolf         dinfo->media_cd = media == MEDIA_CDROM;
1145e34ef046SKevin Wolf         break;
1146e34ef046SKevin Wolf     default:
1147e34ef046SKevin Wolf         break;
1148e34ef046SKevin Wolf     }
1149e34ef046SKevin Wolf 
11502d246f01SKevin Wolf fail:
115133cb7dc8SKevin Wolf     qemu_opts_del(legacy_opts);
11523cb0e25cSMarkus Armbruster     QDECREF(bs_opts);
11532d246f01SKevin Wolf     return dinfo;
115457975222SKevin Wolf }
115557975222SKevin Wolf 
11563e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict)
1157666daa68SMarkus Armbruster {
1158666daa68SMarkus Armbruster     const char *device = qdict_get_str(qdict, "device");
1159a0e8544cSFam Zheng     BlockBackend *blk;
11602d3735d3SStefan Hajnoczi     int ret;
11612d3735d3SStefan Hajnoczi 
1162e8877497SStefan Hajnoczi     if (!strcmp(device, "all")) {
1163e8877497SStefan Hajnoczi         ret = bdrv_commit_all();
1164e8877497SStefan Hajnoczi     } else {
116584aa0140SStefan Hajnoczi         BlockDriverState *bs;
116684aa0140SStefan Hajnoczi         AioContext *aio_context;
116784aa0140SStefan Hajnoczi 
1168a0e8544cSFam Zheng         blk = blk_by_name(device);
1169a0e8544cSFam Zheng         if (!blk) {
117058513bdeSJeff Cody             monitor_printf(mon, "Device '%s' not found\n", device);
1171ac59eb95SMarkus Armbruster             return;
11726ab4b5abSMarkus Armbruster         }
11735433c24fSMax Reitz         if (!blk_is_available(blk)) {
11745433c24fSMax Reitz             monitor_printf(mon, "Device '%s' has no medium\n", device);
11755433c24fSMax Reitz             return;
11765433c24fSMax Reitz         }
117784aa0140SStefan Hajnoczi 
117884aa0140SStefan Hajnoczi         bs = blk_bs(blk);
117984aa0140SStefan Hajnoczi         aio_context = bdrv_get_aio_context(bs);
118084aa0140SStefan Hajnoczi         aio_context_acquire(aio_context);
118184aa0140SStefan Hajnoczi 
118284aa0140SStefan Hajnoczi         ret = bdrv_commit(bs);
118384aa0140SStefan Hajnoczi 
118484aa0140SStefan Hajnoczi         aio_context_release(aio_context);
11852d3735d3SStefan Hajnoczi     }
118658513bdeSJeff Cody     if (ret < 0) {
118758513bdeSJeff Cody         monitor_printf(mon, "'commit' error for '%s': %s\n", device,
118858513bdeSJeff Cody                        strerror(-ret));
1189666daa68SMarkus Armbruster     }
1190666daa68SMarkus Armbruster }
1191666daa68SMarkus Armbruster 
11926a8f9661SEric Blake static void blockdev_do_action(TransactionActionKind type, void *data,
11936a8f9661SEric Blake                                Error **errp)
11946cc2a415SPaolo Bonzini {
1195c8a83e85SKevin Wolf     TransactionAction action;
1196c8a83e85SKevin Wolf     TransactionActionList list;
11976cc2a415SPaolo Bonzini 
11986a8f9661SEric Blake     action.type = type;
11996a8f9661SEric Blake     action.u.data = data;
12006cc2a415SPaolo Bonzini     list.value = &action;
12016cc2a415SPaolo Bonzini     list.next = NULL;
120294d16a64SJohn Snow     qmp_transaction(&list, false, NULL, errp);
12036cc2a415SPaolo Bonzini }
12046cc2a415SPaolo Bonzini 
12050901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
12060901f67eSBenoît Canet                                 bool has_node_name, const char *node_name,
12070901f67eSBenoît Canet                                 const char *snapshot_file,
12080901f67eSBenoît Canet                                 bool has_snapshot_node_name,
12090901f67eSBenoît Canet                                 const char *snapshot_node_name,
12106106e249SLuiz Capitulino                                 bool has_format, const char *format,
12110901f67eSBenoît Canet                                 bool has_mode, NewImageMode mode, Error **errp)
1212f8882568SJes Sorensen {
1213a911e6aeSAlberto Garcia     BlockdevSnapshotSync snapshot = {
12140901f67eSBenoît Canet         .has_device = has_device,
12156cc2a415SPaolo Bonzini         .device = (char *) device,
12160901f67eSBenoît Canet         .has_node_name = has_node_name,
12170901f67eSBenoît Canet         .node_name = (char *) node_name,
12186cc2a415SPaolo Bonzini         .snapshot_file = (char *) snapshot_file,
12190901f67eSBenoît Canet         .has_snapshot_node_name = has_snapshot_node_name,
12200901f67eSBenoît Canet         .snapshot_node_name = (char *) snapshot_node_name,
12216cc2a415SPaolo Bonzini         .has_format = has_format,
12226cc2a415SPaolo Bonzini         .format = (char *) format,
12236cc2a415SPaolo Bonzini         .has_mode = has_mode,
12246cc2a415SPaolo Bonzini         .mode = mode,
12256cc2a415SPaolo Bonzini     };
1226c8a83e85SKevin Wolf     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1227c8a83e85SKevin Wolf                        &snapshot, errp);
1228f8882568SJes Sorensen }
1229f8882568SJes Sorensen 
123043de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay,
123143de7e2dSAlberto Garcia                            Error **errp)
123243de7e2dSAlberto Garcia {
123343de7e2dSAlberto Garcia     BlockdevSnapshot snapshot_data = {
123443de7e2dSAlberto Garcia         .node = (char *) node,
123543de7e2dSAlberto Garcia         .overlay = (char *) overlay
123643de7e2dSAlberto Garcia     };
123743de7e2dSAlberto Garcia 
123843de7e2dSAlberto Garcia     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
123943de7e2dSAlberto Garcia                        &snapshot_data, errp);
124043de7e2dSAlberto Garcia }
124143de7e2dSAlberto Garcia 
1242f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device,
1243f323bc9eSWenchao Xia                                          const char *name,
1244f323bc9eSWenchao Xia                                          Error **errp)
1245f323bc9eSWenchao Xia {
1246f323bc9eSWenchao Xia     BlockdevSnapshotInternal snapshot = {
1247f323bc9eSWenchao Xia         .device = (char *) device,
1248f323bc9eSWenchao Xia         .name = (char *) name
1249f323bc9eSWenchao Xia     };
1250f323bc9eSWenchao Xia 
1251f323bc9eSWenchao Xia     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1252f323bc9eSWenchao Xia                        &snapshot, errp);
1253f323bc9eSWenchao Xia }
1254f323bc9eSWenchao Xia 
125544e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
125644e3e053SWenchao Xia                                                          bool has_id,
125744e3e053SWenchao Xia                                                          const char *id,
125844e3e053SWenchao Xia                                                          bool has_name,
125944e3e053SWenchao Xia                                                          const char *name,
126044e3e053SWenchao Xia                                                          Error **errp)
126144e3e053SWenchao Xia {
1262a0e8544cSFam Zheng     BlockDriverState *bs;
1263a0e8544cSFam Zheng     BlockBackend *blk;
12644ef3982aSStefan Hajnoczi     AioContext *aio_context;
126544e3e053SWenchao Xia     QEMUSnapshotInfo sn;
126644e3e053SWenchao Xia     Error *local_err = NULL;
126744e3e053SWenchao Xia     SnapshotInfo *info = NULL;
126844e3e053SWenchao Xia     int ret;
126944e3e053SWenchao Xia 
1270a0e8544cSFam Zheng     blk = blk_by_name(device);
1271a0e8544cSFam Zheng     if (!blk) {
127275158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
127375158ebbSMarkus Armbruster                   "Device '%s' not found", device);
127444e3e053SWenchao Xia         return NULL;
127544e3e053SWenchao Xia     }
12765433c24fSMax Reitz 
12775433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
12785433c24fSMax Reitz     aio_context_acquire(aio_context);
127944e3e053SWenchao Xia 
128044e3e053SWenchao Xia     if (!has_id) {
128144e3e053SWenchao Xia         id = NULL;
128244e3e053SWenchao Xia     }
128344e3e053SWenchao Xia 
128444e3e053SWenchao Xia     if (!has_name) {
128544e3e053SWenchao Xia         name = NULL;
128644e3e053SWenchao Xia     }
128744e3e053SWenchao Xia 
128844e3e053SWenchao Xia     if (!id && !name) {
128944e3e053SWenchao Xia         error_setg(errp, "Name or id must be provided");
12905433c24fSMax Reitz         goto out_aio_context;
129144e3e053SWenchao Xia     }
129244e3e053SWenchao Xia 
12935433c24fSMax Reitz     if (!blk_is_available(blk)) {
12945433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
12955433c24fSMax Reitz         goto out_aio_context;
12965433c24fSMax Reitz     }
12975433c24fSMax Reitz     bs = blk_bs(blk);
12984ef3982aSStefan Hajnoczi 
12990b928854SStefan Hajnoczi     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
13000b928854SStefan Hajnoczi         goto out_aio_context;
13010b928854SStefan Hajnoczi     }
13020b928854SStefan Hajnoczi 
130344e3e053SWenchao Xia     ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
130484d18f06SMarkus Armbruster     if (local_err) {
130544e3e053SWenchao Xia         error_propagate(errp, local_err);
13064ef3982aSStefan Hajnoczi         goto out_aio_context;
130744e3e053SWenchao Xia     }
130844e3e053SWenchao Xia     if (!ret) {
130944e3e053SWenchao Xia         error_setg(errp,
131044e3e053SWenchao Xia                    "Snapshot with id '%s' and name '%s' does not exist on "
131144e3e053SWenchao Xia                    "device '%s'",
131244e3e053SWenchao Xia                    STR_OR_NULL(id), STR_OR_NULL(name), device);
13134ef3982aSStefan Hajnoczi         goto out_aio_context;
131444e3e053SWenchao Xia     }
131544e3e053SWenchao Xia 
131644e3e053SWenchao Xia     bdrv_snapshot_delete(bs, id, name, &local_err);
131784d18f06SMarkus Armbruster     if (local_err) {
131844e3e053SWenchao Xia         error_propagate(errp, local_err);
13194ef3982aSStefan Hajnoczi         goto out_aio_context;
132044e3e053SWenchao Xia     }
132144e3e053SWenchao Xia 
13224ef3982aSStefan Hajnoczi     aio_context_release(aio_context);
13234ef3982aSStefan Hajnoczi 
13245839e53bSMarkus Armbruster     info = g_new0(SnapshotInfo, 1);
132544e3e053SWenchao Xia     info->id = g_strdup(sn.id_str);
132644e3e053SWenchao Xia     info->name = g_strdup(sn.name);
132744e3e053SWenchao Xia     info->date_nsec = sn.date_nsec;
132844e3e053SWenchao Xia     info->date_sec = sn.date_sec;
132944e3e053SWenchao Xia     info->vm_state_size = sn.vm_state_size;
133044e3e053SWenchao Xia     info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
133144e3e053SWenchao Xia     info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
133244e3e053SWenchao Xia 
133344e3e053SWenchao Xia     return info;
13344ef3982aSStefan Hajnoczi 
13354ef3982aSStefan Hajnoczi out_aio_context:
13364ef3982aSStefan Hajnoczi     aio_context_release(aio_context);
13374ef3982aSStefan Hajnoczi     return NULL;
133844e3e053SWenchao Xia }
13398802d1fdSJeff Cody 
1340341ebc2fSJohn Snow /**
1341341ebc2fSJohn Snow  * block_dirty_bitmap_lookup:
1342341ebc2fSJohn Snow  * Return a dirty bitmap (if present), after validating
1343341ebc2fSJohn Snow  * the node reference and bitmap names.
1344341ebc2fSJohn Snow  *
1345341ebc2fSJohn Snow  * @node: The name of the BDS node to search for bitmaps
1346341ebc2fSJohn Snow  * @name: The name of the bitmap to search for
1347341ebc2fSJohn Snow  * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1348341ebc2fSJohn Snow  * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1349341ebc2fSJohn Snow  * @errp: Output pointer for error information. Can be NULL.
1350341ebc2fSJohn Snow  *
1351341ebc2fSJohn Snow  * @return: A bitmap object on success, or NULL on failure.
1352341ebc2fSJohn Snow  */
1353341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1354341ebc2fSJohn Snow                                                   const char *name,
1355341ebc2fSJohn Snow                                                   BlockDriverState **pbs,
1356341ebc2fSJohn Snow                                                   AioContext **paio,
1357341ebc2fSJohn Snow                                                   Error **errp)
1358341ebc2fSJohn Snow {
1359341ebc2fSJohn Snow     BlockDriverState *bs;
1360341ebc2fSJohn Snow     BdrvDirtyBitmap *bitmap;
1361341ebc2fSJohn Snow     AioContext *aio_context;
1362341ebc2fSJohn Snow 
1363341ebc2fSJohn Snow     if (!node) {
1364341ebc2fSJohn Snow         error_setg(errp, "Node cannot be NULL");
1365341ebc2fSJohn Snow         return NULL;
1366341ebc2fSJohn Snow     }
1367341ebc2fSJohn Snow     if (!name) {
1368341ebc2fSJohn Snow         error_setg(errp, "Bitmap name cannot be NULL");
1369341ebc2fSJohn Snow         return NULL;
1370341ebc2fSJohn Snow     }
1371341ebc2fSJohn Snow     bs = bdrv_lookup_bs(node, node, NULL);
1372341ebc2fSJohn Snow     if (!bs) {
1373341ebc2fSJohn Snow         error_setg(errp, "Node '%s' not found", node);
1374341ebc2fSJohn Snow         return NULL;
1375341ebc2fSJohn Snow     }
1376341ebc2fSJohn Snow 
1377341ebc2fSJohn Snow     aio_context = bdrv_get_aio_context(bs);
1378341ebc2fSJohn Snow     aio_context_acquire(aio_context);
1379341ebc2fSJohn Snow 
1380341ebc2fSJohn Snow     bitmap = bdrv_find_dirty_bitmap(bs, name);
1381341ebc2fSJohn Snow     if (!bitmap) {
1382341ebc2fSJohn Snow         error_setg(errp, "Dirty bitmap '%s' not found", name);
1383341ebc2fSJohn Snow         goto fail;
1384341ebc2fSJohn Snow     }
1385341ebc2fSJohn Snow 
1386341ebc2fSJohn Snow     if (pbs) {
1387341ebc2fSJohn Snow         *pbs = bs;
1388341ebc2fSJohn Snow     }
1389341ebc2fSJohn Snow     if (paio) {
1390341ebc2fSJohn Snow         *paio = aio_context;
1391341ebc2fSJohn Snow     } else {
1392341ebc2fSJohn Snow         aio_context_release(aio_context);
1393341ebc2fSJohn Snow     }
1394341ebc2fSJohn Snow 
1395341ebc2fSJohn Snow     return bitmap;
1396341ebc2fSJohn Snow 
1397341ebc2fSJohn Snow  fail:
1398341ebc2fSJohn Snow     aio_context_release(aio_context);
1399341ebc2fSJohn Snow     return NULL;
1400341ebc2fSJohn Snow }
1401341ebc2fSJohn Snow 
1402b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */
1403ba0c86a3SWenchao Xia 
140450f43f0fSJohn Snow typedef struct BlkActionState BlkActionState;
1405ba0c86a3SWenchao Xia 
140650f43f0fSJohn Snow /**
140750f43f0fSJohn Snow  * BlkActionOps:
140850f43f0fSJohn Snow  * Table of operations that define an Action.
140950f43f0fSJohn Snow  *
141050f43f0fSJohn Snow  * @instance_size: Size of state struct, in bytes.
141150f43f0fSJohn Snow  * @prepare: Prepare the work, must NOT be NULL.
141250f43f0fSJohn Snow  * @commit: Commit the changes, can be NULL.
141350f43f0fSJohn Snow  * @abort: Abort the changes on fail, can be NULL.
141450f43f0fSJohn Snow  * @clean: Clean up resources after all transaction actions have called
141550f43f0fSJohn Snow  *         commit() or abort(). Can be NULL.
141650f43f0fSJohn Snow  *
141750f43f0fSJohn Snow  * Only prepare() may fail. In a single transaction, only one of commit() or
141850f43f0fSJohn Snow  * abort() will be called. clean() will always be called if it is present.
1419ba0c86a3SWenchao Xia  */
142050f43f0fSJohn Snow typedef struct BlkActionOps {
142150f43f0fSJohn Snow     size_t instance_size;
142250f43f0fSJohn Snow     void (*prepare)(BlkActionState *common, Error **errp);
142350f43f0fSJohn Snow     void (*commit)(BlkActionState *common);
142450f43f0fSJohn Snow     void (*abort)(BlkActionState *common);
142550f43f0fSJohn Snow     void (*clean)(BlkActionState *common);
142650f43f0fSJohn Snow } BlkActionOps;
142750f43f0fSJohn Snow 
142850f43f0fSJohn Snow /**
142950f43f0fSJohn Snow  * BlkActionState:
143050f43f0fSJohn Snow  * Describes one Action's state within a Transaction.
143150f43f0fSJohn Snow  *
143250f43f0fSJohn Snow  * @action: QAPI-defined enum identifying which Action to perform.
143350f43f0fSJohn Snow  * @ops: Table of ActionOps this Action can perform.
143494d16a64SJohn Snow  * @block_job_txn: Transaction which this action belongs to.
143550f43f0fSJohn Snow  * @entry: List membership for all Actions in this Transaction.
143650f43f0fSJohn Snow  *
143750f43f0fSJohn Snow  * This structure must be arranged as first member in a subclassed type,
143850f43f0fSJohn Snow  * assuming that the compiler will also arrange it to the same offsets as the
143950f43f0fSJohn Snow  * base class.
144050f43f0fSJohn Snow  */
144150f43f0fSJohn Snow struct BlkActionState {
1442c8a83e85SKevin Wolf     TransactionAction *action;
144350f43f0fSJohn Snow     const BlkActionOps *ops;
144494d16a64SJohn Snow     BlockJobTxn *block_job_txn;
144594d16a64SJohn Snow     TransactionProperties *txn_props;
144650f43f0fSJohn Snow     QSIMPLEQ_ENTRY(BlkActionState) entry;
1447ba0c86a3SWenchao Xia };
1448ba0c86a3SWenchao Xia 
1449bbe86010SWenchao Xia /* internal snapshot private data */
1450bbe86010SWenchao Xia typedef struct InternalSnapshotState {
145150f43f0fSJohn Snow     BlkActionState common;
1452bbe86010SWenchao Xia     BlockDriverState *bs;
14535d6e96efSStefan Hajnoczi     AioContext *aio_context;
1454bbe86010SWenchao Xia     QEMUSnapshotInfo sn;
1455507306ccSFam Zheng     bool created;
1456bbe86010SWenchao Xia } InternalSnapshotState;
1457bbe86010SWenchao Xia 
145894d16a64SJohn Snow 
145994d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp)
146094d16a64SJohn Snow {
146194d16a64SJohn Snow     if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
146294d16a64SJohn Snow         error_setg(errp,
146394d16a64SJohn Snow                    "Action '%s' does not support Transaction property "
146494d16a64SJohn Snow                    "completion-mode = %s",
146594d16a64SJohn Snow                    TransactionActionKind_lookup[s->action->type],
146694d16a64SJohn Snow                    ActionCompletionMode_lookup[s->txn_props->completion_mode]);
146794d16a64SJohn Snow         return -1;
146894d16a64SJohn Snow     }
146994d16a64SJohn Snow     return 0;
147094d16a64SJohn Snow }
147194d16a64SJohn Snow 
147250f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common,
1473bbe86010SWenchao Xia                                       Error **errp)
1474bbe86010SWenchao Xia {
1475f70edf99SMarkus Armbruster     Error *local_err = NULL;
1476bbe86010SWenchao Xia     const char *device;
1477bbe86010SWenchao Xia     const char *name;
1478a0e8544cSFam Zheng     BlockBackend *blk;
1479bbe86010SWenchao Xia     BlockDriverState *bs;
1480bbe86010SWenchao Xia     QEMUSnapshotInfo old_sn, *sn;
1481bbe86010SWenchao Xia     bool ret;
1482bbe86010SWenchao Xia     qemu_timeval tv;
1483bbe86010SWenchao Xia     BlockdevSnapshotInternal *internal;
1484bbe86010SWenchao Xia     InternalSnapshotState *state;
1485bbe86010SWenchao Xia     int ret1;
1486bbe86010SWenchao Xia 
14876a8f9661SEric Blake     g_assert(common->action->type ==
1488bbe86010SWenchao Xia              TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
14896a8f9661SEric Blake     internal = common->action->u.blockdev_snapshot_internal_sync;
1490bbe86010SWenchao Xia     state = DO_UPCAST(InternalSnapshotState, common, common);
1491bbe86010SWenchao Xia 
1492bbe86010SWenchao Xia     /* 1. parse input */
1493bbe86010SWenchao Xia     device = internal->device;
1494bbe86010SWenchao Xia     name = internal->name;
1495bbe86010SWenchao Xia 
1496bbe86010SWenchao Xia     /* 2. check for validation */
149794d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
149894d16a64SJohn Snow         return;
149994d16a64SJohn Snow     }
150094d16a64SJohn Snow 
1501a0e8544cSFam Zheng     blk = blk_by_name(device);
1502a0e8544cSFam Zheng     if (!blk) {
150375158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
150475158ebbSMarkus Armbruster                   "Device '%s' not found", device);
1505bbe86010SWenchao Xia         return;
1506bbe86010SWenchao Xia     }
1507bbe86010SWenchao Xia 
15085d6e96efSStefan Hajnoczi     /* AioContext is released in .clean() */
15095433c24fSMax Reitz     state->aio_context = blk_get_aio_context(blk);
15105d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
15115d6e96efSStefan Hajnoczi 
15125433c24fSMax Reitz     if (!blk_is_available(blk)) {
1513c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1514bbe86010SWenchao Xia         return;
1515bbe86010SWenchao Xia     }
15165433c24fSMax Reitz     bs = blk_bs(blk);
1517bbe86010SWenchao Xia 
1518507306ccSFam Zheng     state->bs = bs;
1519507306ccSFam Zheng     bdrv_drained_begin(bs);
1520507306ccSFam Zheng 
15213dc7ca3cSStefan Hajnoczi     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
15223dc7ca3cSStefan Hajnoczi         return;
15233dc7ca3cSStefan Hajnoczi     }
15243dc7ca3cSStefan Hajnoczi 
1525bbe86010SWenchao Xia     if (bdrv_is_read_only(bs)) {
152681e5f78aSAlberto Garcia         error_setg(errp, "Device '%s' is read only", device);
1527bbe86010SWenchao Xia         return;
1528bbe86010SWenchao Xia     }
1529bbe86010SWenchao Xia 
1530bbe86010SWenchao Xia     if (!bdrv_can_snapshot(bs)) {
153181e5f78aSAlberto Garcia         error_setg(errp, "Block format '%s' used by device '%s' "
153281e5f78aSAlberto Garcia                    "does not support internal snapshots",
153381e5f78aSAlberto Garcia                    bs->drv->format_name, device);
1534bbe86010SWenchao Xia         return;
1535bbe86010SWenchao Xia     }
1536bbe86010SWenchao Xia 
1537bbe86010SWenchao Xia     if (!strlen(name)) {
1538bbe86010SWenchao Xia         error_setg(errp, "Name is empty");
1539bbe86010SWenchao Xia         return;
1540bbe86010SWenchao Xia     }
1541bbe86010SWenchao Xia 
1542bbe86010SWenchao Xia     /* check whether a snapshot with name exist */
1543f70edf99SMarkus Armbruster     ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1544f70edf99SMarkus Armbruster                                             &local_err);
1545f70edf99SMarkus Armbruster     if (local_err) {
1546f70edf99SMarkus Armbruster         error_propagate(errp, local_err);
1547bbe86010SWenchao Xia         return;
1548bbe86010SWenchao Xia     } else if (ret) {
1549bbe86010SWenchao Xia         error_setg(errp,
1550bbe86010SWenchao Xia                    "Snapshot with name '%s' already exists on device '%s'",
1551bbe86010SWenchao Xia                    name, device);
1552bbe86010SWenchao Xia         return;
1553bbe86010SWenchao Xia     }
1554bbe86010SWenchao Xia 
1555bbe86010SWenchao Xia     /* 3. take the snapshot */
1556bbe86010SWenchao Xia     sn = &state->sn;
1557bbe86010SWenchao Xia     pstrcpy(sn->name, sizeof(sn->name), name);
1558bbe86010SWenchao Xia     qemu_gettimeofday(&tv);
1559bbe86010SWenchao Xia     sn->date_sec = tv.tv_sec;
1560bbe86010SWenchao Xia     sn->date_nsec = tv.tv_usec * 1000;
1561bbe86010SWenchao Xia     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1562bbe86010SWenchao Xia 
1563bbe86010SWenchao Xia     ret1 = bdrv_snapshot_create(bs, sn);
1564bbe86010SWenchao Xia     if (ret1 < 0) {
1565bbe86010SWenchao Xia         error_setg_errno(errp, -ret1,
1566bbe86010SWenchao Xia                          "Failed to create snapshot '%s' on device '%s'",
1567bbe86010SWenchao Xia                          name, device);
1568bbe86010SWenchao Xia         return;
1569bbe86010SWenchao Xia     }
1570bbe86010SWenchao Xia 
1571bbe86010SWenchao Xia     /* 4. succeed, mark a snapshot is created */
1572507306ccSFam Zheng     state->created = true;
1573bbe86010SWenchao Xia }
1574bbe86010SWenchao Xia 
157550f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common)
1576bbe86010SWenchao Xia {
1577bbe86010SWenchao Xia     InternalSnapshotState *state =
1578bbe86010SWenchao Xia                              DO_UPCAST(InternalSnapshotState, common, common);
1579bbe86010SWenchao Xia     BlockDriverState *bs = state->bs;
1580bbe86010SWenchao Xia     QEMUSnapshotInfo *sn = &state->sn;
1581bbe86010SWenchao Xia     Error *local_error = NULL;
1582bbe86010SWenchao Xia 
1583507306ccSFam Zheng     if (!state->created) {
1584bbe86010SWenchao Xia         return;
1585bbe86010SWenchao Xia     }
1586bbe86010SWenchao Xia 
1587bbe86010SWenchao Xia     if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1588c29b77f9SMarkus Armbruster         error_reportf_err(local_error,
1589c29b77f9SMarkus Armbruster                           "Failed to delete snapshot with id '%s' and "
1590c29b77f9SMarkus Armbruster                           "name '%s' on device '%s' in abort: ",
1591c29b77f9SMarkus Armbruster                           sn->id_str, sn->name,
1592c29b77f9SMarkus Armbruster                           bdrv_get_device_name(bs));
1593bbe86010SWenchao Xia     }
1594bbe86010SWenchao Xia }
1595bbe86010SWenchao Xia 
159650f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common)
15975d6e96efSStefan Hajnoczi {
15985d6e96efSStefan Hajnoczi     InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
15995d6e96efSStefan Hajnoczi                                              common, common);
16005d6e96efSStefan Hajnoczi 
16015d6e96efSStefan Hajnoczi     if (state->aio_context) {
1602507306ccSFam Zheng         if (state->bs) {
1603507306ccSFam Zheng             bdrv_drained_end(state->bs);
1604507306ccSFam Zheng         }
16055d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
16065d6e96efSStefan Hajnoczi     }
16075d6e96efSStefan Hajnoczi }
16085d6e96efSStefan Hajnoczi 
1609ba0c86a3SWenchao Xia /* external snapshot private data */
1610ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState {
161150f43f0fSJohn Snow     BlkActionState common;
16128802d1fdSJeff Cody     BlockDriverState *old_bs;
16138802d1fdSJeff Cody     BlockDriverState *new_bs;
16145d6e96efSStefan Hajnoczi     AioContext *aio_context;
1615ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState;
16168802d1fdSJeff Cody 
161750f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common,
16189b9877eeSWenchao Xia                                       Error **errp)
16199b9877eeSWenchao Xia {
162043de7e2dSAlberto Garcia     int flags = 0, ret;
162143de7e2dSAlberto Garcia     QDict *options = NULL;
16229b9877eeSWenchao Xia     Error *local_err = NULL;
162343de7e2dSAlberto Garcia     /* Device and node name of the image to generate the snapshot from */
1624e2a31e87SWenchao Xia     const char *device;
16250901f67eSBenoît Canet     const char *node_name;
162643de7e2dSAlberto Garcia     /* Reference to the new image (for 'blockdev-snapshot') */
162743de7e2dSAlberto Garcia     const char *snapshot_ref;
162843de7e2dSAlberto Garcia     /* File name of the new image (for 'blockdev-snapshot-sync') */
1629e2a31e87SWenchao Xia     const char *new_image_file;
1630ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1631ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1632c8a83e85SKevin Wolf     TransactionAction *action = common->action;
16339b9877eeSWenchao Xia 
163443de7e2dSAlberto Garcia     /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
163543de7e2dSAlberto Garcia      * purpose but a different set of parameters */
163643de7e2dSAlberto Garcia     switch (action->type) {
163743de7e2dSAlberto Garcia     case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
163843de7e2dSAlberto Garcia         {
163943de7e2dSAlberto Garcia             BlockdevSnapshot *s = action->u.blockdev_snapshot;
164043de7e2dSAlberto Garcia             device = s->node;
164143de7e2dSAlberto Garcia             node_name = s->node;
164243de7e2dSAlberto Garcia             new_image_file = NULL;
164343de7e2dSAlberto Garcia             snapshot_ref = s->overlay;
1644e2a31e87SWenchao Xia         }
164543de7e2dSAlberto Garcia         break;
164643de7e2dSAlberto Garcia     case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
164743de7e2dSAlberto Garcia         {
164843de7e2dSAlberto Garcia             BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
164943de7e2dSAlberto Garcia             device = s->has_device ? s->device : NULL;
165043de7e2dSAlberto Garcia             node_name = s->has_node_name ? s->node_name : NULL;
165143de7e2dSAlberto Garcia             new_image_file = s->snapshot_file;
165243de7e2dSAlberto Garcia             snapshot_ref = NULL;
165343de7e2dSAlberto Garcia         }
165443de7e2dSAlberto Garcia         break;
165543de7e2dSAlberto Garcia     default:
165643de7e2dSAlberto Garcia         g_assert_not_reached();
1657e2a31e87SWenchao Xia     }
1658e2a31e87SWenchao Xia 
1659e2a31e87SWenchao Xia     /* start processing */
166094d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
166194d16a64SJohn Snow         return;
166294d16a64SJohn Snow     }
166394d16a64SJohn Snow 
166443de7e2dSAlberto Garcia     state->old_bs = bdrv_lookup_bs(device, node_name, errp);
166543de7e2dSAlberto Garcia     if (!state->old_bs) {
16669b9877eeSWenchao Xia         return;
16679b9877eeSWenchao Xia     }
16689b9877eeSWenchao Xia 
16695d6e96efSStefan Hajnoczi     /* Acquire AioContext now so any threads operating on old_bs stop */
16705d6e96efSStefan Hajnoczi     state->aio_context = bdrv_get_aio_context(state->old_bs);
16715d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
1672da763e83SFam Zheng     bdrv_drained_begin(state->old_bs);
16735d6e96efSStefan Hajnoczi 
1674ba5d6ab6SStefan Hajnoczi     if (!bdrv_is_inserted(state->old_bs)) {
1675c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
16769b9877eeSWenchao Xia         return;
16779b9877eeSWenchao Xia     }
16789b9877eeSWenchao Xia 
16793718d8abSFam Zheng     if (bdrv_op_is_blocked(state->old_bs,
16803718d8abSFam Zheng                            BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
16819b9877eeSWenchao Xia         return;
16829b9877eeSWenchao Xia     }
16839b9877eeSWenchao Xia 
1684ba5d6ab6SStefan Hajnoczi     if (!bdrv_is_read_only(state->old_bs)) {
1685ba5d6ab6SStefan Hajnoczi         if (bdrv_flush(state->old_bs)) {
1686c6bd8c70SMarkus Armbruster             error_setg(errp, QERR_IO_ERROR);
16879b9877eeSWenchao Xia             return;
16889b9877eeSWenchao Xia         }
16899b9877eeSWenchao Xia     }
16909b9877eeSWenchao Xia 
1691212a5a8fSBenoît Canet     if (!bdrv_is_first_non_filter(state->old_bs)) {
1692c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1693f6186f49SBenoît Canet         return;
1694f6186f49SBenoît Canet     }
1695f6186f49SBenoît Canet 
169643de7e2dSAlberto Garcia     if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
169743de7e2dSAlberto Garcia         BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
169843de7e2dSAlberto Garcia         const char *format = s->has_format ? s->format : "qcow2";
169943de7e2dSAlberto Garcia         enum NewImageMode mode;
170043de7e2dSAlberto Garcia         const char *snapshot_node_name =
170143de7e2dSAlberto Garcia             s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
170243de7e2dSAlberto Garcia 
170343de7e2dSAlberto Garcia         if (node_name && !snapshot_node_name) {
170443de7e2dSAlberto Garcia             error_setg(errp, "New snapshot node name missing");
170543de7e2dSAlberto Garcia             return;
170643de7e2dSAlberto Garcia         }
170743de7e2dSAlberto Garcia 
170843de7e2dSAlberto Garcia         if (snapshot_node_name &&
170943de7e2dSAlberto Garcia             bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
171043de7e2dSAlberto Garcia             error_setg(errp, "New snapshot node name already in use");
171143de7e2dSAlberto Garcia             return;
171243de7e2dSAlberto Garcia         }
171343de7e2dSAlberto Garcia 
1714ba5d6ab6SStefan Hajnoczi         flags = state->old_bs->open_flags;
17159b9877eeSWenchao Xia 
17169b9877eeSWenchao Xia         /* create new image w/backing file */
171743de7e2dSAlberto Garcia         mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
17189b9877eeSWenchao Xia         if (mode != NEW_IMAGE_MODE_EXISTING) {
17199b9877eeSWenchao Xia             bdrv_img_create(new_image_file, format,
1720ba5d6ab6SStefan Hajnoczi                             state->old_bs->filename,
1721ba5d6ab6SStefan Hajnoczi                             state->old_bs->drv->format_name,
17229b9877eeSWenchao Xia                             NULL, -1, flags, &local_err, false);
172384d18f06SMarkus Armbruster             if (local_err) {
17249b9877eeSWenchao Xia                 error_propagate(errp, local_err);
17259b9877eeSWenchao Xia                 return;
17269b9877eeSWenchao Xia             }
17279b9877eeSWenchao Xia         }
17289b9877eeSWenchao Xia 
17290901f67eSBenoît Canet         options = qdict_new();
173043de7e2dSAlberto Garcia         if (s->has_snapshot_node_name) {
17310901f67eSBenoît Canet             qdict_put(options, "node-name",
17320901f67eSBenoît Canet                       qstring_from_str(snapshot_node_name));
17330901f67eSBenoît Canet         }
1734e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
17350901f67eSBenoît Canet 
173643de7e2dSAlberto Garcia         flags |= BDRV_O_NO_BACKING;
173743de7e2dSAlberto Garcia     }
173843de7e2dSAlberto Garcia 
1739f67503e5SMax Reitz     assert(state->new_bs == NULL);
174043de7e2dSAlberto Garcia     ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options,
174143de7e2dSAlberto Garcia                     flags, errp);
1742f67503e5SMax Reitz     /* We will manually add the backing_hd field to the bs later */
17439b9877eeSWenchao Xia     if (ret != 0) {
174443de7e2dSAlberto Garcia         return;
174543de7e2dSAlberto Garcia     }
174643de7e2dSAlberto Garcia 
174743de7e2dSAlberto Garcia     if (state->new_bs->blk != NULL) {
174843de7e2dSAlberto Garcia         error_setg(errp, "The snapshot is already in use by %s",
174943de7e2dSAlberto Garcia                    blk_name(state->new_bs->blk));
175043de7e2dSAlberto Garcia         return;
175143de7e2dSAlberto Garcia     }
175243de7e2dSAlberto Garcia 
175343de7e2dSAlberto Garcia     if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
175443de7e2dSAlberto Garcia                            errp)) {
175543de7e2dSAlberto Garcia         return;
175643de7e2dSAlberto Garcia     }
175743de7e2dSAlberto Garcia 
175843de7e2dSAlberto Garcia     if (state->new_bs->backing != NULL) {
175943de7e2dSAlberto Garcia         error_setg(errp, "The snapshot already has a backing image");
176008b24cfeSAlberto Garcia         return;
176108b24cfeSAlberto Garcia     }
176208b24cfeSAlberto Garcia 
176308b24cfeSAlberto Garcia     if (!state->new_bs->drv->supports_backing) {
176408b24cfeSAlberto Garcia         error_setg(errp, "The snapshot does not support backing images");
17659b9877eeSWenchao Xia     }
17669b9877eeSWenchao Xia }
17679b9877eeSWenchao Xia 
176850f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common)
17693b0047e8SWenchao Xia {
1770ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1771ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1772ba0c86a3SWenchao Xia 
17735d6e96efSStefan Hajnoczi     bdrv_set_aio_context(state->new_bs, state->aio_context);
17745d6e96efSStefan Hajnoczi 
1775ba5d6ab6SStefan Hajnoczi     /* This removes our old bs and adds the new bs */
1776ba5d6ab6SStefan Hajnoczi     bdrv_append(state->new_bs, state->old_bs);
17773b0047e8SWenchao Xia     /* We don't need (or want) to use the transactional
17783b0047e8SWenchao Xia      * bdrv_reopen_multiple() across all the entries at once, because we
17793b0047e8SWenchao Xia      * don't want to abort all of them if one of them fails the reopen */
1780dd62f1caSKevin Wolf     bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
17813b0047e8SWenchao Xia                 NULL);
17823b0047e8SWenchao Xia }
17833b0047e8SWenchao Xia 
178450f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common)
178596b86bf7SWenchao Xia {
1786ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1787ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1788ba5d6ab6SStefan Hajnoczi     if (state->new_bs) {
17894f6fd349SFam Zheng         bdrv_unref(state->new_bs);
179096b86bf7SWenchao Xia     }
1791da763e83SFam Zheng }
1792da763e83SFam Zheng 
179350f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common)
1794da763e83SFam Zheng {
1795da763e83SFam Zheng     ExternalSnapshotState *state =
1796da763e83SFam Zheng                              DO_UPCAST(ExternalSnapshotState, common, common);
17975d6e96efSStefan Hajnoczi     if (state->aio_context) {
1798da763e83SFam Zheng         bdrv_drained_end(state->old_bs);
17995d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
18005d6e96efSStefan Hajnoczi     }
180196b86bf7SWenchao Xia }
180296b86bf7SWenchao Xia 
18033037f364SStefan Hajnoczi typedef struct DriveBackupState {
180450f43f0fSJohn Snow     BlkActionState common;
18053037f364SStefan Hajnoczi     BlockDriverState *bs;
18065d6e96efSStefan Hajnoczi     AioContext *aio_context;
18073037f364SStefan Hajnoczi     BlockJob *job;
18083037f364SStefan Hajnoczi } DriveBackupState;
18093037f364SStefan Hajnoczi 
181078f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target,
181178f51fdeSJohn Snow                             bool has_format, const char *format,
181278f51fdeSJohn Snow                             enum MirrorSyncMode sync,
181378f51fdeSJohn Snow                             bool has_mode, enum NewImageMode mode,
181478f51fdeSJohn Snow                             bool has_speed, int64_t speed,
181578f51fdeSJohn Snow                             bool has_bitmap, const char *bitmap,
181678f51fdeSJohn Snow                             bool has_on_source_error,
181778f51fdeSJohn Snow                             BlockdevOnError on_source_error,
181878f51fdeSJohn Snow                             bool has_on_target_error,
181978f51fdeSJohn Snow                             BlockdevOnError on_target_error,
182078f51fdeSJohn Snow                             BlockJobTxn *txn, Error **errp);
182178f51fdeSJohn Snow 
182250f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp)
18233037f364SStefan Hajnoczi {
18243037f364SStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1825a0e8544cSFam Zheng     BlockBackend *blk;
18263037f364SStefan Hajnoczi     DriveBackup *backup;
18273037f364SStefan Hajnoczi     Error *local_err = NULL;
18283037f364SStefan Hajnoczi 
18296a8f9661SEric Blake     assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
18306a8f9661SEric Blake     backup = common->action->u.drive_backup;
18313037f364SStefan Hajnoczi 
1832a0e8544cSFam Zheng     blk = blk_by_name(backup->device);
1833a0e8544cSFam Zheng     if (!blk) {
183475158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
183575158ebbSMarkus Armbruster                   "Device '%s' not found", backup->device);
18365d6e96efSStefan Hajnoczi         return;
18375d6e96efSStefan Hajnoczi     }
18385d6e96efSStefan Hajnoczi 
18391fdd4b7bSFam Zheng     if (!blk_is_available(blk)) {
18401fdd4b7bSFam Zheng         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
18411fdd4b7bSFam Zheng         return;
18421fdd4b7bSFam Zheng     }
18431fdd4b7bSFam Zheng 
18445d6e96efSStefan Hajnoczi     /* AioContext is released in .clean() */
18455433c24fSMax Reitz     state->aio_context = blk_get_aio_context(blk);
18465d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
18471fdd4b7bSFam Zheng     bdrv_drained_begin(blk_bs(blk));
18481fdd4b7bSFam Zheng     state->bs = blk_bs(blk);
18495d6e96efSStefan Hajnoczi 
185078f51fdeSJohn Snow     do_drive_backup(backup->device, backup->target,
18513037f364SStefan Hajnoczi                     backup->has_format, backup->format,
1852b53169eaSStefan Hajnoczi                     backup->sync,
18533037f364SStefan Hajnoczi                     backup->has_mode, backup->mode,
18543037f364SStefan Hajnoczi                     backup->has_speed, backup->speed,
1855d58d8453SJohn Snow                     backup->has_bitmap, backup->bitmap,
18563037f364SStefan Hajnoczi                     backup->has_on_source_error, backup->on_source_error,
18573037f364SStefan Hajnoczi                     backup->has_on_target_error, backup->on_target_error,
185894d16a64SJohn Snow                     common->block_job_txn, &local_err);
185984d18f06SMarkus Armbruster     if (local_err) {
18603037f364SStefan Hajnoczi         error_propagate(errp, local_err);
18613037f364SStefan Hajnoczi         return;
18623037f364SStefan Hajnoczi     }
18633037f364SStefan Hajnoczi 
18643037f364SStefan Hajnoczi     state->job = state->bs->job;
18653037f364SStefan Hajnoczi }
18663037f364SStefan Hajnoczi 
186750f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common)
18683037f364SStefan Hajnoczi {
18693037f364SStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
18703037f364SStefan Hajnoczi     BlockDriverState *bs = state->bs;
18713037f364SStefan Hajnoczi 
18723037f364SStefan Hajnoczi     /* Only cancel if it's the job we started */
18733037f364SStefan Hajnoczi     if (bs && bs->job && bs->job == state->job) {
18743037f364SStefan Hajnoczi         block_job_cancel_sync(bs->job);
18753037f364SStefan Hajnoczi     }
18763037f364SStefan Hajnoczi }
18773037f364SStefan Hajnoczi 
187850f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common)
18795d6e96efSStefan Hajnoczi {
18805d6e96efSStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
18815d6e96efSStefan Hajnoczi 
18825d6e96efSStefan Hajnoczi     if (state->aio_context) {
18831fdd4b7bSFam Zheng         bdrv_drained_end(state->bs);
18845d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
18855d6e96efSStefan Hajnoczi     }
18865d6e96efSStefan Hajnoczi }
18875d6e96efSStefan Hajnoczi 
1888bd8baecdSFam Zheng typedef struct BlockdevBackupState {
188950f43f0fSJohn Snow     BlkActionState common;
1890bd8baecdSFam Zheng     BlockDriverState *bs;
1891bd8baecdSFam Zheng     BlockJob *job;
1892bd8baecdSFam Zheng     AioContext *aio_context;
1893bd8baecdSFam Zheng } BlockdevBackupState;
1894bd8baecdSFam Zheng 
189578f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target,
189678f51fdeSJohn Snow                                enum MirrorSyncMode sync,
189778f51fdeSJohn Snow                                bool has_speed, int64_t speed,
189878f51fdeSJohn Snow                                bool has_on_source_error,
189978f51fdeSJohn Snow                                BlockdevOnError on_source_error,
190078f51fdeSJohn Snow                                bool has_on_target_error,
190178f51fdeSJohn Snow                                BlockdevOnError on_target_error,
190278f51fdeSJohn Snow                                BlockJobTxn *txn, Error **errp);
190378f51fdeSJohn Snow 
190450f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1905bd8baecdSFam Zheng {
1906bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1907bd8baecdSFam Zheng     BlockdevBackup *backup;
19085433c24fSMax Reitz     BlockBackend *blk, *target;
1909bd8baecdSFam Zheng     Error *local_err = NULL;
1910bd8baecdSFam Zheng 
19116a8f9661SEric Blake     assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
19126a8f9661SEric Blake     backup = common->action->u.blockdev_backup;
1913bd8baecdSFam Zheng 
1914a0e8544cSFam Zheng     blk = blk_by_name(backup->device);
1915a0e8544cSFam Zheng     if (!blk) {
19165b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", backup->device);
1917bd8baecdSFam Zheng         return;
1918bd8baecdSFam Zheng     }
1919bd8baecdSFam Zheng 
1920ff52bf36SFam Zheng     if (!blk_is_available(blk)) {
1921ff52bf36SFam Zheng         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1922ff52bf36SFam Zheng         return;
1923ff52bf36SFam Zheng     }
1924ff52bf36SFam Zheng 
19255433c24fSMax Reitz     target = blk_by_name(backup->target);
19265433c24fSMax Reitz     if (!target) {
19275b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", backup->target);
1928bd8baecdSFam Zheng         return;
1929bd8baecdSFam Zheng     }
1930bd8baecdSFam Zheng 
1931bd8baecdSFam Zheng     /* AioContext is released in .clean() */
19325433c24fSMax Reitz     state->aio_context = blk_get_aio_context(blk);
19335433c24fSMax Reitz     if (state->aio_context != blk_get_aio_context(target)) {
1934bd8baecdSFam Zheng         state->aio_context = NULL;
1935bd8baecdSFam Zheng         error_setg(errp, "Backup between two IO threads is not implemented");
1936bd8baecdSFam Zheng         return;
1937bd8baecdSFam Zheng     }
1938bd8baecdSFam Zheng     aio_context_acquire(state->aio_context);
1939ff52bf36SFam Zheng     state->bs = blk_bs(blk);
1940ff52bf36SFam Zheng     bdrv_drained_begin(state->bs);
1941bd8baecdSFam Zheng 
194278f51fdeSJohn Snow     do_blockdev_backup(backup->device, backup->target,
1943bd8baecdSFam Zheng                        backup->sync,
1944bd8baecdSFam Zheng                        backup->has_speed, backup->speed,
1945bd8baecdSFam Zheng                        backup->has_on_source_error, backup->on_source_error,
1946bd8baecdSFam Zheng                        backup->has_on_target_error, backup->on_target_error,
194794d16a64SJohn Snow                        common->block_job_txn, &local_err);
1948bd8baecdSFam Zheng     if (local_err) {
1949bd8baecdSFam Zheng         error_propagate(errp, local_err);
1950bd8baecdSFam Zheng         return;
1951bd8baecdSFam Zheng     }
1952bd8baecdSFam Zheng 
1953bd8baecdSFam Zheng     state->job = state->bs->job;
1954bd8baecdSFam Zheng }
1955bd8baecdSFam Zheng 
195650f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common)
1957bd8baecdSFam Zheng {
1958bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1959bd8baecdSFam Zheng     BlockDriverState *bs = state->bs;
1960bd8baecdSFam Zheng 
1961bd8baecdSFam Zheng     /* Only cancel if it's the job we started */
1962bd8baecdSFam Zheng     if (bs && bs->job && bs->job == state->job) {
1963bd8baecdSFam Zheng         block_job_cancel_sync(bs->job);
1964bd8baecdSFam Zheng     }
1965bd8baecdSFam Zheng }
1966bd8baecdSFam Zheng 
196750f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common)
1968bd8baecdSFam Zheng {
1969bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1970bd8baecdSFam Zheng 
1971bd8baecdSFam Zheng     if (state->aio_context) {
1972ff52bf36SFam Zheng         bdrv_drained_end(state->bs);
1973bd8baecdSFam Zheng         aio_context_release(state->aio_context);
1974bd8baecdSFam Zheng     }
1975bd8baecdSFam Zheng }
1976bd8baecdSFam Zheng 
1977df9a681dSFam Zheng typedef struct BlockDirtyBitmapState {
197850f43f0fSJohn Snow     BlkActionState common;
1979df9a681dSFam Zheng     BdrvDirtyBitmap *bitmap;
1980df9a681dSFam Zheng     BlockDriverState *bs;
1981df9a681dSFam Zheng     AioContext *aio_context;
1982df9a681dSFam Zheng     HBitmap *backup;
1983df9a681dSFam Zheng     bool prepared;
1984df9a681dSFam Zheng } BlockDirtyBitmapState;
1985df9a681dSFam Zheng 
198650f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common,
1987df9a681dSFam Zheng                                            Error **errp)
1988df9a681dSFam Zheng {
1989df9a681dSFam Zheng     Error *local_err = NULL;
1990df9a681dSFam Zheng     BlockDirtyBitmapAdd *action;
1991df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1992df9a681dSFam Zheng                                              common, common);
1993df9a681dSFam Zheng 
199494d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
199594d16a64SJohn Snow         return;
199694d16a64SJohn Snow     }
199794d16a64SJohn Snow 
199850f43f0fSJohn Snow     action = common->action->u.block_dirty_bitmap_add;
1999df9a681dSFam Zheng     /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2000df9a681dSFam Zheng     qmp_block_dirty_bitmap_add(action->node, action->name,
2001df9a681dSFam Zheng                                action->has_granularity, action->granularity,
2002df9a681dSFam Zheng                                &local_err);
2003df9a681dSFam Zheng 
2004df9a681dSFam Zheng     if (!local_err) {
2005df9a681dSFam Zheng         state->prepared = true;
2006df9a681dSFam Zheng     } else {
2007df9a681dSFam Zheng         error_propagate(errp, local_err);
2008df9a681dSFam Zheng     }
2009df9a681dSFam Zheng }
2010df9a681dSFam Zheng 
201150f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common)
2012df9a681dSFam Zheng {
2013df9a681dSFam Zheng     BlockDirtyBitmapAdd *action;
2014df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2015df9a681dSFam Zheng                                              common, common);
2016df9a681dSFam Zheng 
201750f43f0fSJohn Snow     action = common->action->u.block_dirty_bitmap_add;
2018df9a681dSFam Zheng     /* Should not be able to fail: IF the bitmap was added via .prepare(),
2019df9a681dSFam Zheng      * then the node reference and bitmap name must have been valid.
2020df9a681dSFam Zheng      */
2021df9a681dSFam Zheng     if (state->prepared) {
2022df9a681dSFam Zheng         qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2023df9a681dSFam Zheng     }
2024df9a681dSFam Zheng }
2025df9a681dSFam Zheng 
202650f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
2027df9a681dSFam Zheng                                              Error **errp)
2028df9a681dSFam Zheng {
2029df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2030df9a681dSFam Zheng                                              common, common);
2031df9a681dSFam Zheng     BlockDirtyBitmap *action;
2032df9a681dSFam Zheng 
203394d16a64SJohn Snow     if (action_check_completion_mode(common, errp) < 0) {
203494d16a64SJohn Snow         return;
203594d16a64SJohn Snow     }
203694d16a64SJohn Snow 
203750f43f0fSJohn Snow     action = common->action->u.block_dirty_bitmap_clear;
2038df9a681dSFam Zheng     state->bitmap = block_dirty_bitmap_lookup(action->node,
2039df9a681dSFam Zheng                                               action->name,
2040df9a681dSFam Zheng                                               &state->bs,
2041df9a681dSFam Zheng                                               &state->aio_context,
2042df9a681dSFam Zheng                                               errp);
2043df9a681dSFam Zheng     if (!state->bitmap) {
2044df9a681dSFam Zheng         return;
2045df9a681dSFam Zheng     }
2046df9a681dSFam Zheng 
2047df9a681dSFam Zheng     if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2048df9a681dSFam Zheng         error_setg(errp, "Cannot modify a frozen bitmap");
2049df9a681dSFam Zheng         return;
2050df9a681dSFam Zheng     } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2051df9a681dSFam Zheng         error_setg(errp, "Cannot clear a disabled bitmap");
2052df9a681dSFam Zheng         return;
2053df9a681dSFam Zheng     }
2054df9a681dSFam Zheng 
2055df9a681dSFam Zheng     bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2056df9a681dSFam Zheng     /* AioContext is released in .clean() */
2057df9a681dSFam Zheng }
2058df9a681dSFam Zheng 
205950f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common)
2060df9a681dSFam Zheng {
2061df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2062df9a681dSFam Zheng                                              common, common);
2063df9a681dSFam Zheng 
2064df9a681dSFam Zheng     bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2065df9a681dSFam Zheng }
2066df9a681dSFam Zheng 
206750f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common)
2068df9a681dSFam Zheng {
2069df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2070df9a681dSFam Zheng                                              common, common);
2071df9a681dSFam Zheng 
2072df9a681dSFam Zheng     hbitmap_free(state->backup);
2073df9a681dSFam Zheng }
2074df9a681dSFam Zheng 
207550f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common)
2076df9a681dSFam Zheng {
2077df9a681dSFam Zheng     BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2078df9a681dSFam Zheng                                              common, common);
2079df9a681dSFam Zheng 
2080df9a681dSFam Zheng     if (state->aio_context) {
2081df9a681dSFam Zheng         aio_context_release(state->aio_context);
2082df9a681dSFam Zheng     }
2083df9a681dSFam Zheng }
2084df9a681dSFam Zheng 
208550f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp)
208678b18b78SStefan Hajnoczi {
208778b18b78SStefan Hajnoczi     error_setg(errp, "Transaction aborted using Abort action");
208878b18b78SStefan Hajnoczi }
208978b18b78SStefan Hajnoczi 
209050f43f0fSJohn Snow static void abort_commit(BlkActionState *common)
209178b18b78SStefan Hajnoczi {
2092dfc6f865SStefan Weil     g_assert_not_reached(); /* this action never succeeds */
209378b18b78SStefan Hajnoczi }
209478b18b78SStefan Hajnoczi 
209550f43f0fSJohn Snow static const BlkActionOps actions[] = {
209643de7e2dSAlberto Garcia     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
209743de7e2dSAlberto Garcia         .instance_size = sizeof(ExternalSnapshotState),
209843de7e2dSAlberto Garcia         .prepare  = external_snapshot_prepare,
209943de7e2dSAlberto Garcia         .commit   = external_snapshot_commit,
210043de7e2dSAlberto Garcia         .abort = external_snapshot_abort,
21014ad6f3dbSAlberto Garcia         .clean = external_snapshot_clean,
210243de7e2dSAlberto Garcia     },
2103c8a83e85SKevin Wolf     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2104ba5d6ab6SStefan Hajnoczi         .instance_size = sizeof(ExternalSnapshotState),
2105ba0c86a3SWenchao Xia         .prepare  = external_snapshot_prepare,
2106ba0c86a3SWenchao Xia         .commit   = external_snapshot_commit,
2107ba0c86a3SWenchao Xia         .abort = external_snapshot_abort,
2108da763e83SFam Zheng         .clean = external_snapshot_clean,
2109ba0c86a3SWenchao Xia     },
21103037f364SStefan Hajnoczi     [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
21113037f364SStefan Hajnoczi         .instance_size = sizeof(DriveBackupState),
21123037f364SStefan Hajnoczi         .prepare = drive_backup_prepare,
21133037f364SStefan Hajnoczi         .abort = drive_backup_abort,
21145d6e96efSStefan Hajnoczi         .clean = drive_backup_clean,
21153037f364SStefan Hajnoczi     },
2116bd8baecdSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2117bd8baecdSFam Zheng         .instance_size = sizeof(BlockdevBackupState),
2118bd8baecdSFam Zheng         .prepare = blockdev_backup_prepare,
2119bd8baecdSFam Zheng         .abort = blockdev_backup_abort,
2120bd8baecdSFam Zheng         .clean = blockdev_backup_clean,
2121bd8baecdSFam Zheng     },
212278b18b78SStefan Hajnoczi     [TRANSACTION_ACTION_KIND_ABORT] = {
212350f43f0fSJohn Snow         .instance_size = sizeof(BlkActionState),
212478b18b78SStefan Hajnoczi         .prepare = abort_prepare,
212578b18b78SStefan Hajnoczi         .commit = abort_commit,
212678b18b78SStefan Hajnoczi     },
2127bbe86010SWenchao Xia     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2128bbe86010SWenchao Xia         .instance_size = sizeof(InternalSnapshotState),
2129bbe86010SWenchao Xia         .prepare  = internal_snapshot_prepare,
2130bbe86010SWenchao Xia         .abort = internal_snapshot_abort,
21315d6e96efSStefan Hajnoczi         .clean = internal_snapshot_clean,
2132bbe86010SWenchao Xia     },
2133df9a681dSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2134df9a681dSFam Zheng         .instance_size = sizeof(BlockDirtyBitmapState),
2135df9a681dSFam Zheng         .prepare = block_dirty_bitmap_add_prepare,
2136df9a681dSFam Zheng         .abort = block_dirty_bitmap_add_abort,
2137df9a681dSFam Zheng     },
2138df9a681dSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2139df9a681dSFam Zheng         .instance_size = sizeof(BlockDirtyBitmapState),
2140df9a681dSFam Zheng         .prepare = block_dirty_bitmap_clear_prepare,
2141df9a681dSFam Zheng         .commit = block_dirty_bitmap_clear_commit,
2142df9a681dSFam Zheng         .abort = block_dirty_bitmap_clear_abort,
2143df9a681dSFam Zheng         .clean = block_dirty_bitmap_clear_clean,
2144df9a681dSFam Zheng     }
2145ba0c86a3SWenchao Xia };
2146ba0c86a3SWenchao Xia 
214794d16a64SJohn Snow /**
214894d16a64SJohn Snow  * Allocate a TransactionProperties structure if necessary, and fill
214994d16a64SJohn Snow  * that structure with desired defaults if they are unset.
215094d16a64SJohn Snow  */
215194d16a64SJohn Snow static TransactionProperties *get_transaction_properties(
215294d16a64SJohn Snow     TransactionProperties *props)
215394d16a64SJohn Snow {
215494d16a64SJohn Snow     if (!props) {
215594d16a64SJohn Snow         props = g_new0(TransactionProperties, 1);
215694d16a64SJohn Snow     }
215794d16a64SJohn Snow 
215894d16a64SJohn Snow     if (!props->has_completion_mode) {
215994d16a64SJohn Snow         props->has_completion_mode = true;
216094d16a64SJohn Snow         props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
216194d16a64SJohn Snow     }
216294d16a64SJohn Snow 
216394d16a64SJohn Snow     return props;
216494d16a64SJohn Snow }
216594d16a64SJohn Snow 
21668802d1fdSJeff Cody /*
2167b756b9ceSStefan Hajnoczi  * 'Atomic' group operations.  The operations are performed as a set, and if
2168b756b9ceSStefan Hajnoczi  * any fail then we roll back all operations in the group.
21698802d1fdSJeff Cody  */
217094d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list,
217194d16a64SJohn Snow                      bool has_props,
217294d16a64SJohn Snow                      struct TransactionProperties *props,
217394d16a64SJohn Snow                      Error **errp)
21748802d1fdSJeff Cody {
2175c8a83e85SKevin Wolf     TransactionActionList *dev_entry = dev_list;
217694d16a64SJohn Snow     BlockJobTxn *block_job_txn = NULL;
217750f43f0fSJohn Snow     BlkActionState *state, *next;
217843e17041SLuiz Capitulino     Error *local_err = NULL;
21798802d1fdSJeff Cody 
218050f43f0fSJohn Snow     QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
21818802d1fdSJeff Cody     QSIMPLEQ_INIT(&snap_bdrv_states);
21828802d1fdSJeff Cody 
218394d16a64SJohn Snow     /* Does this transaction get canceled as a group on failure?
218494d16a64SJohn Snow      * If not, we don't really need to make a BlockJobTxn.
218594d16a64SJohn Snow      */
218694d16a64SJohn Snow     props = get_transaction_properties(props);
218794d16a64SJohn Snow     if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
218894d16a64SJohn Snow         block_job_txn = block_job_txn_new();
218994d16a64SJohn Snow     }
219094d16a64SJohn Snow 
2191b756b9ceSStefan Hajnoczi     /* drain all i/o before any operations */
21928802d1fdSJeff Cody     bdrv_drain_all();
21938802d1fdSJeff Cody 
2194b756b9ceSStefan Hajnoczi     /* We don't do anything in this loop that commits us to the operations */
21958802d1fdSJeff Cody     while (NULL != dev_entry) {
2196c8a83e85SKevin Wolf         TransactionAction *dev_info = NULL;
219750f43f0fSJohn Snow         const BlkActionOps *ops;
219852e7c241SPaolo Bonzini 
21998802d1fdSJeff Cody         dev_info = dev_entry->value;
22008802d1fdSJeff Cody         dev_entry = dev_entry->next;
22018802d1fdSJeff Cody 
22026a8f9661SEric Blake         assert(dev_info->type < ARRAY_SIZE(actions));
2203ba0c86a3SWenchao Xia 
22046a8f9661SEric Blake         ops = &actions[dev_info->type];
2205aa3fe714SMax Reitz         assert(ops->instance_size > 0);
2206aa3fe714SMax Reitz 
2207ba5d6ab6SStefan Hajnoczi         state = g_malloc0(ops->instance_size);
2208ba5d6ab6SStefan Hajnoczi         state->ops = ops;
2209ba5d6ab6SStefan Hajnoczi         state->action = dev_info;
221094d16a64SJohn Snow         state->block_job_txn = block_job_txn;
221194d16a64SJohn Snow         state->txn_props = props;
2212ba5d6ab6SStefan Hajnoczi         QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
22138802d1fdSJeff Cody 
2214ba5d6ab6SStefan Hajnoczi         state->ops->prepare(state, &local_err);
221584d18f06SMarkus Armbruster         if (local_err) {
22169b9877eeSWenchao Xia             error_propagate(errp, local_err);
22179b9877eeSWenchao Xia             goto delete_and_fail;
22189b9877eeSWenchao Xia         }
221952e7c241SPaolo Bonzini     }
22208802d1fdSJeff Cody 
2221ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2222f9ea81e8SStefan Hajnoczi         if (state->ops->commit) {
2223ba5d6ab6SStefan Hajnoczi             state->ops->commit(state);
22248802d1fdSJeff Cody         }
2225f9ea81e8SStefan Hajnoczi     }
22268802d1fdSJeff Cody 
22278802d1fdSJeff Cody     /* success */
22288802d1fdSJeff Cody     goto exit;
22298802d1fdSJeff Cody 
22308802d1fdSJeff Cody delete_and_fail:
2231b756b9ceSStefan Hajnoczi     /* failure, and it is all-or-none; roll back all operations */
2232ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2233ba5d6ab6SStefan Hajnoczi         if (state->ops->abort) {
2234ba5d6ab6SStefan Hajnoczi             state->ops->abort(state);
2235ba0c86a3SWenchao Xia         }
22368802d1fdSJeff Cody     }
22378802d1fdSJeff Cody exit:
2238ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2239ba5d6ab6SStefan Hajnoczi         if (state->ops->clean) {
2240ba5d6ab6SStefan Hajnoczi             state->ops->clean(state);
2241ba0c86a3SWenchao Xia         }
2242ba5d6ab6SStefan Hajnoczi         g_free(state);
22438802d1fdSJeff Cody     }
224494d16a64SJohn Snow     if (!has_props) {
224594d16a64SJohn Snow         qapi_free_TransactionProperties(props);
224694d16a64SJohn Snow     }
224794d16a64SJohn Snow     block_job_txn_unref(block_job_txn);
22488802d1fdSJeff Cody }
22498802d1fdSJeff Cody 
2250c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
2251666daa68SMarkus Armbruster {
225238f54bd1SMax Reitz     Error *local_err = NULL;
2253666daa68SMarkus Armbruster 
225438f54bd1SMax Reitz     qmp_blockdev_open_tray(device, has_force, force, &local_err);
225538f54bd1SMax Reitz     if (local_err) {
225638f54bd1SMax Reitz         error_propagate(errp, local_err);
2257c245b6a3SLuiz Capitulino         return;
2258666daa68SMarkus Armbruster     }
225992d48558SLuiz Capitulino 
22606e0abc25SMax Reitz     qmp_x_blockdev_remove_medium(device, errp);
2261666daa68SMarkus Armbruster }
2262666daa68SMarkus Armbruster 
226312d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device,
226412d3ba82SBenoît Canet                       bool has_node_name, const char *node_name,
226512d3ba82SBenoît Canet                       const char *password, Error **errp)
2266666daa68SMarkus Armbruster {
226712d3ba82SBenoît Canet     Error *local_err = NULL;
2268666daa68SMarkus Armbruster     BlockDriverState *bs;
2269e3442099SStefan Hajnoczi     AioContext *aio_context;
2270666daa68SMarkus Armbruster 
227112d3ba82SBenoît Canet     bs = bdrv_lookup_bs(has_device ? device : NULL,
227212d3ba82SBenoît Canet                         has_node_name ? node_name : NULL,
227312d3ba82SBenoît Canet                         &local_err);
227484d18f06SMarkus Armbruster     if (local_err) {
227512d3ba82SBenoît Canet         error_propagate(errp, local_err);
2276a4dea8a9SLuiz Capitulino         return;
2277666daa68SMarkus Armbruster     }
2278666daa68SMarkus Armbruster 
2279e3442099SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2280e3442099SStefan Hajnoczi     aio_context_acquire(aio_context);
2281e3442099SStefan Hajnoczi 
22824d2855a3SMarkus Armbruster     bdrv_add_key(bs, password, errp);
2283666daa68SMarkus Armbruster 
2284e3442099SStefan Hajnoczi     aio_context_release(aio_context);
2285e3442099SStefan Hajnoczi }
2286e3442099SStefan Hajnoczi 
22877d8a9f71SMax Reitz void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
22887d8a9f71SMax Reitz                             Error **errp)
22897d8a9f71SMax Reitz {
22907d8a9f71SMax Reitz     BlockBackend *blk;
22917d8a9f71SMax Reitz     bool locked;
22927d8a9f71SMax Reitz 
22937d8a9f71SMax Reitz     if (!has_force) {
22947d8a9f71SMax Reitz         force = false;
22957d8a9f71SMax Reitz     }
22967d8a9f71SMax Reitz 
22977d8a9f71SMax Reitz     blk = blk_by_name(device);
22987d8a9f71SMax Reitz     if (!blk) {
22997d8a9f71SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
23007d8a9f71SMax Reitz                   "Device '%s' not found", device);
23017d8a9f71SMax Reitz         return;
23027d8a9f71SMax Reitz     }
23037d8a9f71SMax Reitz 
23047d8a9f71SMax Reitz     if (!blk_dev_has_removable_media(blk)) {
23057d8a9f71SMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
23067d8a9f71SMax Reitz         return;
23077d8a9f71SMax Reitz     }
23087d8a9f71SMax Reitz 
230912c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
231012c7ec87SMax Reitz         /* Ignore this command on tray-less devices */
231112c7ec87SMax Reitz         return;
231212c7ec87SMax Reitz     }
231312c7ec87SMax Reitz 
23147d8a9f71SMax Reitz     if (blk_dev_is_tray_open(blk)) {
23157d8a9f71SMax Reitz         return;
23167d8a9f71SMax Reitz     }
23177d8a9f71SMax Reitz 
23187d8a9f71SMax Reitz     locked = blk_dev_is_medium_locked(blk);
23197d8a9f71SMax Reitz     if (locked) {
23207d8a9f71SMax Reitz         blk_dev_eject_request(blk, force);
23217d8a9f71SMax Reitz     }
23227d8a9f71SMax Reitz 
23237d8a9f71SMax Reitz     if (!locked || force) {
23247d8a9f71SMax Reitz         blk_dev_change_media_cb(blk, false);
23257d8a9f71SMax Reitz     }
23267d8a9f71SMax Reitz }
23277d8a9f71SMax Reitz 
2328abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp)
2329abaaf59dSMax Reitz {
2330abaaf59dSMax Reitz     BlockBackend *blk;
2331abaaf59dSMax Reitz 
2332abaaf59dSMax Reitz     blk = blk_by_name(device);
2333abaaf59dSMax Reitz     if (!blk) {
2334abaaf59dSMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2335abaaf59dSMax Reitz                   "Device '%s' not found", device);
2336abaaf59dSMax Reitz         return;
2337abaaf59dSMax Reitz     }
2338abaaf59dSMax Reitz 
2339abaaf59dSMax Reitz     if (!blk_dev_has_removable_media(blk)) {
2340abaaf59dSMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
2341abaaf59dSMax Reitz         return;
2342abaaf59dSMax Reitz     }
2343abaaf59dSMax Reitz 
234412c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
234512c7ec87SMax Reitz         /* Ignore this command on tray-less devices */
234612c7ec87SMax Reitz         return;
234712c7ec87SMax Reitz     }
234812c7ec87SMax Reitz 
2349abaaf59dSMax Reitz     if (!blk_dev_is_tray_open(blk)) {
2350abaaf59dSMax Reitz         return;
2351abaaf59dSMax Reitz     }
2352abaaf59dSMax Reitz 
2353abaaf59dSMax Reitz     blk_dev_change_media_cb(blk, true);
2354abaaf59dSMax Reitz }
2355abaaf59dSMax Reitz 
23566e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
23572814f672SMax Reitz {
23582814f672SMax Reitz     BlockBackend *blk;
23592814f672SMax Reitz     BlockDriverState *bs;
23602814f672SMax Reitz     AioContext *aio_context;
23612814f672SMax Reitz     bool has_device;
23622814f672SMax Reitz 
23632814f672SMax Reitz     blk = blk_by_name(device);
23642814f672SMax Reitz     if (!blk) {
23652814f672SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
23662814f672SMax Reitz                   "Device '%s' not found", device);
23672814f672SMax Reitz         return;
23682814f672SMax Reitz     }
23692814f672SMax Reitz 
23702814f672SMax Reitz     /* For BBs without a device, we can exchange the BDS tree at will */
23712814f672SMax Reitz     has_device = blk_get_attached_dev(blk);
23722814f672SMax Reitz 
23732814f672SMax Reitz     if (has_device && !blk_dev_has_removable_media(blk)) {
23742814f672SMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
23752814f672SMax Reitz         return;
23762814f672SMax Reitz     }
23772814f672SMax Reitz 
237812c7ec87SMax Reitz     if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
23792814f672SMax Reitz         error_setg(errp, "Tray of device '%s' is not open", device);
23802814f672SMax Reitz         return;
23812814f672SMax Reitz     }
23822814f672SMax Reitz 
23832814f672SMax Reitz     bs = blk_bs(blk);
23842814f672SMax Reitz     if (!bs) {
23852814f672SMax Reitz         return;
23862814f672SMax Reitz     }
23872814f672SMax Reitz 
23882814f672SMax Reitz     aio_context = bdrv_get_aio_context(bs);
23892814f672SMax Reitz     aio_context_acquire(aio_context);
23902814f672SMax Reitz 
23912814f672SMax Reitz     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
23922814f672SMax Reitz         goto out;
23932814f672SMax Reitz     }
23942814f672SMax Reitz 
23952814f672SMax Reitz     /* This follows the convention established by bdrv_make_anon() */
23962814f672SMax Reitz     if (bs->device_list.tqe_prev) {
2397f8aa905aSJeff Cody         bdrv_device_remove(bs);
23982814f672SMax Reitz     }
23992814f672SMax Reitz 
24002814f672SMax Reitz     blk_remove_bs(blk);
24012814f672SMax Reitz 
240212c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
240312c7ec87SMax Reitz         /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
240412c7ec87SMax Reitz          * called at all); therefore, the medium needs to be ejected here.
240512c7ec87SMax Reitz          * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
240612c7ec87SMax Reitz          * value passed here (i.e. false). */
240712c7ec87SMax Reitz         blk_dev_change_media_cb(blk, false);
240812c7ec87SMax Reitz     }
240912c7ec87SMax Reitz 
24102814f672SMax Reitz out:
24112814f672SMax Reitz     aio_context_release(aio_context);
24122814f672SMax Reitz }
24132814f672SMax Reitz 
2414d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device,
2415d1299882SMax Reitz                                             BlockDriverState *bs, Error **errp)
2416d1299882SMax Reitz {
2417d1299882SMax Reitz     BlockBackend *blk;
2418d1299882SMax Reitz     bool has_device;
2419d1299882SMax Reitz 
2420d1299882SMax Reitz     blk = blk_by_name(device);
2421d1299882SMax Reitz     if (!blk) {
2422d1299882SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2423d1299882SMax Reitz                   "Device '%s' not found", device);
2424d1299882SMax Reitz         return;
2425d1299882SMax Reitz     }
2426d1299882SMax Reitz 
2427d1299882SMax Reitz     /* For BBs without a device, we can exchange the BDS tree at will */
2428d1299882SMax Reitz     has_device = blk_get_attached_dev(blk);
2429d1299882SMax Reitz 
2430d1299882SMax Reitz     if (has_device && !blk_dev_has_removable_media(blk)) {
2431d1299882SMax Reitz         error_setg(errp, "Device '%s' is not removable", device);
2432d1299882SMax Reitz         return;
2433d1299882SMax Reitz     }
2434d1299882SMax Reitz 
243512c7ec87SMax Reitz     if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2436d1299882SMax Reitz         error_setg(errp, "Tray of device '%s' is not open", device);
2437d1299882SMax Reitz         return;
2438d1299882SMax Reitz     }
2439d1299882SMax Reitz 
2440d1299882SMax Reitz     if (blk_bs(blk)) {
2441d1299882SMax Reitz         error_setg(errp, "There already is a medium in device '%s'", device);
2442d1299882SMax Reitz         return;
2443d1299882SMax Reitz     }
2444d1299882SMax Reitz 
2445d1299882SMax Reitz     blk_insert_bs(blk, bs);
2446d1299882SMax Reitz 
2447d1299882SMax Reitz     QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
244812c7ec87SMax Reitz 
244912c7ec87SMax Reitz     if (!blk_dev_has_tray(blk)) {
245012c7ec87SMax Reitz         /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
245112c7ec87SMax Reitz          * called at all); therefore, the medium needs to be pushed into the
245212c7ec87SMax Reitz          * slot here.
245312c7ec87SMax Reitz          * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
245412c7ec87SMax Reitz          * value passed here (i.e. true). */
245512c7ec87SMax Reitz         blk_dev_change_media_cb(blk, true);
245612c7ec87SMax Reitz     }
2457d1299882SMax Reitz }
2458d1299882SMax Reitz 
24596e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name,
2460d1299882SMax Reitz                                   Error **errp)
2461d1299882SMax Reitz {
2462d1299882SMax Reitz     BlockDriverState *bs;
2463d1299882SMax Reitz 
2464d1299882SMax Reitz     bs = bdrv_find_node(node_name);
2465d1299882SMax Reitz     if (!bs) {
2466d1299882SMax Reitz         error_setg(errp, "Node '%s' not found", node_name);
2467d1299882SMax Reitz         return;
2468d1299882SMax Reitz     }
2469d1299882SMax Reitz 
2470d1299882SMax Reitz     if (bs->blk) {
2471d1299882SMax Reitz         error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2472d1299882SMax Reitz                    blk_name(bs->blk));
2473d1299882SMax Reitz         return;
2474d1299882SMax Reitz     }
2475d1299882SMax Reitz 
2476d1299882SMax Reitz     qmp_blockdev_insert_anon_medium(device, bs, errp);
2477d1299882SMax Reitz }
2478d1299882SMax Reitz 
247924fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename,
248024fb4133SMax Reitz                                 bool has_format, const char *format,
248139ff43d9SMax Reitz                                 bool has_read_only,
248239ff43d9SMax Reitz                                 BlockdevChangeReadOnlyMode read_only,
248324fb4133SMax Reitz                                 Error **errp)
2484de2c6c05SMax Reitz {
2485de2c6c05SMax Reitz     BlockBackend *blk;
2486de2c6c05SMax Reitz     BlockDriverState *medium_bs = NULL;
2487de2c6c05SMax Reitz     int bdrv_flags, ret;
2488de2c6c05SMax Reitz     QDict *options = NULL;
2489de2c6c05SMax Reitz     Error *err = NULL;
2490de2c6c05SMax Reitz 
2491de2c6c05SMax Reitz     blk = blk_by_name(device);
2492de2c6c05SMax Reitz     if (!blk) {
2493de2c6c05SMax Reitz         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2494de2c6c05SMax Reitz                   "Device '%s' not found", device);
2495de2c6c05SMax Reitz         goto fail;
2496de2c6c05SMax Reitz     }
2497de2c6c05SMax Reitz 
2498de2c6c05SMax Reitz     if (blk_bs(blk)) {
2499de2c6c05SMax Reitz         blk_update_root_state(blk);
2500de2c6c05SMax Reitz     }
2501de2c6c05SMax Reitz 
2502de2c6c05SMax Reitz     bdrv_flags = blk_get_open_flags_from_root_state(blk);
2503de2c6c05SMax Reitz 
250439ff43d9SMax Reitz     if (!has_read_only) {
250539ff43d9SMax Reitz         read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
250639ff43d9SMax Reitz     }
250739ff43d9SMax Reitz 
250839ff43d9SMax Reitz     switch (read_only) {
250939ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
251039ff43d9SMax Reitz         break;
251139ff43d9SMax Reitz 
251239ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
251339ff43d9SMax Reitz         bdrv_flags &= ~BDRV_O_RDWR;
251439ff43d9SMax Reitz         break;
251539ff43d9SMax Reitz 
251639ff43d9SMax Reitz     case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
251739ff43d9SMax Reitz         bdrv_flags |= BDRV_O_RDWR;
251839ff43d9SMax Reitz         break;
251939ff43d9SMax Reitz 
252039ff43d9SMax Reitz     default:
252139ff43d9SMax Reitz         abort();
252239ff43d9SMax Reitz     }
252339ff43d9SMax Reitz 
252424fb4133SMax Reitz     if (has_format) {
2525de2c6c05SMax Reitz         options = qdict_new();
2526de2c6c05SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
2527de2c6c05SMax Reitz     }
2528de2c6c05SMax Reitz 
2529de2c6c05SMax Reitz     assert(!medium_bs);
2530de2c6c05SMax Reitz     ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2531de2c6c05SMax Reitz     if (ret < 0) {
2532de2c6c05SMax Reitz         goto fail;
2533de2c6c05SMax Reitz     }
2534de2c6c05SMax Reitz 
2535de2c6c05SMax Reitz     blk_apply_root_state(blk, medium_bs);
2536de2c6c05SMax Reitz 
2537de2c6c05SMax Reitz     bdrv_add_key(medium_bs, NULL, &err);
2538de2c6c05SMax Reitz     if (err) {
2539de2c6c05SMax Reitz         error_propagate(errp, err);
2540de2c6c05SMax Reitz         goto fail;
2541de2c6c05SMax Reitz     }
2542de2c6c05SMax Reitz 
2543de2c6c05SMax Reitz     qmp_blockdev_open_tray(device, false, false, &err);
2544de2c6c05SMax Reitz     if (err) {
2545de2c6c05SMax Reitz         error_propagate(errp, err);
2546de2c6c05SMax Reitz         goto fail;
2547de2c6c05SMax Reitz     }
2548de2c6c05SMax Reitz 
25496e0abc25SMax Reitz     qmp_x_blockdev_remove_medium(device, &err);
2550de2c6c05SMax Reitz     if (err) {
2551de2c6c05SMax Reitz         error_propagate(errp, err);
2552de2c6c05SMax Reitz         goto fail;
2553de2c6c05SMax Reitz     }
2554de2c6c05SMax Reitz 
2555de2c6c05SMax Reitz     qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2556de2c6c05SMax Reitz     if (err) {
2557de2c6c05SMax Reitz         error_propagate(errp, err);
2558de2c6c05SMax Reitz         goto fail;
2559de2c6c05SMax Reitz     }
2560de2c6c05SMax Reitz 
2561de2c6c05SMax Reitz     qmp_blockdev_close_tray(device, errp);
2562de2c6c05SMax Reitz 
2563de2c6c05SMax Reitz fail:
2564de2c6c05SMax Reitz     /* If the medium has been inserted, the device has its own reference, so
2565de2c6c05SMax Reitz      * ours must be relinquished; and if it has not been inserted successfully,
2566de2c6c05SMax Reitz      * the reference must be relinquished anyway */
2567de2c6c05SMax Reitz     bdrv_unref(medium_bs);
2568de2c6c05SMax Reitz }
2569de2c6c05SMax Reitz 
2570727f005eSZhi Yong Wu /* throttling disk I/O limits */
257180047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
25723e9fab69SBenoît Canet                                int64_t bps_wr,
25733e9fab69SBenoît Canet                                int64_t iops,
25743e9fab69SBenoît Canet                                int64_t iops_rd,
25753e9fab69SBenoît Canet                                int64_t iops_wr,
25763e9fab69SBenoît Canet                                bool has_bps_max,
25773e9fab69SBenoît Canet                                int64_t bps_max,
25783e9fab69SBenoît Canet                                bool has_bps_rd_max,
25793e9fab69SBenoît Canet                                int64_t bps_rd_max,
25803e9fab69SBenoît Canet                                bool has_bps_wr_max,
25813e9fab69SBenoît Canet                                int64_t bps_wr_max,
25823e9fab69SBenoît Canet                                bool has_iops_max,
25833e9fab69SBenoît Canet                                int64_t iops_max,
25843e9fab69SBenoît Canet                                bool has_iops_rd_max,
25853e9fab69SBenoît Canet                                int64_t iops_rd_max,
25863e9fab69SBenoît Canet                                bool has_iops_wr_max,
25872024c1dfSBenoît Canet                                int64_t iops_wr_max,
25882024c1dfSBenoît Canet                                bool has_iops_size,
258976f4afb4SAlberto Garcia                                int64_t iops_size,
259076f4afb4SAlberto Garcia                                bool has_group,
259176f4afb4SAlberto Garcia                                const char *group, Error **errp)
2592727f005eSZhi Yong Wu {
2593cc0681c4SBenoît Canet     ThrottleConfig cfg;
2594727f005eSZhi Yong Wu     BlockDriverState *bs;
2595a0e8544cSFam Zheng     BlockBackend *blk;
2596b15446fdSStefan Hajnoczi     AioContext *aio_context;
2597727f005eSZhi Yong Wu 
2598a0e8544cSFam Zheng     blk = blk_by_name(device);
2599a0e8544cSFam Zheng     if (!blk) {
260075158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
260175158ebbSMarkus Armbruster                   "Device '%s' not found", device);
260280047da5SLuiz Capitulino         return;
2603727f005eSZhi Yong Wu     }
26045433c24fSMax Reitz 
26055433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
26065433c24fSMax Reitz     aio_context_acquire(aio_context);
26075433c24fSMax Reitz 
2608a0e8544cSFam Zheng     bs = blk_bs(blk);
26095433c24fSMax Reitz     if (!bs) {
26105433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
26115433c24fSMax Reitz         goto out;
26125433c24fSMax Reitz     }
2613727f005eSZhi Yong Wu 
2614cc0681c4SBenoît Canet     memset(&cfg, 0, sizeof(cfg));
2615cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2616cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_READ].avg  = bps_rd;
2617cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2618727f005eSZhi Yong Wu 
2619cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2620cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_READ].avg  = iops_rd;
2621cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2622cc0681c4SBenoît Canet 
26233e9fab69SBenoît Canet     if (has_bps_max) {
26243e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
26253e9fab69SBenoît Canet     }
26263e9fab69SBenoît Canet     if (has_bps_rd_max) {
26273e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
26283e9fab69SBenoît Canet     }
26293e9fab69SBenoît Canet     if (has_bps_wr_max) {
26303e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
26313e9fab69SBenoît Canet     }
26323e9fab69SBenoît Canet     if (has_iops_max) {
26333e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
26343e9fab69SBenoît Canet     }
26353e9fab69SBenoît Canet     if (has_iops_rd_max) {
26363e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
26373e9fab69SBenoît Canet     }
26383e9fab69SBenoît Canet     if (has_iops_wr_max) {
26393e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
26403e9fab69SBenoît Canet     }
2641cc0681c4SBenoît Canet 
26422024c1dfSBenoît Canet     if (has_iops_size) {
26432024c1dfSBenoît Canet         cfg.op_size = iops_size;
26442024c1dfSBenoît Canet     }
2645cc0681c4SBenoît Canet 
2646*d5851089SAlberto Garcia     if (!throttle_is_valid(&cfg, errp)) {
26475433c24fSMax Reitz         goto out;
2648727f005eSZhi Yong Wu     }
2649727f005eSZhi Yong Wu 
265076f4afb4SAlberto Garcia     if (throttle_enabled(&cfg)) {
265176f4afb4SAlberto Garcia         /* Enable I/O limits if they're not enabled yet, otherwise
265276f4afb4SAlberto Garcia          * just update the throttling group. */
2653a0d64a61SAlberto Garcia         if (!bs->throttle_state) {
265476f4afb4SAlberto Garcia             bdrv_io_limits_enable(bs, has_group ? group : device);
265576f4afb4SAlberto Garcia         } else if (has_group) {
265676f4afb4SAlberto Garcia             bdrv_io_limits_update_group(bs, group);
2657727f005eSZhi Yong Wu         }
265876f4afb4SAlberto Garcia         /* Set the new throttling configuration */
2659cc0681c4SBenoît Canet         bdrv_set_io_limits(bs, &cfg);
2660a0d64a61SAlberto Garcia     } else if (bs->throttle_state) {
266176f4afb4SAlberto Garcia         /* If all throttling settings are set to 0, disable I/O limits */
266276f4afb4SAlberto Garcia         bdrv_io_limits_disable(bs);
2663727f005eSZhi Yong Wu     }
2664b15446fdSStefan Hajnoczi 
26655433c24fSMax Reitz out:
2666b15446fdSStefan Hajnoczi     aio_context_release(aio_context);
2667727f005eSZhi Yong Wu }
2668727f005eSZhi Yong Wu 
2669341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2670341ebc2fSJohn Snow                                 bool has_granularity, uint32_t granularity,
2671341ebc2fSJohn Snow                                 Error **errp)
2672341ebc2fSJohn Snow {
2673341ebc2fSJohn Snow     AioContext *aio_context;
2674341ebc2fSJohn Snow     BlockDriverState *bs;
2675341ebc2fSJohn Snow 
2676341ebc2fSJohn Snow     if (!name || name[0] == '\0') {
2677341ebc2fSJohn Snow         error_setg(errp, "Bitmap name cannot be empty");
2678341ebc2fSJohn Snow         return;
2679341ebc2fSJohn Snow     }
2680341ebc2fSJohn Snow 
2681341ebc2fSJohn Snow     bs = bdrv_lookup_bs(node, node, errp);
2682341ebc2fSJohn Snow     if (!bs) {
2683341ebc2fSJohn Snow         return;
2684341ebc2fSJohn Snow     }
2685341ebc2fSJohn Snow 
2686341ebc2fSJohn Snow     aio_context = bdrv_get_aio_context(bs);
2687341ebc2fSJohn Snow     aio_context_acquire(aio_context);
2688341ebc2fSJohn Snow 
2689341ebc2fSJohn Snow     if (has_granularity) {
2690341ebc2fSJohn Snow         if (granularity < 512 || !is_power_of_2(granularity)) {
2691341ebc2fSJohn Snow             error_setg(errp, "Granularity must be power of 2 "
2692341ebc2fSJohn Snow                              "and at least 512");
2693341ebc2fSJohn Snow             goto out;
2694341ebc2fSJohn Snow         }
2695341ebc2fSJohn Snow     } else {
2696341ebc2fSJohn Snow         /* Default to cluster size, if available: */
2697341ebc2fSJohn Snow         granularity = bdrv_get_default_bitmap_granularity(bs);
2698341ebc2fSJohn Snow     }
2699341ebc2fSJohn Snow 
2700341ebc2fSJohn Snow     bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2701341ebc2fSJohn Snow 
2702341ebc2fSJohn Snow  out:
2703341ebc2fSJohn Snow     aio_context_release(aio_context);
2704341ebc2fSJohn Snow }
2705341ebc2fSJohn Snow 
2706341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2707341ebc2fSJohn Snow                                    Error **errp)
2708341ebc2fSJohn Snow {
2709341ebc2fSJohn Snow     AioContext *aio_context;
2710341ebc2fSJohn Snow     BlockDriverState *bs;
2711341ebc2fSJohn Snow     BdrvDirtyBitmap *bitmap;
2712341ebc2fSJohn Snow 
2713341ebc2fSJohn Snow     bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2714341ebc2fSJohn Snow     if (!bitmap || !bs) {
2715341ebc2fSJohn Snow         return;
2716341ebc2fSJohn Snow     }
2717341ebc2fSJohn Snow 
27189bd2b08fSJohn Snow     if (bdrv_dirty_bitmap_frozen(bitmap)) {
27199bd2b08fSJohn Snow         error_setg(errp,
27209bd2b08fSJohn Snow                    "Bitmap '%s' is currently frozen and cannot be removed",
27219bd2b08fSJohn Snow                    name);
27229bd2b08fSJohn Snow         goto out;
27239bd2b08fSJohn Snow     }
272420dca810SJohn Snow     bdrv_dirty_bitmap_make_anon(bitmap);
2725341ebc2fSJohn Snow     bdrv_release_dirty_bitmap(bs, bitmap);
2726341ebc2fSJohn Snow 
27279bd2b08fSJohn Snow  out:
2728341ebc2fSJohn Snow     aio_context_release(aio_context);
2729341ebc2fSJohn Snow }
2730341ebc2fSJohn Snow 
2731e74e6b78SJohn Snow /**
2732e74e6b78SJohn Snow  * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2733e74e6b78SJohn Snow  * immediately after a full backup operation.
2734e74e6b78SJohn Snow  */
2735e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2736e74e6b78SJohn Snow                                   Error **errp)
2737e74e6b78SJohn Snow {
2738e74e6b78SJohn Snow     AioContext *aio_context;
2739e74e6b78SJohn Snow     BdrvDirtyBitmap *bitmap;
2740e74e6b78SJohn Snow     BlockDriverState *bs;
2741e74e6b78SJohn Snow 
2742e74e6b78SJohn Snow     bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2743e74e6b78SJohn Snow     if (!bitmap || !bs) {
2744e74e6b78SJohn Snow         return;
2745e74e6b78SJohn Snow     }
2746e74e6b78SJohn Snow 
2747e74e6b78SJohn Snow     if (bdrv_dirty_bitmap_frozen(bitmap)) {
2748e74e6b78SJohn Snow         error_setg(errp,
2749e74e6b78SJohn Snow                    "Bitmap '%s' is currently frozen and cannot be modified",
2750e74e6b78SJohn Snow                    name);
2751e74e6b78SJohn Snow         goto out;
2752e74e6b78SJohn Snow     } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2753e74e6b78SJohn Snow         error_setg(errp,
2754e74e6b78SJohn Snow                    "Bitmap '%s' is currently disabled and cannot be cleared",
2755e74e6b78SJohn Snow                    name);
2756e74e6b78SJohn Snow         goto out;
2757e74e6b78SJohn Snow     }
2758e74e6b78SJohn Snow 
2759df9a681dSFam Zheng     bdrv_clear_dirty_bitmap(bitmap, NULL);
2760e74e6b78SJohn Snow 
2761e74e6b78SJohn Snow  out:
2762e74e6b78SJohn Snow     aio_context_release(aio_context);
2763e74e6b78SJohn Snow }
2764e74e6b78SJohn Snow 
2765072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict)
27669063f814SRyan Harper {
27679063f814SRyan Harper     const char *id = qdict_get_str(qdict, "id");
27687e7d56d9SMarkus Armbruster     BlockBackend *blk;
27699063f814SRyan Harper     BlockDriverState *bs;
27708ad4202bSStefan Hajnoczi     AioContext *aio_context;
27713718d8abSFam Zheng     Error *local_err = NULL;
27729063f814SRyan Harper 
27737e7d56d9SMarkus Armbruster     blk = blk_by_name(id);
27747e7d56d9SMarkus Armbruster     if (!blk) {
2775b1422f20SMarkus Armbruster         error_report("Device '%s' not found", id);
2776072ebe6bSMarkus Armbruster         return;
27779063f814SRyan Harper     }
27788ad4202bSStefan Hajnoczi 
277926f8b3a8SMarkus Armbruster     if (!blk_legacy_dinfo(blk)) {
278048f364ddSMarkus Armbruster         error_report("Deleting device added with blockdev-add"
278148f364ddSMarkus Armbruster                      " is not supported");
2782072ebe6bSMarkus Armbruster         return;
278348f364ddSMarkus Armbruster     }
278448f364ddSMarkus Armbruster 
27855433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
27868ad4202bSStefan Hajnoczi     aio_context_acquire(aio_context);
27878ad4202bSStefan Hajnoczi 
27885433c24fSMax Reitz     bs = blk_bs(blk);
27895433c24fSMax Reitz     if (bs) {
27903718d8abSFam Zheng         if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2791565f65d2SMarkus Armbruster             error_report_err(local_err);
27928ad4202bSStefan Hajnoczi             aio_context_release(aio_context);
2793072ebe6bSMarkus Armbruster             return;
27948591675fSMarcelo Tosatti         }
27959063f814SRyan Harper 
2796938abd43SMax Reitz         blk_remove_bs(blk);
27975433c24fSMax Reitz     }
27989063f814SRyan Harper 
2799fa879d62SMarkus Armbruster     /* if we have a device attached to this BlockDriverState
2800d22b2f41SRyan Harper      * then we need to make the drive anonymous until the device
2801d22b2f41SRyan Harper      * can be removed.  If this is a drive with no device backing
2802d22b2f41SRyan Harper      * then we can just get rid of the block driver state right here.
2803d22b2f41SRyan Harper      */
2804a7f53e26SMarkus Armbruster     if (blk_get_attached_dev(blk)) {
28053e5a50d6SMarkus Armbruster         blk_hide_on_behalf_of_hmp_drive_del(blk);
2806293c51a6SStefan Hajnoczi         /* Further I/O must not pause the guest */
2807373340b2SMax Reitz         blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2808293c51a6SStefan Hajnoczi                          BLOCKDEV_ON_ERROR_REPORT);
2809d22b2f41SRyan Harper     } else {
2810b9fe8a7aSMarkus Armbruster         blk_unref(blk);
2811d22b2f41SRyan Harper     }
28129063f814SRyan Harper 
28138ad4202bSStefan Hajnoczi     aio_context_release(aio_context);
28149063f814SRyan Harper }
28156d4a2b3aSChristoph Hellwig 
28163b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device,
28173b1dbd11SBenoît Canet                       bool has_node_name, const char *node_name,
28183b1dbd11SBenoît Canet                       int64_t size, Error **errp)
28196d4a2b3aSChristoph Hellwig {
28203b1dbd11SBenoît Canet     Error *local_err = NULL;
28216d4a2b3aSChristoph Hellwig     BlockDriverState *bs;
2822927e0e76SStefan Hajnoczi     AioContext *aio_context;
28238732901eSKevin Wolf     int ret;
28246d4a2b3aSChristoph Hellwig 
28253b1dbd11SBenoît Canet     bs = bdrv_lookup_bs(has_device ? device : NULL,
28263b1dbd11SBenoît Canet                         has_node_name ? node_name : NULL,
28273b1dbd11SBenoît Canet                         &local_err);
282884d18f06SMarkus Armbruster     if (local_err) {
28293b1dbd11SBenoît Canet         error_propagate(errp, local_err);
28303b1dbd11SBenoît Canet         return;
28313b1dbd11SBenoît Canet     }
28323b1dbd11SBenoît Canet 
2833927e0e76SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2834927e0e76SStefan Hajnoczi     aio_context_acquire(aio_context);
2835927e0e76SStefan Hajnoczi 
28363b1dbd11SBenoît Canet     if (!bdrv_is_first_non_filter(bs)) {
2837c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2838927e0e76SStefan Hajnoczi         goto out;
28396d4a2b3aSChristoph Hellwig     }
28406d4a2b3aSChristoph Hellwig 
28416d4a2b3aSChristoph Hellwig     if (size < 0) {
2842c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2843927e0e76SStefan Hajnoczi         goto out;
28446d4a2b3aSChristoph Hellwig     }
28456d4a2b3aSChristoph Hellwig 
28469c75e168SJeff Cody     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2847c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_IN_USE, device);
2848927e0e76SStefan Hajnoczi         goto out;
28499c75e168SJeff Cody     }
28509c75e168SJeff Cody 
285192b7a08dSPeter Lieven     /* complete all in-flight operations before resizing the device */
285292b7a08dSPeter Lieven     bdrv_drain_all();
285392b7a08dSPeter Lieven 
28548732901eSKevin Wolf     ret = bdrv_truncate(bs, size);
28558732901eSKevin Wolf     switch (ret) {
2856939a1cc3SStefan Hajnoczi     case 0:
2857939a1cc3SStefan Hajnoczi         break;
2858939a1cc3SStefan Hajnoczi     case -ENOMEDIUM:
2859c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2860939a1cc3SStefan Hajnoczi         break;
2861939a1cc3SStefan Hajnoczi     case -ENOTSUP:
2862c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_UNSUPPORTED);
2863939a1cc3SStefan Hajnoczi         break;
2864939a1cc3SStefan Hajnoczi     case -EACCES:
286581e5f78aSAlberto Garcia         error_setg(errp, "Device '%s' is read only", device);
2866939a1cc3SStefan Hajnoczi         break;
2867939a1cc3SStefan Hajnoczi     case -EBUSY:
2868c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_IN_USE, device);
2869939a1cc3SStefan Hajnoczi         break;
2870939a1cc3SStefan Hajnoczi     default:
28718732901eSKevin Wolf         error_setg_errno(errp, -ret, "Could not resize");
2872939a1cc3SStefan Hajnoczi         break;
28736d4a2b3aSChristoph Hellwig     }
2874927e0e76SStefan Hajnoczi 
2875927e0e76SStefan Hajnoczi out:
2876927e0e76SStefan Hajnoczi     aio_context_release(aio_context);
28776d4a2b3aSChristoph Hellwig }
287812bd451fSStefan Hajnoczi 
28799abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret)
288012bd451fSStefan Hajnoczi {
2881723c5d93SStefan Hajnoczi     /* Note that this function may be executed from another AioContext besides
2882723c5d93SStefan Hajnoczi      * the QEMU main loop.  If you need to access anything that assumes the
2883723c5d93SStefan Hajnoczi      * QEMU global mutex, use a BH or introduce a mutex.
2884723c5d93SStefan Hajnoczi      */
2885723c5d93SStefan Hajnoczi 
288612bd451fSStefan Hajnoczi     BlockDriverState *bs = opaque;
2887bcada37bSWenchao Xia     const char *msg = NULL;
288812bd451fSStefan Hajnoczi 
28899abf2dbaSJeff Cody     trace_block_job_cb(bs, bs->job, ret);
289012bd451fSStefan Hajnoczi 
289112bd451fSStefan Hajnoczi     assert(bs->job);
2892bcada37bSWenchao Xia 
289312bd451fSStefan Hajnoczi     if (ret < 0) {
2894bcada37bSWenchao Xia         msg = strerror(-ret);
289512bd451fSStefan Hajnoczi     }
289612bd451fSStefan Hajnoczi 
2897370521a1SStefan Hajnoczi     if (block_job_is_cancelled(bs->job)) {
2898bcada37bSWenchao Xia         block_job_event_cancelled(bs->job);
2899370521a1SStefan Hajnoczi     } else {
2900bcada37bSWenchao Xia         block_job_event_completed(bs->job, msg);
2901370521a1SStefan Hajnoczi     }
290212bd451fSStefan Hajnoczi }
290312bd451fSStefan Hajnoczi 
290413d8cc51SJeff Cody void qmp_block_stream(const char *device,
290513d8cc51SJeff Cody                       bool has_base, const char *base,
290613d8cc51SJeff Cody                       bool has_backing_file, const char *backing_file,
290713d8cc51SJeff Cody                       bool has_speed, int64_t speed,
29081d809098SPaolo Bonzini                       bool has_on_error, BlockdevOnError on_error,
29091d809098SPaolo Bonzini                       Error **errp)
291012bd451fSStefan Hajnoczi {
2911a0e8544cSFam Zheng     BlockBackend *blk;
291212bd451fSStefan Hajnoczi     BlockDriverState *bs;
2913c8c3080fSMarcelo Tosatti     BlockDriverState *base_bs = NULL;
2914f3e69bebSStefan Hajnoczi     AioContext *aio_context;
2915fd7f8c65SStefan Hajnoczi     Error *local_err = NULL;
291613d8cc51SJeff Cody     const char *base_name = NULL;
291712bd451fSStefan Hajnoczi 
29181d809098SPaolo Bonzini     if (!has_on_error) {
29191d809098SPaolo Bonzini         on_error = BLOCKDEV_ON_ERROR_REPORT;
29201d809098SPaolo Bonzini     }
29211d809098SPaolo Bonzini 
2922a0e8544cSFam Zheng     blk = blk_by_name(device);
2923a0e8544cSFam Zheng     if (!blk) {
292475158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
292575158ebbSMarkus Armbruster                   "Device '%s' not found", device);
292612bd451fSStefan Hajnoczi         return;
292712bd451fSStefan Hajnoczi     }
292812bd451fSStefan Hajnoczi 
29295433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
2930f3e69bebSStefan Hajnoczi     aio_context_acquire(aio_context);
2931f3e69bebSStefan Hajnoczi 
29325433c24fSMax Reitz     if (!blk_is_available(blk)) {
29335433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
29345433c24fSMax Reitz         goto out;
29355433c24fSMax Reitz     }
29365433c24fSMax Reitz     bs = blk_bs(blk);
29375433c24fSMax Reitz 
2938628ff683SFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2939f3e69bebSStefan Hajnoczi         goto out;
2940628ff683SFam Zheng     }
2941628ff683SFam Zheng 
294213d8cc51SJeff Cody     if (has_base) {
2943c8c3080fSMarcelo Tosatti         base_bs = bdrv_find_backing_image(bs, base);
2944c8c3080fSMarcelo Tosatti         if (base_bs == NULL) {
2945c6bd8c70SMarkus Armbruster             error_setg(errp, QERR_BASE_NOT_FOUND, base);
2946f3e69bebSStefan Hajnoczi             goto out;
294712bd451fSStefan Hajnoczi         }
2948f3e69bebSStefan Hajnoczi         assert(bdrv_get_aio_context(base_bs) == aio_context);
294913d8cc51SJeff Cody         base_name = base;
2950c8c3080fSMarcelo Tosatti     }
295112bd451fSStefan Hajnoczi 
295213d8cc51SJeff Cody     /* if we are streaming the entire chain, the result will have no backing
295313d8cc51SJeff Cody      * file, and specifying one is therefore an error */
295413d8cc51SJeff Cody     if (base_bs == NULL && has_backing_file) {
295513d8cc51SJeff Cody         error_setg(errp, "backing file specified, but streaming the "
295613d8cc51SJeff Cody                          "entire chain");
2957f3e69bebSStefan Hajnoczi         goto out;
295813d8cc51SJeff Cody     }
295913d8cc51SJeff Cody 
296013d8cc51SJeff Cody     /* backing_file string overrides base bs filename */
296113d8cc51SJeff Cody     base_name = has_backing_file ? backing_file : base_name;
296213d8cc51SJeff Cody 
296313d8cc51SJeff Cody     stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
29641d809098SPaolo Bonzini                  on_error, block_job_cb, bs, &local_err);
296584d18f06SMarkus Armbruster     if (local_err) {
2966fd7f8c65SStefan Hajnoczi         error_propagate(errp, local_err);
2967f3e69bebSStefan Hajnoczi         goto out;
296812bd451fSStefan Hajnoczi     }
296912bd451fSStefan Hajnoczi 
297012bd451fSStefan Hajnoczi     trace_qmp_block_stream(bs, bs->job);
2971f3e69bebSStefan Hajnoczi 
2972f3e69bebSStefan Hajnoczi out:
2973f3e69bebSStefan Hajnoczi     aio_context_release(aio_context);
297412bd451fSStefan Hajnoczi }
29752d47c6e9SStefan Hajnoczi 
2976ed61fc10SJeff Cody void qmp_block_commit(const char *device,
29777676e2c5SJeff Cody                       bool has_base, const char *base,
29787676e2c5SJeff Cody                       bool has_top, const char *top,
297954e26900SJeff Cody                       bool has_backing_file, const char *backing_file,
2980ed61fc10SJeff Cody                       bool has_speed, int64_t speed,
2981ed61fc10SJeff Cody                       Error **errp)
2982ed61fc10SJeff Cody {
2983a0e8544cSFam Zheng     BlockBackend *blk;
2984ed61fc10SJeff Cody     BlockDriverState *bs;
2985ed61fc10SJeff Cody     BlockDriverState *base_bs, *top_bs;
29869e85cd5cSStefan Hajnoczi     AioContext *aio_context;
2987ed61fc10SJeff Cody     Error *local_err = NULL;
2988ed61fc10SJeff Cody     /* This will be part of the QMP command, if/when the
2989ed61fc10SJeff Cody      * BlockdevOnError change for blkmirror makes it in
2990ed61fc10SJeff Cody      */
299192aa5c6dSPaolo Bonzini     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2992ed61fc10SJeff Cody 
299354504663SMax Reitz     if (!has_speed) {
299454504663SMax Reitz         speed = 0;
299554504663SMax Reitz     }
299654504663SMax Reitz 
29977676e2c5SJeff Cody     /* Important Note:
29987676e2c5SJeff Cody      *  libvirt relies on the DeviceNotFound error class in order to probe for
29997676e2c5SJeff Cody      *  live commit feature versions; for this to work, we must make sure to
30007676e2c5SJeff Cody      *  perform the device lookup before any generic errors that may occur in a
30017676e2c5SJeff Cody      *  scenario in which all optional arguments are omitted. */
3002a0e8544cSFam Zheng     blk = blk_by_name(device);
3003a0e8544cSFam Zheng     if (!blk) {
300475158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
300575158ebbSMarkus Armbruster                   "Device '%s' not found", device);
3006ed61fc10SJeff Cody         return;
3007ed61fc10SJeff Cody     }
3008ed61fc10SJeff Cody 
30095433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
30109e85cd5cSStefan Hajnoczi     aio_context_acquire(aio_context);
30119e85cd5cSStefan Hajnoczi 
30125433c24fSMax Reitz     if (!blk_is_available(blk)) {
30135433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
30145433c24fSMax Reitz         goto out;
30155433c24fSMax Reitz     }
30165433c24fSMax Reitz     bs = blk_bs(blk);
30175433c24fSMax Reitz 
3018bb00021dSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
30199e85cd5cSStefan Hajnoczi         goto out;
3020628ff683SFam Zheng     }
3021628ff683SFam Zheng 
3022ed61fc10SJeff Cody     /* default top_bs is the active layer */
3023ed61fc10SJeff Cody     top_bs = bs;
3024ed61fc10SJeff Cody 
30257676e2c5SJeff Cody     if (has_top && top) {
3026ed61fc10SJeff Cody         if (strcmp(bs->filename, top) != 0) {
3027ed61fc10SJeff Cody             top_bs = bdrv_find_backing_image(bs, top);
3028ed61fc10SJeff Cody         }
3029ed61fc10SJeff Cody     }
3030ed61fc10SJeff Cody 
3031ed61fc10SJeff Cody     if (top_bs == NULL) {
3032ed61fc10SJeff Cody         error_setg(errp, "Top image file %s not found", top ? top : "NULL");
30339e85cd5cSStefan Hajnoczi         goto out;
3034ed61fc10SJeff Cody     }
3035ed61fc10SJeff Cody 
30369e85cd5cSStefan Hajnoczi     assert(bdrv_get_aio_context(top_bs) == aio_context);
30379e85cd5cSStefan Hajnoczi 
3038d5208c45SJeff Cody     if (has_base && base) {
3039d5208c45SJeff Cody         base_bs = bdrv_find_backing_image(top_bs, base);
3040d5208c45SJeff Cody     } else {
3041d5208c45SJeff Cody         base_bs = bdrv_find_base(top_bs);
3042d5208c45SJeff Cody     }
3043d5208c45SJeff Cody 
3044d5208c45SJeff Cody     if (base_bs == NULL) {
3045c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
30469e85cd5cSStefan Hajnoczi         goto out;
3047d5208c45SJeff Cody     }
3048d5208c45SJeff Cody 
30499e85cd5cSStefan Hajnoczi     assert(bdrv_get_aio_context(base_bs) == aio_context);
30509e85cd5cSStefan Hajnoczi 
3051bb00021dSFam Zheng     if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3052bb00021dSFam Zheng         goto out;
3053bb00021dSFam Zheng     }
3054bb00021dSFam Zheng 
30557676e2c5SJeff Cody     /* Do not allow attempts to commit an image into itself */
30567676e2c5SJeff Cody     if (top_bs == base_bs) {
30577676e2c5SJeff Cody         error_setg(errp, "cannot commit an image into itself");
30589e85cd5cSStefan Hajnoczi         goto out;
30597676e2c5SJeff Cody     }
30607676e2c5SJeff Cody 
306120a63d2cSFam Zheng     if (top_bs == bs) {
306254e26900SJeff Cody         if (has_backing_file) {
306354e26900SJeff Cody             error_setg(errp, "'backing-file' specified,"
306454e26900SJeff Cody                              " but 'top' is the active layer");
30659e85cd5cSStefan Hajnoczi             goto out;
306654e26900SJeff Cody         }
306720a63d2cSFam Zheng         commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
306820a63d2cSFam Zheng                             bs, &local_err);
306920a63d2cSFam Zheng     } else {
3070ed61fc10SJeff Cody         commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
307154e26900SJeff Cody                      has_backing_file ? backing_file : NULL, &local_err);
307220a63d2cSFam Zheng     }
3073ed61fc10SJeff Cody     if (local_err != NULL) {
3074ed61fc10SJeff Cody         error_propagate(errp, local_err);
30759e85cd5cSStefan Hajnoczi         goto out;
3076ed61fc10SJeff Cody     }
30779e85cd5cSStefan Hajnoczi 
30789e85cd5cSStefan Hajnoczi out:
30799e85cd5cSStefan Hajnoczi     aio_context_release(aio_context);
3080ed61fc10SJeff Cody }
3081ed61fc10SJeff Cody 
308278f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target,
308399a9addfSStefan Hajnoczi                             bool has_format, const char *format,
3084b53169eaSStefan Hajnoczi                             enum MirrorSyncMode sync,
308599a9addfSStefan Hajnoczi                             bool has_mode, enum NewImageMode mode,
308699a9addfSStefan Hajnoczi                             bool has_speed, int64_t speed,
3087d58d8453SJohn Snow                             bool has_bitmap, const char *bitmap,
308878f51fdeSJohn Snow                             bool has_on_source_error,
308978f51fdeSJohn Snow                             BlockdevOnError on_source_error,
309078f51fdeSJohn Snow                             bool has_on_target_error,
309178f51fdeSJohn Snow                             BlockdevOnError on_target_error,
309278f51fdeSJohn Snow                             BlockJobTxn *txn, Error **errp)
309399a9addfSStefan Hajnoczi {
3094a0e8544cSFam Zheng     BlockBackend *blk;
309599a9addfSStefan Hajnoczi     BlockDriverState *bs;
309699a9addfSStefan Hajnoczi     BlockDriverState *target_bs;
3097fc5d3f84SIan Main     BlockDriverState *source = NULL;
3098d58d8453SJohn Snow     BdrvDirtyBitmap *bmap = NULL;
3099761731b1SStefan Hajnoczi     AioContext *aio_context;
3100e6641719SMax Reitz     QDict *options = NULL;
310199a9addfSStefan Hajnoczi     Error *local_err = NULL;
310299a9addfSStefan Hajnoczi     int flags;
310399a9addfSStefan Hajnoczi     int64_t size;
310499a9addfSStefan Hajnoczi     int ret;
310599a9addfSStefan Hajnoczi 
310699a9addfSStefan Hajnoczi     if (!has_speed) {
310799a9addfSStefan Hajnoczi         speed = 0;
310899a9addfSStefan Hajnoczi     }
310999a9addfSStefan Hajnoczi     if (!has_on_source_error) {
311099a9addfSStefan Hajnoczi         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
311199a9addfSStefan Hajnoczi     }
311299a9addfSStefan Hajnoczi     if (!has_on_target_error) {
311399a9addfSStefan Hajnoczi         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
311499a9addfSStefan Hajnoczi     }
311599a9addfSStefan Hajnoczi     if (!has_mode) {
311699a9addfSStefan Hajnoczi         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
311799a9addfSStefan Hajnoczi     }
311899a9addfSStefan Hajnoczi 
3119a0e8544cSFam Zheng     blk = blk_by_name(device);
3120a0e8544cSFam Zheng     if (!blk) {
312175158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
312275158ebbSMarkus Armbruster                   "Device '%s' not found", device);
312399a9addfSStefan Hajnoczi         return;
312499a9addfSStefan Hajnoczi     }
312599a9addfSStefan Hajnoczi 
31265433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
3127761731b1SStefan Hajnoczi     aio_context_acquire(aio_context);
3128761731b1SStefan Hajnoczi 
3129c29c1dd3SFam Zheng     /* Although backup_run has this check too, we need to use bs->drv below, so
3130c29c1dd3SFam Zheng      * do an early check redundantly. */
31315433c24fSMax Reitz     if (!blk_is_available(blk)) {
3132c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3133761731b1SStefan Hajnoczi         goto out;
313499a9addfSStefan Hajnoczi     }
31355433c24fSMax Reitz     bs = blk_bs(blk);
313699a9addfSStefan Hajnoczi 
313799a9addfSStefan Hajnoczi     if (!has_format) {
313899a9addfSStefan Hajnoczi         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
313999a9addfSStefan Hajnoczi     }
314099a9addfSStefan Hajnoczi 
3141c29c1dd3SFam Zheng     /* Early check to avoid creating target */
31423718d8abSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
3143761731b1SStefan Hajnoczi         goto out;
314499a9addfSStefan Hajnoczi     }
314599a9addfSStefan Hajnoczi 
314699a9addfSStefan Hajnoczi     flags = bs->open_flags | BDRV_O_RDWR;
314799a9addfSStefan Hajnoczi 
3148fc5d3f84SIan Main     /* See if we have a backing HD we can use to create our new image
3149fc5d3f84SIan Main      * on top of. */
3150fc5d3f84SIan Main     if (sync == MIRROR_SYNC_MODE_TOP) {
3151760e0063SKevin Wolf         source = backing_bs(bs);
3152fc5d3f84SIan Main         if (!source) {
3153fc5d3f84SIan Main             sync = MIRROR_SYNC_MODE_FULL;
3154fc5d3f84SIan Main         }
3155fc5d3f84SIan Main     }
3156fc5d3f84SIan Main     if (sync == MIRROR_SYNC_MODE_NONE) {
3157fc5d3f84SIan Main         source = bs;
3158fc5d3f84SIan Main     }
3159fc5d3f84SIan Main 
316099a9addfSStefan Hajnoczi     size = bdrv_getlength(bs);
316199a9addfSStefan Hajnoczi     if (size < 0) {
316299a9addfSStefan Hajnoczi         error_setg_errno(errp, -size, "bdrv_getlength failed");
3163761731b1SStefan Hajnoczi         goto out;
316499a9addfSStefan Hajnoczi     }
316599a9addfSStefan Hajnoczi 
316699a9addfSStefan Hajnoczi     if (mode != NEW_IMAGE_MODE_EXISTING) {
3167e6641719SMax Reitz         assert(format);
3168fc5d3f84SIan Main         if (source) {
3169fc5d3f84SIan Main             bdrv_img_create(target, format, source->filename,
3170fc5d3f84SIan Main                             source->drv->format_name, NULL,
3171fc5d3f84SIan Main                             size, flags, &local_err, false);
3172fc5d3f84SIan Main         } else {
3173fc5d3f84SIan Main             bdrv_img_create(target, format, NULL, NULL, NULL,
3174fc5d3f84SIan Main                             size, flags, &local_err, false);
3175fc5d3f84SIan Main         }
317699a9addfSStefan Hajnoczi     }
317799a9addfSStefan Hajnoczi 
317884d18f06SMarkus Armbruster     if (local_err) {
317999a9addfSStefan Hajnoczi         error_propagate(errp, local_err);
3180761731b1SStefan Hajnoczi         goto out;
318199a9addfSStefan Hajnoczi     }
318299a9addfSStefan Hajnoczi 
3183e6641719SMax Reitz     if (format) {
3184e6641719SMax Reitz         options = qdict_new();
3185e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
3186e6641719SMax Reitz     }
3187e6641719SMax Reitz 
3188f67503e5SMax Reitz     target_bs = NULL;
31896ebf9aa2SMax Reitz     ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
319099a9addfSStefan Hajnoczi     if (ret < 0) {
319134b5d2c6SMax Reitz         error_propagate(errp, local_err);
3192761731b1SStefan Hajnoczi         goto out;
319399a9addfSStefan Hajnoczi     }
319499a9addfSStefan Hajnoczi 
3195761731b1SStefan Hajnoczi     bdrv_set_aio_context(target_bs, aio_context);
3196761731b1SStefan Hajnoczi 
3197d58d8453SJohn Snow     if (has_bitmap) {
3198d58d8453SJohn Snow         bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3199d58d8453SJohn Snow         if (!bmap) {
3200d58d8453SJohn Snow             error_setg(errp, "Bitmap '%s' could not be found", bitmap);
32010702d3d8SMax Reitz             bdrv_unref(target_bs);
3202d58d8453SJohn Snow             goto out;
3203d58d8453SJohn Snow         }
3204d58d8453SJohn Snow     }
3205d58d8453SJohn Snow 
3206d58d8453SJohn Snow     backup_start(bs, target_bs, speed, sync, bmap,
3207d58d8453SJohn Snow                  on_source_error, on_target_error,
320878f51fdeSJohn Snow                  block_job_cb, bs, txn, &local_err);
320999a9addfSStefan Hajnoczi     if (local_err != NULL) {
32104f6fd349SFam Zheng         bdrv_unref(target_bs);
321199a9addfSStefan Hajnoczi         error_propagate(errp, local_err);
3212761731b1SStefan Hajnoczi         goto out;
321399a9addfSStefan Hajnoczi     }
3214761731b1SStefan Hajnoczi 
3215761731b1SStefan Hajnoczi out:
3216761731b1SStefan Hajnoczi     aio_context_release(aio_context);
321799a9addfSStefan Hajnoczi }
321899a9addfSStefan Hajnoczi 
321978f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target,
322078f51fdeSJohn Snow                       bool has_format, const char *format,
322178f51fdeSJohn Snow                       enum MirrorSyncMode sync,
322278f51fdeSJohn Snow                       bool has_mode, enum NewImageMode mode,
322378f51fdeSJohn Snow                       bool has_speed, int64_t speed,
322478f51fdeSJohn Snow                       bool has_bitmap, const char *bitmap,
322578f51fdeSJohn Snow                       bool has_on_source_error, BlockdevOnError on_source_error,
322678f51fdeSJohn Snow                       bool has_on_target_error, BlockdevOnError on_target_error,
322778f51fdeSJohn Snow                       Error **errp)
322878f51fdeSJohn Snow {
322978f51fdeSJohn Snow     return do_drive_backup(device, target, has_format, format, sync,
323078f51fdeSJohn Snow                            has_mode, mode, has_speed, speed,
323178f51fdeSJohn Snow                            has_bitmap, bitmap,
323278f51fdeSJohn Snow                            has_on_source_error, on_source_error,
323378f51fdeSJohn Snow                            has_on_target_error, on_target_error,
323478f51fdeSJohn Snow                            NULL, errp);
323578f51fdeSJohn Snow }
323678f51fdeSJohn Snow 
3237c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3238c13163fbSBenoît Canet {
3239d5a8ee60SAlberto Garcia     return bdrv_named_nodes_list(errp);
3240c13163fbSBenoît Canet }
3241c13163fbSBenoît Canet 
324278f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target,
3243c29c1dd3SFam Zheng                          enum MirrorSyncMode sync,
3244c29c1dd3SFam Zheng                          bool has_speed, int64_t speed,
3245c29c1dd3SFam Zheng                          bool has_on_source_error,
3246c29c1dd3SFam Zheng                          BlockdevOnError on_source_error,
3247c29c1dd3SFam Zheng                          bool has_on_target_error,
3248c29c1dd3SFam Zheng                          BlockdevOnError on_target_error,
324978f51fdeSJohn Snow                          BlockJobTxn *txn, Error **errp)
3250c29c1dd3SFam Zheng {
32515433c24fSMax Reitz     BlockBackend *blk, *target_blk;
3252c29c1dd3SFam Zheng     BlockDriverState *bs;
3253c29c1dd3SFam Zheng     BlockDriverState *target_bs;
3254c29c1dd3SFam Zheng     Error *local_err = NULL;
3255c29c1dd3SFam Zheng     AioContext *aio_context;
3256c29c1dd3SFam Zheng 
3257c29c1dd3SFam Zheng     if (!has_speed) {
3258c29c1dd3SFam Zheng         speed = 0;
3259c29c1dd3SFam Zheng     }
3260c29c1dd3SFam Zheng     if (!has_on_source_error) {
3261c29c1dd3SFam Zheng         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3262c29c1dd3SFam Zheng     }
3263c29c1dd3SFam Zheng     if (!has_on_target_error) {
3264c29c1dd3SFam Zheng         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3265c29c1dd3SFam Zheng     }
3266c29c1dd3SFam Zheng 
3267a0e8544cSFam Zheng     blk = blk_by_name(device);
3268a0e8544cSFam Zheng     if (!blk) {
32695b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", device);
3270c29c1dd3SFam Zheng         return;
3271c29c1dd3SFam Zheng     }
3272c29c1dd3SFam Zheng 
32735433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
3274c29c1dd3SFam Zheng     aio_context_acquire(aio_context);
3275c29c1dd3SFam Zheng 
32765433c24fSMax Reitz     if (!blk_is_available(blk)) {
32775433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
32785433c24fSMax Reitz         goto out;
32795433c24fSMax Reitz     }
32805433c24fSMax Reitz     bs = blk_bs(blk);
32815433c24fSMax Reitz 
32825433c24fSMax Reitz     target_blk = blk_by_name(target);
32835433c24fSMax Reitz     if (!target_blk) {
32845b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", target);
3285c29c1dd3SFam Zheng         goto out;
3286c29c1dd3SFam Zheng     }
32875433c24fSMax Reitz 
32885433c24fSMax Reitz     if (!blk_is_available(target_blk)) {
32895433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", target);
32905433c24fSMax Reitz         goto out;
32915433c24fSMax Reitz     }
32925433c24fSMax Reitz     target_bs = blk_bs(target_blk);
3293c29c1dd3SFam Zheng 
3294c29c1dd3SFam Zheng     bdrv_ref(target_bs);
3295c29c1dd3SFam Zheng     bdrv_set_aio_context(target_bs, aio_context);
3296d58d8453SJohn Snow     backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
329778f51fdeSJohn Snow                  on_target_error, block_job_cb, bs, txn, &local_err);
3298c29c1dd3SFam Zheng     if (local_err != NULL) {
3299c29c1dd3SFam Zheng         bdrv_unref(target_bs);
3300c29c1dd3SFam Zheng         error_propagate(errp, local_err);
3301c29c1dd3SFam Zheng     }
3302c29c1dd3SFam Zheng out:
3303c29c1dd3SFam Zheng     aio_context_release(aio_context);
3304c29c1dd3SFam Zheng }
3305c29c1dd3SFam Zheng 
330678f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target,
330778f51fdeSJohn Snow                          enum MirrorSyncMode sync,
330878f51fdeSJohn Snow                          bool has_speed, int64_t speed,
330978f51fdeSJohn Snow                          bool has_on_source_error,
331078f51fdeSJohn Snow                          BlockdevOnError on_source_error,
331178f51fdeSJohn Snow                          bool has_on_target_error,
331278f51fdeSJohn Snow                          BlockdevOnError on_target_error,
331378f51fdeSJohn Snow                          Error **errp)
331478f51fdeSJohn Snow {
331578f51fdeSJohn Snow     do_blockdev_backup(device, target, sync, has_speed, speed,
331678f51fdeSJohn Snow                        has_on_source_error, on_source_error,
331778f51fdeSJohn Snow                        has_on_target_error, on_target_error,
331878f51fdeSJohn Snow                        NULL, errp);
331978f51fdeSJohn Snow }
332078f51fdeSJohn Snow 
33214193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring.
33224193cdd7SFam Zheng  * Caller should hold @device and @target's aio context (must be the same).
33234193cdd7SFam Zheng  **/
33244193cdd7SFam Zheng static void blockdev_mirror_common(BlockDriverState *bs,
33254193cdd7SFam Zheng                                    BlockDriverState *target,
332609158f00SBenoît Canet                                    bool has_replaces, const char *replaces,
3327d9b902dbSPaolo Bonzini                                    enum MirrorSyncMode sync,
3328b952b558SPaolo Bonzini                                    bool has_speed, int64_t speed,
3329eee13dfeSPaolo Bonzini                                    bool has_granularity, uint32_t granularity,
333008e4ed6cSPaolo Bonzini                                    bool has_buf_size, int64_t buf_size,
33314193cdd7SFam Zheng                                    bool has_on_source_error,
33324193cdd7SFam Zheng                                    BlockdevOnError on_source_error,
33334193cdd7SFam Zheng                                    bool has_on_target_error,
33344193cdd7SFam Zheng                                    BlockdevOnError on_target_error,
33350fc9f8eaSFam Zheng                                    bool has_unmap, bool unmap,
3336b952b558SPaolo Bonzini                                    Error **errp)
3337d9b902dbSPaolo Bonzini {
3338d9b902dbSPaolo Bonzini 
3339d9b902dbSPaolo Bonzini     if (!has_speed) {
3340d9b902dbSPaolo Bonzini         speed = 0;
3341d9b902dbSPaolo Bonzini     }
3342b952b558SPaolo Bonzini     if (!has_on_source_error) {
3343b952b558SPaolo Bonzini         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3344b952b558SPaolo Bonzini     }
3345b952b558SPaolo Bonzini     if (!has_on_target_error) {
3346b952b558SPaolo Bonzini         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3347b952b558SPaolo Bonzini     }
3348eee13dfeSPaolo Bonzini     if (!has_granularity) {
3349eee13dfeSPaolo Bonzini         granularity = 0;
3350eee13dfeSPaolo Bonzini     }
335108e4ed6cSPaolo Bonzini     if (!has_buf_size) {
335248ac0a4dSWen Congyang         buf_size = 0;
335308e4ed6cSPaolo Bonzini     }
33540fc9f8eaSFam Zheng     if (!has_unmap) {
33550fc9f8eaSFam Zheng         unmap = true;
33560fc9f8eaSFam Zheng     }
335708e4ed6cSPaolo Bonzini 
3358eee13dfeSPaolo Bonzini     if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3359c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
33603cbbe9fdSStefan Hajnoczi                    "a value in range [512B, 64MB]");
3361eee13dfeSPaolo Bonzini         return;
3362eee13dfeSPaolo Bonzini     }
3363eee13dfeSPaolo Bonzini     if (granularity & (granularity - 1)) {
3364c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3365c6bd8c70SMarkus Armbruster                    "power of 2");
3366eee13dfeSPaolo Bonzini         return;
3367eee13dfeSPaolo Bonzini     }
3368d9b902dbSPaolo Bonzini 
33694193cdd7SFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
33704193cdd7SFam Zheng         return;
33714193cdd7SFam Zheng     }
3372e40e5027SFam Zheng     if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3373e40e5027SFam Zheng         return;
3374e40e5027SFam Zheng     }
3375df92562eSFam Zheng     if (target->blk) {
3376df92562eSFam Zheng         error_setg(errp, "Cannot mirror to an attached block device");
3377df92562eSFam Zheng         return;
3378df92562eSFam Zheng     }
33794193cdd7SFam Zheng 
33804193cdd7SFam Zheng     if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
33814193cdd7SFam Zheng         sync = MIRROR_SYNC_MODE_FULL;
33824193cdd7SFam Zheng     }
33834193cdd7SFam Zheng 
33844193cdd7SFam Zheng     /* pass the node name to replace to mirror start since it's loose coupling
33854193cdd7SFam Zheng      * and will allow to check whether the node still exist at mirror completion
33864193cdd7SFam Zheng      */
33874193cdd7SFam Zheng     mirror_start(bs, target,
33884193cdd7SFam Zheng                  has_replaces ? replaces : NULL,
33894193cdd7SFam Zheng                  speed, granularity, buf_size, sync,
33904193cdd7SFam Zheng                  on_source_error, on_target_error, unmap,
33914193cdd7SFam Zheng                  block_job_cb, bs, errp);
33924193cdd7SFam Zheng }
33934193cdd7SFam Zheng 
33944193cdd7SFam Zheng void qmp_drive_mirror(const char *device, const char *target,
33954193cdd7SFam Zheng                       bool has_format, const char *format,
33964193cdd7SFam Zheng                       bool has_node_name, const char *node_name,
33974193cdd7SFam Zheng                       bool has_replaces, const char *replaces,
33984193cdd7SFam Zheng                       enum MirrorSyncMode sync,
33994193cdd7SFam Zheng                       bool has_mode, enum NewImageMode mode,
34004193cdd7SFam Zheng                       bool has_speed, int64_t speed,
34014193cdd7SFam Zheng                       bool has_granularity, uint32_t granularity,
34024193cdd7SFam Zheng                       bool has_buf_size, int64_t buf_size,
34034193cdd7SFam Zheng                       bool has_on_source_error, BlockdevOnError on_source_error,
34044193cdd7SFam Zheng                       bool has_on_target_error, BlockdevOnError on_target_error,
34054193cdd7SFam Zheng                       bool has_unmap, bool unmap,
34064193cdd7SFam Zheng                       Error **errp)
34074193cdd7SFam Zheng {
34084193cdd7SFam Zheng     BlockDriverState *bs;
34094193cdd7SFam Zheng     BlockBackend *blk;
34104193cdd7SFam Zheng     BlockDriverState *source, *target_bs;
34114193cdd7SFam Zheng     AioContext *aio_context;
34124193cdd7SFam Zheng     Error *local_err = NULL;
34134193cdd7SFam Zheng     QDict *options = NULL;
34144193cdd7SFam Zheng     int flags;
34154193cdd7SFam Zheng     int64_t size;
34164193cdd7SFam Zheng     int ret;
34174193cdd7SFam Zheng 
3418a0e8544cSFam Zheng     blk = blk_by_name(device);
3419a0e8544cSFam Zheng     if (!blk) {
342075158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
342175158ebbSMarkus Armbruster                   "Device '%s' not found", device);
3422d9b902dbSPaolo Bonzini         return;
3423d9b902dbSPaolo Bonzini     }
3424d9b902dbSPaolo Bonzini 
34255433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
34265a7e7a0bSStefan Hajnoczi     aio_context_acquire(aio_context);
34275a7e7a0bSStefan Hajnoczi 
34285433c24fSMax Reitz     if (!blk_is_available(blk)) {
3429c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
34305a7e7a0bSStefan Hajnoczi         goto out;
3431d9b902dbSPaolo Bonzini     }
34325433c24fSMax Reitz     bs = blk_bs(blk);
34334193cdd7SFam Zheng     if (!has_mode) {
34344193cdd7SFam Zheng         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
34354193cdd7SFam Zheng     }
3436d9b902dbSPaolo Bonzini 
3437d9b902dbSPaolo Bonzini     if (!has_format) {
3438d9b902dbSPaolo Bonzini         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3439d9b902dbSPaolo Bonzini     }
3440d9b902dbSPaolo Bonzini 
3441d9b902dbSPaolo Bonzini     flags = bs->open_flags | BDRV_O_RDWR;
3442760e0063SKevin Wolf     source = backing_bs(bs);
3443d9b902dbSPaolo Bonzini     if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3444d9b902dbSPaolo Bonzini         sync = MIRROR_SYNC_MODE_FULL;
3445d9b902dbSPaolo Bonzini     }
3446117e0c82SMax Reitz     if (sync == MIRROR_SYNC_MODE_NONE) {
3447117e0c82SMax Reitz         source = bs;
3448117e0c82SMax Reitz     }
3449d9b902dbSPaolo Bonzini 
3450ac3c5d83SStefan Hajnoczi     size = bdrv_getlength(bs);
3451ac3c5d83SStefan Hajnoczi     if (size < 0) {
3452ac3c5d83SStefan Hajnoczi         error_setg_errno(errp, -size, "bdrv_getlength failed");
34535a7e7a0bSStefan Hajnoczi         goto out;
3454ac3c5d83SStefan Hajnoczi     }
3455ac3c5d83SStefan Hajnoczi 
345609158f00SBenoît Canet     if (has_replaces) {
345709158f00SBenoît Canet         BlockDriverState *to_replace_bs;
34585a7e7a0bSStefan Hajnoczi         AioContext *replace_aio_context;
34595a7e7a0bSStefan Hajnoczi         int64_t replace_size;
346009158f00SBenoît Canet 
346109158f00SBenoît Canet         if (!has_node_name) {
346209158f00SBenoît Canet             error_setg(errp, "a node-name must be provided when replacing a"
346309158f00SBenoît Canet                              " named node of the graph");
34645a7e7a0bSStefan Hajnoczi             goto out;
346509158f00SBenoît Canet         }
346609158f00SBenoît Canet 
3467e12f3784SWen Congyang         to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
346809158f00SBenoît Canet 
346909158f00SBenoît Canet         if (!to_replace_bs) {
347009158f00SBenoît Canet             error_propagate(errp, local_err);
34715a7e7a0bSStefan Hajnoczi             goto out;
347209158f00SBenoît Canet         }
347309158f00SBenoît Canet 
34745a7e7a0bSStefan Hajnoczi         replace_aio_context = bdrv_get_aio_context(to_replace_bs);
34755a7e7a0bSStefan Hajnoczi         aio_context_acquire(replace_aio_context);
34765a7e7a0bSStefan Hajnoczi         replace_size = bdrv_getlength(to_replace_bs);
34775a7e7a0bSStefan Hajnoczi         aio_context_release(replace_aio_context);
34785a7e7a0bSStefan Hajnoczi 
34795a7e7a0bSStefan Hajnoczi         if (size != replace_size) {
348009158f00SBenoît Canet             error_setg(errp, "cannot replace image with a mirror image of "
348109158f00SBenoît Canet                              "different size");
34825a7e7a0bSStefan Hajnoczi             goto out;
348309158f00SBenoît Canet         }
348409158f00SBenoît Canet     }
348509158f00SBenoît Canet 
348614526864SMax Reitz     if ((sync == MIRROR_SYNC_MODE_FULL || !source)
348714526864SMax Reitz         && mode != NEW_IMAGE_MODE_EXISTING)
348814526864SMax Reitz     {
3489d9b902dbSPaolo Bonzini         /* create new image w/o backing file */
3490e6641719SMax Reitz         assert(format);
3491cf8f2426SLuiz Capitulino         bdrv_img_create(target, format,
3492f382d43aSMiroslav Rezanina                         NULL, NULL, NULL, size, flags, &local_err, false);
3493d9b902dbSPaolo Bonzini     } else {
3494d9b902dbSPaolo Bonzini         switch (mode) {
3495d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_EXISTING:
3496d9b902dbSPaolo Bonzini             break;
3497d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3498d9b902dbSPaolo Bonzini             /* create new image with backing file */
3499cf8f2426SLuiz Capitulino             bdrv_img_create(target, format,
3500d9b902dbSPaolo Bonzini                             source->filename,
3501d9b902dbSPaolo Bonzini                             source->drv->format_name,
3502f382d43aSMiroslav Rezanina                             NULL, size, flags, &local_err, false);
3503d9b902dbSPaolo Bonzini             break;
3504d9b902dbSPaolo Bonzini         default:
3505d9b902dbSPaolo Bonzini             abort();
3506d9b902dbSPaolo Bonzini         }
3507d9b902dbSPaolo Bonzini     }
3508d9b902dbSPaolo Bonzini 
350984d18f06SMarkus Armbruster     if (local_err) {
3510cf8f2426SLuiz Capitulino         error_propagate(errp, local_err);
35115a7e7a0bSStefan Hajnoczi         goto out;
3512d9b902dbSPaolo Bonzini     }
3513d9b902dbSPaolo Bonzini 
35144c828dc6SBenoît Canet     options = qdict_new();
3515e6641719SMax Reitz     if (has_node_name) {
35164c828dc6SBenoît Canet         qdict_put(options, "node-name", qstring_from_str(node_name));
35174c828dc6SBenoît Canet     }
3518e6641719SMax Reitz     if (format) {
3519e6641719SMax Reitz         qdict_put(options, "driver", qstring_from_str(format));
3520e6641719SMax Reitz     }
35214c828dc6SBenoît Canet 
3522b812f671SPaolo Bonzini     /* Mirroring takes care of copy-on-write using the source's backing
3523b812f671SPaolo Bonzini      * file.
3524b812f671SPaolo Bonzini      */
3525f67503e5SMax Reitz     target_bs = NULL;
35264c828dc6SBenoît Canet     ret = bdrv_open(&target_bs, target, NULL, options,
35276ebf9aa2SMax Reitz                     flags | BDRV_O_NO_BACKING, &local_err);
3528d9b902dbSPaolo Bonzini     if (ret < 0) {
352934b5d2c6SMax Reitz         error_propagate(errp, local_err);
35305a7e7a0bSStefan Hajnoczi         goto out;
3531d9b902dbSPaolo Bonzini     }
3532d9b902dbSPaolo Bonzini 
35335a7e7a0bSStefan Hajnoczi     bdrv_set_aio_context(target_bs, aio_context);
35345a7e7a0bSStefan Hajnoczi 
35354193cdd7SFam Zheng     blockdev_mirror_common(bs, target_bs,
35364193cdd7SFam Zheng                            has_replaces, replaces, sync,
35374193cdd7SFam Zheng                            has_speed, speed,
35384193cdd7SFam Zheng                            has_granularity, granularity,
35394193cdd7SFam Zheng                            has_buf_size, buf_size,
35404193cdd7SFam Zheng                            has_on_source_error, on_source_error,
35414193cdd7SFam Zheng                            has_on_target_error, on_target_error,
35424193cdd7SFam Zheng                            has_unmap, unmap,
35434193cdd7SFam Zheng                            &local_err);
35444193cdd7SFam Zheng     if (local_err) {
3545d9b902dbSPaolo Bonzini         error_propagate(errp, local_err);
35464193cdd7SFam Zheng         bdrv_unref(target_bs);
3547d9b902dbSPaolo Bonzini     }
35485a7e7a0bSStefan Hajnoczi out:
35495a7e7a0bSStefan Hajnoczi     aio_context_release(aio_context);
3550d9b902dbSPaolo Bonzini }
3551d9b902dbSPaolo Bonzini 
3552df92562eSFam Zheng void qmp_blockdev_mirror(const char *device, const char *target,
3553df92562eSFam Zheng                          bool has_replaces, const char *replaces,
3554df92562eSFam Zheng                          MirrorSyncMode sync,
3555df92562eSFam Zheng                          bool has_speed, int64_t speed,
3556df92562eSFam Zheng                          bool has_granularity, uint32_t granularity,
3557df92562eSFam Zheng                          bool has_buf_size, int64_t buf_size,
3558df92562eSFam Zheng                          bool has_on_source_error,
3559df92562eSFam Zheng                          BlockdevOnError on_source_error,
3560df92562eSFam Zheng                          bool has_on_target_error,
3561df92562eSFam Zheng                          BlockdevOnError on_target_error,
3562df92562eSFam Zheng                          Error **errp)
3563df92562eSFam Zheng {
3564df92562eSFam Zheng     BlockDriverState *bs;
3565df92562eSFam Zheng     BlockBackend *blk;
3566df92562eSFam Zheng     BlockDriverState *target_bs;
3567df92562eSFam Zheng     AioContext *aio_context;
3568df92562eSFam Zheng     Error *local_err = NULL;
3569df92562eSFam Zheng 
3570df92562eSFam Zheng     blk = blk_by_name(device);
3571df92562eSFam Zheng     if (!blk) {
3572df92562eSFam Zheng         error_setg(errp, "Device '%s' not found", device);
3573df92562eSFam Zheng         return;
3574df92562eSFam Zheng     }
3575df92562eSFam Zheng     bs = blk_bs(blk);
3576df92562eSFam Zheng 
3577df92562eSFam Zheng     if (!bs) {
3578df92562eSFam Zheng         error_setg(errp, "Device '%s' has no media", device);
3579df92562eSFam Zheng         return;
3580df92562eSFam Zheng     }
3581df92562eSFam Zheng 
3582df92562eSFam Zheng     target_bs = bdrv_lookup_bs(target, target, errp);
3583df92562eSFam Zheng     if (!target_bs) {
3584df92562eSFam Zheng         return;
3585df92562eSFam Zheng     }
3586df92562eSFam Zheng 
3587df92562eSFam Zheng     aio_context = bdrv_get_aio_context(bs);
3588df92562eSFam Zheng     aio_context_acquire(aio_context);
3589df92562eSFam Zheng 
3590df92562eSFam Zheng     bdrv_ref(target_bs);
3591df92562eSFam Zheng     bdrv_set_aio_context(target_bs, aio_context);
3592df92562eSFam Zheng 
3593df92562eSFam Zheng     blockdev_mirror_common(bs, target_bs,
3594df92562eSFam Zheng                            has_replaces, replaces, sync,
3595df92562eSFam Zheng                            has_speed, speed,
3596df92562eSFam Zheng                            has_granularity, granularity,
3597df92562eSFam Zheng                            has_buf_size, buf_size,
3598df92562eSFam Zheng                            has_on_source_error, on_source_error,
3599df92562eSFam Zheng                            has_on_target_error, on_target_error,
3600df92562eSFam Zheng                            true, true,
3601df92562eSFam Zheng                            &local_err);
3602df92562eSFam Zheng     if (local_err) {
3603df92562eSFam Zheng         error_propagate(errp, local_err);
3604df92562eSFam Zheng         bdrv_unref(target_bs);
3605df92562eSFam Zheng     }
3606df92562eSFam Zheng 
3607df92562eSFam Zheng     aio_context_release(aio_context);
3608df92562eSFam Zheng }
3609df92562eSFam Zheng 
36103d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */
361124d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context,
361224d6bffeSMarkus Armbruster                                 Error **errp)
36132d47c6e9SStefan Hajnoczi {
3614a0e8544cSFam Zheng     BlockBackend *blk;
36152d47c6e9SStefan Hajnoczi     BlockDriverState *bs;
36162d47c6e9SStefan Hajnoczi 
36175433c24fSMax Reitz     *aio_context = NULL;
36185433c24fSMax Reitz 
3619a0e8544cSFam Zheng     blk = blk_by_name(device);
3620a0e8544cSFam Zheng     if (!blk) {
36213d948cdfSStefan Hajnoczi         goto notfound;
36222d47c6e9SStefan Hajnoczi     }
36233d948cdfSStefan Hajnoczi 
36245433c24fSMax Reitz     *aio_context = blk_get_aio_context(blk);
36253d948cdfSStefan Hajnoczi     aio_context_acquire(*aio_context);
36263d948cdfSStefan Hajnoczi 
36275433c24fSMax Reitz     if (!blk_is_available(blk)) {
36285433c24fSMax Reitz         goto notfound;
36295433c24fSMax Reitz     }
36305433c24fSMax Reitz     bs = blk_bs(blk);
36315433c24fSMax Reitz 
36323d948cdfSStefan Hajnoczi     if (!bs->job) {
36333d948cdfSStefan Hajnoczi         goto notfound;
36343d948cdfSStefan Hajnoczi     }
36353d948cdfSStefan Hajnoczi 
36362d47c6e9SStefan Hajnoczi     return bs->job;
36373d948cdfSStefan Hajnoczi 
36383d948cdfSStefan Hajnoczi notfound:
36392e3a0266SMarkus Armbruster     error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
36402e3a0266SMarkus Armbruster               "No active block job on device '%s'", device);
36415433c24fSMax Reitz     if (*aio_context) {
36425433c24fSMax Reitz         aio_context_release(*aio_context);
36433d948cdfSStefan Hajnoczi         *aio_context = NULL;
36445433c24fSMax Reitz     }
36453d948cdfSStefan Hajnoczi     return NULL;
36462d47c6e9SStefan Hajnoczi }
36472d47c6e9SStefan Hajnoczi 
3648882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
36492d47c6e9SStefan Hajnoczi {
36503d948cdfSStefan Hajnoczi     AioContext *aio_context;
365124d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
36522d47c6e9SStefan Hajnoczi 
36532d47c6e9SStefan Hajnoczi     if (!job) {
36542d47c6e9SStefan Hajnoczi         return;
36552d47c6e9SStefan Hajnoczi     }
36562d47c6e9SStefan Hajnoczi 
3657882ec7ceSStefan Hajnoczi     block_job_set_speed(job, speed, errp);
36583d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
36592d47c6e9SStefan Hajnoczi }
3660370521a1SStefan Hajnoczi 
36616e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device,
36626e37fb81SPaolo Bonzini                           bool has_force, bool force, Error **errp)
36636e37fb81SPaolo Bonzini {
36643d948cdfSStefan Hajnoczi     AioContext *aio_context;
366524d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
36666e37fb81SPaolo Bonzini 
36676e37fb81SPaolo Bonzini     if (!job) {
36686e37fb81SPaolo Bonzini         return;
36696e37fb81SPaolo Bonzini     }
36703d948cdfSStefan Hajnoczi 
36713d948cdfSStefan Hajnoczi     if (!has_force) {
36723d948cdfSStefan Hajnoczi         force = false;
36733d948cdfSStefan Hajnoczi     }
36743d948cdfSStefan Hajnoczi 
3675751ebd76SFam Zheng     if (job->user_paused && !force) {
3676f231b88dSCole Robinson         error_setg(errp, "The block job for device '%s' is currently paused",
3677f231b88dSCole Robinson                    device);
36783d948cdfSStefan Hajnoczi         goto out;
36796e37fb81SPaolo Bonzini     }
36806e37fb81SPaolo Bonzini 
36816e37fb81SPaolo Bonzini     trace_qmp_block_job_cancel(job);
36826e37fb81SPaolo Bonzini     block_job_cancel(job);
36833d948cdfSStefan Hajnoczi out:
36843d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
36856e37fb81SPaolo Bonzini }
36866e37fb81SPaolo Bonzini 
36876e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp)
3688370521a1SStefan Hajnoczi {
36893d948cdfSStefan Hajnoczi     AioContext *aio_context;
369024d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
3691370521a1SStefan Hajnoczi 
3692751ebd76SFam Zheng     if (!job || job->user_paused) {
3693370521a1SStefan Hajnoczi         return;
3694370521a1SStefan Hajnoczi     }
36956e37fb81SPaolo Bonzini 
3696751ebd76SFam Zheng     job->user_paused = true;
36976e37fb81SPaolo Bonzini     trace_qmp_block_job_pause(job);
36986e37fb81SPaolo Bonzini     block_job_pause(job);
36993d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
37006e37fb81SPaolo Bonzini }
37016e37fb81SPaolo Bonzini 
37026e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp)
37036e37fb81SPaolo Bonzini {
37043d948cdfSStefan Hajnoczi     AioContext *aio_context;
370524d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
37066e37fb81SPaolo Bonzini 
3707751ebd76SFam Zheng     if (!job || !job->user_paused) {
37088acc72a4SPaolo Bonzini         return;
37098acc72a4SPaolo Bonzini     }
3710370521a1SStefan Hajnoczi 
3711751ebd76SFam Zheng     job->user_paused = false;
37126e37fb81SPaolo Bonzini     trace_qmp_block_job_resume(job);
37136e37fb81SPaolo Bonzini     block_job_resume(job);
37143d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
3715370521a1SStefan Hajnoczi }
3716fb5458cdSStefan Hajnoczi 
3717aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp)
3718aeae883bSPaolo Bonzini {
37193d948cdfSStefan Hajnoczi     AioContext *aio_context;
372024d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
3721aeae883bSPaolo Bonzini 
3722aeae883bSPaolo Bonzini     if (!job) {
3723aeae883bSPaolo Bonzini         return;
3724aeae883bSPaolo Bonzini     }
3725aeae883bSPaolo Bonzini 
3726aeae883bSPaolo Bonzini     trace_qmp_block_job_complete(job);
3727aeae883bSPaolo Bonzini     block_job_complete(job, errp);
37283d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
3729aeae883bSPaolo Bonzini }
3730aeae883bSPaolo Bonzini 
3731fa40e656SJeff Cody void qmp_change_backing_file(const char *device,
3732fa40e656SJeff Cody                              const char *image_node_name,
3733fa40e656SJeff Cody                              const char *backing_file,
3734fa40e656SJeff Cody                              Error **errp)
3735fa40e656SJeff Cody {
3736a0e8544cSFam Zheng     BlockBackend *blk;
3737fa40e656SJeff Cody     BlockDriverState *bs = NULL;
3738729962f6SStefan Hajnoczi     AioContext *aio_context;
3739fa40e656SJeff Cody     BlockDriverState *image_bs = NULL;
3740fa40e656SJeff Cody     Error *local_err = NULL;
3741fa40e656SJeff Cody     bool ro;
3742fa40e656SJeff Cody     int open_flags;
3743fa40e656SJeff Cody     int ret;
3744fa40e656SJeff Cody 
3745a0e8544cSFam Zheng     blk = blk_by_name(device);
3746a0e8544cSFam Zheng     if (!blk) {
374775158ebbSMarkus Armbruster         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
374875158ebbSMarkus Armbruster                   "Device '%s' not found", device);
3749fa40e656SJeff Cody         return;
3750fa40e656SJeff Cody     }
3751fa40e656SJeff Cody 
37525433c24fSMax Reitz     aio_context = blk_get_aio_context(blk);
3753729962f6SStefan Hajnoczi     aio_context_acquire(aio_context);
3754729962f6SStefan Hajnoczi 
37555433c24fSMax Reitz     if (!blk_is_available(blk)) {
37565433c24fSMax Reitz         error_setg(errp, "Device '%s' has no medium", device);
37575433c24fSMax Reitz         goto out;
37585433c24fSMax Reitz     }
37595433c24fSMax Reitz     bs = blk_bs(blk);
37605433c24fSMax Reitz 
3761fa40e656SJeff Cody     image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3762fa40e656SJeff Cody     if (local_err) {
3763fa40e656SJeff Cody         error_propagate(errp, local_err);
3764729962f6SStefan Hajnoczi         goto out;
3765fa40e656SJeff Cody     }
3766fa40e656SJeff Cody 
3767fa40e656SJeff Cody     if (!image_bs) {
3768fa40e656SJeff Cody         error_setg(errp, "image file not found");
3769729962f6SStefan Hajnoczi         goto out;
3770fa40e656SJeff Cody     }
3771fa40e656SJeff Cody 
3772fa40e656SJeff Cody     if (bdrv_find_base(image_bs) == image_bs) {
3773fa40e656SJeff Cody         error_setg(errp, "not allowing backing file change on an image "
3774fa40e656SJeff Cody                          "without a backing file");
3775729962f6SStefan Hajnoczi         goto out;
3776fa40e656SJeff Cody     }
3777fa40e656SJeff Cody 
3778fa40e656SJeff Cody     /* even though we are not necessarily operating on bs, we need it to
3779fa40e656SJeff Cody      * determine if block ops are currently prohibited on the chain */
3780fa40e656SJeff Cody     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3781729962f6SStefan Hajnoczi         goto out;
3782fa40e656SJeff Cody     }
3783fa40e656SJeff Cody 
3784fa40e656SJeff Cody     /* final sanity check */
3785fa40e656SJeff Cody     if (!bdrv_chain_contains(bs, image_bs)) {
3786fa40e656SJeff Cody         error_setg(errp, "'%s' and image file are not in the same chain",
3787fa40e656SJeff Cody                    device);
3788729962f6SStefan Hajnoczi         goto out;
3789fa40e656SJeff Cody     }
3790fa40e656SJeff Cody 
3791fa40e656SJeff Cody     /* if not r/w, reopen to make r/w */
3792fa40e656SJeff Cody     open_flags = image_bs->open_flags;
3793fa40e656SJeff Cody     ro = bdrv_is_read_only(image_bs);
3794fa40e656SJeff Cody 
3795fa40e656SJeff Cody     if (ro) {
3796fa40e656SJeff Cody         bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3797fa40e656SJeff Cody         if (local_err) {
3798fa40e656SJeff Cody             error_propagate(errp, local_err);
3799729962f6SStefan Hajnoczi             goto out;
3800fa40e656SJeff Cody         }
3801fa40e656SJeff Cody     }
3802fa40e656SJeff Cody 
3803fa40e656SJeff Cody     ret = bdrv_change_backing_file(image_bs, backing_file,
3804fa40e656SJeff Cody                                image_bs->drv ? image_bs->drv->format_name : "");
3805fa40e656SJeff Cody 
3806fa40e656SJeff Cody     if (ret < 0) {
3807fa40e656SJeff Cody         error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3808fa40e656SJeff Cody                          backing_file);
3809fa40e656SJeff Cody         /* don't exit here, so we can try to restore open flags if
3810fa40e656SJeff Cody          * appropriate */
3811fa40e656SJeff Cody     }
3812fa40e656SJeff Cody 
3813fa40e656SJeff Cody     if (ro) {
3814fa40e656SJeff Cody         bdrv_reopen(image_bs, open_flags, &local_err);
3815fa40e656SJeff Cody         if (local_err) {
3816fa40e656SJeff Cody             error_propagate(errp, local_err); /* will preserve prior errp */
3817fa40e656SJeff Cody         }
3818fa40e656SJeff Cody     }
3819729962f6SStefan Hajnoczi 
3820729962f6SStefan Hajnoczi out:
3821729962f6SStefan Hajnoczi     aio_context_release(aio_context);
3822fa40e656SJeff Cody }
3823fa40e656SJeff Cody 
3824d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3825d26c9a15SKevin Wolf {
3826d26c9a15SKevin Wolf     QmpOutputVisitor *ov = qmp_output_visitor_new();
3827be4b67bcSMax Reitz     BlockDriverState *bs;
3828be4b67bcSMax Reitz     BlockBackend *blk = NULL;
3829d26c9a15SKevin Wolf     QObject *obj;
3830d26c9a15SKevin Wolf     QDict *qdict;
3831d26c9a15SKevin Wolf     Error *local_err = NULL;
3832d26c9a15SKevin Wolf 
383360e19e06SMarkus Armbruster     /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3834d26c9a15SKevin Wolf      * cache.direct=false instead of silently switching to aio=threads, except
383560e19e06SMarkus Armbruster      * when called from drive_new().
3836d26c9a15SKevin Wolf      *
3837d26c9a15SKevin Wolf      * For now, simply forbidding the combination for all drivers will do. */
3838d26c9a15SKevin Wolf     if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3839c6e0bd9bSKevin Wolf         bool direct = options->has_cache &&
3840c6e0bd9bSKevin Wolf                       options->cache->has_direct &&
3841c6e0bd9bSKevin Wolf                       options->cache->direct;
3842c6e0bd9bSKevin Wolf         if (!direct) {
3843d26c9a15SKevin Wolf             error_setg(errp, "aio=native requires cache.direct=true");
3844d26c9a15SKevin Wolf             goto fail;
3845d26c9a15SKevin Wolf         }
3846d26c9a15SKevin Wolf     }
3847d26c9a15SKevin Wolf 
384851e72bc1SEric Blake     visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options,
384951e72bc1SEric Blake                                &local_err);
385084d18f06SMarkus Armbruster     if (local_err) {
3851d26c9a15SKevin Wolf         error_propagate(errp, local_err);
3852d26c9a15SKevin Wolf         goto fail;
3853d26c9a15SKevin Wolf     }
3854d26c9a15SKevin Wolf 
3855d26c9a15SKevin Wolf     obj = qmp_output_get_qobject(ov);
3856d26c9a15SKevin Wolf     qdict = qobject_to_qdict(obj);
3857d26c9a15SKevin Wolf 
3858d26c9a15SKevin Wolf     qdict_flatten(qdict);
3859d26c9a15SKevin Wolf 
3860be4b67bcSMax Reitz     if (options->has_id) {
386118e46a03SMarkus Armbruster         blk = blockdev_init(NULL, qdict, &local_err);
386284d18f06SMarkus Armbruster         if (local_err) {
3863b681072dSKevin Wolf             error_propagate(errp, local_err);
3864d26c9a15SKevin Wolf             goto fail;
3865d26c9a15SKevin Wolf         }
3866d26c9a15SKevin Wolf 
3867be4b67bcSMax Reitz         bs = blk_bs(blk);
3868be4b67bcSMax Reitz     } else {
3869be4b67bcSMax Reitz         if (!qdict_get_try_str(qdict, "node-name")) {
3870be4b67bcSMax Reitz             error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3871be4b67bcSMax Reitz                        "the root node");
3872be4b67bcSMax Reitz             goto fail;
3873be4b67bcSMax Reitz         }
3874be4b67bcSMax Reitz 
3875bd745e23SMax Reitz         bs = bds_tree_init(qdict, errp);
3876bd745e23SMax Reitz         if (!bs) {
3877be4b67bcSMax Reitz             goto fail;
3878be4b67bcSMax Reitz         }
38799c4218e9SMax Reitz 
38809c4218e9SMax Reitz         QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3881be4b67bcSMax Reitz     }
3882be4b67bcSMax Reitz 
3883be4b67bcSMax Reitz     if (bs && bdrv_key_required(bs)) {
3884be4b67bcSMax Reitz         if (blk) {
388518e46a03SMarkus Armbruster             blk_unref(blk);
3886be4b67bcSMax Reitz         } else {
38879c4218e9SMax Reitz             QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3888be4b67bcSMax Reitz             bdrv_unref(bs);
3889be4b67bcSMax Reitz         }
38908ae8e904SKevin Wolf         error_setg(errp, "blockdev-add doesn't support encrypted devices");
38918ae8e904SKevin Wolf         goto fail;
38928ae8e904SKevin Wolf     }
38938ae8e904SKevin Wolf 
3894d26c9a15SKevin Wolf fail:
3895d26c9a15SKevin Wolf     qmp_output_visitor_cleanup(ov);
3896d26c9a15SKevin Wolf }
3897d26c9a15SKevin Wolf 
389881b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id,
389981b936aeSAlberto Garcia                         bool has_node_name, const char *node_name, Error **errp)
390081b936aeSAlberto Garcia {
390181b936aeSAlberto Garcia     AioContext *aio_context;
390281b936aeSAlberto Garcia     BlockBackend *blk;
390381b936aeSAlberto Garcia     BlockDriverState *bs;
390481b936aeSAlberto Garcia 
390581b936aeSAlberto Garcia     if (has_id && has_node_name) {
390681b936aeSAlberto Garcia         error_setg(errp, "Only one of id and node-name must be specified");
390781b936aeSAlberto Garcia         return;
390881b936aeSAlberto Garcia     } else if (!has_id && !has_node_name) {
390981b936aeSAlberto Garcia         error_setg(errp, "No block device specified");
391081b936aeSAlberto Garcia         return;
391181b936aeSAlberto Garcia     }
391281b936aeSAlberto Garcia 
391381b936aeSAlberto Garcia     if (has_id) {
391481b936aeSAlberto Garcia         blk = blk_by_name(id);
391581b936aeSAlberto Garcia         if (!blk) {
391681b936aeSAlberto Garcia             error_setg(errp, "Cannot find block backend %s", id);
391781b936aeSAlberto Garcia             return;
391881b936aeSAlberto Garcia         }
391981b936aeSAlberto Garcia         if (blk_get_refcnt(blk) > 1) {
392081b936aeSAlberto Garcia             error_setg(errp, "Block backend %s is in use", id);
392181b936aeSAlberto Garcia             return;
392281b936aeSAlberto Garcia         }
392381b936aeSAlberto Garcia         bs = blk_bs(blk);
392481b936aeSAlberto Garcia         aio_context = blk_get_aio_context(blk);
392581b936aeSAlberto Garcia     } else {
392681b936aeSAlberto Garcia         bs = bdrv_find_node(node_name);
392781b936aeSAlberto Garcia         if (!bs) {
392881b936aeSAlberto Garcia             error_setg(errp, "Cannot find node %s", node_name);
392981b936aeSAlberto Garcia             return;
393081b936aeSAlberto Garcia         }
393181b936aeSAlberto Garcia         blk = bs->blk;
393281b936aeSAlberto Garcia         if (blk) {
393381b936aeSAlberto Garcia             error_setg(errp, "Node %s is in use by %s",
393481b936aeSAlberto Garcia                        node_name, blk_name(blk));
393581b936aeSAlberto Garcia             return;
393681b936aeSAlberto Garcia         }
393781b936aeSAlberto Garcia         aio_context = bdrv_get_aio_context(bs);
393881b936aeSAlberto Garcia     }
393981b936aeSAlberto Garcia 
394081b936aeSAlberto Garcia     aio_context_acquire(aio_context);
394181b936aeSAlberto Garcia 
394281b936aeSAlberto Garcia     if (bs) {
394381b936aeSAlberto Garcia         if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
394481b936aeSAlberto Garcia             goto out;
394581b936aeSAlberto Garcia         }
394681b936aeSAlberto Garcia 
39479c4218e9SMax Reitz         if (!blk && !bs->monitor_list.tqe_prev) {
39489c4218e9SMax Reitz             error_setg(errp, "Node %s is not owned by the monitor",
39499c4218e9SMax Reitz                        bs->node_name);
39509c4218e9SMax Reitz             goto out;
39519c4218e9SMax Reitz         }
39529c4218e9SMax Reitz 
39539c4218e9SMax Reitz         if (bs->refcnt > 1) {
395481b936aeSAlberto Garcia             error_setg(errp, "Block device %s is in use",
395581b936aeSAlberto Garcia                        bdrv_get_device_or_node_name(bs));
395681b936aeSAlberto Garcia             goto out;
395781b936aeSAlberto Garcia         }
395881b936aeSAlberto Garcia     }
395981b936aeSAlberto Garcia 
396081b936aeSAlberto Garcia     if (blk) {
396181b936aeSAlberto Garcia         blk_unref(blk);
396281b936aeSAlberto Garcia     } else {
39639c4218e9SMax Reitz         QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
396481b936aeSAlberto Garcia         bdrv_unref(bs);
396581b936aeSAlberto Garcia     }
396681b936aeSAlberto Garcia 
396781b936aeSAlberto Garcia out:
396881b936aeSAlberto Garcia     aio_context_release(aio_context);
396981b936aeSAlberto Garcia }
397081b936aeSAlberto Garcia 
3971fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3972fb5458cdSStefan Hajnoczi {
3973fea68bb6SMarkus Armbruster     BlockJobInfoList *head = NULL, **p_next = &head;
3974fea68bb6SMarkus Armbruster     BlockDriverState *bs;
3975fea68bb6SMarkus Armbruster 
3976fea68bb6SMarkus Armbruster     for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
397769691e72SStefan Hajnoczi         AioContext *aio_context = bdrv_get_aio_context(bs);
397869691e72SStefan Hajnoczi 
397969691e72SStefan Hajnoczi         aio_context_acquire(aio_context);
398069691e72SStefan Hajnoczi 
3981fea68bb6SMarkus Armbruster         if (bs->job) {
3982fea68bb6SMarkus Armbruster             BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3983fea68bb6SMarkus Armbruster             elem->value = block_job_query(bs->job);
3984fea68bb6SMarkus Armbruster             *p_next = elem;
3985fea68bb6SMarkus Armbruster             p_next = &elem->next;
3986fea68bb6SMarkus Armbruster         }
398769691e72SStefan Hajnoczi 
398869691e72SStefan Hajnoczi         aio_context_release(aio_context);
3989fea68bb6SMarkus Armbruster     }
3990fea68bb6SMarkus Armbruster 
3991fea68bb6SMarkus Armbruster     return head;
3992fb5458cdSStefan Hajnoczi }
39934d454574SPaolo Bonzini 
39940006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = {
39954d454574SPaolo Bonzini     .name = "drive",
39960006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
39974d454574SPaolo Bonzini     .desc = {
39984d454574SPaolo Bonzini         {
39994d454574SPaolo Bonzini             .name = "snapshot",
40004d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
40014d454574SPaolo Bonzini             .help = "enable/disable snapshot mode",
40024d454574SPaolo Bonzini         },{
4003a9384affSPaolo Bonzini             .name = "discard",
4004a9384affSPaolo Bonzini             .type = QEMU_OPT_STRING,
4005a9384affSPaolo Bonzini             .help = "discard operation (ignore/off, unmap/on)",
4006a9384affSPaolo Bonzini         },{
40074d454574SPaolo Bonzini             .name = "aio",
40084d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40094d454574SPaolo Bonzini             .help = "host AIO implementation (threads, native)",
40104d454574SPaolo Bonzini         },{
40114d454574SPaolo Bonzini             .name = "format",
40124d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40134d454574SPaolo Bonzini             .help = "disk format (raw, qcow2, ...)",
40144d454574SPaolo Bonzini         },{
40154d454574SPaolo Bonzini             .name = "rerror",
40164d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40174d454574SPaolo Bonzini             .help = "read error action",
40184d454574SPaolo Bonzini         },{
40194d454574SPaolo Bonzini             .name = "werror",
40204d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
40214d454574SPaolo Bonzini             .help = "write error action",
40224d454574SPaolo Bonzini         },{
40230f227a94SKevin Wolf             .name = "read-only",
40244d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
40254d454574SPaolo Bonzini             .help = "open drive file as read-only",
40264d454574SPaolo Bonzini         },{
402757975222SKevin Wolf             .name = "throttling.iops-total",
40284d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40294d454574SPaolo Bonzini             .help = "limit total I/O operations per second",
40304d454574SPaolo Bonzini         },{
403157975222SKevin Wolf             .name = "throttling.iops-read",
40324d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40334d454574SPaolo Bonzini             .help = "limit read operations per second",
40344d454574SPaolo Bonzini         },{
403557975222SKevin Wolf             .name = "throttling.iops-write",
40364d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40374d454574SPaolo Bonzini             .help = "limit write operations per second",
40384d454574SPaolo Bonzini         },{
403957975222SKevin Wolf             .name = "throttling.bps-total",
40404d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40414d454574SPaolo Bonzini             .help = "limit total bytes per second",
40424d454574SPaolo Bonzini         },{
404357975222SKevin Wolf             .name = "throttling.bps-read",
40444d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40454d454574SPaolo Bonzini             .help = "limit read bytes per second",
40464d454574SPaolo Bonzini         },{
404757975222SKevin Wolf             .name = "throttling.bps-write",
40484d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
40494d454574SPaolo Bonzini             .help = "limit write bytes per second",
40504d454574SPaolo Bonzini         },{
40513e9fab69SBenoît Canet             .name = "throttling.iops-total-max",
40523e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40533e9fab69SBenoît Canet             .help = "I/O operations burst",
40543e9fab69SBenoît Canet         },{
40553e9fab69SBenoît Canet             .name = "throttling.iops-read-max",
40563e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40573e9fab69SBenoît Canet             .help = "I/O operations read burst",
40583e9fab69SBenoît Canet         },{
40593e9fab69SBenoît Canet             .name = "throttling.iops-write-max",
40603e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40613e9fab69SBenoît Canet             .help = "I/O operations write burst",
40623e9fab69SBenoît Canet         },{
40633e9fab69SBenoît Canet             .name = "throttling.bps-total-max",
40643e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40653e9fab69SBenoît Canet             .help = "total bytes burst",
40663e9fab69SBenoît Canet         },{
40673e9fab69SBenoît Canet             .name = "throttling.bps-read-max",
40683e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40693e9fab69SBenoît Canet             .help = "total bytes read burst",
40703e9fab69SBenoît Canet         },{
40713e9fab69SBenoît Canet             .name = "throttling.bps-write-max",
40723e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
40733e9fab69SBenoît Canet             .help = "total bytes write burst",
40743e9fab69SBenoît Canet         },{
40752024c1dfSBenoît Canet             .name = "throttling.iops-size",
40762024c1dfSBenoît Canet             .type = QEMU_OPT_NUMBER,
40772024c1dfSBenoît Canet             .help = "when limiting by iops max size of an I/O in bytes",
40782024c1dfSBenoît Canet         },{
407976f4afb4SAlberto Garcia             .name = "throttling.group",
408076f4afb4SAlberto Garcia             .type = QEMU_OPT_STRING,
408176f4afb4SAlberto Garcia             .help = "name of the block throttling group",
408276f4afb4SAlberto Garcia         },{
40834d454574SPaolo Bonzini             .name = "copy-on-read",
40844d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
40854d454574SPaolo Bonzini             .help = "copy read data from backing file into image file",
4086465bee1dSPeter Lieven         },{
4087465bee1dSPeter Lieven             .name = "detect-zeroes",
4088465bee1dSPeter Lieven             .type = QEMU_OPT_STRING,
4089465bee1dSPeter Lieven             .help = "try to optimize zero writes (off, on, unmap)",
4090362e9299SAlberto Garcia         },{
4091362e9299SAlberto Garcia             .name = "stats-account-invalid",
4092362e9299SAlberto Garcia             .type = QEMU_OPT_BOOL,
4093362e9299SAlberto Garcia             .help = "whether to account for invalid I/O operations "
4094362e9299SAlberto Garcia                     "in the statistics",
4095362e9299SAlberto Garcia         },{
4096362e9299SAlberto Garcia             .name = "stats-account-failed",
4097362e9299SAlberto Garcia             .type = QEMU_OPT_BOOL,
4098362e9299SAlberto Garcia             .help = "whether to account for failed I/O operations "
4099362e9299SAlberto Garcia                     "in the statistics",
41004d454574SPaolo Bonzini         },
41014d454574SPaolo Bonzini         { /* end of list */ }
41024d454574SPaolo Bonzini     },
41034d454574SPaolo Bonzini };
41040006383eSKevin Wolf 
4105bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = {
4106bd745e23SMax Reitz     .name = "root-bds",
4107bd745e23SMax Reitz     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4108bd745e23SMax Reitz     .desc = {
4109bd745e23SMax Reitz         {
4110bd745e23SMax Reitz             .name = "discard",
4111bd745e23SMax Reitz             .type = QEMU_OPT_STRING,
4112bd745e23SMax Reitz             .help = "discard operation (ignore/off, unmap/on)",
4113bd745e23SMax Reitz         },{
4114bd745e23SMax Reitz             .name = "aio",
4115bd745e23SMax Reitz             .type = QEMU_OPT_STRING,
4116bd745e23SMax Reitz             .help = "host AIO implementation (threads, native)",
4117bd745e23SMax Reitz         },{
4118bd745e23SMax Reitz             .name = "read-only",
4119bd745e23SMax Reitz             .type = QEMU_OPT_BOOL,
4120bd745e23SMax Reitz             .help = "open drive file as read-only",
4121bd745e23SMax Reitz         },{
4122bd745e23SMax Reitz             .name = "copy-on-read",
4123bd745e23SMax Reitz             .type = QEMU_OPT_BOOL,
4124bd745e23SMax Reitz             .help = "copy read data from backing file into image file",
4125bd745e23SMax Reitz         },{
4126bd745e23SMax Reitz             .name = "detect-zeroes",
4127bd745e23SMax Reitz             .type = QEMU_OPT_STRING,
4128bd745e23SMax Reitz             .help = "try to optimize zero writes (off, on, unmap)",
4129bd745e23SMax Reitz         },
4130bd745e23SMax Reitz         { /* end of list */ }
4131bd745e23SMax Reitz     },
4132bd745e23SMax Reitz };
4133bd745e23SMax Reitz 
41340006383eSKevin Wolf QemuOptsList qemu_drive_opts = {
41350006383eSKevin Wolf     .name = "drive",
41360006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
41370006383eSKevin Wolf     .desc = {
4138492fdc6fSKevin Wolf         /*
4139492fdc6fSKevin Wolf          * no elements => accept any params
4140492fdc6fSKevin Wolf          * validation will happen later
4141492fdc6fSKevin Wolf          */
41420006383eSKevin Wolf         { /* end of list */ }
41430006383eSKevin Wolf     },
41440006383eSKevin Wolf };
4145