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) { 153b76e4458SLiang Li block_job_cancel(bs->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; 144994d16a64SJohn Snow BlockJobTxn *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 1867111049a4SJohn Snow static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *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 1928111049a4SJohn Snow block_job_cancel_sync(state->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 1957111049a4SJohn Snow static BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *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 2026111049a4SJohn Snow block_job_cancel_sync(state->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; 2055df9a681dSFam Zheng } BlockDirtyBitmapState; 2056df9a681dSFam Zheng 205750f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 2058df9a681dSFam Zheng Error **errp) 2059df9a681dSFam Zheng { 2060df9a681dSFam Zheng Error *local_err = NULL; 2061df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2062df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2063df9a681dSFam Zheng common, common); 2064df9a681dSFam Zheng 206594d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 206694d16a64SJohn Snow return; 206794d16a64SJohn Snow } 206894d16a64SJohn Snow 206932bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2070df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2071df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2072df9a681dSFam Zheng action->has_granularity, action->granularity, 2073fd5ae4ccSVladimir Sementsov-Ogievskiy action->has_persistent, action->persistent, 2074eb738bb5SVladimir Sementsov-Ogievskiy action->has_autoload, action->autoload, 2075df9a681dSFam Zheng &local_err); 2076df9a681dSFam Zheng 2077df9a681dSFam Zheng if (!local_err) { 2078df9a681dSFam Zheng state->prepared = true; 2079df9a681dSFam Zheng } else { 2080df9a681dSFam Zheng error_propagate(errp, local_err); 2081df9a681dSFam Zheng } 2082df9a681dSFam Zheng } 2083df9a681dSFam Zheng 208450f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2085df9a681dSFam Zheng { 2086df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2087df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2088df9a681dSFam Zheng common, common); 2089df9a681dSFam Zheng 209032bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2091df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2092df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2093df9a681dSFam Zheng */ 2094df9a681dSFam Zheng if (state->prepared) { 2095df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2096df9a681dSFam Zheng } 2097df9a681dSFam Zheng } 2098df9a681dSFam Zheng 209950f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2100df9a681dSFam Zheng Error **errp) 2101df9a681dSFam Zheng { 2102df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2103df9a681dSFam Zheng common, common); 2104df9a681dSFam Zheng BlockDirtyBitmap *action; 2105df9a681dSFam Zheng 210694d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 210794d16a64SJohn Snow return; 210894d16a64SJohn Snow } 210994d16a64SJohn Snow 211032bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2111df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2112df9a681dSFam Zheng action->name, 2113df9a681dSFam Zheng &state->bs, 2114df9a681dSFam Zheng errp); 2115df9a681dSFam Zheng if (!state->bitmap) { 2116df9a681dSFam Zheng return; 2117df9a681dSFam Zheng } 2118df9a681dSFam Zheng 2119df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2120df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2121df9a681dSFam Zheng return; 21224f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(state->bitmap)) { 21234f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, "Cannot modify a locked bitmap"); 21244f43e953SVladimir Sementsov-Ogievskiy return; 2125df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2126df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2127df9a681dSFam Zheng return; 2128d6883bc9SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(state->bitmap)) { 2129d6883bc9SVladimir Sementsov-Ogievskiy error_setg(errp, "Cannot clear a readonly bitmap"); 2130d6883bc9SVladimir Sementsov-Ogievskiy return; 2131df9a681dSFam Zheng } 2132df9a681dSFam Zheng 2133df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2134df9a681dSFam Zheng } 2135df9a681dSFam Zheng 213650f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2137df9a681dSFam Zheng { 2138df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2139df9a681dSFam Zheng common, common); 2140df9a681dSFam Zheng 2141184dd9c4SJohn Snow if (state->backup) { 2142df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2143df9a681dSFam Zheng } 2144184dd9c4SJohn Snow } 2145df9a681dSFam Zheng 214650f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2147df9a681dSFam Zheng { 2148df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2149df9a681dSFam Zheng common, common); 2150df9a681dSFam Zheng 2151df9a681dSFam Zheng hbitmap_free(state->backup); 2152df9a681dSFam Zheng } 2153df9a681dSFam Zheng 215450f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 215578b18b78SStefan Hajnoczi { 215678b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 215778b18b78SStefan Hajnoczi } 215878b18b78SStefan Hajnoczi 215950f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 216078b18b78SStefan Hajnoczi { 2161dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 216278b18b78SStefan Hajnoczi } 216378b18b78SStefan Hajnoczi 216450f43f0fSJohn Snow static const BlkActionOps actions[] = { 216543de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 216643de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 216743de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 216843de7e2dSAlberto Garcia .commit = external_snapshot_commit, 216943de7e2dSAlberto Garcia .abort = external_snapshot_abort, 21704ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 217143de7e2dSAlberto Garcia }, 2172c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2173ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2174ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2175ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2176ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2177da763e83SFam Zheng .clean = external_snapshot_clean, 2178ba0c86a3SWenchao Xia }, 21793037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21803037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21813037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 2182111049a4SJohn Snow .commit = drive_backup_commit, 21833037f364SStefan Hajnoczi .abort = drive_backup_abort, 21845d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21853037f364SStefan Hajnoczi }, 2186bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2187bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2188bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2189111049a4SJohn Snow .commit = blockdev_backup_commit, 2190bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2191bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2192bd8baecdSFam Zheng }, 219378b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 219450f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 219578b18b78SStefan Hajnoczi .prepare = abort_prepare, 219678b18b78SStefan Hajnoczi .commit = abort_commit, 219778b18b78SStefan Hajnoczi }, 2198bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2199bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2200bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2201bbe86010SWenchao Xia .abort = internal_snapshot_abort, 22025d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2203bbe86010SWenchao Xia }, 2204df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2205df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2206df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2207df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2208df9a681dSFam Zheng }, 2209df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2210df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2211df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2212df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2213df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2214df9a681dSFam Zheng } 2215ba0c86a3SWenchao Xia }; 2216ba0c86a3SWenchao Xia 221794d16a64SJohn Snow /** 221894d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 221994d16a64SJohn Snow * that structure with desired defaults if they are unset. 222094d16a64SJohn Snow */ 222194d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 222294d16a64SJohn Snow TransactionProperties *props) 222394d16a64SJohn Snow { 222494d16a64SJohn Snow if (!props) { 222594d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 222694d16a64SJohn Snow } 222794d16a64SJohn Snow 222894d16a64SJohn Snow if (!props->has_completion_mode) { 222994d16a64SJohn Snow props->has_completion_mode = true; 223094d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 223194d16a64SJohn Snow } 223294d16a64SJohn Snow 223394d16a64SJohn Snow return props; 223494d16a64SJohn Snow } 223594d16a64SJohn Snow 22368802d1fdSJeff Cody /* 2237b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2238b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 22398802d1fdSJeff Cody */ 224094d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 224194d16a64SJohn Snow bool has_props, 224294d16a64SJohn Snow struct TransactionProperties *props, 224394d16a64SJohn Snow Error **errp) 22448802d1fdSJeff Cody { 2245c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 224694d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 224750f43f0fSJohn Snow BlkActionState *state, *next; 224843e17041SLuiz Capitulino Error *local_err = NULL; 22498802d1fdSJeff Cody 225050f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 22518802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 22528802d1fdSJeff Cody 225394d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 225494d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 225594d16a64SJohn Snow */ 225694d16a64SJohn Snow props = get_transaction_properties(props); 225794d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 225894d16a64SJohn Snow block_job_txn = block_job_txn_new(); 225994d16a64SJohn Snow } 226094d16a64SJohn Snow 2261b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 22628802d1fdSJeff Cody bdrv_drain_all(); 22638802d1fdSJeff Cody 2264b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 22658802d1fdSJeff Cody while (NULL != dev_entry) { 2266c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 226750f43f0fSJohn Snow const BlkActionOps *ops; 226852e7c241SPaolo Bonzini 22698802d1fdSJeff Cody dev_info = dev_entry->value; 22708802d1fdSJeff Cody dev_entry = dev_entry->next; 22718802d1fdSJeff Cody 22726a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2273ba0c86a3SWenchao Xia 22746a8f9661SEric Blake ops = &actions[dev_info->type]; 2275aa3fe714SMax Reitz assert(ops->instance_size > 0); 2276aa3fe714SMax Reitz 2277ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2278ba5d6ab6SStefan Hajnoczi state->ops = ops; 2279ba5d6ab6SStefan Hajnoczi state->action = dev_info; 228094d16a64SJohn Snow state->block_job_txn = block_job_txn; 228194d16a64SJohn Snow state->txn_props = props; 2282ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22838802d1fdSJeff Cody 2284ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 228584d18f06SMarkus Armbruster if (local_err) { 22869b9877eeSWenchao Xia error_propagate(errp, local_err); 22879b9877eeSWenchao Xia goto delete_and_fail; 22889b9877eeSWenchao Xia } 228952e7c241SPaolo Bonzini } 22908802d1fdSJeff Cody 2291ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2292f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2293ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22948802d1fdSJeff Cody } 2295f9ea81e8SStefan Hajnoczi } 22968802d1fdSJeff Cody 22978802d1fdSJeff Cody /* success */ 22988802d1fdSJeff Cody goto exit; 22998802d1fdSJeff Cody 23008802d1fdSJeff Cody delete_and_fail: 2301b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2302ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2303ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2304ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2305ba0c86a3SWenchao Xia } 23068802d1fdSJeff Cody } 23078802d1fdSJeff Cody exit: 2308ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2309ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2310ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2311ba0c86a3SWenchao Xia } 2312ba5d6ab6SStefan Hajnoczi g_free(state); 23138802d1fdSJeff Cody } 231494d16a64SJohn Snow if (!has_props) { 231594d16a64SJohn Snow qapi_free_TransactionProperties(props); 231694d16a64SJohn Snow } 231794d16a64SJohn Snow block_job_txn_unref(block_job_txn); 23188802d1fdSJeff Cody } 23198802d1fdSJeff Cody 2320fbe2d816SKevin Wolf void qmp_eject(bool has_device, const char *device, 2321fbe2d816SKevin Wolf bool has_id, const char *id, 2322fbe2d816SKevin Wolf bool has_force, bool force, Error **errp) 2323666daa68SMarkus Armbruster { 232438f54bd1SMax Reitz Error *local_err = NULL; 23253a3086b7SJohn Snow int rc; 2326666daa68SMarkus Armbruster 23273a3086b7SJohn Snow if (!has_force) { 23283a3086b7SJohn Snow force = false; 23293a3086b7SJohn Snow } 23303a3086b7SJohn Snow 2331fbe2d816SKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2332fbe2d816SKevin Wolf has_id ? id : NULL, 2333fbe2d816SKevin Wolf force, &local_err); 2334bf18bee5SColin Lord if (rc && rc != -ENOSYS) { 233538f54bd1SMax Reitz error_propagate(errp, local_err); 2336c245b6a3SLuiz Capitulino return; 2337666daa68SMarkus Armbruster } 2338bf18bee5SColin Lord error_free(local_err); 23393a3086b7SJohn Snow 234082fcf66eSMax Reitz blockdev_remove_medium(has_device, device, has_id, id, errp); 2341666daa68SMarkus Armbruster } 2342666daa68SMarkus Armbruster 234312d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 234412d3ba82SBenoît Canet bool has_node_name, const char *node_name, 234512d3ba82SBenoît Canet const char *password, Error **errp) 2346666daa68SMarkus Armbruster { 2347c01c214bSDaniel P. Berrange error_setg(errp, 2348c01c214bSDaniel P. Berrange "Setting block passwords directly is no longer supported"); 2349e3442099SStefan Hajnoczi } 2350e3442099SStefan Hajnoczi 2351bf18bee5SColin Lord /* 2352bf18bee5SColin Lord * Attempt to open the tray of @device. 2353bf18bee5SColin Lord * If @force, ignore its tray lock. 2354bf18bee5SColin Lord * Else, if the tray is locked, don't open it, but ask the guest to open it. 2355bf18bee5SColin Lord * On error, store an error through @errp and return -errno. 2356bf18bee5SColin Lord * If @device does not exist, return -ENODEV. 2357bf18bee5SColin Lord * If it has no removable media, return -ENOTSUP. 2358bf18bee5SColin Lord * If it has no tray, return -ENOSYS. 2359bf18bee5SColin Lord * If the guest was asked to open the tray, return -EINPROGRESS. 2360bf18bee5SColin Lord * Else, return 0. 23613a3086b7SJohn Snow */ 2362b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id, 2363b33945cfSKevin Wolf bool force, Error **errp) 23647d8a9f71SMax Reitz { 23657d8a9f71SMax Reitz BlockBackend *blk; 2366b33945cfSKevin Wolf const char *device = qdev_id ?: blk_name; 23677d8a9f71SMax Reitz bool locked; 23687d8a9f71SMax Reitz 2369b33945cfSKevin Wolf blk = qmp_get_blk(blk_name, qdev_id, errp); 23707d8a9f71SMax Reitz if (!blk) { 23713a3086b7SJohn Snow return -ENODEV; 23727d8a9f71SMax Reitz } 23737d8a9f71SMax Reitz 23747d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23757d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23763a3086b7SJohn Snow return -ENOTSUP; 23777d8a9f71SMax Reitz } 23787d8a9f71SMax Reitz 237912c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 2380bf18bee5SColin Lord error_setg(errp, "Device '%s' does not have a tray", device); 2381bf18bee5SColin Lord return -ENOSYS; 238212c7ec87SMax Reitz } 238312c7ec87SMax Reitz 23847d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23853a3086b7SJohn Snow return 0; 23867d8a9f71SMax Reitz } 23877d8a9f71SMax Reitz 23887d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23897d8a9f71SMax Reitz if (locked) { 23907d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23917d8a9f71SMax Reitz } 23927d8a9f71SMax Reitz 23937d8a9f71SMax Reitz if (!locked || force) { 239439829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 23957d8a9f71SMax Reitz } 23963a3086b7SJohn Snow 23973a3086b7SJohn Snow if (locked && !force) { 2398bf18bee5SColin Lord error_setg(errp, "Device '%s' is locked and force was not specified, " 2399bf18bee5SColin Lord "wait for tray to open and try again", device); 2400bf18bee5SColin Lord return -EINPROGRESS; 24013a3086b7SJohn Snow } 24023a3086b7SJohn Snow 24033a3086b7SJohn Snow return 0; 24043a3086b7SJohn Snow } 24053a3086b7SJohn Snow 2406b33945cfSKevin Wolf void qmp_blockdev_open_tray(bool has_device, const char *device, 2407b33945cfSKevin Wolf bool has_id, const char *id, 2408b33945cfSKevin Wolf bool has_force, bool force, 24093a3086b7SJohn Snow Error **errp) 24103a3086b7SJohn Snow { 2411bf18bee5SColin Lord Error *local_err = NULL; 2412bf18bee5SColin Lord int rc; 2413bf18bee5SColin Lord 24143a3086b7SJohn Snow if (!has_force) { 24153a3086b7SJohn Snow force = false; 24163a3086b7SJohn Snow } 2417b33945cfSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2418b33945cfSKevin Wolf has_id ? id : NULL, 2419b33945cfSKevin Wolf force, &local_err); 2420bf18bee5SColin Lord if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { 2421bf18bee5SColin Lord error_propagate(errp, local_err); 2422bf18bee5SColin Lord return; 2423bf18bee5SColin Lord } 2424bf18bee5SColin Lord error_free(local_err); 24257d8a9f71SMax Reitz } 24267d8a9f71SMax Reitz 2427b33945cfSKevin Wolf void qmp_blockdev_close_tray(bool has_device, const char *device, 2428b33945cfSKevin Wolf bool has_id, const char *id, 2429b33945cfSKevin Wolf Error **errp) 2430abaaf59dSMax Reitz { 2431abaaf59dSMax Reitz BlockBackend *blk; 243239829a01SKevin Wolf Error *local_err = NULL; 2433abaaf59dSMax Reitz 2434b33945cfSKevin Wolf device = has_device ? device : NULL; 2435b33945cfSKevin Wolf id = has_id ? id : NULL; 2436b33945cfSKevin Wolf 2437b33945cfSKevin Wolf blk = qmp_get_blk(device, id, errp); 2438abaaf59dSMax Reitz if (!blk) { 2439abaaf59dSMax Reitz return; 2440abaaf59dSMax Reitz } 2441abaaf59dSMax Reitz 2442abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2443b33945cfSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 2444abaaf59dSMax Reitz return; 2445abaaf59dSMax Reitz } 2446abaaf59dSMax Reitz 244712c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 244812c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 244912c7ec87SMax Reitz return; 245012c7ec87SMax Reitz } 245112c7ec87SMax Reitz 2452abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2453abaaf59dSMax Reitz return; 2454abaaf59dSMax Reitz } 2455abaaf59dSMax Reitz 245639829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 245739829a01SKevin Wolf if (local_err) { 245839829a01SKevin Wolf error_propagate(errp, local_err); 245939829a01SKevin Wolf return; 246039829a01SKevin Wolf } 2461abaaf59dSMax Reitz } 2462abaaf59dSMax Reitz 246382fcf66eSMax Reitz static void blockdev_remove_medium(bool has_device, const char *device, 246400949babSKevin Wolf bool has_id, const char *id, Error **errp) 24652814f672SMax Reitz { 24662814f672SMax Reitz BlockBackend *blk; 24672814f672SMax Reitz BlockDriverState *bs; 24682814f672SMax Reitz AioContext *aio_context; 246900949babSKevin Wolf bool has_attached_device; 24702814f672SMax Reitz 247100949babSKevin Wolf device = has_device ? device : NULL; 247200949babSKevin Wolf id = has_id ? id : NULL; 247300949babSKevin Wolf 247400949babSKevin Wolf blk = qmp_get_blk(device, id, errp); 24752814f672SMax Reitz if (!blk) { 24762814f672SMax Reitz return; 24772814f672SMax Reitz } 24782814f672SMax Reitz 24792814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 248000949babSKevin Wolf has_attached_device = blk_get_attached_dev(blk); 24812814f672SMax Reitz 248200949babSKevin Wolf if (has_attached_device && !blk_dev_has_removable_media(blk)) { 248300949babSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 24842814f672SMax Reitz return; 24852814f672SMax Reitz } 24862814f672SMax Reitz 248700949babSKevin Wolf if (has_attached_device && blk_dev_has_tray(blk) && 248800949babSKevin Wolf !blk_dev_is_tray_open(blk)) 248900949babSKevin Wolf { 249000949babSKevin Wolf error_setg(errp, "Tray of device '%s' is not open", device ?: id); 24912814f672SMax Reitz return; 24922814f672SMax Reitz } 24932814f672SMax Reitz 24942814f672SMax Reitz bs = blk_bs(blk); 24952814f672SMax Reitz if (!bs) { 24962814f672SMax Reitz return; 24972814f672SMax Reitz } 24982814f672SMax Reitz 24992814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 25002814f672SMax Reitz aio_context_acquire(aio_context); 25012814f672SMax Reitz 25022814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 25032814f672SMax Reitz goto out; 25042814f672SMax Reitz } 25052814f672SMax Reitz 25062814f672SMax Reitz blk_remove_bs(blk); 25072814f672SMax Reitz 250812c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 250912c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 251012c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 251112c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 251212c7ec87SMax Reitz * value passed here (i.e. false). */ 251339829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 251412c7ec87SMax Reitz } 251512c7ec87SMax Reitz 25162814f672SMax Reitz out: 25172814f672SMax Reitz aio_context_release(aio_context); 25182814f672SMax Reitz } 25192814f672SMax Reitz 252034ce1111SMax Reitz void qmp_blockdev_remove_medium(const char *id, Error **errp) 252182fcf66eSMax Reitz { 252282fcf66eSMax Reitz blockdev_remove_medium(false, NULL, true, id, errp); 252382fcf66eSMax Reitz } 252482fcf66eSMax Reitz 2525716df217SKevin Wolf static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, 2526d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2527d1299882SMax Reitz { 252839829a01SKevin Wolf Error *local_err = NULL; 2529d1299882SMax Reitz bool has_device; 2530d7086422SKevin Wolf int ret; 2531d1299882SMax Reitz 2532d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2533d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2534d1299882SMax Reitz 2535d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2536716df217SKevin Wolf error_setg(errp, "Device is not removable"); 2537d1299882SMax Reitz return; 2538d1299882SMax Reitz } 2539d1299882SMax Reitz 254012c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2541716df217SKevin Wolf error_setg(errp, "Tray of the device is not open"); 2542d1299882SMax Reitz return; 2543d1299882SMax Reitz } 2544d1299882SMax Reitz 2545d1299882SMax Reitz if (blk_bs(blk)) { 2546716df217SKevin Wolf error_setg(errp, "There already is a medium in the device"); 2547d1299882SMax Reitz return; 2548d1299882SMax Reitz } 2549d1299882SMax Reitz 2550d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 2551d7086422SKevin Wolf if (ret < 0) { 2552d7086422SKevin Wolf return; 2553d7086422SKevin Wolf } 2554d1299882SMax Reitz 255512c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 255612c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 255712c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 255812c7ec87SMax Reitz * slot here. 255912c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 256012c7ec87SMax Reitz * value passed here (i.e. true). */ 256139829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 256239829a01SKevin Wolf if (local_err) { 256339829a01SKevin Wolf error_propagate(errp, local_err); 256439829a01SKevin Wolf blk_remove_bs(blk); 256539829a01SKevin Wolf return; 256639829a01SKevin Wolf } 256712c7ec87SMax Reitz } 2568d1299882SMax Reitz } 2569d1299882SMax Reitz 257082fcf66eSMax Reitz static void blockdev_insert_medium(bool has_device, const char *device, 2571716df217SKevin Wolf bool has_id, const char *id, 2572716df217SKevin Wolf const char *node_name, Error **errp) 2573d1299882SMax Reitz { 2574716df217SKevin Wolf BlockBackend *blk; 2575d1299882SMax Reitz BlockDriverState *bs; 2576d1299882SMax Reitz 2577716df217SKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 2578716df217SKevin Wolf has_id ? id : NULL, 2579716df217SKevin Wolf errp); 2580716df217SKevin Wolf if (!blk) { 2581716df217SKevin Wolf return; 2582716df217SKevin Wolf } 2583716df217SKevin Wolf 2584d1299882SMax Reitz bs = bdrv_find_node(node_name); 2585d1299882SMax Reitz if (!bs) { 2586d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2587d1299882SMax Reitz return; 2588d1299882SMax Reitz } 2589d1299882SMax Reitz 25901f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2591e467da7bSKevin Wolf error_setg(errp, "Node '%s' is already in use", node_name); 2592d1299882SMax Reitz return; 2593d1299882SMax Reitz } 2594d1299882SMax Reitz 2595716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, bs, errp); 2596d1299882SMax Reitz } 2597d1299882SMax Reitz 259834ce1111SMax Reitz void qmp_blockdev_insert_medium(const char *id, const char *node_name, 259982fcf66eSMax Reitz Error **errp) 260082fcf66eSMax Reitz { 260182fcf66eSMax Reitz blockdev_insert_medium(false, NULL, true, id, node_name, errp); 260282fcf66eSMax Reitz } 260382fcf66eSMax Reitz 260470e2cb3bSKevin Wolf void qmp_blockdev_change_medium(bool has_device, const char *device, 260570e2cb3bSKevin Wolf bool has_id, const char *id, 260670e2cb3bSKevin Wolf const char *filename, 260724fb4133SMax Reitz bool has_format, const char *format, 260839ff43d9SMax Reitz bool has_read_only, 260939ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 261024fb4133SMax Reitz Error **errp) 2611de2c6c05SMax Reitz { 2612de2c6c05SMax Reitz BlockBackend *blk; 2613de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 26145b363937SMax Reitz int bdrv_flags; 2615b85114f8SKevin Wolf bool detect_zeroes; 261638a53d50SColin Lord int rc; 2617de2c6c05SMax Reitz QDict *options = NULL; 2618de2c6c05SMax Reitz Error *err = NULL; 2619de2c6c05SMax Reitz 262070e2cb3bSKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 262170e2cb3bSKevin Wolf has_id ? id : NULL, 262270e2cb3bSKevin Wolf errp); 2623de2c6c05SMax Reitz if (!blk) { 2624de2c6c05SMax Reitz goto fail; 2625de2c6c05SMax Reitz } 2626de2c6c05SMax Reitz 2627de2c6c05SMax Reitz if (blk_bs(blk)) { 2628de2c6c05SMax Reitz blk_update_root_state(blk); 2629de2c6c05SMax Reitz } 2630de2c6c05SMax Reitz 2631de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2632156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2633156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2634de2c6c05SMax Reitz 263539ff43d9SMax Reitz if (!has_read_only) { 263639ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 263739ff43d9SMax Reitz } 263839ff43d9SMax Reitz 263939ff43d9SMax Reitz switch (read_only) { 264039ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 264139ff43d9SMax Reitz break; 264239ff43d9SMax Reitz 264339ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 264439ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 264539ff43d9SMax Reitz break; 264639ff43d9SMax Reitz 264739ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 264839ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 264939ff43d9SMax Reitz break; 265039ff43d9SMax Reitz 265139ff43d9SMax Reitz default: 265239ff43d9SMax Reitz abort(); 265339ff43d9SMax Reitz } 265439ff43d9SMax Reitz 2655de2c6c05SMax Reitz options = qdict_new(); 2656b85114f8SKevin Wolf detect_zeroes = blk_get_detect_zeroes_from_root_state(blk); 265746f5ac20SEric Blake qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off"); 2658b85114f8SKevin Wolf 2659b85114f8SKevin Wolf if (has_format) { 266046f5ac20SEric Blake qdict_put_str(options, "driver", format); 2661de2c6c05SMax Reitz } 2662de2c6c05SMax Reitz 26635b363937SMax Reitz medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 26645b363937SMax Reitz if (!medium_bs) { 2665de2c6c05SMax Reitz goto fail; 2666de2c6c05SMax Reitz } 2667de2c6c05SMax Reitz 266870e2cb3bSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 266970e2cb3bSKevin Wolf has_id ? id : NULL, 267070e2cb3bSKevin Wolf false, &err); 267138a53d50SColin Lord if (rc && rc != -ENOSYS) { 2672de2c6c05SMax Reitz error_propagate(errp, err); 2673de2c6c05SMax Reitz goto fail; 2674de2c6c05SMax Reitz } 267538a53d50SColin Lord error_free(err); 267638a53d50SColin Lord err = NULL; 2677de2c6c05SMax Reitz 267882fcf66eSMax Reitz blockdev_remove_medium(has_device, device, has_id, id, &err); 2679de2c6c05SMax Reitz if (err) { 2680de2c6c05SMax Reitz error_propagate(errp, err); 2681de2c6c05SMax Reitz goto fail; 2682de2c6c05SMax Reitz } 2683de2c6c05SMax Reitz 2684716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, medium_bs, &err); 2685de2c6c05SMax Reitz if (err) { 2686de2c6c05SMax Reitz error_propagate(errp, err); 2687de2c6c05SMax Reitz goto fail; 2688de2c6c05SMax Reitz } 2689de2c6c05SMax Reitz 269070e2cb3bSKevin Wolf qmp_blockdev_close_tray(has_device, device, has_id, id, errp); 2691de2c6c05SMax Reitz 2692de2c6c05SMax Reitz fail: 2693de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2694de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2695de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2696de2c6c05SMax Reitz bdrv_unref(medium_bs); 2697de2c6c05SMax Reitz } 2698de2c6c05SMax Reitz 2699727f005eSZhi Yong Wu /* throttling disk I/O limits */ 27004dc9397bSEric Blake void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) 2701727f005eSZhi Yong Wu { 2702cc0681c4SBenoît Canet ThrottleConfig cfg; 2703727f005eSZhi Yong Wu BlockDriverState *bs; 2704a0e8544cSFam Zheng BlockBackend *blk; 2705b15446fdSStefan Hajnoczi AioContext *aio_context; 2706727f005eSZhi Yong Wu 27077a9877a0SKevin Wolf blk = qmp_get_blk(arg->has_device ? arg->device : NULL, 27087a9877a0SKevin Wolf arg->has_id ? arg->id : NULL, 27097a9877a0SKevin Wolf errp); 2710a0e8544cSFam Zheng if (!blk) { 271180047da5SLuiz Capitulino return; 2712727f005eSZhi Yong Wu } 27135433c24fSMax Reitz 27145433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 27155433c24fSMax Reitz aio_context_acquire(aio_context); 27165433c24fSMax Reitz 2717a0e8544cSFam Zheng bs = blk_bs(blk); 27185433c24fSMax Reitz if (!bs) { 27197a9877a0SKevin Wolf error_setg(errp, "Device has no medium"); 27205433c24fSMax Reitz goto out; 27215433c24fSMax Reitz } 2722727f005eSZhi Yong Wu 27231588ab5dSAlberto Garcia throttle_config_init(&cfg); 27244dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; 27254dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; 27264dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; 2727727f005eSZhi Yong Wu 27284dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; 27294dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; 27304dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; 2731cc0681c4SBenoît Canet 27324dc9397bSEric Blake if (arg->has_bps_max) { 27334dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; 27343e9fab69SBenoît Canet } 27354dc9397bSEric Blake if (arg->has_bps_rd_max) { 27364dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; 27373e9fab69SBenoît Canet } 27384dc9397bSEric Blake if (arg->has_bps_wr_max) { 27394dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; 27403e9fab69SBenoît Canet } 27414dc9397bSEric Blake if (arg->has_iops_max) { 27424dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; 27433e9fab69SBenoît Canet } 27444dc9397bSEric Blake if (arg->has_iops_rd_max) { 27454dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; 27463e9fab69SBenoît Canet } 27474dc9397bSEric Blake if (arg->has_iops_wr_max) { 27484dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; 27493e9fab69SBenoît Canet } 2750cc0681c4SBenoît Canet 27514dc9397bSEric Blake if (arg->has_bps_max_length) { 27524dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; 2753dce13204SAlberto Garcia } 27544dc9397bSEric Blake if (arg->has_bps_rd_max_length) { 27554dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; 2756dce13204SAlberto Garcia } 27574dc9397bSEric Blake if (arg->has_bps_wr_max_length) { 27584dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; 2759dce13204SAlberto Garcia } 27604dc9397bSEric Blake if (arg->has_iops_max_length) { 27614dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; 2762dce13204SAlberto Garcia } 27634dc9397bSEric Blake if (arg->has_iops_rd_max_length) { 27644dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; 2765dce13204SAlberto Garcia } 27664dc9397bSEric Blake if (arg->has_iops_wr_max_length) { 27674dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; 2768dce13204SAlberto Garcia } 2769dce13204SAlberto Garcia 27704dc9397bSEric Blake if (arg->has_iops_size) { 27714dc9397bSEric Blake cfg.op_size = arg->iops_size; 27722024c1dfSBenoît Canet } 2773cc0681c4SBenoît Canet 2774d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 27755433c24fSMax Reitz goto out; 2776727f005eSZhi Yong Wu } 2777727f005eSZhi Yong Wu 277876f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 277976f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 278076f4afb4SAlberto Garcia * just update the throttling group. */ 2781022cdc9fSManos Pitsidianakis if (!blk_get_public(blk)->throttle_group_member.throttle_state) { 27824dc9397bSEric Blake blk_io_limits_enable(blk, 27837a9877a0SKevin Wolf arg->has_group ? arg->group : 27847a9877a0SKevin Wolf arg->has_device ? arg->device : 27857a9877a0SKevin Wolf arg->id); 27864dc9397bSEric Blake } else if (arg->has_group) { 27874dc9397bSEric Blake blk_io_limits_update_group(blk, arg->group); 2788727f005eSZhi Yong Wu } 278976f4afb4SAlberto Garcia /* Set the new throttling configuration */ 279097148076SKevin Wolf blk_set_io_limits(blk, &cfg); 2791022cdc9fSManos Pitsidianakis } else if (blk_get_public(blk)->throttle_group_member.throttle_state) { 279276f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 279397148076SKevin Wolf blk_io_limits_disable(blk); 2794727f005eSZhi Yong Wu } 2795b15446fdSStefan Hajnoczi 27965433c24fSMax Reitz out: 2797b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2798727f005eSZhi Yong Wu } 2799727f005eSZhi Yong Wu 2800341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2801341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2802fd5ae4ccSVladimir Sementsov-Ogievskiy bool has_persistent, bool persistent, 2803eb738bb5SVladimir Sementsov-Ogievskiy bool has_autoload, bool autoload, 2804341ebc2fSJohn Snow Error **errp) 2805341ebc2fSJohn Snow { 2806341ebc2fSJohn Snow BlockDriverState *bs; 2807fd5ae4ccSVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 2808341ebc2fSJohn Snow 2809341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2810341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2811341ebc2fSJohn Snow return; 2812341ebc2fSJohn Snow } 2813341ebc2fSJohn Snow 2814341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2815341ebc2fSJohn Snow if (!bs) { 2816341ebc2fSJohn Snow return; 2817341ebc2fSJohn Snow } 2818341ebc2fSJohn Snow 2819341ebc2fSJohn Snow if (has_granularity) { 2820341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2821341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2822341ebc2fSJohn Snow "and at least 512"); 28232119882cSPaolo Bonzini return; 2824341ebc2fSJohn Snow } 2825341ebc2fSJohn Snow } else { 2826341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2827341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2828341ebc2fSJohn Snow } 2829341ebc2fSJohn Snow 2830fd5ae4ccSVladimir Sementsov-Ogievskiy if (!has_persistent) { 2831fd5ae4ccSVladimir Sementsov-Ogievskiy persistent = false; 2832fd5ae4ccSVladimir Sementsov-Ogievskiy } 2833eb738bb5SVladimir Sementsov-Ogievskiy 28343e99da5eSVladimir Sementsov-Ogievskiy if (has_autoload) { 28353e99da5eSVladimir Sementsov-Ogievskiy warn_report("Autoload option is deprecated and its value is ignored"); 2836eb738bb5SVladimir Sementsov-Ogievskiy } 2837fd5ae4ccSVladimir Sementsov-Ogievskiy 2838fd5ae4ccSVladimir Sementsov-Ogievskiy if (persistent && 2839fd5ae4ccSVladimir Sementsov-Ogievskiy !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp)) 2840fd5ae4ccSVladimir Sementsov-Ogievskiy { 2841fd5ae4ccSVladimir Sementsov-Ogievskiy return; 2842fd5ae4ccSVladimir Sementsov-Ogievskiy } 2843fd5ae4ccSVladimir Sementsov-Ogievskiy 2844fd5ae4ccSVladimir Sementsov-Ogievskiy bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2845eb738bb5SVladimir Sementsov-Ogievskiy if (bitmap == NULL) { 2846eb738bb5SVladimir Sementsov-Ogievskiy return; 2847fd5ae4ccSVladimir Sementsov-Ogievskiy } 2848eb738bb5SVladimir Sementsov-Ogievskiy 2849eb738bb5SVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_set_persistance(bitmap, persistent); 2850341ebc2fSJohn Snow } 2851341ebc2fSJohn Snow 2852341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2853341ebc2fSJohn Snow Error **errp) 2854341ebc2fSJohn Snow { 2855341ebc2fSJohn Snow BlockDriverState *bs; 2856341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 28575c36c1afSVladimir Sementsov-Ogievskiy Error *local_err = NULL; 2858341ebc2fSJohn Snow 28592119882cSPaolo Bonzini bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2860341ebc2fSJohn Snow if (!bitmap || !bs) { 2861341ebc2fSJohn Snow return; 2862341ebc2fSJohn Snow } 2863341ebc2fSJohn Snow 28649bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 28659bd2b08fSJohn Snow error_setg(errp, 28669bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 28679bd2b08fSJohn Snow name); 28682119882cSPaolo Bonzini return; 28694f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) { 28704f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 28714f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be removed", 28724f43e953SVladimir Sementsov-Ogievskiy name); 28734f43e953SVladimir Sementsov-Ogievskiy return; 28749bd2b08fSJohn Snow } 28755c36c1afSVladimir Sementsov-Ogievskiy 28765c36c1afSVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_get_persistance(bitmap)) { 28775c36c1afSVladimir Sementsov-Ogievskiy bdrv_remove_persistent_dirty_bitmap(bs, name, &local_err); 28785c36c1afSVladimir Sementsov-Ogievskiy if (local_err != NULL) { 28795c36c1afSVladimir Sementsov-Ogievskiy error_propagate(errp, local_err); 28805c36c1afSVladimir Sementsov-Ogievskiy return; 28815c36c1afSVladimir Sementsov-Ogievskiy } 28825c36c1afSVladimir Sementsov-Ogievskiy } 28835c36c1afSVladimir Sementsov-Ogievskiy 288420dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2885341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2886341ebc2fSJohn Snow } 2887341ebc2fSJohn Snow 2888e74e6b78SJohn Snow /** 2889e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2890e74e6b78SJohn Snow * immediately after a full backup operation. 2891e74e6b78SJohn Snow */ 2892e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2893e74e6b78SJohn Snow Error **errp) 2894e74e6b78SJohn Snow { 2895e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2896e74e6b78SJohn Snow BlockDriverState *bs; 2897e74e6b78SJohn Snow 28982119882cSPaolo Bonzini bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2899e74e6b78SJohn Snow if (!bitmap || !bs) { 2900e74e6b78SJohn Snow return; 2901e74e6b78SJohn Snow } 2902e74e6b78SJohn Snow 2903e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2904e74e6b78SJohn Snow error_setg(errp, 2905e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2906e74e6b78SJohn Snow name); 29072119882cSPaolo Bonzini return; 29084f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) { 29094f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 29104f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be modified", 29114f43e953SVladimir Sementsov-Ogievskiy name); 29124f43e953SVladimir Sementsov-Ogievskiy return; 2913e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2914e74e6b78SJohn Snow error_setg(errp, 2915e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2916e74e6b78SJohn Snow name); 29172119882cSPaolo Bonzini return; 2918d6883bc9SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(bitmap)) { 2919d6883bc9SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap '%s' is readonly and cannot be cleared", name); 2920d6883bc9SVladimir Sementsov-Ogievskiy return; 2921e74e6b78SJohn Snow } 2922e74e6b78SJohn Snow 2923df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2924e74e6b78SJohn Snow } 2925e74e6b78SJohn Snow 2926a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, 2927a3b52535SVladimir Sementsov-Ogievskiy const char *name, 2928a3b52535SVladimir Sementsov-Ogievskiy Error **errp) 2929a3b52535SVladimir Sementsov-Ogievskiy { 2930a3b52535SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 2931a3b52535SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 2932a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *ret = NULL; 2933a3b52535SVladimir Sementsov-Ogievskiy char *sha256; 2934a3b52535SVladimir Sementsov-Ogievskiy 2935a3b52535SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2936a3b52535SVladimir Sementsov-Ogievskiy if (!bitmap || !bs) { 2937a3b52535SVladimir Sementsov-Ogievskiy return NULL; 2938a3b52535SVladimir Sementsov-Ogievskiy } 2939a3b52535SVladimir Sementsov-Ogievskiy 2940a3b52535SVladimir Sementsov-Ogievskiy sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp); 2941a3b52535SVladimir Sementsov-Ogievskiy if (sha256 == NULL) { 2942a3b52535SVladimir Sementsov-Ogievskiy return NULL; 2943a3b52535SVladimir Sementsov-Ogievskiy } 2944a3b52535SVladimir Sementsov-Ogievskiy 2945a3b52535SVladimir Sementsov-Ogievskiy ret = g_new(BlockDirtyBitmapSha256, 1); 2946a3b52535SVladimir Sementsov-Ogievskiy ret->sha256 = sha256; 2947a3b52535SVladimir Sementsov-Ogievskiy 2948a3b52535SVladimir Sementsov-Ogievskiy return ret; 2949a3b52535SVladimir Sementsov-Ogievskiy } 2950a3b52535SVladimir Sementsov-Ogievskiy 2951072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 29529063f814SRyan Harper { 29539063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 29547e7d56d9SMarkus Armbruster BlockBackend *blk; 29559063f814SRyan Harper BlockDriverState *bs; 29568ad4202bSStefan Hajnoczi AioContext *aio_context; 29573718d8abSFam Zheng Error *local_err = NULL; 29589063f814SRyan Harper 29592073d410SKevin Wolf bs = bdrv_find_node(id); 29602073d410SKevin Wolf if (bs) { 296179b7a77eSMarkus Armbruster qmp_blockdev_del(id, &local_err); 29622073d410SKevin Wolf if (local_err) { 29632073d410SKevin Wolf error_report_err(local_err); 29642073d410SKevin Wolf } 29652073d410SKevin Wolf return; 29662073d410SKevin Wolf } 29672073d410SKevin Wolf 29687e7d56d9SMarkus Armbruster blk = blk_by_name(id); 29697e7d56d9SMarkus Armbruster if (!blk) { 2970b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2971072ebe6bSMarkus Armbruster return; 29729063f814SRyan Harper } 29738ad4202bSStefan Hajnoczi 297426f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 297548f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 297648f364ddSMarkus Armbruster " is not supported"); 2977072ebe6bSMarkus Armbruster return; 297848f364ddSMarkus Armbruster } 297948f364ddSMarkus Armbruster 29805433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 29818ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 29828ad4202bSStefan Hajnoczi 29835433c24fSMax Reitz bs = blk_bs(blk); 29845433c24fSMax Reitz if (bs) { 29853718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2986565f65d2SMarkus Armbruster error_report_err(local_err); 29878ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2988072ebe6bSMarkus Armbruster return; 29898591675fSMarcelo Tosatti } 29909063f814SRyan Harper 2991938abd43SMax Reitz blk_remove_bs(blk); 29925433c24fSMax Reitz } 29939063f814SRyan Harper 29947c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 2995efaa7c4eSMax Reitz monitor_remove_blk(blk); 2996efaa7c4eSMax Reitz 29977c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 29987c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 2999d22b2f41SRyan Harper */ 3000a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 3001293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 3002373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 3003293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 3004d22b2f41SRyan Harper } else { 3005b9fe8a7aSMarkus Armbruster blk_unref(blk); 3006d22b2f41SRyan Harper } 30079063f814SRyan Harper 30088ad4202bSStefan Hajnoczi aio_context_release(aio_context); 30099063f814SRyan Harper } 30106d4a2b3aSChristoph Hellwig 30113b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 30123b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 30133b1dbd11SBenoît Canet int64_t size, Error **errp) 30146d4a2b3aSChristoph Hellwig { 30153b1dbd11SBenoît Canet Error *local_err = NULL; 30167dad9ee6SKevin Wolf BlockBackend *blk = NULL; 30176d4a2b3aSChristoph Hellwig BlockDriverState *bs; 3018927e0e76SStefan Hajnoczi AioContext *aio_context; 30198732901eSKevin Wolf int ret; 30206d4a2b3aSChristoph Hellwig 30213b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 30223b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 30233b1dbd11SBenoît Canet &local_err); 302484d18f06SMarkus Armbruster if (local_err) { 30253b1dbd11SBenoît Canet error_propagate(errp, local_err); 30263b1dbd11SBenoît Canet return; 30273b1dbd11SBenoît Canet } 30283b1dbd11SBenoît Canet 3029927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 3030927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 3031927e0e76SStefan Hajnoczi 30323b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 3033c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 3034927e0e76SStefan Hajnoczi goto out; 30356d4a2b3aSChristoph Hellwig } 30366d4a2b3aSChristoph Hellwig 30376d4a2b3aSChristoph Hellwig if (size < 0) { 3038c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 3039927e0e76SStefan Hajnoczi goto out; 30406d4a2b3aSChristoph Hellwig } 30416d4a2b3aSChristoph Hellwig 30429c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 3043c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 3044927e0e76SStefan Hajnoczi goto out; 30459c75e168SJeff Cody } 30469c75e168SJeff Cody 30476d0eb64dSKevin Wolf blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL); 3048d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 3049d7086422SKevin Wolf if (ret < 0) { 3050d7086422SKevin Wolf goto out; 3051d7086422SKevin Wolf } 30527dad9ee6SKevin Wolf 3053698bdfa0SJohn Snow bdrv_drained_begin(bs); 30543a691c50SMax Reitz ret = blk_truncate(blk, size, PREALLOC_MODE_OFF, errp); 3055698bdfa0SJohn Snow bdrv_drained_end(bs); 3056927e0e76SStefan Hajnoczi 3057927e0e76SStefan Hajnoczi out: 30587dad9ee6SKevin Wolf blk_unref(blk); 3059927e0e76SStefan Hajnoczi aio_context_release(aio_context); 30606d4a2b3aSChristoph Hellwig } 306112bd451fSStefan Hajnoczi 30622323322eSAlberto Garcia void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, 306313d8cc51SJeff Cody bool has_base, const char *base, 3064312fe09cSAlberto Garcia bool has_base_node, const char *base_node, 306513d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 306613d8cc51SJeff Cody bool has_speed, int64_t speed, 30671d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 30681d809098SPaolo Bonzini Error **errp) 306912bd451fSStefan Hajnoczi { 3070554b6147SAlberto Garcia BlockDriverState *bs, *iter; 3071c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 3072f3e69bebSStefan Hajnoczi AioContext *aio_context; 3073fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 307413d8cc51SJeff Cody const char *base_name = NULL; 307512bd451fSStefan Hajnoczi 30761d809098SPaolo Bonzini if (!has_on_error) { 30771d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 30781d809098SPaolo Bonzini } 30791d809098SPaolo Bonzini 3080554b6147SAlberto Garcia bs = bdrv_lookup_bs(device, device, errp); 3081b6c1bae5SKevin Wolf if (!bs) { 308212bd451fSStefan Hajnoczi return; 308312bd451fSStefan Hajnoczi } 308412bd451fSStefan Hajnoczi 3085b6c1bae5SKevin Wolf aio_context = bdrv_get_aio_context(bs); 3086f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 3087f3e69bebSStefan Hajnoczi 3088312fe09cSAlberto Garcia if (has_base && has_base_node) { 3089312fe09cSAlberto Garcia error_setg(errp, "'base' and 'base-node' cannot be specified " 3090312fe09cSAlberto Garcia "at the same time"); 3091312fe09cSAlberto Garcia goto out; 3092312fe09cSAlberto Garcia } 3093312fe09cSAlberto Garcia 309413d8cc51SJeff Cody if (has_base) { 3095c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3096c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3097c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3098f3e69bebSStefan Hajnoczi goto out; 309912bd451fSStefan Hajnoczi } 3100f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 310113d8cc51SJeff Cody base_name = base; 3102c8c3080fSMarcelo Tosatti } 310312bd451fSStefan Hajnoczi 3104312fe09cSAlberto Garcia if (has_base_node) { 3105312fe09cSAlberto Garcia base_bs = bdrv_lookup_bs(NULL, base_node, errp); 3106312fe09cSAlberto Garcia if (!base_bs) { 3107312fe09cSAlberto Garcia goto out; 3108312fe09cSAlberto Garcia } 3109312fe09cSAlberto Garcia if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { 3110312fe09cSAlberto Garcia error_setg(errp, "Node '%s' is not a backing image of '%s'", 3111312fe09cSAlberto Garcia base_node, device); 3112312fe09cSAlberto Garcia goto out; 3113312fe09cSAlberto Garcia } 3114312fe09cSAlberto Garcia assert(bdrv_get_aio_context(base_bs) == aio_context); 3115312fe09cSAlberto Garcia base_name = base_bs->filename; 3116312fe09cSAlberto Garcia } 3117312fe09cSAlberto Garcia 3118554b6147SAlberto Garcia /* Check for op blockers in the whole chain between bs and base */ 3119554b6147SAlberto Garcia for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) { 3120554b6147SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { 3121554b6147SAlberto Garcia goto out; 3122554b6147SAlberto Garcia } 3123554b6147SAlberto Garcia } 3124554b6147SAlberto Garcia 312513d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 312613d8cc51SJeff Cody * file, and specifying one is therefore an error */ 312713d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 312813d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 312913d8cc51SJeff Cody "entire chain"); 3130f3e69bebSStefan Hajnoczi goto out; 313113d8cc51SJeff Cody } 313213d8cc51SJeff Cody 313313d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 313413d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 313513d8cc51SJeff Cody 31362323322eSAlberto Garcia stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, 31378254b6d9SJohn Snow has_speed ? speed : 0, on_error, &local_err); 313884d18f06SMarkus Armbruster if (local_err) { 3139fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3140f3e69bebSStefan Hajnoczi goto out; 314112bd451fSStefan Hajnoczi } 314212bd451fSStefan Hajnoczi 314312bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3144f3e69bebSStefan Hajnoczi 3145f3e69bebSStefan Hajnoczi out: 3146f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 314712bd451fSStefan Hajnoczi } 31482d47c6e9SStefan Hajnoczi 3149fd62c609SAlberto Garcia void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, 31507676e2c5SJeff Cody bool has_base, const char *base, 31517676e2c5SJeff Cody bool has_top, const char *top, 315254e26900SJeff Cody bool has_backing_file, const char *backing_file, 3153ed61fc10SJeff Cody bool has_speed, int64_t speed, 31540db832f4SKevin Wolf bool has_filter_node_name, const char *filter_node_name, 3155ed61fc10SJeff Cody Error **errp) 3156ed61fc10SJeff Cody { 3157ed61fc10SJeff Cody BlockDriverState *bs; 3158058223a6SAlberto Garcia BlockDriverState *iter; 3159ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 31609e85cd5cSStefan Hajnoczi AioContext *aio_context; 3161ed61fc10SJeff Cody Error *local_err = NULL; 3162ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3163ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3164ed61fc10SJeff Cody */ 316592aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3166ed61fc10SJeff Cody 316754504663SMax Reitz if (!has_speed) { 316854504663SMax Reitz speed = 0; 316954504663SMax Reitz } 31700db832f4SKevin Wolf if (!has_filter_node_name) { 31710db832f4SKevin Wolf filter_node_name = NULL; 31720db832f4SKevin Wolf } 317354504663SMax Reitz 31747676e2c5SJeff Cody /* Important Note: 31757676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 31767676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 31777676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 31787676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 31791d13b167SKevin Wolf bs = qmp_get_root_bs(device, &local_err); 31801d13b167SKevin Wolf if (!bs) { 31811d13b167SKevin Wolf bs = bdrv_lookup_bs(device, device, NULL); 31821d13b167SKevin Wolf if (!bs) { 31831d13b167SKevin Wolf error_free(local_err); 318475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 318575158ebbSMarkus Armbruster "Device '%s' not found", device); 31861d13b167SKevin Wolf } else { 31871d13b167SKevin Wolf error_propagate(errp, local_err); 31881d13b167SKevin Wolf } 3189ed61fc10SJeff Cody return; 3190ed61fc10SJeff Cody } 3191ed61fc10SJeff Cody 31921d13b167SKevin Wolf aio_context = bdrv_get_aio_context(bs); 31939e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 31949e85cd5cSStefan Hajnoczi 3195bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 31969e85cd5cSStefan Hajnoczi goto out; 3197628ff683SFam Zheng } 3198628ff683SFam Zheng 3199ed61fc10SJeff Cody /* default top_bs is the active layer */ 3200ed61fc10SJeff Cody top_bs = bs; 3201ed61fc10SJeff Cody 32027676e2c5SJeff Cody if (has_top && top) { 3203ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3204ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3205ed61fc10SJeff Cody } 3206ed61fc10SJeff Cody } 3207ed61fc10SJeff Cody 3208ed61fc10SJeff Cody if (top_bs == NULL) { 3209ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 32109e85cd5cSStefan Hajnoczi goto out; 3211ed61fc10SJeff Cody } 3212ed61fc10SJeff Cody 32139e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 32149e85cd5cSStefan Hajnoczi 3215d5208c45SJeff Cody if (has_base && base) { 3216d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3217d5208c45SJeff Cody } else { 3218d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3219d5208c45SJeff Cody } 3220d5208c45SJeff Cody 3221d5208c45SJeff Cody if (base_bs == NULL) { 3222c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 32239e85cd5cSStefan Hajnoczi goto out; 3224d5208c45SJeff Cody } 3225d5208c45SJeff Cody 32269e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 32279e85cd5cSStefan Hajnoczi 3228058223a6SAlberto Garcia for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) { 3229058223a6SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3230bb00021dSFam Zheng goto out; 3231bb00021dSFam Zheng } 3232058223a6SAlberto Garcia } 3233bb00021dSFam Zheng 32347676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 32357676e2c5SJeff Cody if (top_bs == base_bs) { 32367676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 32379e85cd5cSStefan Hajnoczi goto out; 32387676e2c5SJeff Cody } 32397676e2c5SJeff Cody 324020a63d2cSFam Zheng if (top_bs == bs) { 324154e26900SJeff Cody if (has_backing_file) { 324254e26900SJeff Cody error_setg(errp, "'backing-file' specified," 324354e26900SJeff Cody " but 'top' is the active layer"); 32449e85cd5cSStefan Hajnoczi goto out; 324554e26900SJeff Cody } 324647970dfbSJohn Snow commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, 32470db832f4SKevin Wolf BLOCK_JOB_DEFAULT, speed, on_error, 324878bbd910SFam Zheng filter_node_name, NULL, NULL, false, &local_err); 324920a63d2cSFam Zheng } else { 3250058223a6SAlberto Garcia BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); 3251058223a6SAlberto Garcia if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3252058223a6SAlberto Garcia goto out; 3253058223a6SAlberto Garcia } 3254fd62c609SAlberto Garcia commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed, 32558254b6d9SJohn Snow on_error, has_backing_file ? backing_file : NULL, 32560db832f4SKevin Wolf filter_node_name, &local_err); 325720a63d2cSFam Zheng } 3258ed61fc10SJeff Cody if (local_err != NULL) { 3259ed61fc10SJeff Cody error_propagate(errp, local_err); 32609e85cd5cSStefan Hajnoczi goto out; 3261ed61fc10SJeff Cody } 32629e85cd5cSStefan Hajnoczi 32639e85cd5cSStefan Hajnoczi out: 32649e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3265ed61fc10SJeff Cody } 3266ed61fc10SJeff Cody 3267111049a4SJohn Snow static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, 3268111049a4SJohn Snow Error **errp) 326999a9addfSStefan Hajnoczi { 327099a9addfSStefan Hajnoczi BlockDriverState *bs; 327199a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3272fc5d3f84SIan Main BlockDriverState *source = NULL; 3273111049a4SJohn Snow BlockJob *job = NULL; 3274d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3275761731b1SStefan Hajnoczi AioContext *aio_context; 3276e6641719SMax Reitz QDict *options = NULL; 327799a9addfSStefan Hajnoczi Error *local_err = NULL; 3278b40dacdcSJohn Snow int flags, job_flags = BLOCK_JOB_DEFAULT; 327999a9addfSStefan Hajnoczi int64_t size; 3280fc0932fdSFam Zheng bool set_backing_hd = false; 328199a9addfSStefan Hajnoczi 328281206a89SPavel Butsykin if (!backup->has_speed) { 328381206a89SPavel Butsykin backup->speed = 0; 328499a9addfSStefan Hajnoczi } 328581206a89SPavel Butsykin if (!backup->has_on_source_error) { 328681206a89SPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 328799a9addfSStefan Hajnoczi } 328881206a89SPavel Butsykin if (!backup->has_on_target_error) { 328981206a89SPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 329099a9addfSStefan Hajnoczi } 329181206a89SPavel Butsykin if (!backup->has_mode) { 329281206a89SPavel Butsykin backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 329381206a89SPavel Butsykin } 329481206a89SPavel Butsykin if (!backup->has_job_id) { 329581206a89SPavel Butsykin backup->job_id = NULL; 329699a9addfSStefan Hajnoczi } 3297b40dacdcSJohn Snow if (!backup->has_auto_finalize) { 3298b40dacdcSJohn Snow backup->auto_finalize = true; 3299b40dacdcSJohn Snow } 3300b40dacdcSJohn Snow if (!backup->has_auto_dismiss) { 3301b40dacdcSJohn Snow backup->auto_dismiss = true; 3302b40dacdcSJohn Snow } 330313b9414bSPavel Butsykin if (!backup->has_compress) { 330413b9414bSPavel Butsykin backup->compress = false; 330513b9414bSPavel Butsykin } 330699a9addfSStefan Hajnoczi 330781206a89SPavel Butsykin bs = qmp_get_root_bs(backup->device, errp); 3308b7e4fa22SKevin Wolf if (!bs) { 3309111049a4SJohn Snow return NULL; 331099a9addfSStefan Hajnoczi } 331199a9addfSStefan Hajnoczi 3312b7e4fa22SKevin Wolf aio_context = bdrv_get_aio_context(bs); 3313761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3314761731b1SStefan Hajnoczi 331581206a89SPavel Butsykin if (!backup->has_format) { 331681206a89SPavel Butsykin backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? 331781206a89SPavel Butsykin NULL : (char*) bs->drv->format_name; 331899a9addfSStefan Hajnoczi } 331999a9addfSStefan Hajnoczi 3320c29c1dd3SFam Zheng /* Early check to avoid creating target */ 33213718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3322761731b1SStefan Hajnoczi goto out; 332399a9addfSStefan Hajnoczi } 332499a9addfSStefan Hajnoczi 332561de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 332699a9addfSStefan Hajnoczi 3327fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3328fc5d3f84SIan Main * on top of. */ 332981206a89SPavel Butsykin if (backup->sync == MIRROR_SYNC_MODE_TOP) { 3330760e0063SKevin Wolf source = backing_bs(bs); 3331fc5d3f84SIan Main if (!source) { 333281206a89SPavel Butsykin backup->sync = MIRROR_SYNC_MODE_FULL; 3333fc5d3f84SIan Main } 3334fc5d3f84SIan Main } 333581206a89SPavel Butsykin if (backup->sync == MIRROR_SYNC_MODE_NONE) { 3336fc5d3f84SIan Main source = bs; 3337fc0932fdSFam Zheng flags |= BDRV_O_NO_BACKING; 3338fc0932fdSFam Zheng set_backing_hd = true; 3339fc5d3f84SIan Main } 3340fc5d3f84SIan Main 334199a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 334299a9addfSStefan Hajnoczi if (size < 0) { 334399a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3344761731b1SStefan Hajnoczi goto out; 334599a9addfSStefan Hajnoczi } 334699a9addfSStefan Hajnoczi 334781206a89SPavel Butsykin if (backup->mode != NEW_IMAGE_MODE_EXISTING) { 334881206a89SPavel Butsykin assert(backup->format); 3349fc5d3f84SIan Main if (source) { 335081206a89SPavel Butsykin bdrv_img_create(backup->target, backup->format, source->filename, 3351fc5d3f84SIan Main source->drv->format_name, NULL, 33529217283dSFam Zheng size, flags, false, &local_err); 3353fc5d3f84SIan Main } else { 335481206a89SPavel Butsykin bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, 33559217283dSFam Zheng size, flags, false, &local_err); 3356fc5d3f84SIan Main } 335799a9addfSStefan Hajnoczi } 335899a9addfSStefan Hajnoczi 335984d18f06SMarkus Armbruster if (local_err) { 336099a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3361761731b1SStefan Hajnoczi goto out; 336299a9addfSStefan Hajnoczi } 336399a9addfSStefan Hajnoczi 336481206a89SPavel Butsykin if (backup->format) { 3365fc0932fdSFam Zheng if (!options) { 3366e6641719SMax Reitz options = qdict_new(); 3367fc0932fdSFam Zheng } 336846f5ac20SEric Blake qdict_put_str(options, "driver", backup->format); 3369e6641719SMax Reitz } 3370e6641719SMax Reitz 337181206a89SPavel Butsykin target_bs = bdrv_open(backup->target, NULL, options, flags, errp); 33725b363937SMax Reitz if (!target_bs) { 3373761731b1SStefan Hajnoczi goto out; 337499a9addfSStefan Hajnoczi } 337599a9addfSStefan Hajnoczi 3376761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3377761731b1SStefan Hajnoczi 3378fc0932fdSFam Zheng if (set_backing_hd) { 3379fc0932fdSFam Zheng bdrv_set_backing_hd(target_bs, source, &local_err); 3380fc0932fdSFam Zheng if (local_err) { 3381fc0932fdSFam Zheng bdrv_unref(target_bs); 3382fc0932fdSFam Zheng goto out; 3383fc0932fdSFam Zheng } 3384fc0932fdSFam Zheng } 3385fc0932fdSFam Zheng 338681206a89SPavel Butsykin if (backup->has_bitmap) { 338781206a89SPavel Butsykin bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); 3388d58d8453SJohn Snow if (!bmap) { 338981206a89SPavel Butsykin error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); 33900702d3d8SMax Reitz bdrv_unref(target_bs); 3391d58d8453SJohn Snow goto out; 3392d58d8453SJohn Snow } 33934f43e953SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_qmp_locked(bmap)) { 33944f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 33954f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be used for " 33964f43e953SVladimir Sementsov-Ogievskiy "backup", backup->bitmap); 33974f43e953SVladimir Sementsov-Ogievskiy goto out; 33984f43e953SVladimir Sementsov-Ogievskiy } 3399d58d8453SJohn Snow } 3400b40dacdcSJohn Snow if (!backup->auto_finalize) { 3401b40dacdcSJohn Snow job_flags |= BLOCK_JOB_MANUAL_FINALIZE; 3402b40dacdcSJohn Snow } 3403b40dacdcSJohn Snow if (!backup->auto_dismiss) { 3404b40dacdcSJohn Snow job_flags |= BLOCK_JOB_MANUAL_DISMISS; 3405b40dacdcSJohn Snow } 3406d58d8453SJohn Snow 3407111049a4SJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3408111049a4SJohn Snow backup->sync, bmap, backup->compress, 3409111049a4SJohn Snow backup->on_source_error, backup->on_target_error, 3410b40dacdcSJohn Snow job_flags, NULL, NULL, txn, &local_err); 34114f6fd349SFam Zheng bdrv_unref(target_bs); 34125c438bc6SKevin Wolf if (local_err != NULL) { 341399a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3414761731b1SStefan Hajnoczi goto out; 341599a9addfSStefan Hajnoczi } 3416761731b1SStefan Hajnoczi 3417761731b1SStefan Hajnoczi out: 3418761731b1SStefan Hajnoczi aio_context_release(aio_context); 3419111049a4SJohn Snow return job; 342099a9addfSStefan Hajnoczi } 342199a9addfSStefan Hajnoczi 342281206a89SPavel Butsykin void qmp_drive_backup(DriveBackup *arg, Error **errp) 342378f51fdeSJohn Snow { 3424111049a4SJohn Snow 3425111049a4SJohn Snow BlockJob *job; 3426111049a4SJohn Snow job = do_drive_backup(arg, NULL, errp); 3427111049a4SJohn Snow if (job) { 3428da01ff7fSKevin Wolf job_start(&job->job); 3429111049a4SJohn Snow } 343078f51fdeSJohn Snow } 343178f51fdeSJohn Snow 3432c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3433c13163fbSBenoît Canet { 3434d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3435c13163fbSBenoît Canet } 3436c13163fbSBenoît Canet 3437111049a4SJohn Snow BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, 3438111049a4SJohn Snow Error **errp) 3439c29c1dd3SFam Zheng { 3440c29c1dd3SFam Zheng BlockDriverState *bs; 3441c29c1dd3SFam Zheng BlockDriverState *target_bs; 3442c29c1dd3SFam Zheng Error *local_err = NULL; 3443c29c1dd3SFam Zheng AioContext *aio_context; 3444111049a4SJohn Snow BlockJob *job = NULL; 3445b40dacdcSJohn Snow int job_flags = BLOCK_JOB_DEFAULT; 3446c29c1dd3SFam Zheng 3447dc7a4a9eSPavel Butsykin if (!backup->has_speed) { 3448dc7a4a9eSPavel Butsykin backup->speed = 0; 3449c29c1dd3SFam Zheng } 3450dc7a4a9eSPavel Butsykin if (!backup->has_on_source_error) { 3451dc7a4a9eSPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3452c29c1dd3SFam Zheng } 3453dc7a4a9eSPavel Butsykin if (!backup->has_on_target_error) { 3454dc7a4a9eSPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3455dc7a4a9eSPavel Butsykin } 3456dc7a4a9eSPavel Butsykin if (!backup->has_job_id) { 3457dc7a4a9eSPavel Butsykin backup->job_id = NULL; 3458c29c1dd3SFam Zheng } 3459b40dacdcSJohn Snow if (!backup->has_auto_finalize) { 3460b40dacdcSJohn Snow backup->auto_finalize = true; 3461b40dacdcSJohn Snow } 3462b40dacdcSJohn Snow if (!backup->has_auto_dismiss) { 3463b40dacdcSJohn Snow backup->auto_dismiss = true; 3464b40dacdcSJohn Snow } 34653b7b1236SPavel Butsykin if (!backup->has_compress) { 34663b7b1236SPavel Butsykin backup->compress = false; 34673b7b1236SPavel Butsykin } 3468c29c1dd3SFam Zheng 3469dc7a4a9eSPavel Butsykin bs = qmp_get_root_bs(backup->device, errp); 3470cef34eebSKevin Wolf if (!bs) { 3471111049a4SJohn Snow return NULL; 3472c29c1dd3SFam Zheng } 3473c29c1dd3SFam Zheng 3474cef34eebSKevin Wolf aio_context = bdrv_get_aio_context(bs); 3475c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3476c29c1dd3SFam Zheng 3477dc7a4a9eSPavel Butsykin target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); 34780d978913SFam Zheng if (!target_bs) { 3479c29c1dd3SFam Zheng goto out; 3480c29c1dd3SFam Zheng } 34815433c24fSMax Reitz 3482efd75567SFam Zheng if (bdrv_get_aio_context(target_bs) != aio_context) { 3483efd75567SFam Zheng if (!bdrv_has_blk(target_bs)) { 3484efd75567SFam Zheng /* The target BDS is not attached, we can safely move it to another 3485efd75567SFam Zheng * AioContext. */ 3486c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3487efd75567SFam Zheng } else { 3488efd75567SFam Zheng error_setg(errp, "Target is attached to a different thread from " 3489efd75567SFam Zheng "source."); 3490efd75567SFam Zheng goto out; 3491efd75567SFam Zheng } 3492efd75567SFam Zheng } 3493b40dacdcSJohn Snow if (!backup->auto_finalize) { 3494b40dacdcSJohn Snow job_flags |= BLOCK_JOB_MANUAL_FINALIZE; 3495b40dacdcSJohn Snow } 3496b40dacdcSJohn Snow if (!backup->auto_dismiss) { 3497b40dacdcSJohn Snow job_flags |= BLOCK_JOB_MANUAL_DISMISS; 3498b40dacdcSJohn Snow } 3499111049a4SJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3500111049a4SJohn Snow backup->sync, NULL, backup->compress, 3501111049a4SJohn Snow backup->on_source_error, backup->on_target_error, 3502b40dacdcSJohn Snow job_flags, NULL, NULL, txn, &local_err); 3503c29c1dd3SFam Zheng if (local_err != NULL) { 3504c29c1dd3SFam Zheng error_propagate(errp, local_err); 3505c29c1dd3SFam Zheng } 3506c29c1dd3SFam Zheng out: 3507c29c1dd3SFam Zheng aio_context_release(aio_context); 3508111049a4SJohn Snow return job; 3509c29c1dd3SFam Zheng } 3510c29c1dd3SFam Zheng 3511dc7a4a9eSPavel Butsykin void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp) 351278f51fdeSJohn Snow { 3513111049a4SJohn Snow BlockJob *job; 3514111049a4SJohn Snow job = do_blockdev_backup(arg, NULL, errp); 3515111049a4SJohn Snow if (job) { 3516da01ff7fSKevin Wolf job_start(&job->job); 3517111049a4SJohn Snow } 351878f51fdeSJohn Snow } 351978f51fdeSJohn Snow 35204193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 35214193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 35224193cdd7SFam Zheng **/ 352371aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, 35244193cdd7SFam Zheng BlockDriverState *target, 352509158f00SBenoît Canet bool has_replaces, const char *replaces, 3526d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3527274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 3528b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3529eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 353008e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 35314193cdd7SFam Zheng bool has_on_source_error, 35324193cdd7SFam Zheng BlockdevOnError on_source_error, 35334193cdd7SFam Zheng bool has_on_target_error, 35344193cdd7SFam Zheng BlockdevOnError on_target_error, 35350fc9f8eaSFam Zheng bool has_unmap, bool unmap, 35366cdbceb1SKevin Wolf bool has_filter_node_name, 35376cdbceb1SKevin Wolf const char *filter_node_name, 3538b952b558SPaolo Bonzini Error **errp) 3539d9b902dbSPaolo Bonzini { 3540d9b902dbSPaolo Bonzini 3541d9b902dbSPaolo Bonzini if (!has_speed) { 3542d9b902dbSPaolo Bonzini speed = 0; 3543d9b902dbSPaolo Bonzini } 3544b952b558SPaolo Bonzini if (!has_on_source_error) { 3545b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3546b952b558SPaolo Bonzini } 3547b952b558SPaolo Bonzini if (!has_on_target_error) { 3548b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3549b952b558SPaolo Bonzini } 3550eee13dfeSPaolo Bonzini if (!has_granularity) { 3551eee13dfeSPaolo Bonzini granularity = 0; 3552eee13dfeSPaolo Bonzini } 355308e4ed6cSPaolo Bonzini if (!has_buf_size) { 355448ac0a4dSWen Congyang buf_size = 0; 355508e4ed6cSPaolo Bonzini } 35560fc9f8eaSFam Zheng if (!has_unmap) { 35570fc9f8eaSFam Zheng unmap = true; 35580fc9f8eaSFam Zheng } 35596cdbceb1SKevin Wolf if (!has_filter_node_name) { 35606cdbceb1SKevin Wolf filter_node_name = NULL; 35616cdbceb1SKevin Wolf } 356208e4ed6cSPaolo Bonzini 3563eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3564c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 35653cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3566eee13dfeSPaolo Bonzini return; 3567eee13dfeSPaolo Bonzini } 3568eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3569c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3570c6bd8c70SMarkus Armbruster "power of 2"); 3571eee13dfeSPaolo Bonzini return; 3572eee13dfeSPaolo Bonzini } 3573d9b902dbSPaolo Bonzini 35744193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 35754193cdd7SFam Zheng return; 35764193cdd7SFam Zheng } 3577e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3578e40e5027SFam Zheng return; 3579e40e5027SFam Zheng } 35804193cdd7SFam Zheng 35814193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 35824193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 35834193cdd7SFam Zheng } 35844193cdd7SFam Zheng 35854193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 35864193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 35874193cdd7SFam Zheng */ 358871aa9867SAlberto Garcia mirror_start(job_id, bs, target, 35894193cdd7SFam Zheng has_replaces ? replaces : NULL, 3590274fcceeSMax Reitz speed, granularity, buf_size, sync, backing_mode, 35916cdbceb1SKevin Wolf on_source_error, on_target_error, unmap, filter_node_name, 35926cdbceb1SKevin Wolf errp); 35934193cdd7SFam Zheng } 35944193cdd7SFam Zheng 3595faecd40aSEric Blake void qmp_drive_mirror(DriveMirror *arg, Error **errp) 35964193cdd7SFam Zheng { 35974193cdd7SFam Zheng BlockDriverState *bs; 35984193cdd7SFam Zheng BlockDriverState *source, *target_bs; 35994193cdd7SFam Zheng AioContext *aio_context; 3600274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 36014193cdd7SFam Zheng Error *local_err = NULL; 36024193cdd7SFam Zheng QDict *options = NULL; 36034193cdd7SFam Zheng int flags; 36044193cdd7SFam Zheng int64_t size; 3605faecd40aSEric Blake const char *format = arg->format; 36064193cdd7SFam Zheng 36070524e93aSKevin Wolf bs = qmp_get_root_bs(arg->device, errp); 36080524e93aSKevin Wolf if (!bs) { 3609d9b902dbSPaolo Bonzini return; 3610d9b902dbSPaolo Bonzini } 3611d9b902dbSPaolo Bonzini 3612cb2af917SPaolo Bonzini /* Early check to avoid creating target */ 3613cb2af917SPaolo Bonzini if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 3614cb2af917SPaolo Bonzini return; 3615cb2af917SPaolo Bonzini } 3616cb2af917SPaolo Bonzini 36170524e93aSKevin Wolf aio_context = bdrv_get_aio_context(bs); 36185a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 36195a7e7a0bSStefan Hajnoczi 3620faecd40aSEric Blake if (!arg->has_mode) { 3621faecd40aSEric Blake arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 36224193cdd7SFam Zheng } 3623d9b902dbSPaolo Bonzini 3624faecd40aSEric Blake if (!arg->has_format) { 3625faecd40aSEric Blake format = (arg->mode == NEW_IMAGE_MODE_EXISTING 3626faecd40aSEric Blake ? NULL : bs->drv->format_name); 3627d9b902dbSPaolo Bonzini } 3628d9b902dbSPaolo Bonzini 362961de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3630760e0063SKevin Wolf source = backing_bs(bs); 3631faecd40aSEric Blake if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { 3632faecd40aSEric Blake arg->sync = MIRROR_SYNC_MODE_FULL; 3633d9b902dbSPaolo Bonzini } 3634faecd40aSEric Blake if (arg->sync == MIRROR_SYNC_MODE_NONE) { 3635117e0c82SMax Reitz source = bs; 3636117e0c82SMax Reitz } 3637d9b902dbSPaolo Bonzini 3638ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3639ac3c5d83SStefan Hajnoczi if (size < 0) { 3640ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 36415a7e7a0bSStefan Hajnoczi goto out; 3642ac3c5d83SStefan Hajnoczi } 3643ac3c5d83SStefan Hajnoczi 3644faecd40aSEric Blake if (arg->has_replaces) { 364509158f00SBenoît Canet BlockDriverState *to_replace_bs; 36465a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 36475a7e7a0bSStefan Hajnoczi int64_t replace_size; 364809158f00SBenoît Canet 3649faecd40aSEric Blake if (!arg->has_node_name) { 365009158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 365109158f00SBenoît Canet " named node of the graph"); 36525a7e7a0bSStefan Hajnoczi goto out; 365309158f00SBenoît Canet } 365409158f00SBenoît Canet 3655faecd40aSEric Blake to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err); 365609158f00SBenoît Canet 365709158f00SBenoît Canet if (!to_replace_bs) { 365809158f00SBenoît Canet error_propagate(errp, local_err); 36595a7e7a0bSStefan Hajnoczi goto out; 366009158f00SBenoît Canet } 366109158f00SBenoît Canet 36625a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 36635a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 36645a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 36655a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 36665a7e7a0bSStefan Hajnoczi 36675a7e7a0bSStefan Hajnoczi if (size != replace_size) { 366809158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 366909158f00SBenoît Canet "different size"); 36705a7e7a0bSStefan Hajnoczi goto out; 367109158f00SBenoît Canet } 367209158f00SBenoît Canet } 367309158f00SBenoît Canet 3674faecd40aSEric Blake if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { 3675274fcceeSMax Reitz backing_mode = MIRROR_SOURCE_BACKING_CHAIN; 3676274fcceeSMax Reitz } else { 3677274fcceeSMax Reitz backing_mode = MIRROR_OPEN_BACKING_CHAIN; 3678274fcceeSMax Reitz } 3679274fcceeSMax Reitz 36802a32c6e8SJohn Snow /* Don't open backing image in create() */ 36812a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING; 36822a32c6e8SJohn Snow 3683faecd40aSEric Blake if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) 3684faecd40aSEric Blake && arg->mode != NEW_IMAGE_MODE_EXISTING) 368514526864SMax Reitz { 3686d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3687e6641719SMax Reitz assert(format); 3688faecd40aSEric Blake bdrv_img_create(arg->target, format, 36899217283dSFam Zheng NULL, NULL, NULL, size, flags, false, &local_err); 3690d9b902dbSPaolo Bonzini } else { 3691faecd40aSEric Blake switch (arg->mode) { 3692d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3693d9b902dbSPaolo Bonzini break; 3694d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3695d9b902dbSPaolo Bonzini /* create new image with backing file */ 3696faecd40aSEric Blake bdrv_img_create(arg->target, format, 3697d9b902dbSPaolo Bonzini source->filename, 3698d9b902dbSPaolo Bonzini source->drv->format_name, 36999217283dSFam Zheng NULL, size, flags, false, &local_err); 3700d9b902dbSPaolo Bonzini break; 3701d9b902dbSPaolo Bonzini default: 3702d9b902dbSPaolo Bonzini abort(); 3703d9b902dbSPaolo Bonzini } 3704d9b902dbSPaolo Bonzini } 3705d9b902dbSPaolo Bonzini 370684d18f06SMarkus Armbruster if (local_err) { 3707cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 37085a7e7a0bSStefan Hajnoczi goto out; 3709d9b902dbSPaolo Bonzini } 3710d9b902dbSPaolo Bonzini 37114c828dc6SBenoît Canet options = qdict_new(); 3712faecd40aSEric Blake if (arg->has_node_name) { 371346f5ac20SEric Blake qdict_put_str(options, "node-name", arg->node_name); 37144c828dc6SBenoît Canet } 3715e6641719SMax Reitz if (format) { 371646f5ac20SEric Blake qdict_put_str(options, "driver", format); 3717e6641719SMax Reitz } 37184c828dc6SBenoît Canet 3719b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3720b812f671SPaolo Bonzini * file. 3721b812f671SPaolo Bonzini */ 37222a32c6e8SJohn Snow target_bs = bdrv_open(arg->target, NULL, options, flags, errp); 37235b363937SMax Reitz if (!target_bs) { 37245a7e7a0bSStefan Hajnoczi goto out; 3725d9b902dbSPaolo Bonzini } 3726d9b902dbSPaolo Bonzini 37275a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 37285a7e7a0bSStefan Hajnoczi 3729faecd40aSEric Blake blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, 3730faecd40aSEric Blake arg->has_replaces, arg->replaces, arg->sync, 3731faecd40aSEric Blake backing_mode, arg->has_speed, arg->speed, 3732faecd40aSEric Blake arg->has_granularity, arg->granularity, 3733faecd40aSEric Blake arg->has_buf_size, arg->buf_size, 3734faecd40aSEric Blake arg->has_on_source_error, arg->on_source_error, 3735faecd40aSEric Blake arg->has_on_target_error, arg->on_target_error, 3736faecd40aSEric Blake arg->has_unmap, arg->unmap, 37376cdbceb1SKevin Wolf false, NULL, 37384193cdd7SFam Zheng &local_err); 3739e253f4b8SKevin Wolf bdrv_unref(target_bs); 3740d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 37415a7e7a0bSStefan Hajnoczi out: 37425a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3743d9b902dbSPaolo Bonzini } 3744d9b902dbSPaolo Bonzini 374571aa9867SAlberto Garcia void qmp_blockdev_mirror(bool has_job_id, const char *job_id, 374671aa9867SAlberto Garcia const char *device, const char *target, 3747df92562eSFam Zheng bool has_replaces, const char *replaces, 3748df92562eSFam Zheng MirrorSyncMode sync, 3749df92562eSFam Zheng bool has_speed, int64_t speed, 3750df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3751df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3752df92562eSFam Zheng bool has_on_source_error, 3753df92562eSFam Zheng BlockdevOnError on_source_error, 3754df92562eSFam Zheng bool has_on_target_error, 3755df92562eSFam Zheng BlockdevOnError on_target_error, 37566cdbceb1SKevin Wolf bool has_filter_node_name, 37576cdbceb1SKevin Wolf const char *filter_node_name, 3758df92562eSFam Zheng Error **errp) 3759df92562eSFam Zheng { 3760df92562eSFam Zheng BlockDriverState *bs; 3761df92562eSFam Zheng BlockDriverState *target_bs; 3762df92562eSFam Zheng AioContext *aio_context; 3763274fcceeSMax Reitz BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; 3764df92562eSFam Zheng Error *local_err = NULL; 3765df92562eSFam Zheng 376607eec652SKevin Wolf bs = qmp_get_root_bs(device, errp); 3767df92562eSFam Zheng if (!bs) { 3768df92562eSFam Zheng return; 3769df92562eSFam Zheng } 3770df92562eSFam Zheng 3771df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3772df92562eSFam Zheng if (!target_bs) { 3773df92562eSFam Zheng return; 3774df92562eSFam Zheng } 3775df92562eSFam Zheng 3776df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3777df92562eSFam Zheng aio_context_acquire(aio_context); 3778df92562eSFam Zheng 3779df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3780df92562eSFam Zheng 378171aa9867SAlberto Garcia blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 3782274fcceeSMax Reitz has_replaces, replaces, sync, backing_mode, 3783df92562eSFam Zheng has_speed, speed, 3784df92562eSFam Zheng has_granularity, granularity, 3785df92562eSFam Zheng has_buf_size, buf_size, 3786df92562eSFam Zheng has_on_source_error, on_source_error, 3787df92562eSFam Zheng has_on_target_error, on_target_error, 3788df92562eSFam Zheng true, true, 37896cdbceb1SKevin Wolf has_filter_node_name, filter_node_name, 3790df92562eSFam Zheng &local_err); 3791df92562eSFam Zheng error_propagate(errp, local_err); 3792df92562eSFam Zheng 3793df92562eSFam Zheng aio_context_release(aio_context); 3794df92562eSFam Zheng } 3795df92562eSFam Zheng 37963ddf3efeSAlberto Garcia /* Get a block job using its ID and acquire its AioContext */ 37973ddf3efeSAlberto Garcia static BlockJob *find_block_job(const char *id, AioContext **aio_context, 379824d6bffeSMarkus Armbruster Error **errp) 37992d47c6e9SStefan Hajnoczi { 38003ddf3efeSAlberto Garcia BlockJob *job; 38013ddf3efeSAlberto Garcia 38023ddf3efeSAlberto Garcia assert(id != NULL); 38032d47c6e9SStefan Hajnoczi 38045433c24fSMax Reitz *aio_context = NULL; 38055433c24fSMax Reitz 38063ddf3efeSAlberto Garcia job = block_job_get(id); 38073ddf3efeSAlberto Garcia 38083ddf3efeSAlberto Garcia if (!job) { 38093ddf3efeSAlberto Garcia error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 38103ddf3efeSAlberto Garcia "Block job '%s' not found", id); 38113ddf3efeSAlberto Garcia return NULL; 38122d47c6e9SStefan Hajnoczi } 38133d948cdfSStefan Hajnoczi 38143ddf3efeSAlberto Garcia *aio_context = blk_get_aio_context(job->blk); 38153d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 38163d948cdfSStefan Hajnoczi 38173ddf3efeSAlberto Garcia return job; 38182d47c6e9SStefan Hajnoczi } 38192d47c6e9SStefan Hajnoczi 3820882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 38212d47c6e9SStefan Hajnoczi { 38223d948cdfSStefan Hajnoczi AioContext *aio_context; 382324d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 38242d47c6e9SStefan Hajnoczi 38252d47c6e9SStefan Hajnoczi if (!job) { 38262d47c6e9SStefan Hajnoczi return; 38272d47c6e9SStefan Hajnoczi } 38282d47c6e9SStefan Hajnoczi 3829882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 38303d948cdfSStefan Hajnoczi aio_context_release(aio_context); 38312d47c6e9SStefan Hajnoczi } 3832370521a1SStefan Hajnoczi 38336e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 38346e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 38356e37fb81SPaolo Bonzini { 38363d948cdfSStefan Hajnoczi AioContext *aio_context; 383724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 38386e37fb81SPaolo Bonzini 38396e37fb81SPaolo Bonzini if (!job) { 38406e37fb81SPaolo Bonzini return; 38416e37fb81SPaolo Bonzini } 38423d948cdfSStefan Hajnoczi 38433d948cdfSStefan Hajnoczi if (!has_force) { 38443d948cdfSStefan Hajnoczi force = false; 38453d948cdfSStefan Hajnoczi } 38463d948cdfSStefan Hajnoczi 3847*b15de828SKevin Wolf if (job_user_paused(&job->job) && !force) { 3848f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3849f231b88dSCole Robinson device); 38503d948cdfSStefan Hajnoczi goto out; 38516e37fb81SPaolo Bonzini } 38526e37fb81SPaolo Bonzini 38536e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 3854b76e4458SLiang Li block_job_user_cancel(job, force, errp); 38553d948cdfSStefan Hajnoczi out: 38563d948cdfSStefan Hajnoczi aio_context_release(aio_context); 38576e37fb81SPaolo Bonzini } 38586e37fb81SPaolo Bonzini 38596e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3860370521a1SStefan Hajnoczi { 38613d948cdfSStefan Hajnoczi AioContext *aio_context; 386224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3863370521a1SStefan Hajnoczi 38640ec4dfb8SJohn Snow if (!job) { 3865370521a1SStefan Hajnoczi return; 3866370521a1SStefan Hajnoczi } 38676e37fb81SPaolo Bonzini 38686e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 3869*b15de828SKevin Wolf job_user_pause(&job->job, errp); 38703d948cdfSStefan Hajnoczi aio_context_release(aio_context); 38716e37fb81SPaolo Bonzini } 38726e37fb81SPaolo Bonzini 38736e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 38746e37fb81SPaolo Bonzini { 38753d948cdfSStefan Hajnoczi AioContext *aio_context; 387624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 38776e37fb81SPaolo Bonzini 38780ec4dfb8SJohn Snow if (!job) { 38798acc72a4SPaolo Bonzini return; 38808acc72a4SPaolo Bonzini } 3881370521a1SStefan Hajnoczi 38826e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 3883*b15de828SKevin Wolf job_user_resume(&job->job, errp); 38843d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3885370521a1SStefan Hajnoczi } 3886fb5458cdSStefan Hajnoczi 3887aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3888aeae883bSPaolo Bonzini { 38893d948cdfSStefan Hajnoczi AioContext *aio_context; 389024d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3891aeae883bSPaolo Bonzini 3892aeae883bSPaolo Bonzini if (!job) { 3893aeae883bSPaolo Bonzini return; 3894aeae883bSPaolo Bonzini } 3895aeae883bSPaolo Bonzini 3896aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3897aeae883bSPaolo Bonzini block_job_complete(job, errp); 38983d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3899aeae883bSPaolo Bonzini } 3900aeae883bSPaolo Bonzini 390111b61fbcSJohn Snow void qmp_block_job_finalize(const char *id, Error **errp) 390211b61fbcSJohn Snow { 390311b61fbcSJohn Snow AioContext *aio_context; 390411b61fbcSJohn Snow BlockJob *job = find_block_job(id, &aio_context, errp); 390511b61fbcSJohn Snow 390611b61fbcSJohn Snow if (!job) { 390711b61fbcSJohn Snow return; 390811b61fbcSJohn Snow } 390911b61fbcSJohn Snow 391011b61fbcSJohn Snow trace_qmp_block_job_finalize(job); 391111b61fbcSJohn Snow block_job_finalize(job, errp); 391211b61fbcSJohn Snow aio_context_release(aio_context); 391311b61fbcSJohn Snow } 391411b61fbcSJohn Snow 391575f71059SJohn Snow void qmp_block_job_dismiss(const char *id, Error **errp) 391675f71059SJohn Snow { 391775f71059SJohn Snow AioContext *aio_context; 391875f71059SJohn Snow BlockJob *job = find_block_job(id, &aio_context, errp); 391975f71059SJohn Snow 392075f71059SJohn Snow if (!job) { 392175f71059SJohn Snow return; 392275f71059SJohn Snow } 392375f71059SJohn Snow 392475f71059SJohn Snow trace_qmp_block_job_dismiss(job); 392575f71059SJohn Snow block_job_dismiss(&job, errp); 392675f71059SJohn Snow aio_context_release(aio_context); 392775f71059SJohn Snow } 392875f71059SJohn Snow 3929fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3930fa40e656SJeff Cody const char *image_node_name, 3931fa40e656SJeff Cody const char *backing_file, 3932fa40e656SJeff Cody Error **errp) 3933fa40e656SJeff Cody { 3934fa40e656SJeff Cody BlockDriverState *bs = NULL; 3935729962f6SStefan Hajnoczi AioContext *aio_context; 3936fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3937fa40e656SJeff Cody Error *local_err = NULL; 3938fa40e656SJeff Cody bool ro; 3939fa40e656SJeff Cody int open_flags; 3940fa40e656SJeff Cody int ret; 3941fa40e656SJeff Cody 39427b5dca3fSKevin Wolf bs = qmp_get_root_bs(device, errp); 39437b5dca3fSKevin Wolf if (!bs) { 3944fa40e656SJeff Cody return; 3945fa40e656SJeff Cody } 3946fa40e656SJeff Cody 39477b5dca3fSKevin Wolf aio_context = bdrv_get_aio_context(bs); 3948729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3949729962f6SStefan Hajnoczi 3950fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3951fa40e656SJeff Cody if (local_err) { 3952fa40e656SJeff Cody error_propagate(errp, local_err); 3953729962f6SStefan Hajnoczi goto out; 3954fa40e656SJeff Cody } 3955fa40e656SJeff Cody 3956fa40e656SJeff Cody if (!image_bs) { 3957fa40e656SJeff Cody error_setg(errp, "image file not found"); 3958729962f6SStefan Hajnoczi goto out; 3959fa40e656SJeff Cody } 3960fa40e656SJeff Cody 3961fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3962fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3963fa40e656SJeff Cody "without a backing file"); 3964729962f6SStefan Hajnoczi goto out; 3965fa40e656SJeff Cody } 3966fa40e656SJeff Cody 3967fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3968fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3969fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3970729962f6SStefan Hajnoczi goto out; 3971fa40e656SJeff Cody } 3972fa40e656SJeff Cody 3973fa40e656SJeff Cody /* final sanity check */ 3974fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3975fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3976fa40e656SJeff Cody device); 3977729962f6SStefan Hajnoczi goto out; 3978fa40e656SJeff Cody } 3979fa40e656SJeff Cody 3980fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3981fa40e656SJeff Cody open_flags = image_bs->open_flags; 3982fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3983fa40e656SJeff Cody 3984fa40e656SJeff Cody if (ro) { 3985fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3986fa40e656SJeff Cody if (local_err) { 3987fa40e656SJeff Cody error_propagate(errp, local_err); 3988729962f6SStefan Hajnoczi goto out; 3989fa40e656SJeff Cody } 3990fa40e656SJeff Cody } 3991fa40e656SJeff Cody 3992fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3993fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3994fa40e656SJeff Cody 3995fa40e656SJeff Cody if (ret < 0) { 3996fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3997fa40e656SJeff Cody backing_file); 3998fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3999fa40e656SJeff Cody * appropriate */ 4000fa40e656SJeff Cody } 4001fa40e656SJeff Cody 4002fa40e656SJeff Cody if (ro) { 4003fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 4004621ff94dSEduardo Habkost error_propagate(errp, local_err); 4005fa40e656SJeff Cody } 4006729962f6SStefan Hajnoczi 4007729962f6SStefan Hajnoczi out: 4008729962f6SStefan Hajnoczi aio_context_release(aio_context); 4009fa40e656SJeff Cody } 4010fa40e656SJeff Cody 4011abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 4012abb21ac3SKevin Wolf { 4013abb21ac3SKevin Wolf QemuOpts *opts; 4014abb21ac3SKevin Wolf QDict *qdict; 4015abb21ac3SKevin Wolf Error *local_err = NULL; 4016abb21ac3SKevin Wolf 4017abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 4018abb21ac3SKevin Wolf if (!opts) { 4019abb21ac3SKevin Wolf return; 4020abb21ac3SKevin Wolf } 4021abb21ac3SKevin Wolf 4022abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 4023abb21ac3SKevin Wolf 4024abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 4025cb3e7f08SMarc-André Lureau qobject_unref(qdict); 4026abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 4027abb21ac3SKevin Wolf goto out; 4028abb21ac3SKevin Wolf } 4029abb21ac3SKevin Wolf 4030abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 4031abb21ac3SKevin Wolf if (!bs) { 4032abb21ac3SKevin Wolf error_report_err(local_err); 4033abb21ac3SKevin Wolf goto out; 4034abb21ac3SKevin Wolf } 4035abb21ac3SKevin Wolf 4036abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4037abb21ac3SKevin Wolf 4038abb21ac3SKevin Wolf out: 4039abb21ac3SKevin Wolf qemu_opts_del(opts); 4040abb21ac3SKevin Wolf } 4041abb21ac3SKevin Wolf 4042d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 4043d26c9a15SKevin Wolf { 4044be4b67bcSMax Reitz BlockDriverState *bs; 4045d26c9a15SKevin Wolf QObject *obj; 40467d5e199aSDaniel P. Berrange Visitor *v = qobject_output_visitor_new(&obj); 4047d26c9a15SKevin Wolf QDict *qdict; 4048d26c9a15SKevin Wolf Error *local_err = NULL; 4049d26c9a15SKevin Wolf 40503b098d56SEric Blake visit_type_BlockdevOptions(v, NULL, &options, &local_err); 405184d18f06SMarkus Armbruster if (local_err) { 4052d26c9a15SKevin Wolf error_propagate(errp, local_err); 4053d26c9a15SKevin Wolf goto fail; 4054d26c9a15SKevin Wolf } 4055d26c9a15SKevin Wolf 40563b098d56SEric Blake visit_complete(v, &obj); 40577dc847ebSMax Reitz qdict = qobject_to(QDict, obj); 4058d26c9a15SKevin Wolf 4059d26c9a15SKevin Wolf qdict_flatten(qdict); 4060d26c9a15SKevin Wolf 4061be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 40629ec8873eSKevin Wolf error_setg(errp, "'node-name' must be specified for the root node"); 4063be4b67bcSMax Reitz goto fail; 4064be4b67bcSMax Reitz } 4065be4b67bcSMax Reitz 4066bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 4067bd745e23SMax Reitz if (!bs) { 4068be4b67bcSMax Reitz goto fail; 4069be4b67bcSMax Reitz } 40709c4218e9SMax Reitz 40719c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4072be4b67bcSMax Reitz 4073d26c9a15SKevin Wolf fail: 40743b098d56SEric Blake visit_free(v); 4075d26c9a15SKevin Wolf } 4076d26c9a15SKevin Wolf 407779b7a77eSMarkus Armbruster void qmp_blockdev_del(const char *node_name, Error **errp) 407881b936aeSAlberto Garcia { 407981b936aeSAlberto Garcia AioContext *aio_context; 408081b936aeSAlberto Garcia BlockDriverState *bs; 408181b936aeSAlberto Garcia 408281b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 408381b936aeSAlberto Garcia if (!bs) { 408481b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 408581b936aeSAlberto Garcia return; 408681b936aeSAlberto Garcia } 40871f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 4088e467da7bSKevin Wolf error_setg(errp, "Node %s is in use", node_name); 408981b936aeSAlberto Garcia return; 409081b936aeSAlberto Garcia } 409181b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 409281b936aeSAlberto Garcia aio_context_acquire(aio_context); 409381b936aeSAlberto Garcia 409481b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 409581b936aeSAlberto Garcia goto out; 409681b936aeSAlberto Garcia } 409781b936aeSAlberto Garcia 40989ec8873eSKevin Wolf if (!bs->monitor_list.tqe_prev) { 40999c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 41009c4218e9SMax Reitz bs->node_name); 41019c4218e9SMax Reitz goto out; 41029c4218e9SMax Reitz } 41039c4218e9SMax Reitz 41049c4218e9SMax Reitz if (bs->refcnt > 1) { 410581b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 410681b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 410781b936aeSAlberto Garcia goto out; 410881b936aeSAlberto Garcia } 410981b936aeSAlberto Garcia 41109c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 411181b936aeSAlberto Garcia bdrv_unref(bs); 411281b936aeSAlberto Garcia 411381b936aeSAlberto Garcia out: 411481b936aeSAlberto Garcia aio_context_release(aio_context); 411581b936aeSAlberto Garcia } 411681b936aeSAlberto Garcia 41177f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 41187f821597SWen Congyang const char *child_name) 41197f821597SWen Congyang { 41207f821597SWen Congyang BdrvChild *child; 41217f821597SWen Congyang 41227f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 41237f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 41247f821597SWen Congyang return child; 41257f821597SWen Congyang } 41267f821597SWen Congyang } 41277f821597SWen Congyang 41287f821597SWen Congyang return NULL; 41297f821597SWen Congyang } 41307f821597SWen Congyang 41317f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 41327f821597SWen Congyang const char *child, bool has_node, 41337f821597SWen Congyang const char *node, Error **errp) 41347f821597SWen Congyang { 41357f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 41367f821597SWen Congyang BdrvChild *p_child; 41377f821597SWen Congyang 41387f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 41397f821597SWen Congyang if (!parent_bs) { 41407f821597SWen Congyang return; 41417f821597SWen Congyang } 41427f821597SWen Congyang 41437f821597SWen Congyang if (has_child == has_node) { 41447f821597SWen Congyang if (has_child) { 41457f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 41467f821597SWen Congyang } else { 41477f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 41487f821597SWen Congyang } 41497f821597SWen Congyang return; 41507f821597SWen Congyang } 41517f821597SWen Congyang 41527f821597SWen Congyang if (has_child) { 41537f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 41547f821597SWen Congyang if (!p_child) { 41557f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 41567f821597SWen Congyang parent, child); 41577f821597SWen Congyang return; 41587f821597SWen Congyang } 41597f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 41607f821597SWen Congyang } 41617f821597SWen Congyang 41627f821597SWen Congyang if (has_node) { 41637f821597SWen Congyang new_bs = bdrv_find_node(node); 41647f821597SWen Congyang if (!new_bs) { 41657f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 41667f821597SWen Congyang return; 41677f821597SWen Congyang } 41687f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 41697f821597SWen Congyang } 41707f821597SWen Congyang } 41717f821597SWen Congyang 4172fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4173fb5458cdSStefan Hajnoczi { 4174fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4175f0f55dedSAlberto Garcia BlockJob *job; 4176fea68bb6SMarkus Armbruster 4177f0f55dedSAlberto Garcia for (job = block_job_next(NULL); job; job = block_job_next(job)) { 4178559b935fSJohn Snow BlockJobInfoList *elem; 4179559b935fSJohn Snow AioContext *aio_context; 418069691e72SStefan Hajnoczi 4181559b935fSJohn Snow if (block_job_is_internal(job)) { 4182559b935fSJohn Snow continue; 4183559b935fSJohn Snow } 4184559b935fSJohn Snow elem = g_new0(BlockJobInfoList, 1); 4185559b935fSJohn Snow aio_context = blk_get_aio_context(job->blk); 418669691e72SStefan Hajnoczi aio_context_acquire(aio_context); 4187559b935fSJohn Snow elem->value = block_job_query(job, errp); 4188f0f55dedSAlberto Garcia aio_context_release(aio_context); 4189559b935fSJohn Snow if (!elem->value) { 4190559b935fSJohn Snow g_free(elem); 4191559b935fSJohn Snow qapi_free_BlockJobInfoList(head); 4192559b935fSJohn Snow return NULL; 4193559b935fSJohn Snow } 4194fea68bb6SMarkus Armbruster *p_next = elem; 4195fea68bb6SMarkus Armbruster p_next = &elem->next; 4196fea68bb6SMarkus Armbruster } 419769691e72SStefan Hajnoczi 4198fea68bb6SMarkus Armbruster return head; 4199fb5458cdSStefan Hajnoczi } 42004d454574SPaolo Bonzini 4201ca00bbb1SStefan Hajnoczi void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread, 4202882e9b89SStefan Hajnoczi bool has_force, bool force, Error **errp) 4203ca00bbb1SStefan Hajnoczi { 4204ca00bbb1SStefan Hajnoczi AioContext *old_context; 4205ca00bbb1SStefan Hajnoczi AioContext *new_context; 4206ca00bbb1SStefan Hajnoczi BlockDriverState *bs; 4207ca00bbb1SStefan Hajnoczi 4208ca00bbb1SStefan Hajnoczi bs = bdrv_find_node(node_name); 4209ca00bbb1SStefan Hajnoczi if (!bs) { 4210ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find node %s", node_name); 4211ca00bbb1SStefan Hajnoczi return; 4212ca00bbb1SStefan Hajnoczi } 4213ca00bbb1SStefan Hajnoczi 4214882e9b89SStefan Hajnoczi /* Protects against accidents. */ 4215882e9b89SStefan Hajnoczi if (!(has_force && force) && bdrv_has_blk(bs)) { 4216882e9b89SStefan Hajnoczi error_setg(errp, "Node %s is associated with a BlockBackend and could " 4217882e9b89SStefan Hajnoczi "be in use (use force=true to override this check)", 4218882e9b89SStefan Hajnoczi node_name); 4219ca00bbb1SStefan Hajnoczi return; 4220ca00bbb1SStefan Hajnoczi } 4221ca00bbb1SStefan Hajnoczi 4222ca00bbb1SStefan Hajnoczi if (iothread->type == QTYPE_QSTRING) { 4223ca00bbb1SStefan Hajnoczi IOThread *obj = iothread_by_id(iothread->u.s); 4224ca00bbb1SStefan Hajnoczi if (!obj) { 4225ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find iothread %s", iothread->u.s); 4226ca00bbb1SStefan Hajnoczi return; 4227ca00bbb1SStefan Hajnoczi } 4228ca00bbb1SStefan Hajnoczi 4229ca00bbb1SStefan Hajnoczi new_context = iothread_get_aio_context(obj); 4230ca00bbb1SStefan Hajnoczi } else { 4231ca00bbb1SStefan Hajnoczi new_context = qemu_get_aio_context(); 4232ca00bbb1SStefan Hajnoczi } 4233ca00bbb1SStefan Hajnoczi 4234ca00bbb1SStefan Hajnoczi old_context = bdrv_get_aio_context(bs); 4235ca00bbb1SStefan Hajnoczi aio_context_acquire(old_context); 4236ca00bbb1SStefan Hajnoczi 4237ca00bbb1SStefan Hajnoczi bdrv_set_aio_context(bs, new_context); 4238ca00bbb1SStefan Hajnoczi 4239ca00bbb1SStefan Hajnoczi aio_context_release(old_context); 4240ca00bbb1SStefan Hajnoczi } 4241ca00bbb1SStefan Hajnoczi 42427e5c776dSVladimir Sementsov-Ogievskiy void qmp_x_block_latency_histogram_set( 42437e5c776dSVladimir Sementsov-Ogievskiy const char *device, 42447e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries, uint64List *boundaries, 42457e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_read, uint64List *boundaries_read, 42467e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_write, uint64List *boundaries_write, 42477e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_flush, uint64List *boundaries_flush, 42487e5c776dSVladimir Sementsov-Ogievskiy Error **errp) 42497e5c776dSVladimir Sementsov-Ogievskiy { 42507e5c776dSVladimir Sementsov-Ogievskiy BlockBackend *blk = blk_by_name(device); 42517e5c776dSVladimir Sementsov-Ogievskiy BlockAcctStats *stats; 42527e5c776dSVladimir Sementsov-Ogievskiy 42537e5c776dSVladimir Sementsov-Ogievskiy if (!blk) { 42547e5c776dSVladimir Sementsov-Ogievskiy error_setg(errp, "Device '%s' not found", device); 42557e5c776dSVladimir Sementsov-Ogievskiy return; 42567e5c776dSVladimir Sementsov-Ogievskiy } 42577e5c776dSVladimir Sementsov-Ogievskiy stats = blk_get_stats(blk); 42587e5c776dSVladimir Sementsov-Ogievskiy 42597e5c776dSVladimir Sementsov-Ogievskiy if (!has_boundaries && !has_boundaries_read && !has_boundaries_write && 42607e5c776dSVladimir Sementsov-Ogievskiy !has_boundaries_flush) 42617e5c776dSVladimir Sementsov-Ogievskiy { 42627e5c776dSVladimir Sementsov-Ogievskiy block_latency_histograms_clear(stats); 42637e5c776dSVladimir Sementsov-Ogievskiy return; 42647e5c776dSVladimir Sementsov-Ogievskiy } 42657e5c776dSVladimir Sementsov-Ogievskiy 42667e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_read) { 42677e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 42687e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_READ, 42697e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_read ? boundaries_read : boundaries); 42707e5c776dSVladimir Sementsov-Ogievskiy } 42717e5c776dSVladimir Sementsov-Ogievskiy 42727e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_write) { 42737e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 42747e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_WRITE, 42757e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_write ? boundaries_write : boundaries); 42767e5c776dSVladimir Sementsov-Ogievskiy } 42777e5c776dSVladimir Sementsov-Ogievskiy 42787e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_flush) { 42797e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 42807e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_FLUSH, 42817e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_flush ? boundaries_flush : boundaries); 42827e5c776dSVladimir Sementsov-Ogievskiy } 42837e5c776dSVladimir Sementsov-Ogievskiy } 42847e5c776dSVladimir Sementsov-Ogievskiy 42850006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 42864d454574SPaolo Bonzini .name = "drive", 42870006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 42884d454574SPaolo Bonzini .desc = { 42894d454574SPaolo Bonzini { 42904d454574SPaolo Bonzini .name = "snapshot", 42914d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 42924d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 42934d454574SPaolo Bonzini },{ 42944d454574SPaolo Bonzini .name = "aio", 42954d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 42964d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 42974d454574SPaolo Bonzini },{ 4298e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4299e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4300e4b24b49SKevin Wolf .help = "Enable writeback mode", 4301e4b24b49SKevin Wolf },{ 43024d454574SPaolo Bonzini .name = "format", 43034d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 43044d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 43054d454574SPaolo Bonzini },{ 43064d454574SPaolo Bonzini .name = "rerror", 43074d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 43084d454574SPaolo Bonzini .help = "read error action", 43094d454574SPaolo Bonzini },{ 43104d454574SPaolo Bonzini .name = "werror", 43114d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 43124d454574SPaolo Bonzini .help = "write error action", 43134d454574SPaolo Bonzini },{ 43144e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 43154d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 43164d454574SPaolo Bonzini .help = "open drive file as read-only", 4317a2a7862cSPradeep Jagadeesh }, 4318a2a7862cSPradeep Jagadeesh 4319a2a7862cSPradeep Jagadeesh THROTTLE_OPTS, 4320a2a7862cSPradeep Jagadeesh 4321a2a7862cSPradeep Jagadeesh { 432276f4afb4SAlberto Garcia .name = "throttling.group", 432376f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 432476f4afb4SAlberto Garcia .help = "name of the block throttling group", 432576f4afb4SAlberto Garcia },{ 43264d454574SPaolo Bonzini .name = "copy-on-read", 43274d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 43284d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4329465bee1dSPeter Lieven },{ 4330465bee1dSPeter Lieven .name = "detect-zeroes", 4331465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4332465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4333362e9299SAlberto Garcia },{ 4334362e9299SAlberto Garcia .name = "stats-account-invalid", 4335362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4336362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4337362e9299SAlberto Garcia "in the statistics", 4338362e9299SAlberto Garcia },{ 4339362e9299SAlberto Garcia .name = "stats-account-failed", 4340362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4341362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4342362e9299SAlberto Garcia "in the statistics", 43434d454574SPaolo Bonzini }, 43444d454574SPaolo Bonzini { /* end of list */ } 43454d454574SPaolo Bonzini }, 43464d454574SPaolo Bonzini }; 43470006383eSKevin Wolf 43480006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 43490006383eSKevin Wolf .name = "drive", 43500006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 43510006383eSKevin Wolf .desc = { 4352492fdc6fSKevin Wolf /* 4353492fdc6fSKevin Wolf * no elements => accept any params 4354492fdc6fSKevin Wolf * validation will happen later 4355492fdc6fSKevin Wolf */ 43560006383eSKevin Wolf { /* end of list */ } 43570006383eSKevin Wolf }, 43580006383eSKevin Wolf }; 4359