1666daa68SMarkus Armbruster /* 2666daa68SMarkus Armbruster * QEMU host block devices 3666daa68SMarkus Armbruster * 4666daa68SMarkus Armbruster * Copyright (c) 2003-2008 Fabrice Bellard 5666daa68SMarkus Armbruster * 6666daa68SMarkus Armbruster * This work is licensed under the terms of the GNU GPL, version 2 or 7666daa68SMarkus Armbruster * later. See the COPYING file in the top-level directory. 83618a094SMarkus Armbruster * 93618a094SMarkus Armbruster * This file incorporates work covered by the following copyright and 103618a094SMarkus Armbruster * permission notice: 113618a094SMarkus Armbruster * 123618a094SMarkus Armbruster * Copyright (c) 2003-2008 Fabrice Bellard 133618a094SMarkus Armbruster * 143618a094SMarkus Armbruster * Permission is hereby granted, free of charge, to any person obtaining a copy 153618a094SMarkus Armbruster * of this software and associated documentation files (the "Software"), to deal 163618a094SMarkus Armbruster * in the Software without restriction, including without limitation the rights 173618a094SMarkus Armbruster * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 183618a094SMarkus Armbruster * copies of the Software, and to permit persons to whom the Software is 193618a094SMarkus Armbruster * furnished to do so, subject to the following conditions: 203618a094SMarkus Armbruster * 213618a094SMarkus Armbruster * The above copyright notice and this permission notice shall be included in 223618a094SMarkus Armbruster * all copies or substantial portions of the Software. 233618a094SMarkus Armbruster * 243618a094SMarkus Armbruster * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 253618a094SMarkus Armbruster * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 263618a094SMarkus Armbruster * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 273618a094SMarkus Armbruster * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 283618a094SMarkus Armbruster * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 293618a094SMarkus Armbruster * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 303618a094SMarkus Armbruster * THE SOFTWARE. 31666daa68SMarkus Armbruster */ 32666daa68SMarkus Armbruster 33d38ea87aSPeter Maydell #include "qemu/osdep.h" 3426f54e9aSMarkus Armbruster #include "sysemu/block-backend.h" 359c17d615SPaolo Bonzini #include "sysemu/blockdev.h" 360d09e41aSPaolo Bonzini #include "hw/block/block.h" 37737e150eSPaolo Bonzini #include "block/blockjob.h" 3876f4afb4SAlberto Garcia #include "block/throttle-groups.h" 3983c9089eSPaolo Bonzini #include "monitor/monitor.h" 40d49b6836SMarkus Armbruster #include "qemu/error-report.h" 411de7afc9SPaolo Bonzini #include "qemu/option.h" 421de7afc9SPaolo Bonzini #include "qemu/config-file.h" 439af23989SMarkus Armbruster #include "qapi/qapi-commands-block.h" 449af23989SMarkus Armbruster #include "qapi/qapi-commands-transaction.h" 459af23989SMarkus Armbruster #include "qapi/qapi-visit-block-core.h" 46452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 4715280c36SMarkus Armbruster #include "qapi/qmp/qnum.h" 486b673957SMarkus Armbruster #include "qapi/qmp/qstring.h" 49e688df6bSMarkus Armbruster #include "qapi/error.h" 50cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 5147e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h" 52b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h" 539c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 54ca00bbb1SStefan Hajnoczi #include "sysemu/iothread.h" 55737e150eSPaolo Bonzini #include "block/block_int.h" 560ab8ed18SDaniel P. Berrange #include "block/trace.h" 579c17d615SPaolo Bonzini #include "sysemu/arch_init.h" 58c616f16eSThomas Huth #include "sysemu/qtest.h" 59f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 60f348b6d1SVeronia Bahaa #include "qemu/help_option.h" 61a2a7862cSPradeep Jagadeesh #include "qemu/throttle-options.h" 62666daa68SMarkus Armbruster 639c4218e9SMax Reitz static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = 649c4218e9SMax Reitz QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); 659c4218e9SMax Reitz 66b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id, 67b33945cfSKevin Wolf bool force, Error **errp); 6882fcf66eSMax Reitz static void blockdev_remove_medium(bool has_device, const char *device, 6982fcf66eSMax Reitz bool has_id, const char *id, Error **errp); 7082fcf66eSMax Reitz static void blockdev_insert_medium(bool has_device, const char *device, 7182fcf66eSMax Reitz bool has_id, const char *id, 7282fcf66eSMax Reitz const char *node_name, Error **errp); 73bf18bee5SColin Lord 741960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 751960966dSMarkus Armbruster [IF_NONE] = "none", 761960966dSMarkus Armbruster [IF_IDE] = "ide", 771960966dSMarkus Armbruster [IF_SCSI] = "scsi", 781960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 791960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 801960966dSMarkus Armbruster [IF_MTD] = "mtd", 811960966dSMarkus Armbruster [IF_SD] = "sd", 821960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 831960966dSMarkus Armbruster [IF_XEN] = "xen", 841960966dSMarkus Armbruster }; 851960966dSMarkus Armbruster 8621dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = { 8727d6bf40SMarkus Armbruster /* 8827d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 8927d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 9027d6bf40SMarkus Armbruster * 91547cb157SWei Jiangang * All controllers used to implement if=T drives need to support 9227d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 9327d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 9427d6bf40SMarkus Armbruster * values. 9527d6bf40SMarkus Armbruster * 9627d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 9727d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 9827d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 9927d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 10027d6bf40SMarkus Armbruster */ 10127d6bf40SMarkus Armbruster [IF_IDE] = 2, 10227d6bf40SMarkus Armbruster [IF_SCSI] = 7, 1031960966dSMarkus Armbruster }; 1041960966dSMarkus Armbruster 10521dff8cfSJohn Snow /** 10621dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides 10721dff8cfSJohn Snow * of the default, global values. 10821dff8cfSJohn Snow */ 10921dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs) 11021dff8cfSJohn Snow { 11118e46a03SMarkus Armbruster BlockBackend *blk; 11221dff8cfSJohn Snow DriveInfo *dinfo; 11321dff8cfSJohn Snow 11421dff8cfSJohn Snow if (max_devs <= 0) { 11521dff8cfSJohn Snow return; 11621dff8cfSJohn Snow } 11721dff8cfSJohn Snow 11818e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 11918e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 12021dff8cfSJohn Snow if (dinfo->type == type) { 12121dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of" 12221dff8cfSJohn Snow " the %s interface, because a drive of that type has" 12321dff8cfSJohn Snow " already been added.\n", if_name[type]); 12421dff8cfSJohn Snow g_assert_not_reached(); 12521dff8cfSJohn Snow } 12621dff8cfSJohn Snow } 12721dff8cfSJohn Snow 12821dff8cfSJohn Snow if_max_devs[type] = max_devs; 12921dff8cfSJohn Snow } 13021dff8cfSJohn Snow 13114bafc54SMarkus Armbruster /* 13214bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 13314bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 13414bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 13514bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 13614bafc54SMarkus Armbruster * when deletion is actually safe. 13714bafc54SMarkus Armbruster */ 1384be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk) 13914bafc54SMarkus Armbruster { 14018e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 1414be74634SMarkus Armbruster BlockDriverState *bs = blk_bs(blk); 14291fddb0dSStefan Hajnoczi AioContext *aio_context; 14314bafc54SMarkus Armbruster 14426f8b3a8SMarkus Armbruster if (!dinfo) { 1452d246f01SKevin Wolf return; 1462d246f01SKevin Wolf } 1472d246f01SKevin Wolf 1485433c24fSMax Reitz if (bs) { 14991fddb0dSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 15091fddb0dSStefan Hajnoczi aio_context_acquire(aio_context); 15191fddb0dSStefan Hajnoczi 15212bde0eeSPaolo Bonzini if (bs->job) { 1533d70ff53SKevin Wolf job_cancel(&bs->job->job, false); 15412bde0eeSPaolo Bonzini } 15591fddb0dSStefan Hajnoczi 15691fddb0dSStefan Hajnoczi aio_context_release(aio_context); 1575433c24fSMax Reitz } 15891fddb0dSStefan Hajnoczi 15914bafc54SMarkus Armbruster dinfo->auto_del = 1; 16014bafc54SMarkus Armbruster } 16114bafc54SMarkus Armbruster 1624be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk) 16314bafc54SMarkus Armbruster { 16418e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 16514bafc54SMarkus Armbruster 1660fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 167efaa7c4eSMax Reitz monitor_remove_blk(blk); 168b9fe8a7aSMarkus Armbruster blk_unref(blk); 16914bafc54SMarkus Armbruster } 17014bafc54SMarkus Armbruster } 17114bafc54SMarkus Armbruster 172d8f94e1bSJohn Snow /** 173d8f94e1bSJohn Snow * Returns the current mapping of how many units per bus 174d8f94e1bSJohn Snow * a particular interface can support. 175d8f94e1bSJohn Snow * 176d8f94e1bSJohn Snow * A positive integer indicates n units per bus. 177d8f94e1bSJohn Snow * 0 implies the mapping has not been established. 178d8f94e1bSJohn Snow * -1 indicates an invalid BlockInterfaceType was given. 179d8f94e1bSJohn Snow */ 180d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type) 181d8f94e1bSJohn Snow { 182d8f94e1bSJohn Snow if (type >= IF_IDE && type < IF_COUNT) { 183d8f94e1bSJohn Snow return if_max_devs[type]; 184d8f94e1bSJohn Snow } 185d8f94e1bSJohn Snow 186d8f94e1bSJohn Snow return -1; 187d8f94e1bSJohn Snow } 188d8f94e1bSJohn Snow 189505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 190505a7fb1SMarkus Armbruster { 191505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 192505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 193505a7fb1SMarkus Armbruster } 194505a7fb1SMarkus Armbruster 195505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 196505a7fb1SMarkus Armbruster { 197505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 198505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 199505a7fb1SMarkus Armbruster } 200505a7fb1SMarkus Armbruster 2012292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 2022292ddaeSMarkus Armbruster { 20370b94331SMarkus Armbruster return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 2042292ddaeSMarkus Armbruster } 2052292ddaeSMarkus Armbruster 2062292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 2075645b0f4SMarkus Armbruster const char *optstr) 208666daa68SMarkus Armbruster { 209666daa68SMarkus Armbruster QemuOpts *opts; 210666daa68SMarkus Armbruster 2112292ddaeSMarkus Armbruster opts = drive_def(optstr); 212666daa68SMarkus Armbruster if (!opts) { 213666daa68SMarkus Armbruster return NULL; 214666daa68SMarkus Armbruster } 2152292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 216f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort); 2172292ddaeSMarkus Armbruster } 2182292ddaeSMarkus Armbruster if (index >= 0) { 219a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort); 2202292ddaeSMarkus Armbruster } 221666daa68SMarkus Armbruster if (file) 222f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort); 223666daa68SMarkus Armbruster return opts; 224666daa68SMarkus Armbruster } 225666daa68SMarkus Armbruster 226666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 227666daa68SMarkus Armbruster { 22818e46a03SMarkus Armbruster BlockBackend *blk; 229666daa68SMarkus Armbruster DriveInfo *dinfo; 230666daa68SMarkus Armbruster 23118e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 23218e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 23318e46a03SMarkus Armbruster if (dinfo && dinfo->type == type 23418e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) { 235666daa68SMarkus Armbruster return dinfo; 236666daa68SMarkus Armbruster } 23718e46a03SMarkus Armbruster } 238666daa68SMarkus Armbruster 239666daa68SMarkus Armbruster return NULL; 240666daa68SMarkus Armbruster } 241666daa68SMarkus Armbruster 242720b8dc0SMarkus Armbruster void drive_check_orphaned(void) 243a66c9dc7SJohn Snow { 24418e46a03SMarkus Armbruster BlockBackend *blk; 245a66c9dc7SJohn Snow DriveInfo *dinfo; 246664cc623SMarkus Armbruster Location loc; 247720b8dc0SMarkus Armbruster bool orphans = false; 248a66c9dc7SJohn Snow 24918e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 25018e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 251a7f53e26SMarkus Armbruster if (!blk_get_attached_dev(blk) && !dinfo->is_default && 252a66c9dc7SJohn Snow dinfo->type != IF_NONE) { 253664cc623SMarkus Armbruster loc_push_none(&loc); 254664cc623SMarkus Armbruster qemu_opts_loc_restore(dinfo->opts); 255720b8dc0SMarkus Armbruster error_report("machine type does not support" 256664cc623SMarkus Armbruster " if=%s,bus=%d,unit=%d", 2575433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit); 258664cc623SMarkus Armbruster loc_pop(&loc); 259720b8dc0SMarkus Armbruster orphans = true; 260a66c9dc7SJohn Snow } 261a66c9dc7SJohn Snow } 262a66c9dc7SJohn Snow 263720b8dc0SMarkus Armbruster if (orphans) { 264720b8dc0SMarkus Armbruster exit(1); 265720b8dc0SMarkus Armbruster } 266a66c9dc7SJohn Snow } 267a66c9dc7SJohn Snow 268f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 269f1bd51acSMarkus Armbruster { 270f1bd51acSMarkus Armbruster return drive_get(type, 271f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 272f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 273f1bd51acSMarkus Armbruster } 274f1bd51acSMarkus Armbruster 275666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 276666daa68SMarkus Armbruster { 277666daa68SMarkus Armbruster int max_bus; 27818e46a03SMarkus Armbruster BlockBackend *blk; 279666daa68SMarkus Armbruster DriveInfo *dinfo; 280666daa68SMarkus Armbruster 281666daa68SMarkus Armbruster max_bus = -1; 28218e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 28318e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 28418e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 285666daa68SMarkus Armbruster max_bus = dinfo->bus; 286666daa68SMarkus Armbruster } 28718e46a03SMarkus Armbruster } 288666daa68SMarkus Armbruster return max_bus; 289666daa68SMarkus Armbruster } 290666daa68SMarkus Armbruster 29113839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 29213839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 29313839974SMarkus Armbruster appropriate bus. */ 29413839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 29513839974SMarkus Armbruster { 29613839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 29713839974SMarkus Armbruster 29813839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 29913839974SMarkus Armbruster } 30013839974SMarkus Armbruster 301666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 302666daa68SMarkus Armbruster { 303807105a7SMarkus Armbruster error_printf(" %s", name); 304666daa68SMarkus Armbruster } 305666daa68SMarkus Armbruster 306aa398a5cSStefan Hajnoczi typedef struct { 307aa398a5cSStefan Hajnoczi QEMUBH *bh; 308fa510ebfSFam Zheng BlockDriverState *bs; 309fa510ebfSFam Zheng } BDRVPutRefBH; 310aa398a5cSStefan Hajnoczi 311b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 312666daa68SMarkus Armbruster { 313666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 31492aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 315666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 31692aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 317666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 31892aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 319666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 32092aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 321666daa68SMarkus Armbruster } else { 322b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 323666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 324666daa68SMarkus Armbruster return -1; 325666daa68SMarkus Armbruster } 326666daa68SMarkus Armbruster } 327666daa68SMarkus Armbruster 32840119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 32940119effSAlberto Garcia Error **errp) 33040119effSAlberto Garcia { 33140119effSAlberto Garcia const QListEntry *entry; 33240119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 33340119effSAlberto Garcia switch (qobject_type(entry->value)) { 33440119effSAlberto Garcia 33540119effSAlberto Garcia case QTYPE_QSTRING: { 33640119effSAlberto Garcia unsigned long long length; 3377dc847ebSMax Reitz const char *str = qstring_get_str(qobject_to(QString, 3387dc847ebSMax Reitz entry->value)); 33940119effSAlberto Garcia if (parse_uint_full(str, &length, 10) == 0 && 34040119effSAlberto Garcia length > 0 && length <= UINT_MAX) { 34140119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 34240119effSAlberto Garcia } else { 34340119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str); 34440119effSAlberto Garcia return false; 34540119effSAlberto Garcia } 34640119effSAlberto Garcia break; 34740119effSAlberto Garcia } 34840119effSAlberto Garcia 34901b2ffceSMarc-André Lureau case QTYPE_QNUM: { 3507dc847ebSMax Reitz int64_t length = qnum_get_int(qobject_to(QNum, entry->value)); 35101b2ffceSMarc-André Lureau 35240119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) { 35340119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 35440119effSAlberto Garcia } else { 35540119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length); 35640119effSAlberto Garcia return false; 35740119effSAlberto Garcia } 35840119effSAlberto Garcia break; 35940119effSAlberto Garcia } 36040119effSAlberto Garcia 36140119effSAlberto Garcia default: 36240119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid"); 36340119effSAlberto Garcia return false; 36440119effSAlberto Garcia } 36540119effSAlberto Garcia } 36640119effSAlberto Garcia return true; 36740119effSAlberto Garcia } 36840119effSAlberto Garcia 36933cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 37033cb7dc8SKevin Wolf 371fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 372fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 373fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 374fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 375fbf8175eSMax Reitz { 376fbf8175eSMax Reitz Error *local_error = NULL; 377fbf8175eSMax Reitz const char *aio; 378fbf8175eSMax Reitz 379fbf8175eSMax Reitz if (bdrv_flags) { 380fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 381fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 382fbf8175eSMax Reitz } 383fbf8175eSMax Reitz 384fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 385fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 386fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 387fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 388fbf8175eSMax Reitz /* this is the default */ 389fbf8175eSMax Reitz } else { 390fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 391fbf8175eSMax Reitz return; 392fbf8175eSMax Reitz } 393fbf8175eSMax Reitz } 394fbf8175eSMax Reitz } 395fbf8175eSMax Reitz 396fbf8175eSMax Reitz /* disk I/O throttling */ 397fbf8175eSMax Reitz if (throttling_group) { 398fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 399fbf8175eSMax Reitz } 400fbf8175eSMax Reitz 401fbf8175eSMax Reitz if (throttle_cfg) { 4021588ab5dSAlberto Garcia throttle_config_init(throttle_cfg); 403fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 404fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 405fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 406fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 407fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 408fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 409fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 410fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 411fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 412fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 413fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 414fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 415fbf8175eSMax Reitz 416fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 417fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 418fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 419fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 420fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 421fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 422fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 423fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 424fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 425fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 426fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 427fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 428fbf8175eSMax Reitz 4298a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = 4308a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); 4318a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = 4328a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); 4338a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = 4348a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); 4358a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = 4368a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); 4378a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = 4388a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); 4398a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = 4408a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); 4418a0fc18dSAlberto Garcia 442fbf8175eSMax Reitz throttle_cfg->op_size = 443fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 444fbf8175eSMax Reitz 445d5851089SAlberto Garcia if (!throttle_is_valid(throttle_cfg, errp)) { 446fbf8175eSMax Reitz return; 447fbf8175eSMax Reitz } 448fbf8175eSMax Reitz } 449fbf8175eSMax Reitz 450fbf8175eSMax Reitz if (detect_zeroes) { 451fbf8175eSMax Reitz *detect_zeroes = 452f7abe0ecSMarc-André Lureau qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, 453fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 454fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 455fbf8175eSMax Reitz &local_error); 456fbf8175eSMax Reitz if (local_error) { 457fbf8175eSMax Reitz error_propagate(errp, local_error); 458fbf8175eSMax Reitz return; 459fbf8175eSMax Reitz } 460fbf8175eSMax Reitz } 461fbf8175eSMax Reitz } 462fbf8175eSMax Reitz 463f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 46418e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 465b681072dSKevin Wolf Error **errp) 466666daa68SMarkus Armbruster { 467666daa68SMarkus Armbruster const char *buf; 468666daa68SMarkus Armbruster int bdrv_flags = 0; 469666daa68SMarkus Armbruster int on_read_error, on_write_error; 470362e9299SAlberto Garcia bool account_invalid, account_failed; 471f87a0e29SAlberto Garcia bool writethrough, read_only; 47226f54e9aSMarkus Armbruster BlockBackend *blk; 473a0f1eab1SMarkus Armbruster BlockDriverState *bs; 474cc0681c4SBenoît Canet ThrottleConfig cfg; 475666daa68SMarkus Armbruster int snapshot = 0; 476c546194fSStefan Hajnoczi Error *error = NULL; 4770006383eSKevin Wolf QemuOpts *opts; 47840119effSAlberto Garcia QDict *interval_dict = NULL; 47940119effSAlberto Garcia QList *interval_list = NULL; 4800006383eSKevin Wolf const char *id; 481fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 482fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 483fbf8175eSMax Reitz const char *throttling_group = NULL; 484666daa68SMarkus Armbruster 485f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 486f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 487f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4880006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 48984d18f06SMarkus Armbruster if (error) { 490b681072dSKevin Wolf error_propagate(errp, error); 4916376f952SMarkus Armbruster goto err_no_opts; 4920006383eSKevin Wolf } 4930006383eSKevin Wolf 4940006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49584d18f06SMarkus Armbruster if (error) { 496b681072dSKevin Wolf error_propagate(errp, error); 497ec9c10d2SStefan Hajnoczi goto early_err; 4980006383eSKevin Wolf } 4990006383eSKevin Wolf 5000006383eSKevin Wolf if (id) { 5010006383eSKevin Wolf qdict_del(bs_opts, "id"); 5020006383eSKevin Wolf } 5030006383eSKevin Wolf 504666daa68SMarkus Armbruster /* extract parameters */ 505666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 506666daa68SMarkus Armbruster 507362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 508362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 509362e9299SAlberto Garcia 510e4b24b49SKevin Wolf writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); 511e4b24b49SKevin Wolf 512ff356ee4SAlberto Garcia id = qemu_opts_id(opts); 513ff356ee4SAlberto Garcia 51440119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51540119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51640119effSAlberto Garcia 51740119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 51840119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 51940119effSAlberto Garcia qdict_first(interval_dict)->key); 52040119effSAlberto Garcia goto early_err; 52140119effSAlberto Garcia } 5222be5506fSAlberto Garcia 523fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 524fbf8175eSMax Reitz &detect_zeroes, &error); 525fbf8175eSMax Reitz if (error) { 526fbf8175eSMax Reitz error_propagate(errp, error); 527ec9c10d2SStefan Hajnoczi goto early_err; 528a9384affSPaolo Bonzini } 529666daa68SMarkus Armbruster 530666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 531c8057f95SPeter Maydell if (is_help_option(buf)) { 532807105a7SMarkus Armbruster error_printf("Supported formats:"); 533666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 534807105a7SMarkus Armbruster error_printf("\n"); 535ec9c10d2SStefan Hajnoczi goto early_err; 536666daa68SMarkus Armbruster } 53774fe54f2SKevin Wolf 538e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 539e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 540ec9c10d2SStefan Hajnoczi goto early_err; 5416db5f5d6SMike Qiu } 54246f5ac20SEric Blake qdict_put_str(bs_opts, "driver", buf); 543666daa68SMarkus Armbruster } 544666daa68SMarkus Armbruster 54592aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 546666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 547b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 54884d18f06SMarkus Armbruster if (error) { 549b681072dSKevin Wolf error_propagate(errp, error); 550ec9c10d2SStefan Hajnoczi goto early_err; 551666daa68SMarkus Armbruster } 552666daa68SMarkus Armbruster } 553666daa68SMarkus Armbruster 55492aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 555666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 556b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 55784d18f06SMarkus Armbruster if (error) { 558b681072dSKevin Wolf error_propagate(errp, error); 559ec9c10d2SStefan Hajnoczi goto early_err; 560666daa68SMarkus Armbruster } 561666daa68SMarkus Armbruster } 562666daa68SMarkus Armbruster 563666daa68SMarkus Armbruster if (snapshot) { 56491a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 565666daa68SMarkus Armbruster } 566666daa68SMarkus Armbruster 567f87a0e29SAlberto Garcia read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false); 568f87a0e29SAlberto Garcia 5695ec18f8cSMax Reitz /* init */ 57039c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5715ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5725ec18f8cSMax Reitz 5736d0eb64dSKevin Wolf blk = blk_new(0, BLK_PERM_ALL); 5745ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5755ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 576f87a0e29SAlberto Garcia blk_rs->read_only = read_only; 5775ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5785ec18f8cSMax Reitz 579cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 5805ec18f8cSMax Reitz } else { 5815ec18f8cSMax Reitz if (file && !*file) { 5825ec18f8cSMax Reitz file = NULL; 5835ec18f8cSMax Reitz } 5845ec18f8cSMax Reitz 58591a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 58691a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 58791a097e7SKevin Wolf * Apply the defaults here instead. */ 58891a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 58991a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 590f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, 591f87a0e29SAlberto Garcia read_only ? "on" : "off"); 59272e775c7SKevin Wolf assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); 59391a097e7SKevin Wolf 59412d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 59512d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 59612d5ee3aSKevin Wolf } 59712d5ee3aSKevin Wolf 598efaa7c4eSMax Reitz blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); 599e4342ce5SMax Reitz if (!blk) { 600e4342ce5SMax Reitz goto err_no_bs_opts; 601e4342ce5SMax Reitz } 602e4342ce5SMax Reitz bs = blk_bs(blk); 6030006383eSKevin Wolf 604e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 605e4342ce5SMax Reitz 6069caa6f3dSPaolo Bonzini block_acct_setup(blk_get_stats(blk), account_invalid, account_failed); 6072be5506fSAlberto Garcia 60840119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6092be5506fSAlberto Garcia blk_unref(blk); 6102be5506fSAlberto Garcia blk = NULL; 6112be5506fSAlberto Garcia goto err_no_bs_opts; 6122be5506fSAlberto Garcia } 6132be5506fSAlberto Garcia } 6145ec18f8cSMax Reitz 6157ca7f0f6SKevin Wolf /* disk I/O throttling */ 6167ca7f0f6SKevin Wolf if (throttle_enabled(&cfg)) { 6177ca7f0f6SKevin Wolf if (!throttling_group) { 618ff356ee4SAlberto Garcia throttling_group = id; 6197ca7f0f6SKevin Wolf } 6207ca7f0f6SKevin Wolf blk_io_limits_enable(blk, throttling_group); 6217ca7f0f6SKevin Wolf blk_set_io_limits(blk, &cfg); 6227ca7f0f6SKevin Wolf } 6237ca7f0f6SKevin Wolf 624e4b24b49SKevin Wolf blk_set_enable_write_cache(blk, !writethrough); 6255ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6260006383eSKevin Wolf 627ff356ee4SAlberto Garcia if (!monitor_add_blk(blk, id, errp)) { 628efaa7c4eSMax Reitz blk_unref(blk); 629efaa7c4eSMax Reitz blk = NULL; 630efaa7c4eSMax Reitz goto err_no_bs_opts; 631efaa7c4eSMax Reitz } 632efaa7c4eSMax Reitz 633e4342ce5SMax Reitz err_no_bs_opts: 6340006383eSKevin Wolf qemu_opts_del(opts); 635cb3e7f08SMarc-André Lureau qobject_unref(interval_dict); 636cb3e7f08SMarc-André Lureau qobject_unref(interval_list); 63718e46a03SMarkus Armbruster return blk; 638a9ae2bffSMarkus Armbruster 639ec9c10d2SStefan Hajnoczi early_err: 640ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 641cb3e7f08SMarc-André Lureau qobject_unref(interval_dict); 642cb3e7f08SMarc-André Lureau qobject_unref(interval_list); 6436376f952SMarkus Armbruster err_no_opts: 644cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 645a9ae2bffSMarkus Armbruster return NULL; 646666daa68SMarkus Armbruster } 647666daa68SMarkus Armbruster 648bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 649bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 650bd745e23SMax Reitz { 651bd745e23SMax Reitz int bdrv_flags = 0; 652bd745e23SMax Reitz 653a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 654a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults. 655a81d6164SKevin Wolf * Apply the defaults here instead. */ 656a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 657a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 658f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off"); 659a81d6164SKevin Wolf 66012d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 66112d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 66212d5ee3aSKevin Wolf } 66312d5ee3aSKevin Wolf 66474e1ae7cSKevin Wolf return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); 665bd745e23SMax Reitz } 666bd745e23SMax Reitz 6679c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void) 6689c4218e9SMax Reitz { 6699c4218e9SMax Reitz BlockDriverState *bs, *next_bs; 6709c4218e9SMax Reitz 6719c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { 6729c4218e9SMax Reitz AioContext *ctx = bdrv_get_aio_context(bs); 6739c4218e9SMax Reitz 6749c4218e9SMax Reitz aio_context_acquire(ctx); 6759c4218e9SMax Reitz bdrv_unref(bs); 6769c4218e9SMax Reitz aio_context_release(ctx); 6779c4218e9SMax Reitz } 6789c4218e9SMax Reitz } 6799c4218e9SMax Reitz 680262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */ 681262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) 682262b4e8fSMax Reitz { 683262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list) 684262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states); 685262b4e8fSMax Reitz } 686262b4e8fSMax Reitz 6875abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 6885abbf0eeSKevin Wolf Error **errp) 68957975222SKevin Wolf { 69057975222SKevin Wolf const char *value; 69157975222SKevin Wolf 69257975222SKevin Wolf value = qemu_opt_get(opts, from); 69357975222SKevin Wolf if (value) { 6945abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 6955abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 6965abbf0eeSKevin Wolf "same time", to, from); 6975abbf0eeSKevin Wolf return; 6985abbf0eeSKevin Wolf } 69920d6cd47SJun Li } 70020d6cd47SJun Li 70120d6cd47SJun Li /* rename all items in opts */ 70220d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 703f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 70457975222SKevin Wolf qemu_opt_unset(opts, from); 70557975222SKevin Wolf } 70657975222SKevin Wolf } 70757975222SKevin Wolf 70833cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 70933cb7dc8SKevin Wolf .name = "drive", 71033cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 71133cb7dc8SKevin Wolf .desc = { 71233cb7dc8SKevin Wolf { 71387a899c5SKevin Wolf .name = "bus", 71487a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 71587a899c5SKevin Wolf .help = "bus number", 71687a899c5SKevin Wolf },{ 71787a899c5SKevin Wolf .name = "unit", 71887a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 71987a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 72087a899c5SKevin Wolf },{ 72187a899c5SKevin Wolf .name = "index", 72287a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 72387a899c5SKevin Wolf .help = "index number", 72487a899c5SKevin Wolf },{ 72533cb7dc8SKevin Wolf .name = "media", 72633cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 72733cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 728593d464bSKevin Wolf },{ 729593d464bSKevin Wolf .name = "if", 730593d464bSKevin Wolf .type = QEMU_OPT_STRING, 731593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 732b41a7338SKevin Wolf },{ 733b41a7338SKevin Wolf .name = "cyls", 734b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 735b41a7338SKevin Wolf .help = "number of cylinders (ide disk geometry)", 736b41a7338SKevin Wolf },{ 737b41a7338SKevin Wolf .name = "heads", 738b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 739b41a7338SKevin Wolf .help = "number of heads (ide disk geometry)", 740b41a7338SKevin Wolf },{ 741b41a7338SKevin Wolf .name = "secs", 742b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 743b41a7338SKevin Wolf .help = "number of sectors (ide disk geometry)", 744b41a7338SKevin Wolf },{ 745b41a7338SKevin Wolf .name = "trans", 746b41a7338SKevin Wolf .type = QEMU_OPT_STRING, 747b41a7338SKevin Wolf .help = "chs translation (auto, lba, none)", 74826929298SKevin Wolf },{ 749394c7d4dSKevin Wolf .name = "addr", 750394c7d4dSKevin Wolf .type = QEMU_OPT_STRING, 751394c7d4dSKevin Wolf .help = "pci address (virtio only)", 752d095b465SMax Reitz },{ 753bcf83158SKevin Wolf .name = "serial", 754bcf83158SKevin Wolf .type = QEMU_OPT_STRING, 755bcf83158SKevin Wolf .help = "disk serial number", 756bcf83158SKevin Wolf },{ 757d095b465SMax Reitz .name = "file", 758d095b465SMax Reitz .type = QEMU_OPT_STRING, 759d095b465SMax Reitz .help = "file name", 76033cb7dc8SKevin Wolf }, 7610ebd24e0SKevin Wolf 7620ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 7630ebd24e0SKevin Wolf { 7644e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 7650ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7660ebd24e0SKevin Wolf .help = "open drive file as read-only", 7670ebd24e0SKevin Wolf },{ 768ee13ed1cSKevin Wolf .name = "rerror", 769ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 770ee13ed1cSKevin Wolf .help = "read error action", 771ee13ed1cSKevin Wolf },{ 772ee13ed1cSKevin Wolf .name = "werror", 773ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 774ee13ed1cSKevin Wolf .help = "write error action", 775ee13ed1cSKevin Wolf },{ 7760ebd24e0SKevin Wolf .name = "copy-on-read", 7770ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7780ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 7790ebd24e0SKevin Wolf }, 7800ebd24e0SKevin Wolf 78133cb7dc8SKevin Wolf { /* end of list */ } 78233cb7dc8SKevin Wolf }, 78333cb7dc8SKevin Wolf }; 78433cb7dc8SKevin Wolf 78560e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 78657975222SKevin Wolf { 78729c4e2b5SKevin Wolf const char *value; 78818e46a03SMarkus Armbruster BlockBackend *blk; 78933cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 790f298d071SKevin Wolf QDict *bs_opts; 79133cb7dc8SKevin Wolf QemuOpts *legacy_opts; 79233cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 793593d464bSKevin Wolf BlockInterfaceType type; 794b41a7338SKevin Wolf int cyls, heads, secs, translation; 79587a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 796394c7d4dSKevin Wolf const char *devaddr; 797ee13ed1cSKevin Wolf const char *werror, *rerror; 798a7fdbcf0SFam Zheng bool read_only = false; 799a7fdbcf0SFam Zheng bool copy_on_read; 800bcf83158SKevin Wolf const char *serial; 801d095b465SMax Reitz const char *filename; 80233cb7dc8SKevin Wolf Error *local_err = NULL; 803247147fbSKevin Wolf int i; 804c616f16eSThomas Huth const char *deprecated[] = { 805c616f16eSThomas Huth "serial", "trans", "secs", "heads", "cyls", "addr" 806c616f16eSThomas Huth }; 80729c4e2b5SKevin Wolf 80857975222SKevin Wolf /* Change legacy command line options into QMP ones */ 809247147fbSKevin Wolf static const struct { 810247147fbSKevin Wolf const char *from; 811247147fbSKevin Wolf const char *to; 812247147fbSKevin Wolf } opt_renames[] = { 813247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 814247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 815247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 81657975222SKevin Wolf 817247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 818247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 819247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 82057975222SKevin Wolf 821247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 822247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 823247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8243e9fab69SBenoît Canet 825247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 826247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 827247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8283e9fab69SBenoît Canet 829247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8302024c1dfSBenoît Canet 83176f4afb4SAlberto Garcia { "group", "throttling.group" }, 83276f4afb4SAlberto Garcia 8334e200cf8SAlberto Garcia { "readonly", BDRV_OPT_READ_ONLY }, 834247147fbSKevin Wolf }; 835247147fbSKevin Wolf 836247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8375abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8385abbf0eeSKevin Wolf &local_err); 8395abbf0eeSKevin Wolf if (local_err) { 840565f65d2SMarkus Armbruster error_report_err(local_err); 8415abbf0eeSKevin Wolf return NULL; 8425abbf0eeSKevin Wolf } 843247147fbSKevin Wolf } 8440f227a94SKevin Wolf 84529c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 84629c4e2b5SKevin Wolf if (value) { 84729c4e2b5SKevin Wolf int flags = 0; 84804feb4a5SKevin Wolf bool writethrough; 84929c4e2b5SKevin Wolf 85004feb4a5SKevin Wolf if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { 85129c4e2b5SKevin Wolf error_report("invalid cache option"); 85229c4e2b5SKevin Wolf return NULL; 85329c4e2b5SKevin Wolf } 85429c4e2b5SKevin Wolf 85529c4e2b5SKevin Wolf /* Specific options take precedence */ 85654861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 85754861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 85804feb4a5SKevin Wolf !writethrough, &error_abort); 85929c4e2b5SKevin Wolf } 86054861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 86154861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 862cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 86329c4e2b5SKevin Wolf } 86454861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 86554861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 866cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 86729c4e2b5SKevin Wolf } 86829c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 86929c4e2b5SKevin Wolf } 87029c4e2b5SKevin Wolf 871f298d071SKevin Wolf /* Get a QDict for processing the options */ 872f298d071SKevin Wolf bs_opts = qdict_new(); 873f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 874f298d071SKevin Wolf 87587ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 87687ea75d5SPeter Crosthwaite &error_abort); 87733cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 87884d18f06SMarkus Armbruster if (local_err) { 879565f65d2SMarkus Armbruster error_report_err(local_err); 88033cb7dc8SKevin Wolf goto fail; 88133cb7dc8SKevin Wolf } 88233cb7dc8SKevin Wolf 883c616f16eSThomas Huth /* Other deprecated options */ 884c616f16eSThomas Huth if (!qtest_enabled()) { 885c616f16eSThomas Huth for (i = 0; i < ARRAY_SIZE(deprecated); i++) { 886c616f16eSThomas Huth if (qemu_opt_get(legacy_opts, deprecated[i]) != NULL) { 887c616f16eSThomas Huth error_report("'%s' is deprecated, please use the corresponding " 888c616f16eSThomas Huth "option of '-device' instead", deprecated[i]); 889c616f16eSThomas Huth } 890c616f16eSThomas Huth } 891c616f16eSThomas Huth } 892c616f16eSThomas Huth 89333cb7dc8SKevin Wolf /* Media type */ 89433cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 89533cb7dc8SKevin Wolf if (value) { 89633cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 89733cb7dc8SKevin Wolf media = MEDIA_DISK; 89833cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 89933cb7dc8SKevin Wolf media = MEDIA_CDROM; 900a7fdbcf0SFam Zheng read_only = true; 90133cb7dc8SKevin Wolf } else { 90233cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 90333cb7dc8SKevin Wolf goto fail; 90433cb7dc8SKevin Wolf } 90533cb7dc8SKevin Wolf } 90633cb7dc8SKevin Wolf 9070ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 9084e200cf8SAlberto Garcia read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false); 9090ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 9100ebd24e0SKevin Wolf 9110ebd24e0SKevin Wolf if (read_only && copy_on_read) { 9123dc6f869SAlistair Francis warn_report("disabling copy-on-read on read-only drive"); 9130ebd24e0SKevin Wolf copy_on_read = false; 9140ebd24e0SKevin Wolf } 9150ebd24e0SKevin Wolf 91646f5ac20SEric Blake qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off"); 91746f5ac20SEric Blake qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off"); 9180ebd24e0SKevin Wolf 919593d464bSKevin Wolf /* Controller type */ 920593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 921593d464bSKevin Wolf if (value) { 922593d464bSKevin Wolf for (type = 0; 923593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 924593d464bSKevin Wolf type++) { 925593d464bSKevin Wolf } 926593d464bSKevin Wolf if (type == IF_COUNT) { 927593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 928593d464bSKevin Wolf goto fail; 929593d464bSKevin Wolf } 930593d464bSKevin Wolf } else { 931593d464bSKevin Wolf type = block_default_type; 932593d464bSKevin Wolf } 933593d464bSKevin Wolf 934b41a7338SKevin Wolf /* Geometry */ 935b41a7338SKevin Wolf cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 936b41a7338SKevin Wolf heads = qemu_opt_get_number(legacy_opts, "heads", 0); 937b41a7338SKevin Wolf secs = qemu_opt_get_number(legacy_opts, "secs", 0); 938b41a7338SKevin Wolf 939b41a7338SKevin Wolf if (cyls || heads || secs) { 940b41a7338SKevin Wolf if (cyls < 1) { 941b41a7338SKevin Wolf error_report("invalid physical cyls number"); 942b41a7338SKevin Wolf goto fail; 943b41a7338SKevin Wolf } 944b41a7338SKevin Wolf if (heads < 1) { 945b41a7338SKevin Wolf error_report("invalid physical heads number"); 946b41a7338SKevin Wolf goto fail; 947b41a7338SKevin Wolf } 948b41a7338SKevin Wolf if (secs < 1) { 949b41a7338SKevin Wolf error_report("invalid physical secs number"); 950b41a7338SKevin Wolf goto fail; 951b41a7338SKevin Wolf } 952b41a7338SKevin Wolf } 953b41a7338SKevin Wolf 954b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 955b41a7338SKevin Wolf value = qemu_opt_get(legacy_opts, "trans"); 956b41a7338SKevin Wolf if (value != NULL) { 957b41a7338SKevin Wolf if (!cyls) { 958b41a7338SKevin Wolf error_report("'%s' trans must be used with cyls, heads and secs", 959b41a7338SKevin Wolf value); 960b41a7338SKevin Wolf goto fail; 961b41a7338SKevin Wolf } 962b41a7338SKevin Wolf if (!strcmp(value, "none")) { 963b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_NONE; 964b41a7338SKevin Wolf } else if (!strcmp(value, "lba")) { 965b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_LBA; 966f31c41ffSPaolo Bonzini } else if (!strcmp(value, "large")) { 967f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_LARGE; 968f31c41ffSPaolo Bonzini } else if (!strcmp(value, "rechs")) { 969f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_RECHS; 970b41a7338SKevin Wolf } else if (!strcmp(value, "auto")) { 971b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 972b41a7338SKevin Wolf } else { 973b41a7338SKevin Wolf error_report("'%s' invalid translation type", value); 974b41a7338SKevin Wolf goto fail; 975b41a7338SKevin Wolf } 976b41a7338SKevin Wolf } 977b41a7338SKevin Wolf 978b41a7338SKevin Wolf if (media == MEDIA_CDROM) { 979b41a7338SKevin Wolf if (cyls || secs || heads) { 980b41a7338SKevin Wolf error_report("CHS can't be set with media=cdrom"); 981b41a7338SKevin Wolf goto fail; 982b41a7338SKevin Wolf } 983b41a7338SKevin Wolf } 984b41a7338SKevin Wolf 98587a899c5SKevin Wolf /* Device address specified by bus/unit or index. 98687a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 98787a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 98887a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 98987a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 99087a899c5SKevin Wolf 99187a899c5SKevin Wolf max_devs = if_max_devs[type]; 99287a899c5SKevin Wolf 99387a899c5SKevin Wolf if (index != -1) { 99487a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 99587a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 99687a899c5SKevin Wolf goto fail; 99787a899c5SKevin Wolf } 99887a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 99987a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 100087a899c5SKevin Wolf } 100187a899c5SKevin Wolf 100287a899c5SKevin Wolf if (unit_id == -1) { 100387a899c5SKevin Wolf unit_id = 0; 100487a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 100587a899c5SKevin Wolf unit_id++; 100687a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 100787a899c5SKevin Wolf unit_id -= max_devs; 100887a899c5SKevin Wolf bus_id++; 100987a899c5SKevin Wolf } 101087a899c5SKevin Wolf } 101187a899c5SKevin Wolf } 101287a899c5SKevin Wolf 101387a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 101487a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 101587a899c5SKevin Wolf goto fail; 101687a899c5SKevin Wolf } 101787a899c5SKevin Wolf 101887a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 101987a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 102087a899c5SKevin Wolf bus_id, unit_id, index); 102187a899c5SKevin Wolf goto fail; 102287a899c5SKevin Wolf } 102387a899c5SKevin Wolf 1024bcf83158SKevin Wolf /* Serial number */ 1025bcf83158SKevin Wolf serial = qemu_opt_get(legacy_opts, "serial"); 1026bcf83158SKevin Wolf 102787a899c5SKevin Wolf /* no id supplied -> create one */ 102887a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 102987a899c5SKevin Wolf char *new_id; 103087a899c5SKevin Wolf const char *mediastr = ""; 103187a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 103287a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 103387a899c5SKevin Wolf } 103487a899c5SKevin Wolf if (max_devs) { 103587a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 103687a899c5SKevin Wolf mediastr, unit_id); 103787a899c5SKevin Wolf } else { 103887a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 103987a899c5SKevin Wolf mediastr, unit_id); 104087a899c5SKevin Wolf } 104146f5ac20SEric Blake qdict_put_str(bs_opts, "id", new_id); 104287a899c5SKevin Wolf g_free(new_id); 104387a899c5SKevin Wolf } 104487a899c5SKevin Wolf 1045394c7d4dSKevin Wolf /* Add virtio block device */ 1046394c7d4dSKevin Wolf devaddr = qemu_opt_get(legacy_opts, "addr"); 1047394c7d4dSKevin Wolf if (devaddr && type != IF_VIRTIO) { 1048394c7d4dSKevin Wolf error_report("addr is not supported by this bus type"); 1049394c7d4dSKevin Wolf goto fail; 1050394c7d4dSKevin Wolf } 1051394c7d4dSKevin Wolf 1052394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 1053394c7d4dSKevin Wolf QemuOpts *devopts; 105487ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 105587ea75d5SPeter Crosthwaite &error_abort); 1056394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 10571f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1058394c7d4dSKevin Wolf } else { 1059f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1060394c7d4dSKevin Wolf } 1061f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1062f43e47dbSMarkus Armbruster &error_abort); 1063394c7d4dSKevin Wolf if (devaddr) { 1064f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1065394c7d4dSKevin Wolf } 1066394c7d4dSKevin Wolf } 1067394c7d4dSKevin Wolf 1068d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 1069d095b465SMax Reitz 1070ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 1071ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 1072ee13ed1cSKevin Wolf if (werror != NULL) { 1073ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1074ee13ed1cSKevin Wolf type != IF_NONE) { 1075ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 1076ee13ed1cSKevin Wolf goto fail; 1077ee13ed1cSKevin Wolf } 107846f5ac20SEric Blake qdict_put_str(bs_opts, "werror", werror); 1079ee13ed1cSKevin Wolf } 1080ee13ed1cSKevin Wolf 1081ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 1082ee13ed1cSKevin Wolf if (rerror != NULL) { 1083ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1084ee13ed1cSKevin Wolf type != IF_NONE) { 1085ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 1086ee13ed1cSKevin Wolf goto fail; 1087ee13ed1cSKevin Wolf } 108846f5ac20SEric Blake qdict_put_str(bs_opts, "rerror", rerror); 1089ee13ed1cSKevin Wolf } 1090ee13ed1cSKevin Wolf 10912d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 109218e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 10933cb0e25cSMarkus Armbruster bs_opts = NULL; 109418e46a03SMarkus Armbruster if (!blk) { 109584d18f06SMarkus Armbruster if (local_err) { 1096565f65d2SMarkus Armbruster error_report_err(local_err); 1097b681072dSKevin Wolf } 10982d246f01SKevin Wolf goto fail; 1099b681072dSKevin Wolf } else { 110084d18f06SMarkus Armbruster assert(!local_err); 11012d246f01SKevin Wolf } 11022d246f01SKevin Wolf 110326f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 110426f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1105f298d071SKevin Wolf dinfo->opts = all_opts; 11062d246f01SKevin Wolf 1107b41a7338SKevin Wolf dinfo->cyls = cyls; 1108b41a7338SKevin Wolf dinfo->heads = heads; 1109b41a7338SKevin Wolf dinfo->secs = secs; 1110b41a7338SKevin Wolf dinfo->trans = translation; 1111b41a7338SKevin Wolf 1112ee13ed1cSKevin Wolf dinfo->type = type; 111387a899c5SKevin Wolf dinfo->bus = bus_id; 111487a899c5SKevin Wolf dinfo->unit = unit_id; 1115394c7d4dSKevin Wolf dinfo->devaddr = devaddr; 1116bcf83158SKevin Wolf dinfo->serial = g_strdup(serial); 1117bcf83158SKevin Wolf 111826f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 111926f8b3a8SMarkus Armbruster 1120e34ef046SKevin Wolf switch(type) { 1121e34ef046SKevin Wolf case IF_IDE: 1122e34ef046SKevin Wolf case IF_SCSI: 1123e34ef046SKevin Wolf case IF_XEN: 1124e34ef046SKevin Wolf case IF_NONE: 1125e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1126e34ef046SKevin Wolf break; 1127e34ef046SKevin Wolf default: 1128e34ef046SKevin Wolf break; 1129e34ef046SKevin Wolf } 1130e34ef046SKevin Wolf 11312d246f01SKevin Wolf fail: 113233cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 1133cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 11342d246f01SKevin Wolf return dinfo; 113557975222SKevin Wolf } 113657975222SKevin Wolf 1137b6c1bae5SKevin Wolf static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) 1138b6c1bae5SKevin Wolf { 1139b6c1bae5SKevin Wolf BlockDriverState *bs; 1140b6c1bae5SKevin Wolf 1141b6c1bae5SKevin Wolf bs = bdrv_lookup_bs(name, name, errp); 1142b6c1bae5SKevin Wolf if (bs == NULL) { 1143b6c1bae5SKevin Wolf return NULL; 1144b6c1bae5SKevin Wolf } 1145b6c1bae5SKevin Wolf 1146b6c1bae5SKevin Wolf if (!bdrv_is_root_node(bs)) { 1147b6c1bae5SKevin Wolf error_setg(errp, "Need a root block node"); 1148b6c1bae5SKevin Wolf return NULL; 1149b6c1bae5SKevin Wolf } 1150b6c1bae5SKevin Wolf 1151b6c1bae5SKevin Wolf if (!bdrv_is_inserted(bs)) { 1152b6c1bae5SKevin Wolf error_setg(errp, "Device has no medium"); 1153b6c1bae5SKevin Wolf return NULL; 1154b6c1bae5SKevin Wolf } 1155b6c1bae5SKevin Wolf 1156b6c1bae5SKevin Wolf return bs; 1157b6c1bae5SKevin Wolf } 1158b6c1bae5SKevin Wolf 1159b33945cfSKevin Wolf static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id, 1160b33945cfSKevin Wolf Error **errp) 1161b33945cfSKevin Wolf { 1162b33945cfSKevin Wolf BlockBackend *blk; 1163b33945cfSKevin Wolf 1164b33945cfSKevin Wolf if (!blk_name == !qdev_id) { 1165b33945cfSKevin Wolf error_setg(errp, "Need exactly one of 'device' and 'id'"); 1166b33945cfSKevin Wolf return NULL; 1167b33945cfSKevin Wolf } 1168b33945cfSKevin Wolf 1169b33945cfSKevin Wolf if (qdev_id) { 1170b33945cfSKevin Wolf blk = blk_by_qdev_id(qdev_id, errp); 1171b33945cfSKevin Wolf } else { 1172b33945cfSKevin Wolf blk = blk_by_name(blk_name); 1173b33945cfSKevin Wolf if (blk == NULL) { 1174b33945cfSKevin Wolf error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1175b33945cfSKevin Wolf "Device '%s' not found", blk_name); 1176b33945cfSKevin Wolf } 1177b33945cfSKevin Wolf } 1178b33945cfSKevin Wolf 1179b33945cfSKevin Wolf return blk; 1180b33945cfSKevin Wolf } 1181b33945cfSKevin Wolf 11823e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1183666daa68SMarkus Armbruster { 1184666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1185a0e8544cSFam Zheng BlockBackend *blk; 11862d3735d3SStefan Hajnoczi int ret; 11872d3735d3SStefan Hajnoczi 1188e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1189da31d594SMax Reitz ret = blk_commit_all(); 1190e8877497SStefan Hajnoczi } else { 119184aa0140SStefan Hajnoczi BlockDriverState *bs; 119284aa0140SStefan Hajnoczi AioContext *aio_context; 119384aa0140SStefan Hajnoczi 1194a0e8544cSFam Zheng blk = blk_by_name(device); 1195a0e8544cSFam Zheng if (!blk) { 119658513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1197ac59eb95SMarkus Armbruster return; 11986ab4b5abSMarkus Armbruster } 11995433c24fSMax Reitz if (!blk_is_available(blk)) { 12005433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 12015433c24fSMax Reitz return; 12025433c24fSMax Reitz } 120384aa0140SStefan Hajnoczi 120484aa0140SStefan Hajnoczi bs = blk_bs(blk); 120584aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 120684aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 120784aa0140SStefan Hajnoczi 120884aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 120984aa0140SStefan Hajnoczi 121084aa0140SStefan Hajnoczi aio_context_release(aio_context); 12112d3735d3SStefan Hajnoczi } 121258513bdeSJeff Cody if (ret < 0) { 121358513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 121458513bdeSJeff Cody strerror(-ret)); 1215666daa68SMarkus Armbruster } 1216666daa68SMarkus Armbruster } 1217666daa68SMarkus Armbruster 121810f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 12196cc2a415SPaolo Bonzini { 1220c8a83e85SKevin Wolf TransactionActionList list; 12216cc2a415SPaolo Bonzini 122210f75907SEric Blake list.value = action; 12236cc2a415SPaolo Bonzini list.next = NULL; 122494d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 12256cc2a415SPaolo Bonzini } 12266cc2a415SPaolo Bonzini 12270901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 12280901f67eSBenoît Canet bool has_node_name, const char *node_name, 12290901f67eSBenoît Canet const char *snapshot_file, 12300901f67eSBenoît Canet bool has_snapshot_node_name, 12310901f67eSBenoît Canet const char *snapshot_node_name, 12326106e249SLuiz Capitulino bool has_format, const char *format, 12330901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1234f8882568SJes Sorensen { 1235a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 12360901f67eSBenoît Canet .has_device = has_device, 12376cc2a415SPaolo Bonzini .device = (char *) device, 12380901f67eSBenoît Canet .has_node_name = has_node_name, 12390901f67eSBenoît Canet .node_name = (char *) node_name, 12406cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 12410901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 12420901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 12436cc2a415SPaolo Bonzini .has_format = has_format, 12446cc2a415SPaolo Bonzini .format = (char *) format, 12456cc2a415SPaolo Bonzini .has_mode = has_mode, 12466cc2a415SPaolo Bonzini .mode = mode, 12476cc2a415SPaolo Bonzini }; 124810f75907SEric Blake TransactionAction action = { 124910f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 125032bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 125110f75907SEric Blake }; 125210f75907SEric Blake blockdev_do_action(&action, errp); 1253f8882568SJes Sorensen } 1254f8882568SJes Sorensen 125543de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 125643de7e2dSAlberto Garcia Error **errp) 125743de7e2dSAlberto Garcia { 125843de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 125943de7e2dSAlberto Garcia .node = (char *) node, 126043de7e2dSAlberto Garcia .overlay = (char *) overlay 126143de7e2dSAlberto Garcia }; 126210f75907SEric Blake TransactionAction action = { 126310f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 126432bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 126510f75907SEric Blake }; 126610f75907SEric Blake blockdev_do_action(&action, errp); 126743de7e2dSAlberto Garcia } 126843de7e2dSAlberto Garcia 1269f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1270f323bc9eSWenchao Xia const char *name, 1271f323bc9eSWenchao Xia Error **errp) 1272f323bc9eSWenchao Xia { 1273f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1274f323bc9eSWenchao Xia .device = (char *) device, 1275f323bc9eSWenchao Xia .name = (char *) name 1276f323bc9eSWenchao Xia }; 127710f75907SEric Blake TransactionAction action = { 127810f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 127932bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 128010f75907SEric Blake }; 128110f75907SEric Blake blockdev_do_action(&action, errp); 1282f323bc9eSWenchao Xia } 1283f323bc9eSWenchao Xia 128444e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 128544e3e053SWenchao Xia bool has_id, 128644e3e053SWenchao Xia const char *id, 128744e3e053SWenchao Xia bool has_name, 128844e3e053SWenchao Xia const char *name, 128944e3e053SWenchao Xia Error **errp) 129044e3e053SWenchao Xia { 1291a0e8544cSFam Zheng BlockDriverState *bs; 12924ef3982aSStefan Hajnoczi AioContext *aio_context; 129344e3e053SWenchao Xia QEMUSnapshotInfo sn; 129444e3e053SWenchao Xia Error *local_err = NULL; 129544e3e053SWenchao Xia SnapshotInfo *info = NULL; 129644e3e053SWenchao Xia int ret; 129744e3e053SWenchao Xia 12982dfb4c03SKevin Wolf bs = qmp_get_root_bs(device, errp); 12992dfb4c03SKevin Wolf if (!bs) { 130044e3e053SWenchao Xia return NULL; 130144e3e053SWenchao Xia } 13022dfb4c03SKevin Wolf aio_context = bdrv_get_aio_context(bs); 13035433c24fSMax Reitz aio_context_acquire(aio_context); 130444e3e053SWenchao Xia 130544e3e053SWenchao Xia if (!has_id) { 130644e3e053SWenchao Xia id = NULL; 130744e3e053SWenchao Xia } 130844e3e053SWenchao Xia 130944e3e053SWenchao Xia if (!has_name) { 131044e3e053SWenchao Xia name = NULL; 131144e3e053SWenchao Xia } 131244e3e053SWenchao Xia 131344e3e053SWenchao Xia if (!id && !name) { 131444e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 13155433c24fSMax Reitz goto out_aio_context; 131644e3e053SWenchao Xia } 131744e3e053SWenchao Xia 13180b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 13190b928854SStefan Hajnoczi goto out_aio_context; 13200b928854SStefan Hajnoczi } 13210b928854SStefan Hajnoczi 132244e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 132384d18f06SMarkus Armbruster if (local_err) { 132444e3e053SWenchao Xia error_propagate(errp, local_err); 13254ef3982aSStefan Hajnoczi goto out_aio_context; 132644e3e053SWenchao Xia } 132744e3e053SWenchao Xia if (!ret) { 132844e3e053SWenchao Xia error_setg(errp, 132944e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 133044e3e053SWenchao Xia "device '%s'", 133144e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 13324ef3982aSStefan Hajnoczi goto out_aio_context; 133344e3e053SWenchao Xia } 133444e3e053SWenchao Xia 133544e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 133684d18f06SMarkus Armbruster if (local_err) { 133744e3e053SWenchao Xia error_propagate(errp, local_err); 13384ef3982aSStefan Hajnoczi goto out_aio_context; 133944e3e053SWenchao Xia } 134044e3e053SWenchao Xia 13414ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13424ef3982aSStefan Hajnoczi 13435839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 134444e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 134544e3e053SWenchao Xia info->name = g_strdup(sn.name); 134644e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 134744e3e053SWenchao Xia info->date_sec = sn.date_sec; 134844e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 134944e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 135044e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 135144e3e053SWenchao Xia 135244e3e053SWenchao Xia return info; 13534ef3982aSStefan Hajnoczi 13544ef3982aSStefan Hajnoczi out_aio_context: 13554ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13564ef3982aSStefan Hajnoczi return NULL; 135744e3e053SWenchao Xia } 13588802d1fdSJeff Cody 1359341ebc2fSJohn Snow /** 1360341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1361341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1362341ebc2fSJohn Snow * the node reference and bitmap names. 1363341ebc2fSJohn Snow * 1364341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1365341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1366341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1367341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1368341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1369341ebc2fSJohn Snow * 1370341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1371341ebc2fSJohn Snow */ 1372341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1373341ebc2fSJohn Snow const char *name, 1374341ebc2fSJohn Snow BlockDriverState **pbs, 1375341ebc2fSJohn Snow Error **errp) 1376341ebc2fSJohn Snow { 1377341ebc2fSJohn Snow BlockDriverState *bs; 1378341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1379341ebc2fSJohn Snow 1380341ebc2fSJohn Snow if (!node) { 1381341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1382341ebc2fSJohn Snow return NULL; 1383341ebc2fSJohn Snow } 1384341ebc2fSJohn Snow if (!name) { 1385341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1386341ebc2fSJohn Snow return NULL; 1387341ebc2fSJohn Snow } 1388341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1389341ebc2fSJohn Snow if (!bs) { 1390341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1391341ebc2fSJohn Snow return NULL; 1392341ebc2fSJohn Snow } 1393341ebc2fSJohn Snow 1394341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1395341ebc2fSJohn Snow if (!bitmap) { 1396341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 13972119882cSPaolo Bonzini return NULL; 1398341ebc2fSJohn Snow } 1399341ebc2fSJohn Snow 1400341ebc2fSJohn Snow if (pbs) { 1401341ebc2fSJohn Snow *pbs = bs; 1402341ebc2fSJohn Snow } 1403341ebc2fSJohn Snow 1404341ebc2fSJohn Snow return bitmap; 1405341ebc2fSJohn Snow } 1406341ebc2fSJohn Snow 1407b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1408ba0c86a3SWenchao Xia 140950f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1410ba0c86a3SWenchao Xia 141150f43f0fSJohn Snow /** 141250f43f0fSJohn Snow * BlkActionOps: 141350f43f0fSJohn Snow * Table of operations that define an Action. 141450f43f0fSJohn Snow * 141550f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 141650f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 141750f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 141850f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 141950f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 142050f43f0fSJohn Snow * commit() or abort(). Can be NULL. 142150f43f0fSJohn Snow * 142250f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 142350f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1424ba0c86a3SWenchao Xia */ 142550f43f0fSJohn Snow typedef struct BlkActionOps { 142650f43f0fSJohn Snow size_t instance_size; 142750f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 142850f43f0fSJohn Snow void (*commit)(BlkActionState *common); 142950f43f0fSJohn Snow void (*abort)(BlkActionState *common); 143050f43f0fSJohn Snow void (*clean)(BlkActionState *common); 143150f43f0fSJohn Snow } BlkActionOps; 143250f43f0fSJohn Snow 143350f43f0fSJohn Snow /** 143450f43f0fSJohn Snow * BlkActionState: 143550f43f0fSJohn Snow * Describes one Action's state within a Transaction. 143650f43f0fSJohn Snow * 143750f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 143850f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 143994d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 144050f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 144150f43f0fSJohn Snow * 144250f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 144350f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 144450f43f0fSJohn Snow * base class. 144550f43f0fSJohn Snow */ 144650f43f0fSJohn Snow struct BlkActionState { 1447c8a83e85SKevin Wolf TransactionAction *action; 144850f43f0fSJohn Snow const BlkActionOps *ops; 144962c9e416SKevin Wolf JobTxn *block_job_txn; 145094d16a64SJohn Snow TransactionProperties *txn_props; 145150f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1452ba0c86a3SWenchao Xia }; 1453ba0c86a3SWenchao Xia 1454bbe86010SWenchao Xia /* internal snapshot private data */ 1455bbe86010SWenchao Xia typedef struct InternalSnapshotState { 145650f43f0fSJohn Snow BlkActionState common; 1457bbe86010SWenchao Xia BlockDriverState *bs; 1458bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1459507306ccSFam Zheng bool created; 1460bbe86010SWenchao Xia } InternalSnapshotState; 1461bbe86010SWenchao Xia 146294d16a64SJohn Snow 146394d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 146494d16a64SJohn Snow { 146594d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 146694d16a64SJohn Snow error_setg(errp, 146794d16a64SJohn Snow "Action '%s' does not support Transaction property " 146894d16a64SJohn Snow "completion-mode = %s", 1469977c736fSMarkus Armbruster TransactionActionKind_str(s->action->type), 1470977c736fSMarkus Armbruster ActionCompletionMode_str(s->txn_props->completion_mode)); 147194d16a64SJohn Snow return -1; 147294d16a64SJohn Snow } 147394d16a64SJohn Snow return 0; 147494d16a64SJohn Snow } 147594d16a64SJohn Snow 147650f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1477bbe86010SWenchao Xia Error **errp) 1478bbe86010SWenchao Xia { 1479f70edf99SMarkus Armbruster Error *local_err = NULL; 1480bbe86010SWenchao Xia const char *device; 1481bbe86010SWenchao Xia const char *name; 1482bbe86010SWenchao Xia BlockDriverState *bs; 1483bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1484bbe86010SWenchao Xia bool ret; 1485bbe86010SWenchao Xia qemu_timeval tv; 1486bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1487bbe86010SWenchao Xia InternalSnapshotState *state; 1488a36e458cSStefan Hajnoczi AioContext *aio_context; 1489bbe86010SWenchao Xia int ret1; 1490bbe86010SWenchao Xia 14916a8f9661SEric Blake g_assert(common->action->type == 1492bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 149332bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1494bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1495bbe86010SWenchao Xia 1496bbe86010SWenchao Xia /* 1. parse input */ 1497bbe86010SWenchao Xia device = internal->device; 1498bbe86010SWenchao Xia name = internal->name; 1499bbe86010SWenchao Xia 1500bbe86010SWenchao Xia /* 2. check for validation */ 150194d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 150294d16a64SJohn Snow return; 150394d16a64SJohn Snow } 150494d16a64SJohn Snow 150575dfd402SKevin Wolf bs = qmp_get_root_bs(device, errp); 150675dfd402SKevin Wolf if (!bs) { 1507bbe86010SWenchao Xia return; 1508bbe86010SWenchao Xia } 1509bbe86010SWenchao Xia 1510a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 1511a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 15125d6e96efSStefan Hajnoczi 1513507306ccSFam Zheng state->bs = bs; 1514a36e458cSStefan Hajnoczi 1515a36e458cSStefan Hajnoczi /* Paired with .clean() */ 1516507306ccSFam Zheng bdrv_drained_begin(bs); 1517507306ccSFam Zheng 15183dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 1519a36e458cSStefan Hajnoczi goto out; 15203dc7ca3cSStefan Hajnoczi } 15213dc7ca3cSStefan Hajnoczi 1522bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 152381e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1524a36e458cSStefan Hajnoczi goto out; 1525bbe86010SWenchao Xia } 1526bbe86010SWenchao Xia 1527bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 152881e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 152981e5f78aSAlberto Garcia "does not support internal snapshots", 153081e5f78aSAlberto Garcia bs->drv->format_name, device); 1531a36e458cSStefan Hajnoczi goto out; 1532bbe86010SWenchao Xia } 1533bbe86010SWenchao Xia 1534bbe86010SWenchao Xia if (!strlen(name)) { 1535bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1536a36e458cSStefan Hajnoczi goto out; 1537bbe86010SWenchao Xia } 1538bbe86010SWenchao Xia 1539bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1540f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1541f70edf99SMarkus Armbruster &local_err); 1542f70edf99SMarkus Armbruster if (local_err) { 1543f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1544a36e458cSStefan Hajnoczi goto out; 1545bbe86010SWenchao Xia } else if (ret) { 1546bbe86010SWenchao Xia error_setg(errp, 1547bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1548bbe86010SWenchao Xia name, device); 1549a36e458cSStefan Hajnoczi goto out; 1550bbe86010SWenchao Xia } 1551bbe86010SWenchao Xia 1552bbe86010SWenchao Xia /* 3. take the snapshot */ 1553bbe86010SWenchao Xia sn = &state->sn; 1554bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1555bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1556bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1557bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1558bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1559bbe86010SWenchao Xia 1560bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1561bbe86010SWenchao Xia if (ret1 < 0) { 1562bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1563bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1564bbe86010SWenchao Xia name, device); 1565a36e458cSStefan Hajnoczi goto out; 1566bbe86010SWenchao Xia } 1567bbe86010SWenchao Xia 1568bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1569507306ccSFam Zheng state->created = true; 1570a36e458cSStefan Hajnoczi 1571a36e458cSStefan Hajnoczi out: 1572a36e458cSStefan Hajnoczi aio_context_release(aio_context); 1573bbe86010SWenchao Xia } 1574bbe86010SWenchao Xia 157550f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1576bbe86010SWenchao Xia { 1577bbe86010SWenchao Xia InternalSnapshotState *state = 1578bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1579bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1580bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1581a36e458cSStefan Hajnoczi AioContext *aio_context; 1582bbe86010SWenchao Xia Error *local_error = NULL; 1583bbe86010SWenchao Xia 1584507306ccSFam Zheng if (!state->created) { 1585bbe86010SWenchao Xia return; 1586bbe86010SWenchao Xia } 1587bbe86010SWenchao Xia 1588a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1589a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 1590a36e458cSStefan Hajnoczi 1591bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1592c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1593c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1594c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1595c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1596c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1597bbe86010SWenchao Xia } 1598a36e458cSStefan Hajnoczi 1599a36e458cSStefan Hajnoczi aio_context_release(aio_context); 1600bbe86010SWenchao Xia } 1601bbe86010SWenchao Xia 160250f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 16035d6e96efSStefan Hajnoczi { 16045d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 16055d6e96efSStefan Hajnoczi common, common); 1606a36e458cSStefan Hajnoczi AioContext *aio_context; 16075d6e96efSStefan Hajnoczi 1608a36e458cSStefan Hajnoczi if (!state->bs) { 1609a36e458cSStefan Hajnoczi return; 1610a36e458cSStefan Hajnoczi } 1611a36e458cSStefan Hajnoczi 1612a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1613a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 1614a36e458cSStefan Hajnoczi 1615507306ccSFam Zheng bdrv_drained_end(state->bs); 1616a36e458cSStefan Hajnoczi 1617a36e458cSStefan Hajnoczi aio_context_release(aio_context); 16185d6e96efSStefan Hajnoczi } 16195d6e96efSStefan Hajnoczi 1620ba0c86a3SWenchao Xia /* external snapshot private data */ 1621ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 162250f43f0fSJohn Snow BlkActionState common; 16238802d1fdSJeff Cody BlockDriverState *old_bs; 16248802d1fdSJeff Cody BlockDriverState *new_bs; 1625067acf28SKevin Wolf bool overlay_appended; 1626ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16278802d1fdSJeff Cody 162850f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16299b9877eeSWenchao Xia Error **errp) 16309b9877eeSWenchao Xia { 16315b363937SMax Reitz int flags = 0; 163243de7e2dSAlberto Garcia QDict *options = NULL; 16339b9877eeSWenchao Xia Error *local_err = NULL; 163443de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1635e2a31e87SWenchao Xia const char *device; 16360901f67eSBenoît Canet const char *node_name; 163743de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 163843de7e2dSAlberto Garcia const char *snapshot_ref; 163943de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1640e2a31e87SWenchao Xia const char *new_image_file; 1641ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1642ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1643c8a83e85SKevin Wolf TransactionAction *action = common->action; 16442d24b60bSStefan Hajnoczi AioContext *aio_context; 16459b9877eeSWenchao Xia 164643de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 164743de7e2dSAlberto Garcia * purpose but a different set of parameters */ 164843de7e2dSAlberto Garcia switch (action->type) { 164943de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 165043de7e2dSAlberto Garcia { 165132bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 165243de7e2dSAlberto Garcia device = s->node; 165343de7e2dSAlberto Garcia node_name = s->node; 165443de7e2dSAlberto Garcia new_image_file = NULL; 165543de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1656e2a31e87SWenchao Xia } 165743de7e2dSAlberto Garcia break; 165843de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 165943de7e2dSAlberto Garcia { 166032bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 166143de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 166243de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 166343de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 166443de7e2dSAlberto Garcia snapshot_ref = NULL; 166543de7e2dSAlberto Garcia } 166643de7e2dSAlberto Garcia break; 166743de7e2dSAlberto Garcia default: 166843de7e2dSAlberto Garcia g_assert_not_reached(); 1669e2a31e87SWenchao Xia } 1670e2a31e87SWenchao Xia 1671e2a31e87SWenchao Xia /* start processing */ 167294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 167394d16a64SJohn Snow return; 167494d16a64SJohn Snow } 167594d16a64SJohn Snow 167643de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 167743de7e2dSAlberto Garcia if (!state->old_bs) { 16789b9877eeSWenchao Xia return; 16799b9877eeSWenchao Xia } 16809b9877eeSWenchao Xia 16812d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 16822d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 16832d24b60bSStefan Hajnoczi 16842d24b60bSStefan Hajnoczi /* Paired with .clean() */ 1685da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 16865d6e96efSStefan Hajnoczi 1687ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1688c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 16892d24b60bSStefan Hajnoczi goto out; 16909b9877eeSWenchao Xia } 16919b9877eeSWenchao Xia 16923718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 16933718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 16942d24b60bSStefan Hajnoczi goto out; 16959b9877eeSWenchao Xia } 16969b9877eeSWenchao Xia 1697ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1698ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1699c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 17002d24b60bSStefan Hajnoczi goto out; 17019b9877eeSWenchao Xia } 17029b9877eeSWenchao Xia } 17039b9877eeSWenchao Xia 1704212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1705c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 17062d24b60bSStefan Hajnoczi goto out; 1707f6186f49SBenoît Canet } 1708f6186f49SBenoît Canet 170943de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 171032bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 171143de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 171243de7e2dSAlberto Garcia enum NewImageMode mode; 171343de7e2dSAlberto Garcia const char *snapshot_node_name = 171443de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 171543de7e2dSAlberto Garcia 171643de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 171743de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 17182d24b60bSStefan Hajnoczi goto out; 171943de7e2dSAlberto Garcia } 172043de7e2dSAlberto Garcia 172143de7e2dSAlberto Garcia if (snapshot_node_name && 172243de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 172343de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 17242d24b60bSStefan Hajnoczi goto out; 172543de7e2dSAlberto Garcia } 172643de7e2dSAlberto Garcia 1727ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17282a32c6e8SJohn Snow flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ); 17292a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING; 17309b9877eeSWenchao Xia 17319b9877eeSWenchao Xia /* create new image w/backing file */ 173243de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17339b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1734f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1735f86b8b58SKevin Wolf if (size < 0) { 1736f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 17372d24b60bSStefan Hajnoczi goto out; 1738f86b8b58SKevin Wolf } 17399b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1740ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1741ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 17429217283dSFam Zheng NULL, size, flags, false, &local_err); 174384d18f06SMarkus Armbruster if (local_err) { 17449b9877eeSWenchao Xia error_propagate(errp, local_err); 17452d24b60bSStefan Hajnoczi goto out; 17469b9877eeSWenchao Xia } 17479b9877eeSWenchao Xia } 17489b9877eeSWenchao Xia 17490901f67eSBenoît Canet options = qdict_new(); 175043de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 175146f5ac20SEric Blake qdict_put_str(options, "node-name", snapshot_node_name); 17520901f67eSBenoît Canet } 175346f5ac20SEric Blake qdict_put_str(options, "driver", format); 175443de7e2dSAlberto Garcia } 175543de7e2dSAlberto Garcia 17565b363937SMax Reitz state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, 17575b363937SMax Reitz errp); 1758f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17595b363937SMax Reitz if (!state->new_bs) { 17602d24b60bSStefan Hajnoczi goto out; 176143de7e2dSAlberto Garcia } 176243de7e2dSAlberto Garcia 17631f0c461bSKevin Wolf if (bdrv_has_blk(state->new_bs)) { 1764e467da7bSKevin Wolf error_setg(errp, "The snapshot is already in use"); 17652d24b60bSStefan Hajnoczi goto out; 176643de7e2dSAlberto Garcia } 176743de7e2dSAlberto Garcia 176843de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 176943de7e2dSAlberto Garcia errp)) { 17702d24b60bSStefan Hajnoczi goto out; 177143de7e2dSAlberto Garcia } 177243de7e2dSAlberto Garcia 177343de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 177443de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 17752d24b60bSStefan Hajnoczi goto out; 177608b24cfeSAlberto Garcia } 177708b24cfeSAlberto Garcia 177808b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 177908b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 17802d24b60bSStefan Hajnoczi goto out; 1781b2c2832cSKevin Wolf } 1782b2c2832cSKevin Wolf 17832d24b60bSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, aio_context); 1784c26a5ab7SFam Zheng 1785b2c2832cSKevin Wolf /* This removes our old bs and adds the new bs. This is an operation that 1786b2c2832cSKevin Wolf * can fail, so we need to do it in .prepare; undoing it for abort is 1787b2c2832cSKevin Wolf * always possible. */ 1788b2c2832cSKevin Wolf bdrv_ref(state->new_bs); 1789b2c2832cSKevin Wolf bdrv_append(state->new_bs, state->old_bs, &local_err); 1790b2c2832cSKevin Wolf if (local_err) { 1791b2c2832cSKevin Wolf error_propagate(errp, local_err); 17922d24b60bSStefan Hajnoczi goto out; 17939b9877eeSWenchao Xia } 1794067acf28SKevin Wolf state->overlay_appended = true; 17952d24b60bSStefan Hajnoczi 17962d24b60bSStefan Hajnoczi out: 17972d24b60bSStefan Hajnoczi aio_context_release(aio_context); 17989b9877eeSWenchao Xia } 17999b9877eeSWenchao Xia 180050f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 18013b0047e8SWenchao Xia { 1802ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1803ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 18042d24b60bSStefan Hajnoczi AioContext *aio_context; 18052d24b60bSStefan Hajnoczi 18062d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 18072d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 1808ba0c86a3SWenchao Xia 18093b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 18103b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 18113b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 1812d3faa13eSPaolo Bonzini if (!atomic_read(&state->old_bs->copy_on_read)) { 1813dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 18143b0047e8SWenchao Xia NULL); 18153b0047e8SWenchao Xia } 18162d24b60bSStefan Hajnoczi 18172d24b60bSStefan Hajnoczi aio_context_release(aio_context); 18184c844983SKevin Wolf } 18193b0047e8SWenchao Xia 182050f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 182196b86bf7SWenchao Xia { 1822ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1823ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1824ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 1825067acf28SKevin Wolf if (state->overlay_appended) { 18262d24b60bSStefan Hajnoczi AioContext *aio_context; 18272d24b60bSStefan Hajnoczi 18282d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 18292d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 18302d24b60bSStefan Hajnoczi 1831719fc28cSJeff Cody bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd() 1832719fc28cSJeff Cody close state->old_bs; we need it */ 1833719fc28cSJeff Cody bdrv_set_backing_hd(state->new_bs, NULL, &error_abort); 18345fe31c25SKevin Wolf bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); 1835719fc28cSJeff Cody bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */ 18362d24b60bSStefan Hajnoczi 18372d24b60bSStefan Hajnoczi aio_context_release(aio_context); 1838b2c2832cSKevin Wolf } 183996b86bf7SWenchao Xia } 1840da763e83SFam Zheng } 1841da763e83SFam Zheng 184250f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1843da763e83SFam Zheng { 1844da763e83SFam Zheng ExternalSnapshotState *state = 1845da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 18462d24b60bSStefan Hajnoczi AioContext *aio_context; 18472d24b60bSStefan Hajnoczi 18482d24b60bSStefan Hajnoczi if (!state->old_bs) { 18492d24b60bSStefan Hajnoczi return; 18502d24b60bSStefan Hajnoczi } 18512d24b60bSStefan Hajnoczi 18522d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 18532d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 18542d24b60bSStefan Hajnoczi 1855da763e83SFam Zheng bdrv_drained_end(state->old_bs); 1856b2c2832cSKevin Wolf bdrv_unref(state->new_bs); 18572d24b60bSStefan Hajnoczi 18582d24b60bSStefan Hajnoczi aio_context_release(aio_context); 185996b86bf7SWenchao Xia } 186096b86bf7SWenchao Xia 18613037f364SStefan Hajnoczi typedef struct DriveBackupState { 186250f43f0fSJohn Snow BlkActionState common; 18633037f364SStefan Hajnoczi BlockDriverState *bs; 18643037f364SStefan Hajnoczi BlockJob *job; 18653037f364SStefan Hajnoczi } DriveBackupState; 18663037f364SStefan Hajnoczi 186762c9e416SKevin Wolf static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn, 186881206a89SPavel Butsykin Error **errp); 186978f51fdeSJohn Snow 187050f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18713037f364SStefan Hajnoczi { 18723037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1873b7e4fa22SKevin Wolf BlockDriverState *bs; 18743037f364SStefan Hajnoczi DriveBackup *backup; 187566d56054SStefan Hajnoczi AioContext *aio_context; 18763037f364SStefan Hajnoczi Error *local_err = NULL; 18773037f364SStefan Hajnoczi 18786a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 187932bafa8fSEric Blake backup = common->action->u.drive_backup.data; 18803037f364SStefan Hajnoczi 1881b7e4fa22SKevin Wolf bs = qmp_get_root_bs(backup->device, errp); 1882b7e4fa22SKevin Wolf if (!bs) { 18831fdd4b7bSFam Zheng return; 18841fdd4b7bSFam Zheng } 18851fdd4b7bSFam Zheng 188666d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 188766d56054SStefan Hajnoczi aio_context_acquire(aio_context); 188866d56054SStefan Hajnoczi 188966d56054SStefan Hajnoczi /* Paired with .clean() */ 1890b7e4fa22SKevin Wolf bdrv_drained_begin(bs); 189166d56054SStefan Hajnoczi 1892b7e4fa22SKevin Wolf state->bs = bs; 18935d6e96efSStefan Hajnoczi 1894111049a4SJohn Snow state->job = do_drive_backup(backup, common->block_job_txn, &local_err); 189584d18f06SMarkus Armbruster if (local_err) { 18963037f364SStefan Hajnoczi error_propagate(errp, local_err); 189766d56054SStefan Hajnoczi goto out; 18983037f364SStefan Hajnoczi } 189966d56054SStefan Hajnoczi 190066d56054SStefan Hajnoczi out: 190166d56054SStefan Hajnoczi aio_context_release(aio_context); 1902111049a4SJohn Snow } 19033037f364SStefan Hajnoczi 1904111049a4SJohn Snow static void drive_backup_commit(BlkActionState *common) 1905111049a4SJohn Snow { 1906111049a4SJohn Snow DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 190766d56054SStefan Hajnoczi AioContext *aio_context; 190866d56054SStefan Hajnoczi 190966d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 191066d56054SStefan Hajnoczi aio_context_acquire(aio_context); 191166d56054SStefan Hajnoczi 1912111049a4SJohn Snow assert(state->job); 1913da01ff7fSKevin Wolf job_start(&state->job->job); 191466d56054SStefan Hajnoczi 191566d56054SStefan Hajnoczi aio_context_release(aio_context); 19163037f364SStefan Hajnoczi } 19173037f364SStefan Hajnoczi 191850f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 19193037f364SStefan Hajnoczi { 19203037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19213037f364SStefan Hajnoczi 1922111049a4SJohn Snow if (state->job) { 192366d56054SStefan Hajnoczi AioContext *aio_context; 192466d56054SStefan Hajnoczi 192566d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 192666d56054SStefan Hajnoczi aio_context_acquire(aio_context); 192766d56054SStefan Hajnoczi 19283d70ff53SKevin Wolf job_cancel_sync(&state->job->job); 192966d56054SStefan Hajnoczi 193066d56054SStefan Hajnoczi aio_context_release(aio_context); 19313037f364SStefan Hajnoczi } 19323037f364SStefan Hajnoczi } 19333037f364SStefan Hajnoczi 193450f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 19355d6e96efSStefan Hajnoczi { 19365d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 193766d56054SStefan Hajnoczi AioContext *aio_context; 19385d6e96efSStefan Hajnoczi 193966d56054SStefan Hajnoczi if (!state->bs) { 194066d56054SStefan Hajnoczi return; 19415d6e96efSStefan Hajnoczi } 194266d56054SStefan Hajnoczi 194366d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 194466d56054SStefan Hajnoczi aio_context_acquire(aio_context); 194566d56054SStefan Hajnoczi 194666d56054SStefan Hajnoczi bdrv_drained_end(state->bs); 194766d56054SStefan Hajnoczi 194866d56054SStefan Hajnoczi aio_context_release(aio_context); 19495d6e96efSStefan Hajnoczi } 19505d6e96efSStefan Hajnoczi 1951bd8baecdSFam Zheng typedef struct BlockdevBackupState { 195250f43f0fSJohn Snow BlkActionState common; 1953bd8baecdSFam Zheng BlockDriverState *bs; 1954bd8baecdSFam Zheng BlockJob *job; 1955bd8baecdSFam Zheng } BlockdevBackupState; 1956bd8baecdSFam Zheng 195762c9e416SKevin Wolf static BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn, 1958dc7a4a9eSPavel Butsykin Error **errp); 195978f51fdeSJohn Snow 196050f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1961bd8baecdSFam Zheng { 1962bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1963bd8baecdSFam Zheng BlockdevBackup *backup; 1964cef34eebSKevin Wolf BlockDriverState *bs, *target; 1965edd5adeeSStefan Hajnoczi AioContext *aio_context; 1966bd8baecdSFam Zheng Error *local_err = NULL; 1967bd8baecdSFam Zheng 19686a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 196932bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1970bd8baecdSFam Zheng 1971cef34eebSKevin Wolf bs = qmp_get_root_bs(backup->device, errp); 1972cef34eebSKevin Wolf if (!bs) { 1973ff52bf36SFam Zheng return; 1974ff52bf36SFam Zheng } 1975ff52bf36SFam Zheng 197639d990acSKevin Wolf target = bdrv_lookup_bs(backup->target, backup->target, errp); 19775433c24fSMax Reitz if (!target) { 1978bd8baecdSFam Zheng return; 1979bd8baecdSFam Zheng } 1980bd8baecdSFam Zheng 1981edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 1982edd5adeeSStefan Hajnoczi if (aio_context != bdrv_get_aio_context(target)) { 1983bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1984bd8baecdSFam Zheng return; 1985bd8baecdSFam Zheng } 1986edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1987cef34eebSKevin Wolf state->bs = bs; 1988edd5adeeSStefan Hajnoczi 1989edd5adeeSStefan Hajnoczi /* Paired with .clean() */ 1990ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1991bd8baecdSFam Zheng 1992111049a4SJohn Snow state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err); 1993bd8baecdSFam Zheng if (local_err) { 1994bd8baecdSFam Zheng error_propagate(errp, local_err); 1995edd5adeeSStefan Hajnoczi goto out; 1996bd8baecdSFam Zheng } 1997edd5adeeSStefan Hajnoczi 1998edd5adeeSStefan Hajnoczi out: 1999edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 2000111049a4SJohn Snow } 2001bd8baecdSFam Zheng 2002111049a4SJohn Snow static void blockdev_backup_commit(BlkActionState *common) 2003111049a4SJohn Snow { 2004111049a4SJohn Snow BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 2005edd5adeeSStefan Hajnoczi AioContext *aio_context; 2006edd5adeeSStefan Hajnoczi 2007edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 2008edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 2009edd5adeeSStefan Hajnoczi 2010111049a4SJohn Snow assert(state->job); 2011da01ff7fSKevin Wolf job_start(&state->job->job); 2012edd5adeeSStefan Hajnoczi 2013edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 2014bd8baecdSFam Zheng } 2015bd8baecdSFam Zheng 201650f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 2017bd8baecdSFam Zheng { 2018bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 2019bd8baecdSFam Zheng 2020111049a4SJohn Snow if (state->job) { 2021edd5adeeSStefan Hajnoczi AioContext *aio_context; 2022edd5adeeSStefan Hajnoczi 2023edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 2024edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 2025edd5adeeSStefan Hajnoczi 20263d70ff53SKevin Wolf job_cancel_sync(&state->job->job); 2027edd5adeeSStefan Hajnoczi 2028edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 2029bd8baecdSFam Zheng } 2030bd8baecdSFam Zheng } 2031bd8baecdSFam Zheng 203250f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 2033bd8baecdSFam Zheng { 2034bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 2035edd5adeeSStefan Hajnoczi AioContext *aio_context; 2036bd8baecdSFam Zheng 2037edd5adeeSStefan Hajnoczi if (!state->bs) { 2038edd5adeeSStefan Hajnoczi return; 2039bd8baecdSFam Zheng } 2040edd5adeeSStefan Hajnoczi 2041edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 2042edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 2043edd5adeeSStefan Hajnoczi 2044edd5adeeSStefan Hajnoczi bdrv_drained_end(state->bs); 2045edd5adeeSStefan Hajnoczi 2046edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 2047bd8baecdSFam Zheng } 2048bd8baecdSFam Zheng 2049df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 205050f43f0fSJohn Snow BlkActionState common; 2051df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 2052df9a681dSFam Zheng BlockDriverState *bs; 2053df9a681dSFam Zheng HBitmap *backup; 2054df9a681dSFam Zheng bool prepared; 2055c6490447SVladimir Sementsov-Ogievskiy bool was_enabled; 2056df9a681dSFam Zheng } BlockDirtyBitmapState; 2057df9a681dSFam Zheng 205850f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 2059df9a681dSFam Zheng Error **errp) 2060df9a681dSFam Zheng { 2061df9a681dSFam Zheng Error *local_err = NULL; 2062df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2063df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2064df9a681dSFam Zheng common, common); 2065df9a681dSFam Zheng 206694d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 206794d16a64SJohn Snow return; 206894d16a64SJohn Snow } 206994d16a64SJohn Snow 207032bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2071df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2072df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2073df9a681dSFam Zheng action->has_granularity, action->granularity, 2074fd5ae4ccSVladimir Sementsov-Ogievskiy action->has_persistent, action->persistent, 2075eb738bb5SVladimir Sementsov-Ogievskiy action->has_autoload, action->autoload, 2076df9a681dSFam Zheng &local_err); 2077df9a681dSFam Zheng 2078df9a681dSFam Zheng if (!local_err) { 2079df9a681dSFam Zheng state->prepared = true; 2080df9a681dSFam Zheng } else { 2081df9a681dSFam Zheng error_propagate(errp, local_err); 2082df9a681dSFam Zheng } 2083df9a681dSFam Zheng } 2084df9a681dSFam Zheng 208550f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2086df9a681dSFam Zheng { 2087df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2088df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2089df9a681dSFam Zheng common, common); 2090df9a681dSFam Zheng 209132bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2092df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2093df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2094df9a681dSFam Zheng */ 2095df9a681dSFam Zheng if (state->prepared) { 2096df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2097df9a681dSFam Zheng } 2098df9a681dSFam Zheng } 2099df9a681dSFam Zheng 210050f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2101df9a681dSFam Zheng Error **errp) 2102df9a681dSFam Zheng { 2103df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2104df9a681dSFam Zheng common, common); 2105df9a681dSFam Zheng BlockDirtyBitmap *action; 2106df9a681dSFam Zheng 210794d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 210894d16a64SJohn Snow return; 210994d16a64SJohn Snow } 211094d16a64SJohn Snow 211132bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2112df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2113df9a681dSFam Zheng action->name, 2114df9a681dSFam Zheng &state->bs, 2115df9a681dSFam Zheng errp); 2116df9a681dSFam Zheng if (!state->bitmap) { 2117df9a681dSFam Zheng return; 2118df9a681dSFam Zheng } 2119df9a681dSFam Zheng 2120df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2121df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2122df9a681dSFam Zheng return; 21234f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(state->bitmap)) { 21244f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, "Cannot modify a locked bitmap"); 21254f43e953SVladimir Sementsov-Ogievskiy return; 2126df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2127df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2128df9a681dSFam Zheng return; 2129d6883bc9SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(state->bitmap)) { 2130d6883bc9SVladimir Sementsov-Ogievskiy error_setg(errp, "Cannot clear a readonly bitmap"); 2131d6883bc9SVladimir Sementsov-Ogievskiy return; 2132df9a681dSFam Zheng } 2133df9a681dSFam Zheng 2134df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2135df9a681dSFam Zheng } 2136df9a681dSFam Zheng 213750f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2138df9a681dSFam Zheng { 2139df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2140df9a681dSFam Zheng common, common); 2141df9a681dSFam Zheng 2142184dd9c4SJohn Snow if (state->backup) { 2143df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2144df9a681dSFam Zheng } 2145184dd9c4SJohn Snow } 2146df9a681dSFam Zheng 214750f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2148df9a681dSFam Zheng { 2149df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2150df9a681dSFam Zheng common, common); 2151df9a681dSFam Zheng 2152df9a681dSFam Zheng hbitmap_free(state->backup); 2153df9a681dSFam Zheng } 2154df9a681dSFam Zheng 2155c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_prepare(BlkActionState *common, 2156c6490447SVladimir Sementsov-Ogievskiy Error **errp) 2157c6490447SVladimir Sementsov-Ogievskiy { 2158c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmap *action; 2159c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2160c6490447SVladimir Sementsov-Ogievskiy common, common); 2161c6490447SVladimir Sementsov-Ogievskiy 2162c6490447SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 2163c6490447SVladimir Sementsov-Ogievskiy return; 2164c6490447SVladimir Sementsov-Ogievskiy } 2165c6490447SVladimir Sementsov-Ogievskiy 2166c6490447SVladimir Sementsov-Ogievskiy action = common->action->u.x_block_dirty_bitmap_enable.data; 2167c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, 2168c6490447SVladimir Sementsov-Ogievskiy action->name, 2169c6490447SVladimir Sementsov-Ogievskiy NULL, 2170c6490447SVladimir Sementsov-Ogievskiy errp); 2171c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) { 2172c6490447SVladimir Sementsov-Ogievskiy return; 2173c6490447SVladimir Sementsov-Ogievskiy } 2174c6490447SVladimir Sementsov-Ogievskiy 2175c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); 2176c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap); 2177c6490447SVladimir Sementsov-Ogievskiy } 2178c6490447SVladimir Sementsov-Ogievskiy 2179c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_abort(BlkActionState *common) 2180c6490447SVladimir Sementsov-Ogievskiy { 2181c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2182c6490447SVladimir Sementsov-Ogievskiy common, common); 2183c6490447SVladimir Sementsov-Ogievskiy 2184c6490447SVladimir Sementsov-Ogievskiy if (!state->was_enabled) { 2185c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap); 2186c6490447SVladimir Sementsov-Ogievskiy } 2187c6490447SVladimir Sementsov-Ogievskiy } 2188c6490447SVladimir Sementsov-Ogievskiy 2189c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_prepare(BlkActionState *common, 2190c6490447SVladimir Sementsov-Ogievskiy Error **errp) 2191c6490447SVladimir Sementsov-Ogievskiy { 2192c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmap *action; 2193c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2194c6490447SVladimir Sementsov-Ogievskiy common, common); 2195c6490447SVladimir Sementsov-Ogievskiy 2196c6490447SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 2197c6490447SVladimir Sementsov-Ogievskiy return; 2198c6490447SVladimir Sementsov-Ogievskiy } 2199c6490447SVladimir Sementsov-Ogievskiy 2200c6490447SVladimir Sementsov-Ogievskiy action = common->action->u.x_block_dirty_bitmap_disable.data; 2201c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, 2202c6490447SVladimir Sementsov-Ogievskiy action->name, 2203c6490447SVladimir Sementsov-Ogievskiy NULL, 2204c6490447SVladimir Sementsov-Ogievskiy errp); 2205c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) { 2206c6490447SVladimir Sementsov-Ogievskiy return; 2207c6490447SVladimir Sementsov-Ogievskiy } 2208c6490447SVladimir Sementsov-Ogievskiy 2209c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); 2210c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap); 2211c6490447SVladimir Sementsov-Ogievskiy } 2212c6490447SVladimir Sementsov-Ogievskiy 2213c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_abort(BlkActionState *common) 2214c6490447SVladimir Sementsov-Ogievskiy { 2215c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2216c6490447SVladimir Sementsov-Ogievskiy common, common); 2217c6490447SVladimir Sementsov-Ogievskiy 2218c6490447SVladimir Sementsov-Ogievskiy if (state->was_enabled) { 2219c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap); 2220c6490447SVladimir Sementsov-Ogievskiy } 2221c6490447SVladimir Sementsov-Ogievskiy } 2222c6490447SVladimir Sementsov-Ogievskiy 222350f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 222478b18b78SStefan Hajnoczi { 222578b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 222678b18b78SStefan Hajnoczi } 222778b18b78SStefan Hajnoczi 222850f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 222978b18b78SStefan Hajnoczi { 2230dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 223178b18b78SStefan Hajnoczi } 223278b18b78SStefan Hajnoczi 223350f43f0fSJohn Snow static const BlkActionOps actions[] = { 223443de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 223543de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 223643de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 223743de7e2dSAlberto Garcia .commit = external_snapshot_commit, 223843de7e2dSAlberto Garcia .abort = external_snapshot_abort, 22394ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 224043de7e2dSAlberto Garcia }, 2241c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2242ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2243ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2244ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2245ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2246da763e83SFam Zheng .clean = external_snapshot_clean, 2247ba0c86a3SWenchao Xia }, 22483037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 22493037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 22503037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 2251111049a4SJohn Snow .commit = drive_backup_commit, 22523037f364SStefan Hajnoczi .abort = drive_backup_abort, 22535d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 22543037f364SStefan Hajnoczi }, 2255bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2256bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2257bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2258111049a4SJohn Snow .commit = blockdev_backup_commit, 2259bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2260bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2261bd8baecdSFam Zheng }, 226278b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 226350f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 226478b18b78SStefan Hajnoczi .prepare = abort_prepare, 226578b18b78SStefan Hajnoczi .commit = abort_commit, 226678b18b78SStefan Hajnoczi }, 2267bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2268bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2269bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2270bbe86010SWenchao Xia .abort = internal_snapshot_abort, 22715d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2272bbe86010SWenchao Xia }, 2273df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2274df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2275df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2276df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2277df9a681dSFam Zheng }, 2278df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2279df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2280df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2281df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2282df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2283c6490447SVladimir Sementsov-Ogievskiy }, 2284c6490447SVladimir Sementsov-Ogievskiy [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_ENABLE] = { 2285c6490447SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 2286c6490447SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_enable_prepare, 2287c6490447SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_enable_abort, 2288c6490447SVladimir Sementsov-Ogievskiy }, 2289c6490447SVladimir Sementsov-Ogievskiy [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_DISABLE] = { 2290c6490447SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 2291c6490447SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_disable_prepare, 2292c6490447SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_disable_abort, 2293df9a681dSFam Zheng } 2294ba0c86a3SWenchao Xia }; 2295ba0c86a3SWenchao Xia 229694d16a64SJohn Snow /** 229794d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 229894d16a64SJohn Snow * that structure with desired defaults if they are unset. 229994d16a64SJohn Snow */ 230094d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 230194d16a64SJohn Snow TransactionProperties *props) 230294d16a64SJohn Snow { 230394d16a64SJohn Snow if (!props) { 230494d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 230594d16a64SJohn Snow } 230694d16a64SJohn Snow 230794d16a64SJohn Snow if (!props->has_completion_mode) { 230894d16a64SJohn Snow props->has_completion_mode = true; 230994d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 231094d16a64SJohn Snow } 231194d16a64SJohn Snow 231294d16a64SJohn Snow return props; 231394d16a64SJohn Snow } 231494d16a64SJohn Snow 23158802d1fdSJeff Cody /* 2316b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2317b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 23188802d1fdSJeff Cody */ 231994d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 232094d16a64SJohn Snow bool has_props, 232194d16a64SJohn Snow struct TransactionProperties *props, 232294d16a64SJohn Snow Error **errp) 23238802d1fdSJeff Cody { 2324c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 232562c9e416SKevin Wolf JobTxn *block_job_txn = NULL; 232650f43f0fSJohn Snow BlkActionState *state, *next; 232743e17041SLuiz Capitulino Error *local_err = NULL; 23288802d1fdSJeff Cody 232950f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 23308802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 23318802d1fdSJeff Cody 233294d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 233362c9e416SKevin Wolf * If not, we don't really need to make a JobTxn. 233494d16a64SJohn Snow */ 233594d16a64SJohn Snow props = get_transaction_properties(props); 233694d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 23377eaa8fb5SKevin Wolf block_job_txn = job_txn_new(); 233894d16a64SJohn Snow } 233994d16a64SJohn Snow 2340b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 23418802d1fdSJeff Cody bdrv_drain_all(); 23428802d1fdSJeff Cody 2343b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 23448802d1fdSJeff Cody while (NULL != dev_entry) { 2345c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 234650f43f0fSJohn Snow const BlkActionOps *ops; 234752e7c241SPaolo Bonzini 23488802d1fdSJeff Cody dev_info = dev_entry->value; 23498802d1fdSJeff Cody dev_entry = dev_entry->next; 23508802d1fdSJeff Cody 23516a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2352ba0c86a3SWenchao Xia 23536a8f9661SEric Blake ops = &actions[dev_info->type]; 2354aa3fe714SMax Reitz assert(ops->instance_size > 0); 2355aa3fe714SMax Reitz 2356ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2357ba5d6ab6SStefan Hajnoczi state->ops = ops; 2358ba5d6ab6SStefan Hajnoczi state->action = dev_info; 235994d16a64SJohn Snow state->block_job_txn = block_job_txn; 236094d16a64SJohn Snow state->txn_props = props; 2361ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 23628802d1fdSJeff Cody 2363ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 236484d18f06SMarkus Armbruster if (local_err) { 23659b9877eeSWenchao Xia error_propagate(errp, local_err); 23669b9877eeSWenchao Xia goto delete_and_fail; 23679b9877eeSWenchao Xia } 236852e7c241SPaolo Bonzini } 23698802d1fdSJeff Cody 2370ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2371f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2372ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 23738802d1fdSJeff Cody } 2374f9ea81e8SStefan Hajnoczi } 23758802d1fdSJeff Cody 23768802d1fdSJeff Cody /* success */ 23778802d1fdSJeff Cody goto exit; 23788802d1fdSJeff Cody 23798802d1fdSJeff Cody delete_and_fail: 2380b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2381ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2382ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2383ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2384ba0c86a3SWenchao Xia } 23858802d1fdSJeff Cody } 23868802d1fdSJeff Cody exit: 2387ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2388ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2389ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2390ba0c86a3SWenchao Xia } 2391ba5d6ab6SStefan Hajnoczi g_free(state); 23928802d1fdSJeff Cody } 239394d16a64SJohn Snow if (!has_props) { 239494d16a64SJohn Snow qapi_free_TransactionProperties(props); 239594d16a64SJohn Snow } 23967eaa8fb5SKevin Wolf job_txn_unref(block_job_txn); 23978802d1fdSJeff Cody } 23988802d1fdSJeff Cody 2399fbe2d816SKevin Wolf void qmp_eject(bool has_device, const char *device, 2400fbe2d816SKevin Wolf bool has_id, const char *id, 2401fbe2d816SKevin Wolf bool has_force, bool force, Error **errp) 2402666daa68SMarkus Armbruster { 240338f54bd1SMax Reitz Error *local_err = NULL; 24043a3086b7SJohn Snow int rc; 2405666daa68SMarkus Armbruster 24063a3086b7SJohn Snow if (!has_force) { 24073a3086b7SJohn Snow force = false; 24083a3086b7SJohn Snow } 24093a3086b7SJohn Snow 2410fbe2d816SKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2411fbe2d816SKevin Wolf has_id ? id : NULL, 2412fbe2d816SKevin Wolf force, &local_err); 2413bf18bee5SColin Lord if (rc && rc != -ENOSYS) { 241438f54bd1SMax Reitz error_propagate(errp, local_err); 2415c245b6a3SLuiz Capitulino return; 2416666daa68SMarkus Armbruster } 2417bf18bee5SColin Lord error_free(local_err); 24183a3086b7SJohn Snow 241982fcf66eSMax Reitz blockdev_remove_medium(has_device, device, has_id, id, errp); 2420666daa68SMarkus Armbruster } 2421666daa68SMarkus Armbruster 242212d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 242312d3ba82SBenoît Canet bool has_node_name, const char *node_name, 242412d3ba82SBenoît Canet const char *password, Error **errp) 2425666daa68SMarkus Armbruster { 2426c01c214bSDaniel P. Berrange error_setg(errp, 2427c01c214bSDaniel P. Berrange "Setting block passwords directly is no longer supported"); 2428e3442099SStefan Hajnoczi } 2429e3442099SStefan Hajnoczi 2430bf18bee5SColin Lord /* 2431bf18bee5SColin Lord * Attempt to open the tray of @device. 2432bf18bee5SColin Lord * If @force, ignore its tray lock. 2433bf18bee5SColin Lord * Else, if the tray is locked, don't open it, but ask the guest to open it. 2434bf18bee5SColin Lord * On error, store an error through @errp and return -errno. 2435bf18bee5SColin Lord * If @device does not exist, return -ENODEV. 2436bf18bee5SColin Lord * If it has no removable media, return -ENOTSUP. 2437bf18bee5SColin Lord * If it has no tray, return -ENOSYS. 2438bf18bee5SColin Lord * If the guest was asked to open the tray, return -EINPROGRESS. 2439bf18bee5SColin Lord * Else, return 0. 24403a3086b7SJohn Snow */ 2441b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id, 2442b33945cfSKevin Wolf bool force, Error **errp) 24437d8a9f71SMax Reitz { 24447d8a9f71SMax Reitz BlockBackend *blk; 2445b33945cfSKevin Wolf const char *device = qdev_id ?: blk_name; 24467d8a9f71SMax Reitz bool locked; 24477d8a9f71SMax Reitz 2448b33945cfSKevin Wolf blk = qmp_get_blk(blk_name, qdev_id, errp); 24497d8a9f71SMax Reitz if (!blk) { 24503a3086b7SJohn Snow return -ENODEV; 24517d8a9f71SMax Reitz } 24527d8a9f71SMax Reitz 24537d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 24547d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 24553a3086b7SJohn Snow return -ENOTSUP; 24567d8a9f71SMax Reitz } 24577d8a9f71SMax Reitz 245812c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 2459bf18bee5SColin Lord error_setg(errp, "Device '%s' does not have a tray", device); 2460bf18bee5SColin Lord return -ENOSYS; 246112c7ec87SMax Reitz } 246212c7ec87SMax Reitz 24637d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 24643a3086b7SJohn Snow return 0; 24657d8a9f71SMax Reitz } 24667d8a9f71SMax Reitz 24677d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 24687d8a9f71SMax Reitz if (locked) { 24697d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 24707d8a9f71SMax Reitz } 24717d8a9f71SMax Reitz 24727d8a9f71SMax Reitz if (!locked || force) { 247339829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 24747d8a9f71SMax Reitz } 24753a3086b7SJohn Snow 24763a3086b7SJohn Snow if (locked && !force) { 2477bf18bee5SColin Lord error_setg(errp, "Device '%s' is locked and force was not specified, " 2478bf18bee5SColin Lord "wait for tray to open and try again", device); 2479bf18bee5SColin Lord return -EINPROGRESS; 24803a3086b7SJohn Snow } 24813a3086b7SJohn Snow 24823a3086b7SJohn Snow return 0; 24833a3086b7SJohn Snow } 24843a3086b7SJohn Snow 2485b33945cfSKevin Wolf void qmp_blockdev_open_tray(bool has_device, const char *device, 2486b33945cfSKevin Wolf bool has_id, const char *id, 2487b33945cfSKevin Wolf bool has_force, bool force, 24883a3086b7SJohn Snow Error **errp) 24893a3086b7SJohn Snow { 2490bf18bee5SColin Lord Error *local_err = NULL; 2491bf18bee5SColin Lord int rc; 2492bf18bee5SColin Lord 24933a3086b7SJohn Snow if (!has_force) { 24943a3086b7SJohn Snow force = false; 24953a3086b7SJohn Snow } 2496b33945cfSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2497b33945cfSKevin Wolf has_id ? id : NULL, 2498b33945cfSKevin Wolf force, &local_err); 2499bf18bee5SColin Lord if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { 2500bf18bee5SColin Lord error_propagate(errp, local_err); 2501bf18bee5SColin Lord return; 2502bf18bee5SColin Lord } 2503bf18bee5SColin Lord error_free(local_err); 25047d8a9f71SMax Reitz } 25057d8a9f71SMax Reitz 2506b33945cfSKevin Wolf void qmp_blockdev_close_tray(bool has_device, const char *device, 2507b33945cfSKevin Wolf bool has_id, const char *id, 2508b33945cfSKevin Wolf Error **errp) 2509abaaf59dSMax Reitz { 2510abaaf59dSMax Reitz BlockBackend *blk; 251139829a01SKevin Wolf Error *local_err = NULL; 2512abaaf59dSMax Reitz 2513b33945cfSKevin Wolf device = has_device ? device : NULL; 2514b33945cfSKevin Wolf id = has_id ? id : NULL; 2515b33945cfSKevin Wolf 2516b33945cfSKevin Wolf blk = qmp_get_blk(device, id, errp); 2517abaaf59dSMax Reitz if (!blk) { 2518abaaf59dSMax Reitz return; 2519abaaf59dSMax Reitz } 2520abaaf59dSMax Reitz 2521abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2522b33945cfSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 2523abaaf59dSMax Reitz return; 2524abaaf59dSMax Reitz } 2525abaaf59dSMax Reitz 252612c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 252712c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 252812c7ec87SMax Reitz return; 252912c7ec87SMax Reitz } 253012c7ec87SMax Reitz 2531abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2532abaaf59dSMax Reitz return; 2533abaaf59dSMax Reitz } 2534abaaf59dSMax Reitz 253539829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 253639829a01SKevin Wolf if (local_err) { 253739829a01SKevin Wolf error_propagate(errp, local_err); 253839829a01SKevin Wolf return; 253939829a01SKevin Wolf } 2540abaaf59dSMax Reitz } 2541abaaf59dSMax Reitz 254282fcf66eSMax Reitz static void blockdev_remove_medium(bool has_device, const char *device, 254300949babSKevin Wolf bool has_id, const char *id, Error **errp) 25442814f672SMax Reitz { 25452814f672SMax Reitz BlockBackend *blk; 25462814f672SMax Reitz BlockDriverState *bs; 25472814f672SMax Reitz AioContext *aio_context; 254800949babSKevin Wolf bool has_attached_device; 25492814f672SMax Reitz 255000949babSKevin Wolf device = has_device ? device : NULL; 255100949babSKevin Wolf id = has_id ? id : NULL; 255200949babSKevin Wolf 255300949babSKevin Wolf blk = qmp_get_blk(device, id, errp); 25542814f672SMax Reitz if (!blk) { 25552814f672SMax Reitz return; 25562814f672SMax Reitz } 25572814f672SMax Reitz 25582814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 255900949babSKevin Wolf has_attached_device = blk_get_attached_dev(blk); 25602814f672SMax Reitz 256100949babSKevin Wolf if (has_attached_device && !blk_dev_has_removable_media(blk)) { 256200949babSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 25632814f672SMax Reitz return; 25642814f672SMax Reitz } 25652814f672SMax Reitz 256600949babSKevin Wolf if (has_attached_device && blk_dev_has_tray(blk) && 256700949babSKevin Wolf !blk_dev_is_tray_open(blk)) 256800949babSKevin Wolf { 256900949babSKevin Wolf error_setg(errp, "Tray of device '%s' is not open", device ?: id); 25702814f672SMax Reitz return; 25712814f672SMax Reitz } 25722814f672SMax Reitz 25732814f672SMax Reitz bs = blk_bs(blk); 25742814f672SMax Reitz if (!bs) { 25752814f672SMax Reitz return; 25762814f672SMax Reitz } 25772814f672SMax Reitz 25782814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 25792814f672SMax Reitz aio_context_acquire(aio_context); 25802814f672SMax Reitz 25812814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 25822814f672SMax Reitz goto out; 25832814f672SMax Reitz } 25842814f672SMax Reitz 25852814f672SMax Reitz blk_remove_bs(blk); 25862814f672SMax Reitz 258712c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 258812c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 258912c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 259012c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 259112c7ec87SMax Reitz * value passed here (i.e. false). */ 259239829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 259312c7ec87SMax Reitz } 259412c7ec87SMax Reitz 25952814f672SMax Reitz out: 25962814f672SMax Reitz aio_context_release(aio_context); 25972814f672SMax Reitz } 25982814f672SMax Reitz 259934ce1111SMax Reitz void qmp_blockdev_remove_medium(const char *id, Error **errp) 260082fcf66eSMax Reitz { 260182fcf66eSMax Reitz blockdev_remove_medium(false, NULL, true, id, errp); 260282fcf66eSMax Reitz } 260382fcf66eSMax Reitz 2604716df217SKevin Wolf static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, 2605d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2606d1299882SMax Reitz { 260739829a01SKevin Wolf Error *local_err = NULL; 2608d1299882SMax Reitz bool has_device; 2609d7086422SKevin Wolf int ret; 2610d1299882SMax Reitz 2611d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2612d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2613d1299882SMax Reitz 2614d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2615716df217SKevin Wolf error_setg(errp, "Device is not removable"); 2616d1299882SMax Reitz return; 2617d1299882SMax Reitz } 2618d1299882SMax Reitz 261912c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2620716df217SKevin Wolf error_setg(errp, "Tray of the device is not open"); 2621d1299882SMax Reitz return; 2622d1299882SMax Reitz } 2623d1299882SMax Reitz 2624d1299882SMax Reitz if (blk_bs(blk)) { 2625716df217SKevin Wolf error_setg(errp, "There already is a medium in the device"); 2626d1299882SMax Reitz return; 2627d1299882SMax Reitz } 2628d1299882SMax Reitz 2629d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 2630d7086422SKevin Wolf if (ret < 0) { 2631d7086422SKevin Wolf return; 2632d7086422SKevin Wolf } 2633d1299882SMax Reitz 263412c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 263512c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 263612c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 263712c7ec87SMax Reitz * slot here. 263812c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 263912c7ec87SMax Reitz * value passed here (i.e. true). */ 264039829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 264139829a01SKevin Wolf if (local_err) { 264239829a01SKevin Wolf error_propagate(errp, local_err); 264339829a01SKevin Wolf blk_remove_bs(blk); 264439829a01SKevin Wolf return; 264539829a01SKevin Wolf } 264612c7ec87SMax Reitz } 2647d1299882SMax Reitz } 2648d1299882SMax Reitz 264982fcf66eSMax Reitz static void blockdev_insert_medium(bool has_device, const char *device, 2650716df217SKevin Wolf bool has_id, const char *id, 2651716df217SKevin Wolf const char *node_name, Error **errp) 2652d1299882SMax Reitz { 2653716df217SKevin Wolf BlockBackend *blk; 2654d1299882SMax Reitz BlockDriverState *bs; 2655d1299882SMax Reitz 2656716df217SKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 2657716df217SKevin Wolf has_id ? id : NULL, 2658716df217SKevin Wolf errp); 2659716df217SKevin Wolf if (!blk) { 2660716df217SKevin Wolf return; 2661716df217SKevin Wolf } 2662716df217SKevin Wolf 2663d1299882SMax Reitz bs = bdrv_find_node(node_name); 2664d1299882SMax Reitz if (!bs) { 2665d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2666d1299882SMax Reitz return; 2667d1299882SMax Reitz } 2668d1299882SMax Reitz 26691f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2670e467da7bSKevin Wolf error_setg(errp, "Node '%s' is already in use", node_name); 2671d1299882SMax Reitz return; 2672d1299882SMax Reitz } 2673d1299882SMax Reitz 2674716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, bs, errp); 2675d1299882SMax Reitz } 2676d1299882SMax Reitz 267734ce1111SMax Reitz void qmp_blockdev_insert_medium(const char *id, const char *node_name, 267882fcf66eSMax Reitz Error **errp) 267982fcf66eSMax Reitz { 268082fcf66eSMax Reitz blockdev_insert_medium(false, NULL, true, id, node_name, errp); 268182fcf66eSMax Reitz } 268282fcf66eSMax Reitz 268370e2cb3bSKevin Wolf void qmp_blockdev_change_medium(bool has_device, const char *device, 268470e2cb3bSKevin Wolf bool has_id, const char *id, 268570e2cb3bSKevin Wolf const char *filename, 268624fb4133SMax Reitz bool has_format, const char *format, 268739ff43d9SMax Reitz bool has_read_only, 268839ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 268924fb4133SMax Reitz Error **errp) 2690de2c6c05SMax Reitz { 2691de2c6c05SMax Reitz BlockBackend *blk; 2692de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 26935b363937SMax Reitz int bdrv_flags; 2694b85114f8SKevin Wolf bool detect_zeroes; 269538a53d50SColin Lord int rc; 2696de2c6c05SMax Reitz QDict *options = NULL; 2697de2c6c05SMax Reitz Error *err = NULL; 2698de2c6c05SMax Reitz 269970e2cb3bSKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 270070e2cb3bSKevin Wolf has_id ? id : NULL, 270170e2cb3bSKevin Wolf errp); 2702de2c6c05SMax Reitz if (!blk) { 2703de2c6c05SMax Reitz goto fail; 2704de2c6c05SMax Reitz } 2705de2c6c05SMax Reitz 2706de2c6c05SMax Reitz if (blk_bs(blk)) { 2707de2c6c05SMax Reitz blk_update_root_state(blk); 2708de2c6c05SMax Reitz } 2709de2c6c05SMax Reitz 2710de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2711156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2712156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2713de2c6c05SMax Reitz 271439ff43d9SMax Reitz if (!has_read_only) { 271539ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 271639ff43d9SMax Reitz } 271739ff43d9SMax Reitz 271839ff43d9SMax Reitz switch (read_only) { 271939ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 272039ff43d9SMax Reitz break; 272139ff43d9SMax Reitz 272239ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 272339ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 272439ff43d9SMax Reitz break; 272539ff43d9SMax Reitz 272639ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 272739ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 272839ff43d9SMax Reitz break; 272939ff43d9SMax Reitz 273039ff43d9SMax Reitz default: 273139ff43d9SMax Reitz abort(); 273239ff43d9SMax Reitz } 273339ff43d9SMax Reitz 2734de2c6c05SMax Reitz options = qdict_new(); 2735b85114f8SKevin Wolf detect_zeroes = blk_get_detect_zeroes_from_root_state(blk); 273646f5ac20SEric Blake qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off"); 2737b85114f8SKevin Wolf 2738b85114f8SKevin Wolf if (has_format) { 273946f5ac20SEric Blake qdict_put_str(options, "driver", format); 2740de2c6c05SMax Reitz } 2741de2c6c05SMax Reitz 27425b363937SMax Reitz medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 27435b363937SMax Reitz if (!medium_bs) { 2744de2c6c05SMax Reitz goto fail; 2745de2c6c05SMax Reitz } 2746de2c6c05SMax Reitz 274770e2cb3bSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 274870e2cb3bSKevin Wolf has_id ? id : NULL, 274970e2cb3bSKevin Wolf false, &err); 275038a53d50SColin Lord if (rc && rc != -ENOSYS) { 2751de2c6c05SMax Reitz error_propagate(errp, err); 2752de2c6c05SMax Reitz goto fail; 2753de2c6c05SMax Reitz } 275438a53d50SColin Lord error_free(err); 275538a53d50SColin Lord err = NULL; 2756de2c6c05SMax Reitz 275782fcf66eSMax Reitz blockdev_remove_medium(has_device, device, has_id, id, &err); 2758de2c6c05SMax Reitz if (err) { 2759de2c6c05SMax Reitz error_propagate(errp, err); 2760de2c6c05SMax Reitz goto fail; 2761de2c6c05SMax Reitz } 2762de2c6c05SMax Reitz 2763716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, medium_bs, &err); 2764de2c6c05SMax Reitz if (err) { 2765de2c6c05SMax Reitz error_propagate(errp, err); 2766de2c6c05SMax Reitz goto fail; 2767de2c6c05SMax Reitz } 2768de2c6c05SMax Reitz 276970e2cb3bSKevin Wolf qmp_blockdev_close_tray(has_device, device, has_id, id, errp); 2770de2c6c05SMax Reitz 2771de2c6c05SMax Reitz fail: 2772de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2773de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2774de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2775de2c6c05SMax Reitz bdrv_unref(medium_bs); 2776de2c6c05SMax Reitz } 2777de2c6c05SMax Reitz 2778727f005eSZhi Yong Wu /* throttling disk I/O limits */ 27794dc9397bSEric Blake void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) 2780727f005eSZhi Yong Wu { 2781cc0681c4SBenoît Canet ThrottleConfig cfg; 2782727f005eSZhi Yong Wu BlockDriverState *bs; 2783a0e8544cSFam Zheng BlockBackend *blk; 2784b15446fdSStefan Hajnoczi AioContext *aio_context; 2785727f005eSZhi Yong Wu 27867a9877a0SKevin Wolf blk = qmp_get_blk(arg->has_device ? arg->device : NULL, 27877a9877a0SKevin Wolf arg->has_id ? arg->id : NULL, 27887a9877a0SKevin Wolf errp); 2789a0e8544cSFam Zheng if (!blk) { 279080047da5SLuiz Capitulino return; 2791727f005eSZhi Yong Wu } 27925433c24fSMax Reitz 27935433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 27945433c24fSMax Reitz aio_context_acquire(aio_context); 27955433c24fSMax Reitz 2796a0e8544cSFam Zheng bs = blk_bs(blk); 27975433c24fSMax Reitz if (!bs) { 27987a9877a0SKevin Wolf error_setg(errp, "Device has no medium"); 27995433c24fSMax Reitz goto out; 28005433c24fSMax Reitz } 2801727f005eSZhi Yong Wu 28021588ab5dSAlberto Garcia throttle_config_init(&cfg); 28034dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; 28044dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; 28054dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; 2806727f005eSZhi Yong Wu 28074dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; 28084dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; 28094dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; 2810cc0681c4SBenoît Canet 28114dc9397bSEric Blake if (arg->has_bps_max) { 28124dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; 28133e9fab69SBenoît Canet } 28144dc9397bSEric Blake if (arg->has_bps_rd_max) { 28154dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; 28163e9fab69SBenoît Canet } 28174dc9397bSEric Blake if (arg->has_bps_wr_max) { 28184dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; 28193e9fab69SBenoît Canet } 28204dc9397bSEric Blake if (arg->has_iops_max) { 28214dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; 28223e9fab69SBenoît Canet } 28234dc9397bSEric Blake if (arg->has_iops_rd_max) { 28244dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; 28253e9fab69SBenoît Canet } 28264dc9397bSEric Blake if (arg->has_iops_wr_max) { 28274dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; 28283e9fab69SBenoît Canet } 2829cc0681c4SBenoît Canet 28304dc9397bSEric Blake if (arg->has_bps_max_length) { 28314dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; 2832dce13204SAlberto Garcia } 28334dc9397bSEric Blake if (arg->has_bps_rd_max_length) { 28344dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; 2835dce13204SAlberto Garcia } 28364dc9397bSEric Blake if (arg->has_bps_wr_max_length) { 28374dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; 2838dce13204SAlberto Garcia } 28394dc9397bSEric Blake if (arg->has_iops_max_length) { 28404dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; 2841dce13204SAlberto Garcia } 28424dc9397bSEric Blake if (arg->has_iops_rd_max_length) { 28434dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; 2844dce13204SAlberto Garcia } 28454dc9397bSEric Blake if (arg->has_iops_wr_max_length) { 28464dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; 2847dce13204SAlberto Garcia } 2848dce13204SAlberto Garcia 28494dc9397bSEric Blake if (arg->has_iops_size) { 28504dc9397bSEric Blake cfg.op_size = arg->iops_size; 28512024c1dfSBenoît Canet } 2852cc0681c4SBenoît Canet 2853d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 28545433c24fSMax Reitz goto out; 2855727f005eSZhi Yong Wu } 2856727f005eSZhi Yong Wu 285776f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 285876f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 285976f4afb4SAlberto Garcia * just update the throttling group. */ 2860022cdc9fSManos Pitsidianakis if (!blk_get_public(blk)->throttle_group_member.throttle_state) { 28614dc9397bSEric Blake blk_io_limits_enable(blk, 28627a9877a0SKevin Wolf arg->has_group ? arg->group : 28637a9877a0SKevin Wolf arg->has_device ? arg->device : 28647a9877a0SKevin Wolf arg->id); 28654dc9397bSEric Blake } else if (arg->has_group) { 28664dc9397bSEric Blake blk_io_limits_update_group(blk, arg->group); 2867727f005eSZhi Yong Wu } 286876f4afb4SAlberto Garcia /* Set the new throttling configuration */ 286997148076SKevin Wolf blk_set_io_limits(blk, &cfg); 2870022cdc9fSManos Pitsidianakis } else if (blk_get_public(blk)->throttle_group_member.throttle_state) { 287176f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 287297148076SKevin Wolf blk_io_limits_disable(blk); 2873727f005eSZhi Yong Wu } 2874b15446fdSStefan Hajnoczi 28755433c24fSMax Reitz out: 2876b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2877727f005eSZhi Yong Wu } 2878727f005eSZhi Yong Wu 2879341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2880341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2881fd5ae4ccSVladimir Sementsov-Ogievskiy bool has_persistent, bool persistent, 2882eb738bb5SVladimir Sementsov-Ogievskiy bool has_autoload, bool autoload, 2883341ebc2fSJohn Snow Error **errp) 2884341ebc2fSJohn Snow { 2885341ebc2fSJohn Snow BlockDriverState *bs; 2886fd5ae4ccSVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 2887341ebc2fSJohn Snow 2888341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2889341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2890341ebc2fSJohn Snow return; 2891341ebc2fSJohn Snow } 2892341ebc2fSJohn Snow 2893341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2894341ebc2fSJohn Snow if (!bs) { 2895341ebc2fSJohn Snow return; 2896341ebc2fSJohn Snow } 2897341ebc2fSJohn Snow 2898341ebc2fSJohn Snow if (has_granularity) { 2899341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2900341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2901341ebc2fSJohn Snow "and at least 512"); 29022119882cSPaolo Bonzini return; 2903341ebc2fSJohn Snow } 2904341ebc2fSJohn Snow } else { 2905341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2906341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2907341ebc2fSJohn Snow } 2908341ebc2fSJohn Snow 2909fd5ae4ccSVladimir Sementsov-Ogievskiy if (!has_persistent) { 2910fd5ae4ccSVladimir Sementsov-Ogievskiy persistent = false; 2911fd5ae4ccSVladimir Sementsov-Ogievskiy } 2912eb738bb5SVladimir Sementsov-Ogievskiy 29133e99da5eSVladimir Sementsov-Ogievskiy if (has_autoload) { 29143e99da5eSVladimir Sementsov-Ogievskiy warn_report("Autoload option is deprecated and its value is ignored"); 2915eb738bb5SVladimir Sementsov-Ogievskiy } 2916fd5ae4ccSVladimir Sementsov-Ogievskiy 2917fd5ae4ccSVladimir Sementsov-Ogievskiy if (persistent && 2918fd5ae4ccSVladimir Sementsov-Ogievskiy !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp)) 2919fd5ae4ccSVladimir Sementsov-Ogievskiy { 2920fd5ae4ccSVladimir Sementsov-Ogievskiy return; 2921fd5ae4ccSVladimir Sementsov-Ogievskiy } 2922fd5ae4ccSVladimir Sementsov-Ogievskiy 2923fd5ae4ccSVladimir Sementsov-Ogievskiy bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2924eb738bb5SVladimir Sementsov-Ogievskiy if (bitmap == NULL) { 2925eb738bb5SVladimir Sementsov-Ogievskiy return; 2926fd5ae4ccSVladimir Sementsov-Ogievskiy } 2927eb738bb5SVladimir Sementsov-Ogievskiy 2928eb738bb5SVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_set_persistance(bitmap, persistent); 2929341ebc2fSJohn Snow } 2930341ebc2fSJohn Snow 2931341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2932341ebc2fSJohn Snow Error **errp) 2933341ebc2fSJohn Snow { 2934341ebc2fSJohn Snow BlockDriverState *bs; 2935341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 29365c36c1afSVladimir Sementsov-Ogievskiy Error *local_err = NULL; 2937341ebc2fSJohn Snow 29382119882cSPaolo Bonzini bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2939341ebc2fSJohn Snow if (!bitmap || !bs) { 2940341ebc2fSJohn Snow return; 2941341ebc2fSJohn Snow } 2942341ebc2fSJohn Snow 29439bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 29449bd2b08fSJohn Snow error_setg(errp, 29459bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 29469bd2b08fSJohn Snow name); 29472119882cSPaolo Bonzini return; 29484f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) { 29494f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 29504f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be removed", 29514f43e953SVladimir Sementsov-Ogievskiy name); 29524f43e953SVladimir Sementsov-Ogievskiy return; 29539bd2b08fSJohn Snow } 29545c36c1afSVladimir Sementsov-Ogievskiy 29555c36c1afSVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_get_persistance(bitmap)) { 29565c36c1afSVladimir Sementsov-Ogievskiy bdrv_remove_persistent_dirty_bitmap(bs, name, &local_err); 29575c36c1afSVladimir Sementsov-Ogievskiy if (local_err != NULL) { 29585c36c1afSVladimir Sementsov-Ogievskiy error_propagate(errp, local_err); 29595c36c1afSVladimir Sementsov-Ogievskiy return; 29605c36c1afSVladimir Sementsov-Ogievskiy } 29615c36c1afSVladimir Sementsov-Ogievskiy } 29625c36c1afSVladimir Sementsov-Ogievskiy 2963341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2964341ebc2fSJohn Snow } 2965341ebc2fSJohn Snow 2966e74e6b78SJohn Snow /** 2967e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2968e74e6b78SJohn Snow * immediately after a full backup operation. 2969e74e6b78SJohn Snow */ 2970e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2971e74e6b78SJohn Snow Error **errp) 2972e74e6b78SJohn Snow { 2973e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2974e74e6b78SJohn Snow BlockDriverState *bs; 2975e74e6b78SJohn Snow 29762119882cSPaolo Bonzini bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2977e74e6b78SJohn Snow if (!bitmap || !bs) { 2978e74e6b78SJohn Snow return; 2979e74e6b78SJohn Snow } 2980e74e6b78SJohn Snow 2981e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2982e74e6b78SJohn Snow error_setg(errp, 2983e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2984e74e6b78SJohn Snow name); 29852119882cSPaolo Bonzini return; 29864f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) { 29874f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 29884f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be modified", 29894f43e953SVladimir Sementsov-Ogievskiy name); 29904f43e953SVladimir Sementsov-Ogievskiy return; 2991e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2992e74e6b78SJohn Snow error_setg(errp, 2993e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2994e74e6b78SJohn Snow name); 29952119882cSPaolo Bonzini return; 2996d6883bc9SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(bitmap)) { 2997d6883bc9SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap '%s' is readonly and cannot be cleared", name); 2998d6883bc9SVladimir Sementsov-Ogievskiy return; 2999e74e6b78SJohn Snow } 3000e74e6b78SJohn Snow 3001df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 3002e74e6b78SJohn Snow } 3003e74e6b78SJohn Snow 30045c5d2e50SVladimir Sementsov-Ogievskiy void qmp_x_block_dirty_bitmap_enable(const char *node, const char *name, 30055c5d2e50SVladimir Sementsov-Ogievskiy Error **errp) 30065c5d2e50SVladimir Sementsov-Ogievskiy { 30075c5d2e50SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 30085c5d2e50SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 30095c5d2e50SVladimir Sementsov-Ogievskiy 30105c5d2e50SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 30115c5d2e50SVladimir Sementsov-Ogievskiy if (!bitmap) { 30125c5d2e50SVladimir Sementsov-Ogievskiy return; 30135c5d2e50SVladimir Sementsov-Ogievskiy } 30145c5d2e50SVladimir Sementsov-Ogievskiy 30155c5d2e50SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_frozen(bitmap)) { 30165c5d2e50SVladimir Sementsov-Ogievskiy error_setg(errp, 30175c5d2e50SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently frozen and cannot be enabled", 30185c5d2e50SVladimir Sementsov-Ogievskiy name); 30195c5d2e50SVladimir Sementsov-Ogievskiy return; 30205c5d2e50SVladimir Sementsov-Ogievskiy } 30215c5d2e50SVladimir Sementsov-Ogievskiy 30225c5d2e50SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(bitmap); 30235c5d2e50SVladimir Sementsov-Ogievskiy } 30245c5d2e50SVladimir Sementsov-Ogievskiy 30255c5d2e50SVladimir Sementsov-Ogievskiy void qmp_x_block_dirty_bitmap_disable(const char *node, const char *name, 30265c5d2e50SVladimir Sementsov-Ogievskiy Error **errp) 30275c5d2e50SVladimir Sementsov-Ogievskiy { 30285c5d2e50SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 30295c5d2e50SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 30305c5d2e50SVladimir Sementsov-Ogievskiy 30315c5d2e50SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 30325c5d2e50SVladimir Sementsov-Ogievskiy if (!bitmap) { 30335c5d2e50SVladimir Sementsov-Ogievskiy return; 30345c5d2e50SVladimir Sementsov-Ogievskiy } 30355c5d2e50SVladimir Sementsov-Ogievskiy 30365c5d2e50SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_frozen(bitmap)) { 30375c5d2e50SVladimir Sementsov-Ogievskiy error_setg(errp, 30385c5d2e50SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently frozen and cannot be disabled", 30395c5d2e50SVladimir Sementsov-Ogievskiy name); 30405c5d2e50SVladimir Sementsov-Ogievskiy return; 30415c5d2e50SVladimir Sementsov-Ogievskiy } 30425c5d2e50SVladimir Sementsov-Ogievskiy 30435c5d2e50SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(bitmap); 30445c5d2e50SVladimir Sementsov-Ogievskiy } 30455c5d2e50SVladimir Sementsov-Ogievskiy 3046*b598e531SVladimir Sementsov-Ogievskiy void qmp_x_block_dirty_bitmap_merge(const char *node, const char *dst_name, 3047*b598e531SVladimir Sementsov-Ogievskiy const char *src_name, Error **errp) 3048*b598e531SVladimir Sementsov-Ogievskiy { 3049*b598e531SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 3050*b598e531SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *dst, *src; 3051*b598e531SVladimir Sementsov-Ogievskiy 3052*b598e531SVladimir Sementsov-Ogievskiy dst = block_dirty_bitmap_lookup(node, dst_name, &bs, errp); 3053*b598e531SVladimir Sementsov-Ogievskiy if (!dst) { 3054*b598e531SVladimir Sementsov-Ogievskiy return; 3055*b598e531SVladimir Sementsov-Ogievskiy } 3056*b598e531SVladimir Sementsov-Ogievskiy 3057*b598e531SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_frozen(dst)) { 3058*b598e531SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap '%s' is frozen and cannot be modified", 3059*b598e531SVladimir Sementsov-Ogievskiy dst_name); 3060*b598e531SVladimir Sementsov-Ogievskiy return; 3061*b598e531SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(dst)) { 3062*b598e531SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap '%s' is readonly and cannot be modified", 3063*b598e531SVladimir Sementsov-Ogievskiy dst_name); 3064*b598e531SVladimir Sementsov-Ogievskiy return; 3065*b598e531SVladimir Sementsov-Ogievskiy } 3066*b598e531SVladimir Sementsov-Ogievskiy 3067*b598e531SVladimir Sementsov-Ogievskiy src = bdrv_find_dirty_bitmap(bs, src_name); 3068*b598e531SVladimir Sementsov-Ogievskiy if (!src) { 3069*b598e531SVladimir Sementsov-Ogievskiy error_setg(errp, "Dirty bitmap '%s' not found", src_name); 3070*b598e531SVladimir Sementsov-Ogievskiy return; 3071*b598e531SVladimir Sementsov-Ogievskiy } 3072*b598e531SVladimir Sementsov-Ogievskiy 3073*b598e531SVladimir Sementsov-Ogievskiy bdrv_merge_dirty_bitmap(dst, src, errp); 3074*b598e531SVladimir Sementsov-Ogievskiy } 3075*b598e531SVladimir Sementsov-Ogievskiy 3076a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, 3077a3b52535SVladimir Sementsov-Ogievskiy const char *name, 3078a3b52535SVladimir Sementsov-Ogievskiy Error **errp) 3079a3b52535SVladimir Sementsov-Ogievskiy { 3080a3b52535SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 3081a3b52535SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 3082a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *ret = NULL; 3083a3b52535SVladimir Sementsov-Ogievskiy char *sha256; 3084a3b52535SVladimir Sementsov-Ogievskiy 3085a3b52535SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 3086a3b52535SVladimir Sementsov-Ogievskiy if (!bitmap || !bs) { 3087a3b52535SVladimir Sementsov-Ogievskiy return NULL; 3088a3b52535SVladimir Sementsov-Ogievskiy } 3089a3b52535SVladimir Sementsov-Ogievskiy 3090a3b52535SVladimir Sementsov-Ogievskiy sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp); 3091a3b52535SVladimir Sementsov-Ogievskiy if (sha256 == NULL) { 3092a3b52535SVladimir Sementsov-Ogievskiy return NULL; 3093a3b52535SVladimir Sementsov-Ogievskiy } 3094a3b52535SVladimir Sementsov-Ogievskiy 3095a3b52535SVladimir Sementsov-Ogievskiy ret = g_new(BlockDirtyBitmapSha256, 1); 3096a3b52535SVladimir Sementsov-Ogievskiy ret->sha256 = sha256; 3097a3b52535SVladimir Sementsov-Ogievskiy 3098a3b52535SVladimir Sementsov-Ogievskiy return ret; 3099a3b52535SVladimir Sementsov-Ogievskiy } 3100a3b52535SVladimir Sementsov-Ogievskiy 3101072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 31029063f814SRyan Harper { 31039063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 31047e7d56d9SMarkus Armbruster BlockBackend *blk; 31059063f814SRyan Harper BlockDriverState *bs; 31068ad4202bSStefan Hajnoczi AioContext *aio_context; 31073718d8abSFam Zheng Error *local_err = NULL; 31089063f814SRyan Harper 31092073d410SKevin Wolf bs = bdrv_find_node(id); 31102073d410SKevin Wolf if (bs) { 311179b7a77eSMarkus Armbruster qmp_blockdev_del(id, &local_err); 31122073d410SKevin Wolf if (local_err) { 31132073d410SKevin Wolf error_report_err(local_err); 31142073d410SKevin Wolf } 31152073d410SKevin Wolf return; 31162073d410SKevin Wolf } 31172073d410SKevin Wolf 31187e7d56d9SMarkus Armbruster blk = blk_by_name(id); 31197e7d56d9SMarkus Armbruster if (!blk) { 3120b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 3121072ebe6bSMarkus Armbruster return; 31229063f814SRyan Harper } 31238ad4202bSStefan Hajnoczi 312426f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 312548f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 312648f364ddSMarkus Armbruster " is not supported"); 3127072ebe6bSMarkus Armbruster return; 312848f364ddSMarkus Armbruster } 312948f364ddSMarkus Armbruster 31305433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 31318ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 31328ad4202bSStefan Hajnoczi 31335433c24fSMax Reitz bs = blk_bs(blk); 31345433c24fSMax Reitz if (bs) { 31353718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 3136565f65d2SMarkus Armbruster error_report_err(local_err); 31378ad4202bSStefan Hajnoczi aio_context_release(aio_context); 3138072ebe6bSMarkus Armbruster return; 31398591675fSMarcelo Tosatti } 31409063f814SRyan Harper 3141938abd43SMax Reitz blk_remove_bs(blk); 31425433c24fSMax Reitz } 31439063f814SRyan Harper 31447c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 3145efaa7c4eSMax Reitz monitor_remove_blk(blk); 3146efaa7c4eSMax Reitz 31477c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 31487c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 3149d22b2f41SRyan Harper */ 3150a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 3151293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 3152373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 3153293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 3154d22b2f41SRyan Harper } else { 3155b9fe8a7aSMarkus Armbruster blk_unref(blk); 3156d22b2f41SRyan Harper } 31579063f814SRyan Harper 31588ad4202bSStefan Hajnoczi aio_context_release(aio_context); 31599063f814SRyan Harper } 31606d4a2b3aSChristoph Hellwig 31613b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 31623b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 31633b1dbd11SBenoît Canet int64_t size, Error **errp) 31646d4a2b3aSChristoph Hellwig { 31653b1dbd11SBenoît Canet Error *local_err = NULL; 31667dad9ee6SKevin Wolf BlockBackend *blk = NULL; 31676d4a2b3aSChristoph Hellwig BlockDriverState *bs; 3168927e0e76SStefan Hajnoczi AioContext *aio_context; 31698732901eSKevin Wolf int ret; 31706d4a2b3aSChristoph Hellwig 31713b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 31723b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 31733b1dbd11SBenoît Canet &local_err); 317484d18f06SMarkus Armbruster if (local_err) { 31753b1dbd11SBenoît Canet error_propagate(errp, local_err); 31763b1dbd11SBenoît Canet return; 31773b1dbd11SBenoît Canet } 31783b1dbd11SBenoît Canet 3179927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 3180927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 3181927e0e76SStefan Hajnoczi 31823b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 3183c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 3184927e0e76SStefan Hajnoczi goto out; 31856d4a2b3aSChristoph Hellwig } 31866d4a2b3aSChristoph Hellwig 31876d4a2b3aSChristoph Hellwig if (size < 0) { 3188c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 3189927e0e76SStefan Hajnoczi goto out; 31906d4a2b3aSChristoph Hellwig } 31916d4a2b3aSChristoph Hellwig 31929c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 3193c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 3194927e0e76SStefan Hajnoczi goto out; 31959c75e168SJeff Cody } 31969c75e168SJeff Cody 31976d0eb64dSKevin Wolf blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL); 3198d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 3199d7086422SKevin Wolf if (ret < 0) { 3200d7086422SKevin Wolf goto out; 3201d7086422SKevin Wolf } 32027dad9ee6SKevin Wolf 3203698bdfa0SJohn Snow bdrv_drained_begin(bs); 32043a691c50SMax Reitz ret = blk_truncate(blk, size, PREALLOC_MODE_OFF, errp); 3205698bdfa0SJohn Snow bdrv_drained_end(bs); 3206927e0e76SStefan Hajnoczi 3207927e0e76SStefan Hajnoczi out: 32087dad9ee6SKevin Wolf blk_unref(blk); 3209927e0e76SStefan Hajnoczi aio_context_release(aio_context); 32106d4a2b3aSChristoph Hellwig } 321112bd451fSStefan Hajnoczi 32122323322eSAlberto Garcia void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, 321313d8cc51SJeff Cody bool has_base, const char *base, 3214312fe09cSAlberto Garcia bool has_base_node, const char *base_node, 321513d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 321613d8cc51SJeff Cody bool has_speed, int64_t speed, 32171d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 32181d809098SPaolo Bonzini Error **errp) 321912bd451fSStefan Hajnoczi { 3220554b6147SAlberto Garcia BlockDriverState *bs, *iter; 3221c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 3222f3e69bebSStefan Hajnoczi AioContext *aio_context; 3223fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 322413d8cc51SJeff Cody const char *base_name = NULL; 322512bd451fSStefan Hajnoczi 32261d809098SPaolo Bonzini if (!has_on_error) { 32271d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 32281d809098SPaolo Bonzini } 32291d809098SPaolo Bonzini 3230554b6147SAlberto Garcia bs = bdrv_lookup_bs(device, device, errp); 3231b6c1bae5SKevin Wolf if (!bs) { 323212bd451fSStefan Hajnoczi return; 323312bd451fSStefan Hajnoczi } 323412bd451fSStefan Hajnoczi 3235b6c1bae5SKevin Wolf aio_context = bdrv_get_aio_context(bs); 3236f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 3237f3e69bebSStefan Hajnoczi 3238312fe09cSAlberto Garcia if (has_base && has_base_node) { 3239312fe09cSAlberto Garcia error_setg(errp, "'base' and 'base-node' cannot be specified " 3240312fe09cSAlberto Garcia "at the same time"); 3241312fe09cSAlberto Garcia goto out; 3242312fe09cSAlberto Garcia } 3243312fe09cSAlberto Garcia 324413d8cc51SJeff Cody if (has_base) { 3245c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3246c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3247c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3248f3e69bebSStefan Hajnoczi goto out; 324912bd451fSStefan Hajnoczi } 3250f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 325113d8cc51SJeff Cody base_name = base; 3252c8c3080fSMarcelo Tosatti } 325312bd451fSStefan Hajnoczi 3254312fe09cSAlberto Garcia if (has_base_node) { 3255312fe09cSAlberto Garcia base_bs = bdrv_lookup_bs(NULL, base_node, errp); 3256312fe09cSAlberto Garcia if (!base_bs) { 3257312fe09cSAlberto Garcia goto out; 3258312fe09cSAlberto Garcia } 3259312fe09cSAlberto Garcia if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { 3260312fe09cSAlberto Garcia error_setg(errp, "Node '%s' is not a backing image of '%s'", 3261312fe09cSAlberto Garcia base_node, device); 3262312fe09cSAlberto Garcia goto out; 3263312fe09cSAlberto Garcia } 3264312fe09cSAlberto Garcia assert(bdrv_get_aio_context(base_bs) == aio_context); 3265312fe09cSAlberto Garcia base_name = base_bs->filename; 3266312fe09cSAlberto Garcia } 3267312fe09cSAlberto Garcia 3268554b6147SAlberto Garcia /* Check for op blockers in the whole chain between bs and base */ 3269554b6147SAlberto Garcia for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) { 3270554b6147SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { 3271554b6147SAlberto Garcia goto out; 3272554b6147SAlberto Garcia } 3273554b6147SAlberto Garcia } 3274554b6147SAlberto Garcia 327513d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 327613d8cc51SJeff Cody * file, and specifying one is therefore an error */ 327713d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 327813d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 327913d8cc51SJeff Cody "entire chain"); 3280f3e69bebSStefan Hajnoczi goto out; 328113d8cc51SJeff Cody } 328213d8cc51SJeff Cody 328313d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 328413d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 328513d8cc51SJeff Cody 32862323322eSAlberto Garcia stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, 32878254b6d9SJohn Snow has_speed ? speed : 0, on_error, &local_err); 328884d18f06SMarkus Armbruster if (local_err) { 3289fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3290f3e69bebSStefan Hajnoczi goto out; 329112bd451fSStefan Hajnoczi } 329212bd451fSStefan Hajnoczi 329312bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3294f3e69bebSStefan Hajnoczi 3295f3e69bebSStefan Hajnoczi out: 3296f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 329712bd451fSStefan Hajnoczi } 32982d47c6e9SStefan Hajnoczi 3299fd62c609SAlberto Garcia void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, 33007676e2c5SJeff Cody bool has_base, const char *base, 33017676e2c5SJeff Cody bool has_top, const char *top, 330254e26900SJeff Cody bool has_backing_file, const char *backing_file, 3303ed61fc10SJeff Cody bool has_speed, int64_t speed, 33040db832f4SKevin Wolf bool has_filter_node_name, const char *filter_node_name, 3305ed61fc10SJeff Cody Error **errp) 3306ed61fc10SJeff Cody { 3307ed61fc10SJeff Cody BlockDriverState *bs; 3308058223a6SAlberto Garcia BlockDriverState *iter; 3309ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 33109e85cd5cSStefan Hajnoczi AioContext *aio_context; 3311ed61fc10SJeff Cody Error *local_err = NULL; 3312ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3313ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3314ed61fc10SJeff Cody */ 331592aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3316ed61fc10SJeff Cody 331754504663SMax Reitz if (!has_speed) { 331854504663SMax Reitz speed = 0; 331954504663SMax Reitz } 33200db832f4SKevin Wolf if (!has_filter_node_name) { 33210db832f4SKevin Wolf filter_node_name = NULL; 33220db832f4SKevin Wolf } 332354504663SMax Reitz 33247676e2c5SJeff Cody /* Important Note: 33257676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 33267676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 33277676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 33287676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 33291d13b167SKevin Wolf bs = qmp_get_root_bs(device, &local_err); 33301d13b167SKevin Wolf if (!bs) { 33311d13b167SKevin Wolf bs = bdrv_lookup_bs(device, device, NULL); 33321d13b167SKevin Wolf if (!bs) { 33331d13b167SKevin Wolf error_free(local_err); 333475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 333575158ebbSMarkus Armbruster "Device '%s' not found", device); 33361d13b167SKevin Wolf } else { 33371d13b167SKevin Wolf error_propagate(errp, local_err); 33381d13b167SKevin Wolf } 3339ed61fc10SJeff Cody return; 3340ed61fc10SJeff Cody } 3341ed61fc10SJeff Cody 33421d13b167SKevin Wolf aio_context = bdrv_get_aio_context(bs); 33439e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 33449e85cd5cSStefan Hajnoczi 3345bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 33469e85cd5cSStefan Hajnoczi goto out; 3347628ff683SFam Zheng } 3348628ff683SFam Zheng 3349ed61fc10SJeff Cody /* default top_bs is the active layer */ 3350ed61fc10SJeff Cody top_bs = bs; 3351ed61fc10SJeff Cody 33527676e2c5SJeff Cody if (has_top && top) { 3353ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3354ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3355ed61fc10SJeff Cody } 3356ed61fc10SJeff Cody } 3357ed61fc10SJeff Cody 3358ed61fc10SJeff Cody if (top_bs == NULL) { 3359ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 33609e85cd5cSStefan Hajnoczi goto out; 3361ed61fc10SJeff Cody } 3362ed61fc10SJeff Cody 33639e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 33649e85cd5cSStefan Hajnoczi 3365d5208c45SJeff Cody if (has_base && base) { 3366d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3367d5208c45SJeff Cody } else { 3368d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3369d5208c45SJeff Cody } 3370d5208c45SJeff Cody 3371d5208c45SJeff Cody if (base_bs == NULL) { 3372c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 33739e85cd5cSStefan Hajnoczi goto out; 3374d5208c45SJeff Cody } 3375d5208c45SJeff Cody 33769e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 33779e85cd5cSStefan Hajnoczi 3378058223a6SAlberto Garcia for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) { 3379058223a6SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3380bb00021dSFam Zheng goto out; 3381bb00021dSFam Zheng } 3382058223a6SAlberto Garcia } 3383bb00021dSFam Zheng 33847676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 33857676e2c5SJeff Cody if (top_bs == base_bs) { 33867676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 33879e85cd5cSStefan Hajnoczi goto out; 33887676e2c5SJeff Cody } 33897676e2c5SJeff Cody 339020a63d2cSFam Zheng if (top_bs == bs) { 339154e26900SJeff Cody if (has_backing_file) { 339254e26900SJeff Cody error_setg(errp, "'backing-file' specified," 339354e26900SJeff Cody " but 'top' is the active layer"); 33949e85cd5cSStefan Hajnoczi goto out; 339554e26900SJeff Cody } 339647970dfbSJohn Snow commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, 3397bb02b65cSKevin Wolf JOB_DEFAULT, speed, on_error, 339878bbd910SFam Zheng filter_node_name, NULL, NULL, false, &local_err); 339920a63d2cSFam Zheng } else { 3400058223a6SAlberto Garcia BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); 3401058223a6SAlberto Garcia if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3402058223a6SAlberto Garcia goto out; 3403058223a6SAlberto Garcia } 3404fd62c609SAlberto Garcia commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed, 34058254b6d9SJohn Snow on_error, has_backing_file ? backing_file : NULL, 34060db832f4SKevin Wolf filter_node_name, &local_err); 340720a63d2cSFam Zheng } 3408ed61fc10SJeff Cody if (local_err != NULL) { 3409ed61fc10SJeff Cody error_propagate(errp, local_err); 34109e85cd5cSStefan Hajnoczi goto out; 3411ed61fc10SJeff Cody } 34129e85cd5cSStefan Hajnoczi 34139e85cd5cSStefan Hajnoczi out: 34149e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3415ed61fc10SJeff Cody } 3416ed61fc10SJeff Cody 341762c9e416SKevin Wolf static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn, 3418111049a4SJohn Snow Error **errp) 341999a9addfSStefan Hajnoczi { 342099a9addfSStefan Hajnoczi BlockDriverState *bs; 342199a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3422fc5d3f84SIan Main BlockDriverState *source = NULL; 3423111049a4SJohn Snow BlockJob *job = NULL; 3424d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3425761731b1SStefan Hajnoczi AioContext *aio_context; 3426e6641719SMax Reitz QDict *options = NULL; 342799a9addfSStefan Hajnoczi Error *local_err = NULL; 3428bb02b65cSKevin Wolf int flags, job_flags = JOB_DEFAULT; 342999a9addfSStefan Hajnoczi int64_t size; 3430fc0932fdSFam Zheng bool set_backing_hd = false; 343199a9addfSStefan Hajnoczi 343281206a89SPavel Butsykin if (!backup->has_speed) { 343381206a89SPavel Butsykin backup->speed = 0; 343499a9addfSStefan Hajnoczi } 343581206a89SPavel Butsykin if (!backup->has_on_source_error) { 343681206a89SPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 343799a9addfSStefan Hajnoczi } 343881206a89SPavel Butsykin if (!backup->has_on_target_error) { 343981206a89SPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 344099a9addfSStefan Hajnoczi } 344181206a89SPavel Butsykin if (!backup->has_mode) { 344281206a89SPavel Butsykin backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 344381206a89SPavel Butsykin } 344481206a89SPavel Butsykin if (!backup->has_job_id) { 344581206a89SPavel Butsykin backup->job_id = NULL; 344699a9addfSStefan Hajnoczi } 3447b40dacdcSJohn Snow if (!backup->has_auto_finalize) { 3448b40dacdcSJohn Snow backup->auto_finalize = true; 3449b40dacdcSJohn Snow } 3450b40dacdcSJohn Snow if (!backup->has_auto_dismiss) { 3451b40dacdcSJohn Snow backup->auto_dismiss = true; 3452b40dacdcSJohn Snow } 345313b9414bSPavel Butsykin if (!backup->has_compress) { 345413b9414bSPavel Butsykin backup->compress = false; 345513b9414bSPavel Butsykin } 345699a9addfSStefan Hajnoczi 345781206a89SPavel Butsykin bs = qmp_get_root_bs(backup->device, errp); 3458b7e4fa22SKevin Wolf if (!bs) { 3459111049a4SJohn Snow return NULL; 346099a9addfSStefan Hajnoczi } 346199a9addfSStefan Hajnoczi 3462b7e4fa22SKevin Wolf aio_context = bdrv_get_aio_context(bs); 3463761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3464761731b1SStefan Hajnoczi 346581206a89SPavel Butsykin if (!backup->has_format) { 346681206a89SPavel Butsykin backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? 346781206a89SPavel Butsykin NULL : (char*) bs->drv->format_name; 346899a9addfSStefan Hajnoczi } 346999a9addfSStefan Hajnoczi 3470c29c1dd3SFam Zheng /* Early check to avoid creating target */ 34713718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3472761731b1SStefan Hajnoczi goto out; 347399a9addfSStefan Hajnoczi } 347499a9addfSStefan Hajnoczi 347561de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 347699a9addfSStefan Hajnoczi 3477fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3478fc5d3f84SIan Main * on top of. */ 347981206a89SPavel Butsykin if (backup->sync == MIRROR_SYNC_MODE_TOP) { 3480760e0063SKevin Wolf source = backing_bs(bs); 3481fc5d3f84SIan Main if (!source) { 348281206a89SPavel Butsykin backup->sync = MIRROR_SYNC_MODE_FULL; 3483fc5d3f84SIan Main } 3484fc5d3f84SIan Main } 348581206a89SPavel Butsykin if (backup->sync == MIRROR_SYNC_MODE_NONE) { 3486fc5d3f84SIan Main source = bs; 3487fc0932fdSFam Zheng flags |= BDRV_O_NO_BACKING; 3488fc0932fdSFam Zheng set_backing_hd = true; 3489fc5d3f84SIan Main } 3490fc5d3f84SIan Main 349199a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 349299a9addfSStefan Hajnoczi if (size < 0) { 349399a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3494761731b1SStefan Hajnoczi goto out; 349599a9addfSStefan Hajnoczi } 349699a9addfSStefan Hajnoczi 349781206a89SPavel Butsykin if (backup->mode != NEW_IMAGE_MODE_EXISTING) { 349881206a89SPavel Butsykin assert(backup->format); 3499fc5d3f84SIan Main if (source) { 350081206a89SPavel Butsykin bdrv_img_create(backup->target, backup->format, source->filename, 3501fc5d3f84SIan Main source->drv->format_name, NULL, 35029217283dSFam Zheng size, flags, false, &local_err); 3503fc5d3f84SIan Main } else { 350481206a89SPavel Butsykin bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, 35059217283dSFam Zheng size, flags, false, &local_err); 3506fc5d3f84SIan Main } 350799a9addfSStefan Hajnoczi } 350899a9addfSStefan Hajnoczi 350984d18f06SMarkus Armbruster if (local_err) { 351099a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3511761731b1SStefan Hajnoczi goto out; 351299a9addfSStefan Hajnoczi } 351399a9addfSStefan Hajnoczi 351481206a89SPavel Butsykin if (backup->format) { 3515fc0932fdSFam Zheng if (!options) { 3516e6641719SMax Reitz options = qdict_new(); 3517fc0932fdSFam Zheng } 351846f5ac20SEric Blake qdict_put_str(options, "driver", backup->format); 3519e6641719SMax Reitz } 3520e6641719SMax Reitz 352181206a89SPavel Butsykin target_bs = bdrv_open(backup->target, NULL, options, flags, errp); 35225b363937SMax Reitz if (!target_bs) { 3523761731b1SStefan Hajnoczi goto out; 352499a9addfSStefan Hajnoczi } 352599a9addfSStefan Hajnoczi 3526761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3527761731b1SStefan Hajnoczi 3528fc0932fdSFam Zheng if (set_backing_hd) { 3529fc0932fdSFam Zheng bdrv_set_backing_hd(target_bs, source, &local_err); 3530fc0932fdSFam Zheng if (local_err) { 3531fc0932fdSFam Zheng bdrv_unref(target_bs); 3532fc0932fdSFam Zheng goto out; 3533fc0932fdSFam Zheng } 3534fc0932fdSFam Zheng } 3535fc0932fdSFam Zheng 353681206a89SPavel Butsykin if (backup->has_bitmap) { 353781206a89SPavel Butsykin bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); 3538d58d8453SJohn Snow if (!bmap) { 353981206a89SPavel Butsykin error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); 35400702d3d8SMax Reitz bdrv_unref(target_bs); 3541d58d8453SJohn Snow goto out; 3542d58d8453SJohn Snow } 35434f43e953SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_qmp_locked(bmap)) { 35444f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 35454f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be used for " 35464f43e953SVladimir Sementsov-Ogievskiy "backup", backup->bitmap); 35474f43e953SVladimir Sementsov-Ogievskiy goto out; 35484f43e953SVladimir Sementsov-Ogievskiy } 3549d58d8453SJohn Snow } 3550b40dacdcSJohn Snow if (!backup->auto_finalize) { 3551bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_FINALIZE; 3552b40dacdcSJohn Snow } 3553b40dacdcSJohn Snow if (!backup->auto_dismiss) { 3554bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_DISMISS; 3555b40dacdcSJohn Snow } 3556d58d8453SJohn Snow 3557111049a4SJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3558111049a4SJohn Snow backup->sync, bmap, backup->compress, 3559111049a4SJohn Snow backup->on_source_error, backup->on_target_error, 3560b40dacdcSJohn Snow job_flags, NULL, NULL, txn, &local_err); 35614f6fd349SFam Zheng bdrv_unref(target_bs); 35625c438bc6SKevin Wolf if (local_err != NULL) { 356399a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3564761731b1SStefan Hajnoczi goto out; 356599a9addfSStefan Hajnoczi } 3566761731b1SStefan Hajnoczi 3567761731b1SStefan Hajnoczi out: 3568761731b1SStefan Hajnoczi aio_context_release(aio_context); 3569111049a4SJohn Snow return job; 357099a9addfSStefan Hajnoczi } 357199a9addfSStefan Hajnoczi 357281206a89SPavel Butsykin void qmp_drive_backup(DriveBackup *arg, Error **errp) 357378f51fdeSJohn Snow { 3574111049a4SJohn Snow 3575111049a4SJohn Snow BlockJob *job; 3576111049a4SJohn Snow job = do_drive_backup(arg, NULL, errp); 3577111049a4SJohn Snow if (job) { 3578da01ff7fSKevin Wolf job_start(&job->job); 3579111049a4SJohn Snow } 358078f51fdeSJohn Snow } 358178f51fdeSJohn Snow 3582c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3583c13163fbSBenoît Canet { 3584d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3585c13163fbSBenoît Canet } 3586c13163fbSBenoît Canet 358762c9e416SKevin Wolf BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn, 3588111049a4SJohn Snow Error **errp) 3589c29c1dd3SFam Zheng { 3590c29c1dd3SFam Zheng BlockDriverState *bs; 3591c29c1dd3SFam Zheng BlockDriverState *target_bs; 3592c29c1dd3SFam Zheng Error *local_err = NULL; 3593c29c1dd3SFam Zheng AioContext *aio_context; 3594111049a4SJohn Snow BlockJob *job = NULL; 3595bb02b65cSKevin Wolf int job_flags = JOB_DEFAULT; 3596c29c1dd3SFam Zheng 3597dc7a4a9eSPavel Butsykin if (!backup->has_speed) { 3598dc7a4a9eSPavel Butsykin backup->speed = 0; 3599c29c1dd3SFam Zheng } 3600dc7a4a9eSPavel Butsykin if (!backup->has_on_source_error) { 3601dc7a4a9eSPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3602c29c1dd3SFam Zheng } 3603dc7a4a9eSPavel Butsykin if (!backup->has_on_target_error) { 3604dc7a4a9eSPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3605dc7a4a9eSPavel Butsykin } 3606dc7a4a9eSPavel Butsykin if (!backup->has_job_id) { 3607dc7a4a9eSPavel Butsykin backup->job_id = NULL; 3608c29c1dd3SFam Zheng } 3609b40dacdcSJohn Snow if (!backup->has_auto_finalize) { 3610b40dacdcSJohn Snow backup->auto_finalize = true; 3611b40dacdcSJohn Snow } 3612b40dacdcSJohn Snow if (!backup->has_auto_dismiss) { 3613b40dacdcSJohn Snow backup->auto_dismiss = true; 3614b40dacdcSJohn Snow } 36153b7b1236SPavel Butsykin if (!backup->has_compress) { 36163b7b1236SPavel Butsykin backup->compress = false; 36173b7b1236SPavel Butsykin } 3618c29c1dd3SFam Zheng 3619dc7a4a9eSPavel Butsykin bs = qmp_get_root_bs(backup->device, errp); 3620cef34eebSKevin Wolf if (!bs) { 3621111049a4SJohn Snow return NULL; 3622c29c1dd3SFam Zheng } 3623c29c1dd3SFam Zheng 3624cef34eebSKevin Wolf aio_context = bdrv_get_aio_context(bs); 3625c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3626c29c1dd3SFam Zheng 3627dc7a4a9eSPavel Butsykin target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); 36280d978913SFam Zheng if (!target_bs) { 3629c29c1dd3SFam Zheng goto out; 3630c29c1dd3SFam Zheng } 36315433c24fSMax Reitz 3632efd75567SFam Zheng if (bdrv_get_aio_context(target_bs) != aio_context) { 3633efd75567SFam Zheng if (!bdrv_has_blk(target_bs)) { 3634efd75567SFam Zheng /* The target BDS is not attached, we can safely move it to another 3635efd75567SFam Zheng * AioContext. */ 3636c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3637efd75567SFam Zheng } else { 3638efd75567SFam Zheng error_setg(errp, "Target is attached to a different thread from " 3639efd75567SFam Zheng "source."); 3640efd75567SFam Zheng goto out; 3641efd75567SFam Zheng } 3642efd75567SFam Zheng } 3643b40dacdcSJohn Snow if (!backup->auto_finalize) { 3644bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_FINALIZE; 3645b40dacdcSJohn Snow } 3646b40dacdcSJohn Snow if (!backup->auto_dismiss) { 3647bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_DISMISS; 3648b40dacdcSJohn Snow } 3649111049a4SJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3650111049a4SJohn Snow backup->sync, NULL, backup->compress, 3651111049a4SJohn Snow backup->on_source_error, backup->on_target_error, 3652b40dacdcSJohn Snow job_flags, NULL, NULL, txn, &local_err); 3653c29c1dd3SFam Zheng if (local_err != NULL) { 3654c29c1dd3SFam Zheng error_propagate(errp, local_err); 3655c29c1dd3SFam Zheng } 3656c29c1dd3SFam Zheng out: 3657c29c1dd3SFam Zheng aio_context_release(aio_context); 3658111049a4SJohn Snow return job; 3659c29c1dd3SFam Zheng } 3660c29c1dd3SFam Zheng 3661dc7a4a9eSPavel Butsykin void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp) 366278f51fdeSJohn Snow { 3663111049a4SJohn Snow BlockJob *job; 3664111049a4SJohn Snow job = do_blockdev_backup(arg, NULL, errp); 3665111049a4SJohn Snow if (job) { 3666da01ff7fSKevin Wolf job_start(&job->job); 3667111049a4SJohn Snow } 366878f51fdeSJohn Snow } 366978f51fdeSJohn Snow 36704193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 36714193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 36724193cdd7SFam Zheng **/ 367371aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, 36744193cdd7SFam Zheng BlockDriverState *target, 367509158f00SBenoît Canet bool has_replaces, const char *replaces, 3676d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3677274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 3678b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3679eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 368008e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 36814193cdd7SFam Zheng bool has_on_source_error, 36824193cdd7SFam Zheng BlockdevOnError on_source_error, 36834193cdd7SFam Zheng bool has_on_target_error, 36844193cdd7SFam Zheng BlockdevOnError on_target_error, 36850fc9f8eaSFam Zheng bool has_unmap, bool unmap, 36866cdbceb1SKevin Wolf bool has_filter_node_name, 36876cdbceb1SKevin Wolf const char *filter_node_name, 3688b952b558SPaolo Bonzini Error **errp) 3689d9b902dbSPaolo Bonzini { 3690d9b902dbSPaolo Bonzini 3691d9b902dbSPaolo Bonzini if (!has_speed) { 3692d9b902dbSPaolo Bonzini speed = 0; 3693d9b902dbSPaolo Bonzini } 3694b952b558SPaolo Bonzini if (!has_on_source_error) { 3695b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3696b952b558SPaolo Bonzini } 3697b952b558SPaolo Bonzini if (!has_on_target_error) { 3698b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3699b952b558SPaolo Bonzini } 3700eee13dfeSPaolo Bonzini if (!has_granularity) { 3701eee13dfeSPaolo Bonzini granularity = 0; 3702eee13dfeSPaolo Bonzini } 370308e4ed6cSPaolo Bonzini if (!has_buf_size) { 370448ac0a4dSWen Congyang buf_size = 0; 370508e4ed6cSPaolo Bonzini } 37060fc9f8eaSFam Zheng if (!has_unmap) { 37070fc9f8eaSFam Zheng unmap = true; 37080fc9f8eaSFam Zheng } 37096cdbceb1SKevin Wolf if (!has_filter_node_name) { 37106cdbceb1SKevin Wolf filter_node_name = NULL; 37116cdbceb1SKevin Wolf } 371208e4ed6cSPaolo Bonzini 3713eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3714c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 37153cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3716eee13dfeSPaolo Bonzini return; 3717eee13dfeSPaolo Bonzini } 3718eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3719c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3720c6bd8c70SMarkus Armbruster "power of 2"); 3721eee13dfeSPaolo Bonzini return; 3722eee13dfeSPaolo Bonzini } 3723d9b902dbSPaolo Bonzini 37244193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 37254193cdd7SFam Zheng return; 37264193cdd7SFam Zheng } 3727e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3728e40e5027SFam Zheng return; 3729e40e5027SFam Zheng } 37304193cdd7SFam Zheng 37314193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 37324193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 37334193cdd7SFam Zheng } 37344193cdd7SFam Zheng 37354193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 37364193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 37374193cdd7SFam Zheng */ 373871aa9867SAlberto Garcia mirror_start(job_id, bs, target, 37394193cdd7SFam Zheng has_replaces ? replaces : NULL, 3740274fcceeSMax Reitz speed, granularity, buf_size, sync, backing_mode, 37416cdbceb1SKevin Wolf on_source_error, on_target_error, unmap, filter_node_name, 37426cdbceb1SKevin Wolf errp); 37434193cdd7SFam Zheng } 37444193cdd7SFam Zheng 3745faecd40aSEric Blake void qmp_drive_mirror(DriveMirror *arg, Error **errp) 37464193cdd7SFam Zheng { 37474193cdd7SFam Zheng BlockDriverState *bs; 37484193cdd7SFam Zheng BlockDriverState *source, *target_bs; 37494193cdd7SFam Zheng AioContext *aio_context; 3750274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 37514193cdd7SFam Zheng Error *local_err = NULL; 37524193cdd7SFam Zheng QDict *options = NULL; 37534193cdd7SFam Zheng int flags; 37544193cdd7SFam Zheng int64_t size; 3755faecd40aSEric Blake const char *format = arg->format; 37564193cdd7SFam Zheng 37570524e93aSKevin Wolf bs = qmp_get_root_bs(arg->device, errp); 37580524e93aSKevin Wolf if (!bs) { 3759d9b902dbSPaolo Bonzini return; 3760d9b902dbSPaolo Bonzini } 3761d9b902dbSPaolo Bonzini 3762cb2af917SPaolo Bonzini /* Early check to avoid creating target */ 3763cb2af917SPaolo Bonzini if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 3764cb2af917SPaolo Bonzini return; 3765cb2af917SPaolo Bonzini } 3766cb2af917SPaolo Bonzini 37670524e93aSKevin Wolf aio_context = bdrv_get_aio_context(bs); 37685a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 37695a7e7a0bSStefan Hajnoczi 3770faecd40aSEric Blake if (!arg->has_mode) { 3771faecd40aSEric Blake arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 37724193cdd7SFam Zheng } 3773d9b902dbSPaolo Bonzini 3774faecd40aSEric Blake if (!arg->has_format) { 3775faecd40aSEric Blake format = (arg->mode == NEW_IMAGE_MODE_EXISTING 3776faecd40aSEric Blake ? NULL : bs->drv->format_name); 3777d9b902dbSPaolo Bonzini } 3778d9b902dbSPaolo Bonzini 377961de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3780760e0063SKevin Wolf source = backing_bs(bs); 3781faecd40aSEric Blake if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { 3782faecd40aSEric Blake arg->sync = MIRROR_SYNC_MODE_FULL; 3783d9b902dbSPaolo Bonzini } 3784faecd40aSEric Blake if (arg->sync == MIRROR_SYNC_MODE_NONE) { 3785117e0c82SMax Reitz source = bs; 3786117e0c82SMax Reitz } 3787d9b902dbSPaolo Bonzini 3788ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3789ac3c5d83SStefan Hajnoczi if (size < 0) { 3790ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 37915a7e7a0bSStefan Hajnoczi goto out; 3792ac3c5d83SStefan Hajnoczi } 3793ac3c5d83SStefan Hajnoczi 3794faecd40aSEric Blake if (arg->has_replaces) { 379509158f00SBenoît Canet BlockDriverState *to_replace_bs; 37965a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 37975a7e7a0bSStefan Hajnoczi int64_t replace_size; 379809158f00SBenoît Canet 3799faecd40aSEric Blake if (!arg->has_node_name) { 380009158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 380109158f00SBenoît Canet " named node of the graph"); 38025a7e7a0bSStefan Hajnoczi goto out; 380309158f00SBenoît Canet } 380409158f00SBenoît Canet 3805faecd40aSEric Blake to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err); 380609158f00SBenoît Canet 380709158f00SBenoît Canet if (!to_replace_bs) { 380809158f00SBenoît Canet error_propagate(errp, local_err); 38095a7e7a0bSStefan Hajnoczi goto out; 381009158f00SBenoît Canet } 381109158f00SBenoît Canet 38125a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 38135a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 38145a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 38155a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 38165a7e7a0bSStefan Hajnoczi 38175a7e7a0bSStefan Hajnoczi if (size != replace_size) { 381809158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 381909158f00SBenoît Canet "different size"); 38205a7e7a0bSStefan Hajnoczi goto out; 382109158f00SBenoît Canet } 382209158f00SBenoît Canet } 382309158f00SBenoît Canet 3824faecd40aSEric Blake if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { 3825274fcceeSMax Reitz backing_mode = MIRROR_SOURCE_BACKING_CHAIN; 3826274fcceeSMax Reitz } else { 3827274fcceeSMax Reitz backing_mode = MIRROR_OPEN_BACKING_CHAIN; 3828274fcceeSMax Reitz } 3829274fcceeSMax Reitz 38302a32c6e8SJohn Snow /* Don't open backing image in create() */ 38312a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING; 38322a32c6e8SJohn Snow 3833faecd40aSEric Blake if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) 3834faecd40aSEric Blake && arg->mode != NEW_IMAGE_MODE_EXISTING) 383514526864SMax Reitz { 3836d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3837e6641719SMax Reitz assert(format); 3838faecd40aSEric Blake bdrv_img_create(arg->target, format, 38399217283dSFam Zheng NULL, NULL, NULL, size, flags, false, &local_err); 3840d9b902dbSPaolo Bonzini } else { 3841faecd40aSEric Blake switch (arg->mode) { 3842d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3843d9b902dbSPaolo Bonzini break; 3844d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3845d9b902dbSPaolo Bonzini /* create new image with backing file */ 3846faecd40aSEric Blake bdrv_img_create(arg->target, format, 3847d9b902dbSPaolo Bonzini source->filename, 3848d9b902dbSPaolo Bonzini source->drv->format_name, 38499217283dSFam Zheng NULL, size, flags, false, &local_err); 3850d9b902dbSPaolo Bonzini break; 3851d9b902dbSPaolo Bonzini default: 3852d9b902dbSPaolo Bonzini abort(); 3853d9b902dbSPaolo Bonzini } 3854d9b902dbSPaolo Bonzini } 3855d9b902dbSPaolo Bonzini 385684d18f06SMarkus Armbruster if (local_err) { 3857cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 38585a7e7a0bSStefan Hajnoczi goto out; 3859d9b902dbSPaolo Bonzini } 3860d9b902dbSPaolo Bonzini 38614c828dc6SBenoît Canet options = qdict_new(); 3862faecd40aSEric Blake if (arg->has_node_name) { 386346f5ac20SEric Blake qdict_put_str(options, "node-name", arg->node_name); 38644c828dc6SBenoît Canet } 3865e6641719SMax Reitz if (format) { 386646f5ac20SEric Blake qdict_put_str(options, "driver", format); 3867e6641719SMax Reitz } 38684c828dc6SBenoît Canet 3869b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3870b812f671SPaolo Bonzini * file. 3871b812f671SPaolo Bonzini */ 38722a32c6e8SJohn Snow target_bs = bdrv_open(arg->target, NULL, options, flags, errp); 38735b363937SMax Reitz if (!target_bs) { 38745a7e7a0bSStefan Hajnoczi goto out; 3875d9b902dbSPaolo Bonzini } 3876d9b902dbSPaolo Bonzini 38775a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 38785a7e7a0bSStefan Hajnoczi 3879faecd40aSEric Blake blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, 3880faecd40aSEric Blake arg->has_replaces, arg->replaces, arg->sync, 3881faecd40aSEric Blake backing_mode, arg->has_speed, arg->speed, 3882faecd40aSEric Blake arg->has_granularity, arg->granularity, 3883faecd40aSEric Blake arg->has_buf_size, arg->buf_size, 3884faecd40aSEric Blake arg->has_on_source_error, arg->on_source_error, 3885faecd40aSEric Blake arg->has_on_target_error, arg->on_target_error, 3886faecd40aSEric Blake arg->has_unmap, arg->unmap, 38876cdbceb1SKevin Wolf false, NULL, 38884193cdd7SFam Zheng &local_err); 3889e253f4b8SKevin Wolf bdrv_unref(target_bs); 3890d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 38915a7e7a0bSStefan Hajnoczi out: 38925a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3893d9b902dbSPaolo Bonzini } 3894d9b902dbSPaolo Bonzini 389571aa9867SAlberto Garcia void qmp_blockdev_mirror(bool has_job_id, const char *job_id, 389671aa9867SAlberto Garcia const char *device, const char *target, 3897df92562eSFam Zheng bool has_replaces, const char *replaces, 3898df92562eSFam Zheng MirrorSyncMode sync, 3899df92562eSFam Zheng bool has_speed, int64_t speed, 3900df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3901df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3902df92562eSFam Zheng bool has_on_source_error, 3903df92562eSFam Zheng BlockdevOnError on_source_error, 3904df92562eSFam Zheng bool has_on_target_error, 3905df92562eSFam Zheng BlockdevOnError on_target_error, 39066cdbceb1SKevin Wolf bool has_filter_node_name, 39076cdbceb1SKevin Wolf const char *filter_node_name, 3908df92562eSFam Zheng Error **errp) 3909df92562eSFam Zheng { 3910df92562eSFam Zheng BlockDriverState *bs; 3911df92562eSFam Zheng BlockDriverState *target_bs; 3912df92562eSFam Zheng AioContext *aio_context; 3913274fcceeSMax Reitz BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; 3914df92562eSFam Zheng Error *local_err = NULL; 3915df92562eSFam Zheng 391607eec652SKevin Wolf bs = qmp_get_root_bs(device, errp); 3917df92562eSFam Zheng if (!bs) { 3918df92562eSFam Zheng return; 3919df92562eSFam Zheng } 3920df92562eSFam Zheng 3921df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3922df92562eSFam Zheng if (!target_bs) { 3923df92562eSFam Zheng return; 3924df92562eSFam Zheng } 3925df92562eSFam Zheng 3926df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3927df92562eSFam Zheng aio_context_acquire(aio_context); 3928df92562eSFam Zheng 3929df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3930df92562eSFam Zheng 393171aa9867SAlberto Garcia blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 3932274fcceeSMax Reitz has_replaces, replaces, sync, backing_mode, 3933df92562eSFam Zheng has_speed, speed, 3934df92562eSFam Zheng has_granularity, granularity, 3935df92562eSFam Zheng has_buf_size, buf_size, 3936df92562eSFam Zheng has_on_source_error, on_source_error, 3937df92562eSFam Zheng has_on_target_error, on_target_error, 3938df92562eSFam Zheng true, true, 39396cdbceb1SKevin Wolf has_filter_node_name, filter_node_name, 3940df92562eSFam Zheng &local_err); 3941df92562eSFam Zheng error_propagate(errp, local_err); 3942df92562eSFam Zheng 3943df92562eSFam Zheng aio_context_release(aio_context); 3944df92562eSFam Zheng } 3945df92562eSFam Zheng 39463ddf3efeSAlberto Garcia /* Get a block job using its ID and acquire its AioContext */ 39473ddf3efeSAlberto Garcia static BlockJob *find_block_job(const char *id, AioContext **aio_context, 394824d6bffeSMarkus Armbruster Error **errp) 39492d47c6e9SStefan Hajnoczi { 39503ddf3efeSAlberto Garcia BlockJob *job; 39513ddf3efeSAlberto Garcia 39523ddf3efeSAlberto Garcia assert(id != NULL); 39532d47c6e9SStefan Hajnoczi 39545433c24fSMax Reitz *aio_context = NULL; 39555433c24fSMax Reitz 39563ddf3efeSAlberto Garcia job = block_job_get(id); 39573ddf3efeSAlberto Garcia 39583ddf3efeSAlberto Garcia if (!job) { 39593ddf3efeSAlberto Garcia error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 39603ddf3efeSAlberto Garcia "Block job '%s' not found", id); 39613ddf3efeSAlberto Garcia return NULL; 39622d47c6e9SStefan Hajnoczi } 39633d948cdfSStefan Hajnoczi 39643ddf3efeSAlberto Garcia *aio_context = blk_get_aio_context(job->blk); 39653d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 39663d948cdfSStefan Hajnoczi 39673ddf3efeSAlberto Garcia return job; 39682d47c6e9SStefan Hajnoczi } 39692d47c6e9SStefan Hajnoczi 3970882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 39712d47c6e9SStefan Hajnoczi { 39723d948cdfSStefan Hajnoczi AioContext *aio_context; 397324d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 39742d47c6e9SStefan Hajnoczi 39752d47c6e9SStefan Hajnoczi if (!job) { 39762d47c6e9SStefan Hajnoczi return; 39772d47c6e9SStefan Hajnoczi } 39782d47c6e9SStefan Hajnoczi 3979882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 39803d948cdfSStefan Hajnoczi aio_context_release(aio_context); 39812d47c6e9SStefan Hajnoczi } 3982370521a1SStefan Hajnoczi 39836e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 39846e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 39856e37fb81SPaolo Bonzini { 39863d948cdfSStefan Hajnoczi AioContext *aio_context; 398724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 39886e37fb81SPaolo Bonzini 39896e37fb81SPaolo Bonzini if (!job) { 39906e37fb81SPaolo Bonzini return; 39916e37fb81SPaolo Bonzini } 39923d948cdfSStefan Hajnoczi 39933d948cdfSStefan Hajnoczi if (!has_force) { 39943d948cdfSStefan Hajnoczi force = false; 39953d948cdfSStefan Hajnoczi } 39963d948cdfSStefan Hajnoczi 3997b15de828SKevin Wolf if (job_user_paused(&job->job) && !force) { 3998f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3999f231b88dSCole Robinson device); 40003d948cdfSStefan Hajnoczi goto out; 40016e37fb81SPaolo Bonzini } 40026e37fb81SPaolo Bonzini 40036e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 40043d70ff53SKevin Wolf job_user_cancel(&job->job, force, errp); 40053d948cdfSStefan Hajnoczi out: 40063d948cdfSStefan Hajnoczi aio_context_release(aio_context); 40076e37fb81SPaolo Bonzini } 40086e37fb81SPaolo Bonzini 40096e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 4010370521a1SStefan Hajnoczi { 40113d948cdfSStefan Hajnoczi AioContext *aio_context; 401224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 4013370521a1SStefan Hajnoczi 40140ec4dfb8SJohn Snow if (!job) { 4015370521a1SStefan Hajnoczi return; 4016370521a1SStefan Hajnoczi } 40176e37fb81SPaolo Bonzini 40186e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 4019b15de828SKevin Wolf job_user_pause(&job->job, errp); 40203d948cdfSStefan Hajnoczi aio_context_release(aio_context); 40216e37fb81SPaolo Bonzini } 40226e37fb81SPaolo Bonzini 40236e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 40246e37fb81SPaolo Bonzini { 40253d948cdfSStefan Hajnoczi AioContext *aio_context; 402624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 40276e37fb81SPaolo Bonzini 40280ec4dfb8SJohn Snow if (!job) { 40298acc72a4SPaolo Bonzini return; 40308acc72a4SPaolo Bonzini } 4031370521a1SStefan Hajnoczi 40326e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 4033b15de828SKevin Wolf job_user_resume(&job->job, errp); 40343d948cdfSStefan Hajnoczi aio_context_release(aio_context); 4035370521a1SStefan Hajnoczi } 4036fb5458cdSStefan Hajnoczi 4037aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 4038aeae883bSPaolo Bonzini { 40393d948cdfSStefan Hajnoczi AioContext *aio_context; 404024d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 4041aeae883bSPaolo Bonzini 4042aeae883bSPaolo Bonzini if (!job) { 4043aeae883bSPaolo Bonzini return; 4044aeae883bSPaolo Bonzini } 4045aeae883bSPaolo Bonzini 4046aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 40473453d972SKevin Wolf job_complete(&job->job, errp); 40483d948cdfSStefan Hajnoczi aio_context_release(aio_context); 4049aeae883bSPaolo Bonzini } 4050aeae883bSPaolo Bonzini 405111b61fbcSJohn Snow void qmp_block_job_finalize(const char *id, Error **errp) 405211b61fbcSJohn Snow { 405311b61fbcSJohn Snow AioContext *aio_context; 405411b61fbcSJohn Snow BlockJob *job = find_block_job(id, &aio_context, errp); 405511b61fbcSJohn Snow 405611b61fbcSJohn Snow if (!job) { 405711b61fbcSJohn Snow return; 405811b61fbcSJohn Snow } 405911b61fbcSJohn Snow 406011b61fbcSJohn Snow trace_qmp_block_job_finalize(job); 40617eaa8fb5SKevin Wolf job_finalize(&job->job, errp); 406211b61fbcSJohn Snow aio_context_release(aio_context); 406311b61fbcSJohn Snow } 406411b61fbcSJohn Snow 406575f71059SJohn Snow void qmp_block_job_dismiss(const char *id, Error **errp) 406675f71059SJohn Snow { 406775f71059SJohn Snow AioContext *aio_context; 40685f9a6a08SKevin Wolf BlockJob *bjob = find_block_job(id, &aio_context, errp); 40695f9a6a08SKevin Wolf Job *job; 407075f71059SJohn Snow 40715f9a6a08SKevin Wolf if (!bjob) { 407275f71059SJohn Snow return; 407375f71059SJohn Snow } 407475f71059SJohn Snow 40755f9a6a08SKevin Wolf trace_qmp_block_job_dismiss(bjob); 40765f9a6a08SKevin Wolf job = &bjob->job; 40775f9a6a08SKevin Wolf job_dismiss(&job, errp); 407875f71059SJohn Snow aio_context_release(aio_context); 407975f71059SJohn Snow } 408075f71059SJohn Snow 4081fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 4082fa40e656SJeff Cody const char *image_node_name, 4083fa40e656SJeff Cody const char *backing_file, 4084fa40e656SJeff Cody Error **errp) 4085fa40e656SJeff Cody { 4086fa40e656SJeff Cody BlockDriverState *bs = NULL; 4087729962f6SStefan Hajnoczi AioContext *aio_context; 4088fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 4089fa40e656SJeff Cody Error *local_err = NULL; 4090fa40e656SJeff Cody bool ro; 4091fa40e656SJeff Cody int open_flags; 4092fa40e656SJeff Cody int ret; 4093fa40e656SJeff Cody 40947b5dca3fSKevin Wolf bs = qmp_get_root_bs(device, errp); 40957b5dca3fSKevin Wolf if (!bs) { 4096fa40e656SJeff Cody return; 4097fa40e656SJeff Cody } 4098fa40e656SJeff Cody 40997b5dca3fSKevin Wolf aio_context = bdrv_get_aio_context(bs); 4100729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 4101729962f6SStefan Hajnoczi 4102fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 4103fa40e656SJeff Cody if (local_err) { 4104fa40e656SJeff Cody error_propagate(errp, local_err); 4105729962f6SStefan Hajnoczi goto out; 4106fa40e656SJeff Cody } 4107fa40e656SJeff Cody 4108fa40e656SJeff Cody if (!image_bs) { 4109fa40e656SJeff Cody error_setg(errp, "image file not found"); 4110729962f6SStefan Hajnoczi goto out; 4111fa40e656SJeff Cody } 4112fa40e656SJeff Cody 4113fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 4114fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 4115fa40e656SJeff Cody "without a backing file"); 4116729962f6SStefan Hajnoczi goto out; 4117fa40e656SJeff Cody } 4118fa40e656SJeff Cody 4119fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 4120fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 4121fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 4122729962f6SStefan Hajnoczi goto out; 4123fa40e656SJeff Cody } 4124fa40e656SJeff Cody 4125fa40e656SJeff Cody /* final sanity check */ 4126fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 4127fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 4128fa40e656SJeff Cody device); 4129729962f6SStefan Hajnoczi goto out; 4130fa40e656SJeff Cody } 4131fa40e656SJeff Cody 4132fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 4133fa40e656SJeff Cody open_flags = image_bs->open_flags; 4134fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 4135fa40e656SJeff Cody 4136fa40e656SJeff Cody if (ro) { 4137fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 4138fa40e656SJeff Cody if (local_err) { 4139fa40e656SJeff Cody error_propagate(errp, local_err); 4140729962f6SStefan Hajnoczi goto out; 4141fa40e656SJeff Cody } 4142fa40e656SJeff Cody } 4143fa40e656SJeff Cody 4144fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 4145fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 4146fa40e656SJeff Cody 4147fa40e656SJeff Cody if (ret < 0) { 4148fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 4149fa40e656SJeff Cody backing_file); 4150fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 4151fa40e656SJeff Cody * appropriate */ 4152fa40e656SJeff Cody } 4153fa40e656SJeff Cody 4154fa40e656SJeff Cody if (ro) { 4155fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 4156621ff94dSEduardo Habkost error_propagate(errp, local_err); 4157fa40e656SJeff Cody } 4158729962f6SStefan Hajnoczi 4159729962f6SStefan Hajnoczi out: 4160729962f6SStefan Hajnoczi aio_context_release(aio_context); 4161fa40e656SJeff Cody } 4162fa40e656SJeff Cody 4163abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 4164abb21ac3SKevin Wolf { 4165abb21ac3SKevin Wolf QemuOpts *opts; 4166abb21ac3SKevin Wolf QDict *qdict; 4167abb21ac3SKevin Wolf Error *local_err = NULL; 4168abb21ac3SKevin Wolf 4169abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 4170abb21ac3SKevin Wolf if (!opts) { 4171abb21ac3SKevin Wolf return; 4172abb21ac3SKevin Wolf } 4173abb21ac3SKevin Wolf 4174abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 4175abb21ac3SKevin Wolf 4176abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 4177cb3e7f08SMarc-André Lureau qobject_unref(qdict); 4178abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 4179abb21ac3SKevin Wolf goto out; 4180abb21ac3SKevin Wolf } 4181abb21ac3SKevin Wolf 4182abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 4183abb21ac3SKevin Wolf if (!bs) { 4184abb21ac3SKevin Wolf error_report_err(local_err); 4185abb21ac3SKevin Wolf goto out; 4186abb21ac3SKevin Wolf } 4187abb21ac3SKevin Wolf 4188abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4189abb21ac3SKevin Wolf 4190abb21ac3SKevin Wolf out: 4191abb21ac3SKevin Wolf qemu_opts_del(opts); 4192abb21ac3SKevin Wolf } 4193abb21ac3SKevin Wolf 4194d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 4195d26c9a15SKevin Wolf { 4196be4b67bcSMax Reitz BlockDriverState *bs; 4197d26c9a15SKevin Wolf QObject *obj; 41987d5e199aSDaniel P. Berrange Visitor *v = qobject_output_visitor_new(&obj); 4199d26c9a15SKevin Wolf QDict *qdict; 4200d26c9a15SKevin Wolf Error *local_err = NULL; 4201d26c9a15SKevin Wolf 42023b098d56SEric Blake visit_type_BlockdevOptions(v, NULL, &options, &local_err); 420384d18f06SMarkus Armbruster if (local_err) { 4204d26c9a15SKevin Wolf error_propagate(errp, local_err); 4205d26c9a15SKevin Wolf goto fail; 4206d26c9a15SKevin Wolf } 4207d26c9a15SKevin Wolf 42083b098d56SEric Blake visit_complete(v, &obj); 42097dc847ebSMax Reitz qdict = qobject_to(QDict, obj); 4210d26c9a15SKevin Wolf 4211d26c9a15SKevin Wolf qdict_flatten(qdict); 4212d26c9a15SKevin Wolf 4213be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 42149ec8873eSKevin Wolf error_setg(errp, "'node-name' must be specified for the root node"); 4215be4b67bcSMax Reitz goto fail; 4216be4b67bcSMax Reitz } 4217be4b67bcSMax Reitz 4218bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 4219bd745e23SMax Reitz if (!bs) { 4220be4b67bcSMax Reitz goto fail; 4221be4b67bcSMax Reitz } 42229c4218e9SMax Reitz 42239c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4224be4b67bcSMax Reitz 4225d26c9a15SKevin Wolf fail: 42263b098d56SEric Blake visit_free(v); 4227d26c9a15SKevin Wolf } 4228d26c9a15SKevin Wolf 422979b7a77eSMarkus Armbruster void qmp_blockdev_del(const char *node_name, Error **errp) 423081b936aeSAlberto Garcia { 423181b936aeSAlberto Garcia AioContext *aio_context; 423281b936aeSAlberto Garcia BlockDriverState *bs; 423381b936aeSAlberto Garcia 423481b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 423581b936aeSAlberto Garcia if (!bs) { 423681b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 423781b936aeSAlberto Garcia return; 423881b936aeSAlberto Garcia } 42391f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 4240e467da7bSKevin Wolf error_setg(errp, "Node %s is in use", node_name); 424181b936aeSAlberto Garcia return; 424281b936aeSAlberto Garcia } 424381b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 424481b936aeSAlberto Garcia aio_context_acquire(aio_context); 424581b936aeSAlberto Garcia 424681b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 424781b936aeSAlberto Garcia goto out; 424881b936aeSAlberto Garcia } 424981b936aeSAlberto Garcia 42509ec8873eSKevin Wolf if (!bs->monitor_list.tqe_prev) { 42519c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 42529c4218e9SMax Reitz bs->node_name); 42539c4218e9SMax Reitz goto out; 42549c4218e9SMax Reitz } 42559c4218e9SMax Reitz 42569c4218e9SMax Reitz if (bs->refcnt > 1) { 425781b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 425881b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 425981b936aeSAlberto Garcia goto out; 426081b936aeSAlberto Garcia } 426181b936aeSAlberto Garcia 42629c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 426381b936aeSAlberto Garcia bdrv_unref(bs); 426481b936aeSAlberto Garcia 426581b936aeSAlberto Garcia out: 426681b936aeSAlberto Garcia aio_context_release(aio_context); 426781b936aeSAlberto Garcia } 426881b936aeSAlberto Garcia 42697f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 42707f821597SWen Congyang const char *child_name) 42717f821597SWen Congyang { 42727f821597SWen Congyang BdrvChild *child; 42737f821597SWen Congyang 42747f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 42757f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 42767f821597SWen Congyang return child; 42777f821597SWen Congyang } 42787f821597SWen Congyang } 42797f821597SWen Congyang 42807f821597SWen Congyang return NULL; 42817f821597SWen Congyang } 42827f821597SWen Congyang 42837f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 42847f821597SWen Congyang const char *child, bool has_node, 42857f821597SWen Congyang const char *node, Error **errp) 42867f821597SWen Congyang { 42877f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 42887f821597SWen Congyang BdrvChild *p_child; 42897f821597SWen Congyang 42907f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 42917f821597SWen Congyang if (!parent_bs) { 42927f821597SWen Congyang return; 42937f821597SWen Congyang } 42947f821597SWen Congyang 42957f821597SWen Congyang if (has_child == has_node) { 42967f821597SWen Congyang if (has_child) { 42977f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 42987f821597SWen Congyang } else { 42997f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 43007f821597SWen Congyang } 43017f821597SWen Congyang return; 43027f821597SWen Congyang } 43037f821597SWen Congyang 43047f821597SWen Congyang if (has_child) { 43057f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 43067f821597SWen Congyang if (!p_child) { 43077f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 43087f821597SWen Congyang parent, child); 43097f821597SWen Congyang return; 43107f821597SWen Congyang } 43117f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 43127f821597SWen Congyang } 43137f821597SWen Congyang 43147f821597SWen Congyang if (has_node) { 43157f821597SWen Congyang new_bs = bdrv_find_node(node); 43167f821597SWen Congyang if (!new_bs) { 43177f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 43187f821597SWen Congyang return; 43197f821597SWen Congyang } 43207f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 43217f821597SWen Congyang } 43227f821597SWen Congyang } 43237f821597SWen Congyang 4324fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4325fb5458cdSStefan Hajnoczi { 4326fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4327f0f55dedSAlberto Garcia BlockJob *job; 4328fea68bb6SMarkus Armbruster 4329f0f55dedSAlberto Garcia for (job = block_job_next(NULL); job; job = block_job_next(job)) { 4330559b935fSJohn Snow BlockJobInfoList *elem; 4331559b935fSJohn Snow AioContext *aio_context; 433269691e72SStefan Hajnoczi 4333559b935fSJohn Snow if (block_job_is_internal(job)) { 4334559b935fSJohn Snow continue; 4335559b935fSJohn Snow } 4336559b935fSJohn Snow elem = g_new0(BlockJobInfoList, 1); 4337559b935fSJohn Snow aio_context = blk_get_aio_context(job->blk); 433869691e72SStefan Hajnoczi aio_context_acquire(aio_context); 4339559b935fSJohn Snow elem->value = block_job_query(job, errp); 4340f0f55dedSAlberto Garcia aio_context_release(aio_context); 4341559b935fSJohn Snow if (!elem->value) { 4342559b935fSJohn Snow g_free(elem); 4343559b935fSJohn Snow qapi_free_BlockJobInfoList(head); 4344559b935fSJohn Snow return NULL; 4345559b935fSJohn Snow } 4346fea68bb6SMarkus Armbruster *p_next = elem; 4347fea68bb6SMarkus Armbruster p_next = &elem->next; 4348fea68bb6SMarkus Armbruster } 434969691e72SStefan Hajnoczi 4350fea68bb6SMarkus Armbruster return head; 4351fb5458cdSStefan Hajnoczi } 43524d454574SPaolo Bonzini 4353ca00bbb1SStefan Hajnoczi void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread, 4354882e9b89SStefan Hajnoczi bool has_force, bool force, Error **errp) 4355ca00bbb1SStefan Hajnoczi { 4356ca00bbb1SStefan Hajnoczi AioContext *old_context; 4357ca00bbb1SStefan Hajnoczi AioContext *new_context; 4358ca00bbb1SStefan Hajnoczi BlockDriverState *bs; 4359ca00bbb1SStefan Hajnoczi 4360ca00bbb1SStefan Hajnoczi bs = bdrv_find_node(node_name); 4361ca00bbb1SStefan Hajnoczi if (!bs) { 4362ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find node %s", node_name); 4363ca00bbb1SStefan Hajnoczi return; 4364ca00bbb1SStefan Hajnoczi } 4365ca00bbb1SStefan Hajnoczi 4366882e9b89SStefan Hajnoczi /* Protects against accidents. */ 4367882e9b89SStefan Hajnoczi if (!(has_force && force) && bdrv_has_blk(bs)) { 4368882e9b89SStefan Hajnoczi error_setg(errp, "Node %s is associated with a BlockBackend and could " 4369882e9b89SStefan Hajnoczi "be in use (use force=true to override this check)", 4370882e9b89SStefan Hajnoczi node_name); 4371ca00bbb1SStefan Hajnoczi return; 4372ca00bbb1SStefan Hajnoczi } 4373ca00bbb1SStefan Hajnoczi 4374ca00bbb1SStefan Hajnoczi if (iothread->type == QTYPE_QSTRING) { 4375ca00bbb1SStefan Hajnoczi IOThread *obj = iothread_by_id(iothread->u.s); 4376ca00bbb1SStefan Hajnoczi if (!obj) { 4377ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find iothread %s", iothread->u.s); 4378ca00bbb1SStefan Hajnoczi return; 4379ca00bbb1SStefan Hajnoczi } 4380ca00bbb1SStefan Hajnoczi 4381ca00bbb1SStefan Hajnoczi new_context = iothread_get_aio_context(obj); 4382ca00bbb1SStefan Hajnoczi } else { 4383ca00bbb1SStefan Hajnoczi new_context = qemu_get_aio_context(); 4384ca00bbb1SStefan Hajnoczi } 4385ca00bbb1SStefan Hajnoczi 4386ca00bbb1SStefan Hajnoczi old_context = bdrv_get_aio_context(bs); 4387ca00bbb1SStefan Hajnoczi aio_context_acquire(old_context); 4388ca00bbb1SStefan Hajnoczi 4389ca00bbb1SStefan Hajnoczi bdrv_set_aio_context(bs, new_context); 4390ca00bbb1SStefan Hajnoczi 4391ca00bbb1SStefan Hajnoczi aio_context_release(old_context); 4392ca00bbb1SStefan Hajnoczi } 4393ca00bbb1SStefan Hajnoczi 43947e5c776dSVladimir Sementsov-Ogievskiy void qmp_x_block_latency_histogram_set( 43957e5c776dSVladimir Sementsov-Ogievskiy const char *device, 43967e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries, uint64List *boundaries, 43977e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_read, uint64List *boundaries_read, 43987e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_write, uint64List *boundaries_write, 43997e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_flush, uint64List *boundaries_flush, 44007e5c776dSVladimir Sementsov-Ogievskiy Error **errp) 44017e5c776dSVladimir Sementsov-Ogievskiy { 44027e5c776dSVladimir Sementsov-Ogievskiy BlockBackend *blk = blk_by_name(device); 44037e5c776dSVladimir Sementsov-Ogievskiy BlockAcctStats *stats; 44047e5c776dSVladimir Sementsov-Ogievskiy 44057e5c776dSVladimir Sementsov-Ogievskiy if (!blk) { 44067e5c776dSVladimir Sementsov-Ogievskiy error_setg(errp, "Device '%s' not found", device); 44077e5c776dSVladimir Sementsov-Ogievskiy return; 44087e5c776dSVladimir Sementsov-Ogievskiy } 44097e5c776dSVladimir Sementsov-Ogievskiy stats = blk_get_stats(blk); 44107e5c776dSVladimir Sementsov-Ogievskiy 44117e5c776dSVladimir Sementsov-Ogievskiy if (!has_boundaries && !has_boundaries_read && !has_boundaries_write && 44127e5c776dSVladimir Sementsov-Ogievskiy !has_boundaries_flush) 44137e5c776dSVladimir Sementsov-Ogievskiy { 44147e5c776dSVladimir Sementsov-Ogievskiy block_latency_histograms_clear(stats); 44157e5c776dSVladimir Sementsov-Ogievskiy return; 44167e5c776dSVladimir Sementsov-Ogievskiy } 44177e5c776dSVladimir Sementsov-Ogievskiy 44187e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_read) { 44197e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 44207e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_READ, 44217e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_read ? boundaries_read : boundaries); 44227e5c776dSVladimir Sementsov-Ogievskiy } 44237e5c776dSVladimir Sementsov-Ogievskiy 44247e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_write) { 44257e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 44267e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_WRITE, 44277e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_write ? boundaries_write : boundaries); 44287e5c776dSVladimir Sementsov-Ogievskiy } 44297e5c776dSVladimir Sementsov-Ogievskiy 44307e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_flush) { 44317e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 44327e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_FLUSH, 44337e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_flush ? boundaries_flush : boundaries); 44347e5c776dSVladimir Sementsov-Ogievskiy } 44357e5c776dSVladimir Sementsov-Ogievskiy } 44367e5c776dSVladimir Sementsov-Ogievskiy 44370006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 44384d454574SPaolo Bonzini .name = "drive", 44390006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 44404d454574SPaolo Bonzini .desc = { 44414d454574SPaolo Bonzini { 44424d454574SPaolo Bonzini .name = "snapshot", 44434d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 44444d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 44454d454574SPaolo Bonzini },{ 44464d454574SPaolo Bonzini .name = "aio", 44474d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 44484d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 44494d454574SPaolo Bonzini },{ 4450e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4451e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4452e4b24b49SKevin Wolf .help = "Enable writeback mode", 4453e4b24b49SKevin Wolf },{ 44544d454574SPaolo Bonzini .name = "format", 44554d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 44564d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 44574d454574SPaolo Bonzini },{ 44584d454574SPaolo Bonzini .name = "rerror", 44594d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 44604d454574SPaolo Bonzini .help = "read error action", 44614d454574SPaolo Bonzini },{ 44624d454574SPaolo Bonzini .name = "werror", 44634d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 44644d454574SPaolo Bonzini .help = "write error action", 44654d454574SPaolo Bonzini },{ 44664e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 44674d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 44684d454574SPaolo Bonzini .help = "open drive file as read-only", 4469a2a7862cSPradeep Jagadeesh }, 4470a2a7862cSPradeep Jagadeesh 4471a2a7862cSPradeep Jagadeesh THROTTLE_OPTS, 4472a2a7862cSPradeep Jagadeesh 4473a2a7862cSPradeep Jagadeesh { 447476f4afb4SAlberto Garcia .name = "throttling.group", 447576f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 447676f4afb4SAlberto Garcia .help = "name of the block throttling group", 447776f4afb4SAlberto Garcia },{ 44784d454574SPaolo Bonzini .name = "copy-on-read", 44794d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 44804d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4481465bee1dSPeter Lieven },{ 4482465bee1dSPeter Lieven .name = "detect-zeroes", 4483465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4484465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4485362e9299SAlberto Garcia },{ 4486362e9299SAlberto Garcia .name = "stats-account-invalid", 4487362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4488362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4489362e9299SAlberto Garcia "in the statistics", 4490362e9299SAlberto Garcia },{ 4491362e9299SAlberto Garcia .name = "stats-account-failed", 4492362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4493362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4494362e9299SAlberto Garcia "in the statistics", 44954d454574SPaolo Bonzini }, 44964d454574SPaolo Bonzini { /* end of list */ } 44974d454574SPaolo Bonzini }, 44984d454574SPaolo Bonzini }; 44990006383eSKevin Wolf 45000006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 45010006383eSKevin Wolf .name = "drive", 45020006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 45030006383eSKevin Wolf .desc = { 4504492fdc6fSKevin Wolf /* 4505492fdc6fSKevin Wolf * no elements => accept any params 4506492fdc6fSKevin Wolf * validation will happen later 4507492fdc6fSKevin Wolf */ 45080006383eSKevin Wolf { /* end of list */ } 45090006383eSKevin Wolf }, 45100006383eSKevin Wolf }; 4511