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, 2076*a6e2ca5fSVladimir Sementsov-Ogievskiy action->has_x_disabled, action->x_disabled, 2077df9a681dSFam Zheng &local_err); 2078df9a681dSFam Zheng 2079df9a681dSFam Zheng if (!local_err) { 2080df9a681dSFam Zheng state->prepared = true; 2081df9a681dSFam Zheng } else { 2082df9a681dSFam Zheng error_propagate(errp, local_err); 2083df9a681dSFam Zheng } 2084df9a681dSFam Zheng } 2085df9a681dSFam Zheng 208650f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2087df9a681dSFam Zheng { 2088df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2089df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2090df9a681dSFam Zheng common, common); 2091df9a681dSFam Zheng 209232bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2093df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2094df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2095df9a681dSFam Zheng */ 2096df9a681dSFam Zheng if (state->prepared) { 2097df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2098df9a681dSFam Zheng } 2099df9a681dSFam Zheng } 2100df9a681dSFam Zheng 210150f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2102df9a681dSFam Zheng Error **errp) 2103df9a681dSFam Zheng { 2104df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2105df9a681dSFam Zheng common, common); 2106df9a681dSFam Zheng BlockDirtyBitmap *action; 2107df9a681dSFam Zheng 210894d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 210994d16a64SJohn Snow return; 211094d16a64SJohn Snow } 211194d16a64SJohn Snow 211232bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2113df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2114df9a681dSFam Zheng action->name, 2115df9a681dSFam Zheng &state->bs, 2116df9a681dSFam Zheng errp); 2117df9a681dSFam Zheng if (!state->bitmap) { 2118df9a681dSFam Zheng return; 2119df9a681dSFam Zheng } 2120df9a681dSFam Zheng 2121df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2122df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2123df9a681dSFam Zheng return; 21244f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(state->bitmap)) { 21254f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, "Cannot modify a locked bitmap"); 21264f43e953SVladimir Sementsov-Ogievskiy return; 2127df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2128df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2129df9a681dSFam Zheng return; 2130d6883bc9SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(state->bitmap)) { 2131d6883bc9SVladimir Sementsov-Ogievskiy error_setg(errp, "Cannot clear a readonly bitmap"); 2132d6883bc9SVladimir Sementsov-Ogievskiy return; 2133df9a681dSFam Zheng } 2134df9a681dSFam Zheng 2135df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2136df9a681dSFam Zheng } 2137df9a681dSFam Zheng 213850f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2139df9a681dSFam Zheng { 2140df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2141df9a681dSFam Zheng common, common); 2142df9a681dSFam Zheng 2143184dd9c4SJohn Snow if (state->backup) { 2144df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2145df9a681dSFam Zheng } 2146184dd9c4SJohn Snow } 2147df9a681dSFam Zheng 214850f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2149df9a681dSFam Zheng { 2150df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2151df9a681dSFam Zheng common, common); 2152df9a681dSFam Zheng 2153df9a681dSFam Zheng hbitmap_free(state->backup); 2154df9a681dSFam Zheng } 2155df9a681dSFam Zheng 2156c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_prepare(BlkActionState *common, 2157c6490447SVladimir Sementsov-Ogievskiy Error **errp) 2158c6490447SVladimir Sementsov-Ogievskiy { 2159c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmap *action; 2160c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2161c6490447SVladimir Sementsov-Ogievskiy common, common); 2162c6490447SVladimir Sementsov-Ogievskiy 2163c6490447SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 2164c6490447SVladimir Sementsov-Ogievskiy return; 2165c6490447SVladimir Sementsov-Ogievskiy } 2166c6490447SVladimir Sementsov-Ogievskiy 2167c6490447SVladimir Sementsov-Ogievskiy action = common->action->u.x_block_dirty_bitmap_enable.data; 2168c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, 2169c6490447SVladimir Sementsov-Ogievskiy action->name, 2170c6490447SVladimir Sementsov-Ogievskiy NULL, 2171c6490447SVladimir Sementsov-Ogievskiy errp); 2172c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) { 2173c6490447SVladimir Sementsov-Ogievskiy return; 2174c6490447SVladimir Sementsov-Ogievskiy } 2175c6490447SVladimir Sementsov-Ogievskiy 2176c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); 2177c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap); 2178c6490447SVladimir Sementsov-Ogievskiy } 2179c6490447SVladimir Sementsov-Ogievskiy 2180c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_abort(BlkActionState *common) 2181c6490447SVladimir Sementsov-Ogievskiy { 2182c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2183c6490447SVladimir Sementsov-Ogievskiy common, common); 2184c6490447SVladimir Sementsov-Ogievskiy 2185c6490447SVladimir Sementsov-Ogievskiy if (!state->was_enabled) { 2186c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap); 2187c6490447SVladimir Sementsov-Ogievskiy } 2188c6490447SVladimir Sementsov-Ogievskiy } 2189c6490447SVladimir Sementsov-Ogievskiy 2190c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_prepare(BlkActionState *common, 2191c6490447SVladimir Sementsov-Ogievskiy Error **errp) 2192c6490447SVladimir Sementsov-Ogievskiy { 2193c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmap *action; 2194c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2195c6490447SVladimir Sementsov-Ogievskiy common, common); 2196c6490447SVladimir Sementsov-Ogievskiy 2197c6490447SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 2198c6490447SVladimir Sementsov-Ogievskiy return; 2199c6490447SVladimir Sementsov-Ogievskiy } 2200c6490447SVladimir Sementsov-Ogievskiy 2201c6490447SVladimir Sementsov-Ogievskiy action = common->action->u.x_block_dirty_bitmap_disable.data; 2202c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, 2203c6490447SVladimir Sementsov-Ogievskiy action->name, 2204c6490447SVladimir Sementsov-Ogievskiy NULL, 2205c6490447SVladimir Sementsov-Ogievskiy errp); 2206c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) { 2207c6490447SVladimir Sementsov-Ogievskiy return; 2208c6490447SVladimir Sementsov-Ogievskiy } 2209c6490447SVladimir Sementsov-Ogievskiy 2210c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); 2211c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap); 2212c6490447SVladimir Sementsov-Ogievskiy } 2213c6490447SVladimir Sementsov-Ogievskiy 2214c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_abort(BlkActionState *common) 2215c6490447SVladimir Sementsov-Ogievskiy { 2216c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2217c6490447SVladimir Sementsov-Ogievskiy common, common); 2218c6490447SVladimir Sementsov-Ogievskiy 2219c6490447SVladimir Sementsov-Ogievskiy if (state->was_enabled) { 2220c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap); 2221c6490447SVladimir Sementsov-Ogievskiy } 2222c6490447SVladimir Sementsov-Ogievskiy } 2223c6490447SVladimir Sementsov-Ogievskiy 222450f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 222578b18b78SStefan Hajnoczi { 222678b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 222778b18b78SStefan Hajnoczi } 222878b18b78SStefan Hajnoczi 222950f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 223078b18b78SStefan Hajnoczi { 2231dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 223278b18b78SStefan Hajnoczi } 223378b18b78SStefan Hajnoczi 223450f43f0fSJohn Snow static const BlkActionOps actions[] = { 223543de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 223643de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 223743de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 223843de7e2dSAlberto Garcia .commit = external_snapshot_commit, 223943de7e2dSAlberto Garcia .abort = external_snapshot_abort, 22404ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 224143de7e2dSAlberto Garcia }, 2242c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2243ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2244ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2245ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2246ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2247da763e83SFam Zheng .clean = external_snapshot_clean, 2248ba0c86a3SWenchao Xia }, 22493037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 22503037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 22513037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 2252111049a4SJohn Snow .commit = drive_backup_commit, 22533037f364SStefan Hajnoczi .abort = drive_backup_abort, 22545d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 22553037f364SStefan Hajnoczi }, 2256bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2257bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2258bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2259111049a4SJohn Snow .commit = blockdev_backup_commit, 2260bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2261bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2262bd8baecdSFam Zheng }, 226378b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 226450f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 226578b18b78SStefan Hajnoczi .prepare = abort_prepare, 226678b18b78SStefan Hajnoczi .commit = abort_commit, 226778b18b78SStefan Hajnoczi }, 2268bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2269bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2270bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2271bbe86010SWenchao Xia .abort = internal_snapshot_abort, 22725d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2273bbe86010SWenchao Xia }, 2274df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2275df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2276df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2277df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2278df9a681dSFam Zheng }, 2279df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2280df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2281df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2282df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2283df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2284c6490447SVladimir Sementsov-Ogievskiy }, 2285c6490447SVladimir Sementsov-Ogievskiy [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_ENABLE] = { 2286c6490447SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 2287c6490447SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_enable_prepare, 2288c6490447SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_enable_abort, 2289c6490447SVladimir Sementsov-Ogievskiy }, 2290c6490447SVladimir Sementsov-Ogievskiy [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_DISABLE] = { 2291c6490447SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 2292c6490447SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_disable_prepare, 2293c6490447SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_disable_abort, 2294df9a681dSFam Zheng } 2295ba0c86a3SWenchao Xia }; 2296ba0c86a3SWenchao Xia 229794d16a64SJohn Snow /** 229894d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 229994d16a64SJohn Snow * that structure with desired defaults if they are unset. 230094d16a64SJohn Snow */ 230194d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 230294d16a64SJohn Snow TransactionProperties *props) 230394d16a64SJohn Snow { 230494d16a64SJohn Snow if (!props) { 230594d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 230694d16a64SJohn Snow } 230794d16a64SJohn Snow 230894d16a64SJohn Snow if (!props->has_completion_mode) { 230994d16a64SJohn Snow props->has_completion_mode = true; 231094d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 231194d16a64SJohn Snow } 231294d16a64SJohn Snow 231394d16a64SJohn Snow return props; 231494d16a64SJohn Snow } 231594d16a64SJohn Snow 23168802d1fdSJeff Cody /* 2317b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2318b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 23198802d1fdSJeff Cody */ 232094d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 232194d16a64SJohn Snow bool has_props, 232294d16a64SJohn Snow struct TransactionProperties *props, 232394d16a64SJohn Snow Error **errp) 23248802d1fdSJeff Cody { 2325c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 232662c9e416SKevin Wolf JobTxn *block_job_txn = NULL; 232750f43f0fSJohn Snow BlkActionState *state, *next; 232843e17041SLuiz Capitulino Error *local_err = NULL; 23298802d1fdSJeff Cody 233050f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 23318802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 23328802d1fdSJeff Cody 233394d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 233462c9e416SKevin Wolf * If not, we don't really need to make a JobTxn. 233594d16a64SJohn Snow */ 233694d16a64SJohn Snow props = get_transaction_properties(props); 233794d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 23387eaa8fb5SKevin Wolf block_job_txn = job_txn_new(); 233994d16a64SJohn Snow } 234094d16a64SJohn Snow 2341b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 23428802d1fdSJeff Cody bdrv_drain_all(); 23438802d1fdSJeff Cody 2344b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 23458802d1fdSJeff Cody while (NULL != dev_entry) { 2346c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 234750f43f0fSJohn Snow const BlkActionOps *ops; 234852e7c241SPaolo Bonzini 23498802d1fdSJeff Cody dev_info = dev_entry->value; 23508802d1fdSJeff Cody dev_entry = dev_entry->next; 23518802d1fdSJeff Cody 23526a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2353ba0c86a3SWenchao Xia 23546a8f9661SEric Blake ops = &actions[dev_info->type]; 2355aa3fe714SMax Reitz assert(ops->instance_size > 0); 2356aa3fe714SMax Reitz 2357ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2358ba5d6ab6SStefan Hajnoczi state->ops = ops; 2359ba5d6ab6SStefan Hajnoczi state->action = dev_info; 236094d16a64SJohn Snow state->block_job_txn = block_job_txn; 236194d16a64SJohn Snow state->txn_props = props; 2362ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 23638802d1fdSJeff Cody 2364ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 236584d18f06SMarkus Armbruster if (local_err) { 23669b9877eeSWenchao Xia error_propagate(errp, local_err); 23679b9877eeSWenchao Xia goto delete_and_fail; 23689b9877eeSWenchao Xia } 236952e7c241SPaolo Bonzini } 23708802d1fdSJeff Cody 2371ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2372f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2373ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 23748802d1fdSJeff Cody } 2375f9ea81e8SStefan Hajnoczi } 23768802d1fdSJeff Cody 23778802d1fdSJeff Cody /* success */ 23788802d1fdSJeff Cody goto exit; 23798802d1fdSJeff Cody 23808802d1fdSJeff Cody delete_and_fail: 2381b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2382ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2383ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2384ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2385ba0c86a3SWenchao Xia } 23868802d1fdSJeff Cody } 23878802d1fdSJeff Cody exit: 2388ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2389ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2390ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2391ba0c86a3SWenchao Xia } 2392ba5d6ab6SStefan Hajnoczi g_free(state); 23938802d1fdSJeff Cody } 239494d16a64SJohn Snow if (!has_props) { 239594d16a64SJohn Snow qapi_free_TransactionProperties(props); 239694d16a64SJohn Snow } 23977eaa8fb5SKevin Wolf job_txn_unref(block_job_txn); 23988802d1fdSJeff Cody } 23998802d1fdSJeff Cody 2400fbe2d816SKevin Wolf void qmp_eject(bool has_device, const char *device, 2401fbe2d816SKevin Wolf bool has_id, const char *id, 2402fbe2d816SKevin Wolf bool has_force, bool force, Error **errp) 2403666daa68SMarkus Armbruster { 240438f54bd1SMax Reitz Error *local_err = NULL; 24053a3086b7SJohn Snow int rc; 2406666daa68SMarkus Armbruster 24073a3086b7SJohn Snow if (!has_force) { 24083a3086b7SJohn Snow force = false; 24093a3086b7SJohn Snow } 24103a3086b7SJohn Snow 2411fbe2d816SKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2412fbe2d816SKevin Wolf has_id ? id : NULL, 2413fbe2d816SKevin Wolf force, &local_err); 2414bf18bee5SColin Lord if (rc && rc != -ENOSYS) { 241538f54bd1SMax Reitz error_propagate(errp, local_err); 2416c245b6a3SLuiz Capitulino return; 2417666daa68SMarkus Armbruster } 2418bf18bee5SColin Lord error_free(local_err); 24193a3086b7SJohn Snow 242082fcf66eSMax Reitz blockdev_remove_medium(has_device, device, has_id, id, errp); 2421666daa68SMarkus Armbruster } 2422666daa68SMarkus Armbruster 242312d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 242412d3ba82SBenoît Canet bool has_node_name, const char *node_name, 242512d3ba82SBenoît Canet const char *password, Error **errp) 2426666daa68SMarkus Armbruster { 2427c01c214bSDaniel P. Berrange error_setg(errp, 2428c01c214bSDaniel P. Berrange "Setting block passwords directly is no longer supported"); 2429e3442099SStefan Hajnoczi } 2430e3442099SStefan Hajnoczi 2431bf18bee5SColin Lord /* 2432bf18bee5SColin Lord * Attempt to open the tray of @device. 2433bf18bee5SColin Lord * If @force, ignore its tray lock. 2434bf18bee5SColin Lord * Else, if the tray is locked, don't open it, but ask the guest to open it. 2435bf18bee5SColin Lord * On error, store an error through @errp and return -errno. 2436bf18bee5SColin Lord * If @device does not exist, return -ENODEV. 2437bf18bee5SColin Lord * If it has no removable media, return -ENOTSUP. 2438bf18bee5SColin Lord * If it has no tray, return -ENOSYS. 2439bf18bee5SColin Lord * If the guest was asked to open the tray, return -EINPROGRESS. 2440bf18bee5SColin Lord * Else, return 0. 24413a3086b7SJohn Snow */ 2442b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id, 2443b33945cfSKevin Wolf bool force, Error **errp) 24447d8a9f71SMax Reitz { 24457d8a9f71SMax Reitz BlockBackend *blk; 2446b33945cfSKevin Wolf const char *device = qdev_id ?: blk_name; 24477d8a9f71SMax Reitz bool locked; 24487d8a9f71SMax Reitz 2449b33945cfSKevin Wolf blk = qmp_get_blk(blk_name, qdev_id, errp); 24507d8a9f71SMax Reitz if (!blk) { 24513a3086b7SJohn Snow return -ENODEV; 24527d8a9f71SMax Reitz } 24537d8a9f71SMax Reitz 24547d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 24557d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 24563a3086b7SJohn Snow return -ENOTSUP; 24577d8a9f71SMax Reitz } 24587d8a9f71SMax Reitz 245912c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 2460bf18bee5SColin Lord error_setg(errp, "Device '%s' does not have a tray", device); 2461bf18bee5SColin Lord return -ENOSYS; 246212c7ec87SMax Reitz } 246312c7ec87SMax Reitz 24647d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 24653a3086b7SJohn Snow return 0; 24667d8a9f71SMax Reitz } 24677d8a9f71SMax Reitz 24687d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 24697d8a9f71SMax Reitz if (locked) { 24707d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 24717d8a9f71SMax Reitz } 24727d8a9f71SMax Reitz 24737d8a9f71SMax Reitz if (!locked || force) { 247439829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 24757d8a9f71SMax Reitz } 24763a3086b7SJohn Snow 24773a3086b7SJohn Snow if (locked && !force) { 2478bf18bee5SColin Lord error_setg(errp, "Device '%s' is locked and force was not specified, " 2479bf18bee5SColin Lord "wait for tray to open and try again", device); 2480bf18bee5SColin Lord return -EINPROGRESS; 24813a3086b7SJohn Snow } 24823a3086b7SJohn Snow 24833a3086b7SJohn Snow return 0; 24843a3086b7SJohn Snow } 24853a3086b7SJohn Snow 2486b33945cfSKevin Wolf void qmp_blockdev_open_tray(bool has_device, const char *device, 2487b33945cfSKevin Wolf bool has_id, const char *id, 2488b33945cfSKevin Wolf bool has_force, bool force, 24893a3086b7SJohn Snow Error **errp) 24903a3086b7SJohn Snow { 2491bf18bee5SColin Lord Error *local_err = NULL; 2492bf18bee5SColin Lord int rc; 2493bf18bee5SColin Lord 24943a3086b7SJohn Snow if (!has_force) { 24953a3086b7SJohn Snow force = false; 24963a3086b7SJohn Snow } 2497b33945cfSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2498b33945cfSKevin Wolf has_id ? id : NULL, 2499b33945cfSKevin Wolf force, &local_err); 2500bf18bee5SColin Lord if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { 2501bf18bee5SColin Lord error_propagate(errp, local_err); 2502bf18bee5SColin Lord return; 2503bf18bee5SColin Lord } 2504bf18bee5SColin Lord error_free(local_err); 25057d8a9f71SMax Reitz } 25067d8a9f71SMax Reitz 2507b33945cfSKevin Wolf void qmp_blockdev_close_tray(bool has_device, const char *device, 2508b33945cfSKevin Wolf bool has_id, const char *id, 2509b33945cfSKevin Wolf Error **errp) 2510abaaf59dSMax Reitz { 2511abaaf59dSMax Reitz BlockBackend *blk; 251239829a01SKevin Wolf Error *local_err = NULL; 2513abaaf59dSMax Reitz 2514b33945cfSKevin Wolf device = has_device ? device : NULL; 2515b33945cfSKevin Wolf id = has_id ? id : NULL; 2516b33945cfSKevin Wolf 2517b33945cfSKevin Wolf blk = qmp_get_blk(device, id, errp); 2518abaaf59dSMax Reitz if (!blk) { 2519abaaf59dSMax Reitz return; 2520abaaf59dSMax Reitz } 2521abaaf59dSMax Reitz 2522abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2523b33945cfSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 2524abaaf59dSMax Reitz return; 2525abaaf59dSMax Reitz } 2526abaaf59dSMax Reitz 252712c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 252812c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 252912c7ec87SMax Reitz return; 253012c7ec87SMax Reitz } 253112c7ec87SMax Reitz 2532abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2533abaaf59dSMax Reitz return; 2534abaaf59dSMax Reitz } 2535abaaf59dSMax Reitz 253639829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 253739829a01SKevin Wolf if (local_err) { 253839829a01SKevin Wolf error_propagate(errp, local_err); 253939829a01SKevin Wolf return; 254039829a01SKevin Wolf } 2541abaaf59dSMax Reitz } 2542abaaf59dSMax Reitz 254382fcf66eSMax Reitz static void blockdev_remove_medium(bool has_device, const char *device, 254400949babSKevin Wolf bool has_id, const char *id, Error **errp) 25452814f672SMax Reitz { 25462814f672SMax Reitz BlockBackend *blk; 25472814f672SMax Reitz BlockDriverState *bs; 25482814f672SMax Reitz AioContext *aio_context; 254900949babSKevin Wolf bool has_attached_device; 25502814f672SMax Reitz 255100949babSKevin Wolf device = has_device ? device : NULL; 255200949babSKevin Wolf id = has_id ? id : NULL; 255300949babSKevin Wolf 255400949babSKevin Wolf blk = qmp_get_blk(device, id, errp); 25552814f672SMax Reitz if (!blk) { 25562814f672SMax Reitz return; 25572814f672SMax Reitz } 25582814f672SMax Reitz 25592814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 256000949babSKevin Wolf has_attached_device = blk_get_attached_dev(blk); 25612814f672SMax Reitz 256200949babSKevin Wolf if (has_attached_device && !blk_dev_has_removable_media(blk)) { 256300949babSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 25642814f672SMax Reitz return; 25652814f672SMax Reitz } 25662814f672SMax Reitz 256700949babSKevin Wolf if (has_attached_device && blk_dev_has_tray(blk) && 256800949babSKevin Wolf !blk_dev_is_tray_open(blk)) 256900949babSKevin Wolf { 257000949babSKevin Wolf error_setg(errp, "Tray of device '%s' is not open", device ?: id); 25712814f672SMax Reitz return; 25722814f672SMax Reitz } 25732814f672SMax Reitz 25742814f672SMax Reitz bs = blk_bs(blk); 25752814f672SMax Reitz if (!bs) { 25762814f672SMax Reitz return; 25772814f672SMax Reitz } 25782814f672SMax Reitz 25792814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 25802814f672SMax Reitz aio_context_acquire(aio_context); 25812814f672SMax Reitz 25822814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 25832814f672SMax Reitz goto out; 25842814f672SMax Reitz } 25852814f672SMax Reitz 25862814f672SMax Reitz blk_remove_bs(blk); 25872814f672SMax Reitz 258812c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 258912c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 259012c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 259112c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 259212c7ec87SMax Reitz * value passed here (i.e. false). */ 259339829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 259412c7ec87SMax Reitz } 259512c7ec87SMax Reitz 25962814f672SMax Reitz out: 25972814f672SMax Reitz aio_context_release(aio_context); 25982814f672SMax Reitz } 25992814f672SMax Reitz 260034ce1111SMax Reitz void qmp_blockdev_remove_medium(const char *id, Error **errp) 260182fcf66eSMax Reitz { 260282fcf66eSMax Reitz blockdev_remove_medium(false, NULL, true, id, errp); 260382fcf66eSMax Reitz } 260482fcf66eSMax Reitz 2605716df217SKevin Wolf static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, 2606d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2607d1299882SMax Reitz { 260839829a01SKevin Wolf Error *local_err = NULL; 2609d1299882SMax Reitz bool has_device; 2610d7086422SKevin Wolf int ret; 2611d1299882SMax Reitz 2612d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2613d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2614d1299882SMax Reitz 2615d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2616716df217SKevin Wolf error_setg(errp, "Device is not removable"); 2617d1299882SMax Reitz return; 2618d1299882SMax Reitz } 2619d1299882SMax Reitz 262012c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2621716df217SKevin Wolf error_setg(errp, "Tray of the device is not open"); 2622d1299882SMax Reitz return; 2623d1299882SMax Reitz } 2624d1299882SMax Reitz 2625d1299882SMax Reitz if (blk_bs(blk)) { 2626716df217SKevin Wolf error_setg(errp, "There already is a medium in the device"); 2627d1299882SMax Reitz return; 2628d1299882SMax Reitz } 2629d1299882SMax Reitz 2630d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 2631d7086422SKevin Wolf if (ret < 0) { 2632d7086422SKevin Wolf return; 2633d7086422SKevin Wolf } 2634d1299882SMax Reitz 263512c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 263612c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 263712c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 263812c7ec87SMax Reitz * slot here. 263912c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 264012c7ec87SMax Reitz * value passed here (i.e. true). */ 264139829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 264239829a01SKevin Wolf if (local_err) { 264339829a01SKevin Wolf error_propagate(errp, local_err); 264439829a01SKevin Wolf blk_remove_bs(blk); 264539829a01SKevin Wolf return; 264639829a01SKevin Wolf } 264712c7ec87SMax Reitz } 2648d1299882SMax Reitz } 2649d1299882SMax Reitz 265082fcf66eSMax Reitz static void blockdev_insert_medium(bool has_device, const char *device, 2651716df217SKevin Wolf bool has_id, const char *id, 2652716df217SKevin Wolf const char *node_name, Error **errp) 2653d1299882SMax Reitz { 2654716df217SKevin Wolf BlockBackend *blk; 2655d1299882SMax Reitz BlockDriverState *bs; 2656d1299882SMax Reitz 2657716df217SKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 2658716df217SKevin Wolf has_id ? id : NULL, 2659716df217SKevin Wolf errp); 2660716df217SKevin Wolf if (!blk) { 2661716df217SKevin Wolf return; 2662716df217SKevin Wolf } 2663716df217SKevin Wolf 2664d1299882SMax Reitz bs = bdrv_find_node(node_name); 2665d1299882SMax Reitz if (!bs) { 2666d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2667d1299882SMax Reitz return; 2668d1299882SMax Reitz } 2669d1299882SMax Reitz 26701f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2671e467da7bSKevin Wolf error_setg(errp, "Node '%s' is already in use", node_name); 2672d1299882SMax Reitz return; 2673d1299882SMax Reitz } 2674d1299882SMax Reitz 2675716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, bs, errp); 2676d1299882SMax Reitz } 2677d1299882SMax Reitz 267834ce1111SMax Reitz void qmp_blockdev_insert_medium(const char *id, const char *node_name, 267982fcf66eSMax Reitz Error **errp) 268082fcf66eSMax Reitz { 268182fcf66eSMax Reitz blockdev_insert_medium(false, NULL, true, id, node_name, errp); 268282fcf66eSMax Reitz } 268382fcf66eSMax Reitz 268470e2cb3bSKevin Wolf void qmp_blockdev_change_medium(bool has_device, const char *device, 268570e2cb3bSKevin Wolf bool has_id, const char *id, 268670e2cb3bSKevin Wolf const char *filename, 268724fb4133SMax Reitz bool has_format, const char *format, 268839ff43d9SMax Reitz bool has_read_only, 268939ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 269024fb4133SMax Reitz Error **errp) 2691de2c6c05SMax Reitz { 2692de2c6c05SMax Reitz BlockBackend *blk; 2693de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 26945b363937SMax Reitz int bdrv_flags; 2695b85114f8SKevin Wolf bool detect_zeroes; 269638a53d50SColin Lord int rc; 2697de2c6c05SMax Reitz QDict *options = NULL; 2698de2c6c05SMax Reitz Error *err = NULL; 2699de2c6c05SMax Reitz 270070e2cb3bSKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 270170e2cb3bSKevin Wolf has_id ? id : NULL, 270270e2cb3bSKevin Wolf errp); 2703de2c6c05SMax Reitz if (!blk) { 2704de2c6c05SMax Reitz goto fail; 2705de2c6c05SMax Reitz } 2706de2c6c05SMax Reitz 2707de2c6c05SMax Reitz if (blk_bs(blk)) { 2708de2c6c05SMax Reitz blk_update_root_state(blk); 2709de2c6c05SMax Reitz } 2710de2c6c05SMax Reitz 2711de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2712156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2713156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2714de2c6c05SMax Reitz 271539ff43d9SMax Reitz if (!has_read_only) { 271639ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 271739ff43d9SMax Reitz } 271839ff43d9SMax Reitz 271939ff43d9SMax Reitz switch (read_only) { 272039ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 272139ff43d9SMax Reitz break; 272239ff43d9SMax Reitz 272339ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 272439ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 272539ff43d9SMax Reitz break; 272639ff43d9SMax Reitz 272739ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 272839ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 272939ff43d9SMax Reitz break; 273039ff43d9SMax Reitz 273139ff43d9SMax Reitz default: 273239ff43d9SMax Reitz abort(); 273339ff43d9SMax Reitz } 273439ff43d9SMax Reitz 2735de2c6c05SMax Reitz options = qdict_new(); 2736b85114f8SKevin Wolf detect_zeroes = blk_get_detect_zeroes_from_root_state(blk); 273746f5ac20SEric Blake qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off"); 2738b85114f8SKevin Wolf 2739b85114f8SKevin Wolf if (has_format) { 274046f5ac20SEric Blake qdict_put_str(options, "driver", format); 2741de2c6c05SMax Reitz } 2742de2c6c05SMax Reitz 27435b363937SMax Reitz medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 27445b363937SMax Reitz if (!medium_bs) { 2745de2c6c05SMax Reitz goto fail; 2746de2c6c05SMax Reitz } 2747de2c6c05SMax Reitz 274870e2cb3bSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 274970e2cb3bSKevin Wolf has_id ? id : NULL, 275070e2cb3bSKevin Wolf false, &err); 275138a53d50SColin Lord if (rc && rc != -ENOSYS) { 2752de2c6c05SMax Reitz error_propagate(errp, err); 2753de2c6c05SMax Reitz goto fail; 2754de2c6c05SMax Reitz } 275538a53d50SColin Lord error_free(err); 275638a53d50SColin Lord err = NULL; 2757de2c6c05SMax Reitz 275882fcf66eSMax Reitz blockdev_remove_medium(has_device, device, has_id, id, &err); 2759de2c6c05SMax Reitz if (err) { 2760de2c6c05SMax Reitz error_propagate(errp, err); 2761de2c6c05SMax Reitz goto fail; 2762de2c6c05SMax Reitz } 2763de2c6c05SMax Reitz 2764716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, medium_bs, &err); 2765de2c6c05SMax Reitz if (err) { 2766de2c6c05SMax Reitz error_propagate(errp, err); 2767de2c6c05SMax Reitz goto fail; 2768de2c6c05SMax Reitz } 2769de2c6c05SMax Reitz 277070e2cb3bSKevin Wolf qmp_blockdev_close_tray(has_device, device, has_id, id, errp); 2771de2c6c05SMax Reitz 2772de2c6c05SMax Reitz fail: 2773de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2774de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2775de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2776de2c6c05SMax Reitz bdrv_unref(medium_bs); 2777de2c6c05SMax Reitz } 2778de2c6c05SMax Reitz 2779727f005eSZhi Yong Wu /* throttling disk I/O limits */ 27804dc9397bSEric Blake void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) 2781727f005eSZhi Yong Wu { 2782cc0681c4SBenoît Canet ThrottleConfig cfg; 2783727f005eSZhi Yong Wu BlockDriverState *bs; 2784a0e8544cSFam Zheng BlockBackend *blk; 2785b15446fdSStefan Hajnoczi AioContext *aio_context; 2786727f005eSZhi Yong Wu 27877a9877a0SKevin Wolf blk = qmp_get_blk(arg->has_device ? arg->device : NULL, 27887a9877a0SKevin Wolf arg->has_id ? arg->id : NULL, 27897a9877a0SKevin Wolf errp); 2790a0e8544cSFam Zheng if (!blk) { 279180047da5SLuiz Capitulino return; 2792727f005eSZhi Yong Wu } 27935433c24fSMax Reitz 27945433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 27955433c24fSMax Reitz aio_context_acquire(aio_context); 27965433c24fSMax Reitz 2797a0e8544cSFam Zheng bs = blk_bs(blk); 27985433c24fSMax Reitz if (!bs) { 27997a9877a0SKevin Wolf error_setg(errp, "Device has no medium"); 28005433c24fSMax Reitz goto out; 28015433c24fSMax Reitz } 2802727f005eSZhi Yong Wu 28031588ab5dSAlberto Garcia throttle_config_init(&cfg); 28044dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; 28054dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; 28064dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; 2807727f005eSZhi Yong Wu 28084dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; 28094dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; 28104dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; 2811cc0681c4SBenoît Canet 28124dc9397bSEric Blake if (arg->has_bps_max) { 28134dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; 28143e9fab69SBenoît Canet } 28154dc9397bSEric Blake if (arg->has_bps_rd_max) { 28164dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; 28173e9fab69SBenoît Canet } 28184dc9397bSEric Blake if (arg->has_bps_wr_max) { 28194dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; 28203e9fab69SBenoît Canet } 28214dc9397bSEric Blake if (arg->has_iops_max) { 28224dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; 28233e9fab69SBenoît Canet } 28244dc9397bSEric Blake if (arg->has_iops_rd_max) { 28254dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; 28263e9fab69SBenoît Canet } 28274dc9397bSEric Blake if (arg->has_iops_wr_max) { 28284dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; 28293e9fab69SBenoît Canet } 2830cc0681c4SBenoît Canet 28314dc9397bSEric Blake if (arg->has_bps_max_length) { 28324dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; 2833dce13204SAlberto Garcia } 28344dc9397bSEric Blake if (arg->has_bps_rd_max_length) { 28354dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; 2836dce13204SAlberto Garcia } 28374dc9397bSEric Blake if (arg->has_bps_wr_max_length) { 28384dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; 2839dce13204SAlberto Garcia } 28404dc9397bSEric Blake if (arg->has_iops_max_length) { 28414dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; 2842dce13204SAlberto Garcia } 28434dc9397bSEric Blake if (arg->has_iops_rd_max_length) { 28444dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; 2845dce13204SAlberto Garcia } 28464dc9397bSEric Blake if (arg->has_iops_wr_max_length) { 28474dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; 2848dce13204SAlberto Garcia } 2849dce13204SAlberto Garcia 28504dc9397bSEric Blake if (arg->has_iops_size) { 28514dc9397bSEric Blake cfg.op_size = arg->iops_size; 28522024c1dfSBenoît Canet } 2853cc0681c4SBenoît Canet 2854d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 28555433c24fSMax Reitz goto out; 2856727f005eSZhi Yong Wu } 2857727f005eSZhi Yong Wu 285876f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 285976f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 286076f4afb4SAlberto Garcia * just update the throttling group. */ 2861022cdc9fSManos Pitsidianakis if (!blk_get_public(blk)->throttle_group_member.throttle_state) { 28624dc9397bSEric Blake blk_io_limits_enable(blk, 28637a9877a0SKevin Wolf arg->has_group ? arg->group : 28647a9877a0SKevin Wolf arg->has_device ? arg->device : 28657a9877a0SKevin Wolf arg->id); 28664dc9397bSEric Blake } else if (arg->has_group) { 28674dc9397bSEric Blake blk_io_limits_update_group(blk, arg->group); 2868727f005eSZhi Yong Wu } 286976f4afb4SAlberto Garcia /* Set the new throttling configuration */ 287097148076SKevin Wolf blk_set_io_limits(blk, &cfg); 2871022cdc9fSManos Pitsidianakis } else if (blk_get_public(blk)->throttle_group_member.throttle_state) { 287276f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 287397148076SKevin Wolf blk_io_limits_disable(blk); 2874727f005eSZhi Yong Wu } 2875b15446fdSStefan Hajnoczi 28765433c24fSMax Reitz out: 2877b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2878727f005eSZhi Yong Wu } 2879727f005eSZhi Yong Wu 2880341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2881341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2882fd5ae4ccSVladimir Sementsov-Ogievskiy bool has_persistent, bool persistent, 2883eb738bb5SVladimir Sementsov-Ogievskiy bool has_autoload, bool autoload, 2884*a6e2ca5fSVladimir Sementsov-Ogievskiy bool has_disabled, bool disabled, 2885341ebc2fSJohn Snow Error **errp) 2886341ebc2fSJohn Snow { 2887341ebc2fSJohn Snow BlockDriverState *bs; 2888fd5ae4ccSVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 2889341ebc2fSJohn Snow 2890341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2891341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2892341ebc2fSJohn Snow return; 2893341ebc2fSJohn Snow } 2894341ebc2fSJohn Snow 2895341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2896341ebc2fSJohn Snow if (!bs) { 2897341ebc2fSJohn Snow return; 2898341ebc2fSJohn Snow } 2899341ebc2fSJohn Snow 2900341ebc2fSJohn Snow if (has_granularity) { 2901341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2902341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2903341ebc2fSJohn Snow "and at least 512"); 29042119882cSPaolo Bonzini return; 2905341ebc2fSJohn Snow } 2906341ebc2fSJohn Snow } else { 2907341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2908341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2909341ebc2fSJohn Snow } 2910341ebc2fSJohn Snow 2911fd5ae4ccSVladimir Sementsov-Ogievskiy if (!has_persistent) { 2912fd5ae4ccSVladimir Sementsov-Ogievskiy persistent = false; 2913fd5ae4ccSVladimir Sementsov-Ogievskiy } 2914eb738bb5SVladimir Sementsov-Ogievskiy 29153e99da5eSVladimir Sementsov-Ogievskiy if (has_autoload) { 29163e99da5eSVladimir Sementsov-Ogievskiy warn_report("Autoload option is deprecated and its value is ignored"); 2917eb738bb5SVladimir Sementsov-Ogievskiy } 2918fd5ae4ccSVladimir Sementsov-Ogievskiy 2919*a6e2ca5fSVladimir Sementsov-Ogievskiy if (!has_disabled) { 2920*a6e2ca5fSVladimir Sementsov-Ogievskiy disabled = false; 2921*a6e2ca5fSVladimir Sementsov-Ogievskiy } 2922*a6e2ca5fSVladimir Sementsov-Ogievskiy 2923fd5ae4ccSVladimir Sementsov-Ogievskiy if (persistent && 2924fd5ae4ccSVladimir Sementsov-Ogievskiy !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp)) 2925fd5ae4ccSVladimir Sementsov-Ogievskiy { 2926fd5ae4ccSVladimir Sementsov-Ogievskiy return; 2927fd5ae4ccSVladimir Sementsov-Ogievskiy } 2928fd5ae4ccSVladimir Sementsov-Ogievskiy 2929fd5ae4ccSVladimir Sementsov-Ogievskiy bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2930eb738bb5SVladimir Sementsov-Ogievskiy if (bitmap == NULL) { 2931eb738bb5SVladimir Sementsov-Ogievskiy return; 2932fd5ae4ccSVladimir Sementsov-Ogievskiy } 2933eb738bb5SVladimir Sementsov-Ogievskiy 2934*a6e2ca5fSVladimir Sementsov-Ogievskiy if (disabled) { 2935*a6e2ca5fSVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(bitmap); 2936*a6e2ca5fSVladimir Sementsov-Ogievskiy } 2937*a6e2ca5fSVladimir Sementsov-Ogievskiy 2938eb738bb5SVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_set_persistance(bitmap, persistent); 2939341ebc2fSJohn Snow } 2940341ebc2fSJohn Snow 2941341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2942341ebc2fSJohn Snow Error **errp) 2943341ebc2fSJohn Snow { 2944341ebc2fSJohn Snow BlockDriverState *bs; 2945341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 29465c36c1afSVladimir Sementsov-Ogievskiy Error *local_err = NULL; 2947341ebc2fSJohn Snow 29482119882cSPaolo Bonzini bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2949341ebc2fSJohn Snow if (!bitmap || !bs) { 2950341ebc2fSJohn Snow return; 2951341ebc2fSJohn Snow } 2952341ebc2fSJohn Snow 29539bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 29549bd2b08fSJohn Snow error_setg(errp, 29559bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 29569bd2b08fSJohn Snow name); 29572119882cSPaolo Bonzini return; 29584f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) { 29594f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 29604f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be removed", 29614f43e953SVladimir Sementsov-Ogievskiy name); 29624f43e953SVladimir Sementsov-Ogievskiy return; 29639bd2b08fSJohn Snow } 29645c36c1afSVladimir Sementsov-Ogievskiy 29655c36c1afSVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_get_persistance(bitmap)) { 29665c36c1afSVladimir Sementsov-Ogievskiy bdrv_remove_persistent_dirty_bitmap(bs, name, &local_err); 29675c36c1afSVladimir Sementsov-Ogievskiy if (local_err != NULL) { 29685c36c1afSVladimir Sementsov-Ogievskiy error_propagate(errp, local_err); 29695c36c1afSVladimir Sementsov-Ogievskiy return; 29705c36c1afSVladimir Sementsov-Ogievskiy } 29715c36c1afSVladimir Sementsov-Ogievskiy } 29725c36c1afSVladimir Sementsov-Ogievskiy 2973341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2974341ebc2fSJohn Snow } 2975341ebc2fSJohn Snow 2976e74e6b78SJohn Snow /** 2977e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2978e74e6b78SJohn Snow * immediately after a full backup operation. 2979e74e6b78SJohn Snow */ 2980e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2981e74e6b78SJohn Snow Error **errp) 2982e74e6b78SJohn Snow { 2983e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2984e74e6b78SJohn Snow BlockDriverState *bs; 2985e74e6b78SJohn Snow 29862119882cSPaolo Bonzini bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2987e74e6b78SJohn Snow if (!bitmap || !bs) { 2988e74e6b78SJohn Snow return; 2989e74e6b78SJohn Snow } 2990e74e6b78SJohn Snow 2991e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2992e74e6b78SJohn Snow error_setg(errp, 2993e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2994e74e6b78SJohn Snow name); 29952119882cSPaolo Bonzini return; 29964f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) { 29974f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 29984f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be modified", 29994f43e953SVladimir Sementsov-Ogievskiy name); 30004f43e953SVladimir Sementsov-Ogievskiy return; 3001e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 3002e74e6b78SJohn Snow error_setg(errp, 3003e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 3004e74e6b78SJohn Snow name); 30052119882cSPaolo Bonzini return; 3006d6883bc9SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(bitmap)) { 3007d6883bc9SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap '%s' is readonly and cannot be cleared", name); 3008d6883bc9SVladimir Sementsov-Ogievskiy return; 3009e74e6b78SJohn Snow } 3010e74e6b78SJohn Snow 3011df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 3012e74e6b78SJohn Snow } 3013e74e6b78SJohn Snow 30145c5d2e50SVladimir Sementsov-Ogievskiy void qmp_x_block_dirty_bitmap_enable(const char *node, const char *name, 30155c5d2e50SVladimir Sementsov-Ogievskiy Error **errp) 30165c5d2e50SVladimir Sementsov-Ogievskiy { 30175c5d2e50SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 30185c5d2e50SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 30195c5d2e50SVladimir Sementsov-Ogievskiy 30205c5d2e50SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 30215c5d2e50SVladimir Sementsov-Ogievskiy if (!bitmap) { 30225c5d2e50SVladimir Sementsov-Ogievskiy return; 30235c5d2e50SVladimir Sementsov-Ogievskiy } 30245c5d2e50SVladimir Sementsov-Ogievskiy 30255c5d2e50SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_frozen(bitmap)) { 30265c5d2e50SVladimir Sementsov-Ogievskiy error_setg(errp, 30275c5d2e50SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently frozen and cannot be enabled", 30285c5d2e50SVladimir Sementsov-Ogievskiy name); 30295c5d2e50SVladimir Sementsov-Ogievskiy return; 30305c5d2e50SVladimir Sementsov-Ogievskiy } 30315c5d2e50SVladimir Sementsov-Ogievskiy 30325c5d2e50SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(bitmap); 30335c5d2e50SVladimir Sementsov-Ogievskiy } 30345c5d2e50SVladimir Sementsov-Ogievskiy 30355c5d2e50SVladimir Sementsov-Ogievskiy void qmp_x_block_dirty_bitmap_disable(const char *node, const char *name, 30365c5d2e50SVladimir Sementsov-Ogievskiy Error **errp) 30375c5d2e50SVladimir Sementsov-Ogievskiy { 30385c5d2e50SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 30395c5d2e50SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 30405c5d2e50SVladimir Sementsov-Ogievskiy 30415c5d2e50SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 30425c5d2e50SVladimir Sementsov-Ogievskiy if (!bitmap) { 30435c5d2e50SVladimir Sementsov-Ogievskiy return; 30445c5d2e50SVladimir Sementsov-Ogievskiy } 30455c5d2e50SVladimir Sementsov-Ogievskiy 30465c5d2e50SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_frozen(bitmap)) { 30475c5d2e50SVladimir Sementsov-Ogievskiy error_setg(errp, 30485c5d2e50SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently frozen and cannot be disabled", 30495c5d2e50SVladimir Sementsov-Ogievskiy name); 30505c5d2e50SVladimir Sementsov-Ogievskiy return; 30515c5d2e50SVladimir Sementsov-Ogievskiy } 30525c5d2e50SVladimir Sementsov-Ogievskiy 30535c5d2e50SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(bitmap); 30545c5d2e50SVladimir Sementsov-Ogievskiy } 30555c5d2e50SVladimir Sementsov-Ogievskiy 3056b598e531SVladimir Sementsov-Ogievskiy void qmp_x_block_dirty_bitmap_merge(const char *node, const char *dst_name, 3057b598e531SVladimir Sementsov-Ogievskiy const char *src_name, Error **errp) 3058b598e531SVladimir Sementsov-Ogievskiy { 3059b598e531SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 3060b598e531SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *dst, *src; 3061b598e531SVladimir Sementsov-Ogievskiy 3062b598e531SVladimir Sementsov-Ogievskiy dst = block_dirty_bitmap_lookup(node, dst_name, &bs, errp); 3063b598e531SVladimir Sementsov-Ogievskiy if (!dst) { 3064b598e531SVladimir Sementsov-Ogievskiy return; 3065b598e531SVladimir Sementsov-Ogievskiy } 3066b598e531SVladimir Sementsov-Ogievskiy 3067b598e531SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_frozen(dst)) { 3068b598e531SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap '%s' is frozen and cannot be modified", 3069b598e531SVladimir Sementsov-Ogievskiy dst_name); 3070b598e531SVladimir Sementsov-Ogievskiy return; 3071b598e531SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(dst)) { 3072b598e531SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap '%s' is readonly and cannot be modified", 3073b598e531SVladimir Sementsov-Ogievskiy dst_name); 3074b598e531SVladimir Sementsov-Ogievskiy return; 3075b598e531SVladimir Sementsov-Ogievskiy } 3076b598e531SVladimir Sementsov-Ogievskiy 3077b598e531SVladimir Sementsov-Ogievskiy src = bdrv_find_dirty_bitmap(bs, src_name); 3078b598e531SVladimir Sementsov-Ogievskiy if (!src) { 3079b598e531SVladimir Sementsov-Ogievskiy error_setg(errp, "Dirty bitmap '%s' not found", src_name); 3080b598e531SVladimir Sementsov-Ogievskiy return; 3081b598e531SVladimir Sementsov-Ogievskiy } 3082b598e531SVladimir Sementsov-Ogievskiy 3083b598e531SVladimir Sementsov-Ogievskiy bdrv_merge_dirty_bitmap(dst, src, errp); 3084b598e531SVladimir Sementsov-Ogievskiy } 3085b598e531SVladimir Sementsov-Ogievskiy 3086a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, 3087a3b52535SVladimir Sementsov-Ogievskiy const char *name, 3088a3b52535SVladimir Sementsov-Ogievskiy Error **errp) 3089a3b52535SVladimir Sementsov-Ogievskiy { 3090a3b52535SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 3091a3b52535SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 3092a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *ret = NULL; 3093a3b52535SVladimir Sementsov-Ogievskiy char *sha256; 3094a3b52535SVladimir Sementsov-Ogievskiy 3095a3b52535SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 3096a3b52535SVladimir Sementsov-Ogievskiy if (!bitmap || !bs) { 3097a3b52535SVladimir Sementsov-Ogievskiy return NULL; 3098a3b52535SVladimir Sementsov-Ogievskiy } 3099a3b52535SVladimir Sementsov-Ogievskiy 3100a3b52535SVladimir Sementsov-Ogievskiy sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp); 3101a3b52535SVladimir Sementsov-Ogievskiy if (sha256 == NULL) { 3102a3b52535SVladimir Sementsov-Ogievskiy return NULL; 3103a3b52535SVladimir Sementsov-Ogievskiy } 3104a3b52535SVladimir Sementsov-Ogievskiy 3105a3b52535SVladimir Sementsov-Ogievskiy ret = g_new(BlockDirtyBitmapSha256, 1); 3106a3b52535SVladimir Sementsov-Ogievskiy ret->sha256 = sha256; 3107a3b52535SVladimir Sementsov-Ogievskiy 3108a3b52535SVladimir Sementsov-Ogievskiy return ret; 3109a3b52535SVladimir Sementsov-Ogievskiy } 3110a3b52535SVladimir Sementsov-Ogievskiy 3111072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 31129063f814SRyan Harper { 31139063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 31147e7d56d9SMarkus Armbruster BlockBackend *blk; 31159063f814SRyan Harper BlockDriverState *bs; 31168ad4202bSStefan Hajnoczi AioContext *aio_context; 31173718d8abSFam Zheng Error *local_err = NULL; 31189063f814SRyan Harper 31192073d410SKevin Wolf bs = bdrv_find_node(id); 31202073d410SKevin Wolf if (bs) { 312179b7a77eSMarkus Armbruster qmp_blockdev_del(id, &local_err); 31222073d410SKevin Wolf if (local_err) { 31232073d410SKevin Wolf error_report_err(local_err); 31242073d410SKevin Wolf } 31252073d410SKevin Wolf return; 31262073d410SKevin Wolf } 31272073d410SKevin Wolf 31287e7d56d9SMarkus Armbruster blk = blk_by_name(id); 31297e7d56d9SMarkus Armbruster if (!blk) { 3130b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 3131072ebe6bSMarkus Armbruster return; 31329063f814SRyan Harper } 31338ad4202bSStefan Hajnoczi 313426f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 313548f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 313648f364ddSMarkus Armbruster " is not supported"); 3137072ebe6bSMarkus Armbruster return; 313848f364ddSMarkus Armbruster } 313948f364ddSMarkus Armbruster 31405433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 31418ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 31428ad4202bSStefan Hajnoczi 31435433c24fSMax Reitz bs = blk_bs(blk); 31445433c24fSMax Reitz if (bs) { 31453718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 3146565f65d2SMarkus Armbruster error_report_err(local_err); 31478ad4202bSStefan Hajnoczi aio_context_release(aio_context); 3148072ebe6bSMarkus Armbruster return; 31498591675fSMarcelo Tosatti } 31509063f814SRyan Harper 3151938abd43SMax Reitz blk_remove_bs(blk); 31525433c24fSMax Reitz } 31539063f814SRyan Harper 31547c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 3155efaa7c4eSMax Reitz monitor_remove_blk(blk); 3156efaa7c4eSMax Reitz 31577c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 31587c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 3159d22b2f41SRyan Harper */ 3160a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 3161293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 3162373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 3163293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 3164d22b2f41SRyan Harper } else { 3165b9fe8a7aSMarkus Armbruster blk_unref(blk); 3166d22b2f41SRyan Harper } 31679063f814SRyan Harper 31688ad4202bSStefan Hajnoczi aio_context_release(aio_context); 31699063f814SRyan Harper } 31706d4a2b3aSChristoph Hellwig 31713b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 31723b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 31733b1dbd11SBenoît Canet int64_t size, Error **errp) 31746d4a2b3aSChristoph Hellwig { 31753b1dbd11SBenoît Canet Error *local_err = NULL; 31767dad9ee6SKevin Wolf BlockBackend *blk = NULL; 31776d4a2b3aSChristoph Hellwig BlockDriverState *bs; 3178927e0e76SStefan Hajnoczi AioContext *aio_context; 31798732901eSKevin Wolf int ret; 31806d4a2b3aSChristoph Hellwig 31813b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 31823b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 31833b1dbd11SBenoît Canet &local_err); 318484d18f06SMarkus Armbruster if (local_err) { 31853b1dbd11SBenoît Canet error_propagate(errp, local_err); 31863b1dbd11SBenoît Canet return; 31873b1dbd11SBenoît Canet } 31883b1dbd11SBenoît Canet 3189927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 3190927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 3191927e0e76SStefan Hajnoczi 31923b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 3193c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 3194927e0e76SStefan Hajnoczi goto out; 31956d4a2b3aSChristoph Hellwig } 31966d4a2b3aSChristoph Hellwig 31976d4a2b3aSChristoph Hellwig if (size < 0) { 3198c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 3199927e0e76SStefan Hajnoczi goto out; 32006d4a2b3aSChristoph Hellwig } 32016d4a2b3aSChristoph Hellwig 32029c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 3203c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 3204927e0e76SStefan Hajnoczi goto out; 32059c75e168SJeff Cody } 32069c75e168SJeff Cody 32076d0eb64dSKevin Wolf blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL); 3208d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 3209d7086422SKevin Wolf if (ret < 0) { 3210d7086422SKevin Wolf goto out; 3211d7086422SKevin Wolf } 32127dad9ee6SKevin Wolf 3213698bdfa0SJohn Snow bdrv_drained_begin(bs); 32143a691c50SMax Reitz ret = blk_truncate(blk, size, PREALLOC_MODE_OFF, errp); 3215698bdfa0SJohn Snow bdrv_drained_end(bs); 3216927e0e76SStefan Hajnoczi 3217927e0e76SStefan Hajnoczi out: 32187dad9ee6SKevin Wolf blk_unref(blk); 3219927e0e76SStefan Hajnoczi aio_context_release(aio_context); 32206d4a2b3aSChristoph Hellwig } 322112bd451fSStefan Hajnoczi 32222323322eSAlberto Garcia void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, 322313d8cc51SJeff Cody bool has_base, const char *base, 3224312fe09cSAlberto Garcia bool has_base_node, const char *base_node, 322513d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 322613d8cc51SJeff Cody bool has_speed, int64_t speed, 32271d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 32281d809098SPaolo Bonzini Error **errp) 322912bd451fSStefan Hajnoczi { 3230554b6147SAlberto Garcia BlockDriverState *bs, *iter; 3231c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 3232f3e69bebSStefan Hajnoczi AioContext *aio_context; 3233fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 323413d8cc51SJeff Cody const char *base_name = NULL; 323512bd451fSStefan Hajnoczi 32361d809098SPaolo Bonzini if (!has_on_error) { 32371d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 32381d809098SPaolo Bonzini } 32391d809098SPaolo Bonzini 3240554b6147SAlberto Garcia bs = bdrv_lookup_bs(device, device, errp); 3241b6c1bae5SKevin Wolf if (!bs) { 324212bd451fSStefan Hajnoczi return; 324312bd451fSStefan Hajnoczi } 324412bd451fSStefan Hajnoczi 3245b6c1bae5SKevin Wolf aio_context = bdrv_get_aio_context(bs); 3246f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 3247f3e69bebSStefan Hajnoczi 3248312fe09cSAlberto Garcia if (has_base && has_base_node) { 3249312fe09cSAlberto Garcia error_setg(errp, "'base' and 'base-node' cannot be specified " 3250312fe09cSAlberto Garcia "at the same time"); 3251312fe09cSAlberto Garcia goto out; 3252312fe09cSAlberto Garcia } 3253312fe09cSAlberto Garcia 325413d8cc51SJeff Cody if (has_base) { 3255c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3256c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3257c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3258f3e69bebSStefan Hajnoczi goto out; 325912bd451fSStefan Hajnoczi } 3260f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 326113d8cc51SJeff Cody base_name = base; 3262c8c3080fSMarcelo Tosatti } 326312bd451fSStefan Hajnoczi 3264312fe09cSAlberto Garcia if (has_base_node) { 3265312fe09cSAlberto Garcia base_bs = bdrv_lookup_bs(NULL, base_node, errp); 3266312fe09cSAlberto Garcia if (!base_bs) { 3267312fe09cSAlberto Garcia goto out; 3268312fe09cSAlberto Garcia } 3269312fe09cSAlberto Garcia if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { 3270312fe09cSAlberto Garcia error_setg(errp, "Node '%s' is not a backing image of '%s'", 3271312fe09cSAlberto Garcia base_node, device); 3272312fe09cSAlberto Garcia goto out; 3273312fe09cSAlberto Garcia } 3274312fe09cSAlberto Garcia assert(bdrv_get_aio_context(base_bs) == aio_context); 3275312fe09cSAlberto Garcia base_name = base_bs->filename; 3276312fe09cSAlberto Garcia } 3277312fe09cSAlberto Garcia 3278554b6147SAlberto Garcia /* Check for op blockers in the whole chain between bs and base */ 3279554b6147SAlberto Garcia for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) { 3280554b6147SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { 3281554b6147SAlberto Garcia goto out; 3282554b6147SAlberto Garcia } 3283554b6147SAlberto Garcia } 3284554b6147SAlberto Garcia 328513d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 328613d8cc51SJeff Cody * file, and specifying one is therefore an error */ 328713d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 328813d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 328913d8cc51SJeff Cody "entire chain"); 3290f3e69bebSStefan Hajnoczi goto out; 329113d8cc51SJeff Cody } 329213d8cc51SJeff Cody 329313d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 329413d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 329513d8cc51SJeff Cody 32962323322eSAlberto Garcia stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, 32978254b6d9SJohn Snow has_speed ? speed : 0, on_error, &local_err); 329884d18f06SMarkus Armbruster if (local_err) { 3299fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3300f3e69bebSStefan Hajnoczi goto out; 330112bd451fSStefan Hajnoczi } 330212bd451fSStefan Hajnoczi 330312bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3304f3e69bebSStefan Hajnoczi 3305f3e69bebSStefan Hajnoczi out: 3306f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 330712bd451fSStefan Hajnoczi } 33082d47c6e9SStefan Hajnoczi 3309fd62c609SAlberto Garcia void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, 33107676e2c5SJeff Cody bool has_base, const char *base, 33117676e2c5SJeff Cody bool has_top, const char *top, 331254e26900SJeff Cody bool has_backing_file, const char *backing_file, 3313ed61fc10SJeff Cody bool has_speed, int64_t speed, 33140db832f4SKevin Wolf bool has_filter_node_name, const char *filter_node_name, 3315ed61fc10SJeff Cody Error **errp) 3316ed61fc10SJeff Cody { 3317ed61fc10SJeff Cody BlockDriverState *bs; 3318058223a6SAlberto Garcia BlockDriverState *iter; 3319ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 33209e85cd5cSStefan Hajnoczi AioContext *aio_context; 3321ed61fc10SJeff Cody Error *local_err = NULL; 3322ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3323ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3324ed61fc10SJeff Cody */ 332592aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3326ed61fc10SJeff Cody 332754504663SMax Reitz if (!has_speed) { 332854504663SMax Reitz speed = 0; 332954504663SMax Reitz } 33300db832f4SKevin Wolf if (!has_filter_node_name) { 33310db832f4SKevin Wolf filter_node_name = NULL; 33320db832f4SKevin Wolf } 333354504663SMax Reitz 33347676e2c5SJeff Cody /* Important Note: 33357676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 33367676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 33377676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 33387676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 33391d13b167SKevin Wolf bs = qmp_get_root_bs(device, &local_err); 33401d13b167SKevin Wolf if (!bs) { 33411d13b167SKevin Wolf bs = bdrv_lookup_bs(device, device, NULL); 33421d13b167SKevin Wolf if (!bs) { 33431d13b167SKevin Wolf error_free(local_err); 334475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 334575158ebbSMarkus Armbruster "Device '%s' not found", device); 33461d13b167SKevin Wolf } else { 33471d13b167SKevin Wolf error_propagate(errp, local_err); 33481d13b167SKevin Wolf } 3349ed61fc10SJeff Cody return; 3350ed61fc10SJeff Cody } 3351ed61fc10SJeff Cody 33521d13b167SKevin Wolf aio_context = bdrv_get_aio_context(bs); 33539e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 33549e85cd5cSStefan Hajnoczi 3355bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 33569e85cd5cSStefan Hajnoczi goto out; 3357628ff683SFam Zheng } 3358628ff683SFam Zheng 3359ed61fc10SJeff Cody /* default top_bs is the active layer */ 3360ed61fc10SJeff Cody top_bs = bs; 3361ed61fc10SJeff Cody 33627676e2c5SJeff Cody if (has_top && top) { 3363ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3364ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3365ed61fc10SJeff Cody } 3366ed61fc10SJeff Cody } 3367ed61fc10SJeff Cody 3368ed61fc10SJeff Cody if (top_bs == NULL) { 3369ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 33709e85cd5cSStefan Hajnoczi goto out; 3371ed61fc10SJeff Cody } 3372ed61fc10SJeff Cody 33739e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 33749e85cd5cSStefan Hajnoczi 3375d5208c45SJeff Cody if (has_base && base) { 3376d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3377d5208c45SJeff Cody } else { 3378d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3379d5208c45SJeff Cody } 3380d5208c45SJeff Cody 3381d5208c45SJeff Cody if (base_bs == NULL) { 3382c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 33839e85cd5cSStefan Hajnoczi goto out; 3384d5208c45SJeff Cody } 3385d5208c45SJeff Cody 33869e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 33879e85cd5cSStefan Hajnoczi 3388058223a6SAlberto Garcia for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) { 3389058223a6SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3390bb00021dSFam Zheng goto out; 3391bb00021dSFam Zheng } 3392058223a6SAlberto Garcia } 3393bb00021dSFam Zheng 33947676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 33957676e2c5SJeff Cody if (top_bs == base_bs) { 33967676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 33979e85cd5cSStefan Hajnoczi goto out; 33987676e2c5SJeff Cody } 33997676e2c5SJeff Cody 340020a63d2cSFam Zheng if (top_bs == bs) { 340154e26900SJeff Cody if (has_backing_file) { 340254e26900SJeff Cody error_setg(errp, "'backing-file' specified," 340354e26900SJeff Cody " but 'top' is the active layer"); 34049e85cd5cSStefan Hajnoczi goto out; 340554e26900SJeff Cody } 340647970dfbSJohn Snow commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, 3407bb02b65cSKevin Wolf JOB_DEFAULT, speed, on_error, 340878bbd910SFam Zheng filter_node_name, NULL, NULL, false, &local_err); 340920a63d2cSFam Zheng } else { 3410058223a6SAlberto Garcia BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); 3411058223a6SAlberto Garcia if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3412058223a6SAlberto Garcia goto out; 3413058223a6SAlberto Garcia } 3414fd62c609SAlberto Garcia commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed, 34158254b6d9SJohn Snow on_error, has_backing_file ? backing_file : NULL, 34160db832f4SKevin Wolf filter_node_name, &local_err); 341720a63d2cSFam Zheng } 3418ed61fc10SJeff Cody if (local_err != NULL) { 3419ed61fc10SJeff Cody error_propagate(errp, local_err); 34209e85cd5cSStefan Hajnoczi goto out; 3421ed61fc10SJeff Cody } 34229e85cd5cSStefan Hajnoczi 34239e85cd5cSStefan Hajnoczi out: 34249e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3425ed61fc10SJeff Cody } 3426ed61fc10SJeff Cody 342762c9e416SKevin Wolf static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn, 3428111049a4SJohn Snow Error **errp) 342999a9addfSStefan Hajnoczi { 343099a9addfSStefan Hajnoczi BlockDriverState *bs; 343199a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3432fc5d3f84SIan Main BlockDriverState *source = NULL; 3433111049a4SJohn Snow BlockJob *job = NULL; 3434d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3435761731b1SStefan Hajnoczi AioContext *aio_context; 3436e6641719SMax Reitz QDict *options = NULL; 343799a9addfSStefan Hajnoczi Error *local_err = NULL; 3438bb02b65cSKevin Wolf int flags, job_flags = JOB_DEFAULT; 343999a9addfSStefan Hajnoczi int64_t size; 3440fc0932fdSFam Zheng bool set_backing_hd = false; 344199a9addfSStefan Hajnoczi 344281206a89SPavel Butsykin if (!backup->has_speed) { 344381206a89SPavel Butsykin backup->speed = 0; 344499a9addfSStefan Hajnoczi } 344581206a89SPavel Butsykin if (!backup->has_on_source_error) { 344681206a89SPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 344799a9addfSStefan Hajnoczi } 344881206a89SPavel Butsykin if (!backup->has_on_target_error) { 344981206a89SPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 345099a9addfSStefan Hajnoczi } 345181206a89SPavel Butsykin if (!backup->has_mode) { 345281206a89SPavel Butsykin backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 345381206a89SPavel Butsykin } 345481206a89SPavel Butsykin if (!backup->has_job_id) { 345581206a89SPavel Butsykin backup->job_id = NULL; 345699a9addfSStefan Hajnoczi } 3457b40dacdcSJohn Snow if (!backup->has_auto_finalize) { 3458b40dacdcSJohn Snow backup->auto_finalize = true; 3459b40dacdcSJohn Snow } 3460b40dacdcSJohn Snow if (!backup->has_auto_dismiss) { 3461b40dacdcSJohn Snow backup->auto_dismiss = true; 3462b40dacdcSJohn Snow } 346313b9414bSPavel Butsykin if (!backup->has_compress) { 346413b9414bSPavel Butsykin backup->compress = false; 346513b9414bSPavel Butsykin } 346699a9addfSStefan Hajnoczi 346781206a89SPavel Butsykin bs = qmp_get_root_bs(backup->device, errp); 3468b7e4fa22SKevin Wolf if (!bs) { 3469111049a4SJohn Snow return NULL; 347099a9addfSStefan Hajnoczi } 347199a9addfSStefan Hajnoczi 3472b7e4fa22SKevin Wolf aio_context = bdrv_get_aio_context(bs); 3473761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3474761731b1SStefan Hajnoczi 347581206a89SPavel Butsykin if (!backup->has_format) { 347681206a89SPavel Butsykin backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? 347781206a89SPavel Butsykin NULL : (char*) bs->drv->format_name; 347899a9addfSStefan Hajnoczi } 347999a9addfSStefan Hajnoczi 3480c29c1dd3SFam Zheng /* Early check to avoid creating target */ 34813718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3482761731b1SStefan Hajnoczi goto out; 348399a9addfSStefan Hajnoczi } 348499a9addfSStefan Hajnoczi 348561de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 348699a9addfSStefan Hajnoczi 3487fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3488fc5d3f84SIan Main * on top of. */ 348981206a89SPavel Butsykin if (backup->sync == MIRROR_SYNC_MODE_TOP) { 3490760e0063SKevin Wolf source = backing_bs(bs); 3491fc5d3f84SIan Main if (!source) { 349281206a89SPavel Butsykin backup->sync = MIRROR_SYNC_MODE_FULL; 3493fc5d3f84SIan Main } 3494fc5d3f84SIan Main } 349581206a89SPavel Butsykin if (backup->sync == MIRROR_SYNC_MODE_NONE) { 3496fc5d3f84SIan Main source = bs; 3497fc0932fdSFam Zheng flags |= BDRV_O_NO_BACKING; 3498fc0932fdSFam Zheng set_backing_hd = true; 3499fc5d3f84SIan Main } 3500fc5d3f84SIan Main 350199a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 350299a9addfSStefan Hajnoczi if (size < 0) { 350399a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3504761731b1SStefan Hajnoczi goto out; 350599a9addfSStefan Hajnoczi } 350699a9addfSStefan Hajnoczi 350781206a89SPavel Butsykin if (backup->mode != NEW_IMAGE_MODE_EXISTING) { 350881206a89SPavel Butsykin assert(backup->format); 3509fc5d3f84SIan Main if (source) { 351081206a89SPavel Butsykin bdrv_img_create(backup->target, backup->format, source->filename, 3511fc5d3f84SIan Main source->drv->format_name, NULL, 35129217283dSFam Zheng size, flags, false, &local_err); 3513fc5d3f84SIan Main } else { 351481206a89SPavel Butsykin bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, 35159217283dSFam Zheng size, flags, false, &local_err); 3516fc5d3f84SIan Main } 351799a9addfSStefan Hajnoczi } 351899a9addfSStefan Hajnoczi 351984d18f06SMarkus Armbruster if (local_err) { 352099a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3521761731b1SStefan Hajnoczi goto out; 352299a9addfSStefan Hajnoczi } 352399a9addfSStefan Hajnoczi 352481206a89SPavel Butsykin if (backup->format) { 3525fc0932fdSFam Zheng if (!options) { 3526e6641719SMax Reitz options = qdict_new(); 3527fc0932fdSFam Zheng } 352846f5ac20SEric Blake qdict_put_str(options, "driver", backup->format); 3529e6641719SMax Reitz } 3530e6641719SMax Reitz 353181206a89SPavel Butsykin target_bs = bdrv_open(backup->target, NULL, options, flags, errp); 35325b363937SMax Reitz if (!target_bs) { 3533761731b1SStefan Hajnoczi goto out; 353499a9addfSStefan Hajnoczi } 353599a9addfSStefan Hajnoczi 3536761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3537761731b1SStefan Hajnoczi 3538fc0932fdSFam Zheng if (set_backing_hd) { 3539fc0932fdSFam Zheng bdrv_set_backing_hd(target_bs, source, &local_err); 3540fc0932fdSFam Zheng if (local_err) { 3541fc0932fdSFam Zheng bdrv_unref(target_bs); 3542fc0932fdSFam Zheng goto out; 3543fc0932fdSFam Zheng } 3544fc0932fdSFam Zheng } 3545fc0932fdSFam Zheng 354681206a89SPavel Butsykin if (backup->has_bitmap) { 354781206a89SPavel Butsykin bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); 3548d58d8453SJohn Snow if (!bmap) { 354981206a89SPavel Butsykin error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); 35500702d3d8SMax Reitz bdrv_unref(target_bs); 3551d58d8453SJohn Snow goto out; 3552d58d8453SJohn Snow } 35534f43e953SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_qmp_locked(bmap)) { 35544f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 35554f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be used for " 35564f43e953SVladimir Sementsov-Ogievskiy "backup", backup->bitmap); 35574f43e953SVladimir Sementsov-Ogievskiy goto out; 35584f43e953SVladimir Sementsov-Ogievskiy } 3559d58d8453SJohn Snow } 3560b40dacdcSJohn Snow if (!backup->auto_finalize) { 3561bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_FINALIZE; 3562b40dacdcSJohn Snow } 3563b40dacdcSJohn Snow if (!backup->auto_dismiss) { 3564bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_DISMISS; 3565b40dacdcSJohn Snow } 3566d58d8453SJohn Snow 3567111049a4SJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3568111049a4SJohn Snow backup->sync, bmap, backup->compress, 3569111049a4SJohn Snow backup->on_source_error, backup->on_target_error, 3570b40dacdcSJohn Snow job_flags, NULL, NULL, txn, &local_err); 35714f6fd349SFam Zheng bdrv_unref(target_bs); 35725c438bc6SKevin Wolf if (local_err != NULL) { 357399a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3574761731b1SStefan Hajnoczi goto out; 357599a9addfSStefan Hajnoczi } 3576761731b1SStefan Hajnoczi 3577761731b1SStefan Hajnoczi out: 3578761731b1SStefan Hajnoczi aio_context_release(aio_context); 3579111049a4SJohn Snow return job; 358099a9addfSStefan Hajnoczi } 358199a9addfSStefan Hajnoczi 358281206a89SPavel Butsykin void qmp_drive_backup(DriveBackup *arg, Error **errp) 358378f51fdeSJohn Snow { 3584111049a4SJohn Snow 3585111049a4SJohn Snow BlockJob *job; 3586111049a4SJohn Snow job = do_drive_backup(arg, NULL, errp); 3587111049a4SJohn Snow if (job) { 3588da01ff7fSKevin Wolf job_start(&job->job); 3589111049a4SJohn Snow } 359078f51fdeSJohn Snow } 359178f51fdeSJohn Snow 3592c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3593c13163fbSBenoît Canet { 3594d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3595c13163fbSBenoît Canet } 3596c13163fbSBenoît Canet 359762c9e416SKevin Wolf BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn, 3598111049a4SJohn Snow Error **errp) 3599c29c1dd3SFam Zheng { 3600c29c1dd3SFam Zheng BlockDriverState *bs; 3601c29c1dd3SFam Zheng BlockDriverState *target_bs; 3602c29c1dd3SFam Zheng Error *local_err = NULL; 3603c29c1dd3SFam Zheng AioContext *aio_context; 3604111049a4SJohn Snow BlockJob *job = NULL; 3605bb02b65cSKevin Wolf int job_flags = JOB_DEFAULT; 3606c29c1dd3SFam Zheng 3607dc7a4a9eSPavel Butsykin if (!backup->has_speed) { 3608dc7a4a9eSPavel Butsykin backup->speed = 0; 3609c29c1dd3SFam Zheng } 3610dc7a4a9eSPavel Butsykin if (!backup->has_on_source_error) { 3611dc7a4a9eSPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3612c29c1dd3SFam Zheng } 3613dc7a4a9eSPavel Butsykin if (!backup->has_on_target_error) { 3614dc7a4a9eSPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3615dc7a4a9eSPavel Butsykin } 3616dc7a4a9eSPavel Butsykin if (!backup->has_job_id) { 3617dc7a4a9eSPavel Butsykin backup->job_id = NULL; 3618c29c1dd3SFam Zheng } 3619b40dacdcSJohn Snow if (!backup->has_auto_finalize) { 3620b40dacdcSJohn Snow backup->auto_finalize = true; 3621b40dacdcSJohn Snow } 3622b40dacdcSJohn Snow if (!backup->has_auto_dismiss) { 3623b40dacdcSJohn Snow backup->auto_dismiss = true; 3624b40dacdcSJohn Snow } 36253b7b1236SPavel Butsykin if (!backup->has_compress) { 36263b7b1236SPavel Butsykin backup->compress = false; 36273b7b1236SPavel Butsykin } 3628c29c1dd3SFam Zheng 3629dc7a4a9eSPavel Butsykin bs = qmp_get_root_bs(backup->device, errp); 3630cef34eebSKevin Wolf if (!bs) { 3631111049a4SJohn Snow return NULL; 3632c29c1dd3SFam Zheng } 3633c29c1dd3SFam Zheng 3634cef34eebSKevin Wolf aio_context = bdrv_get_aio_context(bs); 3635c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3636c29c1dd3SFam Zheng 3637dc7a4a9eSPavel Butsykin target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); 36380d978913SFam Zheng if (!target_bs) { 3639c29c1dd3SFam Zheng goto out; 3640c29c1dd3SFam Zheng } 36415433c24fSMax Reitz 3642efd75567SFam Zheng if (bdrv_get_aio_context(target_bs) != aio_context) { 3643efd75567SFam Zheng if (!bdrv_has_blk(target_bs)) { 3644efd75567SFam Zheng /* The target BDS is not attached, we can safely move it to another 3645efd75567SFam Zheng * AioContext. */ 3646c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3647efd75567SFam Zheng } else { 3648efd75567SFam Zheng error_setg(errp, "Target is attached to a different thread from " 3649efd75567SFam Zheng "source."); 3650efd75567SFam Zheng goto out; 3651efd75567SFam Zheng } 3652efd75567SFam Zheng } 3653b40dacdcSJohn Snow if (!backup->auto_finalize) { 3654bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_FINALIZE; 3655b40dacdcSJohn Snow } 3656b40dacdcSJohn Snow if (!backup->auto_dismiss) { 3657bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_DISMISS; 3658b40dacdcSJohn Snow } 3659111049a4SJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3660111049a4SJohn Snow backup->sync, NULL, backup->compress, 3661111049a4SJohn Snow backup->on_source_error, backup->on_target_error, 3662b40dacdcSJohn Snow job_flags, NULL, NULL, txn, &local_err); 3663c29c1dd3SFam Zheng if (local_err != NULL) { 3664c29c1dd3SFam Zheng error_propagate(errp, local_err); 3665c29c1dd3SFam Zheng } 3666c29c1dd3SFam Zheng out: 3667c29c1dd3SFam Zheng aio_context_release(aio_context); 3668111049a4SJohn Snow return job; 3669c29c1dd3SFam Zheng } 3670c29c1dd3SFam Zheng 3671dc7a4a9eSPavel Butsykin void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp) 367278f51fdeSJohn Snow { 3673111049a4SJohn Snow BlockJob *job; 3674111049a4SJohn Snow job = do_blockdev_backup(arg, NULL, errp); 3675111049a4SJohn Snow if (job) { 3676da01ff7fSKevin Wolf job_start(&job->job); 3677111049a4SJohn Snow } 367878f51fdeSJohn Snow } 367978f51fdeSJohn Snow 36804193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 36814193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 36824193cdd7SFam Zheng **/ 368371aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, 36844193cdd7SFam Zheng BlockDriverState *target, 368509158f00SBenoît Canet bool has_replaces, const char *replaces, 3686d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3687274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 3688b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3689eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 369008e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 36914193cdd7SFam Zheng bool has_on_source_error, 36924193cdd7SFam Zheng BlockdevOnError on_source_error, 36934193cdd7SFam Zheng bool has_on_target_error, 36944193cdd7SFam Zheng BlockdevOnError on_target_error, 36950fc9f8eaSFam Zheng bool has_unmap, bool unmap, 36966cdbceb1SKevin Wolf bool has_filter_node_name, 36976cdbceb1SKevin Wolf const char *filter_node_name, 3698b952b558SPaolo Bonzini Error **errp) 3699d9b902dbSPaolo Bonzini { 3700d9b902dbSPaolo Bonzini 3701d9b902dbSPaolo Bonzini if (!has_speed) { 3702d9b902dbSPaolo Bonzini speed = 0; 3703d9b902dbSPaolo Bonzini } 3704b952b558SPaolo Bonzini if (!has_on_source_error) { 3705b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3706b952b558SPaolo Bonzini } 3707b952b558SPaolo Bonzini if (!has_on_target_error) { 3708b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3709b952b558SPaolo Bonzini } 3710eee13dfeSPaolo Bonzini if (!has_granularity) { 3711eee13dfeSPaolo Bonzini granularity = 0; 3712eee13dfeSPaolo Bonzini } 371308e4ed6cSPaolo Bonzini if (!has_buf_size) { 371448ac0a4dSWen Congyang buf_size = 0; 371508e4ed6cSPaolo Bonzini } 37160fc9f8eaSFam Zheng if (!has_unmap) { 37170fc9f8eaSFam Zheng unmap = true; 37180fc9f8eaSFam Zheng } 37196cdbceb1SKevin Wolf if (!has_filter_node_name) { 37206cdbceb1SKevin Wolf filter_node_name = NULL; 37216cdbceb1SKevin Wolf } 372208e4ed6cSPaolo Bonzini 3723eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3724c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 37253cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3726eee13dfeSPaolo Bonzini return; 3727eee13dfeSPaolo Bonzini } 3728eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3729c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3730c6bd8c70SMarkus Armbruster "power of 2"); 3731eee13dfeSPaolo Bonzini return; 3732eee13dfeSPaolo Bonzini } 3733d9b902dbSPaolo Bonzini 37344193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 37354193cdd7SFam Zheng return; 37364193cdd7SFam Zheng } 3737e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3738e40e5027SFam Zheng return; 3739e40e5027SFam Zheng } 37404193cdd7SFam Zheng 37414193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 37424193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 37434193cdd7SFam Zheng } 37444193cdd7SFam Zheng 37454193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 37464193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 37474193cdd7SFam Zheng */ 374871aa9867SAlberto Garcia mirror_start(job_id, bs, target, 37494193cdd7SFam Zheng has_replaces ? replaces : NULL, 3750274fcceeSMax Reitz speed, granularity, buf_size, sync, backing_mode, 37516cdbceb1SKevin Wolf on_source_error, on_target_error, unmap, filter_node_name, 37526cdbceb1SKevin Wolf errp); 37534193cdd7SFam Zheng } 37544193cdd7SFam Zheng 3755faecd40aSEric Blake void qmp_drive_mirror(DriveMirror *arg, Error **errp) 37564193cdd7SFam Zheng { 37574193cdd7SFam Zheng BlockDriverState *bs; 37584193cdd7SFam Zheng BlockDriverState *source, *target_bs; 37594193cdd7SFam Zheng AioContext *aio_context; 3760274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 37614193cdd7SFam Zheng Error *local_err = NULL; 37624193cdd7SFam Zheng QDict *options = NULL; 37634193cdd7SFam Zheng int flags; 37644193cdd7SFam Zheng int64_t size; 3765faecd40aSEric Blake const char *format = arg->format; 37664193cdd7SFam Zheng 37670524e93aSKevin Wolf bs = qmp_get_root_bs(arg->device, errp); 37680524e93aSKevin Wolf if (!bs) { 3769d9b902dbSPaolo Bonzini return; 3770d9b902dbSPaolo Bonzini } 3771d9b902dbSPaolo Bonzini 3772cb2af917SPaolo Bonzini /* Early check to avoid creating target */ 3773cb2af917SPaolo Bonzini if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 3774cb2af917SPaolo Bonzini return; 3775cb2af917SPaolo Bonzini } 3776cb2af917SPaolo Bonzini 37770524e93aSKevin Wolf aio_context = bdrv_get_aio_context(bs); 37785a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 37795a7e7a0bSStefan Hajnoczi 3780faecd40aSEric Blake if (!arg->has_mode) { 3781faecd40aSEric Blake arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 37824193cdd7SFam Zheng } 3783d9b902dbSPaolo Bonzini 3784faecd40aSEric Blake if (!arg->has_format) { 3785faecd40aSEric Blake format = (arg->mode == NEW_IMAGE_MODE_EXISTING 3786faecd40aSEric Blake ? NULL : bs->drv->format_name); 3787d9b902dbSPaolo Bonzini } 3788d9b902dbSPaolo Bonzini 378961de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3790760e0063SKevin Wolf source = backing_bs(bs); 3791faecd40aSEric Blake if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { 3792faecd40aSEric Blake arg->sync = MIRROR_SYNC_MODE_FULL; 3793d9b902dbSPaolo Bonzini } 3794faecd40aSEric Blake if (arg->sync == MIRROR_SYNC_MODE_NONE) { 3795117e0c82SMax Reitz source = bs; 3796117e0c82SMax Reitz } 3797d9b902dbSPaolo Bonzini 3798ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3799ac3c5d83SStefan Hajnoczi if (size < 0) { 3800ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 38015a7e7a0bSStefan Hajnoczi goto out; 3802ac3c5d83SStefan Hajnoczi } 3803ac3c5d83SStefan Hajnoczi 3804faecd40aSEric Blake if (arg->has_replaces) { 380509158f00SBenoît Canet BlockDriverState *to_replace_bs; 38065a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 38075a7e7a0bSStefan Hajnoczi int64_t replace_size; 380809158f00SBenoît Canet 3809faecd40aSEric Blake if (!arg->has_node_name) { 381009158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 381109158f00SBenoît Canet " named node of the graph"); 38125a7e7a0bSStefan Hajnoczi goto out; 381309158f00SBenoît Canet } 381409158f00SBenoît Canet 3815faecd40aSEric Blake to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err); 381609158f00SBenoît Canet 381709158f00SBenoît Canet if (!to_replace_bs) { 381809158f00SBenoît Canet error_propagate(errp, local_err); 38195a7e7a0bSStefan Hajnoczi goto out; 382009158f00SBenoît Canet } 382109158f00SBenoît Canet 38225a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 38235a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 38245a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 38255a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 38265a7e7a0bSStefan Hajnoczi 38275a7e7a0bSStefan Hajnoczi if (size != replace_size) { 382809158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 382909158f00SBenoît Canet "different size"); 38305a7e7a0bSStefan Hajnoczi goto out; 383109158f00SBenoît Canet } 383209158f00SBenoît Canet } 383309158f00SBenoît Canet 3834faecd40aSEric Blake if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { 3835274fcceeSMax Reitz backing_mode = MIRROR_SOURCE_BACKING_CHAIN; 3836274fcceeSMax Reitz } else { 3837274fcceeSMax Reitz backing_mode = MIRROR_OPEN_BACKING_CHAIN; 3838274fcceeSMax Reitz } 3839274fcceeSMax Reitz 38402a32c6e8SJohn Snow /* Don't open backing image in create() */ 38412a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING; 38422a32c6e8SJohn Snow 3843faecd40aSEric Blake if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) 3844faecd40aSEric Blake && arg->mode != NEW_IMAGE_MODE_EXISTING) 384514526864SMax Reitz { 3846d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3847e6641719SMax Reitz assert(format); 3848faecd40aSEric Blake bdrv_img_create(arg->target, format, 38499217283dSFam Zheng NULL, NULL, NULL, size, flags, false, &local_err); 3850d9b902dbSPaolo Bonzini } else { 3851faecd40aSEric Blake switch (arg->mode) { 3852d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3853d9b902dbSPaolo Bonzini break; 3854d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3855d9b902dbSPaolo Bonzini /* create new image with backing file */ 3856faecd40aSEric Blake bdrv_img_create(arg->target, format, 3857d9b902dbSPaolo Bonzini source->filename, 3858d9b902dbSPaolo Bonzini source->drv->format_name, 38599217283dSFam Zheng NULL, size, flags, false, &local_err); 3860d9b902dbSPaolo Bonzini break; 3861d9b902dbSPaolo Bonzini default: 3862d9b902dbSPaolo Bonzini abort(); 3863d9b902dbSPaolo Bonzini } 3864d9b902dbSPaolo Bonzini } 3865d9b902dbSPaolo Bonzini 386684d18f06SMarkus Armbruster if (local_err) { 3867cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 38685a7e7a0bSStefan Hajnoczi goto out; 3869d9b902dbSPaolo Bonzini } 3870d9b902dbSPaolo Bonzini 38714c828dc6SBenoît Canet options = qdict_new(); 3872faecd40aSEric Blake if (arg->has_node_name) { 387346f5ac20SEric Blake qdict_put_str(options, "node-name", arg->node_name); 38744c828dc6SBenoît Canet } 3875e6641719SMax Reitz if (format) { 387646f5ac20SEric Blake qdict_put_str(options, "driver", format); 3877e6641719SMax Reitz } 38784c828dc6SBenoît Canet 3879b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3880b812f671SPaolo Bonzini * file. 3881b812f671SPaolo Bonzini */ 38822a32c6e8SJohn Snow target_bs = bdrv_open(arg->target, NULL, options, flags, errp); 38835b363937SMax Reitz if (!target_bs) { 38845a7e7a0bSStefan Hajnoczi goto out; 3885d9b902dbSPaolo Bonzini } 3886d9b902dbSPaolo Bonzini 38875a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 38885a7e7a0bSStefan Hajnoczi 3889faecd40aSEric Blake blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, 3890faecd40aSEric Blake arg->has_replaces, arg->replaces, arg->sync, 3891faecd40aSEric Blake backing_mode, arg->has_speed, arg->speed, 3892faecd40aSEric Blake arg->has_granularity, arg->granularity, 3893faecd40aSEric Blake arg->has_buf_size, arg->buf_size, 3894faecd40aSEric Blake arg->has_on_source_error, arg->on_source_error, 3895faecd40aSEric Blake arg->has_on_target_error, arg->on_target_error, 3896faecd40aSEric Blake arg->has_unmap, arg->unmap, 38976cdbceb1SKevin Wolf false, NULL, 38984193cdd7SFam Zheng &local_err); 3899e253f4b8SKevin Wolf bdrv_unref(target_bs); 3900d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 39015a7e7a0bSStefan Hajnoczi out: 39025a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3903d9b902dbSPaolo Bonzini } 3904d9b902dbSPaolo Bonzini 390571aa9867SAlberto Garcia void qmp_blockdev_mirror(bool has_job_id, const char *job_id, 390671aa9867SAlberto Garcia const char *device, const char *target, 3907df92562eSFam Zheng bool has_replaces, const char *replaces, 3908df92562eSFam Zheng MirrorSyncMode sync, 3909df92562eSFam Zheng bool has_speed, int64_t speed, 3910df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3911df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3912df92562eSFam Zheng bool has_on_source_error, 3913df92562eSFam Zheng BlockdevOnError on_source_error, 3914df92562eSFam Zheng bool has_on_target_error, 3915df92562eSFam Zheng BlockdevOnError on_target_error, 39166cdbceb1SKevin Wolf bool has_filter_node_name, 39176cdbceb1SKevin Wolf const char *filter_node_name, 3918df92562eSFam Zheng Error **errp) 3919df92562eSFam Zheng { 3920df92562eSFam Zheng BlockDriverState *bs; 3921df92562eSFam Zheng BlockDriverState *target_bs; 3922df92562eSFam Zheng AioContext *aio_context; 3923274fcceeSMax Reitz BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; 3924df92562eSFam Zheng Error *local_err = NULL; 3925df92562eSFam Zheng 392607eec652SKevin Wolf bs = qmp_get_root_bs(device, errp); 3927df92562eSFam Zheng if (!bs) { 3928df92562eSFam Zheng return; 3929df92562eSFam Zheng } 3930df92562eSFam Zheng 3931df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3932df92562eSFam Zheng if (!target_bs) { 3933df92562eSFam Zheng return; 3934df92562eSFam Zheng } 3935df92562eSFam Zheng 3936df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3937df92562eSFam Zheng aio_context_acquire(aio_context); 3938df92562eSFam Zheng 3939df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3940df92562eSFam Zheng 394171aa9867SAlberto Garcia blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 3942274fcceeSMax Reitz has_replaces, replaces, sync, backing_mode, 3943df92562eSFam Zheng has_speed, speed, 3944df92562eSFam Zheng has_granularity, granularity, 3945df92562eSFam Zheng has_buf_size, buf_size, 3946df92562eSFam Zheng has_on_source_error, on_source_error, 3947df92562eSFam Zheng has_on_target_error, on_target_error, 3948df92562eSFam Zheng true, true, 39496cdbceb1SKevin Wolf has_filter_node_name, filter_node_name, 3950df92562eSFam Zheng &local_err); 3951df92562eSFam Zheng error_propagate(errp, local_err); 3952df92562eSFam Zheng 3953df92562eSFam Zheng aio_context_release(aio_context); 3954df92562eSFam Zheng } 3955df92562eSFam Zheng 39563ddf3efeSAlberto Garcia /* Get a block job using its ID and acquire its AioContext */ 39573ddf3efeSAlberto Garcia static BlockJob *find_block_job(const char *id, AioContext **aio_context, 395824d6bffeSMarkus Armbruster Error **errp) 39592d47c6e9SStefan Hajnoczi { 39603ddf3efeSAlberto Garcia BlockJob *job; 39613ddf3efeSAlberto Garcia 39623ddf3efeSAlberto Garcia assert(id != NULL); 39632d47c6e9SStefan Hajnoczi 39645433c24fSMax Reitz *aio_context = NULL; 39655433c24fSMax Reitz 39663ddf3efeSAlberto Garcia job = block_job_get(id); 39673ddf3efeSAlberto Garcia 39683ddf3efeSAlberto Garcia if (!job) { 39693ddf3efeSAlberto Garcia error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 39703ddf3efeSAlberto Garcia "Block job '%s' not found", id); 39713ddf3efeSAlberto Garcia return NULL; 39722d47c6e9SStefan Hajnoczi } 39733d948cdfSStefan Hajnoczi 39743ddf3efeSAlberto Garcia *aio_context = blk_get_aio_context(job->blk); 39753d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 39763d948cdfSStefan Hajnoczi 39773ddf3efeSAlberto Garcia return job; 39782d47c6e9SStefan Hajnoczi } 39792d47c6e9SStefan Hajnoczi 3980882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 39812d47c6e9SStefan Hajnoczi { 39823d948cdfSStefan Hajnoczi AioContext *aio_context; 398324d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 39842d47c6e9SStefan Hajnoczi 39852d47c6e9SStefan Hajnoczi if (!job) { 39862d47c6e9SStefan Hajnoczi return; 39872d47c6e9SStefan Hajnoczi } 39882d47c6e9SStefan Hajnoczi 3989882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 39903d948cdfSStefan Hajnoczi aio_context_release(aio_context); 39912d47c6e9SStefan Hajnoczi } 3992370521a1SStefan Hajnoczi 39936e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 39946e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 39956e37fb81SPaolo Bonzini { 39963d948cdfSStefan Hajnoczi AioContext *aio_context; 399724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 39986e37fb81SPaolo Bonzini 39996e37fb81SPaolo Bonzini if (!job) { 40006e37fb81SPaolo Bonzini return; 40016e37fb81SPaolo Bonzini } 40023d948cdfSStefan Hajnoczi 40033d948cdfSStefan Hajnoczi if (!has_force) { 40043d948cdfSStefan Hajnoczi force = false; 40053d948cdfSStefan Hajnoczi } 40063d948cdfSStefan Hajnoczi 4007b15de828SKevin Wolf if (job_user_paused(&job->job) && !force) { 4008f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 4009f231b88dSCole Robinson device); 40103d948cdfSStefan Hajnoczi goto out; 40116e37fb81SPaolo Bonzini } 40126e37fb81SPaolo Bonzini 40136e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 40143d70ff53SKevin Wolf job_user_cancel(&job->job, force, errp); 40153d948cdfSStefan Hajnoczi out: 40163d948cdfSStefan Hajnoczi aio_context_release(aio_context); 40176e37fb81SPaolo Bonzini } 40186e37fb81SPaolo Bonzini 40196e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 4020370521a1SStefan Hajnoczi { 40213d948cdfSStefan Hajnoczi AioContext *aio_context; 402224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 4023370521a1SStefan Hajnoczi 40240ec4dfb8SJohn Snow if (!job) { 4025370521a1SStefan Hajnoczi return; 4026370521a1SStefan Hajnoczi } 40276e37fb81SPaolo Bonzini 40286e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 4029b15de828SKevin Wolf job_user_pause(&job->job, errp); 40303d948cdfSStefan Hajnoczi aio_context_release(aio_context); 40316e37fb81SPaolo Bonzini } 40326e37fb81SPaolo Bonzini 40336e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 40346e37fb81SPaolo Bonzini { 40353d948cdfSStefan Hajnoczi AioContext *aio_context; 403624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 40376e37fb81SPaolo Bonzini 40380ec4dfb8SJohn Snow if (!job) { 40398acc72a4SPaolo Bonzini return; 40408acc72a4SPaolo Bonzini } 4041370521a1SStefan Hajnoczi 40426e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 4043b15de828SKevin Wolf job_user_resume(&job->job, errp); 40443d948cdfSStefan Hajnoczi aio_context_release(aio_context); 4045370521a1SStefan Hajnoczi } 4046fb5458cdSStefan Hajnoczi 4047aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 4048aeae883bSPaolo Bonzini { 40493d948cdfSStefan Hajnoczi AioContext *aio_context; 405024d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 4051aeae883bSPaolo Bonzini 4052aeae883bSPaolo Bonzini if (!job) { 4053aeae883bSPaolo Bonzini return; 4054aeae883bSPaolo Bonzini } 4055aeae883bSPaolo Bonzini 4056aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 40573453d972SKevin Wolf job_complete(&job->job, errp); 40583d948cdfSStefan Hajnoczi aio_context_release(aio_context); 4059aeae883bSPaolo Bonzini } 4060aeae883bSPaolo Bonzini 406111b61fbcSJohn Snow void qmp_block_job_finalize(const char *id, Error **errp) 406211b61fbcSJohn Snow { 406311b61fbcSJohn Snow AioContext *aio_context; 406411b61fbcSJohn Snow BlockJob *job = find_block_job(id, &aio_context, errp); 406511b61fbcSJohn Snow 406611b61fbcSJohn Snow if (!job) { 406711b61fbcSJohn Snow return; 406811b61fbcSJohn Snow } 406911b61fbcSJohn Snow 407011b61fbcSJohn Snow trace_qmp_block_job_finalize(job); 40717eaa8fb5SKevin Wolf job_finalize(&job->job, errp); 407211b61fbcSJohn Snow aio_context_release(aio_context); 407311b61fbcSJohn Snow } 407411b61fbcSJohn Snow 407575f71059SJohn Snow void qmp_block_job_dismiss(const char *id, Error **errp) 407675f71059SJohn Snow { 407775f71059SJohn Snow AioContext *aio_context; 40785f9a6a08SKevin Wolf BlockJob *bjob = find_block_job(id, &aio_context, errp); 40795f9a6a08SKevin Wolf Job *job; 408075f71059SJohn Snow 40815f9a6a08SKevin Wolf if (!bjob) { 408275f71059SJohn Snow return; 408375f71059SJohn Snow } 408475f71059SJohn Snow 40855f9a6a08SKevin Wolf trace_qmp_block_job_dismiss(bjob); 40865f9a6a08SKevin Wolf job = &bjob->job; 40875f9a6a08SKevin Wolf job_dismiss(&job, errp); 408875f71059SJohn Snow aio_context_release(aio_context); 408975f71059SJohn Snow } 409075f71059SJohn Snow 4091fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 4092fa40e656SJeff Cody const char *image_node_name, 4093fa40e656SJeff Cody const char *backing_file, 4094fa40e656SJeff Cody Error **errp) 4095fa40e656SJeff Cody { 4096fa40e656SJeff Cody BlockDriverState *bs = NULL; 4097729962f6SStefan Hajnoczi AioContext *aio_context; 4098fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 4099fa40e656SJeff Cody Error *local_err = NULL; 4100fa40e656SJeff Cody bool ro; 4101fa40e656SJeff Cody int open_flags; 4102fa40e656SJeff Cody int ret; 4103fa40e656SJeff Cody 41047b5dca3fSKevin Wolf bs = qmp_get_root_bs(device, errp); 41057b5dca3fSKevin Wolf if (!bs) { 4106fa40e656SJeff Cody return; 4107fa40e656SJeff Cody } 4108fa40e656SJeff Cody 41097b5dca3fSKevin Wolf aio_context = bdrv_get_aio_context(bs); 4110729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 4111729962f6SStefan Hajnoczi 4112fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 4113fa40e656SJeff Cody if (local_err) { 4114fa40e656SJeff Cody error_propagate(errp, local_err); 4115729962f6SStefan Hajnoczi goto out; 4116fa40e656SJeff Cody } 4117fa40e656SJeff Cody 4118fa40e656SJeff Cody if (!image_bs) { 4119fa40e656SJeff Cody error_setg(errp, "image file not found"); 4120729962f6SStefan Hajnoczi goto out; 4121fa40e656SJeff Cody } 4122fa40e656SJeff Cody 4123fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 4124fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 4125fa40e656SJeff Cody "without a backing file"); 4126729962f6SStefan Hajnoczi goto out; 4127fa40e656SJeff Cody } 4128fa40e656SJeff Cody 4129fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 4130fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 4131fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 4132729962f6SStefan Hajnoczi goto out; 4133fa40e656SJeff Cody } 4134fa40e656SJeff Cody 4135fa40e656SJeff Cody /* final sanity check */ 4136fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 4137fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 4138fa40e656SJeff Cody device); 4139729962f6SStefan Hajnoczi goto out; 4140fa40e656SJeff Cody } 4141fa40e656SJeff Cody 4142fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 4143fa40e656SJeff Cody open_flags = image_bs->open_flags; 4144fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 4145fa40e656SJeff Cody 4146fa40e656SJeff Cody if (ro) { 4147fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 4148fa40e656SJeff Cody if (local_err) { 4149fa40e656SJeff Cody error_propagate(errp, local_err); 4150729962f6SStefan Hajnoczi goto out; 4151fa40e656SJeff Cody } 4152fa40e656SJeff Cody } 4153fa40e656SJeff Cody 4154fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 4155fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 4156fa40e656SJeff Cody 4157fa40e656SJeff Cody if (ret < 0) { 4158fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 4159fa40e656SJeff Cody backing_file); 4160fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 4161fa40e656SJeff Cody * appropriate */ 4162fa40e656SJeff Cody } 4163fa40e656SJeff Cody 4164fa40e656SJeff Cody if (ro) { 4165fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 4166621ff94dSEduardo Habkost error_propagate(errp, local_err); 4167fa40e656SJeff Cody } 4168729962f6SStefan Hajnoczi 4169729962f6SStefan Hajnoczi out: 4170729962f6SStefan Hajnoczi aio_context_release(aio_context); 4171fa40e656SJeff Cody } 4172fa40e656SJeff Cody 4173abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 4174abb21ac3SKevin Wolf { 4175abb21ac3SKevin Wolf QemuOpts *opts; 4176abb21ac3SKevin Wolf QDict *qdict; 4177abb21ac3SKevin Wolf Error *local_err = NULL; 4178abb21ac3SKevin Wolf 4179abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 4180abb21ac3SKevin Wolf if (!opts) { 4181abb21ac3SKevin Wolf return; 4182abb21ac3SKevin Wolf } 4183abb21ac3SKevin Wolf 4184abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 4185abb21ac3SKevin Wolf 4186abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 4187cb3e7f08SMarc-André Lureau qobject_unref(qdict); 4188abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 4189abb21ac3SKevin Wolf goto out; 4190abb21ac3SKevin Wolf } 4191abb21ac3SKevin Wolf 4192abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 4193abb21ac3SKevin Wolf if (!bs) { 4194abb21ac3SKevin Wolf error_report_err(local_err); 4195abb21ac3SKevin Wolf goto out; 4196abb21ac3SKevin Wolf } 4197abb21ac3SKevin Wolf 4198abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4199abb21ac3SKevin Wolf 4200abb21ac3SKevin Wolf out: 4201abb21ac3SKevin Wolf qemu_opts_del(opts); 4202abb21ac3SKevin Wolf } 4203abb21ac3SKevin Wolf 4204d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 4205d26c9a15SKevin Wolf { 4206be4b67bcSMax Reitz BlockDriverState *bs; 4207d26c9a15SKevin Wolf QObject *obj; 42087d5e199aSDaniel P. Berrange Visitor *v = qobject_output_visitor_new(&obj); 4209d26c9a15SKevin Wolf QDict *qdict; 4210d26c9a15SKevin Wolf Error *local_err = NULL; 4211d26c9a15SKevin Wolf 42123b098d56SEric Blake visit_type_BlockdevOptions(v, NULL, &options, &local_err); 421384d18f06SMarkus Armbruster if (local_err) { 4214d26c9a15SKevin Wolf error_propagate(errp, local_err); 4215d26c9a15SKevin Wolf goto fail; 4216d26c9a15SKevin Wolf } 4217d26c9a15SKevin Wolf 42183b098d56SEric Blake visit_complete(v, &obj); 42197dc847ebSMax Reitz qdict = qobject_to(QDict, obj); 4220d26c9a15SKevin Wolf 4221d26c9a15SKevin Wolf qdict_flatten(qdict); 4222d26c9a15SKevin Wolf 4223be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 42249ec8873eSKevin Wolf error_setg(errp, "'node-name' must be specified for the root node"); 4225be4b67bcSMax Reitz goto fail; 4226be4b67bcSMax Reitz } 4227be4b67bcSMax Reitz 4228bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 4229bd745e23SMax Reitz if (!bs) { 4230be4b67bcSMax Reitz goto fail; 4231be4b67bcSMax Reitz } 42329c4218e9SMax Reitz 42339c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4234be4b67bcSMax Reitz 4235d26c9a15SKevin Wolf fail: 42363b098d56SEric Blake visit_free(v); 4237d26c9a15SKevin Wolf } 4238d26c9a15SKevin Wolf 423979b7a77eSMarkus Armbruster void qmp_blockdev_del(const char *node_name, Error **errp) 424081b936aeSAlberto Garcia { 424181b936aeSAlberto Garcia AioContext *aio_context; 424281b936aeSAlberto Garcia BlockDriverState *bs; 424381b936aeSAlberto Garcia 424481b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 424581b936aeSAlberto Garcia if (!bs) { 424681b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 424781b936aeSAlberto Garcia return; 424881b936aeSAlberto Garcia } 42491f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 4250e467da7bSKevin Wolf error_setg(errp, "Node %s is in use", node_name); 425181b936aeSAlberto Garcia return; 425281b936aeSAlberto Garcia } 425381b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 425481b936aeSAlberto Garcia aio_context_acquire(aio_context); 425581b936aeSAlberto Garcia 425681b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 425781b936aeSAlberto Garcia goto out; 425881b936aeSAlberto Garcia } 425981b936aeSAlberto Garcia 42609ec8873eSKevin Wolf if (!bs->monitor_list.tqe_prev) { 42619c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 42629c4218e9SMax Reitz bs->node_name); 42639c4218e9SMax Reitz goto out; 42649c4218e9SMax Reitz } 42659c4218e9SMax Reitz 42669c4218e9SMax Reitz if (bs->refcnt > 1) { 426781b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 426881b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 426981b936aeSAlberto Garcia goto out; 427081b936aeSAlberto Garcia } 427181b936aeSAlberto Garcia 42729c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 427381b936aeSAlberto Garcia bdrv_unref(bs); 427481b936aeSAlberto Garcia 427581b936aeSAlberto Garcia out: 427681b936aeSAlberto Garcia aio_context_release(aio_context); 427781b936aeSAlberto Garcia } 427881b936aeSAlberto Garcia 42797f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 42807f821597SWen Congyang const char *child_name) 42817f821597SWen Congyang { 42827f821597SWen Congyang BdrvChild *child; 42837f821597SWen Congyang 42847f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 42857f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 42867f821597SWen Congyang return child; 42877f821597SWen Congyang } 42887f821597SWen Congyang } 42897f821597SWen Congyang 42907f821597SWen Congyang return NULL; 42917f821597SWen Congyang } 42927f821597SWen Congyang 42937f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 42947f821597SWen Congyang const char *child, bool has_node, 42957f821597SWen Congyang const char *node, Error **errp) 42967f821597SWen Congyang { 42977f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 42987f821597SWen Congyang BdrvChild *p_child; 42997f821597SWen Congyang 43007f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 43017f821597SWen Congyang if (!parent_bs) { 43027f821597SWen Congyang return; 43037f821597SWen Congyang } 43047f821597SWen Congyang 43057f821597SWen Congyang if (has_child == has_node) { 43067f821597SWen Congyang if (has_child) { 43077f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 43087f821597SWen Congyang } else { 43097f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 43107f821597SWen Congyang } 43117f821597SWen Congyang return; 43127f821597SWen Congyang } 43137f821597SWen Congyang 43147f821597SWen Congyang if (has_child) { 43157f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 43167f821597SWen Congyang if (!p_child) { 43177f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 43187f821597SWen Congyang parent, child); 43197f821597SWen Congyang return; 43207f821597SWen Congyang } 43217f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 43227f821597SWen Congyang } 43237f821597SWen Congyang 43247f821597SWen Congyang if (has_node) { 43257f821597SWen Congyang new_bs = bdrv_find_node(node); 43267f821597SWen Congyang if (!new_bs) { 43277f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 43287f821597SWen Congyang return; 43297f821597SWen Congyang } 43307f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 43317f821597SWen Congyang } 43327f821597SWen Congyang } 43337f821597SWen Congyang 4334fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4335fb5458cdSStefan Hajnoczi { 4336fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4337f0f55dedSAlberto Garcia BlockJob *job; 4338fea68bb6SMarkus Armbruster 4339f0f55dedSAlberto Garcia for (job = block_job_next(NULL); job; job = block_job_next(job)) { 4340559b935fSJohn Snow BlockJobInfoList *elem; 4341559b935fSJohn Snow AioContext *aio_context; 434269691e72SStefan Hajnoczi 4343559b935fSJohn Snow if (block_job_is_internal(job)) { 4344559b935fSJohn Snow continue; 4345559b935fSJohn Snow } 4346559b935fSJohn Snow elem = g_new0(BlockJobInfoList, 1); 4347559b935fSJohn Snow aio_context = blk_get_aio_context(job->blk); 434869691e72SStefan Hajnoczi aio_context_acquire(aio_context); 4349559b935fSJohn Snow elem->value = block_job_query(job, errp); 4350f0f55dedSAlberto Garcia aio_context_release(aio_context); 4351559b935fSJohn Snow if (!elem->value) { 4352559b935fSJohn Snow g_free(elem); 4353559b935fSJohn Snow qapi_free_BlockJobInfoList(head); 4354559b935fSJohn Snow return NULL; 4355559b935fSJohn Snow } 4356fea68bb6SMarkus Armbruster *p_next = elem; 4357fea68bb6SMarkus Armbruster p_next = &elem->next; 4358fea68bb6SMarkus Armbruster } 435969691e72SStefan Hajnoczi 4360fea68bb6SMarkus Armbruster return head; 4361fb5458cdSStefan Hajnoczi } 43624d454574SPaolo Bonzini 4363ca00bbb1SStefan Hajnoczi void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread, 4364882e9b89SStefan Hajnoczi bool has_force, bool force, Error **errp) 4365ca00bbb1SStefan Hajnoczi { 4366ca00bbb1SStefan Hajnoczi AioContext *old_context; 4367ca00bbb1SStefan Hajnoczi AioContext *new_context; 4368ca00bbb1SStefan Hajnoczi BlockDriverState *bs; 4369ca00bbb1SStefan Hajnoczi 4370ca00bbb1SStefan Hajnoczi bs = bdrv_find_node(node_name); 4371ca00bbb1SStefan Hajnoczi if (!bs) { 4372ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find node %s", node_name); 4373ca00bbb1SStefan Hajnoczi return; 4374ca00bbb1SStefan Hajnoczi } 4375ca00bbb1SStefan Hajnoczi 4376882e9b89SStefan Hajnoczi /* Protects against accidents. */ 4377882e9b89SStefan Hajnoczi if (!(has_force && force) && bdrv_has_blk(bs)) { 4378882e9b89SStefan Hajnoczi error_setg(errp, "Node %s is associated with a BlockBackend and could " 4379882e9b89SStefan Hajnoczi "be in use (use force=true to override this check)", 4380882e9b89SStefan Hajnoczi node_name); 4381ca00bbb1SStefan Hajnoczi return; 4382ca00bbb1SStefan Hajnoczi } 4383ca00bbb1SStefan Hajnoczi 4384ca00bbb1SStefan Hajnoczi if (iothread->type == QTYPE_QSTRING) { 4385ca00bbb1SStefan Hajnoczi IOThread *obj = iothread_by_id(iothread->u.s); 4386ca00bbb1SStefan Hajnoczi if (!obj) { 4387ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find iothread %s", iothread->u.s); 4388ca00bbb1SStefan Hajnoczi return; 4389ca00bbb1SStefan Hajnoczi } 4390ca00bbb1SStefan Hajnoczi 4391ca00bbb1SStefan Hajnoczi new_context = iothread_get_aio_context(obj); 4392ca00bbb1SStefan Hajnoczi } else { 4393ca00bbb1SStefan Hajnoczi new_context = qemu_get_aio_context(); 4394ca00bbb1SStefan Hajnoczi } 4395ca00bbb1SStefan Hajnoczi 4396ca00bbb1SStefan Hajnoczi old_context = bdrv_get_aio_context(bs); 4397ca00bbb1SStefan Hajnoczi aio_context_acquire(old_context); 4398ca00bbb1SStefan Hajnoczi 4399ca00bbb1SStefan Hajnoczi bdrv_set_aio_context(bs, new_context); 4400ca00bbb1SStefan Hajnoczi 4401ca00bbb1SStefan Hajnoczi aio_context_release(old_context); 4402ca00bbb1SStefan Hajnoczi } 4403ca00bbb1SStefan Hajnoczi 44047e5c776dSVladimir Sementsov-Ogievskiy void qmp_x_block_latency_histogram_set( 44057e5c776dSVladimir Sementsov-Ogievskiy const char *device, 44067e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries, uint64List *boundaries, 44077e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_read, uint64List *boundaries_read, 44087e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_write, uint64List *boundaries_write, 44097e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_flush, uint64List *boundaries_flush, 44107e5c776dSVladimir Sementsov-Ogievskiy Error **errp) 44117e5c776dSVladimir Sementsov-Ogievskiy { 44127e5c776dSVladimir Sementsov-Ogievskiy BlockBackend *blk = blk_by_name(device); 44137e5c776dSVladimir Sementsov-Ogievskiy BlockAcctStats *stats; 44147e5c776dSVladimir Sementsov-Ogievskiy 44157e5c776dSVladimir Sementsov-Ogievskiy if (!blk) { 44167e5c776dSVladimir Sementsov-Ogievskiy error_setg(errp, "Device '%s' not found", device); 44177e5c776dSVladimir Sementsov-Ogievskiy return; 44187e5c776dSVladimir Sementsov-Ogievskiy } 44197e5c776dSVladimir Sementsov-Ogievskiy stats = blk_get_stats(blk); 44207e5c776dSVladimir Sementsov-Ogievskiy 44217e5c776dSVladimir Sementsov-Ogievskiy if (!has_boundaries && !has_boundaries_read && !has_boundaries_write && 44227e5c776dSVladimir Sementsov-Ogievskiy !has_boundaries_flush) 44237e5c776dSVladimir Sementsov-Ogievskiy { 44247e5c776dSVladimir Sementsov-Ogievskiy block_latency_histograms_clear(stats); 44257e5c776dSVladimir Sementsov-Ogievskiy return; 44267e5c776dSVladimir Sementsov-Ogievskiy } 44277e5c776dSVladimir Sementsov-Ogievskiy 44287e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_read) { 44297e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 44307e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_READ, 44317e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_read ? boundaries_read : boundaries); 44327e5c776dSVladimir Sementsov-Ogievskiy } 44337e5c776dSVladimir Sementsov-Ogievskiy 44347e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_write) { 44357e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 44367e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_WRITE, 44377e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_write ? boundaries_write : boundaries); 44387e5c776dSVladimir Sementsov-Ogievskiy } 44397e5c776dSVladimir Sementsov-Ogievskiy 44407e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_flush) { 44417e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 44427e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_FLUSH, 44437e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_flush ? boundaries_flush : boundaries); 44447e5c776dSVladimir Sementsov-Ogievskiy } 44457e5c776dSVladimir Sementsov-Ogievskiy } 44467e5c776dSVladimir Sementsov-Ogievskiy 44470006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 44484d454574SPaolo Bonzini .name = "drive", 44490006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 44504d454574SPaolo Bonzini .desc = { 44514d454574SPaolo Bonzini { 44524d454574SPaolo Bonzini .name = "snapshot", 44534d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 44544d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 44554d454574SPaolo Bonzini },{ 44564d454574SPaolo Bonzini .name = "aio", 44574d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 44584d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 44594d454574SPaolo Bonzini },{ 4460e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4461e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4462e4b24b49SKevin Wolf .help = "Enable writeback mode", 4463e4b24b49SKevin Wolf },{ 44644d454574SPaolo Bonzini .name = "format", 44654d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 44664d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 44674d454574SPaolo Bonzini },{ 44684d454574SPaolo Bonzini .name = "rerror", 44694d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 44704d454574SPaolo Bonzini .help = "read error action", 44714d454574SPaolo Bonzini },{ 44724d454574SPaolo Bonzini .name = "werror", 44734d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 44744d454574SPaolo Bonzini .help = "write error action", 44754d454574SPaolo Bonzini },{ 44764e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 44774d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 44784d454574SPaolo Bonzini .help = "open drive file as read-only", 4479a2a7862cSPradeep Jagadeesh }, 4480a2a7862cSPradeep Jagadeesh 4481a2a7862cSPradeep Jagadeesh THROTTLE_OPTS, 4482a2a7862cSPradeep Jagadeesh 4483a2a7862cSPradeep Jagadeesh { 448476f4afb4SAlberto Garcia .name = "throttling.group", 448576f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 448676f4afb4SAlberto Garcia .help = "name of the block throttling group", 448776f4afb4SAlberto Garcia },{ 44884d454574SPaolo Bonzini .name = "copy-on-read", 44894d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 44904d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4491465bee1dSPeter Lieven },{ 4492465bee1dSPeter Lieven .name = "detect-zeroes", 4493465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4494465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4495362e9299SAlberto Garcia },{ 4496362e9299SAlberto Garcia .name = "stats-account-invalid", 4497362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4498362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4499362e9299SAlberto Garcia "in the statistics", 4500362e9299SAlberto Garcia },{ 4501362e9299SAlberto Garcia .name = "stats-account-failed", 4502362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4503362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4504362e9299SAlberto Garcia "in the statistics", 45054d454574SPaolo Bonzini }, 45064d454574SPaolo Bonzini { /* end of list */ } 45074d454574SPaolo Bonzini }, 45084d454574SPaolo Bonzini }; 45090006383eSKevin Wolf 45100006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 45110006383eSKevin Wolf .name = "drive", 45120006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 45130006383eSKevin Wolf .desc = { 4514492fdc6fSKevin Wolf /* 4515492fdc6fSKevin Wolf * no elements => accept any params 4516492fdc6fSKevin Wolf * validation will happen later 4517492fdc6fSKevin Wolf */ 45180006383eSKevin Wolf { /* end of list */ } 45190006383eSKevin Wolf }, 45200006383eSKevin Wolf }; 4521