xref: /openbmc/qemu/blockdev.c (revision d5a8ee60a0fbc20a2c2d02f3bda1bb1bd365f1ee)
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 
3326f54e9aSMarkus Armbruster #include "sysemu/block-backend.h"
349c17d615SPaolo Bonzini #include "sysemu/blockdev.h"
350d09e41aSPaolo Bonzini #include "hw/block/block.h"
36737e150eSPaolo Bonzini #include "block/blockjob.h"
3783c9089eSPaolo Bonzini #include "monitor/monitor.h"
381de7afc9SPaolo Bonzini #include "qemu/option.h"
391de7afc9SPaolo Bonzini #include "qemu/config-file.h"
407b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h"
41d26c9a15SKevin Wolf #include "qapi-visit.h"
42d26c9a15SKevin Wolf #include "qapi/qmp-output-visitor.h"
439e7dac7cSPeter Lieven #include "qapi/util.h"
449c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
45737e150eSPaolo Bonzini #include "block/block_int.h"
46a4dea8a9SLuiz Capitulino #include "qmp-commands.h"
4712bd451fSStefan Hajnoczi #include "trace.h"
489c17d615SPaolo Bonzini #include "sysemu/arch_init.h"
49666daa68SMarkus Armbruster 
501960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = {
511960966dSMarkus Armbruster     [IF_NONE] = "none",
521960966dSMarkus Armbruster     [IF_IDE] = "ide",
531960966dSMarkus Armbruster     [IF_SCSI] = "scsi",
541960966dSMarkus Armbruster     [IF_FLOPPY] = "floppy",
551960966dSMarkus Armbruster     [IF_PFLASH] = "pflash",
561960966dSMarkus Armbruster     [IF_MTD] = "mtd",
571960966dSMarkus Armbruster     [IF_SD] = "sd",
581960966dSMarkus Armbruster     [IF_VIRTIO] = "virtio",
591960966dSMarkus Armbruster     [IF_XEN] = "xen",
601960966dSMarkus Armbruster };
611960966dSMarkus Armbruster 
6221dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = {
6327d6bf40SMarkus Armbruster     /*
6427d6bf40SMarkus Armbruster      * Do not change these numbers!  They govern how drive option
6527d6bf40SMarkus Armbruster      * index maps to unit and bus.  That mapping is ABI.
6627d6bf40SMarkus Armbruster      *
6727d6bf40SMarkus Armbruster      * All controllers used to imlement if=T drives need to support
6827d6bf40SMarkus Armbruster      * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
6927d6bf40SMarkus Armbruster      * Otherwise, some index values map to "impossible" bus, unit
7027d6bf40SMarkus Armbruster      * values.
7127d6bf40SMarkus Armbruster      *
7227d6bf40SMarkus Armbruster      * For instance, if you change [IF_SCSI] to 255, -drive
7327d6bf40SMarkus Armbruster      * if=scsi,index=12 no longer means bus=1,unit=5, but
7427d6bf40SMarkus Armbruster      * bus=0,unit=12.  With an lsi53c895a controller (7 units max),
7527d6bf40SMarkus Armbruster      * the drive can't be set up.  Regression.
7627d6bf40SMarkus Armbruster      */
7727d6bf40SMarkus Armbruster     [IF_IDE] = 2,
7827d6bf40SMarkus Armbruster     [IF_SCSI] = 7,
791960966dSMarkus Armbruster };
801960966dSMarkus Armbruster 
8121dff8cfSJohn Snow /**
8221dff8cfSJohn Snow  * Boards may call this to offer board-by-board overrides
8321dff8cfSJohn Snow  * of the default, global values.
8421dff8cfSJohn Snow  */
8521dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs)
8621dff8cfSJohn Snow {
8718e46a03SMarkus Armbruster     BlockBackend *blk;
8821dff8cfSJohn Snow     DriveInfo *dinfo;
8921dff8cfSJohn Snow 
9021dff8cfSJohn Snow     if (max_devs <= 0) {
9121dff8cfSJohn Snow         return;
9221dff8cfSJohn Snow     }
9321dff8cfSJohn Snow 
9418e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
9518e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
9621dff8cfSJohn Snow         if (dinfo->type == type) {
9721dff8cfSJohn Snow             fprintf(stderr, "Cannot override units-per-bus property of"
9821dff8cfSJohn Snow                     " the %s interface, because a drive of that type has"
9921dff8cfSJohn Snow                     " already been added.\n", if_name[type]);
10021dff8cfSJohn Snow             g_assert_not_reached();
10121dff8cfSJohn Snow         }
10221dff8cfSJohn Snow     }
10321dff8cfSJohn Snow 
10421dff8cfSJohn Snow     if_max_devs[type] = max_devs;
10521dff8cfSJohn Snow }
10621dff8cfSJohn Snow 
10714bafc54SMarkus Armbruster /*
10814bafc54SMarkus Armbruster  * We automatically delete the drive when a device using it gets
10914bafc54SMarkus Armbruster  * unplugged.  Questionable feature, but we can't just drop it.
11014bafc54SMarkus Armbruster  * Device models call blockdev_mark_auto_del() to schedule the
11114bafc54SMarkus Armbruster  * automatic deletion, and generic qdev code calls blockdev_auto_del()
11214bafc54SMarkus Armbruster  * when deletion is actually safe.
11314bafc54SMarkus Armbruster  */
1144be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk)
11514bafc54SMarkus Armbruster {
11618e46a03SMarkus Armbruster     DriveInfo *dinfo = blk_legacy_dinfo(blk);
1174be74634SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
11891fddb0dSStefan Hajnoczi     AioContext *aio_context;
11914bafc54SMarkus Armbruster 
12026f8b3a8SMarkus Armbruster     if (!dinfo) {
1212d246f01SKevin Wolf         return;
1222d246f01SKevin Wolf     }
1232d246f01SKevin Wolf 
12491fddb0dSStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
12591fddb0dSStefan Hajnoczi     aio_context_acquire(aio_context);
12691fddb0dSStefan Hajnoczi 
12712bde0eeSPaolo Bonzini     if (bs->job) {
12812bde0eeSPaolo Bonzini         block_job_cancel(bs->job);
12912bde0eeSPaolo Bonzini     }
13091fddb0dSStefan Hajnoczi 
13191fddb0dSStefan Hajnoczi     aio_context_release(aio_context);
13291fddb0dSStefan Hajnoczi 
13314bafc54SMarkus Armbruster     dinfo->auto_del = 1;
13414bafc54SMarkus Armbruster }
13514bafc54SMarkus Armbruster 
1364be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk)
13714bafc54SMarkus Armbruster {
13818e46a03SMarkus Armbruster     DriveInfo *dinfo = blk_legacy_dinfo(blk);
13914bafc54SMarkus Armbruster 
1400fc0f1faSRyan Harper     if (dinfo && dinfo->auto_del) {
141b9fe8a7aSMarkus Armbruster         blk_unref(blk);
14214bafc54SMarkus Armbruster     }
14314bafc54SMarkus Armbruster }
14414bafc54SMarkus Armbruster 
145d8f94e1bSJohn Snow /**
146d8f94e1bSJohn Snow  * Returns the current mapping of how many units per bus
147d8f94e1bSJohn Snow  * a particular interface can support.
148d8f94e1bSJohn Snow  *
149d8f94e1bSJohn Snow  *  A positive integer indicates n units per bus.
150d8f94e1bSJohn Snow  *  0 implies the mapping has not been established.
151d8f94e1bSJohn Snow  * -1 indicates an invalid BlockInterfaceType was given.
152d8f94e1bSJohn Snow  */
153d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type)
154d8f94e1bSJohn Snow {
155d8f94e1bSJohn Snow     if (type >= IF_IDE && type < IF_COUNT) {
156d8f94e1bSJohn Snow         return if_max_devs[type];
157d8f94e1bSJohn Snow     }
158d8f94e1bSJohn Snow 
159d8f94e1bSJohn Snow     return -1;
160d8f94e1bSJohn Snow }
161d8f94e1bSJohn Snow 
162505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index)
163505a7fb1SMarkus Armbruster {
164505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
165505a7fb1SMarkus Armbruster     return max_devs ? index / max_devs : 0;
166505a7fb1SMarkus Armbruster }
167505a7fb1SMarkus Armbruster 
168505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index)
169505a7fb1SMarkus Armbruster {
170505a7fb1SMarkus Armbruster     int max_devs = if_max_devs[type];
171505a7fb1SMarkus Armbruster     return max_devs ? index % max_devs : index;
172505a7fb1SMarkus Armbruster }
173505a7fb1SMarkus Armbruster 
1742292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr)
1752292ddaeSMarkus Armbruster {
1762292ddaeSMarkus Armbruster     return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
1772292ddaeSMarkus Armbruster }
1782292ddaeSMarkus Armbruster 
1792292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
1805645b0f4SMarkus Armbruster                     const char *optstr)
181666daa68SMarkus Armbruster {
182666daa68SMarkus Armbruster     QemuOpts *opts;
183666daa68SMarkus Armbruster 
1842292ddaeSMarkus Armbruster     opts = drive_def(optstr);
185666daa68SMarkus Armbruster     if (!opts) {
186666daa68SMarkus Armbruster         return NULL;
187666daa68SMarkus Armbruster     }
1882292ddaeSMarkus Armbruster     if (type != IF_DEFAULT) {
189f43e47dbSMarkus Armbruster         qemu_opt_set(opts, "if", if_name[type], &error_abort);
1902292ddaeSMarkus Armbruster     }
1912292ddaeSMarkus Armbruster     if (index >= 0) {
192a8b18f8fSMarkus Armbruster         qemu_opt_set_number(opts, "index", index, &error_abort);
1932292ddaeSMarkus Armbruster     }
194666daa68SMarkus Armbruster     if (file)
195f43e47dbSMarkus Armbruster         qemu_opt_set(opts, "file", file, &error_abort);
196666daa68SMarkus Armbruster     return opts;
197666daa68SMarkus Armbruster }
198666daa68SMarkus Armbruster 
199666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
200666daa68SMarkus Armbruster {
20118e46a03SMarkus Armbruster     BlockBackend *blk;
202666daa68SMarkus Armbruster     DriveInfo *dinfo;
203666daa68SMarkus Armbruster 
20418e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
20518e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
20618e46a03SMarkus Armbruster         if (dinfo && dinfo->type == type
20718e46a03SMarkus Armbruster             && dinfo->bus == bus && dinfo->unit == unit) {
208666daa68SMarkus Armbruster             return dinfo;
209666daa68SMarkus Armbruster         }
21018e46a03SMarkus Armbruster     }
211666daa68SMarkus Armbruster 
212666daa68SMarkus Armbruster     return NULL;
213666daa68SMarkus Armbruster }
214666daa68SMarkus Armbruster 
215a66c9dc7SJohn Snow bool drive_check_orphaned(void)
216a66c9dc7SJohn Snow {
21718e46a03SMarkus Armbruster     BlockBackend *blk;
218a66c9dc7SJohn Snow     DriveInfo *dinfo;
219a66c9dc7SJohn Snow     bool rs = false;
220a66c9dc7SJohn Snow 
22118e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
22218e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
223a66c9dc7SJohn Snow         /* If dinfo->bdrv->dev is NULL, it has no device attached. */
224a66c9dc7SJohn Snow         /* Unless this is a default drive, this may be an oversight. */
225a7f53e26SMarkus Armbruster         if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
226a66c9dc7SJohn Snow             dinfo->type != IF_NONE) {
227a66c9dc7SJohn Snow             fprintf(stderr, "Warning: Orphaned drive without device: "
228a66c9dc7SJohn Snow                     "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
229d3aeb1b7SMarkus Armbruster                     blk_name(blk), blk_bs(blk)->filename, if_name[dinfo->type],
230a66c9dc7SJohn Snow                     dinfo->bus, dinfo->unit);
231a66c9dc7SJohn Snow             rs = true;
232a66c9dc7SJohn Snow         }
233a66c9dc7SJohn Snow     }
234a66c9dc7SJohn Snow 
235a66c9dc7SJohn Snow     return rs;
236a66c9dc7SJohn Snow }
237a66c9dc7SJohn Snow 
238f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
239f1bd51acSMarkus Armbruster {
240f1bd51acSMarkus Armbruster     return drive_get(type,
241f1bd51acSMarkus Armbruster                      drive_index_to_bus_id(type, index),
242f1bd51acSMarkus Armbruster                      drive_index_to_unit_id(type, index));
243f1bd51acSMarkus Armbruster }
244f1bd51acSMarkus Armbruster 
245666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type)
246666daa68SMarkus Armbruster {
247666daa68SMarkus Armbruster     int max_bus;
24818e46a03SMarkus Armbruster     BlockBackend *blk;
249666daa68SMarkus Armbruster     DriveInfo *dinfo;
250666daa68SMarkus Armbruster 
251666daa68SMarkus Armbruster     max_bus = -1;
25218e46a03SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
25318e46a03SMarkus Armbruster         dinfo = blk_legacy_dinfo(blk);
25418e46a03SMarkus Armbruster         if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
255666daa68SMarkus Armbruster             max_bus = dinfo->bus;
256666daa68SMarkus Armbruster         }
25718e46a03SMarkus Armbruster     }
258666daa68SMarkus Armbruster     return max_bus;
259666daa68SMarkus Armbruster }
260666daa68SMarkus Armbruster 
26113839974SMarkus Armbruster /* Get a block device.  This should only be used for single-drive devices
26213839974SMarkus Armbruster    (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
26313839974SMarkus Armbruster    appropriate bus.  */
26413839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type)
26513839974SMarkus Armbruster {
26613839974SMarkus Armbruster     static int next_block_unit[IF_COUNT];
26713839974SMarkus Armbruster 
26813839974SMarkus Armbruster     return drive_get(type, 0, next_block_unit[type]++);
26913839974SMarkus Armbruster }
27013839974SMarkus Armbruster 
271666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name)
272666daa68SMarkus Armbruster {
273807105a7SMarkus Armbruster     error_printf(" %s", name);
274666daa68SMarkus Armbruster }
275666daa68SMarkus Armbruster 
276aa398a5cSStefan Hajnoczi typedef struct {
277aa398a5cSStefan Hajnoczi     QEMUBH *bh;
278fa510ebfSFam Zheng     BlockDriverState *bs;
279fa510ebfSFam Zheng } BDRVPutRefBH;
280aa398a5cSStefan Hajnoczi 
281fa510ebfSFam Zheng static void bdrv_put_ref_bh(void *opaque)
282aa398a5cSStefan Hajnoczi {
283fa510ebfSFam Zheng     BDRVPutRefBH *s = opaque;
284aa398a5cSStefan Hajnoczi 
285fa510ebfSFam Zheng     bdrv_unref(s->bs);
286aa398a5cSStefan Hajnoczi     qemu_bh_delete(s->bh);
287aa398a5cSStefan Hajnoczi     g_free(s);
288aa398a5cSStefan Hajnoczi }
289aa398a5cSStefan Hajnoczi 
290aa398a5cSStefan Hajnoczi /*
291fa510ebfSFam Zheng  * Release a BDS reference in a BH
292aa398a5cSStefan Hajnoczi  *
293fa510ebfSFam Zheng  * It is not safe to use bdrv_unref() from a callback function when the callers
294fa510ebfSFam Zheng  * still need the BlockDriverState.  In such cases we schedule a BH to release
295fa510ebfSFam Zheng  * the reference.
296aa398a5cSStefan Hajnoczi  */
297fa510ebfSFam Zheng static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
298aa398a5cSStefan Hajnoczi {
299fa510ebfSFam Zheng     BDRVPutRefBH *s;
300aa398a5cSStefan Hajnoczi 
301fa510ebfSFam Zheng     s = g_new(BDRVPutRefBH, 1);
302fa510ebfSFam Zheng     s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
303fa510ebfSFam Zheng     s->bs = bs;
304aa398a5cSStefan Hajnoczi     qemu_bh_schedule(s->bh);
305aa398a5cSStefan Hajnoczi }
306aa398a5cSStefan Hajnoczi 
307b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
308666daa68SMarkus Armbruster {
309666daa68SMarkus Armbruster     if (!strcmp(buf, "ignore")) {
31092aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_IGNORE;
311666daa68SMarkus Armbruster     } else if (!is_read && !strcmp(buf, "enospc")) {
31292aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_ENOSPC;
313666daa68SMarkus Armbruster     } else if (!strcmp(buf, "stop")) {
31492aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_STOP;
315666daa68SMarkus Armbruster     } else if (!strcmp(buf, "report")) {
31692aa5c6dSPaolo Bonzini         return BLOCKDEV_ON_ERROR_REPORT;
317666daa68SMarkus Armbruster     } else {
318b681072dSKevin Wolf         error_setg(errp, "'%s' invalid %s error action",
319666daa68SMarkus Armbruster                    buf, is_read ? "read" : "write");
320666daa68SMarkus Armbruster         return -1;
321666daa68SMarkus Armbruster     }
322666daa68SMarkus Armbruster }
323666daa68SMarkus Armbruster 
324cc0681c4SBenoît Canet static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
3250563e191SZhi Yong Wu {
326cc0681c4SBenoît Canet     if (throttle_conflicting(cfg)) {
327cc0681c4SBenoît Canet         error_setg(errp, "bps/iops/max total values and read/write values"
328c546194fSStefan Hajnoczi                          " cannot be used at the same time");
3290563e191SZhi Yong Wu         return false;
3300563e191SZhi Yong Wu     }
3310563e191SZhi Yong Wu 
332cc0681c4SBenoît Canet     if (!throttle_is_valid(cfg)) {
333cc0681c4SBenoît Canet         error_setg(errp, "bps/iops/maxs values must be 0 or greater");
3347d81c141SStefan Hajnoczi         return false;
3357d81c141SStefan Hajnoczi     }
3367d81c141SStefan Hajnoczi 
3370563e191SZhi Yong Wu     return true;
3380563e191SZhi Yong Wu }
3390563e191SZhi Yong Wu 
34033cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
34133cb7dc8SKevin Wolf 
342f298d071SKevin Wolf /* Takes the ownership of bs_opts */
34318e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
344b681072dSKevin Wolf                                    Error **errp)
345666daa68SMarkus Armbruster {
346666daa68SMarkus Armbruster     const char *buf;
347666daa68SMarkus Armbruster     int ro = 0;
348666daa68SMarkus Armbruster     int bdrv_flags = 0;
349666daa68SMarkus Armbruster     int on_read_error, on_write_error;
35026f54e9aSMarkus Armbruster     BlockBackend *blk;
351a0f1eab1SMarkus Armbruster     BlockDriverState *bs;
352cc0681c4SBenoît Canet     ThrottleConfig cfg;
353666daa68SMarkus Armbruster     int snapshot = 0;
354fb0490f6SStefan Hajnoczi     bool copy_on_read;
355c546194fSStefan Hajnoczi     Error *error = NULL;
3560006383eSKevin Wolf     QemuOpts *opts;
3570006383eSKevin Wolf     const char *id;
35874fe54f2SKevin Wolf     bool has_driver_specific_opts;
359465bee1dSPeter Lieven     BlockdevDetectZeroesOptions detect_zeroes;
360666daa68SMarkus Armbruster 
361f298d071SKevin Wolf     /* Check common options by copying from bs_opts to opts, all other options
362f298d071SKevin Wolf      * stay in bs_opts for processing by bdrv_open(). */
363f298d071SKevin Wolf     id = qdict_get_try_str(bs_opts, "id");
3640006383eSKevin Wolf     opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
36584d18f06SMarkus Armbruster     if (error) {
366b681072dSKevin Wolf         error_propagate(errp, error);
3676376f952SMarkus Armbruster         goto err_no_opts;
3680006383eSKevin Wolf     }
3690006383eSKevin Wolf 
3700006383eSKevin Wolf     qemu_opts_absorb_qdict(opts, bs_opts, &error);
37184d18f06SMarkus Armbruster     if (error) {
372b681072dSKevin Wolf         error_propagate(errp, error);
373ec9c10d2SStefan Hajnoczi         goto early_err;
3740006383eSKevin Wolf     }
3750006383eSKevin Wolf 
3760006383eSKevin Wolf     if (id) {
3770006383eSKevin Wolf         qdict_del(bs_opts, "id");
3780006383eSKevin Wolf     }
3790006383eSKevin Wolf 
38074fe54f2SKevin Wolf     has_driver_specific_opts = !!qdict_size(bs_opts);
38174fe54f2SKevin Wolf 
382666daa68SMarkus Armbruster     /* extract parameters */
383666daa68SMarkus Armbruster     snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
3840f227a94SKevin Wolf     ro = qemu_opt_get_bool(opts, "read-only", 0);
385fb0490f6SStefan Hajnoczi     copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
386666daa68SMarkus Armbruster 
387a9384affSPaolo Bonzini     if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
388a9384affSPaolo Bonzini         if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
389b681072dSKevin Wolf             error_setg(errp, "invalid discard option");
390ec9c10d2SStefan Hajnoczi             goto early_err;
391a9384affSPaolo Bonzini         }
392a9384affSPaolo Bonzini     }
393a9384affSPaolo Bonzini 
39429c4e2b5SKevin Wolf     if (qemu_opt_get_bool(opts, "cache.writeback", true)) {
3951f212b9dSPaolo Bonzini         bdrv_flags |= BDRV_O_CACHE_WB;
396666daa68SMarkus Armbruster     }
39729c4e2b5SKevin Wolf     if (qemu_opt_get_bool(opts, "cache.direct", false)) {
39829c4e2b5SKevin Wolf         bdrv_flags |= BDRV_O_NOCACHE;
39929c4e2b5SKevin Wolf     }
4001df6fa4bSPaolo Bonzini     if (qemu_opt_get_bool(opts, "cache.no-flush", false)) {
40129c4e2b5SKevin Wolf         bdrv_flags |= BDRV_O_NO_FLUSH;
402666daa68SMarkus Armbruster     }
403666daa68SMarkus Armbruster 
404666daa68SMarkus Armbruster #ifdef CONFIG_LINUX_AIO
405666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
406666daa68SMarkus Armbruster         if (!strcmp(buf, "native")) {
407666daa68SMarkus Armbruster             bdrv_flags |= BDRV_O_NATIVE_AIO;
408666daa68SMarkus Armbruster         } else if (!strcmp(buf, "threads")) {
409666daa68SMarkus Armbruster             /* this is the default */
410666daa68SMarkus Armbruster         } else {
411b681072dSKevin Wolf            error_setg(errp, "invalid aio option");
412ec9c10d2SStefan Hajnoczi            goto early_err;
413666daa68SMarkus Armbruster         }
414666daa68SMarkus Armbruster     }
415666daa68SMarkus Armbruster #endif
416666daa68SMarkus Armbruster 
417666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "format")) != NULL) {
418c8057f95SPeter Maydell         if (is_help_option(buf)) {
419807105a7SMarkus Armbruster             error_printf("Supported formats:");
420666daa68SMarkus Armbruster             bdrv_iterate_format(bdrv_format_print, NULL);
421807105a7SMarkus Armbruster             error_printf("\n");
422ec9c10d2SStefan Hajnoczi             goto early_err;
423666daa68SMarkus Armbruster         }
42474fe54f2SKevin Wolf 
425e4342ce5SMax Reitz         if (qdict_haskey(bs_opts, "driver")) {
426e4342ce5SMax Reitz             error_setg(errp, "Cannot specify both 'driver' and 'format'");
427ec9c10d2SStefan Hajnoczi             goto early_err;
4286db5f5d6SMike Qiu         }
429e4342ce5SMax Reitz         qdict_put(bs_opts, "driver", qstring_from_str(buf));
430666daa68SMarkus Armbruster     }
431666daa68SMarkus Armbruster 
4320563e191SZhi Yong Wu     /* disk I/O throttling */
433cc0681c4SBenoît Canet     memset(&cfg, 0, sizeof(cfg));
434cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_TOTAL].avg =
43557975222SKevin Wolf         qemu_opt_get_number(opts, "throttling.bps-total", 0);
436cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_READ].avg  =
43757975222SKevin Wolf         qemu_opt_get_number(opts, "throttling.bps-read", 0);
438cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_WRITE].avg =
43957975222SKevin Wolf         qemu_opt_get_number(opts, "throttling.bps-write", 0);
440cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_TOTAL].avg =
44157975222SKevin Wolf         qemu_opt_get_number(opts, "throttling.iops-total", 0);
442cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_READ].avg =
44357975222SKevin Wolf         qemu_opt_get_number(opts, "throttling.iops-read", 0);
444cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_WRITE].avg =
44557975222SKevin Wolf         qemu_opt_get_number(opts, "throttling.iops-write", 0);
4460563e191SZhi Yong Wu 
4473e9fab69SBenoît Canet     cfg.buckets[THROTTLE_BPS_TOTAL].max =
4483e9fab69SBenoît Canet         qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
4493e9fab69SBenoît Canet     cfg.buckets[THROTTLE_BPS_READ].max  =
4503e9fab69SBenoît Canet         qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
4513e9fab69SBenoît Canet     cfg.buckets[THROTTLE_BPS_WRITE].max =
4523e9fab69SBenoît Canet         qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
4533e9fab69SBenoît Canet     cfg.buckets[THROTTLE_OPS_TOTAL].max =
4543e9fab69SBenoît Canet         qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
4553e9fab69SBenoît Canet     cfg.buckets[THROTTLE_OPS_READ].max =
4563e9fab69SBenoît Canet         qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
4573e9fab69SBenoît Canet     cfg.buckets[THROTTLE_OPS_WRITE].max =
4583e9fab69SBenoît Canet         qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
459cc0681c4SBenoît Canet 
4602024c1dfSBenoît Canet     cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
461cc0681c4SBenoît Canet 
462cc0681c4SBenoît Canet     if (!check_throttle_config(&cfg, &error)) {
463b681072dSKevin Wolf         error_propagate(errp, error);
464ec9c10d2SStefan Hajnoczi         goto early_err;
4650563e191SZhi Yong Wu     }
4660563e191SZhi Yong Wu 
46792aa5c6dSPaolo Bonzini     on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
468666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
469b681072dSKevin Wolf         on_write_error = parse_block_error_action(buf, 0, &error);
47084d18f06SMarkus Armbruster         if (error) {
471b681072dSKevin Wolf             error_propagate(errp, error);
472ec9c10d2SStefan Hajnoczi             goto early_err;
473666daa68SMarkus Armbruster         }
474666daa68SMarkus Armbruster     }
475666daa68SMarkus Armbruster 
47692aa5c6dSPaolo Bonzini     on_read_error = BLOCKDEV_ON_ERROR_REPORT;
477666daa68SMarkus Armbruster     if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
478b681072dSKevin Wolf         on_read_error = parse_block_error_action(buf, 1, &error);
47984d18f06SMarkus Armbruster         if (error) {
480b681072dSKevin Wolf             error_propagate(errp, error);
481ec9c10d2SStefan Hajnoczi             goto early_err;
482666daa68SMarkus Armbruster         }
483666daa68SMarkus Armbruster     }
484666daa68SMarkus Armbruster 
485465bee1dSPeter Lieven     detect_zeroes =
4869e7dac7cSPeter Lieven         qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
487465bee1dSPeter Lieven                         qemu_opt_get(opts, "detect-zeroes"),
488465bee1dSPeter Lieven                         BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
489465bee1dSPeter Lieven                         BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
490465bee1dSPeter Lieven                         &error);
491465bee1dSPeter Lieven     if (error) {
492465bee1dSPeter Lieven         error_propagate(errp, error);
493465bee1dSPeter Lieven         goto early_err;
494465bee1dSPeter Lieven     }
495465bee1dSPeter Lieven 
496465bee1dSPeter Lieven     if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
497465bee1dSPeter Lieven         !(bdrv_flags & BDRV_O_UNMAP)) {
498465bee1dSPeter Lieven         error_setg(errp, "setting detect-zeroes to unmap is not allowed "
499465bee1dSPeter Lieven                          "without setting discard operation to unmap");
500465bee1dSPeter Lieven         goto early_err;
501465bee1dSPeter Lieven     }
502465bee1dSPeter Lieven 
503326642bcSKevin Wolf     /* init */
504e4342ce5SMax Reitz     if ((!file || !*file) && !has_driver_specific_opts) {
5057e7d56d9SMarkus Armbruster         blk = blk_new_with_bs(qemu_opts_id(opts), errp);
50626f54e9aSMarkus Armbruster         if (!blk) {
50726f54e9aSMarkus Armbruster             goto early_err;
50826f54e9aSMarkus Armbruster         }
509e4342ce5SMax Reitz 
5107e7d56d9SMarkus Armbruster         bs = blk_bs(blk);
511a0f1eab1SMarkus Armbruster         bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
512a0f1eab1SMarkus Armbruster         bs->read_only = ro;
513666daa68SMarkus Armbruster 
514ec9c10d2SStefan Hajnoczi         QDECREF(bs_opts);
515e4342ce5SMax Reitz     } else {
516e4342ce5SMax Reitz         if (file && !*file) {
517e4342ce5SMax Reitz             file = NULL;
518666daa68SMarkus Armbruster         }
519e4342ce5SMax Reitz 
520666daa68SMarkus Armbruster         if (snapshot) {
521666daa68SMarkus Armbruster             /* always use cache=unsafe with snapshot */
522666daa68SMarkus Armbruster             bdrv_flags &= ~BDRV_O_CACHE_MASK;
523666daa68SMarkus Armbruster             bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
524666daa68SMarkus Armbruster         }
525666daa68SMarkus Armbruster 
526fb0490f6SStefan Hajnoczi         if (copy_on_read) {
527fb0490f6SStefan Hajnoczi             bdrv_flags |= BDRV_O_COPY_ON_READ;
528fb0490f6SStefan Hajnoczi         }
529fb0490f6SStefan Hajnoczi 
530ed9d4205SBenoît Canet         if (runstate_check(RUN_STATE_INMIGRATE)) {
531ed9d4205SBenoît Canet             bdrv_flags |= BDRV_O_INCOMING;
532ed9d4205SBenoît Canet         }
533ed9d4205SBenoît Canet 
534666daa68SMarkus Armbruster         bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
535666daa68SMarkus Armbruster 
536e4342ce5SMax Reitz         blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
537e4342ce5SMax Reitz                            errp);
538e4342ce5SMax Reitz         if (!blk) {
539e4342ce5SMax Reitz             goto err_no_bs_opts;
540e4342ce5SMax Reitz         }
541e4342ce5SMax Reitz         bs = blk_bs(blk);
542e4342ce5SMax Reitz     }
5430006383eSKevin Wolf 
544e4342ce5SMax Reitz     bs->detect_zeroes = detect_zeroes;
545e4342ce5SMax Reitz 
546e4342ce5SMax Reitz     bdrv_set_on_error(bs, on_read_error, on_write_error);
547e4342ce5SMax Reitz 
548e4342ce5SMax Reitz     /* disk I/O throttling */
549e4342ce5SMax Reitz     if (throttle_enabled(&cfg)) {
550e4342ce5SMax Reitz         bdrv_io_limits_enable(bs);
551e4342ce5SMax Reitz         bdrv_set_io_limits(bs, &cfg);
552666daa68SMarkus Armbruster     }
553666daa68SMarkus Armbruster 
554a0f1eab1SMarkus Armbruster     if (bdrv_key_required(bs)) {
555666daa68SMarkus Armbruster         autostart = 0;
556a0f1eab1SMarkus Armbruster     }
5570006383eSKevin Wolf 
558e4342ce5SMax Reitz err_no_bs_opts:
5590006383eSKevin Wolf     qemu_opts_del(opts);
56018e46a03SMarkus Armbruster     return blk;
561a9ae2bffSMarkus Armbruster 
562ec9c10d2SStefan Hajnoczi early_err:
563ec9c10d2SStefan Hajnoczi     qemu_opts_del(opts);
5646376f952SMarkus Armbruster err_no_opts:
5656376f952SMarkus Armbruster     QDECREF(bs_opts);
566a9ae2bffSMarkus Armbruster     return NULL;
567666daa68SMarkus Armbruster }
568666daa68SMarkus Armbruster 
5695abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
5705abbf0eeSKevin Wolf                             Error **errp)
57157975222SKevin Wolf {
57257975222SKevin Wolf     const char *value;
57357975222SKevin Wolf 
57457975222SKevin Wolf     value = qemu_opt_get(opts, from);
57557975222SKevin Wolf     if (value) {
5765abbf0eeSKevin Wolf         if (qemu_opt_find(opts, to)) {
5775abbf0eeSKevin Wolf             error_setg(errp, "'%s' and its alias '%s' can't be used at the "
5785abbf0eeSKevin Wolf                        "same time", to, from);
5795abbf0eeSKevin Wolf             return;
5805abbf0eeSKevin Wolf         }
58120d6cd47SJun Li     }
58220d6cd47SJun Li 
58320d6cd47SJun Li     /* rename all items in opts */
58420d6cd47SJun Li     while ((value = qemu_opt_get(opts, from))) {
585f43e47dbSMarkus Armbruster         qemu_opt_set(opts, to, value, &error_abort);
58657975222SKevin Wolf         qemu_opt_unset(opts, from);
58757975222SKevin Wolf     }
58857975222SKevin Wolf }
58957975222SKevin Wolf 
59033cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = {
59133cb7dc8SKevin Wolf     .name = "drive",
59233cb7dc8SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
59333cb7dc8SKevin Wolf     .desc = {
59433cb7dc8SKevin Wolf         {
59587a899c5SKevin Wolf             .name = "bus",
59687a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
59787a899c5SKevin Wolf             .help = "bus number",
59887a899c5SKevin Wolf         },{
59987a899c5SKevin Wolf             .name = "unit",
60087a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
60187a899c5SKevin Wolf             .help = "unit number (i.e. lun for scsi)",
60287a899c5SKevin Wolf         },{
60387a899c5SKevin Wolf             .name = "index",
60487a899c5SKevin Wolf             .type = QEMU_OPT_NUMBER,
60587a899c5SKevin Wolf             .help = "index number",
60687a899c5SKevin Wolf         },{
60733cb7dc8SKevin Wolf             .name = "media",
60833cb7dc8SKevin Wolf             .type = QEMU_OPT_STRING,
60933cb7dc8SKevin Wolf             .help = "media type (disk, cdrom)",
610593d464bSKevin Wolf         },{
611593d464bSKevin Wolf             .name = "if",
612593d464bSKevin Wolf             .type = QEMU_OPT_STRING,
613593d464bSKevin Wolf             .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
614b41a7338SKevin Wolf         },{
615b41a7338SKevin Wolf             .name = "cyls",
616b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
617b41a7338SKevin Wolf             .help = "number of cylinders (ide disk geometry)",
618b41a7338SKevin Wolf         },{
619b41a7338SKevin Wolf             .name = "heads",
620b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
621b41a7338SKevin Wolf             .help = "number of heads (ide disk geometry)",
622b41a7338SKevin Wolf         },{
623b41a7338SKevin Wolf             .name = "secs",
624b41a7338SKevin Wolf             .type = QEMU_OPT_NUMBER,
625b41a7338SKevin Wolf             .help = "number of sectors (ide disk geometry)",
626b41a7338SKevin Wolf         },{
627b41a7338SKevin Wolf             .name = "trans",
628b41a7338SKevin Wolf             .type = QEMU_OPT_STRING,
629b41a7338SKevin Wolf             .help = "chs translation (auto, lba, none)",
63026929298SKevin Wolf         },{
63126929298SKevin Wolf             .name = "boot",
63226929298SKevin Wolf             .type = QEMU_OPT_BOOL,
63326929298SKevin Wolf             .help = "(deprecated, ignored)",
634394c7d4dSKevin Wolf         },{
635394c7d4dSKevin Wolf             .name = "addr",
636394c7d4dSKevin Wolf             .type = QEMU_OPT_STRING,
637394c7d4dSKevin Wolf             .help = "pci address (virtio only)",
638d095b465SMax Reitz         },{
639bcf83158SKevin Wolf             .name = "serial",
640bcf83158SKevin Wolf             .type = QEMU_OPT_STRING,
641bcf83158SKevin Wolf             .help = "disk serial number",
642bcf83158SKevin Wolf         },{
643d095b465SMax Reitz             .name = "file",
644d095b465SMax Reitz             .type = QEMU_OPT_STRING,
645d095b465SMax Reitz             .help = "file name",
64633cb7dc8SKevin Wolf         },
6470ebd24e0SKevin Wolf 
6480ebd24e0SKevin Wolf         /* Options that are passed on, but have special semantics with -drive */
6490ebd24e0SKevin Wolf         {
6500ebd24e0SKevin Wolf             .name = "read-only",
6510ebd24e0SKevin Wolf             .type = QEMU_OPT_BOOL,
6520ebd24e0SKevin Wolf             .help = "open drive file as read-only",
6530ebd24e0SKevin Wolf         },{
654ee13ed1cSKevin Wolf             .name = "rerror",
655ee13ed1cSKevin Wolf             .type = QEMU_OPT_STRING,
656ee13ed1cSKevin Wolf             .help = "read error action",
657ee13ed1cSKevin Wolf         },{
658ee13ed1cSKevin Wolf             .name = "werror",
659ee13ed1cSKevin Wolf             .type = QEMU_OPT_STRING,
660ee13ed1cSKevin Wolf             .help = "write error action",
661ee13ed1cSKevin Wolf         },{
6620ebd24e0SKevin Wolf             .name = "copy-on-read",
6630ebd24e0SKevin Wolf             .type = QEMU_OPT_BOOL,
6640ebd24e0SKevin Wolf             .help = "copy read data from backing file into image file",
6650ebd24e0SKevin Wolf         },
6660ebd24e0SKevin Wolf 
66733cb7dc8SKevin Wolf         { /* end of list */ }
66833cb7dc8SKevin Wolf     },
66933cb7dc8SKevin Wolf };
67033cb7dc8SKevin Wolf 
67160e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
67257975222SKevin Wolf {
67329c4e2b5SKevin Wolf     const char *value;
67418e46a03SMarkus Armbruster     BlockBackend *blk;
67533cb7dc8SKevin Wolf     DriveInfo *dinfo = NULL;
676f298d071SKevin Wolf     QDict *bs_opts;
67733cb7dc8SKevin Wolf     QemuOpts *legacy_opts;
67833cb7dc8SKevin Wolf     DriveMediaType media = MEDIA_DISK;
679593d464bSKevin Wolf     BlockInterfaceType type;
680b41a7338SKevin Wolf     int cyls, heads, secs, translation;
68187a899c5SKevin Wolf     int max_devs, bus_id, unit_id, index;
682394c7d4dSKevin Wolf     const char *devaddr;
683ee13ed1cSKevin Wolf     const char *werror, *rerror;
684a7fdbcf0SFam Zheng     bool read_only = false;
685a7fdbcf0SFam Zheng     bool copy_on_read;
686bcf83158SKevin Wolf     const char *serial;
687d095b465SMax Reitz     const char *filename;
68833cb7dc8SKevin Wolf     Error *local_err = NULL;
689247147fbSKevin Wolf     int i;
69029c4e2b5SKevin Wolf 
69157975222SKevin Wolf     /* Change legacy command line options into QMP ones */
692247147fbSKevin Wolf     static const struct {
693247147fbSKevin Wolf         const char *from;
694247147fbSKevin Wolf         const char *to;
695247147fbSKevin Wolf     } opt_renames[] = {
696247147fbSKevin Wolf         { "iops",           "throttling.iops-total" },
697247147fbSKevin Wolf         { "iops_rd",        "throttling.iops-read" },
698247147fbSKevin Wolf         { "iops_wr",        "throttling.iops-write" },
69957975222SKevin Wolf 
700247147fbSKevin Wolf         { "bps",            "throttling.bps-total" },
701247147fbSKevin Wolf         { "bps_rd",         "throttling.bps-read" },
702247147fbSKevin Wolf         { "bps_wr",         "throttling.bps-write" },
70357975222SKevin Wolf 
704247147fbSKevin Wolf         { "iops_max",       "throttling.iops-total-max" },
705247147fbSKevin Wolf         { "iops_rd_max",    "throttling.iops-read-max" },
706247147fbSKevin Wolf         { "iops_wr_max",    "throttling.iops-write-max" },
7073e9fab69SBenoît Canet 
708247147fbSKevin Wolf         { "bps_max",        "throttling.bps-total-max" },
709247147fbSKevin Wolf         { "bps_rd_max",     "throttling.bps-read-max" },
710247147fbSKevin Wolf         { "bps_wr_max",     "throttling.bps-write-max" },
7113e9fab69SBenoît Canet 
712247147fbSKevin Wolf         { "iops_size",      "throttling.iops-size" },
7132024c1dfSBenoît Canet 
714247147fbSKevin Wolf         { "readonly",       "read-only" },
715247147fbSKevin Wolf     };
716247147fbSKevin Wolf 
717247147fbSKevin Wolf     for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
7185abbf0eeSKevin Wolf         qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
7195abbf0eeSKevin Wolf                         &local_err);
7205abbf0eeSKevin Wolf         if (local_err) {
721565f65d2SMarkus Armbruster             error_report_err(local_err);
7225abbf0eeSKevin Wolf             return NULL;
7235abbf0eeSKevin Wolf         }
724247147fbSKevin Wolf     }
7250f227a94SKevin Wolf 
72629c4e2b5SKevin Wolf     value = qemu_opt_get(all_opts, "cache");
72729c4e2b5SKevin Wolf     if (value) {
72829c4e2b5SKevin Wolf         int flags = 0;
72929c4e2b5SKevin Wolf 
73029c4e2b5SKevin Wolf         if (bdrv_parse_cache_flags(value, &flags) != 0) {
73129c4e2b5SKevin Wolf             error_report("invalid cache option");
73229c4e2b5SKevin Wolf             return NULL;
73329c4e2b5SKevin Wolf         }
73429c4e2b5SKevin Wolf 
73529c4e2b5SKevin Wolf         /* Specific options take precedence */
73629c4e2b5SKevin Wolf         if (!qemu_opt_get(all_opts, "cache.writeback")) {
73729c4e2b5SKevin Wolf             qemu_opt_set_bool(all_opts, "cache.writeback",
738cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_CACHE_WB), &error_abort);
73929c4e2b5SKevin Wolf         }
74029c4e2b5SKevin Wolf         if (!qemu_opt_get(all_opts, "cache.direct")) {
74129c4e2b5SKevin Wolf             qemu_opt_set_bool(all_opts, "cache.direct",
742cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_NOCACHE), &error_abort);
74329c4e2b5SKevin Wolf         }
74429c4e2b5SKevin Wolf         if (!qemu_opt_get(all_opts, "cache.no-flush")) {
74529c4e2b5SKevin Wolf             qemu_opt_set_bool(all_opts, "cache.no-flush",
746cccb7967SMarkus Armbruster                               !!(flags & BDRV_O_NO_FLUSH), &error_abort);
74729c4e2b5SKevin Wolf         }
74829c4e2b5SKevin Wolf         qemu_opt_unset(all_opts, "cache");
74929c4e2b5SKevin Wolf     }
75029c4e2b5SKevin Wolf 
751f298d071SKevin Wolf     /* Get a QDict for processing the options */
752f298d071SKevin Wolf     bs_opts = qdict_new();
753f298d071SKevin Wolf     qemu_opts_to_qdict(all_opts, bs_opts);
754f298d071SKevin Wolf 
75587ea75d5SPeter Crosthwaite     legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
75687ea75d5SPeter Crosthwaite                                    &error_abort);
75733cb7dc8SKevin Wolf     qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
75884d18f06SMarkus Armbruster     if (local_err) {
759565f65d2SMarkus Armbruster         error_report_err(local_err);
76033cb7dc8SKevin Wolf         goto fail;
76133cb7dc8SKevin Wolf     }
76233cb7dc8SKevin Wolf 
76326929298SKevin Wolf     /* Deprecated option boot=[on|off] */
76426929298SKevin Wolf     if (qemu_opt_get(legacy_opts, "boot") != NULL) {
76526929298SKevin Wolf         fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
76626929298SKevin Wolf                 "ignored. Future versions will reject this parameter. Please "
76726929298SKevin Wolf                 "update your scripts.\n");
76826929298SKevin Wolf     }
76926929298SKevin Wolf 
77033cb7dc8SKevin Wolf     /* Media type */
77133cb7dc8SKevin Wolf     value = qemu_opt_get(legacy_opts, "media");
77233cb7dc8SKevin Wolf     if (value) {
77333cb7dc8SKevin Wolf         if (!strcmp(value, "disk")) {
77433cb7dc8SKevin Wolf             media = MEDIA_DISK;
77533cb7dc8SKevin Wolf         } else if (!strcmp(value, "cdrom")) {
77633cb7dc8SKevin Wolf             media = MEDIA_CDROM;
777a7fdbcf0SFam Zheng             read_only = true;
77833cb7dc8SKevin Wolf         } else {
77933cb7dc8SKevin Wolf             error_report("'%s' invalid media", value);
78033cb7dc8SKevin Wolf             goto fail;
78133cb7dc8SKevin Wolf         }
78233cb7dc8SKevin Wolf     }
78333cb7dc8SKevin Wolf 
7840ebd24e0SKevin Wolf     /* copy-on-read is disabled with a warning for read-only devices */
785a7fdbcf0SFam Zheng     read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
7860ebd24e0SKevin Wolf     copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
7870ebd24e0SKevin Wolf 
7880ebd24e0SKevin Wolf     if (read_only && copy_on_read) {
7890ebd24e0SKevin Wolf         error_report("warning: disabling copy-on-read on read-only drive");
7900ebd24e0SKevin Wolf         copy_on_read = false;
7910ebd24e0SKevin Wolf     }
7920ebd24e0SKevin Wolf 
7930ebd24e0SKevin Wolf     qdict_put(bs_opts, "read-only",
7940ebd24e0SKevin Wolf               qstring_from_str(read_only ? "on" : "off"));
7950ebd24e0SKevin Wolf     qdict_put(bs_opts, "copy-on-read",
7960ebd24e0SKevin Wolf               qstring_from_str(copy_on_read ? "on" :"off"));
7970ebd24e0SKevin Wolf 
798593d464bSKevin Wolf     /* Controller type */
799593d464bSKevin Wolf     value = qemu_opt_get(legacy_opts, "if");
800593d464bSKevin Wolf     if (value) {
801593d464bSKevin Wolf         for (type = 0;
802593d464bSKevin Wolf              type < IF_COUNT && strcmp(value, if_name[type]);
803593d464bSKevin Wolf              type++) {
804593d464bSKevin Wolf         }
805593d464bSKevin Wolf         if (type == IF_COUNT) {
806593d464bSKevin Wolf             error_report("unsupported bus type '%s'", value);
807593d464bSKevin Wolf             goto fail;
808593d464bSKevin Wolf         }
809593d464bSKevin Wolf     } else {
810593d464bSKevin Wolf         type = block_default_type;
811593d464bSKevin Wolf     }
812593d464bSKevin Wolf 
813b41a7338SKevin Wolf     /* Geometry */
814b41a7338SKevin Wolf     cyls  = qemu_opt_get_number(legacy_opts, "cyls", 0);
815b41a7338SKevin Wolf     heads = qemu_opt_get_number(legacy_opts, "heads", 0);
816b41a7338SKevin Wolf     secs  = qemu_opt_get_number(legacy_opts, "secs", 0);
817b41a7338SKevin Wolf 
818b41a7338SKevin Wolf     if (cyls || heads || secs) {
819b41a7338SKevin Wolf         if (cyls < 1) {
820b41a7338SKevin Wolf             error_report("invalid physical cyls number");
821b41a7338SKevin Wolf             goto fail;
822b41a7338SKevin Wolf         }
823b41a7338SKevin Wolf         if (heads < 1) {
824b41a7338SKevin Wolf             error_report("invalid physical heads number");
825b41a7338SKevin Wolf             goto fail;
826b41a7338SKevin Wolf         }
827b41a7338SKevin Wolf         if (secs < 1) {
828b41a7338SKevin Wolf             error_report("invalid physical secs number");
829b41a7338SKevin Wolf             goto fail;
830b41a7338SKevin Wolf         }
831b41a7338SKevin Wolf     }
832b41a7338SKevin Wolf 
833b41a7338SKevin Wolf     translation = BIOS_ATA_TRANSLATION_AUTO;
834b41a7338SKevin Wolf     value = qemu_opt_get(legacy_opts, "trans");
835b41a7338SKevin Wolf     if (value != NULL) {
836b41a7338SKevin Wolf         if (!cyls) {
837b41a7338SKevin Wolf             error_report("'%s' trans must be used with cyls, heads and secs",
838b41a7338SKevin Wolf                          value);
839b41a7338SKevin Wolf             goto fail;
840b41a7338SKevin Wolf         }
841b41a7338SKevin Wolf         if (!strcmp(value, "none")) {
842b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_NONE;
843b41a7338SKevin Wolf         } else if (!strcmp(value, "lba")) {
844b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_LBA;
845f31c41ffSPaolo Bonzini         } else if (!strcmp(value, "large")) {
846f31c41ffSPaolo Bonzini             translation = BIOS_ATA_TRANSLATION_LARGE;
847f31c41ffSPaolo Bonzini         } else if (!strcmp(value, "rechs")) {
848f31c41ffSPaolo Bonzini             translation = BIOS_ATA_TRANSLATION_RECHS;
849b41a7338SKevin Wolf         } else if (!strcmp(value, "auto")) {
850b41a7338SKevin Wolf             translation = BIOS_ATA_TRANSLATION_AUTO;
851b41a7338SKevin Wolf         } else {
852b41a7338SKevin Wolf             error_report("'%s' invalid translation type", value);
853b41a7338SKevin Wolf             goto fail;
854b41a7338SKevin Wolf         }
855b41a7338SKevin Wolf     }
856b41a7338SKevin Wolf 
857b41a7338SKevin Wolf     if (media == MEDIA_CDROM) {
858b41a7338SKevin Wolf         if (cyls || secs || heads) {
859b41a7338SKevin Wolf             error_report("CHS can't be set with media=cdrom");
860b41a7338SKevin Wolf             goto fail;
861b41a7338SKevin Wolf         }
862b41a7338SKevin Wolf     }
863b41a7338SKevin Wolf 
86487a899c5SKevin Wolf     /* Device address specified by bus/unit or index.
86587a899c5SKevin Wolf      * If none was specified, try to find the first free one. */
86687a899c5SKevin Wolf     bus_id  = qemu_opt_get_number(legacy_opts, "bus", 0);
86787a899c5SKevin Wolf     unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
86887a899c5SKevin Wolf     index   = qemu_opt_get_number(legacy_opts, "index", -1);
86987a899c5SKevin Wolf 
87087a899c5SKevin Wolf     max_devs = if_max_devs[type];
87187a899c5SKevin Wolf 
87287a899c5SKevin Wolf     if (index != -1) {
87387a899c5SKevin Wolf         if (bus_id != 0 || unit_id != -1) {
87487a899c5SKevin Wolf             error_report("index cannot be used with bus and unit");
87587a899c5SKevin Wolf             goto fail;
87687a899c5SKevin Wolf         }
87787a899c5SKevin Wolf         bus_id = drive_index_to_bus_id(type, index);
87887a899c5SKevin Wolf         unit_id = drive_index_to_unit_id(type, index);
87987a899c5SKevin Wolf     }
88087a899c5SKevin Wolf 
88187a899c5SKevin Wolf     if (unit_id == -1) {
88287a899c5SKevin Wolf        unit_id = 0;
88387a899c5SKevin Wolf        while (drive_get(type, bus_id, unit_id) != NULL) {
88487a899c5SKevin Wolf            unit_id++;
88587a899c5SKevin Wolf            if (max_devs && unit_id >= max_devs) {
88687a899c5SKevin Wolf                unit_id -= max_devs;
88787a899c5SKevin Wolf                bus_id++;
88887a899c5SKevin Wolf            }
88987a899c5SKevin Wolf        }
89087a899c5SKevin Wolf     }
89187a899c5SKevin Wolf 
89287a899c5SKevin Wolf     if (max_devs && unit_id >= max_devs) {
89387a899c5SKevin Wolf         error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
89487a899c5SKevin Wolf         goto fail;
89587a899c5SKevin Wolf     }
89687a899c5SKevin Wolf 
89787a899c5SKevin Wolf     if (drive_get(type, bus_id, unit_id) != NULL) {
89887a899c5SKevin Wolf         error_report("drive with bus=%d, unit=%d (index=%d) exists",
89987a899c5SKevin Wolf                      bus_id, unit_id, index);
90087a899c5SKevin Wolf         goto fail;
90187a899c5SKevin Wolf     }
90287a899c5SKevin Wolf 
903bcf83158SKevin Wolf     /* Serial number */
904bcf83158SKevin Wolf     serial = qemu_opt_get(legacy_opts, "serial");
905bcf83158SKevin Wolf 
90687a899c5SKevin Wolf     /* no id supplied -> create one */
90787a899c5SKevin Wolf     if (qemu_opts_id(all_opts) == NULL) {
90887a899c5SKevin Wolf         char *new_id;
90987a899c5SKevin Wolf         const char *mediastr = "";
91087a899c5SKevin Wolf         if (type == IF_IDE || type == IF_SCSI) {
91187a899c5SKevin Wolf             mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
91287a899c5SKevin Wolf         }
91387a899c5SKevin Wolf         if (max_devs) {
91487a899c5SKevin Wolf             new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
91587a899c5SKevin Wolf                                      mediastr, unit_id);
91687a899c5SKevin Wolf         } else {
91787a899c5SKevin Wolf             new_id = g_strdup_printf("%s%s%i", if_name[type],
91887a899c5SKevin Wolf                                      mediastr, unit_id);
91987a899c5SKevin Wolf         }
92087a899c5SKevin Wolf         qdict_put(bs_opts, "id", qstring_from_str(new_id));
92187a899c5SKevin Wolf         g_free(new_id);
92287a899c5SKevin Wolf     }
92387a899c5SKevin Wolf 
924394c7d4dSKevin Wolf     /* Add virtio block device */
925394c7d4dSKevin Wolf     devaddr = qemu_opt_get(legacy_opts, "addr");
926394c7d4dSKevin Wolf     if (devaddr && type != IF_VIRTIO) {
927394c7d4dSKevin Wolf         error_report("addr is not supported by this bus type");
928394c7d4dSKevin Wolf         goto fail;
929394c7d4dSKevin Wolf     }
930394c7d4dSKevin Wolf 
931394c7d4dSKevin Wolf     if (type == IF_VIRTIO) {
932394c7d4dSKevin Wolf         QemuOpts *devopts;
93387ea75d5SPeter Crosthwaite         devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
93487ea75d5SPeter Crosthwaite                                    &error_abort);
935394c7d4dSKevin Wolf         if (arch_type == QEMU_ARCH_S390X) {
936f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "driver", "virtio-blk-s390", &error_abort);
937394c7d4dSKevin Wolf         } else {
938f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
939394c7d4dSKevin Wolf         }
940f43e47dbSMarkus Armbruster         qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
941f43e47dbSMarkus Armbruster                      &error_abort);
942394c7d4dSKevin Wolf         if (devaddr) {
943f43e47dbSMarkus Armbruster             qemu_opt_set(devopts, "addr", devaddr, &error_abort);
944394c7d4dSKevin Wolf         }
945394c7d4dSKevin Wolf     }
946394c7d4dSKevin Wolf 
947d095b465SMax Reitz     filename = qemu_opt_get(legacy_opts, "file");
948d095b465SMax Reitz 
949ee13ed1cSKevin Wolf     /* Check werror/rerror compatibility with if=... */
950ee13ed1cSKevin Wolf     werror = qemu_opt_get(legacy_opts, "werror");
951ee13ed1cSKevin Wolf     if (werror != NULL) {
952ee13ed1cSKevin Wolf         if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
953ee13ed1cSKevin Wolf             type != IF_NONE) {
954ee13ed1cSKevin Wolf             error_report("werror is not supported by this bus type");
955ee13ed1cSKevin Wolf             goto fail;
956ee13ed1cSKevin Wolf         }
957ee13ed1cSKevin Wolf         qdict_put(bs_opts, "werror", qstring_from_str(werror));
958ee13ed1cSKevin Wolf     }
959ee13ed1cSKevin Wolf 
960ee13ed1cSKevin Wolf     rerror = qemu_opt_get(legacy_opts, "rerror");
961ee13ed1cSKevin Wolf     if (rerror != NULL) {
962ee13ed1cSKevin Wolf         if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
963ee13ed1cSKevin Wolf             type != IF_NONE) {
964ee13ed1cSKevin Wolf             error_report("rerror is not supported by this bus type");
965ee13ed1cSKevin Wolf             goto fail;
966ee13ed1cSKevin Wolf         }
967ee13ed1cSKevin Wolf         qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
968ee13ed1cSKevin Wolf     }
969ee13ed1cSKevin Wolf 
9702d246f01SKevin Wolf     /* Actual block device init: Functionality shared with blockdev-add */
97118e46a03SMarkus Armbruster     blk = blockdev_init(filename, bs_opts, &local_err);
9723cb0e25cSMarkus Armbruster     bs_opts = NULL;
97318e46a03SMarkus Armbruster     if (!blk) {
97484d18f06SMarkus Armbruster         if (local_err) {
975565f65d2SMarkus Armbruster             error_report_err(local_err);
976b681072dSKevin Wolf         }
9772d246f01SKevin Wolf         goto fail;
978b681072dSKevin Wolf     } else {
97984d18f06SMarkus Armbruster         assert(!local_err);
9802d246f01SKevin Wolf     }
9812d246f01SKevin Wolf 
98226f8b3a8SMarkus Armbruster     /* Create legacy DriveInfo */
98326f8b3a8SMarkus Armbruster     dinfo = g_malloc0(sizeof(*dinfo));
984f298d071SKevin Wolf     dinfo->opts = all_opts;
9852d246f01SKevin Wolf 
986b41a7338SKevin Wolf     dinfo->cyls = cyls;
987b41a7338SKevin Wolf     dinfo->heads = heads;
988b41a7338SKevin Wolf     dinfo->secs = secs;
989b41a7338SKevin Wolf     dinfo->trans = translation;
990b41a7338SKevin Wolf 
991ee13ed1cSKevin Wolf     dinfo->type = type;
99287a899c5SKevin Wolf     dinfo->bus = bus_id;
99387a899c5SKevin Wolf     dinfo->unit = unit_id;
994394c7d4dSKevin Wolf     dinfo->devaddr = devaddr;
995bcf83158SKevin Wolf     dinfo->serial = g_strdup(serial);
996bcf83158SKevin Wolf 
99726f8b3a8SMarkus Armbruster     blk_set_legacy_dinfo(blk, dinfo);
99826f8b3a8SMarkus Armbruster 
999e34ef046SKevin Wolf     switch(type) {
1000e34ef046SKevin Wolf     case IF_IDE:
1001e34ef046SKevin Wolf     case IF_SCSI:
1002e34ef046SKevin Wolf     case IF_XEN:
1003e34ef046SKevin Wolf     case IF_NONE:
1004e34ef046SKevin Wolf         dinfo->media_cd = media == MEDIA_CDROM;
1005e34ef046SKevin Wolf         break;
1006e34ef046SKevin Wolf     default:
1007e34ef046SKevin Wolf         break;
1008e34ef046SKevin Wolf     }
1009e34ef046SKevin Wolf 
10102d246f01SKevin Wolf fail:
101133cb7dc8SKevin Wolf     qemu_opts_del(legacy_opts);
10123cb0e25cSMarkus Armbruster     QDECREF(bs_opts);
10132d246f01SKevin Wolf     return dinfo;
101457975222SKevin Wolf }
101557975222SKevin Wolf 
10163e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict)
1017666daa68SMarkus Armbruster {
1018666daa68SMarkus Armbruster     const char *device = qdict_get_str(qdict, "device");
1019a0e8544cSFam Zheng     BlockBackend *blk;
10202d3735d3SStefan Hajnoczi     int ret;
10212d3735d3SStefan Hajnoczi 
1022e8877497SStefan Hajnoczi     if (!strcmp(device, "all")) {
1023e8877497SStefan Hajnoczi         ret = bdrv_commit_all();
1024e8877497SStefan Hajnoczi     } else {
1025a0e8544cSFam Zheng         blk = blk_by_name(device);
1026a0e8544cSFam Zheng         if (!blk) {
102758513bdeSJeff Cody             monitor_printf(mon, "Device '%s' not found\n", device);
1028ac59eb95SMarkus Armbruster             return;
10296ab4b5abSMarkus Armbruster         }
1030a0e8544cSFam Zheng         ret = bdrv_commit(blk_bs(blk));
10312d3735d3SStefan Hajnoczi     }
103258513bdeSJeff Cody     if (ret < 0) {
103358513bdeSJeff Cody         monitor_printf(mon, "'commit' error for '%s': %s\n", device,
103458513bdeSJeff Cody                        strerror(-ret));
1035666daa68SMarkus Armbruster     }
1036666daa68SMarkus Armbruster }
1037666daa68SMarkus Armbruster 
10386cc2a415SPaolo Bonzini static void blockdev_do_action(int kind, void *data, Error **errp)
10396cc2a415SPaolo Bonzini {
1040c8a83e85SKevin Wolf     TransactionAction action;
1041c8a83e85SKevin Wolf     TransactionActionList list;
10426cc2a415SPaolo Bonzini 
10436cc2a415SPaolo Bonzini     action.kind = kind;
10446cc2a415SPaolo Bonzini     action.data = data;
10456cc2a415SPaolo Bonzini     list.value = &action;
10466cc2a415SPaolo Bonzini     list.next = NULL;
10476cc2a415SPaolo Bonzini     qmp_transaction(&list, errp);
10486cc2a415SPaolo Bonzini }
10496cc2a415SPaolo Bonzini 
10500901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
10510901f67eSBenoît Canet                                 bool has_node_name, const char *node_name,
10520901f67eSBenoît Canet                                 const char *snapshot_file,
10530901f67eSBenoît Canet                                 bool has_snapshot_node_name,
10540901f67eSBenoît Canet                                 const char *snapshot_node_name,
10556106e249SLuiz Capitulino                                 bool has_format, const char *format,
10560901f67eSBenoît Canet                                 bool has_mode, NewImageMode mode, Error **errp)
1057f8882568SJes Sorensen {
10586cc2a415SPaolo Bonzini     BlockdevSnapshot snapshot = {
10590901f67eSBenoît Canet         .has_device = has_device,
10606cc2a415SPaolo Bonzini         .device = (char *) device,
10610901f67eSBenoît Canet         .has_node_name = has_node_name,
10620901f67eSBenoît Canet         .node_name = (char *) node_name,
10636cc2a415SPaolo Bonzini         .snapshot_file = (char *) snapshot_file,
10640901f67eSBenoît Canet         .has_snapshot_node_name = has_snapshot_node_name,
10650901f67eSBenoît Canet         .snapshot_node_name = (char *) snapshot_node_name,
10666cc2a415SPaolo Bonzini         .has_format = has_format,
10676cc2a415SPaolo Bonzini         .format = (char *) format,
10686cc2a415SPaolo Bonzini         .has_mode = has_mode,
10696cc2a415SPaolo Bonzini         .mode = mode,
10706cc2a415SPaolo Bonzini     };
1071c8a83e85SKevin Wolf     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1072c8a83e85SKevin Wolf                        &snapshot, errp);
1073f8882568SJes Sorensen }
1074f8882568SJes Sorensen 
1075f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device,
1076f323bc9eSWenchao Xia                                          const char *name,
1077f323bc9eSWenchao Xia                                          Error **errp)
1078f323bc9eSWenchao Xia {
1079f323bc9eSWenchao Xia     BlockdevSnapshotInternal snapshot = {
1080f323bc9eSWenchao Xia         .device = (char *) device,
1081f323bc9eSWenchao Xia         .name = (char *) name
1082f323bc9eSWenchao Xia     };
1083f323bc9eSWenchao Xia 
1084f323bc9eSWenchao Xia     blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1085f323bc9eSWenchao Xia                        &snapshot, errp);
1086f323bc9eSWenchao Xia }
1087f323bc9eSWenchao Xia 
108844e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
108944e3e053SWenchao Xia                                                          bool has_id,
109044e3e053SWenchao Xia                                                          const char *id,
109144e3e053SWenchao Xia                                                          bool has_name,
109244e3e053SWenchao Xia                                                          const char *name,
109344e3e053SWenchao Xia                                                          Error **errp)
109444e3e053SWenchao Xia {
1095a0e8544cSFam Zheng     BlockDriverState *bs;
1096a0e8544cSFam Zheng     BlockBackend *blk;
10974ef3982aSStefan Hajnoczi     AioContext *aio_context;
109844e3e053SWenchao Xia     QEMUSnapshotInfo sn;
109944e3e053SWenchao Xia     Error *local_err = NULL;
110044e3e053SWenchao Xia     SnapshotInfo *info = NULL;
110144e3e053SWenchao Xia     int ret;
110244e3e053SWenchao Xia 
1103a0e8544cSFam Zheng     blk = blk_by_name(device);
1104a0e8544cSFam Zheng     if (!blk) {
110544e3e053SWenchao Xia         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
110644e3e053SWenchao Xia         return NULL;
110744e3e053SWenchao Xia     }
1108a0e8544cSFam Zheng     bs = blk_bs(blk);
110944e3e053SWenchao Xia 
111044e3e053SWenchao Xia     if (!has_id) {
111144e3e053SWenchao Xia         id = NULL;
111244e3e053SWenchao Xia     }
111344e3e053SWenchao Xia 
111444e3e053SWenchao Xia     if (!has_name) {
111544e3e053SWenchao Xia         name = NULL;
111644e3e053SWenchao Xia     }
111744e3e053SWenchao Xia 
111844e3e053SWenchao Xia     if (!id && !name) {
111944e3e053SWenchao Xia         error_setg(errp, "Name or id must be provided");
112044e3e053SWenchao Xia         return NULL;
112144e3e053SWenchao Xia     }
112244e3e053SWenchao Xia 
11234ef3982aSStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
11244ef3982aSStefan Hajnoczi     aio_context_acquire(aio_context);
11254ef3982aSStefan Hajnoczi 
11260b928854SStefan Hajnoczi     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
11270b928854SStefan Hajnoczi         goto out_aio_context;
11280b928854SStefan Hajnoczi     }
11290b928854SStefan Hajnoczi 
113044e3e053SWenchao Xia     ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
113184d18f06SMarkus Armbruster     if (local_err) {
113244e3e053SWenchao Xia         error_propagate(errp, local_err);
11334ef3982aSStefan Hajnoczi         goto out_aio_context;
113444e3e053SWenchao Xia     }
113544e3e053SWenchao Xia     if (!ret) {
113644e3e053SWenchao Xia         error_setg(errp,
113744e3e053SWenchao Xia                    "Snapshot with id '%s' and name '%s' does not exist on "
113844e3e053SWenchao Xia                    "device '%s'",
113944e3e053SWenchao Xia                    STR_OR_NULL(id), STR_OR_NULL(name), device);
11404ef3982aSStefan Hajnoczi         goto out_aio_context;
114144e3e053SWenchao Xia     }
114244e3e053SWenchao Xia 
114344e3e053SWenchao Xia     bdrv_snapshot_delete(bs, id, name, &local_err);
114484d18f06SMarkus Armbruster     if (local_err) {
114544e3e053SWenchao Xia         error_propagate(errp, local_err);
11464ef3982aSStefan Hajnoczi         goto out_aio_context;
114744e3e053SWenchao Xia     }
114844e3e053SWenchao Xia 
11494ef3982aSStefan Hajnoczi     aio_context_release(aio_context);
11504ef3982aSStefan Hajnoczi 
11515839e53bSMarkus Armbruster     info = g_new0(SnapshotInfo, 1);
115244e3e053SWenchao Xia     info->id = g_strdup(sn.id_str);
115344e3e053SWenchao Xia     info->name = g_strdup(sn.name);
115444e3e053SWenchao Xia     info->date_nsec = sn.date_nsec;
115544e3e053SWenchao Xia     info->date_sec = sn.date_sec;
115644e3e053SWenchao Xia     info->vm_state_size = sn.vm_state_size;
115744e3e053SWenchao Xia     info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
115844e3e053SWenchao Xia     info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
115944e3e053SWenchao Xia 
116044e3e053SWenchao Xia     return info;
11614ef3982aSStefan Hajnoczi 
11624ef3982aSStefan Hajnoczi out_aio_context:
11634ef3982aSStefan Hajnoczi     aio_context_release(aio_context);
11644ef3982aSStefan Hajnoczi     return NULL;
116544e3e053SWenchao Xia }
11668802d1fdSJeff Cody 
1167b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */
1168ba0c86a3SWenchao Xia 
1169ba5d6ab6SStefan Hajnoczi typedef struct BlkTransactionState BlkTransactionState;
1170ba0c86a3SWenchao Xia 
1171ba0c86a3SWenchao Xia /* Only prepare() may fail. In a single transaction, only one of commit() or
1172ba0c86a3SWenchao Xia    abort() will be called, clean() will always be called if it present. */
1173ba0c86a3SWenchao Xia typedef struct BdrvActionOps {
1174ba0c86a3SWenchao Xia     /* Size of state struct, in bytes. */
1175ba0c86a3SWenchao Xia     size_t instance_size;
1176ba0c86a3SWenchao Xia     /* Prepare the work, must NOT be NULL. */
1177ba5d6ab6SStefan Hajnoczi     void (*prepare)(BlkTransactionState *common, Error **errp);
1178f9ea81e8SStefan Hajnoczi     /* Commit the changes, can be NULL. */
1179ba5d6ab6SStefan Hajnoczi     void (*commit)(BlkTransactionState *common);
1180ba0c86a3SWenchao Xia     /* Abort the changes on fail, can be NULL. */
1181ba5d6ab6SStefan Hajnoczi     void (*abort)(BlkTransactionState *common);
1182ba0c86a3SWenchao Xia     /* Clean up resource in the end, can be NULL. */
1183ba5d6ab6SStefan Hajnoczi     void (*clean)(BlkTransactionState *common);
1184ba0c86a3SWenchao Xia } BdrvActionOps;
1185ba0c86a3SWenchao Xia 
1186ba0c86a3SWenchao Xia /*
1187ba0c86a3SWenchao Xia  * This structure must be arranged as first member in child type, assuming
1188ba0c86a3SWenchao Xia  * that compiler will also arrange it to the same address with parent instance.
1189ba0c86a3SWenchao Xia  * Later it will be used in free().
1190ba0c86a3SWenchao Xia  */
1191ba5d6ab6SStefan Hajnoczi struct BlkTransactionState {
1192c8a83e85SKevin Wolf     TransactionAction *action;
1193ba0c86a3SWenchao Xia     const BdrvActionOps *ops;
1194ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_ENTRY(BlkTransactionState) entry;
1195ba0c86a3SWenchao Xia };
1196ba0c86a3SWenchao Xia 
1197bbe86010SWenchao Xia /* internal snapshot private data */
1198bbe86010SWenchao Xia typedef struct InternalSnapshotState {
1199bbe86010SWenchao Xia     BlkTransactionState common;
1200bbe86010SWenchao Xia     BlockDriverState *bs;
12015d6e96efSStefan Hajnoczi     AioContext *aio_context;
1202bbe86010SWenchao Xia     QEMUSnapshotInfo sn;
1203bbe86010SWenchao Xia } InternalSnapshotState;
1204bbe86010SWenchao Xia 
1205bbe86010SWenchao Xia static void internal_snapshot_prepare(BlkTransactionState *common,
1206bbe86010SWenchao Xia                                       Error **errp)
1207bbe86010SWenchao Xia {
1208f70edf99SMarkus Armbruster     Error *local_err = NULL;
1209bbe86010SWenchao Xia     const char *device;
1210bbe86010SWenchao Xia     const char *name;
1211a0e8544cSFam Zheng     BlockBackend *blk;
1212bbe86010SWenchao Xia     BlockDriverState *bs;
1213bbe86010SWenchao Xia     QEMUSnapshotInfo old_sn, *sn;
1214bbe86010SWenchao Xia     bool ret;
1215bbe86010SWenchao Xia     qemu_timeval tv;
1216bbe86010SWenchao Xia     BlockdevSnapshotInternal *internal;
1217bbe86010SWenchao Xia     InternalSnapshotState *state;
1218bbe86010SWenchao Xia     int ret1;
1219bbe86010SWenchao Xia 
1220bbe86010SWenchao Xia     g_assert(common->action->kind ==
1221bbe86010SWenchao Xia              TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1222bbe86010SWenchao Xia     internal = common->action->blockdev_snapshot_internal_sync;
1223bbe86010SWenchao Xia     state = DO_UPCAST(InternalSnapshotState, common, common);
1224bbe86010SWenchao Xia 
1225bbe86010SWenchao Xia     /* 1. parse input */
1226bbe86010SWenchao Xia     device = internal->device;
1227bbe86010SWenchao Xia     name = internal->name;
1228bbe86010SWenchao Xia 
1229bbe86010SWenchao Xia     /* 2. check for validation */
1230a0e8544cSFam Zheng     blk = blk_by_name(device);
1231a0e8544cSFam Zheng     if (!blk) {
1232bbe86010SWenchao Xia         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1233bbe86010SWenchao Xia         return;
1234bbe86010SWenchao Xia     }
1235a0e8544cSFam Zheng     bs = blk_bs(blk);
1236bbe86010SWenchao Xia 
12375d6e96efSStefan Hajnoczi     /* AioContext is released in .clean() */
12385d6e96efSStefan Hajnoczi     state->aio_context = bdrv_get_aio_context(bs);
12395d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
12405d6e96efSStefan Hajnoczi 
1241bbe86010SWenchao Xia     if (!bdrv_is_inserted(bs)) {
1242bbe86010SWenchao Xia         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1243bbe86010SWenchao Xia         return;
1244bbe86010SWenchao Xia     }
1245bbe86010SWenchao Xia 
12463dc7ca3cSStefan Hajnoczi     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
12473dc7ca3cSStefan Hajnoczi         return;
12483dc7ca3cSStefan Hajnoczi     }
12493dc7ca3cSStefan Hajnoczi 
1250bbe86010SWenchao Xia     if (bdrv_is_read_only(bs)) {
125181e5f78aSAlberto Garcia         error_setg(errp, "Device '%s' is read only", device);
1252bbe86010SWenchao Xia         return;
1253bbe86010SWenchao Xia     }
1254bbe86010SWenchao Xia 
1255bbe86010SWenchao Xia     if (!bdrv_can_snapshot(bs)) {
125681e5f78aSAlberto Garcia         error_setg(errp, "Block format '%s' used by device '%s' "
125781e5f78aSAlberto Garcia                    "does not support internal snapshots",
125881e5f78aSAlberto Garcia                    bs->drv->format_name, device);
1259bbe86010SWenchao Xia         return;
1260bbe86010SWenchao Xia     }
1261bbe86010SWenchao Xia 
1262bbe86010SWenchao Xia     if (!strlen(name)) {
1263bbe86010SWenchao Xia         error_setg(errp, "Name is empty");
1264bbe86010SWenchao Xia         return;
1265bbe86010SWenchao Xia     }
1266bbe86010SWenchao Xia 
1267bbe86010SWenchao Xia     /* check whether a snapshot with name exist */
1268f70edf99SMarkus Armbruster     ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1269f70edf99SMarkus Armbruster                                             &local_err);
1270f70edf99SMarkus Armbruster     if (local_err) {
1271f70edf99SMarkus Armbruster         error_propagate(errp, local_err);
1272bbe86010SWenchao Xia         return;
1273bbe86010SWenchao Xia     } else if (ret) {
1274bbe86010SWenchao Xia         error_setg(errp,
1275bbe86010SWenchao Xia                    "Snapshot with name '%s' already exists on device '%s'",
1276bbe86010SWenchao Xia                    name, device);
1277bbe86010SWenchao Xia         return;
1278bbe86010SWenchao Xia     }
1279bbe86010SWenchao Xia 
1280bbe86010SWenchao Xia     /* 3. take the snapshot */
1281bbe86010SWenchao Xia     sn = &state->sn;
1282bbe86010SWenchao Xia     pstrcpy(sn->name, sizeof(sn->name), name);
1283bbe86010SWenchao Xia     qemu_gettimeofday(&tv);
1284bbe86010SWenchao Xia     sn->date_sec = tv.tv_sec;
1285bbe86010SWenchao Xia     sn->date_nsec = tv.tv_usec * 1000;
1286bbe86010SWenchao Xia     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1287bbe86010SWenchao Xia 
1288bbe86010SWenchao Xia     ret1 = bdrv_snapshot_create(bs, sn);
1289bbe86010SWenchao Xia     if (ret1 < 0) {
1290bbe86010SWenchao Xia         error_setg_errno(errp, -ret1,
1291bbe86010SWenchao Xia                          "Failed to create snapshot '%s' on device '%s'",
1292bbe86010SWenchao Xia                          name, device);
1293bbe86010SWenchao Xia         return;
1294bbe86010SWenchao Xia     }
1295bbe86010SWenchao Xia 
1296bbe86010SWenchao Xia     /* 4. succeed, mark a snapshot is created */
1297bbe86010SWenchao Xia     state->bs = bs;
1298bbe86010SWenchao Xia }
1299bbe86010SWenchao Xia 
1300bbe86010SWenchao Xia static void internal_snapshot_abort(BlkTransactionState *common)
1301bbe86010SWenchao Xia {
1302bbe86010SWenchao Xia     InternalSnapshotState *state =
1303bbe86010SWenchao Xia                              DO_UPCAST(InternalSnapshotState, common, common);
1304bbe86010SWenchao Xia     BlockDriverState *bs = state->bs;
1305bbe86010SWenchao Xia     QEMUSnapshotInfo *sn = &state->sn;
1306bbe86010SWenchao Xia     Error *local_error = NULL;
1307bbe86010SWenchao Xia 
1308bbe86010SWenchao Xia     if (!bs) {
1309bbe86010SWenchao Xia         return;
1310bbe86010SWenchao Xia     }
1311bbe86010SWenchao Xia 
1312bbe86010SWenchao Xia     if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1313bbe86010SWenchao Xia         error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1314bbe86010SWenchao Xia                      "device '%s' in abort: %s",
1315bbe86010SWenchao Xia                      sn->id_str,
1316bbe86010SWenchao Xia                      sn->name,
1317bbe86010SWenchao Xia                      bdrv_get_device_name(bs),
1318bbe86010SWenchao Xia                      error_get_pretty(local_error));
1319bbe86010SWenchao Xia         error_free(local_error);
1320bbe86010SWenchao Xia     }
1321bbe86010SWenchao Xia }
1322bbe86010SWenchao Xia 
13235d6e96efSStefan Hajnoczi static void internal_snapshot_clean(BlkTransactionState *common)
13245d6e96efSStefan Hajnoczi {
13255d6e96efSStefan Hajnoczi     InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
13265d6e96efSStefan Hajnoczi                                              common, common);
13275d6e96efSStefan Hajnoczi 
13285d6e96efSStefan Hajnoczi     if (state->aio_context) {
13295d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
13305d6e96efSStefan Hajnoczi     }
13315d6e96efSStefan Hajnoczi }
13325d6e96efSStefan Hajnoczi 
1333ba0c86a3SWenchao Xia /* external snapshot private data */
1334ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState {
1335ba5d6ab6SStefan Hajnoczi     BlkTransactionState common;
13368802d1fdSJeff Cody     BlockDriverState *old_bs;
13378802d1fdSJeff Cody     BlockDriverState *new_bs;
13385d6e96efSStefan Hajnoczi     AioContext *aio_context;
1339ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState;
13408802d1fdSJeff Cody 
1341ba5d6ab6SStefan Hajnoczi static void external_snapshot_prepare(BlkTransactionState *common,
13429b9877eeSWenchao Xia                                       Error **errp)
13439b9877eeSWenchao Xia {
13449b9877eeSWenchao Xia     BlockDriver *drv;
13459b9877eeSWenchao Xia     int flags, ret;
13460901f67eSBenoît Canet     QDict *options = NULL;
13479b9877eeSWenchao Xia     Error *local_err = NULL;
13480901f67eSBenoît Canet     bool has_device = false;
1349e2a31e87SWenchao Xia     const char *device;
13500901f67eSBenoît Canet     bool has_node_name = false;
13510901f67eSBenoît Canet     const char *node_name;
13520901f67eSBenoît Canet     bool has_snapshot_node_name = false;
13530901f67eSBenoît Canet     const char *snapshot_node_name;
1354e2a31e87SWenchao Xia     const char *new_image_file;
1355e2a31e87SWenchao Xia     const char *format = "qcow2";
1356e2a31e87SWenchao Xia     enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1357ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1358ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1359c8a83e85SKevin Wolf     TransactionAction *action = common->action;
13609b9877eeSWenchao Xia 
1361e2a31e87SWenchao Xia     /* get parameters */
1362c8a83e85SKevin Wolf     g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1363e2a31e87SWenchao Xia 
13640901f67eSBenoît Canet     has_device = action->blockdev_snapshot_sync->has_device;
1365e2a31e87SWenchao Xia     device = action->blockdev_snapshot_sync->device;
13660901f67eSBenoît Canet     has_node_name = action->blockdev_snapshot_sync->has_node_name;
13670901f67eSBenoît Canet     node_name = action->blockdev_snapshot_sync->node_name;
13680901f67eSBenoît Canet     has_snapshot_node_name =
13690901f67eSBenoît Canet         action->blockdev_snapshot_sync->has_snapshot_node_name;
13700901f67eSBenoît Canet     snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name;
13710901f67eSBenoît Canet 
1372e2a31e87SWenchao Xia     new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1373e2a31e87SWenchao Xia     if (action->blockdev_snapshot_sync->has_format) {
1374e2a31e87SWenchao Xia         format = action->blockdev_snapshot_sync->format;
1375e2a31e87SWenchao Xia     }
1376e2a31e87SWenchao Xia     if (action->blockdev_snapshot_sync->has_mode) {
1377e2a31e87SWenchao Xia         mode = action->blockdev_snapshot_sync->mode;
1378e2a31e87SWenchao Xia     }
1379e2a31e87SWenchao Xia 
1380e2a31e87SWenchao Xia     /* start processing */
13819b9877eeSWenchao Xia     drv = bdrv_find_format(format);
13829b9877eeSWenchao Xia     if (!drv) {
13839b9877eeSWenchao Xia         error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
13849b9877eeSWenchao Xia         return;
13859b9877eeSWenchao Xia     }
13869b9877eeSWenchao Xia 
13870901f67eSBenoît Canet     state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
13880901f67eSBenoît Canet                                    has_node_name ? node_name : NULL,
13890901f67eSBenoît Canet                                    &local_err);
139084d18f06SMarkus Armbruster     if (local_err) {
13910901f67eSBenoît Canet         error_propagate(errp, local_err);
13920901f67eSBenoît Canet         return;
13930901f67eSBenoît Canet     }
13940901f67eSBenoît Canet 
13950901f67eSBenoît Canet     if (has_node_name && !has_snapshot_node_name) {
13960901f67eSBenoît Canet         error_setg(errp, "New snapshot node name missing");
13970901f67eSBenoît Canet         return;
13980901f67eSBenoît Canet     }
13990901f67eSBenoît Canet 
14000901f67eSBenoît Canet     if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) {
14010901f67eSBenoît Canet         error_setg(errp, "New snapshot node name already existing");
14029b9877eeSWenchao Xia         return;
14039b9877eeSWenchao Xia     }
14049b9877eeSWenchao Xia 
14055d6e96efSStefan Hajnoczi     /* Acquire AioContext now so any threads operating on old_bs stop */
14065d6e96efSStefan Hajnoczi     state->aio_context = bdrv_get_aio_context(state->old_bs);
14075d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
14085d6e96efSStefan Hajnoczi 
1409ba5d6ab6SStefan Hajnoczi     if (!bdrv_is_inserted(state->old_bs)) {
14109b9877eeSWenchao Xia         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
14119b9877eeSWenchao Xia         return;
14129b9877eeSWenchao Xia     }
14139b9877eeSWenchao Xia 
14143718d8abSFam Zheng     if (bdrv_op_is_blocked(state->old_bs,
14153718d8abSFam Zheng                            BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
14169b9877eeSWenchao Xia         return;
14179b9877eeSWenchao Xia     }
14189b9877eeSWenchao Xia 
1419ba5d6ab6SStefan Hajnoczi     if (!bdrv_is_read_only(state->old_bs)) {
1420ba5d6ab6SStefan Hajnoczi         if (bdrv_flush(state->old_bs)) {
14219b9877eeSWenchao Xia             error_set(errp, QERR_IO_ERROR);
14229b9877eeSWenchao Xia             return;
14239b9877eeSWenchao Xia         }
14249b9877eeSWenchao Xia     }
14259b9877eeSWenchao Xia 
1426212a5a8fSBenoît Canet     if (!bdrv_is_first_non_filter(state->old_bs)) {
1427f6186f49SBenoît Canet         error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
1428f6186f49SBenoît Canet         return;
1429f6186f49SBenoît Canet     }
1430f6186f49SBenoît Canet 
1431ba5d6ab6SStefan Hajnoczi     flags = state->old_bs->open_flags;
14329b9877eeSWenchao Xia 
14339b9877eeSWenchao Xia     /* create new image w/backing file */
14349b9877eeSWenchao Xia     if (mode != NEW_IMAGE_MODE_EXISTING) {
14359b9877eeSWenchao Xia         bdrv_img_create(new_image_file, format,
1436ba5d6ab6SStefan Hajnoczi                         state->old_bs->filename,
1437ba5d6ab6SStefan Hajnoczi                         state->old_bs->drv->format_name,
14389b9877eeSWenchao Xia                         NULL, -1, flags, &local_err, false);
143984d18f06SMarkus Armbruster         if (local_err) {
14409b9877eeSWenchao Xia             error_propagate(errp, local_err);
14419b9877eeSWenchao Xia             return;
14429b9877eeSWenchao Xia         }
14439b9877eeSWenchao Xia     }
14449b9877eeSWenchao Xia 
14450901f67eSBenoît Canet     if (has_snapshot_node_name) {
14460901f67eSBenoît Canet         options = qdict_new();
14470901f67eSBenoît Canet         qdict_put(options, "node-name",
14480901f67eSBenoît Canet                   qstring_from_str(snapshot_node_name));
14490901f67eSBenoît Canet     }
14500901f67eSBenoît Canet 
14519b9877eeSWenchao Xia     /* TODO Inherit bs->options or only take explicit options with an
14529b9877eeSWenchao Xia      * extended QMP command? */
1453f67503e5SMax Reitz     assert(state->new_bs == NULL);
1454ddf5636dSMax Reitz     ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
145534b5d2c6SMax Reitz                     flags | BDRV_O_NO_BACKING, drv, &local_err);
1456f67503e5SMax Reitz     /* We will manually add the backing_hd field to the bs later */
14579b9877eeSWenchao Xia     if (ret != 0) {
145834b5d2c6SMax Reitz         error_propagate(errp, local_err);
14599b9877eeSWenchao Xia     }
14609b9877eeSWenchao Xia }
14619b9877eeSWenchao Xia 
1462ba5d6ab6SStefan Hajnoczi static void external_snapshot_commit(BlkTransactionState *common)
14633b0047e8SWenchao Xia {
1464ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1465ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1466ba0c86a3SWenchao Xia 
14675d6e96efSStefan Hajnoczi     bdrv_set_aio_context(state->new_bs, state->aio_context);
14685d6e96efSStefan Hajnoczi 
1469ba5d6ab6SStefan Hajnoczi     /* This removes our old bs and adds the new bs */
1470ba5d6ab6SStefan Hajnoczi     bdrv_append(state->new_bs, state->old_bs);
14713b0047e8SWenchao Xia     /* We don't need (or want) to use the transactional
14723b0047e8SWenchao Xia      * bdrv_reopen_multiple() across all the entries at once, because we
14733b0047e8SWenchao Xia      * don't want to abort all of them if one of them fails the reopen */
1474ba5d6ab6SStefan Hajnoczi     bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
14753b0047e8SWenchao Xia                 NULL);
14765d6e96efSStefan Hajnoczi 
14775d6e96efSStefan Hajnoczi     aio_context_release(state->aio_context);
14783b0047e8SWenchao Xia }
14793b0047e8SWenchao Xia 
1480ba5d6ab6SStefan Hajnoczi static void external_snapshot_abort(BlkTransactionState *common)
148196b86bf7SWenchao Xia {
1482ba5d6ab6SStefan Hajnoczi     ExternalSnapshotState *state =
1483ba5d6ab6SStefan Hajnoczi                              DO_UPCAST(ExternalSnapshotState, common, common);
1484ba5d6ab6SStefan Hajnoczi     if (state->new_bs) {
14854f6fd349SFam Zheng         bdrv_unref(state->new_bs);
148696b86bf7SWenchao Xia     }
14875d6e96efSStefan Hajnoczi     if (state->aio_context) {
14885d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
14895d6e96efSStefan Hajnoczi     }
149096b86bf7SWenchao Xia }
149196b86bf7SWenchao Xia 
14923037f364SStefan Hajnoczi typedef struct DriveBackupState {
14933037f364SStefan Hajnoczi     BlkTransactionState common;
14943037f364SStefan Hajnoczi     BlockDriverState *bs;
14955d6e96efSStefan Hajnoczi     AioContext *aio_context;
14963037f364SStefan Hajnoczi     BlockJob *job;
14973037f364SStefan Hajnoczi } DriveBackupState;
14983037f364SStefan Hajnoczi 
14993037f364SStefan Hajnoczi static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
15003037f364SStefan Hajnoczi {
15013037f364SStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
15025d6e96efSStefan Hajnoczi     BlockDriverState *bs;
1503a0e8544cSFam Zheng     BlockBackend *blk;
15043037f364SStefan Hajnoczi     DriveBackup *backup;
15053037f364SStefan Hajnoczi     Error *local_err = NULL;
15063037f364SStefan Hajnoczi 
15073037f364SStefan Hajnoczi     assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
15083037f364SStefan Hajnoczi     backup = common->action->drive_backup;
15093037f364SStefan Hajnoczi 
1510a0e8544cSFam Zheng     blk = blk_by_name(backup->device);
1511a0e8544cSFam Zheng     if (!blk) {
15125d6e96efSStefan Hajnoczi         error_set(errp, QERR_DEVICE_NOT_FOUND, backup->device);
15135d6e96efSStefan Hajnoczi         return;
15145d6e96efSStefan Hajnoczi     }
1515a0e8544cSFam Zheng     bs = blk_bs(blk);
15165d6e96efSStefan Hajnoczi 
15175d6e96efSStefan Hajnoczi     /* AioContext is released in .clean() */
15185d6e96efSStefan Hajnoczi     state->aio_context = bdrv_get_aio_context(bs);
15195d6e96efSStefan Hajnoczi     aio_context_acquire(state->aio_context);
15205d6e96efSStefan Hajnoczi 
15213037f364SStefan Hajnoczi     qmp_drive_backup(backup->device, backup->target,
15223037f364SStefan Hajnoczi                      backup->has_format, backup->format,
1523b53169eaSStefan Hajnoczi                      backup->sync,
15243037f364SStefan Hajnoczi                      backup->has_mode, backup->mode,
15253037f364SStefan Hajnoczi                      backup->has_speed, backup->speed,
15263037f364SStefan Hajnoczi                      backup->has_on_source_error, backup->on_source_error,
15273037f364SStefan Hajnoczi                      backup->has_on_target_error, backup->on_target_error,
15283037f364SStefan Hajnoczi                      &local_err);
152984d18f06SMarkus Armbruster     if (local_err) {
15303037f364SStefan Hajnoczi         error_propagate(errp, local_err);
15313037f364SStefan Hajnoczi         return;
15323037f364SStefan Hajnoczi     }
15333037f364SStefan Hajnoczi 
15345d6e96efSStefan Hajnoczi     state->bs = bs;
15353037f364SStefan Hajnoczi     state->job = state->bs->job;
15363037f364SStefan Hajnoczi }
15373037f364SStefan Hajnoczi 
15383037f364SStefan Hajnoczi static void drive_backup_abort(BlkTransactionState *common)
15393037f364SStefan Hajnoczi {
15403037f364SStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
15413037f364SStefan Hajnoczi     BlockDriverState *bs = state->bs;
15423037f364SStefan Hajnoczi 
15433037f364SStefan Hajnoczi     /* Only cancel if it's the job we started */
15443037f364SStefan Hajnoczi     if (bs && bs->job && bs->job == state->job) {
15453037f364SStefan Hajnoczi         block_job_cancel_sync(bs->job);
15463037f364SStefan Hajnoczi     }
15473037f364SStefan Hajnoczi }
15483037f364SStefan Hajnoczi 
15495d6e96efSStefan Hajnoczi static void drive_backup_clean(BlkTransactionState *common)
15505d6e96efSStefan Hajnoczi {
15515d6e96efSStefan Hajnoczi     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
15525d6e96efSStefan Hajnoczi 
15535d6e96efSStefan Hajnoczi     if (state->aio_context) {
15545d6e96efSStefan Hajnoczi         aio_context_release(state->aio_context);
15555d6e96efSStefan Hajnoczi     }
15565d6e96efSStefan Hajnoczi }
15575d6e96efSStefan Hajnoczi 
1558bd8baecdSFam Zheng typedef struct BlockdevBackupState {
1559bd8baecdSFam Zheng     BlkTransactionState common;
1560bd8baecdSFam Zheng     BlockDriverState *bs;
1561bd8baecdSFam Zheng     BlockJob *job;
1562bd8baecdSFam Zheng     AioContext *aio_context;
1563bd8baecdSFam Zheng } BlockdevBackupState;
1564bd8baecdSFam Zheng 
1565bd8baecdSFam Zheng static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp)
1566bd8baecdSFam Zheng {
1567bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1568bd8baecdSFam Zheng     BlockdevBackup *backup;
1569bd8baecdSFam Zheng     BlockDriverState *bs, *target;
1570a0e8544cSFam Zheng     BlockBackend *blk;
1571bd8baecdSFam Zheng     Error *local_err = NULL;
1572bd8baecdSFam Zheng 
1573bd8baecdSFam Zheng     assert(common->action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1574bd8baecdSFam Zheng     backup = common->action->blockdev_backup;
1575bd8baecdSFam Zheng 
1576a0e8544cSFam Zheng     blk = blk_by_name(backup->device);
1577a0e8544cSFam Zheng     if (!blk) {
15785b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", backup->device);
1579bd8baecdSFam Zheng         return;
1580bd8baecdSFam Zheng     }
1581a0e8544cSFam Zheng     bs = blk_bs(blk);
1582bd8baecdSFam Zheng 
1583a0e8544cSFam Zheng     blk = blk_by_name(backup->target);
1584a0e8544cSFam Zheng     if (!blk) {
15855b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", backup->target);
1586bd8baecdSFam Zheng         return;
1587bd8baecdSFam Zheng     }
1588a0e8544cSFam Zheng     target = blk_bs(blk);
1589bd8baecdSFam Zheng 
1590bd8baecdSFam Zheng     /* AioContext is released in .clean() */
1591bd8baecdSFam Zheng     state->aio_context = bdrv_get_aio_context(bs);
1592bd8baecdSFam Zheng     if (state->aio_context != bdrv_get_aio_context(target)) {
1593bd8baecdSFam Zheng         state->aio_context = NULL;
1594bd8baecdSFam Zheng         error_setg(errp, "Backup between two IO threads is not implemented");
1595bd8baecdSFam Zheng         return;
1596bd8baecdSFam Zheng     }
1597bd8baecdSFam Zheng     aio_context_acquire(state->aio_context);
1598bd8baecdSFam Zheng 
1599bd8baecdSFam Zheng     qmp_blockdev_backup(backup->device, backup->target,
1600bd8baecdSFam Zheng                         backup->sync,
1601bd8baecdSFam Zheng                         backup->has_speed, backup->speed,
1602bd8baecdSFam Zheng                         backup->has_on_source_error, backup->on_source_error,
1603bd8baecdSFam Zheng                         backup->has_on_target_error, backup->on_target_error,
1604bd8baecdSFam Zheng                         &local_err);
1605bd8baecdSFam Zheng     if (local_err) {
1606bd8baecdSFam Zheng         error_propagate(errp, local_err);
1607bd8baecdSFam Zheng         return;
1608bd8baecdSFam Zheng     }
1609bd8baecdSFam Zheng 
1610bd8baecdSFam Zheng     state->bs = bs;
1611bd8baecdSFam Zheng     state->job = state->bs->job;
1612bd8baecdSFam Zheng }
1613bd8baecdSFam Zheng 
1614bd8baecdSFam Zheng static void blockdev_backup_abort(BlkTransactionState *common)
1615bd8baecdSFam Zheng {
1616bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1617bd8baecdSFam Zheng     BlockDriverState *bs = state->bs;
1618bd8baecdSFam Zheng 
1619bd8baecdSFam Zheng     /* Only cancel if it's the job we started */
1620bd8baecdSFam Zheng     if (bs && bs->job && bs->job == state->job) {
1621bd8baecdSFam Zheng         block_job_cancel_sync(bs->job);
1622bd8baecdSFam Zheng     }
1623bd8baecdSFam Zheng }
1624bd8baecdSFam Zheng 
1625bd8baecdSFam Zheng static void blockdev_backup_clean(BlkTransactionState *common)
1626bd8baecdSFam Zheng {
1627bd8baecdSFam Zheng     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1628bd8baecdSFam Zheng 
1629bd8baecdSFam Zheng     if (state->aio_context) {
1630bd8baecdSFam Zheng         aio_context_release(state->aio_context);
1631bd8baecdSFam Zheng     }
1632bd8baecdSFam Zheng }
1633bd8baecdSFam Zheng 
163478b18b78SStefan Hajnoczi static void abort_prepare(BlkTransactionState *common, Error **errp)
163578b18b78SStefan Hajnoczi {
163678b18b78SStefan Hajnoczi     error_setg(errp, "Transaction aborted using Abort action");
163778b18b78SStefan Hajnoczi }
163878b18b78SStefan Hajnoczi 
163978b18b78SStefan Hajnoczi static void abort_commit(BlkTransactionState *common)
164078b18b78SStefan Hajnoczi {
1641dfc6f865SStefan Weil     g_assert_not_reached(); /* this action never succeeds */
164278b18b78SStefan Hajnoczi }
164378b18b78SStefan Hajnoczi 
1644ba0c86a3SWenchao Xia static const BdrvActionOps actions[] = {
1645c8a83e85SKevin Wolf     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1646ba5d6ab6SStefan Hajnoczi         .instance_size = sizeof(ExternalSnapshotState),
1647ba0c86a3SWenchao Xia         .prepare  = external_snapshot_prepare,
1648ba0c86a3SWenchao Xia         .commit   = external_snapshot_commit,
1649ba0c86a3SWenchao Xia         .abort = external_snapshot_abort,
1650ba0c86a3SWenchao Xia     },
16513037f364SStefan Hajnoczi     [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
16523037f364SStefan Hajnoczi         .instance_size = sizeof(DriveBackupState),
16533037f364SStefan Hajnoczi         .prepare = drive_backup_prepare,
16543037f364SStefan Hajnoczi         .abort = drive_backup_abort,
16555d6e96efSStefan Hajnoczi         .clean = drive_backup_clean,
16563037f364SStefan Hajnoczi     },
1657bd8baecdSFam Zheng     [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
1658bd8baecdSFam Zheng         .instance_size = sizeof(BlockdevBackupState),
1659bd8baecdSFam Zheng         .prepare = blockdev_backup_prepare,
1660bd8baecdSFam Zheng         .abort = blockdev_backup_abort,
1661bd8baecdSFam Zheng         .clean = blockdev_backup_clean,
1662bd8baecdSFam Zheng     },
166378b18b78SStefan Hajnoczi     [TRANSACTION_ACTION_KIND_ABORT] = {
166478b18b78SStefan Hajnoczi         .instance_size = sizeof(BlkTransactionState),
166578b18b78SStefan Hajnoczi         .prepare = abort_prepare,
166678b18b78SStefan Hajnoczi         .commit = abort_commit,
166778b18b78SStefan Hajnoczi     },
1668bbe86010SWenchao Xia     [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1669bbe86010SWenchao Xia         .instance_size = sizeof(InternalSnapshotState),
1670bbe86010SWenchao Xia         .prepare  = internal_snapshot_prepare,
1671bbe86010SWenchao Xia         .abort = internal_snapshot_abort,
16725d6e96efSStefan Hajnoczi         .clean = internal_snapshot_clean,
1673bbe86010SWenchao Xia     },
1674ba0c86a3SWenchao Xia };
1675ba0c86a3SWenchao Xia 
16768802d1fdSJeff Cody /*
1677b756b9ceSStefan Hajnoczi  * 'Atomic' group operations.  The operations are performed as a set, and if
1678b756b9ceSStefan Hajnoczi  * any fail then we roll back all operations in the group.
16798802d1fdSJeff Cody  */
1680c8a83e85SKevin Wolf void qmp_transaction(TransactionActionList *dev_list, Error **errp)
16818802d1fdSJeff Cody {
1682c8a83e85SKevin Wolf     TransactionActionList *dev_entry = dev_list;
1683ba5d6ab6SStefan Hajnoczi     BlkTransactionState *state, *next;
168443e17041SLuiz Capitulino     Error *local_err = NULL;
16858802d1fdSJeff Cody 
1686ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
16878802d1fdSJeff Cody     QSIMPLEQ_INIT(&snap_bdrv_states);
16888802d1fdSJeff Cody 
1689b756b9ceSStefan Hajnoczi     /* drain all i/o before any operations */
16908802d1fdSJeff Cody     bdrv_drain_all();
16918802d1fdSJeff Cody 
1692b756b9ceSStefan Hajnoczi     /* We don't do anything in this loop that commits us to the operations */
16938802d1fdSJeff Cody     while (NULL != dev_entry) {
1694c8a83e85SKevin Wolf         TransactionAction *dev_info = NULL;
1695ba0c86a3SWenchao Xia         const BdrvActionOps *ops;
169652e7c241SPaolo Bonzini 
16978802d1fdSJeff Cody         dev_info = dev_entry->value;
16988802d1fdSJeff Cody         dev_entry = dev_entry->next;
16998802d1fdSJeff Cody 
1700ba0c86a3SWenchao Xia         assert(dev_info->kind < ARRAY_SIZE(actions));
1701ba0c86a3SWenchao Xia 
1702ba0c86a3SWenchao Xia         ops = &actions[dev_info->kind];
1703aa3fe714SMax Reitz         assert(ops->instance_size > 0);
1704aa3fe714SMax Reitz 
1705ba5d6ab6SStefan Hajnoczi         state = g_malloc0(ops->instance_size);
1706ba5d6ab6SStefan Hajnoczi         state->ops = ops;
1707ba5d6ab6SStefan Hajnoczi         state->action = dev_info;
1708ba5d6ab6SStefan Hajnoczi         QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
17098802d1fdSJeff Cody 
1710ba5d6ab6SStefan Hajnoczi         state->ops->prepare(state, &local_err);
171184d18f06SMarkus Armbruster         if (local_err) {
17129b9877eeSWenchao Xia             error_propagate(errp, local_err);
17139b9877eeSWenchao Xia             goto delete_and_fail;
17149b9877eeSWenchao Xia         }
171552e7c241SPaolo Bonzini     }
17168802d1fdSJeff Cody 
1717ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1718f9ea81e8SStefan Hajnoczi         if (state->ops->commit) {
1719ba5d6ab6SStefan Hajnoczi             state->ops->commit(state);
17208802d1fdSJeff Cody         }
1721f9ea81e8SStefan Hajnoczi     }
17228802d1fdSJeff Cody 
17238802d1fdSJeff Cody     /* success */
17248802d1fdSJeff Cody     goto exit;
17258802d1fdSJeff Cody 
17268802d1fdSJeff Cody delete_and_fail:
1727b756b9ceSStefan Hajnoczi     /* failure, and it is all-or-none; roll back all operations */
1728ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1729ba5d6ab6SStefan Hajnoczi         if (state->ops->abort) {
1730ba5d6ab6SStefan Hajnoczi             state->ops->abort(state);
1731ba0c86a3SWenchao Xia         }
17328802d1fdSJeff Cody     }
17338802d1fdSJeff Cody exit:
1734ba5d6ab6SStefan Hajnoczi     QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1735ba5d6ab6SStefan Hajnoczi         if (state->ops->clean) {
1736ba5d6ab6SStefan Hajnoczi             state->ops->clean(state);
1737ba0c86a3SWenchao Xia         }
1738ba5d6ab6SStefan Hajnoczi         g_free(state);
17398802d1fdSJeff Cody     }
17408802d1fdSJeff Cody }
17418802d1fdSJeff Cody 
17428802d1fdSJeff Cody 
17436007cdd4SMarkus Armbruster static void eject_device(BlockBackend *blk, int force, Error **errp)
1744666daa68SMarkus Armbruster {
17456007cdd4SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
1746e3442099SStefan Hajnoczi     AioContext *aio_context;
1747e3442099SStefan Hajnoczi 
1748e3442099SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
1749e3442099SStefan Hajnoczi     aio_context_acquire(aio_context);
17506007cdd4SMarkus Armbruster 
17513718d8abSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
1752e3442099SStefan Hajnoczi         goto out;
17532d3735d3SStefan Hajnoczi     }
1754a7f53e26SMarkus Armbruster     if (!blk_dev_has_removable_media(blk)) {
1755f231b88dSCole Robinson         error_setg(errp, "Device '%s' is not removable",
1756f231b88dSCole Robinson                    bdrv_get_device_name(bs));
1757e3442099SStefan Hajnoczi         goto out;
1758666daa68SMarkus Armbruster     }
175992d48558SLuiz Capitulino 
1760a7f53e26SMarkus Armbruster     if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) {
1761a7f53e26SMarkus Armbruster         blk_dev_eject_request(blk, force);
1762025ccaa7SPaolo Bonzini         if (!force) {
1763f231b88dSCole Robinson             error_setg(errp, "Device '%s' is locked",
1764f231b88dSCole Robinson                        bdrv_get_device_name(bs));
1765e3442099SStefan Hajnoczi             goto out;
1766666daa68SMarkus Armbruster         }
1767025ccaa7SPaolo Bonzini     }
176892d48558SLuiz Capitulino 
1769666daa68SMarkus Armbruster     bdrv_close(bs);
1770e3442099SStefan Hajnoczi 
1771e3442099SStefan Hajnoczi out:
1772e3442099SStefan Hajnoczi     aio_context_release(aio_context);
1773666daa68SMarkus Armbruster }
1774666daa68SMarkus Armbruster 
1775c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1776666daa68SMarkus Armbruster {
17776007cdd4SMarkus Armbruster     BlockBackend *blk;
1778666daa68SMarkus Armbruster 
17796007cdd4SMarkus Armbruster     blk = blk_by_name(device);
17806007cdd4SMarkus Armbruster     if (!blk) {
1781c245b6a3SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1782c245b6a3SLuiz Capitulino         return;
1783666daa68SMarkus Armbruster     }
178492d48558SLuiz Capitulino 
17856007cdd4SMarkus Armbruster     eject_device(blk, force, errp);
1786666daa68SMarkus Armbruster }
1787666daa68SMarkus Armbruster 
178812d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device,
178912d3ba82SBenoît Canet                       bool has_node_name, const char *node_name,
179012d3ba82SBenoît Canet                       const char *password, Error **errp)
1791666daa68SMarkus Armbruster {
179212d3ba82SBenoît Canet     Error *local_err = NULL;
1793666daa68SMarkus Armbruster     BlockDriverState *bs;
1794e3442099SStefan Hajnoczi     AioContext *aio_context;
1795666daa68SMarkus Armbruster 
179612d3ba82SBenoît Canet     bs = bdrv_lookup_bs(has_device ? device : NULL,
179712d3ba82SBenoît Canet                         has_node_name ? node_name : NULL,
179812d3ba82SBenoît Canet                         &local_err);
179984d18f06SMarkus Armbruster     if (local_err) {
180012d3ba82SBenoît Canet         error_propagate(errp, local_err);
1801a4dea8a9SLuiz Capitulino         return;
1802666daa68SMarkus Armbruster     }
1803666daa68SMarkus Armbruster 
1804e3442099SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
1805e3442099SStefan Hajnoczi     aio_context_acquire(aio_context);
1806e3442099SStefan Hajnoczi 
18074d2855a3SMarkus Armbruster     bdrv_add_key(bs, password, errp);
1808666daa68SMarkus Armbruster 
1809e3442099SStefan Hajnoczi     aio_context_release(aio_context);
1810e3442099SStefan Hajnoczi }
1811e3442099SStefan Hajnoczi 
1812e3442099SStefan Hajnoczi /* Assumes AioContext is held */
1813333a96ecSLuiz Capitulino static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1814333a96ecSLuiz Capitulino                                     int bdrv_flags, BlockDriver *drv,
1815333a96ecSLuiz Capitulino                                     const char *password, Error **errp)
1816333a96ecSLuiz Capitulino {
181734b5d2c6SMax Reitz     Error *local_err = NULL;
18180eef407cSLuiz Capitulino     int ret;
18190eef407cSLuiz Capitulino 
1820ddf5636dSMax Reitz     ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
18210eef407cSLuiz Capitulino     if (ret < 0) {
182234b5d2c6SMax Reitz         error_propagate(errp, local_err);
1823333a96ecSLuiz Capitulino         return;
1824333a96ecSLuiz Capitulino     }
1825333a96ecSLuiz Capitulino 
18264d2855a3SMarkus Armbruster     bdrv_add_key(bs, password, errp);
1827333a96ecSLuiz Capitulino }
1828333a96ecSLuiz Capitulino 
1829333a96ecSLuiz Capitulino void qmp_change_blockdev(const char *device, const char *filename,
1830314f7ea7SMarc-André Lureau                          const char *format, Error **errp)
1831666daa68SMarkus Armbruster {
18326007cdd4SMarkus Armbruster     BlockBackend *blk;
1833666daa68SMarkus Armbruster     BlockDriverState *bs;
1834e3442099SStefan Hajnoczi     AioContext *aio_context;
1835666daa68SMarkus Armbruster     BlockDriver *drv = NULL;
1836666daa68SMarkus Armbruster     int bdrv_flags;
183792d48558SLuiz Capitulino     Error *err = NULL;
1838666daa68SMarkus Armbruster 
18396007cdd4SMarkus Armbruster     blk = blk_by_name(device);
18406007cdd4SMarkus Armbruster     if (!blk) {
1841333a96ecSLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1842333a96ecSLuiz Capitulino         return;
1843666daa68SMarkus Armbruster     }
18446007cdd4SMarkus Armbruster     bs = blk_bs(blk);
1845333a96ecSLuiz Capitulino 
1846e3442099SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
1847e3442099SStefan Hajnoczi     aio_context_acquire(aio_context);
1848e3442099SStefan Hajnoczi 
1849333a96ecSLuiz Capitulino     if (format) {
1850b64ec4e4SFam Zheng         drv = bdrv_find_whitelisted_format(format, bs->read_only);
1851666daa68SMarkus Armbruster         if (!drv) {
1852333a96ecSLuiz Capitulino             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1853e3442099SStefan Hajnoczi             goto out;
1854666daa68SMarkus Armbruster         }
1855666daa68SMarkus Armbruster     }
1856333a96ecSLuiz Capitulino 
18576007cdd4SMarkus Armbruster     eject_device(blk, 0, &err);
185884d18f06SMarkus Armbruster     if (err) {
1859333a96ecSLuiz Capitulino         error_propagate(errp, err);
1860e3442099SStefan Hajnoczi         goto out;
1861666daa68SMarkus Armbruster     }
1862333a96ecSLuiz Capitulino 
1863528f7663SMarkus Armbruster     bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1864199630b6SBlue Swirl     bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1865333a96ecSLuiz Capitulino 
1866333a96ecSLuiz Capitulino     qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1867e3442099SStefan Hajnoczi 
1868e3442099SStefan Hajnoczi out:
1869e3442099SStefan Hajnoczi     aio_context_release(aio_context);
1870666daa68SMarkus Armbruster }
18719063f814SRyan Harper 
1872727f005eSZhi Yong Wu /* throttling disk I/O limits */
187380047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
18743e9fab69SBenoît Canet                                int64_t bps_wr,
18753e9fab69SBenoît Canet                                int64_t iops,
18763e9fab69SBenoît Canet                                int64_t iops_rd,
18773e9fab69SBenoît Canet                                int64_t iops_wr,
18783e9fab69SBenoît Canet                                bool has_bps_max,
18793e9fab69SBenoît Canet                                int64_t bps_max,
18803e9fab69SBenoît Canet                                bool has_bps_rd_max,
18813e9fab69SBenoît Canet                                int64_t bps_rd_max,
18823e9fab69SBenoît Canet                                bool has_bps_wr_max,
18833e9fab69SBenoît Canet                                int64_t bps_wr_max,
18843e9fab69SBenoît Canet                                bool has_iops_max,
18853e9fab69SBenoît Canet                                int64_t iops_max,
18863e9fab69SBenoît Canet                                bool has_iops_rd_max,
18873e9fab69SBenoît Canet                                int64_t iops_rd_max,
18883e9fab69SBenoît Canet                                bool has_iops_wr_max,
18892024c1dfSBenoît Canet                                int64_t iops_wr_max,
18902024c1dfSBenoît Canet                                bool has_iops_size,
18912024c1dfSBenoît Canet                                int64_t iops_size, Error **errp)
1892727f005eSZhi Yong Wu {
1893cc0681c4SBenoît Canet     ThrottleConfig cfg;
1894727f005eSZhi Yong Wu     BlockDriverState *bs;
1895a0e8544cSFam Zheng     BlockBackend *blk;
1896b15446fdSStefan Hajnoczi     AioContext *aio_context;
1897727f005eSZhi Yong Wu 
1898a0e8544cSFam Zheng     blk = blk_by_name(device);
1899a0e8544cSFam Zheng     if (!blk) {
190080047da5SLuiz Capitulino         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
190180047da5SLuiz Capitulino         return;
1902727f005eSZhi Yong Wu     }
1903a0e8544cSFam Zheng     bs = blk_bs(blk);
1904727f005eSZhi Yong Wu 
1905cc0681c4SBenoît Canet     memset(&cfg, 0, sizeof(cfg));
1906cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1907cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_READ].avg  = bps_rd;
1908cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1909727f005eSZhi Yong Wu 
1910cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1911cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_READ].avg  = iops_rd;
1912cc0681c4SBenoît Canet     cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1913cc0681c4SBenoît Canet 
19143e9fab69SBenoît Canet     if (has_bps_max) {
19153e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
19163e9fab69SBenoît Canet     }
19173e9fab69SBenoît Canet     if (has_bps_rd_max) {
19183e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
19193e9fab69SBenoît Canet     }
19203e9fab69SBenoît Canet     if (has_bps_wr_max) {
19213e9fab69SBenoît Canet         cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
19223e9fab69SBenoît Canet     }
19233e9fab69SBenoît Canet     if (has_iops_max) {
19243e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
19253e9fab69SBenoît Canet     }
19263e9fab69SBenoît Canet     if (has_iops_rd_max) {
19273e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
19283e9fab69SBenoît Canet     }
19293e9fab69SBenoît Canet     if (has_iops_wr_max) {
19303e9fab69SBenoît Canet         cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
19313e9fab69SBenoît Canet     }
1932cc0681c4SBenoît Canet 
19332024c1dfSBenoît Canet     if (has_iops_size) {
19342024c1dfSBenoît Canet         cfg.op_size = iops_size;
19352024c1dfSBenoît Canet     }
1936cc0681c4SBenoît Canet 
1937cc0681c4SBenoît Canet     if (!check_throttle_config(&cfg, errp)) {
193880047da5SLuiz Capitulino         return;
1939727f005eSZhi Yong Wu     }
1940727f005eSZhi Yong Wu 
1941b15446fdSStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
1942b15446fdSStefan Hajnoczi     aio_context_acquire(aio_context);
1943b15446fdSStefan Hajnoczi 
1944cc0681c4SBenoît Canet     if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
1945727f005eSZhi Yong Wu         bdrv_io_limits_enable(bs);
1946cc0681c4SBenoît Canet     } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
1947727f005eSZhi Yong Wu         bdrv_io_limits_disable(bs);
1948727f005eSZhi Yong Wu     }
1949cc0681c4SBenoît Canet 
1950cc0681c4SBenoît Canet     if (bs->io_limits_enabled) {
1951cc0681c4SBenoît Canet         bdrv_set_io_limits(bs, &cfg);
1952727f005eSZhi Yong Wu     }
1953b15446fdSStefan Hajnoczi 
1954b15446fdSStefan Hajnoczi     aio_context_release(aio_context);
1955727f005eSZhi Yong Wu }
1956727f005eSZhi Yong Wu 
19573e5a50d6SMarkus Armbruster int hmp_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
19589063f814SRyan Harper {
19599063f814SRyan Harper     const char *id = qdict_get_str(qdict, "id");
19607e7d56d9SMarkus Armbruster     BlockBackend *blk;
19619063f814SRyan Harper     BlockDriverState *bs;
19628ad4202bSStefan Hajnoczi     AioContext *aio_context;
19633718d8abSFam Zheng     Error *local_err = NULL;
19649063f814SRyan Harper 
19657e7d56d9SMarkus Armbruster     blk = blk_by_name(id);
19667e7d56d9SMarkus Armbruster     if (!blk) {
1967b1422f20SMarkus Armbruster         error_report("Device '%s' not found", id);
19689063f814SRyan Harper         return -1;
19699063f814SRyan Harper     }
19707e7d56d9SMarkus Armbruster     bs = blk_bs(blk);
19718ad4202bSStefan Hajnoczi 
197226f8b3a8SMarkus Armbruster     if (!blk_legacy_dinfo(blk)) {
197348f364ddSMarkus Armbruster         error_report("Deleting device added with blockdev-add"
197448f364ddSMarkus Armbruster                      " is not supported");
197548f364ddSMarkus Armbruster         return -1;
197648f364ddSMarkus Armbruster     }
197748f364ddSMarkus Armbruster 
19788ad4202bSStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
19798ad4202bSStefan Hajnoczi     aio_context_acquire(aio_context);
19808ad4202bSStefan Hajnoczi 
19813718d8abSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
1982565f65d2SMarkus Armbruster         error_report_err(local_err);
19838ad4202bSStefan Hajnoczi         aio_context_release(aio_context);
19848591675fSMarcelo Tosatti         return -1;
19858591675fSMarcelo Tosatti     }
19869063f814SRyan Harper 
19879063f814SRyan Harper     /* quiesce block driver; prevent further io */
1988922453bcSStefan Hajnoczi     bdrv_drain_all();
19899063f814SRyan Harper     bdrv_flush(bs);
19909063f814SRyan Harper     bdrv_close(bs);
19919063f814SRyan Harper 
1992fa879d62SMarkus Armbruster     /* if we have a device attached to this BlockDriverState
1993d22b2f41SRyan Harper      * then we need to make the drive anonymous until the device
1994d22b2f41SRyan Harper      * can be removed.  If this is a drive with no device backing
1995d22b2f41SRyan Harper      * then we can just get rid of the block driver state right here.
1996d22b2f41SRyan Harper      */
1997a7f53e26SMarkus Armbruster     if (blk_get_attached_dev(blk)) {
19983e5a50d6SMarkus Armbruster         blk_hide_on_behalf_of_hmp_drive_del(blk);
1999293c51a6SStefan Hajnoczi         /* Further I/O must not pause the guest */
2000293c51a6SStefan Hajnoczi         bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
2001293c51a6SStefan Hajnoczi                           BLOCKDEV_ON_ERROR_REPORT);
2002d22b2f41SRyan Harper     } else {
2003b9fe8a7aSMarkus Armbruster         blk_unref(blk);
2004d22b2f41SRyan Harper     }
20059063f814SRyan Harper 
20068ad4202bSStefan Hajnoczi     aio_context_release(aio_context);
20079063f814SRyan Harper     return 0;
20089063f814SRyan Harper }
20096d4a2b3aSChristoph Hellwig 
20103b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device,
20113b1dbd11SBenoît Canet                       bool has_node_name, const char *node_name,
20123b1dbd11SBenoît Canet                       int64_t size, Error **errp)
20136d4a2b3aSChristoph Hellwig {
20143b1dbd11SBenoît Canet     Error *local_err = NULL;
20156d4a2b3aSChristoph Hellwig     BlockDriverState *bs;
2016927e0e76SStefan Hajnoczi     AioContext *aio_context;
20178732901eSKevin Wolf     int ret;
20186d4a2b3aSChristoph Hellwig 
20193b1dbd11SBenoît Canet     bs = bdrv_lookup_bs(has_device ? device : NULL,
20203b1dbd11SBenoît Canet                         has_node_name ? node_name : NULL,
20213b1dbd11SBenoît Canet                         &local_err);
202284d18f06SMarkus Armbruster     if (local_err) {
20233b1dbd11SBenoît Canet         error_propagate(errp, local_err);
20243b1dbd11SBenoît Canet         return;
20253b1dbd11SBenoît Canet     }
20263b1dbd11SBenoît Canet 
2027927e0e76SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2028927e0e76SStefan Hajnoczi     aio_context_acquire(aio_context);
2029927e0e76SStefan Hajnoczi 
20303b1dbd11SBenoît Canet     if (!bdrv_is_first_non_filter(bs)) {
20313b1dbd11SBenoît Canet         error_set(errp, QERR_FEATURE_DISABLED, "resize");
2032927e0e76SStefan Hajnoczi         goto out;
20336d4a2b3aSChristoph Hellwig     }
20346d4a2b3aSChristoph Hellwig 
20356d4a2b3aSChristoph Hellwig     if (size < 0) {
2036939a1cc3SStefan Hajnoczi         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2037927e0e76SStefan Hajnoczi         goto out;
20386d4a2b3aSChristoph Hellwig     }
20396d4a2b3aSChristoph Hellwig 
20409c75e168SJeff Cody     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
20419c75e168SJeff Cody         error_set(errp, QERR_DEVICE_IN_USE, device);
2042927e0e76SStefan Hajnoczi         goto out;
20439c75e168SJeff Cody     }
20449c75e168SJeff Cody 
204592b7a08dSPeter Lieven     /* complete all in-flight operations before resizing the device */
204692b7a08dSPeter Lieven     bdrv_drain_all();
204792b7a08dSPeter Lieven 
20488732901eSKevin Wolf     ret = bdrv_truncate(bs, size);
20498732901eSKevin Wolf     switch (ret) {
2050939a1cc3SStefan Hajnoczi     case 0:
2051939a1cc3SStefan Hajnoczi         break;
2052939a1cc3SStefan Hajnoczi     case -ENOMEDIUM:
2053939a1cc3SStefan Hajnoczi         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2054939a1cc3SStefan Hajnoczi         break;
2055939a1cc3SStefan Hajnoczi     case -ENOTSUP:
2056939a1cc3SStefan Hajnoczi         error_set(errp, QERR_UNSUPPORTED);
2057939a1cc3SStefan Hajnoczi         break;
2058939a1cc3SStefan Hajnoczi     case -EACCES:
205981e5f78aSAlberto Garcia         error_setg(errp, "Device '%s' is read only", device);
2060939a1cc3SStefan Hajnoczi         break;
2061939a1cc3SStefan Hajnoczi     case -EBUSY:
2062939a1cc3SStefan Hajnoczi         error_set(errp, QERR_DEVICE_IN_USE, device);
2063939a1cc3SStefan Hajnoczi         break;
2064939a1cc3SStefan Hajnoczi     default:
20658732901eSKevin Wolf         error_setg_errno(errp, -ret, "Could not resize");
2066939a1cc3SStefan Hajnoczi         break;
20676d4a2b3aSChristoph Hellwig     }
2068927e0e76SStefan Hajnoczi 
2069927e0e76SStefan Hajnoczi out:
2070927e0e76SStefan Hajnoczi     aio_context_release(aio_context);
20716d4a2b3aSChristoph Hellwig }
207212bd451fSStefan Hajnoczi 
20739abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret)
207412bd451fSStefan Hajnoczi {
2075723c5d93SStefan Hajnoczi     /* Note that this function may be executed from another AioContext besides
2076723c5d93SStefan Hajnoczi      * the QEMU main loop.  If you need to access anything that assumes the
2077723c5d93SStefan Hajnoczi      * QEMU global mutex, use a BH or introduce a mutex.
2078723c5d93SStefan Hajnoczi      */
2079723c5d93SStefan Hajnoczi 
208012bd451fSStefan Hajnoczi     BlockDriverState *bs = opaque;
2081bcada37bSWenchao Xia     const char *msg = NULL;
208212bd451fSStefan Hajnoczi 
20839abf2dbaSJeff Cody     trace_block_job_cb(bs, bs->job, ret);
208412bd451fSStefan Hajnoczi 
208512bd451fSStefan Hajnoczi     assert(bs->job);
2086bcada37bSWenchao Xia 
208712bd451fSStefan Hajnoczi     if (ret < 0) {
2088bcada37bSWenchao Xia         msg = strerror(-ret);
208912bd451fSStefan Hajnoczi     }
209012bd451fSStefan Hajnoczi 
2091370521a1SStefan Hajnoczi     if (block_job_is_cancelled(bs->job)) {
2092bcada37bSWenchao Xia         block_job_event_cancelled(bs->job);
2093370521a1SStefan Hajnoczi     } else {
2094bcada37bSWenchao Xia         block_job_event_completed(bs->job, msg);
2095370521a1SStefan Hajnoczi     }
2096aa398a5cSStefan Hajnoczi 
2097fa510ebfSFam Zheng     bdrv_put_ref_bh_schedule(bs);
209812bd451fSStefan Hajnoczi }
209912bd451fSStefan Hajnoczi 
210013d8cc51SJeff Cody void qmp_block_stream(const char *device,
210113d8cc51SJeff Cody                       bool has_base, const char *base,
210213d8cc51SJeff Cody                       bool has_backing_file, const char *backing_file,
210313d8cc51SJeff Cody                       bool has_speed, int64_t speed,
21041d809098SPaolo Bonzini                       bool has_on_error, BlockdevOnError on_error,
21051d809098SPaolo Bonzini                       Error **errp)
210612bd451fSStefan Hajnoczi {
2107a0e8544cSFam Zheng     BlockBackend *blk;
210812bd451fSStefan Hajnoczi     BlockDriverState *bs;
2109c8c3080fSMarcelo Tosatti     BlockDriverState *base_bs = NULL;
2110f3e69bebSStefan Hajnoczi     AioContext *aio_context;
2111fd7f8c65SStefan Hajnoczi     Error *local_err = NULL;
211213d8cc51SJeff Cody     const char *base_name = NULL;
211312bd451fSStefan Hajnoczi 
21141d809098SPaolo Bonzini     if (!has_on_error) {
21151d809098SPaolo Bonzini         on_error = BLOCKDEV_ON_ERROR_REPORT;
21161d809098SPaolo Bonzini     }
21171d809098SPaolo Bonzini 
2118a0e8544cSFam Zheng     blk = blk_by_name(device);
2119a0e8544cSFam Zheng     if (!blk) {
212012bd451fSStefan Hajnoczi         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
212112bd451fSStefan Hajnoczi         return;
212212bd451fSStefan Hajnoczi     }
2123a0e8544cSFam Zheng     bs = blk_bs(blk);
212412bd451fSStefan Hajnoczi 
2125f3e69bebSStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2126f3e69bebSStefan Hajnoczi     aio_context_acquire(aio_context);
2127f3e69bebSStefan Hajnoczi 
2128628ff683SFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2129f3e69bebSStefan Hajnoczi         goto out;
2130628ff683SFam Zheng     }
2131628ff683SFam Zheng 
213213d8cc51SJeff Cody     if (has_base) {
2133c8c3080fSMarcelo Tosatti         base_bs = bdrv_find_backing_image(bs, base);
2134c8c3080fSMarcelo Tosatti         if (base_bs == NULL) {
2135c8c3080fSMarcelo Tosatti             error_set(errp, QERR_BASE_NOT_FOUND, base);
2136f3e69bebSStefan Hajnoczi             goto out;
213712bd451fSStefan Hajnoczi         }
2138f3e69bebSStefan Hajnoczi         assert(bdrv_get_aio_context(base_bs) == aio_context);
213913d8cc51SJeff Cody         base_name = base;
2140c8c3080fSMarcelo Tosatti     }
214112bd451fSStefan Hajnoczi 
214213d8cc51SJeff Cody     /* if we are streaming the entire chain, the result will have no backing
214313d8cc51SJeff Cody      * file, and specifying one is therefore an error */
214413d8cc51SJeff Cody     if (base_bs == NULL && has_backing_file) {
214513d8cc51SJeff Cody         error_setg(errp, "backing file specified, but streaming the "
214613d8cc51SJeff Cody                          "entire chain");
2147f3e69bebSStefan Hajnoczi         goto out;
214813d8cc51SJeff Cody     }
214913d8cc51SJeff Cody 
215013d8cc51SJeff Cody     /* backing_file string overrides base bs filename */
215113d8cc51SJeff Cody     base_name = has_backing_file ? backing_file : base_name;
215213d8cc51SJeff Cody 
215313d8cc51SJeff Cody     stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
21541d809098SPaolo Bonzini                  on_error, block_job_cb, bs, &local_err);
215584d18f06SMarkus Armbruster     if (local_err) {
2156fd7f8c65SStefan Hajnoczi         error_propagate(errp, local_err);
2157f3e69bebSStefan Hajnoczi         goto out;
215812bd451fSStefan Hajnoczi     }
215912bd451fSStefan Hajnoczi 
216012bd451fSStefan Hajnoczi     trace_qmp_block_stream(bs, bs->job);
2161f3e69bebSStefan Hajnoczi 
2162f3e69bebSStefan Hajnoczi out:
2163f3e69bebSStefan Hajnoczi     aio_context_release(aio_context);
216412bd451fSStefan Hajnoczi }
21652d47c6e9SStefan Hajnoczi 
2166ed61fc10SJeff Cody void qmp_block_commit(const char *device,
21677676e2c5SJeff Cody                       bool has_base, const char *base,
21687676e2c5SJeff Cody                       bool has_top, const char *top,
216954e26900SJeff Cody                       bool has_backing_file, const char *backing_file,
2170ed61fc10SJeff Cody                       bool has_speed, int64_t speed,
2171ed61fc10SJeff Cody                       Error **errp)
2172ed61fc10SJeff Cody {
2173a0e8544cSFam Zheng     BlockBackend *blk;
2174ed61fc10SJeff Cody     BlockDriverState *bs;
2175ed61fc10SJeff Cody     BlockDriverState *base_bs, *top_bs;
21769e85cd5cSStefan Hajnoczi     AioContext *aio_context;
2177ed61fc10SJeff Cody     Error *local_err = NULL;
2178ed61fc10SJeff Cody     /* This will be part of the QMP command, if/when the
2179ed61fc10SJeff Cody      * BlockdevOnError change for blkmirror makes it in
2180ed61fc10SJeff Cody      */
218192aa5c6dSPaolo Bonzini     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2182ed61fc10SJeff Cody 
218354504663SMax Reitz     if (!has_speed) {
218454504663SMax Reitz         speed = 0;
218554504663SMax Reitz     }
218654504663SMax Reitz 
21877676e2c5SJeff Cody     /* Important Note:
21887676e2c5SJeff Cody      *  libvirt relies on the DeviceNotFound error class in order to probe for
21897676e2c5SJeff Cody      *  live commit feature versions; for this to work, we must make sure to
21907676e2c5SJeff Cody      *  perform the device lookup before any generic errors that may occur in a
21917676e2c5SJeff Cody      *  scenario in which all optional arguments are omitted. */
2192a0e8544cSFam Zheng     blk = blk_by_name(device);
2193a0e8544cSFam Zheng     if (!blk) {
2194ed61fc10SJeff Cody         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2195ed61fc10SJeff Cody         return;
2196ed61fc10SJeff Cody     }
2197a0e8544cSFam Zheng     bs = blk_bs(blk);
2198ed61fc10SJeff Cody 
21999e85cd5cSStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
22009e85cd5cSStefan Hajnoczi     aio_context_acquire(aio_context);
22019e85cd5cSStefan Hajnoczi 
22029e85cd5cSStefan Hajnoczi     /* drain all i/o before commits */
22039e85cd5cSStefan Hajnoczi     bdrv_drain_all();
22049e85cd5cSStefan Hajnoczi 
2205bb00021dSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
22069e85cd5cSStefan Hajnoczi         goto out;
2207628ff683SFam Zheng     }
2208628ff683SFam Zheng 
2209ed61fc10SJeff Cody     /* default top_bs is the active layer */
2210ed61fc10SJeff Cody     top_bs = bs;
2211ed61fc10SJeff Cody 
22127676e2c5SJeff Cody     if (has_top && top) {
2213ed61fc10SJeff Cody         if (strcmp(bs->filename, top) != 0) {
2214ed61fc10SJeff Cody             top_bs = bdrv_find_backing_image(bs, top);
2215ed61fc10SJeff Cody         }
2216ed61fc10SJeff Cody     }
2217ed61fc10SJeff Cody 
2218ed61fc10SJeff Cody     if (top_bs == NULL) {
2219ed61fc10SJeff Cody         error_setg(errp, "Top image file %s not found", top ? top : "NULL");
22209e85cd5cSStefan Hajnoczi         goto out;
2221ed61fc10SJeff Cody     }
2222ed61fc10SJeff Cody 
22239e85cd5cSStefan Hajnoczi     assert(bdrv_get_aio_context(top_bs) == aio_context);
22249e85cd5cSStefan Hajnoczi 
2225d5208c45SJeff Cody     if (has_base && base) {
2226d5208c45SJeff Cody         base_bs = bdrv_find_backing_image(top_bs, base);
2227d5208c45SJeff Cody     } else {
2228d5208c45SJeff Cody         base_bs = bdrv_find_base(top_bs);
2229d5208c45SJeff Cody     }
2230d5208c45SJeff Cody 
2231d5208c45SJeff Cody     if (base_bs == NULL) {
2232d5208c45SJeff Cody         error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
22339e85cd5cSStefan Hajnoczi         goto out;
2234d5208c45SJeff Cody     }
2235d5208c45SJeff Cody 
22369e85cd5cSStefan Hajnoczi     assert(bdrv_get_aio_context(base_bs) == aio_context);
22379e85cd5cSStefan Hajnoczi 
2238bb00021dSFam Zheng     if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2239bb00021dSFam Zheng         goto out;
2240bb00021dSFam Zheng     }
2241bb00021dSFam Zheng 
22427676e2c5SJeff Cody     /* Do not allow attempts to commit an image into itself */
22437676e2c5SJeff Cody     if (top_bs == base_bs) {
22447676e2c5SJeff Cody         error_setg(errp, "cannot commit an image into itself");
22459e85cd5cSStefan Hajnoczi         goto out;
22467676e2c5SJeff Cody     }
22477676e2c5SJeff Cody 
224820a63d2cSFam Zheng     if (top_bs == bs) {
224954e26900SJeff Cody         if (has_backing_file) {
225054e26900SJeff Cody             error_setg(errp, "'backing-file' specified,"
225154e26900SJeff Cody                              " but 'top' is the active layer");
22529e85cd5cSStefan Hajnoczi             goto out;
225354e26900SJeff Cody         }
225420a63d2cSFam Zheng         commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
225520a63d2cSFam Zheng                             bs, &local_err);
225620a63d2cSFam Zheng     } else {
2257ed61fc10SJeff Cody         commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
225854e26900SJeff Cody                      has_backing_file ? backing_file : NULL, &local_err);
225920a63d2cSFam Zheng     }
2260ed61fc10SJeff Cody     if (local_err != NULL) {
2261ed61fc10SJeff Cody         error_propagate(errp, local_err);
22629e85cd5cSStefan Hajnoczi         goto out;
2263ed61fc10SJeff Cody     }
22649e85cd5cSStefan Hajnoczi 
22659e85cd5cSStefan Hajnoczi out:
22669e85cd5cSStefan Hajnoczi     aio_context_release(aio_context);
2267ed61fc10SJeff Cody }
2268ed61fc10SJeff Cody 
226999a9addfSStefan Hajnoczi void qmp_drive_backup(const char *device, const char *target,
227099a9addfSStefan Hajnoczi                       bool has_format, const char *format,
2271b53169eaSStefan Hajnoczi                       enum MirrorSyncMode sync,
227299a9addfSStefan Hajnoczi                       bool has_mode, enum NewImageMode mode,
227399a9addfSStefan Hajnoczi                       bool has_speed, int64_t speed,
227499a9addfSStefan Hajnoczi                       bool has_on_source_error, BlockdevOnError on_source_error,
227599a9addfSStefan Hajnoczi                       bool has_on_target_error, BlockdevOnError on_target_error,
227699a9addfSStefan Hajnoczi                       Error **errp)
227799a9addfSStefan Hajnoczi {
2278a0e8544cSFam Zheng     BlockBackend *blk;
227999a9addfSStefan Hajnoczi     BlockDriverState *bs;
228099a9addfSStefan Hajnoczi     BlockDriverState *target_bs;
2281fc5d3f84SIan Main     BlockDriverState *source = NULL;
2282761731b1SStefan Hajnoczi     AioContext *aio_context;
228399a9addfSStefan Hajnoczi     BlockDriver *drv = NULL;
228499a9addfSStefan Hajnoczi     Error *local_err = NULL;
228599a9addfSStefan Hajnoczi     int flags;
228699a9addfSStefan Hajnoczi     int64_t size;
228799a9addfSStefan Hajnoczi     int ret;
228899a9addfSStefan Hajnoczi 
228999a9addfSStefan Hajnoczi     if (!has_speed) {
229099a9addfSStefan Hajnoczi         speed = 0;
229199a9addfSStefan Hajnoczi     }
229299a9addfSStefan Hajnoczi     if (!has_on_source_error) {
229399a9addfSStefan Hajnoczi         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
229499a9addfSStefan Hajnoczi     }
229599a9addfSStefan Hajnoczi     if (!has_on_target_error) {
229699a9addfSStefan Hajnoczi         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
229799a9addfSStefan Hajnoczi     }
229899a9addfSStefan Hajnoczi     if (!has_mode) {
229999a9addfSStefan Hajnoczi         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
230099a9addfSStefan Hajnoczi     }
230199a9addfSStefan Hajnoczi 
2302a0e8544cSFam Zheng     blk = blk_by_name(device);
2303a0e8544cSFam Zheng     if (!blk) {
230499a9addfSStefan Hajnoczi         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
230599a9addfSStefan Hajnoczi         return;
230699a9addfSStefan Hajnoczi     }
2307a0e8544cSFam Zheng     bs = blk_bs(blk);
230899a9addfSStefan Hajnoczi 
2309761731b1SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2310761731b1SStefan Hajnoczi     aio_context_acquire(aio_context);
2311761731b1SStefan Hajnoczi 
2312c29c1dd3SFam Zheng     /* Although backup_run has this check too, we need to use bs->drv below, so
2313c29c1dd3SFam Zheng      * do an early check redundantly. */
231499a9addfSStefan Hajnoczi     if (!bdrv_is_inserted(bs)) {
231599a9addfSStefan Hajnoczi         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2316761731b1SStefan Hajnoczi         goto out;
231799a9addfSStefan Hajnoczi     }
231899a9addfSStefan Hajnoczi 
231999a9addfSStefan Hajnoczi     if (!has_format) {
232099a9addfSStefan Hajnoczi         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
232199a9addfSStefan Hajnoczi     }
232299a9addfSStefan Hajnoczi     if (format) {
232399a9addfSStefan Hajnoczi         drv = bdrv_find_format(format);
232499a9addfSStefan Hajnoczi         if (!drv) {
232599a9addfSStefan Hajnoczi             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
2326761731b1SStefan Hajnoczi             goto out;
232799a9addfSStefan Hajnoczi         }
232899a9addfSStefan Hajnoczi     }
232999a9addfSStefan Hajnoczi 
2330c29c1dd3SFam Zheng     /* Early check to avoid creating target */
23313718d8abSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
2332761731b1SStefan Hajnoczi         goto out;
233399a9addfSStefan Hajnoczi     }
233499a9addfSStefan Hajnoczi 
233599a9addfSStefan Hajnoczi     flags = bs->open_flags | BDRV_O_RDWR;
233699a9addfSStefan Hajnoczi 
2337fc5d3f84SIan Main     /* See if we have a backing HD we can use to create our new image
2338fc5d3f84SIan Main      * on top of. */
2339fc5d3f84SIan Main     if (sync == MIRROR_SYNC_MODE_TOP) {
2340fc5d3f84SIan Main         source = bs->backing_hd;
2341fc5d3f84SIan Main         if (!source) {
2342fc5d3f84SIan Main             sync = MIRROR_SYNC_MODE_FULL;
2343fc5d3f84SIan Main         }
2344fc5d3f84SIan Main     }
2345fc5d3f84SIan Main     if (sync == MIRROR_SYNC_MODE_NONE) {
2346fc5d3f84SIan Main         source = bs;
2347fc5d3f84SIan Main     }
2348fc5d3f84SIan Main 
234999a9addfSStefan Hajnoczi     size = bdrv_getlength(bs);
235099a9addfSStefan Hajnoczi     if (size < 0) {
235199a9addfSStefan Hajnoczi         error_setg_errno(errp, -size, "bdrv_getlength failed");
2352761731b1SStefan Hajnoczi         goto out;
235399a9addfSStefan Hajnoczi     }
235499a9addfSStefan Hajnoczi 
235599a9addfSStefan Hajnoczi     if (mode != NEW_IMAGE_MODE_EXISTING) {
235699a9addfSStefan Hajnoczi         assert(format && drv);
2357fc5d3f84SIan Main         if (source) {
2358fc5d3f84SIan Main             bdrv_img_create(target, format, source->filename,
2359fc5d3f84SIan Main                             source->drv->format_name, NULL,
2360fc5d3f84SIan Main                             size, flags, &local_err, false);
2361fc5d3f84SIan Main         } else {
2362fc5d3f84SIan Main             bdrv_img_create(target, format, NULL, NULL, NULL,
2363fc5d3f84SIan Main                             size, flags, &local_err, false);
2364fc5d3f84SIan Main         }
236599a9addfSStefan Hajnoczi     }
236699a9addfSStefan Hajnoczi 
236784d18f06SMarkus Armbruster     if (local_err) {
236899a9addfSStefan Hajnoczi         error_propagate(errp, local_err);
2369761731b1SStefan Hajnoczi         goto out;
237099a9addfSStefan Hajnoczi     }
237199a9addfSStefan Hajnoczi 
2372f67503e5SMax Reitz     target_bs = NULL;
2373ddf5636dSMax Reitz     ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err);
237499a9addfSStefan Hajnoczi     if (ret < 0) {
237534b5d2c6SMax Reitz         error_propagate(errp, local_err);
2376761731b1SStefan Hajnoczi         goto out;
237799a9addfSStefan Hajnoczi     }
237899a9addfSStefan Hajnoczi 
2379761731b1SStefan Hajnoczi     bdrv_set_aio_context(target_bs, aio_context);
2380761731b1SStefan Hajnoczi 
2381fc5d3f84SIan Main     backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
238299a9addfSStefan Hajnoczi                  block_job_cb, bs, &local_err);
238399a9addfSStefan Hajnoczi     if (local_err != NULL) {
23844f6fd349SFam Zheng         bdrv_unref(target_bs);
238599a9addfSStefan Hajnoczi         error_propagate(errp, local_err);
2386761731b1SStefan Hajnoczi         goto out;
238799a9addfSStefan Hajnoczi     }
2388761731b1SStefan Hajnoczi 
2389761731b1SStefan Hajnoczi out:
2390761731b1SStefan Hajnoczi     aio_context_release(aio_context);
239199a9addfSStefan Hajnoczi }
239299a9addfSStefan Hajnoczi 
2393c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
2394c13163fbSBenoît Canet {
2395*d5a8ee60SAlberto Garcia     return bdrv_named_nodes_list(errp);
2396c13163fbSBenoît Canet }
2397c13163fbSBenoît Canet 
2398c29c1dd3SFam Zheng void qmp_blockdev_backup(const char *device, const char *target,
2399c29c1dd3SFam Zheng                          enum MirrorSyncMode sync,
2400c29c1dd3SFam Zheng                          bool has_speed, int64_t speed,
2401c29c1dd3SFam Zheng                          bool has_on_source_error,
2402c29c1dd3SFam Zheng                          BlockdevOnError on_source_error,
2403c29c1dd3SFam Zheng                          bool has_on_target_error,
2404c29c1dd3SFam Zheng                          BlockdevOnError on_target_error,
2405c29c1dd3SFam Zheng                          Error **errp)
2406c29c1dd3SFam Zheng {
2407a0e8544cSFam Zheng     BlockBackend *blk;
2408c29c1dd3SFam Zheng     BlockDriverState *bs;
2409c29c1dd3SFam Zheng     BlockDriverState *target_bs;
2410c29c1dd3SFam Zheng     Error *local_err = NULL;
2411c29c1dd3SFam Zheng     AioContext *aio_context;
2412c29c1dd3SFam Zheng 
2413c29c1dd3SFam Zheng     if (!has_speed) {
2414c29c1dd3SFam Zheng         speed = 0;
2415c29c1dd3SFam Zheng     }
2416c29c1dd3SFam Zheng     if (!has_on_source_error) {
2417c29c1dd3SFam Zheng         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2418c29c1dd3SFam Zheng     }
2419c29c1dd3SFam Zheng     if (!has_on_target_error) {
2420c29c1dd3SFam Zheng         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2421c29c1dd3SFam Zheng     }
2422c29c1dd3SFam Zheng 
2423a0e8544cSFam Zheng     blk = blk_by_name(device);
2424a0e8544cSFam Zheng     if (!blk) {
24255b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", device);
2426c29c1dd3SFam Zheng         return;
2427c29c1dd3SFam Zheng     }
2428a0e8544cSFam Zheng     bs = blk_bs(blk);
2429c29c1dd3SFam Zheng 
2430c29c1dd3SFam Zheng     aio_context = bdrv_get_aio_context(bs);
2431c29c1dd3SFam Zheng     aio_context_acquire(aio_context);
2432c29c1dd3SFam Zheng 
2433a0e8544cSFam Zheng     blk = blk_by_name(target);
2434a0e8544cSFam Zheng     if (!blk) {
24355b347c54SMarkus Armbruster         error_setg(errp, "Device '%s' not found", target);
2436c29c1dd3SFam Zheng         goto out;
2437c29c1dd3SFam Zheng     }
2438a0e8544cSFam Zheng     target_bs = blk_bs(blk);
2439c29c1dd3SFam Zheng 
2440c29c1dd3SFam Zheng     bdrv_ref(target_bs);
2441c29c1dd3SFam Zheng     bdrv_set_aio_context(target_bs, aio_context);
2442c29c1dd3SFam Zheng     backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
2443c29c1dd3SFam Zheng                  block_job_cb, bs, &local_err);
2444c29c1dd3SFam Zheng     if (local_err != NULL) {
2445c29c1dd3SFam Zheng         bdrv_unref(target_bs);
2446c29c1dd3SFam Zheng         error_propagate(errp, local_err);
2447c29c1dd3SFam Zheng     }
2448c29c1dd3SFam Zheng out:
2449c29c1dd3SFam Zheng     aio_context_release(aio_context);
2450c29c1dd3SFam Zheng }
2451c29c1dd3SFam Zheng 
245208e4ed6cSPaolo Bonzini #define DEFAULT_MIRROR_BUF_SIZE   (10 << 20)
245308e4ed6cSPaolo Bonzini 
2454d9b902dbSPaolo Bonzini void qmp_drive_mirror(const char *device, const char *target,
2455d9b902dbSPaolo Bonzini                       bool has_format, const char *format,
24564c828dc6SBenoît Canet                       bool has_node_name, const char *node_name,
245709158f00SBenoît Canet                       bool has_replaces, const char *replaces,
2458d9b902dbSPaolo Bonzini                       enum MirrorSyncMode sync,
2459d9b902dbSPaolo Bonzini                       bool has_mode, enum NewImageMode mode,
2460b952b558SPaolo Bonzini                       bool has_speed, int64_t speed,
2461eee13dfeSPaolo Bonzini                       bool has_granularity, uint32_t granularity,
246208e4ed6cSPaolo Bonzini                       bool has_buf_size, int64_t buf_size,
2463b952b558SPaolo Bonzini                       bool has_on_source_error, BlockdevOnError on_source_error,
2464b952b558SPaolo Bonzini                       bool has_on_target_error, BlockdevOnError on_target_error,
2465b952b558SPaolo Bonzini                       Error **errp)
2466d9b902dbSPaolo Bonzini {
2467a0e8544cSFam Zheng     BlockBackend *blk;
2468d9b902dbSPaolo Bonzini     BlockDriverState *bs;
2469d9b902dbSPaolo Bonzini     BlockDriverState *source, *target_bs;
24705a7e7a0bSStefan Hajnoczi     AioContext *aio_context;
2471d9b902dbSPaolo Bonzini     BlockDriver *drv = NULL;
2472d9b902dbSPaolo Bonzini     Error *local_err = NULL;
24734c828dc6SBenoît Canet     QDict *options = NULL;
2474d9b902dbSPaolo Bonzini     int flags;
2475ac3c5d83SStefan Hajnoczi     int64_t size;
2476d9b902dbSPaolo Bonzini     int ret;
2477d9b902dbSPaolo Bonzini 
2478d9b902dbSPaolo Bonzini     if (!has_speed) {
2479d9b902dbSPaolo Bonzini         speed = 0;
2480d9b902dbSPaolo Bonzini     }
2481b952b558SPaolo Bonzini     if (!has_on_source_error) {
2482b952b558SPaolo Bonzini         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2483b952b558SPaolo Bonzini     }
2484b952b558SPaolo Bonzini     if (!has_on_target_error) {
2485b952b558SPaolo Bonzini         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2486b952b558SPaolo Bonzini     }
2487d9b902dbSPaolo Bonzini     if (!has_mode) {
2488d9b902dbSPaolo Bonzini         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2489d9b902dbSPaolo Bonzini     }
2490eee13dfeSPaolo Bonzini     if (!has_granularity) {
2491eee13dfeSPaolo Bonzini         granularity = 0;
2492eee13dfeSPaolo Bonzini     }
249308e4ed6cSPaolo Bonzini     if (!has_buf_size) {
249408e4ed6cSPaolo Bonzini         buf_size = DEFAULT_MIRROR_BUF_SIZE;
249508e4ed6cSPaolo Bonzini     }
249608e4ed6cSPaolo Bonzini 
2497eee13dfeSPaolo Bonzini     if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
24983cbbe9fdSStefan Hajnoczi         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
24993cbbe9fdSStefan Hajnoczi                   "a value in range [512B, 64MB]");
2500eee13dfeSPaolo Bonzini         return;
2501eee13dfeSPaolo Bonzini     }
2502eee13dfeSPaolo Bonzini     if (granularity & (granularity - 1)) {
25033cbbe9fdSStefan Hajnoczi         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2");
2504eee13dfeSPaolo Bonzini         return;
2505eee13dfeSPaolo Bonzini     }
2506d9b902dbSPaolo Bonzini 
2507a0e8544cSFam Zheng     blk = blk_by_name(device);
2508a0e8544cSFam Zheng     if (!blk) {
2509d9b902dbSPaolo Bonzini         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2510d9b902dbSPaolo Bonzini         return;
2511d9b902dbSPaolo Bonzini     }
2512a0e8544cSFam Zheng     bs = blk_bs(blk);
2513d9b902dbSPaolo Bonzini 
25145a7e7a0bSStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
25155a7e7a0bSStefan Hajnoczi     aio_context_acquire(aio_context);
25165a7e7a0bSStefan Hajnoczi 
2517d9b902dbSPaolo Bonzini     if (!bdrv_is_inserted(bs)) {
2518d9b902dbSPaolo Bonzini         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
25195a7e7a0bSStefan Hajnoczi         goto out;
2520d9b902dbSPaolo Bonzini     }
2521d9b902dbSPaolo Bonzini 
2522d9b902dbSPaolo Bonzini     if (!has_format) {
2523d9b902dbSPaolo Bonzini         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2524d9b902dbSPaolo Bonzini     }
2525d9b902dbSPaolo Bonzini     if (format) {
2526d9b902dbSPaolo Bonzini         drv = bdrv_find_format(format);
2527d9b902dbSPaolo Bonzini         if (!drv) {
2528d9b902dbSPaolo Bonzini             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
25295a7e7a0bSStefan Hajnoczi             goto out;
2530d9b902dbSPaolo Bonzini         }
2531d9b902dbSPaolo Bonzini     }
2532d9b902dbSPaolo Bonzini 
25333718d8abSFam Zheng     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
25345a7e7a0bSStefan Hajnoczi         goto out;
2535d9b902dbSPaolo Bonzini     }
2536d9b902dbSPaolo Bonzini 
2537d9b902dbSPaolo Bonzini     flags = bs->open_flags | BDRV_O_RDWR;
2538d9b902dbSPaolo Bonzini     source = bs->backing_hd;
2539d9b902dbSPaolo Bonzini     if (!source && sync == MIRROR_SYNC_MODE_TOP) {
2540d9b902dbSPaolo Bonzini         sync = MIRROR_SYNC_MODE_FULL;
2541d9b902dbSPaolo Bonzini     }
2542117e0c82SMax Reitz     if (sync == MIRROR_SYNC_MODE_NONE) {
2543117e0c82SMax Reitz         source = bs;
2544117e0c82SMax Reitz     }
2545d9b902dbSPaolo Bonzini 
2546ac3c5d83SStefan Hajnoczi     size = bdrv_getlength(bs);
2547ac3c5d83SStefan Hajnoczi     if (size < 0) {
2548ac3c5d83SStefan Hajnoczi         error_setg_errno(errp, -size, "bdrv_getlength failed");
25495a7e7a0bSStefan Hajnoczi         goto out;
2550ac3c5d83SStefan Hajnoczi     }
2551ac3c5d83SStefan Hajnoczi 
255209158f00SBenoît Canet     if (has_replaces) {
255309158f00SBenoît Canet         BlockDriverState *to_replace_bs;
25545a7e7a0bSStefan Hajnoczi         AioContext *replace_aio_context;
25555a7e7a0bSStefan Hajnoczi         int64_t replace_size;
255609158f00SBenoît Canet 
255709158f00SBenoît Canet         if (!has_node_name) {
255809158f00SBenoît Canet             error_setg(errp, "a node-name must be provided when replacing a"
255909158f00SBenoît Canet                              " named node of the graph");
25605a7e7a0bSStefan Hajnoczi             goto out;
256109158f00SBenoît Canet         }
256209158f00SBenoît Canet 
256309158f00SBenoît Canet         to_replace_bs = check_to_replace_node(replaces, &local_err);
256409158f00SBenoît Canet 
256509158f00SBenoît Canet         if (!to_replace_bs) {
256609158f00SBenoît Canet             error_propagate(errp, local_err);
25675a7e7a0bSStefan Hajnoczi             goto out;
256809158f00SBenoît Canet         }
256909158f00SBenoît Canet 
25705a7e7a0bSStefan Hajnoczi         replace_aio_context = bdrv_get_aio_context(to_replace_bs);
25715a7e7a0bSStefan Hajnoczi         aio_context_acquire(replace_aio_context);
25725a7e7a0bSStefan Hajnoczi         replace_size = bdrv_getlength(to_replace_bs);
25735a7e7a0bSStefan Hajnoczi         aio_context_release(replace_aio_context);
25745a7e7a0bSStefan Hajnoczi 
25755a7e7a0bSStefan Hajnoczi         if (size != replace_size) {
257609158f00SBenoît Canet             error_setg(errp, "cannot replace image with a mirror image of "
257709158f00SBenoît Canet                              "different size");
25785a7e7a0bSStefan Hajnoczi             goto out;
257909158f00SBenoît Canet         }
258009158f00SBenoît Canet     }
258109158f00SBenoît Canet 
258214526864SMax Reitz     if ((sync == MIRROR_SYNC_MODE_FULL || !source)
258314526864SMax Reitz         && mode != NEW_IMAGE_MODE_EXISTING)
258414526864SMax Reitz     {
2585d9b902dbSPaolo Bonzini         /* create new image w/o backing file */
2586d9b902dbSPaolo Bonzini         assert(format && drv);
2587cf8f2426SLuiz Capitulino         bdrv_img_create(target, format,
2588f382d43aSMiroslav Rezanina                         NULL, NULL, NULL, size, flags, &local_err, false);
2589d9b902dbSPaolo Bonzini     } else {
2590d9b902dbSPaolo Bonzini         switch (mode) {
2591d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_EXISTING:
2592d9b902dbSPaolo Bonzini             break;
2593d9b902dbSPaolo Bonzini         case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2594d9b902dbSPaolo Bonzini             /* create new image with backing file */
2595cf8f2426SLuiz Capitulino             bdrv_img_create(target, format,
2596d9b902dbSPaolo Bonzini                             source->filename,
2597d9b902dbSPaolo Bonzini                             source->drv->format_name,
2598f382d43aSMiroslav Rezanina                             NULL, size, flags, &local_err, false);
2599d9b902dbSPaolo Bonzini             break;
2600d9b902dbSPaolo Bonzini         default:
2601d9b902dbSPaolo Bonzini             abort();
2602d9b902dbSPaolo Bonzini         }
2603d9b902dbSPaolo Bonzini     }
2604d9b902dbSPaolo Bonzini 
260584d18f06SMarkus Armbruster     if (local_err) {
2606cf8f2426SLuiz Capitulino         error_propagate(errp, local_err);
26075a7e7a0bSStefan Hajnoczi         goto out;
2608d9b902dbSPaolo Bonzini     }
2609d9b902dbSPaolo Bonzini 
26104c828dc6SBenoît Canet     if (has_node_name) {
26114c828dc6SBenoît Canet         options = qdict_new();
26124c828dc6SBenoît Canet         qdict_put(options, "node-name", qstring_from_str(node_name));
26134c828dc6SBenoît Canet     }
26144c828dc6SBenoît Canet 
2615b812f671SPaolo Bonzini     /* Mirroring takes care of copy-on-write using the source's backing
2616b812f671SPaolo Bonzini      * file.
2617b812f671SPaolo Bonzini      */
2618f67503e5SMax Reitz     target_bs = NULL;
26194c828dc6SBenoît Canet     ret = bdrv_open(&target_bs, target, NULL, options,
26204c828dc6SBenoît Canet                     flags | BDRV_O_NO_BACKING, drv, &local_err);
2621d9b902dbSPaolo Bonzini     if (ret < 0) {
262234b5d2c6SMax Reitz         error_propagate(errp, local_err);
26235a7e7a0bSStefan Hajnoczi         goto out;
2624d9b902dbSPaolo Bonzini     }
2625d9b902dbSPaolo Bonzini 
26265a7e7a0bSStefan Hajnoczi     bdrv_set_aio_context(target_bs, aio_context);
26275a7e7a0bSStefan Hajnoczi 
262809158f00SBenoît Canet     /* pass the node name to replace to mirror start since it's loose coupling
262909158f00SBenoît Canet      * and will allow to check whether the node still exist at mirror completion
263009158f00SBenoît Canet      */
263109158f00SBenoît Canet     mirror_start(bs, target_bs,
263209158f00SBenoît Canet                  has_replaces ? replaces : NULL,
263309158f00SBenoît Canet                  speed, granularity, buf_size, sync,
2634eee13dfeSPaolo Bonzini                  on_source_error, on_target_error,
2635b952b558SPaolo Bonzini                  block_job_cb, bs, &local_err);
2636d9b902dbSPaolo Bonzini     if (local_err != NULL) {
26374f6fd349SFam Zheng         bdrv_unref(target_bs);
2638d9b902dbSPaolo Bonzini         error_propagate(errp, local_err);
26395a7e7a0bSStefan Hajnoczi         goto out;
2640d9b902dbSPaolo Bonzini     }
26415a7e7a0bSStefan Hajnoczi 
26425a7e7a0bSStefan Hajnoczi out:
26435a7e7a0bSStefan Hajnoczi     aio_context_release(aio_context);
2644d9b902dbSPaolo Bonzini }
2645d9b902dbSPaolo Bonzini 
26463d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */
264724d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context,
264824d6bffeSMarkus Armbruster                                 Error **errp)
26492d47c6e9SStefan Hajnoczi {
2650a0e8544cSFam Zheng     BlockBackend *blk;
26512d47c6e9SStefan Hajnoczi     BlockDriverState *bs;
26522d47c6e9SStefan Hajnoczi 
2653a0e8544cSFam Zheng     blk = blk_by_name(device);
2654a0e8544cSFam Zheng     if (!blk) {
26553d948cdfSStefan Hajnoczi         goto notfound;
26562d47c6e9SStefan Hajnoczi     }
2657a0e8544cSFam Zheng     bs = blk_bs(blk);
26583d948cdfSStefan Hajnoczi 
26593d948cdfSStefan Hajnoczi     *aio_context = bdrv_get_aio_context(bs);
26603d948cdfSStefan Hajnoczi     aio_context_acquire(*aio_context);
26613d948cdfSStefan Hajnoczi 
26623d948cdfSStefan Hajnoczi     if (!bs->job) {
26633d948cdfSStefan Hajnoczi         aio_context_release(*aio_context);
26643d948cdfSStefan Hajnoczi         goto notfound;
26653d948cdfSStefan Hajnoczi     }
26663d948cdfSStefan Hajnoczi 
26672d47c6e9SStefan Hajnoczi     return bs->job;
26683d948cdfSStefan Hajnoczi 
26693d948cdfSStefan Hajnoczi notfound:
26702e3a0266SMarkus Armbruster     error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
26712e3a0266SMarkus Armbruster               "No active block job on device '%s'", device);
26723d948cdfSStefan Hajnoczi     *aio_context = NULL;
26733d948cdfSStefan Hajnoczi     return NULL;
26742d47c6e9SStefan Hajnoczi }
26752d47c6e9SStefan Hajnoczi 
2676882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
26772d47c6e9SStefan Hajnoczi {
26783d948cdfSStefan Hajnoczi     AioContext *aio_context;
267924d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
26802d47c6e9SStefan Hajnoczi 
26812d47c6e9SStefan Hajnoczi     if (!job) {
26822d47c6e9SStefan Hajnoczi         return;
26832d47c6e9SStefan Hajnoczi     }
26842d47c6e9SStefan Hajnoczi 
2685882ec7ceSStefan Hajnoczi     block_job_set_speed(job, speed, errp);
26863d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
26872d47c6e9SStefan Hajnoczi }
2688370521a1SStefan Hajnoczi 
26896e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device,
26906e37fb81SPaolo Bonzini                           bool has_force, bool force, Error **errp)
26916e37fb81SPaolo Bonzini {
26923d948cdfSStefan Hajnoczi     AioContext *aio_context;
269324d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
26946e37fb81SPaolo Bonzini 
26956e37fb81SPaolo Bonzini     if (!job) {
26966e37fb81SPaolo Bonzini         return;
26976e37fb81SPaolo Bonzini     }
26983d948cdfSStefan Hajnoczi 
26993d948cdfSStefan Hajnoczi     if (!has_force) {
27003d948cdfSStefan Hajnoczi         force = false;
27013d948cdfSStefan Hajnoczi     }
27023d948cdfSStefan Hajnoczi 
2703751ebd76SFam Zheng     if (job->user_paused && !force) {
2704f231b88dSCole Robinson         error_setg(errp, "The block job for device '%s' is currently paused",
2705f231b88dSCole Robinson                    device);
27063d948cdfSStefan Hajnoczi         goto out;
27076e37fb81SPaolo Bonzini     }
27086e37fb81SPaolo Bonzini 
27096e37fb81SPaolo Bonzini     trace_qmp_block_job_cancel(job);
27106e37fb81SPaolo Bonzini     block_job_cancel(job);
27113d948cdfSStefan Hajnoczi out:
27123d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
27136e37fb81SPaolo Bonzini }
27146e37fb81SPaolo Bonzini 
27156e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp)
2716370521a1SStefan Hajnoczi {
27173d948cdfSStefan Hajnoczi     AioContext *aio_context;
271824d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
2719370521a1SStefan Hajnoczi 
2720751ebd76SFam Zheng     if (!job || job->user_paused) {
2721370521a1SStefan Hajnoczi         return;
2722370521a1SStefan Hajnoczi     }
27236e37fb81SPaolo Bonzini 
2724751ebd76SFam Zheng     job->user_paused = true;
27256e37fb81SPaolo Bonzini     trace_qmp_block_job_pause(job);
27266e37fb81SPaolo Bonzini     block_job_pause(job);
27273d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
27286e37fb81SPaolo Bonzini }
27296e37fb81SPaolo Bonzini 
27306e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp)
27316e37fb81SPaolo Bonzini {
27323d948cdfSStefan Hajnoczi     AioContext *aio_context;
273324d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
27346e37fb81SPaolo Bonzini 
2735751ebd76SFam Zheng     if (!job || !job->user_paused) {
27368acc72a4SPaolo Bonzini         return;
27378acc72a4SPaolo Bonzini     }
2738370521a1SStefan Hajnoczi 
2739751ebd76SFam Zheng     job->user_paused = false;
27406e37fb81SPaolo Bonzini     trace_qmp_block_job_resume(job);
27416e37fb81SPaolo Bonzini     block_job_resume(job);
27423d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
2743370521a1SStefan Hajnoczi }
2744fb5458cdSStefan Hajnoczi 
2745aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp)
2746aeae883bSPaolo Bonzini {
27473d948cdfSStefan Hajnoczi     AioContext *aio_context;
274824d6bffeSMarkus Armbruster     BlockJob *job = find_block_job(device, &aio_context, errp);
2749aeae883bSPaolo Bonzini 
2750aeae883bSPaolo Bonzini     if (!job) {
2751aeae883bSPaolo Bonzini         return;
2752aeae883bSPaolo Bonzini     }
2753aeae883bSPaolo Bonzini 
2754aeae883bSPaolo Bonzini     trace_qmp_block_job_complete(job);
2755aeae883bSPaolo Bonzini     block_job_complete(job, errp);
27563d948cdfSStefan Hajnoczi     aio_context_release(aio_context);
2757aeae883bSPaolo Bonzini }
2758aeae883bSPaolo Bonzini 
2759fa40e656SJeff Cody void qmp_change_backing_file(const char *device,
2760fa40e656SJeff Cody                              const char *image_node_name,
2761fa40e656SJeff Cody                              const char *backing_file,
2762fa40e656SJeff Cody                              Error **errp)
2763fa40e656SJeff Cody {
2764a0e8544cSFam Zheng     BlockBackend *blk;
2765fa40e656SJeff Cody     BlockDriverState *bs = NULL;
2766729962f6SStefan Hajnoczi     AioContext *aio_context;
2767fa40e656SJeff Cody     BlockDriverState *image_bs = NULL;
2768fa40e656SJeff Cody     Error *local_err = NULL;
2769fa40e656SJeff Cody     bool ro;
2770fa40e656SJeff Cody     int open_flags;
2771fa40e656SJeff Cody     int ret;
2772fa40e656SJeff Cody 
2773a0e8544cSFam Zheng     blk = blk_by_name(device);
2774a0e8544cSFam Zheng     if (!blk) {
2775fa40e656SJeff Cody         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2776fa40e656SJeff Cody         return;
2777fa40e656SJeff Cody     }
2778a0e8544cSFam Zheng     bs = blk_bs(blk);
2779fa40e656SJeff Cody 
2780729962f6SStefan Hajnoczi     aio_context = bdrv_get_aio_context(bs);
2781729962f6SStefan Hajnoczi     aio_context_acquire(aio_context);
2782729962f6SStefan Hajnoczi 
2783fa40e656SJeff Cody     image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
2784fa40e656SJeff Cody     if (local_err) {
2785fa40e656SJeff Cody         error_propagate(errp, local_err);
2786729962f6SStefan Hajnoczi         goto out;
2787fa40e656SJeff Cody     }
2788fa40e656SJeff Cody 
2789fa40e656SJeff Cody     if (!image_bs) {
2790fa40e656SJeff Cody         error_setg(errp, "image file not found");
2791729962f6SStefan Hajnoczi         goto out;
2792fa40e656SJeff Cody     }
2793fa40e656SJeff Cody 
2794fa40e656SJeff Cody     if (bdrv_find_base(image_bs) == image_bs) {
2795fa40e656SJeff Cody         error_setg(errp, "not allowing backing file change on an image "
2796fa40e656SJeff Cody                          "without a backing file");
2797729962f6SStefan Hajnoczi         goto out;
2798fa40e656SJeff Cody     }
2799fa40e656SJeff Cody 
2800fa40e656SJeff Cody     /* even though we are not necessarily operating on bs, we need it to
2801fa40e656SJeff Cody      * determine if block ops are currently prohibited on the chain */
2802fa40e656SJeff Cody     if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
2803729962f6SStefan Hajnoczi         goto out;
2804fa40e656SJeff Cody     }
2805fa40e656SJeff Cody 
2806fa40e656SJeff Cody     /* final sanity check */
2807fa40e656SJeff Cody     if (!bdrv_chain_contains(bs, image_bs)) {
2808fa40e656SJeff Cody         error_setg(errp, "'%s' and image file are not in the same chain",
2809fa40e656SJeff Cody                    device);
2810729962f6SStefan Hajnoczi         goto out;
2811fa40e656SJeff Cody     }
2812fa40e656SJeff Cody 
2813fa40e656SJeff Cody     /* if not r/w, reopen to make r/w */
2814fa40e656SJeff Cody     open_flags = image_bs->open_flags;
2815fa40e656SJeff Cody     ro = bdrv_is_read_only(image_bs);
2816fa40e656SJeff Cody 
2817fa40e656SJeff Cody     if (ro) {
2818fa40e656SJeff Cody         bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
2819fa40e656SJeff Cody         if (local_err) {
2820fa40e656SJeff Cody             error_propagate(errp, local_err);
2821729962f6SStefan Hajnoczi             goto out;
2822fa40e656SJeff Cody         }
2823fa40e656SJeff Cody     }
2824fa40e656SJeff Cody 
2825fa40e656SJeff Cody     ret = bdrv_change_backing_file(image_bs, backing_file,
2826fa40e656SJeff Cody                                image_bs->drv ? image_bs->drv->format_name : "");
2827fa40e656SJeff Cody 
2828fa40e656SJeff Cody     if (ret < 0) {
2829fa40e656SJeff Cody         error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
2830fa40e656SJeff Cody                          backing_file);
2831fa40e656SJeff Cody         /* don't exit here, so we can try to restore open flags if
2832fa40e656SJeff Cody          * appropriate */
2833fa40e656SJeff Cody     }
2834fa40e656SJeff Cody 
2835fa40e656SJeff Cody     if (ro) {
2836fa40e656SJeff Cody         bdrv_reopen(image_bs, open_flags, &local_err);
2837fa40e656SJeff Cody         if (local_err) {
2838fa40e656SJeff Cody             error_propagate(errp, local_err); /* will preserve prior errp */
2839fa40e656SJeff Cody         }
2840fa40e656SJeff Cody     }
2841729962f6SStefan Hajnoczi 
2842729962f6SStefan Hajnoczi out:
2843729962f6SStefan Hajnoczi     aio_context_release(aio_context);
2844fa40e656SJeff Cody }
2845fa40e656SJeff Cody 
2846d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
2847d26c9a15SKevin Wolf {
2848d26c9a15SKevin Wolf     QmpOutputVisitor *ov = qmp_output_visitor_new();
284918e46a03SMarkus Armbruster     BlockBackend *blk;
2850d26c9a15SKevin Wolf     QObject *obj;
2851d26c9a15SKevin Wolf     QDict *qdict;
2852d26c9a15SKevin Wolf     Error *local_err = NULL;
2853d26c9a15SKevin Wolf 
2854d26c9a15SKevin Wolf     /* Require an ID in the top level */
2855d26c9a15SKevin Wolf     if (!options->has_id) {
2856d26c9a15SKevin Wolf         error_setg(errp, "Block device needs an ID");
2857d26c9a15SKevin Wolf         goto fail;
2858d26c9a15SKevin Wolf     }
2859d26c9a15SKevin Wolf 
286060e19e06SMarkus Armbruster     /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
2861d26c9a15SKevin Wolf      * cache.direct=false instead of silently switching to aio=threads, except
286260e19e06SMarkus Armbruster      * when called from drive_new().
2863d26c9a15SKevin Wolf      *
2864d26c9a15SKevin Wolf      * For now, simply forbidding the combination for all drivers will do. */
2865d26c9a15SKevin Wolf     if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
2866c6e0bd9bSKevin Wolf         bool direct = options->has_cache &&
2867c6e0bd9bSKevin Wolf                       options->cache->has_direct &&
2868c6e0bd9bSKevin Wolf                       options->cache->direct;
2869c6e0bd9bSKevin Wolf         if (!direct) {
2870d26c9a15SKevin Wolf             error_setg(errp, "aio=native requires cache.direct=true");
2871d26c9a15SKevin Wolf             goto fail;
2872d26c9a15SKevin Wolf         }
2873d26c9a15SKevin Wolf     }
2874d26c9a15SKevin Wolf 
2875d26c9a15SKevin Wolf     visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
2876d26c9a15SKevin Wolf                                &options, NULL, &local_err);
287784d18f06SMarkus Armbruster     if (local_err) {
2878d26c9a15SKevin Wolf         error_propagate(errp, local_err);
2879d26c9a15SKevin Wolf         goto fail;
2880d26c9a15SKevin Wolf     }
2881d26c9a15SKevin Wolf 
2882d26c9a15SKevin Wolf     obj = qmp_output_get_qobject(ov);
2883d26c9a15SKevin Wolf     qdict = qobject_to_qdict(obj);
2884d26c9a15SKevin Wolf 
2885d26c9a15SKevin Wolf     qdict_flatten(qdict);
2886d26c9a15SKevin Wolf 
288718e46a03SMarkus Armbruster     blk = blockdev_init(NULL, qdict, &local_err);
288884d18f06SMarkus Armbruster     if (local_err) {
2889b681072dSKevin Wolf         error_propagate(errp, local_err);
2890d26c9a15SKevin Wolf         goto fail;
2891d26c9a15SKevin Wolf     }
2892d26c9a15SKevin Wolf 
289318e46a03SMarkus Armbruster     if (bdrv_key_required(blk_bs(blk))) {
289418e46a03SMarkus Armbruster         blk_unref(blk);
28958ae8e904SKevin Wolf         error_setg(errp, "blockdev-add doesn't support encrypted devices");
28968ae8e904SKevin Wolf         goto fail;
28978ae8e904SKevin Wolf     }
28988ae8e904SKevin Wolf 
2899d26c9a15SKevin Wolf fail:
2900d26c9a15SKevin Wolf     qmp_output_visitor_cleanup(ov);
2901d26c9a15SKevin Wolf }
2902d26c9a15SKevin Wolf 
2903fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp)
2904fb5458cdSStefan Hajnoczi {
2905fea68bb6SMarkus Armbruster     BlockJobInfoList *head = NULL, **p_next = &head;
2906fea68bb6SMarkus Armbruster     BlockDriverState *bs;
2907fea68bb6SMarkus Armbruster 
2908fea68bb6SMarkus Armbruster     for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
290969691e72SStefan Hajnoczi         AioContext *aio_context = bdrv_get_aio_context(bs);
291069691e72SStefan Hajnoczi 
291169691e72SStefan Hajnoczi         aio_context_acquire(aio_context);
291269691e72SStefan Hajnoczi 
2913fea68bb6SMarkus Armbruster         if (bs->job) {
2914fea68bb6SMarkus Armbruster             BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
2915fea68bb6SMarkus Armbruster             elem->value = block_job_query(bs->job);
2916fea68bb6SMarkus Armbruster             *p_next = elem;
2917fea68bb6SMarkus Armbruster             p_next = &elem->next;
2918fea68bb6SMarkus Armbruster         }
291969691e72SStefan Hajnoczi 
292069691e72SStefan Hajnoczi         aio_context_release(aio_context);
2921fea68bb6SMarkus Armbruster     }
2922fea68bb6SMarkus Armbruster 
2923fea68bb6SMarkus Armbruster     return head;
2924fb5458cdSStefan Hajnoczi }
29254d454574SPaolo Bonzini 
29260006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = {
29274d454574SPaolo Bonzini     .name = "drive",
29280006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
29294d454574SPaolo Bonzini     .desc = {
29304d454574SPaolo Bonzini         {
29314d454574SPaolo Bonzini             .name = "snapshot",
29324d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
29334d454574SPaolo Bonzini             .help = "enable/disable snapshot mode",
29344d454574SPaolo Bonzini         },{
2935a9384affSPaolo Bonzini             .name = "discard",
2936a9384affSPaolo Bonzini             .type = QEMU_OPT_STRING,
2937a9384affSPaolo Bonzini             .help = "discard operation (ignore/off, unmap/on)",
2938a9384affSPaolo Bonzini         },{
293929c4e2b5SKevin Wolf             .name = "cache.writeback",
294029c4e2b5SKevin Wolf             .type = QEMU_OPT_BOOL,
294129c4e2b5SKevin Wolf             .help = "enables writeback mode for any caches",
294229c4e2b5SKevin Wolf         },{
294329c4e2b5SKevin Wolf             .name = "cache.direct",
294429c4e2b5SKevin Wolf             .type = QEMU_OPT_BOOL,
294529c4e2b5SKevin Wolf             .help = "enables use of O_DIRECT (bypass the host page cache)",
294629c4e2b5SKevin Wolf         },{
294729c4e2b5SKevin Wolf             .name = "cache.no-flush",
294829c4e2b5SKevin Wolf             .type = QEMU_OPT_BOOL,
294929c4e2b5SKevin Wolf             .help = "ignore any flush requests for the device",
29504d454574SPaolo Bonzini         },{
29514d454574SPaolo Bonzini             .name = "aio",
29524d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
29534d454574SPaolo Bonzini             .help = "host AIO implementation (threads, native)",
29544d454574SPaolo Bonzini         },{
29554d454574SPaolo Bonzini             .name = "format",
29564d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
29574d454574SPaolo Bonzini             .help = "disk format (raw, qcow2, ...)",
29584d454574SPaolo Bonzini         },{
29594d454574SPaolo Bonzini             .name = "rerror",
29604d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
29614d454574SPaolo Bonzini             .help = "read error action",
29624d454574SPaolo Bonzini         },{
29634d454574SPaolo Bonzini             .name = "werror",
29644d454574SPaolo Bonzini             .type = QEMU_OPT_STRING,
29654d454574SPaolo Bonzini             .help = "write error action",
29664d454574SPaolo Bonzini         },{
29670f227a94SKevin Wolf             .name = "read-only",
29684d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
29694d454574SPaolo Bonzini             .help = "open drive file as read-only",
29704d454574SPaolo Bonzini         },{
297157975222SKevin Wolf             .name = "throttling.iops-total",
29724d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
29734d454574SPaolo Bonzini             .help = "limit total I/O operations per second",
29744d454574SPaolo Bonzini         },{
297557975222SKevin Wolf             .name = "throttling.iops-read",
29764d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
29774d454574SPaolo Bonzini             .help = "limit read operations per second",
29784d454574SPaolo Bonzini         },{
297957975222SKevin Wolf             .name = "throttling.iops-write",
29804d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
29814d454574SPaolo Bonzini             .help = "limit write operations per second",
29824d454574SPaolo Bonzini         },{
298357975222SKevin Wolf             .name = "throttling.bps-total",
29844d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
29854d454574SPaolo Bonzini             .help = "limit total bytes per second",
29864d454574SPaolo Bonzini         },{
298757975222SKevin Wolf             .name = "throttling.bps-read",
29884d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
29894d454574SPaolo Bonzini             .help = "limit read bytes per second",
29904d454574SPaolo Bonzini         },{
299157975222SKevin Wolf             .name = "throttling.bps-write",
29924d454574SPaolo Bonzini             .type = QEMU_OPT_NUMBER,
29934d454574SPaolo Bonzini             .help = "limit write bytes per second",
29944d454574SPaolo Bonzini         },{
29953e9fab69SBenoît Canet             .name = "throttling.iops-total-max",
29963e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
29973e9fab69SBenoît Canet             .help = "I/O operations burst",
29983e9fab69SBenoît Canet         },{
29993e9fab69SBenoît Canet             .name = "throttling.iops-read-max",
30003e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
30013e9fab69SBenoît Canet             .help = "I/O operations read burst",
30023e9fab69SBenoît Canet         },{
30033e9fab69SBenoît Canet             .name = "throttling.iops-write-max",
30043e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
30053e9fab69SBenoît Canet             .help = "I/O operations write burst",
30063e9fab69SBenoît Canet         },{
30073e9fab69SBenoît Canet             .name = "throttling.bps-total-max",
30083e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
30093e9fab69SBenoît Canet             .help = "total bytes burst",
30103e9fab69SBenoît Canet         },{
30113e9fab69SBenoît Canet             .name = "throttling.bps-read-max",
30123e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
30133e9fab69SBenoît Canet             .help = "total bytes read burst",
30143e9fab69SBenoît Canet         },{
30153e9fab69SBenoît Canet             .name = "throttling.bps-write-max",
30163e9fab69SBenoît Canet             .type = QEMU_OPT_NUMBER,
30173e9fab69SBenoît Canet             .help = "total bytes write burst",
30183e9fab69SBenoît Canet         },{
30192024c1dfSBenoît Canet             .name = "throttling.iops-size",
30202024c1dfSBenoît Canet             .type = QEMU_OPT_NUMBER,
30212024c1dfSBenoît Canet             .help = "when limiting by iops max size of an I/O in bytes",
30222024c1dfSBenoît Canet         },{
30234d454574SPaolo Bonzini             .name = "copy-on-read",
30244d454574SPaolo Bonzini             .type = QEMU_OPT_BOOL,
30254d454574SPaolo Bonzini             .help = "copy read data from backing file into image file",
3026465bee1dSPeter Lieven         },{
3027465bee1dSPeter Lieven             .name = "detect-zeroes",
3028465bee1dSPeter Lieven             .type = QEMU_OPT_STRING,
3029465bee1dSPeter Lieven             .help = "try to optimize zero writes (off, on, unmap)",
30304d454574SPaolo Bonzini         },
30314d454574SPaolo Bonzini         { /* end of list */ }
30324d454574SPaolo Bonzini     },
30334d454574SPaolo Bonzini };
30340006383eSKevin Wolf 
30350006383eSKevin Wolf QemuOptsList qemu_drive_opts = {
30360006383eSKevin Wolf     .name = "drive",
30370006383eSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
30380006383eSKevin Wolf     .desc = {
3039492fdc6fSKevin Wolf         /*
3040492fdc6fSKevin Wolf          * no elements => accept any params
3041492fdc6fSKevin Wolf          * validation will happen later
3042492fdc6fSKevin Wolf          */
30430006383eSKevin Wolf         { /* end of list */ }
30440006383eSKevin Wolf     },
30450006383eSKevin Wolf };
3046