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" 38609f45eaSMax Reitz #include "block/qdict.h" 3976f4afb4SAlberto Garcia #include "block/throttle-groups.h" 4083c9089eSPaolo Bonzini #include "monitor/monitor.h" 41d49b6836SMarkus Armbruster #include "qemu/error-report.h" 421de7afc9SPaolo Bonzini #include "qemu/option.h" 431de7afc9SPaolo Bonzini #include "qemu/config-file.h" 449af23989SMarkus Armbruster #include "qapi/qapi-commands-block.h" 459af23989SMarkus Armbruster #include "qapi/qapi-commands-transaction.h" 469af23989SMarkus Armbruster #include "qapi/qapi-visit-block-core.h" 47452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 4815280c36SMarkus Armbruster #include "qapi/qmp/qnum.h" 496b673957SMarkus Armbruster #include "qapi/qmp/qstring.h" 50e688df6bSMarkus Armbruster #include "qapi/error.h" 51cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 5247e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h" 53b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h" 549c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 55ca00bbb1SStefan Hajnoczi #include "sysemu/iothread.h" 56737e150eSPaolo Bonzini #include "block/block_int.h" 570ab8ed18SDaniel P. Berrange #include "block/trace.h" 589c17d615SPaolo Bonzini #include "sysemu/arch_init.h" 59c616f16eSThomas Huth #include "sysemu/qtest.h" 60f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 61f348b6d1SVeronia Bahaa #include "qemu/help_option.h" 62a2a7862cSPradeep Jagadeesh #include "qemu/throttle-options.h" 63666daa68SMarkus Armbruster 649c4218e9SMax Reitz static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = 659c4218e9SMax Reitz QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); 669c4218e9SMax Reitz 67b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id, 68b33945cfSKevin Wolf bool force, Error **errp); 6982fcf66eSMax Reitz static void blockdev_remove_medium(bool has_device, const char *device, 7082fcf66eSMax Reitz bool has_id, const char *id, Error **errp); 7182fcf66eSMax Reitz static void blockdev_insert_medium(bool has_device, const char *device, 7282fcf66eSMax Reitz bool has_id, const char *id, 7382fcf66eSMax Reitz const char *node_name, Error **errp); 74bf18bee5SColin Lord 751960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 761960966dSMarkus Armbruster [IF_NONE] = "none", 771960966dSMarkus Armbruster [IF_IDE] = "ide", 781960966dSMarkus Armbruster [IF_SCSI] = "scsi", 791960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 801960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 811960966dSMarkus Armbruster [IF_MTD] = "mtd", 821960966dSMarkus Armbruster [IF_SD] = "sd", 831960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 841960966dSMarkus Armbruster [IF_XEN] = "xen", 851960966dSMarkus Armbruster }; 861960966dSMarkus Armbruster 8721dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = { 8827d6bf40SMarkus Armbruster /* 8927d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 9027d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 9127d6bf40SMarkus Armbruster * 92547cb157SWei Jiangang * All controllers used to implement if=T drives need to support 9327d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 9427d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 9527d6bf40SMarkus Armbruster * values. 9627d6bf40SMarkus Armbruster * 9727d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 9827d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 9927d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 10027d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 10127d6bf40SMarkus Armbruster */ 10227d6bf40SMarkus Armbruster [IF_IDE] = 2, 10327d6bf40SMarkus Armbruster [IF_SCSI] = 7, 1041960966dSMarkus Armbruster }; 1051960966dSMarkus Armbruster 10621dff8cfSJohn Snow /** 10721dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides 10821dff8cfSJohn Snow * of the default, global values. 10921dff8cfSJohn Snow */ 11021dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs) 11121dff8cfSJohn Snow { 11218e46a03SMarkus Armbruster BlockBackend *blk; 11321dff8cfSJohn Snow DriveInfo *dinfo; 11421dff8cfSJohn Snow 11521dff8cfSJohn Snow if (max_devs <= 0) { 11621dff8cfSJohn Snow return; 11721dff8cfSJohn Snow } 11821dff8cfSJohn Snow 11918e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 12018e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 12121dff8cfSJohn Snow if (dinfo->type == type) { 12221dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of" 12321dff8cfSJohn Snow " the %s interface, because a drive of that type has" 12421dff8cfSJohn Snow " already been added.\n", if_name[type]); 12521dff8cfSJohn Snow g_assert_not_reached(); 12621dff8cfSJohn Snow } 12721dff8cfSJohn Snow } 12821dff8cfSJohn Snow 12921dff8cfSJohn Snow if_max_devs[type] = max_devs; 13021dff8cfSJohn Snow } 13121dff8cfSJohn Snow 13214bafc54SMarkus Armbruster /* 13314bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 13414bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 13514bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 13614bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 13714bafc54SMarkus Armbruster * when deletion is actually safe. 13814bafc54SMarkus Armbruster */ 1394be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk) 14014bafc54SMarkus Armbruster { 14118e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 1424be74634SMarkus Armbruster BlockDriverState *bs = blk_bs(blk); 14391fddb0dSStefan Hajnoczi AioContext *aio_context; 14414bafc54SMarkus Armbruster 14526f8b3a8SMarkus Armbruster if (!dinfo) { 1462d246f01SKevin Wolf return; 1472d246f01SKevin Wolf } 1482d246f01SKevin Wolf 1495433c24fSMax Reitz if (bs) { 15091fddb0dSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 15191fddb0dSStefan Hajnoczi aio_context_acquire(aio_context); 15291fddb0dSStefan Hajnoczi 15312bde0eeSPaolo Bonzini if (bs->job) { 1543d70ff53SKevin Wolf job_cancel(&bs->job->job, false); 15512bde0eeSPaolo Bonzini } 15691fddb0dSStefan Hajnoczi 15791fddb0dSStefan Hajnoczi aio_context_release(aio_context); 1585433c24fSMax Reitz } 15991fddb0dSStefan Hajnoczi 16014bafc54SMarkus Armbruster dinfo->auto_del = 1; 16114bafc54SMarkus Armbruster } 16214bafc54SMarkus Armbruster 1634be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk) 16414bafc54SMarkus Armbruster { 16518e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 16614bafc54SMarkus Armbruster 1670fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 168efaa7c4eSMax Reitz monitor_remove_blk(blk); 169b9fe8a7aSMarkus Armbruster blk_unref(blk); 17014bafc54SMarkus Armbruster } 17114bafc54SMarkus Armbruster } 17214bafc54SMarkus Armbruster 173d8f94e1bSJohn Snow /** 174d8f94e1bSJohn Snow * Returns the current mapping of how many units per bus 175d8f94e1bSJohn Snow * a particular interface can support. 176d8f94e1bSJohn Snow * 177d8f94e1bSJohn Snow * A positive integer indicates n units per bus. 178d8f94e1bSJohn Snow * 0 implies the mapping has not been established. 179d8f94e1bSJohn Snow * -1 indicates an invalid BlockInterfaceType was given. 180d8f94e1bSJohn Snow */ 181d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type) 182d8f94e1bSJohn Snow { 183d8f94e1bSJohn Snow if (type >= IF_IDE && type < IF_COUNT) { 184d8f94e1bSJohn Snow return if_max_devs[type]; 185d8f94e1bSJohn Snow } 186d8f94e1bSJohn Snow 187d8f94e1bSJohn Snow return -1; 188d8f94e1bSJohn Snow } 189d8f94e1bSJohn Snow 190505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 191505a7fb1SMarkus Armbruster { 192505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 193505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 194505a7fb1SMarkus Armbruster } 195505a7fb1SMarkus Armbruster 196505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 197505a7fb1SMarkus Armbruster { 198505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 199505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 200505a7fb1SMarkus Armbruster } 201505a7fb1SMarkus Armbruster 2022292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 2032292ddaeSMarkus Armbruster { 20470b94331SMarkus Armbruster return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 2052292ddaeSMarkus Armbruster } 2062292ddaeSMarkus Armbruster 2072292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 2085645b0f4SMarkus Armbruster const char *optstr) 209666daa68SMarkus Armbruster { 210666daa68SMarkus Armbruster QemuOpts *opts; 211666daa68SMarkus Armbruster 2122292ddaeSMarkus Armbruster opts = drive_def(optstr); 213666daa68SMarkus Armbruster if (!opts) { 214666daa68SMarkus Armbruster return NULL; 215666daa68SMarkus Armbruster } 2162292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 217f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort); 2182292ddaeSMarkus Armbruster } 2192292ddaeSMarkus Armbruster if (index >= 0) { 220a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort); 2212292ddaeSMarkus Armbruster } 222666daa68SMarkus Armbruster if (file) 223f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort); 224666daa68SMarkus Armbruster return opts; 225666daa68SMarkus Armbruster } 226666daa68SMarkus Armbruster 227666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 228666daa68SMarkus Armbruster { 22918e46a03SMarkus Armbruster BlockBackend *blk; 230666daa68SMarkus Armbruster DriveInfo *dinfo; 231666daa68SMarkus Armbruster 23218e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 23318e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 23418e46a03SMarkus Armbruster if (dinfo && dinfo->type == type 23518e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) { 236666daa68SMarkus Armbruster return dinfo; 237666daa68SMarkus Armbruster } 23818e46a03SMarkus Armbruster } 239666daa68SMarkus Armbruster 240666daa68SMarkus Armbruster return NULL; 241666daa68SMarkus Armbruster } 242666daa68SMarkus Armbruster 243720b8dc0SMarkus Armbruster void drive_check_orphaned(void) 244a66c9dc7SJohn Snow { 24518e46a03SMarkus Armbruster BlockBackend *blk; 246a66c9dc7SJohn Snow DriveInfo *dinfo; 247664cc623SMarkus Armbruster Location loc; 248720b8dc0SMarkus Armbruster bool orphans = false; 249a66c9dc7SJohn Snow 25018e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 25118e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 252a7f53e26SMarkus Armbruster if (!blk_get_attached_dev(blk) && !dinfo->is_default && 253a66c9dc7SJohn Snow dinfo->type != IF_NONE) { 254664cc623SMarkus Armbruster loc_push_none(&loc); 255664cc623SMarkus Armbruster qemu_opts_loc_restore(dinfo->opts); 256720b8dc0SMarkus Armbruster error_report("machine type does not support" 257664cc623SMarkus Armbruster " if=%s,bus=%d,unit=%d", 2585433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit); 259664cc623SMarkus Armbruster loc_pop(&loc); 260720b8dc0SMarkus Armbruster orphans = true; 261a66c9dc7SJohn Snow } 262a66c9dc7SJohn Snow } 263a66c9dc7SJohn Snow 264720b8dc0SMarkus Armbruster if (orphans) { 265720b8dc0SMarkus Armbruster exit(1); 266720b8dc0SMarkus Armbruster } 267a66c9dc7SJohn Snow } 268a66c9dc7SJohn Snow 269f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 270f1bd51acSMarkus Armbruster { 271f1bd51acSMarkus Armbruster return drive_get(type, 272f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 273f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 274f1bd51acSMarkus Armbruster } 275f1bd51acSMarkus Armbruster 276666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 277666daa68SMarkus Armbruster { 278666daa68SMarkus Armbruster int max_bus; 27918e46a03SMarkus Armbruster BlockBackend *blk; 280666daa68SMarkus Armbruster DriveInfo *dinfo; 281666daa68SMarkus Armbruster 282666daa68SMarkus Armbruster max_bus = -1; 28318e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 28418e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 28518e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 286666daa68SMarkus Armbruster max_bus = dinfo->bus; 287666daa68SMarkus Armbruster } 28818e46a03SMarkus Armbruster } 289666daa68SMarkus Armbruster return max_bus; 290666daa68SMarkus Armbruster } 291666daa68SMarkus Armbruster 29213839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 29313839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 29413839974SMarkus Armbruster appropriate bus. */ 29513839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 29613839974SMarkus Armbruster { 29713839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 29813839974SMarkus Armbruster 29913839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 30013839974SMarkus Armbruster } 30113839974SMarkus Armbruster 302666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 303666daa68SMarkus Armbruster { 304807105a7SMarkus Armbruster error_printf(" %s", name); 305666daa68SMarkus Armbruster } 306666daa68SMarkus Armbruster 307aa398a5cSStefan Hajnoczi typedef struct { 308aa398a5cSStefan Hajnoczi QEMUBH *bh; 309fa510ebfSFam Zheng BlockDriverState *bs; 310fa510ebfSFam Zheng } BDRVPutRefBH; 311aa398a5cSStefan Hajnoczi 312b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 313666daa68SMarkus Armbruster { 314666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 31592aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 316666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 31792aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 318666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 31992aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 320666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 32192aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 322666daa68SMarkus Armbruster } else { 323b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 324666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 325666daa68SMarkus Armbruster return -1; 326666daa68SMarkus Armbruster } 327666daa68SMarkus Armbruster } 328666daa68SMarkus Armbruster 32940119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 33040119effSAlberto Garcia Error **errp) 33140119effSAlberto Garcia { 33240119effSAlberto Garcia const QListEntry *entry; 33340119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 33440119effSAlberto Garcia switch (qobject_type(entry->value)) { 33540119effSAlberto Garcia 33640119effSAlberto Garcia case QTYPE_QSTRING: { 33740119effSAlberto Garcia unsigned long long length; 3387dc847ebSMax Reitz const char *str = qstring_get_str(qobject_to(QString, 3397dc847ebSMax Reitz entry->value)); 34040119effSAlberto Garcia if (parse_uint_full(str, &length, 10) == 0 && 34140119effSAlberto Garcia length > 0 && length <= UINT_MAX) { 34240119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 34340119effSAlberto Garcia } else { 34440119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str); 34540119effSAlberto Garcia return false; 34640119effSAlberto Garcia } 34740119effSAlberto Garcia break; 34840119effSAlberto Garcia } 34940119effSAlberto Garcia 35001b2ffceSMarc-André Lureau case QTYPE_QNUM: { 3517dc847ebSMax Reitz int64_t length = qnum_get_int(qobject_to(QNum, entry->value)); 35201b2ffceSMarc-André Lureau 35340119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) { 35440119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 35540119effSAlberto Garcia } else { 35640119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length); 35740119effSAlberto Garcia return false; 35840119effSAlberto Garcia } 35940119effSAlberto Garcia break; 36040119effSAlberto Garcia } 36140119effSAlberto Garcia 36240119effSAlberto Garcia default: 36340119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid"); 36440119effSAlberto Garcia return false; 36540119effSAlberto Garcia } 36640119effSAlberto Garcia } 36740119effSAlberto Garcia return true; 36840119effSAlberto Garcia } 36940119effSAlberto Garcia 37033cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 37133cb7dc8SKevin Wolf 372fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 373fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 374fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 375fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 376fbf8175eSMax Reitz { 377fbf8175eSMax Reitz Error *local_error = NULL; 378fbf8175eSMax Reitz const char *aio; 379fbf8175eSMax Reitz 380fbf8175eSMax Reitz if (bdrv_flags) { 381fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 382fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 383fbf8175eSMax Reitz } 384fbf8175eSMax Reitz 385fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 386fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 387fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 388fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 389fbf8175eSMax Reitz /* this is the default */ 390fbf8175eSMax Reitz } else { 391fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 392fbf8175eSMax Reitz return; 393fbf8175eSMax Reitz } 394fbf8175eSMax Reitz } 395fbf8175eSMax Reitz } 396fbf8175eSMax Reitz 397fbf8175eSMax Reitz /* disk I/O throttling */ 398fbf8175eSMax Reitz if (throttling_group) { 399fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 400fbf8175eSMax Reitz } 401fbf8175eSMax Reitz 402fbf8175eSMax Reitz if (throttle_cfg) { 4031588ab5dSAlberto Garcia throttle_config_init(throttle_cfg); 404fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 405fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 406fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 407fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 408fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 409fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 410fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 411fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 412fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 413fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 414fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 415fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 416fbf8175eSMax Reitz 417fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 418fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 419fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 420fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 421fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 422fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 423fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 424fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 425fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 426fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 427fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 428fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 429fbf8175eSMax Reitz 4308a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = 4318a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); 4328a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = 4338a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); 4348a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = 4358a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); 4368a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = 4378a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); 4388a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = 4398a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); 4408a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = 4418a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); 4428a0fc18dSAlberto Garcia 443fbf8175eSMax Reitz throttle_cfg->op_size = 444fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 445fbf8175eSMax Reitz 446d5851089SAlberto Garcia if (!throttle_is_valid(throttle_cfg, errp)) { 447fbf8175eSMax Reitz return; 448fbf8175eSMax Reitz } 449fbf8175eSMax Reitz } 450fbf8175eSMax Reitz 451fbf8175eSMax Reitz if (detect_zeroes) { 452fbf8175eSMax Reitz *detect_zeroes = 453f7abe0ecSMarc-André Lureau qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, 454fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 455fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 456fbf8175eSMax Reitz &local_error); 457fbf8175eSMax Reitz if (local_error) { 458fbf8175eSMax Reitz error_propagate(errp, local_error); 459fbf8175eSMax Reitz return; 460fbf8175eSMax Reitz } 461fbf8175eSMax Reitz } 462fbf8175eSMax Reitz } 463fbf8175eSMax Reitz 464f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 46518e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 466b681072dSKevin Wolf Error **errp) 467666daa68SMarkus Armbruster { 468666daa68SMarkus Armbruster const char *buf; 469666daa68SMarkus Armbruster int bdrv_flags = 0; 470666daa68SMarkus Armbruster int on_read_error, on_write_error; 471362e9299SAlberto Garcia bool account_invalid, account_failed; 472f87a0e29SAlberto Garcia bool writethrough, read_only; 47326f54e9aSMarkus Armbruster BlockBackend *blk; 474a0f1eab1SMarkus Armbruster BlockDriverState *bs; 475cc0681c4SBenoît Canet ThrottleConfig cfg; 476666daa68SMarkus Armbruster int snapshot = 0; 477c546194fSStefan Hajnoczi Error *error = NULL; 4780006383eSKevin Wolf QemuOpts *opts; 47940119effSAlberto Garcia QDict *interval_dict = NULL; 48040119effSAlberto Garcia QList *interval_list = NULL; 4810006383eSKevin Wolf const char *id; 482fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 483fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 484fbf8175eSMax Reitz const char *throttling_group = NULL; 485666daa68SMarkus Armbruster 486f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 487f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 488f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4890006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 49084d18f06SMarkus Armbruster if (error) { 491b681072dSKevin Wolf error_propagate(errp, error); 4926376f952SMarkus Armbruster goto err_no_opts; 4930006383eSKevin Wolf } 4940006383eSKevin Wolf 4950006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49684d18f06SMarkus Armbruster if (error) { 497b681072dSKevin Wolf error_propagate(errp, error); 498ec9c10d2SStefan Hajnoczi goto early_err; 4990006383eSKevin Wolf } 5000006383eSKevin Wolf 5010006383eSKevin Wolf if (id) { 5020006383eSKevin Wolf qdict_del(bs_opts, "id"); 5030006383eSKevin Wolf } 5040006383eSKevin Wolf 505666daa68SMarkus Armbruster /* extract parameters */ 506666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 507666daa68SMarkus Armbruster 508362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 509362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 510362e9299SAlberto Garcia 511e4b24b49SKevin Wolf writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); 512e4b24b49SKevin Wolf 513ff356ee4SAlberto Garcia id = qemu_opts_id(opts); 514ff356ee4SAlberto Garcia 51540119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51640119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51740119effSAlberto Garcia 51840119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 51940119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 52040119effSAlberto Garcia qdict_first(interval_dict)->key); 52140119effSAlberto Garcia goto early_err; 52240119effSAlberto Garcia } 5232be5506fSAlberto Garcia 524fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 525fbf8175eSMax Reitz &detect_zeroes, &error); 526fbf8175eSMax Reitz if (error) { 527fbf8175eSMax Reitz error_propagate(errp, error); 528ec9c10d2SStefan Hajnoczi goto early_err; 529a9384affSPaolo Bonzini } 530666daa68SMarkus Armbruster 531666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 532c8057f95SPeter Maydell if (is_help_option(buf)) { 533807105a7SMarkus Armbruster error_printf("Supported formats:"); 534666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 535807105a7SMarkus Armbruster error_printf("\n"); 536ec9c10d2SStefan Hajnoczi goto early_err; 537666daa68SMarkus Armbruster } 53874fe54f2SKevin Wolf 539e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 540e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 541ec9c10d2SStefan Hajnoczi goto early_err; 5426db5f5d6SMike Qiu } 54346f5ac20SEric Blake qdict_put_str(bs_opts, "driver", buf); 544666daa68SMarkus Armbruster } 545666daa68SMarkus Armbruster 54692aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 547666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 548b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 54984d18f06SMarkus Armbruster if (error) { 550b681072dSKevin Wolf error_propagate(errp, error); 551ec9c10d2SStefan Hajnoczi goto early_err; 552666daa68SMarkus Armbruster } 553666daa68SMarkus Armbruster } 554666daa68SMarkus Armbruster 55592aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 556666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 557b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 55884d18f06SMarkus Armbruster if (error) { 559b681072dSKevin Wolf error_propagate(errp, error); 560ec9c10d2SStefan Hajnoczi goto early_err; 561666daa68SMarkus Armbruster } 562666daa68SMarkus Armbruster } 563666daa68SMarkus Armbruster 564666daa68SMarkus Armbruster if (snapshot) { 56591a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 566666daa68SMarkus Armbruster } 567666daa68SMarkus Armbruster 568f87a0e29SAlberto Garcia read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false); 569f87a0e29SAlberto Garcia 5705ec18f8cSMax Reitz /* init */ 57139c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5725ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5735ec18f8cSMax Reitz 5746d0eb64dSKevin Wolf blk = blk_new(0, BLK_PERM_ALL); 5755ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5765ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 577f87a0e29SAlberto Garcia blk_rs->read_only = read_only; 5785ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5795ec18f8cSMax Reitz 580cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 5815ec18f8cSMax Reitz } else { 5825ec18f8cSMax Reitz if (file && !*file) { 5835ec18f8cSMax Reitz file = NULL; 5845ec18f8cSMax Reitz } 5855ec18f8cSMax Reitz 58691a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 58791a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 58891a097e7SKevin Wolf * Apply the defaults here instead. */ 58991a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 59091a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 591f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, 592f87a0e29SAlberto Garcia read_only ? "on" : "off"); 59372e775c7SKevin Wolf assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); 59491a097e7SKevin Wolf 59512d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 59612d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 59712d5ee3aSKevin Wolf } 59812d5ee3aSKevin Wolf 599efaa7c4eSMax Reitz blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); 600e4342ce5SMax Reitz if (!blk) { 601e4342ce5SMax Reitz goto err_no_bs_opts; 602e4342ce5SMax Reitz } 603e4342ce5SMax Reitz bs = blk_bs(blk); 6040006383eSKevin Wolf 605e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 606e4342ce5SMax Reitz 6079caa6f3dSPaolo Bonzini block_acct_setup(blk_get_stats(blk), account_invalid, account_failed); 6082be5506fSAlberto Garcia 60940119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6102be5506fSAlberto Garcia blk_unref(blk); 6112be5506fSAlberto Garcia blk = NULL; 6122be5506fSAlberto Garcia goto err_no_bs_opts; 6132be5506fSAlberto Garcia } 6142be5506fSAlberto Garcia } 6155ec18f8cSMax Reitz 6167ca7f0f6SKevin Wolf /* disk I/O throttling */ 6177ca7f0f6SKevin Wolf if (throttle_enabled(&cfg)) { 6187ca7f0f6SKevin Wolf if (!throttling_group) { 619ff356ee4SAlberto Garcia throttling_group = id; 6207ca7f0f6SKevin Wolf } 6217ca7f0f6SKevin Wolf blk_io_limits_enable(blk, throttling_group); 6227ca7f0f6SKevin Wolf blk_set_io_limits(blk, &cfg); 6237ca7f0f6SKevin Wolf } 6247ca7f0f6SKevin Wolf 625e4b24b49SKevin Wolf blk_set_enable_write_cache(blk, !writethrough); 6265ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6270006383eSKevin Wolf 628ff356ee4SAlberto Garcia if (!monitor_add_blk(blk, id, errp)) { 629efaa7c4eSMax Reitz blk_unref(blk); 630efaa7c4eSMax Reitz blk = NULL; 631efaa7c4eSMax Reitz goto err_no_bs_opts; 632efaa7c4eSMax Reitz } 633efaa7c4eSMax Reitz 634e4342ce5SMax Reitz err_no_bs_opts: 6350006383eSKevin Wolf qemu_opts_del(opts); 636cb3e7f08SMarc-André Lureau qobject_unref(interval_dict); 637cb3e7f08SMarc-André Lureau qobject_unref(interval_list); 63818e46a03SMarkus Armbruster return blk; 639a9ae2bffSMarkus Armbruster 640ec9c10d2SStefan Hajnoczi early_err: 641ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 642cb3e7f08SMarc-André Lureau qobject_unref(interval_dict); 643cb3e7f08SMarc-André Lureau qobject_unref(interval_list); 6446376f952SMarkus Armbruster err_no_opts: 645cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 646a9ae2bffSMarkus Armbruster return NULL; 647666daa68SMarkus Armbruster } 648666daa68SMarkus Armbruster 649bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 650bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 651bd745e23SMax Reitz { 652bd745e23SMax Reitz int bdrv_flags = 0; 653bd745e23SMax Reitz 654a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 655a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults. 656a81d6164SKevin Wolf * Apply the defaults here instead. */ 657a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 658a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 659f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off"); 660a81d6164SKevin Wolf 66112d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 66212d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 66312d5ee3aSKevin Wolf } 66412d5ee3aSKevin Wolf 66574e1ae7cSKevin Wolf return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); 666bd745e23SMax Reitz } 667bd745e23SMax Reitz 6689c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void) 6699c4218e9SMax Reitz { 6709c4218e9SMax Reitz BlockDriverState *bs, *next_bs; 6719c4218e9SMax Reitz 6729c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { 6739c4218e9SMax Reitz AioContext *ctx = bdrv_get_aio_context(bs); 6749c4218e9SMax Reitz 6759c4218e9SMax Reitz aio_context_acquire(ctx); 6769c4218e9SMax Reitz bdrv_unref(bs); 6779c4218e9SMax Reitz aio_context_release(ctx); 6789c4218e9SMax Reitz } 6799c4218e9SMax Reitz } 6809c4218e9SMax Reitz 681262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */ 682262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) 683262b4e8fSMax Reitz { 684262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list) 685262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states); 686262b4e8fSMax Reitz } 687262b4e8fSMax Reitz 6885abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 6895abbf0eeSKevin Wolf Error **errp) 69057975222SKevin Wolf { 69157975222SKevin Wolf const char *value; 69257975222SKevin Wolf 69357975222SKevin Wolf value = qemu_opt_get(opts, from); 69457975222SKevin Wolf if (value) { 6955abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 6965abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 6975abbf0eeSKevin Wolf "same time", to, from); 6985abbf0eeSKevin Wolf return; 6995abbf0eeSKevin Wolf } 70020d6cd47SJun Li } 70120d6cd47SJun Li 70220d6cd47SJun Li /* rename all items in opts */ 70320d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 704f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 70557975222SKevin Wolf qemu_opt_unset(opts, from); 70657975222SKevin Wolf } 70757975222SKevin Wolf } 70857975222SKevin Wolf 70933cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 71033cb7dc8SKevin Wolf .name = "drive", 71133cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 71233cb7dc8SKevin Wolf .desc = { 71333cb7dc8SKevin Wolf { 71487a899c5SKevin Wolf .name = "bus", 71587a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 71687a899c5SKevin Wolf .help = "bus number", 71787a899c5SKevin Wolf },{ 71887a899c5SKevin Wolf .name = "unit", 71987a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 72087a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 72187a899c5SKevin Wolf },{ 72287a899c5SKevin Wolf .name = "index", 72387a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 72487a899c5SKevin Wolf .help = "index number", 72587a899c5SKevin Wolf },{ 72633cb7dc8SKevin Wolf .name = "media", 72733cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 72833cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 729593d464bSKevin Wolf },{ 730593d464bSKevin Wolf .name = "if", 731593d464bSKevin Wolf .type = QEMU_OPT_STRING, 732593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 733b41a7338SKevin Wolf },{ 734d095b465SMax Reitz .name = "file", 735d095b465SMax Reitz .type = QEMU_OPT_STRING, 736d095b465SMax Reitz .help = "file name", 73733cb7dc8SKevin Wolf }, 7380ebd24e0SKevin Wolf 7390ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 7400ebd24e0SKevin Wolf { 7414e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 7420ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7430ebd24e0SKevin Wolf .help = "open drive file as read-only", 7440ebd24e0SKevin Wolf },{ 745ee13ed1cSKevin Wolf .name = "rerror", 746ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 747ee13ed1cSKevin Wolf .help = "read error action", 748ee13ed1cSKevin Wolf },{ 749ee13ed1cSKevin Wolf .name = "werror", 750ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 751ee13ed1cSKevin Wolf .help = "write error action", 752ee13ed1cSKevin Wolf },{ 7530ebd24e0SKevin Wolf .name = "copy-on-read", 7540ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7550ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 7560ebd24e0SKevin Wolf }, 7570ebd24e0SKevin Wolf 75833cb7dc8SKevin Wolf { /* end of list */ } 75933cb7dc8SKevin Wolf }, 76033cb7dc8SKevin Wolf }; 76133cb7dc8SKevin Wolf 76260e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 76357975222SKevin Wolf { 76429c4e2b5SKevin Wolf const char *value; 76518e46a03SMarkus Armbruster BlockBackend *blk; 76633cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 767f298d071SKevin Wolf QDict *bs_opts; 76833cb7dc8SKevin Wolf QemuOpts *legacy_opts; 76933cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 770593d464bSKevin Wolf BlockInterfaceType type; 77187a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 772ee13ed1cSKevin Wolf const char *werror, *rerror; 773a7fdbcf0SFam Zheng bool read_only = false; 774a7fdbcf0SFam Zheng bool copy_on_read; 775d095b465SMax Reitz const char *filename; 77633cb7dc8SKevin Wolf Error *local_err = NULL; 777247147fbSKevin Wolf int i; 77829c4e2b5SKevin Wolf 77957975222SKevin Wolf /* Change legacy command line options into QMP ones */ 780247147fbSKevin Wolf static const struct { 781247147fbSKevin Wolf const char *from; 782247147fbSKevin Wolf const char *to; 783247147fbSKevin Wolf } opt_renames[] = { 784247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 785247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 786247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 78757975222SKevin Wolf 788247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 789247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 790247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 79157975222SKevin Wolf 792247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 793247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 794247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 7953e9fab69SBenoît Canet 796247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 797247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 798247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 7993e9fab69SBenoît Canet 800247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8012024c1dfSBenoît Canet 80276f4afb4SAlberto Garcia { "group", "throttling.group" }, 80376f4afb4SAlberto Garcia 8044e200cf8SAlberto Garcia { "readonly", BDRV_OPT_READ_ONLY }, 805247147fbSKevin Wolf }; 806247147fbSKevin Wolf 807247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8085abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8095abbf0eeSKevin Wolf &local_err); 8105abbf0eeSKevin Wolf if (local_err) { 811565f65d2SMarkus Armbruster error_report_err(local_err); 8125abbf0eeSKevin Wolf return NULL; 8135abbf0eeSKevin Wolf } 814247147fbSKevin Wolf } 8150f227a94SKevin Wolf 81629c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 81729c4e2b5SKevin Wolf if (value) { 81829c4e2b5SKevin Wolf int flags = 0; 81904feb4a5SKevin Wolf bool writethrough; 82029c4e2b5SKevin Wolf 82104feb4a5SKevin Wolf if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { 82229c4e2b5SKevin Wolf error_report("invalid cache option"); 82329c4e2b5SKevin Wolf return NULL; 82429c4e2b5SKevin Wolf } 82529c4e2b5SKevin Wolf 82629c4e2b5SKevin Wolf /* Specific options take precedence */ 82754861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 82854861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 82904feb4a5SKevin Wolf !writethrough, &error_abort); 83029c4e2b5SKevin Wolf } 83154861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 83254861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 833cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 83429c4e2b5SKevin Wolf } 83554861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 83654861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 837cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 83829c4e2b5SKevin Wolf } 83929c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 84029c4e2b5SKevin Wolf } 84129c4e2b5SKevin Wolf 842f298d071SKevin Wolf /* Get a QDict for processing the options */ 843f298d071SKevin Wolf bs_opts = qdict_new(); 844f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 845f298d071SKevin Wolf 84687ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 84787ea75d5SPeter Crosthwaite &error_abort); 84833cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 84984d18f06SMarkus Armbruster if (local_err) { 850565f65d2SMarkus Armbruster error_report_err(local_err); 85133cb7dc8SKevin Wolf goto fail; 85233cb7dc8SKevin Wolf } 85333cb7dc8SKevin Wolf 85433cb7dc8SKevin Wolf /* Media type */ 85533cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 85633cb7dc8SKevin Wolf if (value) { 85733cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 85833cb7dc8SKevin Wolf media = MEDIA_DISK; 85933cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 86033cb7dc8SKevin Wolf media = MEDIA_CDROM; 861a7fdbcf0SFam Zheng read_only = true; 86233cb7dc8SKevin Wolf } else { 86333cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 86433cb7dc8SKevin Wolf goto fail; 86533cb7dc8SKevin Wolf } 86633cb7dc8SKevin Wolf } 86733cb7dc8SKevin Wolf 8680ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 8694e200cf8SAlberto Garcia read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false); 8700ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 8710ebd24e0SKevin Wolf 8720ebd24e0SKevin Wolf if (read_only && copy_on_read) { 8733dc6f869SAlistair Francis warn_report("disabling copy-on-read on read-only drive"); 8740ebd24e0SKevin Wolf copy_on_read = false; 8750ebd24e0SKevin Wolf } 8760ebd24e0SKevin Wolf 87746f5ac20SEric Blake qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off"); 87846f5ac20SEric Blake qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off"); 8790ebd24e0SKevin Wolf 880593d464bSKevin Wolf /* Controller type */ 881593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 882593d464bSKevin Wolf if (value) { 883593d464bSKevin Wolf for (type = 0; 884593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 885593d464bSKevin Wolf type++) { 886593d464bSKevin Wolf } 887593d464bSKevin Wolf if (type == IF_COUNT) { 888593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 889593d464bSKevin Wolf goto fail; 890593d464bSKevin Wolf } 891593d464bSKevin Wolf } else { 892593d464bSKevin Wolf type = block_default_type; 893593d464bSKevin Wolf } 894593d464bSKevin Wolf 89587a899c5SKevin Wolf /* Device address specified by bus/unit or index. 89687a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 89787a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 89887a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 89987a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 90087a899c5SKevin Wolf 90187a899c5SKevin Wolf max_devs = if_max_devs[type]; 90287a899c5SKevin Wolf 90387a899c5SKevin Wolf if (index != -1) { 90487a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 90587a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 90687a899c5SKevin Wolf goto fail; 90787a899c5SKevin Wolf } 90887a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 90987a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 91087a899c5SKevin Wolf } 91187a899c5SKevin Wolf 91287a899c5SKevin Wolf if (unit_id == -1) { 91387a899c5SKevin Wolf unit_id = 0; 91487a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 91587a899c5SKevin Wolf unit_id++; 91687a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 91787a899c5SKevin Wolf unit_id -= max_devs; 91887a899c5SKevin Wolf bus_id++; 91987a899c5SKevin Wolf } 92087a899c5SKevin Wolf } 92187a899c5SKevin Wolf } 92287a899c5SKevin Wolf 92387a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 92487a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 92587a899c5SKevin Wolf goto fail; 92687a899c5SKevin Wolf } 92787a899c5SKevin Wolf 92887a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 92987a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 93087a899c5SKevin Wolf bus_id, unit_id, index); 93187a899c5SKevin Wolf goto fail; 93287a899c5SKevin Wolf } 93387a899c5SKevin Wolf 93487a899c5SKevin Wolf /* no id supplied -> create one */ 93587a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 93687a899c5SKevin Wolf char *new_id; 93787a899c5SKevin Wolf const char *mediastr = ""; 93887a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 93987a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 94087a899c5SKevin Wolf } 94187a899c5SKevin Wolf if (max_devs) { 94287a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 94387a899c5SKevin Wolf mediastr, unit_id); 94487a899c5SKevin Wolf } else { 94587a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 94687a899c5SKevin Wolf mediastr, unit_id); 94787a899c5SKevin Wolf } 94846f5ac20SEric Blake qdict_put_str(bs_opts, "id", new_id); 94987a899c5SKevin Wolf g_free(new_id); 95087a899c5SKevin Wolf } 95187a899c5SKevin Wolf 952394c7d4dSKevin Wolf /* Add virtio block device */ 953394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 954394c7d4dSKevin Wolf QemuOpts *devopts; 95587ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 95687ea75d5SPeter Crosthwaite &error_abort); 957394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 9581f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 959394c7d4dSKevin Wolf } else { 960f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 961394c7d4dSKevin Wolf } 962f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 963f43e47dbSMarkus Armbruster &error_abort); 964394c7d4dSKevin Wolf } 965394c7d4dSKevin Wolf 966d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 967d095b465SMax Reitz 968ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 969ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 970ee13ed1cSKevin Wolf if (werror != NULL) { 971ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 972ee13ed1cSKevin Wolf type != IF_NONE) { 973ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 974ee13ed1cSKevin Wolf goto fail; 975ee13ed1cSKevin Wolf } 97646f5ac20SEric Blake qdict_put_str(bs_opts, "werror", werror); 977ee13ed1cSKevin Wolf } 978ee13ed1cSKevin Wolf 979ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 980ee13ed1cSKevin Wolf if (rerror != NULL) { 981ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 982ee13ed1cSKevin Wolf type != IF_NONE) { 983ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 984ee13ed1cSKevin Wolf goto fail; 985ee13ed1cSKevin Wolf } 98646f5ac20SEric Blake qdict_put_str(bs_opts, "rerror", rerror); 987ee13ed1cSKevin Wolf } 988ee13ed1cSKevin Wolf 9892d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 99018e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 9913cb0e25cSMarkus Armbruster bs_opts = NULL; 99218e46a03SMarkus Armbruster if (!blk) { 99384d18f06SMarkus Armbruster if (local_err) { 994565f65d2SMarkus Armbruster error_report_err(local_err); 995b681072dSKevin Wolf } 9962d246f01SKevin Wolf goto fail; 997b681072dSKevin Wolf } else { 99884d18f06SMarkus Armbruster assert(!local_err); 9992d246f01SKevin Wolf } 10002d246f01SKevin Wolf 100126f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 100226f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1003f298d071SKevin Wolf dinfo->opts = all_opts; 10042d246f01SKevin Wolf 1005ee13ed1cSKevin Wolf dinfo->type = type; 100687a899c5SKevin Wolf dinfo->bus = bus_id; 100787a899c5SKevin Wolf dinfo->unit = unit_id; 1008bcf83158SKevin Wolf 100926f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 101026f8b3a8SMarkus Armbruster 1011e34ef046SKevin Wolf switch(type) { 1012e34ef046SKevin Wolf case IF_IDE: 1013e34ef046SKevin Wolf case IF_SCSI: 1014e34ef046SKevin Wolf case IF_XEN: 1015e34ef046SKevin Wolf case IF_NONE: 1016e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1017e34ef046SKevin Wolf break; 1018e34ef046SKevin Wolf default: 1019e34ef046SKevin Wolf break; 1020e34ef046SKevin Wolf } 1021e34ef046SKevin Wolf 10222d246f01SKevin Wolf fail: 102333cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 1024cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 10252d246f01SKevin Wolf return dinfo; 102657975222SKevin Wolf } 102757975222SKevin Wolf 1028b6c1bae5SKevin Wolf static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) 1029b6c1bae5SKevin Wolf { 1030b6c1bae5SKevin Wolf BlockDriverState *bs; 1031b6c1bae5SKevin Wolf 1032b6c1bae5SKevin Wolf bs = bdrv_lookup_bs(name, name, errp); 1033b6c1bae5SKevin Wolf if (bs == NULL) { 1034b6c1bae5SKevin Wolf return NULL; 1035b6c1bae5SKevin Wolf } 1036b6c1bae5SKevin Wolf 1037b6c1bae5SKevin Wolf if (!bdrv_is_root_node(bs)) { 1038b6c1bae5SKevin Wolf error_setg(errp, "Need a root block node"); 1039b6c1bae5SKevin Wolf return NULL; 1040b6c1bae5SKevin Wolf } 1041b6c1bae5SKevin Wolf 1042b6c1bae5SKevin Wolf if (!bdrv_is_inserted(bs)) { 1043b6c1bae5SKevin Wolf error_setg(errp, "Device has no medium"); 1044b6c1bae5SKevin Wolf return NULL; 1045b6c1bae5SKevin Wolf } 1046b6c1bae5SKevin Wolf 1047b6c1bae5SKevin Wolf return bs; 1048b6c1bae5SKevin Wolf } 1049b6c1bae5SKevin Wolf 1050b33945cfSKevin Wolf static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id, 1051b33945cfSKevin Wolf Error **errp) 1052b33945cfSKevin Wolf { 1053b33945cfSKevin Wolf BlockBackend *blk; 1054b33945cfSKevin Wolf 1055b33945cfSKevin Wolf if (!blk_name == !qdev_id) { 1056b33945cfSKevin Wolf error_setg(errp, "Need exactly one of 'device' and 'id'"); 1057b33945cfSKevin Wolf return NULL; 1058b33945cfSKevin Wolf } 1059b33945cfSKevin Wolf 1060b33945cfSKevin Wolf if (qdev_id) { 1061b33945cfSKevin Wolf blk = blk_by_qdev_id(qdev_id, errp); 1062b33945cfSKevin Wolf } else { 1063b33945cfSKevin Wolf blk = blk_by_name(blk_name); 1064b33945cfSKevin Wolf if (blk == NULL) { 1065b33945cfSKevin Wolf error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1066b33945cfSKevin Wolf "Device '%s' not found", blk_name); 1067b33945cfSKevin Wolf } 1068b33945cfSKevin Wolf } 1069b33945cfSKevin Wolf 1070b33945cfSKevin Wolf return blk; 1071b33945cfSKevin Wolf } 1072b33945cfSKevin Wolf 10733e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1074666daa68SMarkus Armbruster { 1075666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1076a0e8544cSFam Zheng BlockBackend *blk; 10772d3735d3SStefan Hajnoczi int ret; 10782d3735d3SStefan Hajnoczi 1079e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1080da31d594SMax Reitz ret = blk_commit_all(); 1081e8877497SStefan Hajnoczi } else { 108284aa0140SStefan Hajnoczi BlockDriverState *bs; 108384aa0140SStefan Hajnoczi AioContext *aio_context; 108484aa0140SStefan Hajnoczi 1085a0e8544cSFam Zheng blk = blk_by_name(device); 1086a0e8544cSFam Zheng if (!blk) { 108758513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1088ac59eb95SMarkus Armbruster return; 10896ab4b5abSMarkus Armbruster } 10905433c24fSMax Reitz if (!blk_is_available(blk)) { 10915433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 10925433c24fSMax Reitz return; 10935433c24fSMax Reitz } 109484aa0140SStefan Hajnoczi 109584aa0140SStefan Hajnoczi bs = blk_bs(blk); 109684aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 109784aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 109884aa0140SStefan Hajnoczi 109984aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 110084aa0140SStefan Hajnoczi 110184aa0140SStefan Hajnoczi aio_context_release(aio_context); 11022d3735d3SStefan Hajnoczi } 110358513bdeSJeff Cody if (ret < 0) { 110458513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 110558513bdeSJeff Cody strerror(-ret)); 1106666daa68SMarkus Armbruster } 1107666daa68SMarkus Armbruster } 1108666daa68SMarkus Armbruster 110910f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 11106cc2a415SPaolo Bonzini { 1111c8a83e85SKevin Wolf TransactionActionList list; 11126cc2a415SPaolo Bonzini 111310f75907SEric Blake list.value = action; 11146cc2a415SPaolo Bonzini list.next = NULL; 111594d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 11166cc2a415SPaolo Bonzini } 11176cc2a415SPaolo Bonzini 11180901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 11190901f67eSBenoît Canet bool has_node_name, const char *node_name, 11200901f67eSBenoît Canet const char *snapshot_file, 11210901f67eSBenoît Canet bool has_snapshot_node_name, 11220901f67eSBenoît Canet const char *snapshot_node_name, 11236106e249SLuiz Capitulino bool has_format, const char *format, 11240901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1125f8882568SJes Sorensen { 1126a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 11270901f67eSBenoît Canet .has_device = has_device, 11286cc2a415SPaolo Bonzini .device = (char *) device, 11290901f67eSBenoît Canet .has_node_name = has_node_name, 11300901f67eSBenoît Canet .node_name = (char *) node_name, 11316cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 11320901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 11330901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 11346cc2a415SPaolo Bonzini .has_format = has_format, 11356cc2a415SPaolo Bonzini .format = (char *) format, 11366cc2a415SPaolo Bonzini .has_mode = has_mode, 11376cc2a415SPaolo Bonzini .mode = mode, 11386cc2a415SPaolo Bonzini }; 113910f75907SEric Blake TransactionAction action = { 114010f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 114132bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 114210f75907SEric Blake }; 114310f75907SEric Blake blockdev_do_action(&action, errp); 1144f8882568SJes Sorensen } 1145f8882568SJes Sorensen 114643de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 114743de7e2dSAlberto Garcia Error **errp) 114843de7e2dSAlberto Garcia { 114943de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 115043de7e2dSAlberto Garcia .node = (char *) node, 115143de7e2dSAlberto Garcia .overlay = (char *) overlay 115243de7e2dSAlberto Garcia }; 115310f75907SEric Blake TransactionAction action = { 115410f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 115532bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 115610f75907SEric Blake }; 115710f75907SEric Blake blockdev_do_action(&action, errp); 115843de7e2dSAlberto Garcia } 115943de7e2dSAlberto Garcia 1160f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1161f323bc9eSWenchao Xia const char *name, 1162f323bc9eSWenchao Xia Error **errp) 1163f323bc9eSWenchao Xia { 1164f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1165f323bc9eSWenchao Xia .device = (char *) device, 1166f323bc9eSWenchao Xia .name = (char *) name 1167f323bc9eSWenchao Xia }; 116810f75907SEric Blake TransactionAction action = { 116910f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 117032bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 117110f75907SEric Blake }; 117210f75907SEric Blake blockdev_do_action(&action, errp); 1173f323bc9eSWenchao Xia } 1174f323bc9eSWenchao Xia 117544e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 117644e3e053SWenchao Xia bool has_id, 117744e3e053SWenchao Xia const char *id, 117844e3e053SWenchao Xia bool has_name, 117944e3e053SWenchao Xia const char *name, 118044e3e053SWenchao Xia Error **errp) 118144e3e053SWenchao Xia { 1182a0e8544cSFam Zheng BlockDriverState *bs; 11834ef3982aSStefan Hajnoczi AioContext *aio_context; 118444e3e053SWenchao Xia QEMUSnapshotInfo sn; 118544e3e053SWenchao Xia Error *local_err = NULL; 118644e3e053SWenchao Xia SnapshotInfo *info = NULL; 118744e3e053SWenchao Xia int ret; 118844e3e053SWenchao Xia 11892dfb4c03SKevin Wolf bs = qmp_get_root_bs(device, errp); 11902dfb4c03SKevin Wolf if (!bs) { 119144e3e053SWenchao Xia return NULL; 119244e3e053SWenchao Xia } 11932dfb4c03SKevin Wolf aio_context = bdrv_get_aio_context(bs); 11945433c24fSMax Reitz aio_context_acquire(aio_context); 119544e3e053SWenchao Xia 119644e3e053SWenchao Xia if (!has_id) { 119744e3e053SWenchao Xia id = NULL; 119844e3e053SWenchao Xia } 119944e3e053SWenchao Xia 120044e3e053SWenchao Xia if (!has_name) { 120144e3e053SWenchao Xia name = NULL; 120244e3e053SWenchao Xia } 120344e3e053SWenchao Xia 120444e3e053SWenchao Xia if (!id && !name) { 120544e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 12065433c24fSMax Reitz goto out_aio_context; 120744e3e053SWenchao Xia } 120844e3e053SWenchao Xia 12090b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 12100b928854SStefan Hajnoczi goto out_aio_context; 12110b928854SStefan Hajnoczi } 12120b928854SStefan Hajnoczi 121344e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 121484d18f06SMarkus Armbruster if (local_err) { 121544e3e053SWenchao Xia error_propagate(errp, local_err); 12164ef3982aSStefan Hajnoczi goto out_aio_context; 121744e3e053SWenchao Xia } 121844e3e053SWenchao Xia if (!ret) { 121944e3e053SWenchao Xia error_setg(errp, 122044e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 122144e3e053SWenchao Xia "device '%s'", 122244e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 12234ef3982aSStefan Hajnoczi goto out_aio_context; 122444e3e053SWenchao Xia } 122544e3e053SWenchao Xia 122644e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 122784d18f06SMarkus Armbruster if (local_err) { 122844e3e053SWenchao Xia error_propagate(errp, local_err); 12294ef3982aSStefan Hajnoczi goto out_aio_context; 123044e3e053SWenchao Xia } 123144e3e053SWenchao Xia 12324ef3982aSStefan Hajnoczi aio_context_release(aio_context); 12334ef3982aSStefan Hajnoczi 12345839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 123544e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 123644e3e053SWenchao Xia info->name = g_strdup(sn.name); 123744e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 123844e3e053SWenchao Xia info->date_sec = sn.date_sec; 123944e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 124044e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 124144e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 124244e3e053SWenchao Xia 124344e3e053SWenchao Xia return info; 12444ef3982aSStefan Hajnoczi 12454ef3982aSStefan Hajnoczi out_aio_context: 12464ef3982aSStefan Hajnoczi aio_context_release(aio_context); 12474ef3982aSStefan Hajnoczi return NULL; 124844e3e053SWenchao Xia } 12498802d1fdSJeff Cody 1250341ebc2fSJohn Snow /** 1251341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1252341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1253341ebc2fSJohn Snow * the node reference and bitmap names. 1254341ebc2fSJohn Snow * 1255341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1256341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1257341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1258341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1259341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1260341ebc2fSJohn Snow * 1261341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1262341ebc2fSJohn Snow */ 1263341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1264341ebc2fSJohn Snow const char *name, 1265341ebc2fSJohn Snow BlockDriverState **pbs, 1266341ebc2fSJohn Snow Error **errp) 1267341ebc2fSJohn Snow { 1268341ebc2fSJohn Snow BlockDriverState *bs; 1269341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1270341ebc2fSJohn Snow 1271341ebc2fSJohn Snow if (!node) { 1272341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1273341ebc2fSJohn Snow return NULL; 1274341ebc2fSJohn Snow } 1275341ebc2fSJohn Snow if (!name) { 1276341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1277341ebc2fSJohn Snow return NULL; 1278341ebc2fSJohn Snow } 1279341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1280341ebc2fSJohn Snow if (!bs) { 1281341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1282341ebc2fSJohn Snow return NULL; 1283341ebc2fSJohn Snow } 1284341ebc2fSJohn Snow 1285341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1286341ebc2fSJohn Snow if (!bitmap) { 1287341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 12882119882cSPaolo Bonzini return NULL; 1289341ebc2fSJohn Snow } 1290341ebc2fSJohn Snow 1291341ebc2fSJohn Snow if (pbs) { 1292341ebc2fSJohn Snow *pbs = bs; 1293341ebc2fSJohn Snow } 1294341ebc2fSJohn Snow 1295341ebc2fSJohn Snow return bitmap; 1296341ebc2fSJohn Snow } 1297341ebc2fSJohn Snow 1298b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1299ba0c86a3SWenchao Xia 130050f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1301ba0c86a3SWenchao Xia 130250f43f0fSJohn Snow /** 130350f43f0fSJohn Snow * BlkActionOps: 130450f43f0fSJohn Snow * Table of operations that define an Action. 130550f43f0fSJohn Snow * 130650f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 130750f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 130850f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 130950f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 131050f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 131150f43f0fSJohn Snow * commit() or abort(). Can be NULL. 131250f43f0fSJohn Snow * 131350f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 131450f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1315ba0c86a3SWenchao Xia */ 131650f43f0fSJohn Snow typedef struct BlkActionOps { 131750f43f0fSJohn Snow size_t instance_size; 131850f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 131950f43f0fSJohn Snow void (*commit)(BlkActionState *common); 132050f43f0fSJohn Snow void (*abort)(BlkActionState *common); 132150f43f0fSJohn Snow void (*clean)(BlkActionState *common); 132250f43f0fSJohn Snow } BlkActionOps; 132350f43f0fSJohn Snow 132450f43f0fSJohn Snow /** 132550f43f0fSJohn Snow * BlkActionState: 132650f43f0fSJohn Snow * Describes one Action's state within a Transaction. 132750f43f0fSJohn Snow * 132850f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 132950f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 133094d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 133150f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 133250f43f0fSJohn Snow * 133350f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 133450f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 133550f43f0fSJohn Snow * base class. 133650f43f0fSJohn Snow */ 133750f43f0fSJohn Snow struct BlkActionState { 1338c8a83e85SKevin Wolf TransactionAction *action; 133950f43f0fSJohn Snow const BlkActionOps *ops; 134062c9e416SKevin Wolf JobTxn *block_job_txn; 134194d16a64SJohn Snow TransactionProperties *txn_props; 134250f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1343ba0c86a3SWenchao Xia }; 1344ba0c86a3SWenchao Xia 1345bbe86010SWenchao Xia /* internal snapshot private data */ 1346bbe86010SWenchao Xia typedef struct InternalSnapshotState { 134750f43f0fSJohn Snow BlkActionState common; 1348bbe86010SWenchao Xia BlockDriverState *bs; 1349bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1350507306ccSFam Zheng bool created; 1351bbe86010SWenchao Xia } InternalSnapshotState; 1352bbe86010SWenchao Xia 135394d16a64SJohn Snow 135494d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 135594d16a64SJohn Snow { 135694d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 135794d16a64SJohn Snow error_setg(errp, 135894d16a64SJohn Snow "Action '%s' does not support Transaction property " 135994d16a64SJohn Snow "completion-mode = %s", 1360977c736fSMarkus Armbruster TransactionActionKind_str(s->action->type), 1361977c736fSMarkus Armbruster ActionCompletionMode_str(s->txn_props->completion_mode)); 136294d16a64SJohn Snow return -1; 136394d16a64SJohn Snow } 136494d16a64SJohn Snow return 0; 136594d16a64SJohn Snow } 136694d16a64SJohn Snow 136750f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1368bbe86010SWenchao Xia Error **errp) 1369bbe86010SWenchao Xia { 1370f70edf99SMarkus Armbruster Error *local_err = NULL; 1371bbe86010SWenchao Xia const char *device; 1372bbe86010SWenchao Xia const char *name; 1373bbe86010SWenchao Xia BlockDriverState *bs; 1374bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1375bbe86010SWenchao Xia bool ret; 1376bbe86010SWenchao Xia qemu_timeval tv; 1377bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1378bbe86010SWenchao Xia InternalSnapshotState *state; 1379a36e458cSStefan Hajnoczi AioContext *aio_context; 1380bbe86010SWenchao Xia int ret1; 1381bbe86010SWenchao Xia 13826a8f9661SEric Blake g_assert(common->action->type == 1383bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 138432bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1385bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1386bbe86010SWenchao Xia 1387bbe86010SWenchao Xia /* 1. parse input */ 1388bbe86010SWenchao Xia device = internal->device; 1389bbe86010SWenchao Xia name = internal->name; 1390bbe86010SWenchao Xia 1391bbe86010SWenchao Xia /* 2. check for validation */ 139294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 139394d16a64SJohn Snow return; 139494d16a64SJohn Snow } 139594d16a64SJohn Snow 139675dfd402SKevin Wolf bs = qmp_get_root_bs(device, errp); 139775dfd402SKevin Wolf if (!bs) { 1398bbe86010SWenchao Xia return; 1399bbe86010SWenchao Xia } 1400bbe86010SWenchao Xia 1401a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 1402a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 14035d6e96efSStefan Hajnoczi 1404507306ccSFam Zheng state->bs = bs; 1405a36e458cSStefan Hajnoczi 1406a36e458cSStefan Hajnoczi /* Paired with .clean() */ 1407507306ccSFam Zheng bdrv_drained_begin(bs); 1408507306ccSFam Zheng 14093dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 1410a36e458cSStefan Hajnoczi goto out; 14113dc7ca3cSStefan Hajnoczi } 14123dc7ca3cSStefan Hajnoczi 1413bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 141481e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1415a36e458cSStefan Hajnoczi goto out; 1416bbe86010SWenchao Xia } 1417bbe86010SWenchao Xia 1418bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 141981e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 142081e5f78aSAlberto Garcia "does not support internal snapshots", 142181e5f78aSAlberto Garcia bs->drv->format_name, device); 1422a36e458cSStefan Hajnoczi goto out; 1423bbe86010SWenchao Xia } 1424bbe86010SWenchao Xia 1425bbe86010SWenchao Xia if (!strlen(name)) { 1426bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1427a36e458cSStefan Hajnoczi goto out; 1428bbe86010SWenchao Xia } 1429bbe86010SWenchao Xia 1430bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1431f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1432f70edf99SMarkus Armbruster &local_err); 1433f70edf99SMarkus Armbruster if (local_err) { 1434f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1435a36e458cSStefan Hajnoczi goto out; 1436bbe86010SWenchao Xia } else if (ret) { 1437bbe86010SWenchao Xia error_setg(errp, 1438bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1439bbe86010SWenchao Xia name, device); 1440a36e458cSStefan Hajnoczi goto out; 1441bbe86010SWenchao Xia } 1442bbe86010SWenchao Xia 1443bbe86010SWenchao Xia /* 3. take the snapshot */ 1444bbe86010SWenchao Xia sn = &state->sn; 1445bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1446bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1447bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1448bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1449bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1450bbe86010SWenchao Xia 1451bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1452bbe86010SWenchao Xia if (ret1 < 0) { 1453bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1454bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1455bbe86010SWenchao Xia name, device); 1456a36e458cSStefan Hajnoczi goto out; 1457bbe86010SWenchao Xia } 1458bbe86010SWenchao Xia 1459bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1460507306ccSFam Zheng state->created = true; 1461a36e458cSStefan Hajnoczi 1462a36e458cSStefan Hajnoczi out: 1463a36e458cSStefan Hajnoczi aio_context_release(aio_context); 1464bbe86010SWenchao Xia } 1465bbe86010SWenchao Xia 146650f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1467bbe86010SWenchao Xia { 1468bbe86010SWenchao Xia InternalSnapshotState *state = 1469bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1470bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1471bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1472a36e458cSStefan Hajnoczi AioContext *aio_context; 1473bbe86010SWenchao Xia Error *local_error = NULL; 1474bbe86010SWenchao Xia 1475507306ccSFam Zheng if (!state->created) { 1476bbe86010SWenchao Xia return; 1477bbe86010SWenchao Xia } 1478bbe86010SWenchao Xia 1479a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1480a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 1481a36e458cSStefan Hajnoczi 1482bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1483c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1484c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1485c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1486c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1487c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1488bbe86010SWenchao Xia } 1489a36e458cSStefan Hajnoczi 1490a36e458cSStefan Hajnoczi aio_context_release(aio_context); 1491bbe86010SWenchao Xia } 1492bbe86010SWenchao Xia 149350f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 14945d6e96efSStefan Hajnoczi { 14955d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 14965d6e96efSStefan Hajnoczi common, common); 1497a36e458cSStefan Hajnoczi AioContext *aio_context; 14985d6e96efSStefan Hajnoczi 1499a36e458cSStefan Hajnoczi if (!state->bs) { 1500a36e458cSStefan Hajnoczi return; 1501a36e458cSStefan Hajnoczi } 1502a36e458cSStefan Hajnoczi 1503a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1504a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 1505a36e458cSStefan Hajnoczi 1506507306ccSFam Zheng bdrv_drained_end(state->bs); 1507a36e458cSStefan Hajnoczi 1508a36e458cSStefan Hajnoczi aio_context_release(aio_context); 15095d6e96efSStefan Hajnoczi } 15105d6e96efSStefan Hajnoczi 1511ba0c86a3SWenchao Xia /* external snapshot private data */ 1512ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 151350f43f0fSJohn Snow BlkActionState common; 15148802d1fdSJeff Cody BlockDriverState *old_bs; 15158802d1fdSJeff Cody BlockDriverState *new_bs; 1516067acf28SKevin Wolf bool overlay_appended; 1517ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 15188802d1fdSJeff Cody 151950f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 15209b9877eeSWenchao Xia Error **errp) 15219b9877eeSWenchao Xia { 15225b363937SMax Reitz int flags = 0; 152343de7e2dSAlberto Garcia QDict *options = NULL; 15249b9877eeSWenchao Xia Error *local_err = NULL; 152543de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1526e2a31e87SWenchao Xia const char *device; 15270901f67eSBenoît Canet const char *node_name; 152843de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 152943de7e2dSAlberto Garcia const char *snapshot_ref; 153043de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1531e2a31e87SWenchao Xia const char *new_image_file; 1532ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1533ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1534c8a83e85SKevin Wolf TransactionAction *action = common->action; 15352d24b60bSStefan Hajnoczi AioContext *aio_context; 15369b9877eeSWenchao Xia 153743de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 153843de7e2dSAlberto Garcia * purpose but a different set of parameters */ 153943de7e2dSAlberto Garcia switch (action->type) { 154043de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 154143de7e2dSAlberto Garcia { 154232bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 154343de7e2dSAlberto Garcia device = s->node; 154443de7e2dSAlberto Garcia node_name = s->node; 154543de7e2dSAlberto Garcia new_image_file = NULL; 154643de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1547e2a31e87SWenchao Xia } 154843de7e2dSAlberto Garcia break; 154943de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 155043de7e2dSAlberto Garcia { 155132bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 155243de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 155343de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 155443de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 155543de7e2dSAlberto Garcia snapshot_ref = NULL; 155643de7e2dSAlberto Garcia } 155743de7e2dSAlberto Garcia break; 155843de7e2dSAlberto Garcia default: 155943de7e2dSAlberto Garcia g_assert_not_reached(); 1560e2a31e87SWenchao Xia } 1561e2a31e87SWenchao Xia 1562e2a31e87SWenchao Xia /* start processing */ 156394d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 156494d16a64SJohn Snow return; 156594d16a64SJohn Snow } 156694d16a64SJohn Snow 156743de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 156843de7e2dSAlberto Garcia if (!state->old_bs) { 15699b9877eeSWenchao Xia return; 15709b9877eeSWenchao Xia } 15719b9877eeSWenchao Xia 15722d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 15732d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 15742d24b60bSStefan Hajnoczi 15752d24b60bSStefan Hajnoczi /* Paired with .clean() */ 1576da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 15775d6e96efSStefan Hajnoczi 1578ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1579c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 15802d24b60bSStefan Hajnoczi goto out; 15819b9877eeSWenchao Xia } 15829b9877eeSWenchao Xia 15833718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 15843718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 15852d24b60bSStefan Hajnoczi goto out; 15869b9877eeSWenchao Xia } 15879b9877eeSWenchao Xia 1588ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1589ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1590c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 15912d24b60bSStefan Hajnoczi goto out; 15929b9877eeSWenchao Xia } 15939b9877eeSWenchao Xia } 15949b9877eeSWenchao Xia 1595212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1596c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 15972d24b60bSStefan Hajnoczi goto out; 1598f6186f49SBenoît Canet } 1599f6186f49SBenoît Canet 160043de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 160132bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 160243de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 160343de7e2dSAlberto Garcia enum NewImageMode mode; 160443de7e2dSAlberto Garcia const char *snapshot_node_name = 160543de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 160643de7e2dSAlberto Garcia 160743de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 160843de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 16092d24b60bSStefan Hajnoczi goto out; 161043de7e2dSAlberto Garcia } 161143de7e2dSAlberto Garcia 161243de7e2dSAlberto Garcia if (snapshot_node_name && 161343de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 161443de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 16152d24b60bSStefan Hajnoczi goto out; 161643de7e2dSAlberto Garcia } 161743de7e2dSAlberto Garcia 1618ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 16192a32c6e8SJohn Snow flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ); 16202a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING; 16219b9877eeSWenchao Xia 16229b9877eeSWenchao Xia /* create new image w/backing file */ 162343de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 16249b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1625f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1626f86b8b58SKevin Wolf if (size < 0) { 1627f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 16282d24b60bSStefan Hajnoczi goto out; 1629f86b8b58SKevin Wolf } 16309b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1631ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1632ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 16339217283dSFam Zheng NULL, size, flags, false, &local_err); 163484d18f06SMarkus Armbruster if (local_err) { 16359b9877eeSWenchao Xia error_propagate(errp, local_err); 16362d24b60bSStefan Hajnoczi goto out; 16379b9877eeSWenchao Xia } 16389b9877eeSWenchao Xia } 16399b9877eeSWenchao Xia 16400901f67eSBenoît Canet options = qdict_new(); 164143de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 164246f5ac20SEric Blake qdict_put_str(options, "node-name", snapshot_node_name); 16430901f67eSBenoît Canet } 164446f5ac20SEric Blake qdict_put_str(options, "driver", format); 164543de7e2dSAlberto Garcia } 164643de7e2dSAlberto Garcia 16475b363937SMax Reitz state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, 16485b363937SMax Reitz errp); 1649f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 16505b363937SMax Reitz if (!state->new_bs) { 16512d24b60bSStefan Hajnoczi goto out; 165243de7e2dSAlberto Garcia } 165343de7e2dSAlberto Garcia 16541f0c461bSKevin Wolf if (bdrv_has_blk(state->new_bs)) { 1655e467da7bSKevin Wolf error_setg(errp, "The snapshot is already in use"); 16562d24b60bSStefan Hajnoczi goto out; 165743de7e2dSAlberto Garcia } 165843de7e2dSAlberto Garcia 165943de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 166043de7e2dSAlberto Garcia errp)) { 16612d24b60bSStefan Hajnoczi goto out; 166243de7e2dSAlberto Garcia } 166343de7e2dSAlberto Garcia 166443de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 166543de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 16662d24b60bSStefan Hajnoczi goto out; 166708b24cfeSAlberto Garcia } 166808b24cfeSAlberto Garcia 166908b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 167008b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 16712d24b60bSStefan Hajnoczi goto out; 1672b2c2832cSKevin Wolf } 1673b2c2832cSKevin Wolf 16742d24b60bSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, aio_context); 1675c26a5ab7SFam Zheng 1676b2c2832cSKevin Wolf /* This removes our old bs and adds the new bs. This is an operation that 1677b2c2832cSKevin Wolf * can fail, so we need to do it in .prepare; undoing it for abort is 1678b2c2832cSKevin Wolf * always possible. */ 1679b2c2832cSKevin Wolf bdrv_ref(state->new_bs); 1680b2c2832cSKevin Wolf bdrv_append(state->new_bs, state->old_bs, &local_err); 1681b2c2832cSKevin Wolf if (local_err) { 1682b2c2832cSKevin Wolf error_propagate(errp, local_err); 16832d24b60bSStefan Hajnoczi goto out; 16849b9877eeSWenchao Xia } 1685067acf28SKevin Wolf state->overlay_appended = true; 16862d24b60bSStefan Hajnoczi 16872d24b60bSStefan Hajnoczi out: 16882d24b60bSStefan Hajnoczi aio_context_release(aio_context); 16899b9877eeSWenchao Xia } 16909b9877eeSWenchao Xia 169150f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 16923b0047e8SWenchao Xia { 1693ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1694ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 16952d24b60bSStefan Hajnoczi AioContext *aio_context; 16962d24b60bSStefan Hajnoczi 16972d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 16982d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 1699ba0c86a3SWenchao Xia 17003b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 17013b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 17023b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 1703d3faa13eSPaolo Bonzini if (!atomic_read(&state->old_bs->copy_on_read)) { 1704dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 17053b0047e8SWenchao Xia NULL); 17063b0047e8SWenchao Xia } 17072d24b60bSStefan Hajnoczi 17082d24b60bSStefan Hajnoczi aio_context_release(aio_context); 17094c844983SKevin Wolf } 17103b0047e8SWenchao Xia 171150f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 171296b86bf7SWenchao Xia { 1713ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1714ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1715ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 1716067acf28SKevin Wolf if (state->overlay_appended) { 17172d24b60bSStefan Hajnoczi AioContext *aio_context; 17182d24b60bSStefan Hajnoczi 17192d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 17202d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 17212d24b60bSStefan Hajnoczi 1722719fc28cSJeff Cody bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd() 1723719fc28cSJeff Cody close state->old_bs; we need it */ 1724719fc28cSJeff Cody bdrv_set_backing_hd(state->new_bs, NULL, &error_abort); 17255fe31c25SKevin Wolf bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); 1726719fc28cSJeff Cody bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */ 17272d24b60bSStefan Hajnoczi 17282d24b60bSStefan Hajnoczi aio_context_release(aio_context); 1729b2c2832cSKevin Wolf } 173096b86bf7SWenchao Xia } 1731da763e83SFam Zheng } 1732da763e83SFam Zheng 173350f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1734da763e83SFam Zheng { 1735da763e83SFam Zheng ExternalSnapshotState *state = 1736da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 17372d24b60bSStefan Hajnoczi AioContext *aio_context; 17382d24b60bSStefan Hajnoczi 17392d24b60bSStefan Hajnoczi if (!state->old_bs) { 17402d24b60bSStefan Hajnoczi return; 17412d24b60bSStefan Hajnoczi } 17422d24b60bSStefan Hajnoczi 17432d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 17442d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 17452d24b60bSStefan Hajnoczi 1746da763e83SFam Zheng bdrv_drained_end(state->old_bs); 1747b2c2832cSKevin Wolf bdrv_unref(state->new_bs); 17482d24b60bSStefan Hajnoczi 17492d24b60bSStefan Hajnoczi aio_context_release(aio_context); 175096b86bf7SWenchao Xia } 175196b86bf7SWenchao Xia 17523037f364SStefan Hajnoczi typedef struct DriveBackupState { 175350f43f0fSJohn Snow BlkActionState common; 17543037f364SStefan Hajnoczi BlockDriverState *bs; 17553037f364SStefan Hajnoczi BlockJob *job; 17563037f364SStefan Hajnoczi } DriveBackupState; 17573037f364SStefan Hajnoczi 175862c9e416SKevin Wolf static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn, 175981206a89SPavel Butsykin Error **errp); 176078f51fdeSJohn Snow 176150f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 17623037f364SStefan Hajnoczi { 17633037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1764b7e4fa22SKevin Wolf BlockDriverState *bs; 17653037f364SStefan Hajnoczi DriveBackup *backup; 176666d56054SStefan Hajnoczi AioContext *aio_context; 17673037f364SStefan Hajnoczi Error *local_err = NULL; 17683037f364SStefan Hajnoczi 17696a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 177032bafa8fSEric Blake backup = common->action->u.drive_backup.data; 17713037f364SStefan Hajnoczi 1772b7e4fa22SKevin Wolf bs = qmp_get_root_bs(backup->device, errp); 1773b7e4fa22SKevin Wolf if (!bs) { 17741fdd4b7bSFam Zheng return; 17751fdd4b7bSFam Zheng } 17761fdd4b7bSFam Zheng 177766d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 177866d56054SStefan Hajnoczi aio_context_acquire(aio_context); 177966d56054SStefan Hajnoczi 178066d56054SStefan Hajnoczi /* Paired with .clean() */ 1781b7e4fa22SKevin Wolf bdrv_drained_begin(bs); 178266d56054SStefan Hajnoczi 1783b7e4fa22SKevin Wolf state->bs = bs; 17845d6e96efSStefan Hajnoczi 1785111049a4SJohn Snow state->job = do_drive_backup(backup, common->block_job_txn, &local_err); 178684d18f06SMarkus Armbruster if (local_err) { 17873037f364SStefan Hajnoczi error_propagate(errp, local_err); 178866d56054SStefan Hajnoczi goto out; 17893037f364SStefan Hajnoczi } 179066d56054SStefan Hajnoczi 179166d56054SStefan Hajnoczi out: 179266d56054SStefan Hajnoczi aio_context_release(aio_context); 1793111049a4SJohn Snow } 17943037f364SStefan Hajnoczi 1795111049a4SJohn Snow static void drive_backup_commit(BlkActionState *common) 1796111049a4SJohn Snow { 1797111049a4SJohn Snow DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 179866d56054SStefan Hajnoczi AioContext *aio_context; 179966d56054SStefan Hajnoczi 180066d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 180166d56054SStefan Hajnoczi aio_context_acquire(aio_context); 180266d56054SStefan Hajnoczi 1803111049a4SJohn Snow assert(state->job); 1804da01ff7fSKevin Wolf job_start(&state->job->job); 180566d56054SStefan Hajnoczi 180666d56054SStefan Hajnoczi aio_context_release(aio_context); 18073037f364SStefan Hajnoczi } 18083037f364SStefan Hajnoczi 180950f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 18103037f364SStefan Hajnoczi { 18113037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18123037f364SStefan Hajnoczi 1813111049a4SJohn Snow if (state->job) { 181466d56054SStefan Hajnoczi AioContext *aio_context; 181566d56054SStefan Hajnoczi 181666d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 181766d56054SStefan Hajnoczi aio_context_acquire(aio_context); 181866d56054SStefan Hajnoczi 18193d70ff53SKevin Wolf job_cancel_sync(&state->job->job); 182066d56054SStefan Hajnoczi 182166d56054SStefan Hajnoczi aio_context_release(aio_context); 18223037f364SStefan Hajnoczi } 18233037f364SStefan Hajnoczi } 18243037f364SStefan Hajnoczi 182550f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 18265d6e96efSStefan Hajnoczi { 18275d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 182866d56054SStefan Hajnoczi AioContext *aio_context; 18295d6e96efSStefan Hajnoczi 183066d56054SStefan Hajnoczi if (!state->bs) { 183166d56054SStefan Hajnoczi return; 18325d6e96efSStefan Hajnoczi } 183366d56054SStefan Hajnoczi 183466d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 183566d56054SStefan Hajnoczi aio_context_acquire(aio_context); 183666d56054SStefan Hajnoczi 183766d56054SStefan Hajnoczi bdrv_drained_end(state->bs); 183866d56054SStefan Hajnoczi 183966d56054SStefan Hajnoczi aio_context_release(aio_context); 18405d6e96efSStefan Hajnoczi } 18415d6e96efSStefan Hajnoczi 1842bd8baecdSFam Zheng typedef struct BlockdevBackupState { 184350f43f0fSJohn Snow BlkActionState common; 1844bd8baecdSFam Zheng BlockDriverState *bs; 1845bd8baecdSFam Zheng BlockJob *job; 1846bd8baecdSFam Zheng } BlockdevBackupState; 1847bd8baecdSFam Zheng 184862c9e416SKevin Wolf static BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn, 1849dc7a4a9eSPavel Butsykin Error **errp); 185078f51fdeSJohn Snow 185150f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1852bd8baecdSFam Zheng { 1853bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1854bd8baecdSFam Zheng BlockdevBackup *backup; 1855cef34eebSKevin Wolf BlockDriverState *bs, *target; 1856edd5adeeSStefan Hajnoczi AioContext *aio_context; 1857bd8baecdSFam Zheng Error *local_err = NULL; 1858bd8baecdSFam Zheng 18596a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 186032bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1861bd8baecdSFam Zheng 1862930fe17fSVladimir Sementsov-Ogievskiy bs = bdrv_lookup_bs(backup->device, backup->device, errp); 1863cef34eebSKevin Wolf if (!bs) { 1864ff52bf36SFam Zheng return; 1865ff52bf36SFam Zheng } 1866ff52bf36SFam Zheng 186739d990acSKevin Wolf target = bdrv_lookup_bs(backup->target, backup->target, errp); 18685433c24fSMax Reitz if (!target) { 1869bd8baecdSFam Zheng return; 1870bd8baecdSFam Zheng } 1871bd8baecdSFam Zheng 1872edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 1873edd5adeeSStefan Hajnoczi if (aio_context != bdrv_get_aio_context(target)) { 1874bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1875bd8baecdSFam Zheng return; 1876bd8baecdSFam Zheng } 1877edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1878cef34eebSKevin Wolf state->bs = bs; 1879edd5adeeSStefan Hajnoczi 1880edd5adeeSStefan Hajnoczi /* Paired with .clean() */ 1881ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1882bd8baecdSFam Zheng 1883111049a4SJohn Snow state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err); 1884bd8baecdSFam Zheng if (local_err) { 1885bd8baecdSFam Zheng error_propagate(errp, local_err); 1886edd5adeeSStefan Hajnoczi goto out; 1887bd8baecdSFam Zheng } 1888edd5adeeSStefan Hajnoczi 1889edd5adeeSStefan Hajnoczi out: 1890edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 1891111049a4SJohn Snow } 1892bd8baecdSFam Zheng 1893111049a4SJohn Snow static void blockdev_backup_commit(BlkActionState *common) 1894111049a4SJohn Snow { 1895111049a4SJohn Snow BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1896edd5adeeSStefan Hajnoczi AioContext *aio_context; 1897edd5adeeSStefan Hajnoczi 1898edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1899edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1900edd5adeeSStefan Hajnoczi 1901111049a4SJohn Snow assert(state->job); 1902da01ff7fSKevin Wolf job_start(&state->job->job); 1903edd5adeeSStefan Hajnoczi 1904edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 1905bd8baecdSFam Zheng } 1906bd8baecdSFam Zheng 190750f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1908bd8baecdSFam Zheng { 1909bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1910bd8baecdSFam Zheng 1911111049a4SJohn Snow if (state->job) { 1912edd5adeeSStefan Hajnoczi AioContext *aio_context; 1913edd5adeeSStefan Hajnoczi 1914edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1915edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1916edd5adeeSStefan Hajnoczi 19173d70ff53SKevin Wolf job_cancel_sync(&state->job->job); 1918edd5adeeSStefan Hajnoczi 1919edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 1920bd8baecdSFam Zheng } 1921bd8baecdSFam Zheng } 1922bd8baecdSFam Zheng 192350f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 1924bd8baecdSFam Zheng { 1925bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1926edd5adeeSStefan Hajnoczi AioContext *aio_context; 1927bd8baecdSFam Zheng 1928edd5adeeSStefan Hajnoczi if (!state->bs) { 1929edd5adeeSStefan Hajnoczi return; 1930bd8baecdSFam Zheng } 1931edd5adeeSStefan Hajnoczi 1932edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1933edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1934edd5adeeSStefan Hajnoczi 1935edd5adeeSStefan Hajnoczi bdrv_drained_end(state->bs); 1936edd5adeeSStefan Hajnoczi 1937edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 1938bd8baecdSFam Zheng } 1939bd8baecdSFam Zheng 1940df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 194150f43f0fSJohn Snow BlkActionState common; 1942df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 1943df9a681dSFam Zheng BlockDriverState *bs; 1944df9a681dSFam Zheng HBitmap *backup; 1945df9a681dSFam Zheng bool prepared; 1946c6490447SVladimir Sementsov-Ogievskiy bool was_enabled; 1947df9a681dSFam Zheng } BlockDirtyBitmapState; 1948df9a681dSFam Zheng 194950f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 1950df9a681dSFam Zheng Error **errp) 1951df9a681dSFam Zheng { 1952df9a681dSFam Zheng Error *local_err = NULL; 1953df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 1954df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1955df9a681dSFam Zheng common, common); 1956df9a681dSFam Zheng 195794d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 195894d16a64SJohn Snow return; 195994d16a64SJohn Snow } 196094d16a64SJohn Snow 196132bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 1962df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 1963df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 1964df9a681dSFam Zheng action->has_granularity, action->granularity, 1965fd5ae4ccSVladimir Sementsov-Ogievskiy action->has_persistent, action->persistent, 1966eb738bb5SVladimir Sementsov-Ogievskiy action->has_autoload, action->autoload, 1967a6e2ca5fSVladimir Sementsov-Ogievskiy action->has_x_disabled, action->x_disabled, 1968df9a681dSFam Zheng &local_err); 1969df9a681dSFam Zheng 1970df9a681dSFam Zheng if (!local_err) { 1971df9a681dSFam Zheng state->prepared = true; 1972df9a681dSFam Zheng } else { 1973df9a681dSFam Zheng error_propagate(errp, local_err); 1974df9a681dSFam Zheng } 1975df9a681dSFam Zheng } 1976df9a681dSFam Zheng 197750f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 1978df9a681dSFam Zheng { 1979df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 1980df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1981df9a681dSFam Zheng common, common); 1982df9a681dSFam Zheng 198332bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 1984df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 1985df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 1986df9a681dSFam Zheng */ 1987df9a681dSFam Zheng if (state->prepared) { 1988df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 1989df9a681dSFam Zheng } 1990df9a681dSFam Zheng } 1991df9a681dSFam Zheng 199250f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 1993df9a681dSFam Zheng Error **errp) 1994df9a681dSFam Zheng { 1995df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1996df9a681dSFam Zheng common, common); 1997df9a681dSFam Zheng BlockDirtyBitmap *action; 1998df9a681dSFam Zheng 199994d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 200094d16a64SJohn Snow return; 200194d16a64SJohn Snow } 200294d16a64SJohn Snow 200332bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2004df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2005df9a681dSFam Zheng action->name, 2006df9a681dSFam Zheng &state->bs, 2007df9a681dSFam Zheng errp); 2008df9a681dSFam Zheng if (!state->bitmap) { 2009df9a681dSFam Zheng return; 2010df9a681dSFam Zheng } 2011df9a681dSFam Zheng 2012df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2013df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2014df9a681dSFam Zheng return; 20154f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(state->bitmap)) { 20164f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, "Cannot modify a locked bitmap"); 20174f43e953SVladimir Sementsov-Ogievskiy return; 2018df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2019df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2020df9a681dSFam Zheng return; 2021d6883bc9SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(state->bitmap)) { 2022d6883bc9SVladimir Sementsov-Ogievskiy error_setg(errp, "Cannot clear a readonly bitmap"); 2023d6883bc9SVladimir Sementsov-Ogievskiy return; 2024df9a681dSFam Zheng } 2025df9a681dSFam Zheng 2026df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2027df9a681dSFam Zheng } 2028df9a681dSFam Zheng 202950f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2030df9a681dSFam Zheng { 2031df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2032df9a681dSFam Zheng common, common); 2033df9a681dSFam Zheng 2034184dd9c4SJohn Snow if (state->backup) { 2035df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2036df9a681dSFam Zheng } 2037184dd9c4SJohn Snow } 2038df9a681dSFam Zheng 203950f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2040df9a681dSFam Zheng { 2041df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2042df9a681dSFam Zheng common, common); 2043df9a681dSFam Zheng 2044df9a681dSFam Zheng hbitmap_free(state->backup); 2045df9a681dSFam Zheng } 2046df9a681dSFam Zheng 2047c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_prepare(BlkActionState *common, 2048c6490447SVladimir Sementsov-Ogievskiy Error **errp) 2049c6490447SVladimir Sementsov-Ogievskiy { 2050c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmap *action; 2051c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2052c6490447SVladimir Sementsov-Ogievskiy common, common); 2053c6490447SVladimir Sementsov-Ogievskiy 2054c6490447SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 2055c6490447SVladimir Sementsov-Ogievskiy return; 2056c6490447SVladimir Sementsov-Ogievskiy } 2057c6490447SVladimir Sementsov-Ogievskiy 2058c6490447SVladimir Sementsov-Ogievskiy action = common->action->u.x_block_dirty_bitmap_enable.data; 2059c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, 2060c6490447SVladimir Sementsov-Ogievskiy action->name, 2061c6490447SVladimir Sementsov-Ogievskiy NULL, 2062c6490447SVladimir Sementsov-Ogievskiy errp); 2063c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) { 2064c6490447SVladimir Sementsov-Ogievskiy return; 2065c6490447SVladimir Sementsov-Ogievskiy } 2066c6490447SVladimir Sementsov-Ogievskiy 2067c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); 2068c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap); 2069c6490447SVladimir Sementsov-Ogievskiy } 2070c6490447SVladimir Sementsov-Ogievskiy 2071c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_abort(BlkActionState *common) 2072c6490447SVladimir Sementsov-Ogievskiy { 2073c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2074c6490447SVladimir Sementsov-Ogievskiy common, common); 2075c6490447SVladimir Sementsov-Ogievskiy 2076c6490447SVladimir Sementsov-Ogievskiy if (!state->was_enabled) { 2077c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap); 2078c6490447SVladimir Sementsov-Ogievskiy } 2079c6490447SVladimir Sementsov-Ogievskiy } 2080c6490447SVladimir Sementsov-Ogievskiy 2081c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_prepare(BlkActionState *common, 2082c6490447SVladimir Sementsov-Ogievskiy Error **errp) 2083c6490447SVladimir Sementsov-Ogievskiy { 2084c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmap *action; 2085c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2086c6490447SVladimir Sementsov-Ogievskiy common, common); 2087c6490447SVladimir Sementsov-Ogievskiy 2088c6490447SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 2089c6490447SVladimir Sementsov-Ogievskiy return; 2090c6490447SVladimir Sementsov-Ogievskiy } 2091c6490447SVladimir Sementsov-Ogievskiy 2092c6490447SVladimir Sementsov-Ogievskiy action = common->action->u.x_block_dirty_bitmap_disable.data; 2093c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, 2094c6490447SVladimir Sementsov-Ogievskiy action->name, 2095c6490447SVladimir Sementsov-Ogievskiy NULL, 2096c6490447SVladimir Sementsov-Ogievskiy errp); 2097c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) { 2098c6490447SVladimir Sementsov-Ogievskiy return; 2099c6490447SVladimir Sementsov-Ogievskiy } 2100c6490447SVladimir Sementsov-Ogievskiy 2101c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); 2102c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap); 2103c6490447SVladimir Sementsov-Ogievskiy } 2104c6490447SVladimir Sementsov-Ogievskiy 2105c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_abort(BlkActionState *common) 2106c6490447SVladimir Sementsov-Ogievskiy { 2107c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2108c6490447SVladimir Sementsov-Ogievskiy common, common); 2109c6490447SVladimir Sementsov-Ogievskiy 2110c6490447SVladimir Sementsov-Ogievskiy if (state->was_enabled) { 2111c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap); 2112c6490447SVladimir Sementsov-Ogievskiy } 2113c6490447SVladimir Sementsov-Ogievskiy } 2114c6490447SVladimir Sementsov-Ogievskiy 211550f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 211678b18b78SStefan Hajnoczi { 211778b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 211878b18b78SStefan Hajnoczi } 211978b18b78SStefan Hajnoczi 212050f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 212178b18b78SStefan Hajnoczi { 2122dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 212378b18b78SStefan Hajnoczi } 212478b18b78SStefan Hajnoczi 212550f43f0fSJohn Snow static const BlkActionOps actions[] = { 212643de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 212743de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 212843de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 212943de7e2dSAlberto Garcia .commit = external_snapshot_commit, 213043de7e2dSAlberto Garcia .abort = external_snapshot_abort, 21314ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 213243de7e2dSAlberto Garcia }, 2133c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2134ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2135ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2136ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2137ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2138da763e83SFam Zheng .clean = external_snapshot_clean, 2139ba0c86a3SWenchao Xia }, 21403037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21413037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21423037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 2143111049a4SJohn Snow .commit = drive_backup_commit, 21443037f364SStefan Hajnoczi .abort = drive_backup_abort, 21455d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21463037f364SStefan Hajnoczi }, 2147bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2148bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2149bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2150111049a4SJohn Snow .commit = blockdev_backup_commit, 2151bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2152bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2153bd8baecdSFam Zheng }, 215478b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 215550f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 215678b18b78SStefan Hajnoczi .prepare = abort_prepare, 215778b18b78SStefan Hajnoczi .commit = abort_commit, 215878b18b78SStefan Hajnoczi }, 2159bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2160bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2161bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2162bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21635d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2164bbe86010SWenchao Xia }, 2165df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2166df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2167df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2168df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2169df9a681dSFam Zheng }, 2170df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2171df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2172df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2173df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2174df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2175c6490447SVladimir Sementsov-Ogievskiy }, 2176c6490447SVladimir Sementsov-Ogievskiy [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_ENABLE] = { 2177c6490447SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 2178c6490447SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_enable_prepare, 2179c6490447SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_enable_abort, 2180c6490447SVladimir Sementsov-Ogievskiy }, 2181c6490447SVladimir Sementsov-Ogievskiy [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_DISABLE] = { 2182c6490447SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 2183c6490447SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_disable_prepare, 2184c6490447SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_disable_abort, 2185df9a681dSFam Zheng } 2186ba0c86a3SWenchao Xia }; 2187ba0c86a3SWenchao Xia 218894d16a64SJohn Snow /** 218994d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 219094d16a64SJohn Snow * that structure with desired defaults if they are unset. 219194d16a64SJohn Snow */ 219294d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 219394d16a64SJohn Snow TransactionProperties *props) 219494d16a64SJohn Snow { 219594d16a64SJohn Snow if (!props) { 219694d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 219794d16a64SJohn Snow } 219894d16a64SJohn Snow 219994d16a64SJohn Snow if (!props->has_completion_mode) { 220094d16a64SJohn Snow props->has_completion_mode = true; 220194d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 220294d16a64SJohn Snow } 220394d16a64SJohn Snow 220494d16a64SJohn Snow return props; 220594d16a64SJohn Snow } 220694d16a64SJohn Snow 22078802d1fdSJeff Cody /* 2208b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2209b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 22108802d1fdSJeff Cody */ 221194d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 221294d16a64SJohn Snow bool has_props, 221394d16a64SJohn Snow struct TransactionProperties *props, 221494d16a64SJohn Snow Error **errp) 22158802d1fdSJeff Cody { 2216c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 221762c9e416SKevin Wolf JobTxn *block_job_txn = NULL; 221850f43f0fSJohn Snow BlkActionState *state, *next; 221943e17041SLuiz Capitulino Error *local_err = NULL; 22208802d1fdSJeff Cody 222150f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 22228802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 22238802d1fdSJeff Cody 222494d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 222562c9e416SKevin Wolf * If not, we don't really need to make a JobTxn. 222694d16a64SJohn Snow */ 222794d16a64SJohn Snow props = get_transaction_properties(props); 222894d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 22297eaa8fb5SKevin Wolf block_job_txn = job_txn_new(); 223094d16a64SJohn Snow } 223194d16a64SJohn Snow 2232b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 22338802d1fdSJeff Cody bdrv_drain_all(); 22348802d1fdSJeff Cody 2235b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 22368802d1fdSJeff Cody while (NULL != dev_entry) { 2237c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 223850f43f0fSJohn Snow const BlkActionOps *ops; 223952e7c241SPaolo Bonzini 22408802d1fdSJeff Cody dev_info = dev_entry->value; 22418802d1fdSJeff Cody dev_entry = dev_entry->next; 22428802d1fdSJeff Cody 22436a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2244ba0c86a3SWenchao Xia 22456a8f9661SEric Blake ops = &actions[dev_info->type]; 2246aa3fe714SMax Reitz assert(ops->instance_size > 0); 2247aa3fe714SMax Reitz 2248ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2249ba5d6ab6SStefan Hajnoczi state->ops = ops; 2250ba5d6ab6SStefan Hajnoczi state->action = dev_info; 225194d16a64SJohn Snow state->block_job_txn = block_job_txn; 225294d16a64SJohn Snow state->txn_props = props; 2253ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22548802d1fdSJeff Cody 2255ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 225684d18f06SMarkus Armbruster if (local_err) { 22579b9877eeSWenchao Xia error_propagate(errp, local_err); 22589b9877eeSWenchao Xia goto delete_and_fail; 22599b9877eeSWenchao Xia } 226052e7c241SPaolo Bonzini } 22618802d1fdSJeff Cody 2262ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2263f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2264ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22658802d1fdSJeff Cody } 2266f9ea81e8SStefan Hajnoczi } 22678802d1fdSJeff Cody 22688802d1fdSJeff Cody /* success */ 22698802d1fdSJeff Cody goto exit; 22708802d1fdSJeff Cody 22718802d1fdSJeff Cody delete_and_fail: 2272b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2273ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2274ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2275ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2276ba0c86a3SWenchao Xia } 22778802d1fdSJeff Cody } 22788802d1fdSJeff Cody exit: 2279ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2280ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2281ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2282ba0c86a3SWenchao Xia } 2283ba5d6ab6SStefan Hajnoczi g_free(state); 22848802d1fdSJeff Cody } 228594d16a64SJohn Snow if (!has_props) { 228694d16a64SJohn Snow qapi_free_TransactionProperties(props); 228794d16a64SJohn Snow } 22887eaa8fb5SKevin Wolf job_txn_unref(block_job_txn); 22898802d1fdSJeff Cody } 22908802d1fdSJeff Cody 2291fbe2d816SKevin Wolf void qmp_eject(bool has_device, const char *device, 2292fbe2d816SKevin Wolf bool has_id, const char *id, 2293fbe2d816SKevin Wolf bool has_force, bool force, Error **errp) 2294666daa68SMarkus Armbruster { 229538f54bd1SMax Reitz Error *local_err = NULL; 22963a3086b7SJohn Snow int rc; 2297666daa68SMarkus Armbruster 22983a3086b7SJohn Snow if (!has_force) { 22993a3086b7SJohn Snow force = false; 23003a3086b7SJohn Snow } 23013a3086b7SJohn Snow 2302fbe2d816SKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2303fbe2d816SKevin Wolf has_id ? id : NULL, 2304fbe2d816SKevin Wolf force, &local_err); 2305bf18bee5SColin Lord if (rc && rc != -ENOSYS) { 230638f54bd1SMax Reitz error_propagate(errp, local_err); 2307c245b6a3SLuiz Capitulino return; 2308666daa68SMarkus Armbruster } 2309bf18bee5SColin Lord error_free(local_err); 23103a3086b7SJohn Snow 231182fcf66eSMax Reitz blockdev_remove_medium(has_device, device, has_id, id, errp); 2312666daa68SMarkus Armbruster } 2313666daa68SMarkus Armbruster 231412d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 231512d3ba82SBenoît Canet bool has_node_name, const char *node_name, 231612d3ba82SBenoît Canet const char *password, Error **errp) 2317666daa68SMarkus Armbruster { 2318c01c214bSDaniel P. Berrange error_setg(errp, 2319c01c214bSDaniel P. Berrange "Setting block passwords directly is no longer supported"); 2320e3442099SStefan Hajnoczi } 2321e3442099SStefan Hajnoczi 2322bf18bee5SColin Lord /* 2323bf18bee5SColin Lord * Attempt to open the tray of @device. 2324bf18bee5SColin Lord * If @force, ignore its tray lock. 2325bf18bee5SColin Lord * Else, if the tray is locked, don't open it, but ask the guest to open it. 2326bf18bee5SColin Lord * On error, store an error through @errp and return -errno. 2327bf18bee5SColin Lord * If @device does not exist, return -ENODEV. 2328bf18bee5SColin Lord * If it has no removable media, return -ENOTSUP. 2329bf18bee5SColin Lord * If it has no tray, return -ENOSYS. 2330bf18bee5SColin Lord * If the guest was asked to open the tray, return -EINPROGRESS. 2331bf18bee5SColin Lord * Else, return 0. 23323a3086b7SJohn Snow */ 2333b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id, 2334b33945cfSKevin Wolf bool force, Error **errp) 23357d8a9f71SMax Reitz { 23367d8a9f71SMax Reitz BlockBackend *blk; 2337b33945cfSKevin Wolf const char *device = qdev_id ?: blk_name; 23387d8a9f71SMax Reitz bool locked; 23397d8a9f71SMax Reitz 2340b33945cfSKevin Wolf blk = qmp_get_blk(blk_name, qdev_id, errp); 23417d8a9f71SMax Reitz if (!blk) { 23423a3086b7SJohn Snow return -ENODEV; 23437d8a9f71SMax Reitz } 23447d8a9f71SMax Reitz 23457d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23467d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23473a3086b7SJohn Snow return -ENOTSUP; 23487d8a9f71SMax Reitz } 23497d8a9f71SMax Reitz 235012c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 2351bf18bee5SColin Lord error_setg(errp, "Device '%s' does not have a tray", device); 2352bf18bee5SColin Lord return -ENOSYS; 235312c7ec87SMax Reitz } 235412c7ec87SMax Reitz 23557d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23563a3086b7SJohn Snow return 0; 23577d8a9f71SMax Reitz } 23587d8a9f71SMax Reitz 23597d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23607d8a9f71SMax Reitz if (locked) { 23617d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23627d8a9f71SMax Reitz } 23637d8a9f71SMax Reitz 23647d8a9f71SMax Reitz if (!locked || force) { 236539829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 23667d8a9f71SMax Reitz } 23673a3086b7SJohn Snow 23683a3086b7SJohn Snow if (locked && !force) { 2369bf18bee5SColin Lord error_setg(errp, "Device '%s' is locked and force was not specified, " 2370bf18bee5SColin Lord "wait for tray to open and try again", device); 2371bf18bee5SColin Lord return -EINPROGRESS; 23723a3086b7SJohn Snow } 23733a3086b7SJohn Snow 23743a3086b7SJohn Snow return 0; 23753a3086b7SJohn Snow } 23763a3086b7SJohn Snow 2377b33945cfSKevin Wolf void qmp_blockdev_open_tray(bool has_device, const char *device, 2378b33945cfSKevin Wolf bool has_id, const char *id, 2379b33945cfSKevin Wolf bool has_force, bool force, 23803a3086b7SJohn Snow Error **errp) 23813a3086b7SJohn Snow { 2382bf18bee5SColin Lord Error *local_err = NULL; 2383bf18bee5SColin Lord int rc; 2384bf18bee5SColin Lord 23853a3086b7SJohn Snow if (!has_force) { 23863a3086b7SJohn Snow force = false; 23873a3086b7SJohn Snow } 2388b33945cfSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2389b33945cfSKevin Wolf has_id ? id : NULL, 2390b33945cfSKevin Wolf force, &local_err); 2391bf18bee5SColin Lord if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { 2392bf18bee5SColin Lord error_propagate(errp, local_err); 2393bf18bee5SColin Lord return; 2394bf18bee5SColin Lord } 2395bf18bee5SColin Lord error_free(local_err); 23967d8a9f71SMax Reitz } 23977d8a9f71SMax Reitz 2398b33945cfSKevin Wolf void qmp_blockdev_close_tray(bool has_device, const char *device, 2399b33945cfSKevin Wolf bool has_id, const char *id, 2400b33945cfSKevin Wolf Error **errp) 2401abaaf59dSMax Reitz { 2402abaaf59dSMax Reitz BlockBackend *blk; 240339829a01SKevin Wolf Error *local_err = NULL; 2404abaaf59dSMax Reitz 2405b33945cfSKevin Wolf device = has_device ? device : NULL; 2406b33945cfSKevin Wolf id = has_id ? id : NULL; 2407b33945cfSKevin Wolf 2408b33945cfSKevin Wolf blk = qmp_get_blk(device, id, errp); 2409abaaf59dSMax Reitz if (!blk) { 2410abaaf59dSMax Reitz return; 2411abaaf59dSMax Reitz } 2412abaaf59dSMax Reitz 2413abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2414b33945cfSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 2415abaaf59dSMax Reitz return; 2416abaaf59dSMax Reitz } 2417abaaf59dSMax Reitz 241812c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 241912c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 242012c7ec87SMax Reitz return; 242112c7ec87SMax Reitz } 242212c7ec87SMax Reitz 2423abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2424abaaf59dSMax Reitz return; 2425abaaf59dSMax Reitz } 2426abaaf59dSMax Reitz 242739829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 242839829a01SKevin Wolf if (local_err) { 242939829a01SKevin Wolf error_propagate(errp, local_err); 243039829a01SKevin Wolf return; 243139829a01SKevin Wolf } 2432abaaf59dSMax Reitz } 2433abaaf59dSMax Reitz 243482fcf66eSMax Reitz static void blockdev_remove_medium(bool has_device, const char *device, 243500949babSKevin Wolf bool has_id, const char *id, Error **errp) 24362814f672SMax Reitz { 24372814f672SMax Reitz BlockBackend *blk; 24382814f672SMax Reitz BlockDriverState *bs; 24392814f672SMax Reitz AioContext *aio_context; 244000949babSKevin Wolf bool has_attached_device; 24412814f672SMax Reitz 244200949babSKevin Wolf device = has_device ? device : NULL; 244300949babSKevin Wolf id = has_id ? id : NULL; 244400949babSKevin Wolf 244500949babSKevin Wolf blk = qmp_get_blk(device, id, errp); 24462814f672SMax Reitz if (!blk) { 24472814f672SMax Reitz return; 24482814f672SMax Reitz } 24492814f672SMax Reitz 24502814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 245100949babSKevin Wolf has_attached_device = blk_get_attached_dev(blk); 24522814f672SMax Reitz 245300949babSKevin Wolf if (has_attached_device && !blk_dev_has_removable_media(blk)) { 245400949babSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 24552814f672SMax Reitz return; 24562814f672SMax Reitz } 24572814f672SMax Reitz 245800949babSKevin Wolf if (has_attached_device && blk_dev_has_tray(blk) && 245900949babSKevin Wolf !blk_dev_is_tray_open(blk)) 246000949babSKevin Wolf { 246100949babSKevin Wolf error_setg(errp, "Tray of device '%s' is not open", device ?: id); 24622814f672SMax Reitz return; 24632814f672SMax Reitz } 24642814f672SMax Reitz 24652814f672SMax Reitz bs = blk_bs(blk); 24662814f672SMax Reitz if (!bs) { 24672814f672SMax Reitz return; 24682814f672SMax Reitz } 24692814f672SMax Reitz 24702814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 24712814f672SMax Reitz aio_context_acquire(aio_context); 24722814f672SMax Reitz 24732814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 24742814f672SMax Reitz goto out; 24752814f672SMax Reitz } 24762814f672SMax Reitz 24772814f672SMax Reitz blk_remove_bs(blk); 24782814f672SMax Reitz 247912c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 248012c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 248112c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 248212c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 248312c7ec87SMax Reitz * value passed here (i.e. false). */ 248439829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 248512c7ec87SMax Reitz } 248612c7ec87SMax Reitz 24872814f672SMax Reitz out: 24882814f672SMax Reitz aio_context_release(aio_context); 24892814f672SMax Reitz } 24902814f672SMax Reitz 249134ce1111SMax Reitz void qmp_blockdev_remove_medium(const char *id, Error **errp) 249282fcf66eSMax Reitz { 249382fcf66eSMax Reitz blockdev_remove_medium(false, NULL, true, id, errp); 249482fcf66eSMax Reitz } 249582fcf66eSMax Reitz 2496716df217SKevin Wolf static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, 2497d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2498d1299882SMax Reitz { 249939829a01SKevin Wolf Error *local_err = NULL; 2500d1299882SMax Reitz bool has_device; 2501d7086422SKevin Wolf int ret; 2502d1299882SMax Reitz 2503d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2504d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2505d1299882SMax Reitz 2506d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2507716df217SKevin Wolf error_setg(errp, "Device is not removable"); 2508d1299882SMax Reitz return; 2509d1299882SMax Reitz } 2510d1299882SMax Reitz 251112c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2512716df217SKevin Wolf error_setg(errp, "Tray of the device is not open"); 2513d1299882SMax Reitz return; 2514d1299882SMax Reitz } 2515d1299882SMax Reitz 2516d1299882SMax Reitz if (blk_bs(blk)) { 2517716df217SKevin Wolf error_setg(errp, "There already is a medium in the device"); 2518d1299882SMax Reitz return; 2519d1299882SMax Reitz } 2520d1299882SMax Reitz 2521d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 2522d7086422SKevin Wolf if (ret < 0) { 2523d7086422SKevin Wolf return; 2524d7086422SKevin Wolf } 2525d1299882SMax Reitz 252612c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 252712c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 252812c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 252912c7ec87SMax Reitz * slot here. 253012c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 253112c7ec87SMax Reitz * value passed here (i.e. true). */ 253239829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 253339829a01SKevin Wolf if (local_err) { 253439829a01SKevin Wolf error_propagate(errp, local_err); 253539829a01SKevin Wolf blk_remove_bs(blk); 253639829a01SKevin Wolf return; 253739829a01SKevin Wolf } 253812c7ec87SMax Reitz } 2539d1299882SMax Reitz } 2540d1299882SMax Reitz 254182fcf66eSMax Reitz static void blockdev_insert_medium(bool has_device, const char *device, 2542716df217SKevin Wolf bool has_id, const char *id, 2543716df217SKevin Wolf const char *node_name, Error **errp) 2544d1299882SMax Reitz { 2545716df217SKevin Wolf BlockBackend *blk; 2546d1299882SMax Reitz BlockDriverState *bs; 2547d1299882SMax Reitz 2548716df217SKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 2549716df217SKevin Wolf has_id ? id : NULL, 2550716df217SKevin Wolf errp); 2551716df217SKevin Wolf if (!blk) { 2552716df217SKevin Wolf return; 2553716df217SKevin Wolf } 2554716df217SKevin Wolf 2555d1299882SMax Reitz bs = bdrv_find_node(node_name); 2556d1299882SMax Reitz if (!bs) { 2557d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2558d1299882SMax Reitz return; 2559d1299882SMax Reitz } 2560d1299882SMax Reitz 25611f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2562e467da7bSKevin Wolf error_setg(errp, "Node '%s' is already in use", node_name); 2563d1299882SMax Reitz return; 2564d1299882SMax Reitz } 2565d1299882SMax Reitz 2566716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, bs, errp); 2567d1299882SMax Reitz } 2568d1299882SMax Reitz 256934ce1111SMax Reitz void qmp_blockdev_insert_medium(const char *id, const char *node_name, 257082fcf66eSMax Reitz Error **errp) 257182fcf66eSMax Reitz { 257282fcf66eSMax Reitz blockdev_insert_medium(false, NULL, true, id, node_name, errp); 257382fcf66eSMax Reitz } 257482fcf66eSMax Reitz 257570e2cb3bSKevin Wolf void qmp_blockdev_change_medium(bool has_device, const char *device, 257670e2cb3bSKevin Wolf bool has_id, const char *id, 257770e2cb3bSKevin Wolf const char *filename, 257824fb4133SMax Reitz bool has_format, const char *format, 257939ff43d9SMax Reitz bool has_read_only, 258039ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 258124fb4133SMax Reitz Error **errp) 2582de2c6c05SMax Reitz { 2583de2c6c05SMax Reitz BlockBackend *blk; 2584de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 25855b363937SMax Reitz int bdrv_flags; 2586b85114f8SKevin Wolf bool detect_zeroes; 258738a53d50SColin Lord int rc; 2588de2c6c05SMax Reitz QDict *options = NULL; 2589de2c6c05SMax Reitz Error *err = NULL; 2590de2c6c05SMax Reitz 259170e2cb3bSKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 259270e2cb3bSKevin Wolf has_id ? id : NULL, 259370e2cb3bSKevin Wolf errp); 2594de2c6c05SMax Reitz if (!blk) { 2595de2c6c05SMax Reitz goto fail; 2596de2c6c05SMax Reitz } 2597de2c6c05SMax Reitz 2598de2c6c05SMax Reitz if (blk_bs(blk)) { 2599de2c6c05SMax Reitz blk_update_root_state(blk); 2600de2c6c05SMax Reitz } 2601de2c6c05SMax Reitz 2602de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2603156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2604156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2605de2c6c05SMax Reitz 260639ff43d9SMax Reitz if (!has_read_only) { 260739ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 260839ff43d9SMax Reitz } 260939ff43d9SMax Reitz 261039ff43d9SMax Reitz switch (read_only) { 261139ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 261239ff43d9SMax Reitz break; 261339ff43d9SMax Reitz 261439ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 261539ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 261639ff43d9SMax Reitz break; 261739ff43d9SMax Reitz 261839ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 261939ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 262039ff43d9SMax Reitz break; 262139ff43d9SMax Reitz 262239ff43d9SMax Reitz default: 262339ff43d9SMax Reitz abort(); 262439ff43d9SMax Reitz } 262539ff43d9SMax Reitz 2626de2c6c05SMax Reitz options = qdict_new(); 2627b85114f8SKevin Wolf detect_zeroes = blk_get_detect_zeroes_from_root_state(blk); 262846f5ac20SEric Blake qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off"); 2629b85114f8SKevin Wolf 2630b85114f8SKevin Wolf if (has_format) { 263146f5ac20SEric Blake qdict_put_str(options, "driver", format); 2632de2c6c05SMax Reitz } 2633de2c6c05SMax Reitz 26345b363937SMax Reitz medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 26355b363937SMax Reitz if (!medium_bs) { 2636de2c6c05SMax Reitz goto fail; 2637de2c6c05SMax Reitz } 2638de2c6c05SMax Reitz 263970e2cb3bSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 264070e2cb3bSKevin Wolf has_id ? id : NULL, 264170e2cb3bSKevin Wolf false, &err); 264238a53d50SColin Lord if (rc && rc != -ENOSYS) { 2643de2c6c05SMax Reitz error_propagate(errp, err); 2644de2c6c05SMax Reitz goto fail; 2645de2c6c05SMax Reitz } 264638a53d50SColin Lord error_free(err); 264738a53d50SColin Lord err = NULL; 2648de2c6c05SMax Reitz 264982fcf66eSMax Reitz blockdev_remove_medium(has_device, device, has_id, id, &err); 2650de2c6c05SMax Reitz if (err) { 2651de2c6c05SMax Reitz error_propagate(errp, err); 2652de2c6c05SMax Reitz goto fail; 2653de2c6c05SMax Reitz } 2654de2c6c05SMax Reitz 2655716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, medium_bs, &err); 2656de2c6c05SMax Reitz if (err) { 2657de2c6c05SMax Reitz error_propagate(errp, err); 2658de2c6c05SMax Reitz goto fail; 2659de2c6c05SMax Reitz } 2660de2c6c05SMax Reitz 266170e2cb3bSKevin Wolf qmp_blockdev_close_tray(has_device, device, has_id, id, errp); 2662de2c6c05SMax Reitz 2663de2c6c05SMax Reitz fail: 2664de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2665de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2666de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2667de2c6c05SMax Reitz bdrv_unref(medium_bs); 2668de2c6c05SMax Reitz } 2669de2c6c05SMax Reitz 2670727f005eSZhi Yong Wu /* throttling disk I/O limits */ 26714dc9397bSEric Blake void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) 2672727f005eSZhi Yong Wu { 2673cc0681c4SBenoît Canet ThrottleConfig cfg; 2674727f005eSZhi Yong Wu BlockDriverState *bs; 2675a0e8544cSFam Zheng BlockBackend *blk; 2676b15446fdSStefan Hajnoczi AioContext *aio_context; 2677727f005eSZhi Yong Wu 26787a9877a0SKevin Wolf blk = qmp_get_blk(arg->has_device ? arg->device : NULL, 26797a9877a0SKevin Wolf arg->has_id ? arg->id : NULL, 26807a9877a0SKevin Wolf errp); 2681a0e8544cSFam Zheng if (!blk) { 268280047da5SLuiz Capitulino return; 2683727f005eSZhi Yong Wu } 26845433c24fSMax Reitz 26855433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 26865433c24fSMax Reitz aio_context_acquire(aio_context); 26875433c24fSMax Reitz 2688a0e8544cSFam Zheng bs = blk_bs(blk); 26895433c24fSMax Reitz if (!bs) { 26907a9877a0SKevin Wolf error_setg(errp, "Device has no medium"); 26915433c24fSMax Reitz goto out; 26925433c24fSMax Reitz } 2693727f005eSZhi Yong Wu 26941588ab5dSAlberto Garcia throttle_config_init(&cfg); 26954dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; 26964dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; 26974dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; 2698727f005eSZhi Yong Wu 26994dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; 27004dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; 27014dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; 2702cc0681c4SBenoît Canet 27034dc9397bSEric Blake if (arg->has_bps_max) { 27044dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; 27053e9fab69SBenoît Canet } 27064dc9397bSEric Blake if (arg->has_bps_rd_max) { 27074dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; 27083e9fab69SBenoît Canet } 27094dc9397bSEric Blake if (arg->has_bps_wr_max) { 27104dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; 27113e9fab69SBenoît Canet } 27124dc9397bSEric Blake if (arg->has_iops_max) { 27134dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; 27143e9fab69SBenoît Canet } 27154dc9397bSEric Blake if (arg->has_iops_rd_max) { 27164dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; 27173e9fab69SBenoît Canet } 27184dc9397bSEric Blake if (arg->has_iops_wr_max) { 27194dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; 27203e9fab69SBenoît Canet } 2721cc0681c4SBenoît Canet 27224dc9397bSEric Blake if (arg->has_bps_max_length) { 27234dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; 2724dce13204SAlberto Garcia } 27254dc9397bSEric Blake if (arg->has_bps_rd_max_length) { 27264dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; 2727dce13204SAlberto Garcia } 27284dc9397bSEric Blake if (arg->has_bps_wr_max_length) { 27294dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; 2730dce13204SAlberto Garcia } 27314dc9397bSEric Blake if (arg->has_iops_max_length) { 27324dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; 2733dce13204SAlberto Garcia } 27344dc9397bSEric Blake if (arg->has_iops_rd_max_length) { 27354dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; 2736dce13204SAlberto Garcia } 27374dc9397bSEric Blake if (arg->has_iops_wr_max_length) { 27384dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; 2739dce13204SAlberto Garcia } 2740dce13204SAlberto Garcia 27414dc9397bSEric Blake if (arg->has_iops_size) { 27424dc9397bSEric Blake cfg.op_size = arg->iops_size; 27432024c1dfSBenoît Canet } 2744cc0681c4SBenoît Canet 2745d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 27465433c24fSMax Reitz goto out; 2747727f005eSZhi Yong Wu } 2748727f005eSZhi Yong Wu 274976f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 275076f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 275176f4afb4SAlberto Garcia * just update the throttling group. */ 2752022cdc9fSManos Pitsidianakis if (!blk_get_public(blk)->throttle_group_member.throttle_state) { 27534dc9397bSEric Blake blk_io_limits_enable(blk, 27547a9877a0SKevin Wolf arg->has_group ? arg->group : 27557a9877a0SKevin Wolf arg->has_device ? arg->device : 27567a9877a0SKevin Wolf arg->id); 27574dc9397bSEric Blake } else if (arg->has_group) { 27584dc9397bSEric Blake blk_io_limits_update_group(blk, arg->group); 2759727f005eSZhi Yong Wu } 276076f4afb4SAlberto Garcia /* Set the new throttling configuration */ 276197148076SKevin Wolf blk_set_io_limits(blk, &cfg); 2762022cdc9fSManos Pitsidianakis } else if (blk_get_public(blk)->throttle_group_member.throttle_state) { 276376f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 276497148076SKevin Wolf blk_io_limits_disable(blk); 2765727f005eSZhi Yong Wu } 2766b15446fdSStefan Hajnoczi 27675433c24fSMax Reitz out: 2768b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2769727f005eSZhi Yong Wu } 2770727f005eSZhi Yong Wu 2771341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2772341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2773fd5ae4ccSVladimir Sementsov-Ogievskiy bool has_persistent, bool persistent, 2774eb738bb5SVladimir Sementsov-Ogievskiy bool has_autoload, bool autoload, 2775a6e2ca5fSVladimir Sementsov-Ogievskiy bool has_disabled, bool disabled, 2776341ebc2fSJohn Snow Error **errp) 2777341ebc2fSJohn Snow { 2778341ebc2fSJohn Snow BlockDriverState *bs; 2779fd5ae4ccSVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 2780341ebc2fSJohn Snow 2781341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2782341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2783341ebc2fSJohn Snow return; 2784341ebc2fSJohn Snow } 2785341ebc2fSJohn Snow 2786341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2787341ebc2fSJohn Snow if (!bs) { 2788341ebc2fSJohn Snow return; 2789341ebc2fSJohn Snow } 2790341ebc2fSJohn Snow 2791341ebc2fSJohn Snow if (has_granularity) { 2792341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2793341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2794341ebc2fSJohn Snow "and at least 512"); 27952119882cSPaolo Bonzini return; 2796341ebc2fSJohn Snow } 2797341ebc2fSJohn Snow } else { 2798341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2799341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2800341ebc2fSJohn Snow } 2801341ebc2fSJohn Snow 2802fd5ae4ccSVladimir Sementsov-Ogievskiy if (!has_persistent) { 2803fd5ae4ccSVladimir Sementsov-Ogievskiy persistent = false; 2804fd5ae4ccSVladimir Sementsov-Ogievskiy } 2805eb738bb5SVladimir Sementsov-Ogievskiy 28063e99da5eSVladimir Sementsov-Ogievskiy if (has_autoload) { 28073e99da5eSVladimir Sementsov-Ogievskiy warn_report("Autoload option is deprecated and its value is ignored"); 2808eb738bb5SVladimir Sementsov-Ogievskiy } 2809fd5ae4ccSVladimir Sementsov-Ogievskiy 2810a6e2ca5fSVladimir Sementsov-Ogievskiy if (!has_disabled) { 2811a6e2ca5fSVladimir Sementsov-Ogievskiy disabled = false; 2812a6e2ca5fSVladimir Sementsov-Ogievskiy } 2813a6e2ca5fSVladimir Sementsov-Ogievskiy 2814fd5ae4ccSVladimir Sementsov-Ogievskiy if (persistent && 2815fd5ae4ccSVladimir Sementsov-Ogievskiy !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp)) 2816fd5ae4ccSVladimir Sementsov-Ogievskiy { 2817fd5ae4ccSVladimir Sementsov-Ogievskiy return; 2818fd5ae4ccSVladimir Sementsov-Ogievskiy } 2819fd5ae4ccSVladimir Sementsov-Ogievskiy 2820fd5ae4ccSVladimir Sementsov-Ogievskiy bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2821eb738bb5SVladimir Sementsov-Ogievskiy if (bitmap == NULL) { 2822eb738bb5SVladimir Sementsov-Ogievskiy return; 2823fd5ae4ccSVladimir Sementsov-Ogievskiy } 2824eb738bb5SVladimir Sementsov-Ogievskiy 2825a6e2ca5fSVladimir Sementsov-Ogievskiy if (disabled) { 2826a6e2ca5fSVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(bitmap); 2827a6e2ca5fSVladimir Sementsov-Ogievskiy } 2828a6e2ca5fSVladimir Sementsov-Ogievskiy 2829eb738bb5SVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_set_persistance(bitmap, persistent); 2830341ebc2fSJohn Snow } 2831341ebc2fSJohn Snow 2832341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2833341ebc2fSJohn Snow Error **errp) 2834341ebc2fSJohn Snow { 2835341ebc2fSJohn Snow BlockDriverState *bs; 2836341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 28375c36c1afSVladimir Sementsov-Ogievskiy Error *local_err = NULL; 2838341ebc2fSJohn Snow 28392119882cSPaolo Bonzini bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2840341ebc2fSJohn Snow if (!bitmap || !bs) { 2841341ebc2fSJohn Snow return; 2842341ebc2fSJohn Snow } 2843341ebc2fSJohn Snow 28449bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 28459bd2b08fSJohn Snow error_setg(errp, 28469bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 28479bd2b08fSJohn Snow name); 28482119882cSPaolo Bonzini return; 28494f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) { 28504f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 28514f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be removed", 28524f43e953SVladimir Sementsov-Ogievskiy name); 28534f43e953SVladimir Sementsov-Ogievskiy return; 28549bd2b08fSJohn Snow } 28555c36c1afSVladimir Sementsov-Ogievskiy 28565c36c1afSVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_get_persistance(bitmap)) { 28575c36c1afSVladimir Sementsov-Ogievskiy bdrv_remove_persistent_dirty_bitmap(bs, name, &local_err); 28585c36c1afSVladimir Sementsov-Ogievskiy if (local_err != NULL) { 28595c36c1afSVladimir Sementsov-Ogievskiy error_propagate(errp, local_err); 28605c36c1afSVladimir Sementsov-Ogievskiy return; 28615c36c1afSVladimir Sementsov-Ogievskiy } 28625c36c1afSVladimir Sementsov-Ogievskiy } 28635c36c1afSVladimir Sementsov-Ogievskiy 2864341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2865341ebc2fSJohn Snow } 2866341ebc2fSJohn Snow 2867e74e6b78SJohn Snow /** 2868e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2869e74e6b78SJohn Snow * immediately after a full backup operation. 2870e74e6b78SJohn Snow */ 2871e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2872e74e6b78SJohn Snow Error **errp) 2873e74e6b78SJohn Snow { 2874e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2875e74e6b78SJohn Snow BlockDriverState *bs; 2876e74e6b78SJohn Snow 28772119882cSPaolo Bonzini bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2878e74e6b78SJohn Snow if (!bitmap || !bs) { 2879e74e6b78SJohn Snow return; 2880e74e6b78SJohn Snow } 2881e74e6b78SJohn Snow 2882e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2883e74e6b78SJohn Snow error_setg(errp, 2884e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2885e74e6b78SJohn Snow name); 28862119882cSPaolo Bonzini return; 28874f43e953SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) { 28884f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 28894f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be modified", 28904f43e953SVladimir Sementsov-Ogievskiy name); 28914f43e953SVladimir Sementsov-Ogievskiy return; 2892e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2893e74e6b78SJohn Snow error_setg(errp, 2894e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2895e74e6b78SJohn Snow name); 28962119882cSPaolo Bonzini return; 2897d6883bc9SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(bitmap)) { 2898d6883bc9SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap '%s' is readonly and cannot be cleared", name); 2899d6883bc9SVladimir Sementsov-Ogievskiy return; 2900e74e6b78SJohn Snow } 2901e74e6b78SJohn Snow 2902df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2903e74e6b78SJohn Snow } 2904e74e6b78SJohn Snow 29055c5d2e50SVladimir Sementsov-Ogievskiy void qmp_x_block_dirty_bitmap_enable(const char *node, const char *name, 29065c5d2e50SVladimir Sementsov-Ogievskiy Error **errp) 29075c5d2e50SVladimir Sementsov-Ogievskiy { 29085c5d2e50SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 29095c5d2e50SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 29105c5d2e50SVladimir Sementsov-Ogievskiy 29115c5d2e50SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 29125c5d2e50SVladimir Sementsov-Ogievskiy if (!bitmap) { 29135c5d2e50SVladimir Sementsov-Ogievskiy return; 29145c5d2e50SVladimir Sementsov-Ogievskiy } 29155c5d2e50SVladimir Sementsov-Ogievskiy 29165c5d2e50SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_frozen(bitmap)) { 29175c5d2e50SVladimir Sementsov-Ogievskiy error_setg(errp, 29185c5d2e50SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently frozen and cannot be enabled", 29195c5d2e50SVladimir Sementsov-Ogievskiy name); 29205c5d2e50SVladimir Sementsov-Ogievskiy return; 29215c5d2e50SVladimir Sementsov-Ogievskiy } 29225c5d2e50SVladimir Sementsov-Ogievskiy 29235c5d2e50SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(bitmap); 29245c5d2e50SVladimir Sementsov-Ogievskiy } 29255c5d2e50SVladimir Sementsov-Ogievskiy 29265c5d2e50SVladimir Sementsov-Ogievskiy void qmp_x_block_dirty_bitmap_disable(const char *node, const char *name, 29275c5d2e50SVladimir Sementsov-Ogievskiy Error **errp) 29285c5d2e50SVladimir Sementsov-Ogievskiy { 29295c5d2e50SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 29305c5d2e50SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 29315c5d2e50SVladimir Sementsov-Ogievskiy 29325c5d2e50SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 29335c5d2e50SVladimir Sementsov-Ogievskiy if (!bitmap) { 29345c5d2e50SVladimir Sementsov-Ogievskiy return; 29355c5d2e50SVladimir Sementsov-Ogievskiy } 29365c5d2e50SVladimir Sementsov-Ogievskiy 29375c5d2e50SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_frozen(bitmap)) { 29385c5d2e50SVladimir Sementsov-Ogievskiy error_setg(errp, 29395c5d2e50SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently frozen and cannot be disabled", 29405c5d2e50SVladimir Sementsov-Ogievskiy name); 29415c5d2e50SVladimir Sementsov-Ogievskiy return; 29425c5d2e50SVladimir Sementsov-Ogievskiy } 29435c5d2e50SVladimir Sementsov-Ogievskiy 29445c5d2e50SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(bitmap); 29455c5d2e50SVladimir Sementsov-Ogievskiy } 29465c5d2e50SVladimir Sementsov-Ogievskiy 2947b598e531SVladimir Sementsov-Ogievskiy void qmp_x_block_dirty_bitmap_merge(const char *node, const char *dst_name, 2948b598e531SVladimir Sementsov-Ogievskiy const char *src_name, Error **errp) 2949b598e531SVladimir Sementsov-Ogievskiy { 2950b598e531SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 2951b598e531SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *dst, *src; 2952b598e531SVladimir Sementsov-Ogievskiy 2953b598e531SVladimir Sementsov-Ogievskiy dst = block_dirty_bitmap_lookup(node, dst_name, &bs, errp); 2954b598e531SVladimir Sementsov-Ogievskiy if (!dst) { 2955b598e531SVladimir Sementsov-Ogievskiy return; 2956b598e531SVladimir Sementsov-Ogievskiy } 2957b598e531SVladimir Sementsov-Ogievskiy 2958b598e531SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_frozen(dst)) { 2959b598e531SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap '%s' is frozen and cannot be modified", 2960b598e531SVladimir Sementsov-Ogievskiy dst_name); 2961b598e531SVladimir Sementsov-Ogievskiy return; 2962b598e531SVladimir Sementsov-Ogievskiy } else if (bdrv_dirty_bitmap_readonly(dst)) { 2963b598e531SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap '%s' is readonly and cannot be modified", 2964b598e531SVladimir Sementsov-Ogievskiy dst_name); 2965b598e531SVladimir Sementsov-Ogievskiy return; 2966b598e531SVladimir Sementsov-Ogievskiy } 2967b598e531SVladimir Sementsov-Ogievskiy 2968b598e531SVladimir Sementsov-Ogievskiy src = bdrv_find_dirty_bitmap(bs, src_name); 2969b598e531SVladimir Sementsov-Ogievskiy if (!src) { 2970b598e531SVladimir Sementsov-Ogievskiy error_setg(errp, "Dirty bitmap '%s' not found", src_name); 2971b598e531SVladimir Sementsov-Ogievskiy return; 2972b598e531SVladimir Sementsov-Ogievskiy } 2973b598e531SVladimir Sementsov-Ogievskiy 2974b598e531SVladimir Sementsov-Ogievskiy bdrv_merge_dirty_bitmap(dst, src, errp); 2975b598e531SVladimir Sementsov-Ogievskiy } 2976b598e531SVladimir Sementsov-Ogievskiy 2977a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, 2978a3b52535SVladimir Sementsov-Ogievskiy const char *name, 2979a3b52535SVladimir Sementsov-Ogievskiy Error **errp) 2980a3b52535SVladimir Sementsov-Ogievskiy { 2981a3b52535SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 2982a3b52535SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 2983a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *ret = NULL; 2984a3b52535SVladimir Sementsov-Ogievskiy char *sha256; 2985a3b52535SVladimir Sementsov-Ogievskiy 2986a3b52535SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2987a3b52535SVladimir Sementsov-Ogievskiy if (!bitmap || !bs) { 2988a3b52535SVladimir Sementsov-Ogievskiy return NULL; 2989a3b52535SVladimir Sementsov-Ogievskiy } 2990a3b52535SVladimir Sementsov-Ogievskiy 2991a3b52535SVladimir Sementsov-Ogievskiy sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp); 2992a3b52535SVladimir Sementsov-Ogievskiy if (sha256 == NULL) { 2993a3b52535SVladimir Sementsov-Ogievskiy return NULL; 2994a3b52535SVladimir Sementsov-Ogievskiy } 2995a3b52535SVladimir Sementsov-Ogievskiy 2996a3b52535SVladimir Sementsov-Ogievskiy ret = g_new(BlockDirtyBitmapSha256, 1); 2997a3b52535SVladimir Sementsov-Ogievskiy ret->sha256 = sha256; 2998a3b52535SVladimir Sementsov-Ogievskiy 2999a3b52535SVladimir Sementsov-Ogievskiy return ret; 3000a3b52535SVladimir Sementsov-Ogievskiy } 3001a3b52535SVladimir Sementsov-Ogievskiy 3002072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 30039063f814SRyan Harper { 30049063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 30057e7d56d9SMarkus Armbruster BlockBackend *blk; 30069063f814SRyan Harper BlockDriverState *bs; 30078ad4202bSStefan Hajnoczi AioContext *aio_context; 30083718d8abSFam Zheng Error *local_err = NULL; 30099063f814SRyan Harper 30102073d410SKevin Wolf bs = bdrv_find_node(id); 30112073d410SKevin Wolf if (bs) { 301279b7a77eSMarkus Armbruster qmp_blockdev_del(id, &local_err); 30132073d410SKevin Wolf if (local_err) { 30142073d410SKevin Wolf error_report_err(local_err); 30152073d410SKevin Wolf } 30162073d410SKevin Wolf return; 30172073d410SKevin Wolf } 30182073d410SKevin Wolf 30197e7d56d9SMarkus Armbruster blk = blk_by_name(id); 30207e7d56d9SMarkus Armbruster if (!blk) { 3021b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 3022072ebe6bSMarkus Armbruster return; 30239063f814SRyan Harper } 30248ad4202bSStefan Hajnoczi 302526f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 302648f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 302748f364ddSMarkus Armbruster " is not supported"); 3028072ebe6bSMarkus Armbruster return; 302948f364ddSMarkus Armbruster } 303048f364ddSMarkus Armbruster 30315433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 30328ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 30338ad4202bSStefan Hajnoczi 30345433c24fSMax Reitz bs = blk_bs(blk); 30355433c24fSMax Reitz if (bs) { 30363718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 3037565f65d2SMarkus Armbruster error_report_err(local_err); 30388ad4202bSStefan Hajnoczi aio_context_release(aio_context); 3039072ebe6bSMarkus Armbruster return; 30408591675fSMarcelo Tosatti } 30419063f814SRyan Harper 3042938abd43SMax Reitz blk_remove_bs(blk); 30435433c24fSMax Reitz } 30449063f814SRyan Harper 30457c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 3046efaa7c4eSMax Reitz monitor_remove_blk(blk); 3047efaa7c4eSMax Reitz 30487c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 30497c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 3050d22b2f41SRyan Harper */ 3051a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 3052293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 3053373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 3054293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 3055d22b2f41SRyan Harper } else { 3056b9fe8a7aSMarkus Armbruster blk_unref(blk); 3057d22b2f41SRyan Harper } 30589063f814SRyan Harper 30598ad4202bSStefan Hajnoczi aio_context_release(aio_context); 30609063f814SRyan Harper } 30616d4a2b3aSChristoph Hellwig 30623b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 30633b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 30643b1dbd11SBenoît Canet int64_t size, Error **errp) 30656d4a2b3aSChristoph Hellwig { 30663b1dbd11SBenoît Canet Error *local_err = NULL; 30677dad9ee6SKevin Wolf BlockBackend *blk = NULL; 30686d4a2b3aSChristoph Hellwig BlockDriverState *bs; 3069927e0e76SStefan Hajnoczi AioContext *aio_context; 30708732901eSKevin Wolf int ret; 30716d4a2b3aSChristoph Hellwig 30723b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 30733b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 30743b1dbd11SBenoît Canet &local_err); 307584d18f06SMarkus Armbruster if (local_err) { 30763b1dbd11SBenoît Canet error_propagate(errp, local_err); 30773b1dbd11SBenoît Canet return; 30783b1dbd11SBenoît Canet } 30793b1dbd11SBenoît Canet 3080927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 3081927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 3082927e0e76SStefan Hajnoczi 30833b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 3084c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 3085927e0e76SStefan Hajnoczi goto out; 30866d4a2b3aSChristoph Hellwig } 30876d4a2b3aSChristoph Hellwig 30886d4a2b3aSChristoph Hellwig if (size < 0) { 3089c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 3090927e0e76SStefan Hajnoczi goto out; 30916d4a2b3aSChristoph Hellwig } 30926d4a2b3aSChristoph Hellwig 30939c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 3094c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 3095927e0e76SStefan Hajnoczi goto out; 30969c75e168SJeff Cody } 30979c75e168SJeff Cody 30986d0eb64dSKevin Wolf blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL); 3099d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 3100d7086422SKevin Wolf if (ret < 0) { 3101d7086422SKevin Wolf goto out; 3102d7086422SKevin Wolf } 31037dad9ee6SKevin Wolf 3104698bdfa0SJohn Snow bdrv_drained_begin(bs); 31053a691c50SMax Reitz ret = blk_truncate(blk, size, PREALLOC_MODE_OFF, errp); 3106698bdfa0SJohn Snow bdrv_drained_end(bs); 3107927e0e76SStefan Hajnoczi 3108927e0e76SStefan Hajnoczi out: 31097dad9ee6SKevin Wolf blk_unref(blk); 3110927e0e76SStefan Hajnoczi aio_context_release(aio_context); 31116d4a2b3aSChristoph Hellwig } 311212bd451fSStefan Hajnoczi 31132323322eSAlberto Garcia void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, 311413d8cc51SJeff Cody bool has_base, const char *base, 3115312fe09cSAlberto Garcia bool has_base_node, const char *base_node, 311613d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 311713d8cc51SJeff Cody bool has_speed, int64_t speed, 31181d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 31191d809098SPaolo Bonzini Error **errp) 312012bd451fSStefan Hajnoczi { 3121554b6147SAlberto Garcia BlockDriverState *bs, *iter; 3122c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 3123f3e69bebSStefan Hajnoczi AioContext *aio_context; 3124fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 312513d8cc51SJeff Cody const char *base_name = NULL; 3126cf6320dfSJohn Snow int job_flags = JOB_DEFAULT; 312712bd451fSStefan Hajnoczi 31281d809098SPaolo Bonzini if (!has_on_error) { 31291d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 31301d809098SPaolo Bonzini } 31311d809098SPaolo Bonzini 3132554b6147SAlberto Garcia bs = bdrv_lookup_bs(device, device, errp); 3133b6c1bae5SKevin Wolf if (!bs) { 313412bd451fSStefan Hajnoczi return; 313512bd451fSStefan Hajnoczi } 313612bd451fSStefan Hajnoczi 3137b6c1bae5SKevin Wolf aio_context = bdrv_get_aio_context(bs); 3138f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 3139f3e69bebSStefan Hajnoczi 3140312fe09cSAlberto Garcia if (has_base && has_base_node) { 3141312fe09cSAlberto Garcia error_setg(errp, "'base' and 'base-node' cannot be specified " 3142312fe09cSAlberto Garcia "at the same time"); 3143312fe09cSAlberto Garcia goto out; 3144312fe09cSAlberto Garcia } 3145312fe09cSAlberto Garcia 314613d8cc51SJeff Cody if (has_base) { 3147c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3148c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3149c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3150f3e69bebSStefan Hajnoczi goto out; 315112bd451fSStefan Hajnoczi } 3152f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 315313d8cc51SJeff Cody base_name = base; 3154c8c3080fSMarcelo Tosatti } 315512bd451fSStefan Hajnoczi 3156312fe09cSAlberto Garcia if (has_base_node) { 3157312fe09cSAlberto Garcia base_bs = bdrv_lookup_bs(NULL, base_node, errp); 3158312fe09cSAlberto Garcia if (!base_bs) { 3159312fe09cSAlberto Garcia goto out; 3160312fe09cSAlberto Garcia } 3161312fe09cSAlberto Garcia if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { 3162312fe09cSAlberto Garcia error_setg(errp, "Node '%s' is not a backing image of '%s'", 3163312fe09cSAlberto Garcia base_node, device); 3164312fe09cSAlberto Garcia goto out; 3165312fe09cSAlberto Garcia } 3166312fe09cSAlberto Garcia assert(bdrv_get_aio_context(base_bs) == aio_context); 3167312fe09cSAlberto Garcia base_name = base_bs->filename; 3168312fe09cSAlberto Garcia } 3169312fe09cSAlberto Garcia 3170554b6147SAlberto Garcia /* Check for op blockers in the whole chain between bs and base */ 3171554b6147SAlberto Garcia for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) { 3172554b6147SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { 3173554b6147SAlberto Garcia goto out; 3174554b6147SAlberto Garcia } 3175554b6147SAlberto Garcia } 3176554b6147SAlberto Garcia 317713d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 317813d8cc51SJeff Cody * file, and specifying one is therefore an error */ 317913d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 318013d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 318113d8cc51SJeff Cody "entire chain"); 3182f3e69bebSStefan Hajnoczi goto out; 318313d8cc51SJeff Cody } 318413d8cc51SJeff Cody 318513d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 318613d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 318713d8cc51SJeff Cody 31882323322eSAlberto Garcia stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, 3189cf6320dfSJohn Snow job_flags, has_speed ? speed : 0, on_error, &local_err); 319084d18f06SMarkus Armbruster if (local_err) { 3191fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3192f3e69bebSStefan Hajnoczi goto out; 319312bd451fSStefan Hajnoczi } 319412bd451fSStefan Hajnoczi 319512bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3196f3e69bebSStefan Hajnoczi 3197f3e69bebSStefan Hajnoczi out: 3198f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 319912bd451fSStefan Hajnoczi } 32002d47c6e9SStefan Hajnoczi 3201fd62c609SAlberto Garcia void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, 32027676e2c5SJeff Cody bool has_base, const char *base, 32037676e2c5SJeff Cody bool has_top, const char *top, 320454e26900SJeff Cody bool has_backing_file, const char *backing_file, 3205ed61fc10SJeff Cody bool has_speed, int64_t speed, 32060db832f4SKevin Wolf bool has_filter_node_name, const char *filter_node_name, 320796fbf534SJohn Snow bool has_auto_finalize, bool auto_finalize, 320896fbf534SJohn Snow bool has_auto_dismiss, bool auto_dismiss, 3209ed61fc10SJeff Cody Error **errp) 3210ed61fc10SJeff Cody { 3211ed61fc10SJeff Cody BlockDriverState *bs; 3212058223a6SAlberto Garcia BlockDriverState *iter; 3213ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 32149e85cd5cSStefan Hajnoczi AioContext *aio_context; 3215ed61fc10SJeff Cody Error *local_err = NULL; 3216ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3217ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3218ed61fc10SJeff Cody */ 321992aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 32205360782dSJohn Snow int job_flags = JOB_DEFAULT; 3221ed61fc10SJeff Cody 322254504663SMax Reitz if (!has_speed) { 322354504663SMax Reitz speed = 0; 322454504663SMax Reitz } 32250db832f4SKevin Wolf if (!has_filter_node_name) { 32260db832f4SKevin Wolf filter_node_name = NULL; 32270db832f4SKevin Wolf } 322896fbf534SJohn Snow if (has_auto_finalize && !auto_finalize) { 322996fbf534SJohn Snow job_flags |= JOB_MANUAL_FINALIZE; 323096fbf534SJohn Snow } 323196fbf534SJohn Snow if (has_auto_dismiss && !auto_dismiss) { 323296fbf534SJohn Snow job_flags |= JOB_MANUAL_DISMISS; 323396fbf534SJohn Snow } 323454504663SMax Reitz 32357676e2c5SJeff Cody /* Important Note: 32367676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 32377676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 32387676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 32397676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 32401d13b167SKevin Wolf bs = qmp_get_root_bs(device, &local_err); 32411d13b167SKevin Wolf if (!bs) { 32421d13b167SKevin Wolf bs = bdrv_lookup_bs(device, device, NULL); 32431d13b167SKevin Wolf if (!bs) { 32441d13b167SKevin Wolf error_free(local_err); 324575158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 324675158ebbSMarkus Armbruster "Device '%s' not found", device); 32471d13b167SKevin Wolf } else { 32481d13b167SKevin Wolf error_propagate(errp, local_err); 32491d13b167SKevin Wolf } 3250ed61fc10SJeff Cody return; 3251ed61fc10SJeff Cody } 3252ed61fc10SJeff Cody 32531d13b167SKevin Wolf aio_context = bdrv_get_aio_context(bs); 32549e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 32559e85cd5cSStefan Hajnoczi 3256bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 32579e85cd5cSStefan Hajnoczi goto out; 3258628ff683SFam Zheng } 3259628ff683SFam Zheng 3260ed61fc10SJeff Cody /* default top_bs is the active layer */ 3261ed61fc10SJeff Cody top_bs = bs; 3262ed61fc10SJeff Cody 32637676e2c5SJeff Cody if (has_top && top) { 3264ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3265ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3266ed61fc10SJeff Cody } 3267ed61fc10SJeff Cody } 3268ed61fc10SJeff Cody 3269ed61fc10SJeff Cody if (top_bs == NULL) { 3270ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 32719e85cd5cSStefan Hajnoczi goto out; 3272ed61fc10SJeff Cody } 3273ed61fc10SJeff Cody 32749e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 32759e85cd5cSStefan Hajnoczi 3276d5208c45SJeff Cody if (has_base && base) { 3277d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3278d5208c45SJeff Cody } else { 3279d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3280d5208c45SJeff Cody } 3281d5208c45SJeff Cody 3282d5208c45SJeff Cody if (base_bs == NULL) { 3283c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 32849e85cd5cSStefan Hajnoczi goto out; 3285d5208c45SJeff Cody } 3286d5208c45SJeff Cody 32879e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 32889e85cd5cSStefan Hajnoczi 3289058223a6SAlberto Garcia for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) { 3290058223a6SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3291bb00021dSFam Zheng goto out; 3292bb00021dSFam Zheng } 3293058223a6SAlberto Garcia } 3294bb00021dSFam Zheng 32957676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 32967676e2c5SJeff Cody if (top_bs == base_bs) { 32977676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 32989e85cd5cSStefan Hajnoczi goto out; 32997676e2c5SJeff Cody } 33007676e2c5SJeff Cody 330120a63d2cSFam Zheng if (top_bs == bs) { 330254e26900SJeff Cody if (has_backing_file) { 330354e26900SJeff Cody error_setg(errp, "'backing-file' specified," 330454e26900SJeff Cody " but 'top' is the active layer"); 33059e85cd5cSStefan Hajnoczi goto out; 330654e26900SJeff Cody } 330747970dfbSJohn Snow commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, 33085360782dSJohn Snow job_flags, speed, on_error, 330978bbd910SFam Zheng filter_node_name, NULL, NULL, false, &local_err); 331020a63d2cSFam Zheng } else { 3311058223a6SAlberto Garcia BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); 3312058223a6SAlberto Garcia if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3313058223a6SAlberto Garcia goto out; 3314058223a6SAlberto Garcia } 33155360782dSJohn Snow commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags, 33165360782dSJohn Snow speed, on_error, has_backing_file ? backing_file : NULL, 33170db832f4SKevin Wolf filter_node_name, &local_err); 331820a63d2cSFam Zheng } 3319ed61fc10SJeff Cody if (local_err != NULL) { 3320ed61fc10SJeff Cody error_propagate(errp, local_err); 33219e85cd5cSStefan Hajnoczi goto out; 3322ed61fc10SJeff Cody } 33239e85cd5cSStefan Hajnoczi 33249e85cd5cSStefan Hajnoczi out: 33259e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3326ed61fc10SJeff Cody } 3327ed61fc10SJeff Cody 332862c9e416SKevin Wolf static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn, 3329111049a4SJohn Snow Error **errp) 333099a9addfSStefan Hajnoczi { 333199a9addfSStefan Hajnoczi BlockDriverState *bs; 333299a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3333fc5d3f84SIan Main BlockDriverState *source = NULL; 3334111049a4SJohn Snow BlockJob *job = NULL; 3335d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3336761731b1SStefan Hajnoczi AioContext *aio_context; 3337e6641719SMax Reitz QDict *options = NULL; 333899a9addfSStefan Hajnoczi Error *local_err = NULL; 3339bb02b65cSKevin Wolf int flags, job_flags = JOB_DEFAULT; 334099a9addfSStefan Hajnoczi int64_t size; 3341fc0932fdSFam Zheng bool set_backing_hd = false; 334299a9addfSStefan Hajnoczi 334381206a89SPavel Butsykin if (!backup->has_speed) { 334481206a89SPavel Butsykin backup->speed = 0; 334599a9addfSStefan Hajnoczi } 334681206a89SPavel Butsykin if (!backup->has_on_source_error) { 334781206a89SPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 334899a9addfSStefan Hajnoczi } 334981206a89SPavel Butsykin if (!backup->has_on_target_error) { 335081206a89SPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 335199a9addfSStefan Hajnoczi } 335281206a89SPavel Butsykin if (!backup->has_mode) { 335381206a89SPavel Butsykin backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 335481206a89SPavel Butsykin } 335581206a89SPavel Butsykin if (!backup->has_job_id) { 335681206a89SPavel Butsykin backup->job_id = NULL; 335799a9addfSStefan Hajnoczi } 3358b40dacdcSJohn Snow if (!backup->has_auto_finalize) { 3359b40dacdcSJohn Snow backup->auto_finalize = true; 3360b40dacdcSJohn Snow } 3361b40dacdcSJohn Snow if (!backup->has_auto_dismiss) { 3362b40dacdcSJohn Snow backup->auto_dismiss = true; 3363b40dacdcSJohn Snow } 336413b9414bSPavel Butsykin if (!backup->has_compress) { 336513b9414bSPavel Butsykin backup->compress = false; 336613b9414bSPavel Butsykin } 336799a9addfSStefan Hajnoczi 336881206a89SPavel Butsykin bs = qmp_get_root_bs(backup->device, errp); 3369b7e4fa22SKevin Wolf if (!bs) { 3370111049a4SJohn Snow return NULL; 337199a9addfSStefan Hajnoczi } 337299a9addfSStefan Hajnoczi 3373b7e4fa22SKevin Wolf aio_context = bdrv_get_aio_context(bs); 3374761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3375761731b1SStefan Hajnoczi 337681206a89SPavel Butsykin if (!backup->has_format) { 337781206a89SPavel Butsykin backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? 337881206a89SPavel Butsykin NULL : (char*) bs->drv->format_name; 337999a9addfSStefan Hajnoczi } 338099a9addfSStefan Hajnoczi 3381c29c1dd3SFam Zheng /* Early check to avoid creating target */ 33823718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3383761731b1SStefan Hajnoczi goto out; 338499a9addfSStefan Hajnoczi } 338599a9addfSStefan Hajnoczi 338661de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 338799a9addfSStefan Hajnoczi 3388fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3389fc5d3f84SIan Main * on top of. */ 339081206a89SPavel Butsykin if (backup->sync == MIRROR_SYNC_MODE_TOP) { 3391760e0063SKevin Wolf source = backing_bs(bs); 3392fc5d3f84SIan Main if (!source) { 339381206a89SPavel Butsykin backup->sync = MIRROR_SYNC_MODE_FULL; 3394fc5d3f84SIan Main } 3395fc5d3f84SIan Main } 339681206a89SPavel Butsykin if (backup->sync == MIRROR_SYNC_MODE_NONE) { 3397fc5d3f84SIan Main source = bs; 3398fc0932fdSFam Zheng flags |= BDRV_O_NO_BACKING; 3399fc0932fdSFam Zheng set_backing_hd = true; 3400fc5d3f84SIan Main } 3401fc5d3f84SIan Main 340299a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 340399a9addfSStefan Hajnoczi if (size < 0) { 340499a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3405761731b1SStefan Hajnoczi goto out; 340699a9addfSStefan Hajnoczi } 340799a9addfSStefan Hajnoczi 340881206a89SPavel Butsykin if (backup->mode != NEW_IMAGE_MODE_EXISTING) { 340981206a89SPavel Butsykin assert(backup->format); 3410fc5d3f84SIan Main if (source) { 341181206a89SPavel Butsykin bdrv_img_create(backup->target, backup->format, source->filename, 3412fc5d3f84SIan Main source->drv->format_name, NULL, 34139217283dSFam Zheng size, flags, false, &local_err); 3414fc5d3f84SIan Main } else { 341581206a89SPavel Butsykin bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, 34169217283dSFam Zheng size, flags, false, &local_err); 3417fc5d3f84SIan Main } 341899a9addfSStefan Hajnoczi } 341999a9addfSStefan Hajnoczi 342084d18f06SMarkus Armbruster if (local_err) { 342199a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3422761731b1SStefan Hajnoczi goto out; 342399a9addfSStefan Hajnoczi } 342499a9addfSStefan Hajnoczi 342581206a89SPavel Butsykin if (backup->format) { 3426fc0932fdSFam Zheng if (!options) { 3427e6641719SMax Reitz options = qdict_new(); 3428fc0932fdSFam Zheng } 342946f5ac20SEric Blake qdict_put_str(options, "driver", backup->format); 3430e6641719SMax Reitz } 3431e6641719SMax Reitz 343281206a89SPavel Butsykin target_bs = bdrv_open(backup->target, NULL, options, flags, errp); 34335b363937SMax Reitz if (!target_bs) { 3434761731b1SStefan Hajnoczi goto out; 343599a9addfSStefan Hajnoczi } 343699a9addfSStefan Hajnoczi 3437761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3438761731b1SStefan Hajnoczi 3439fc0932fdSFam Zheng if (set_backing_hd) { 3440fc0932fdSFam Zheng bdrv_set_backing_hd(target_bs, source, &local_err); 3441fc0932fdSFam Zheng if (local_err) { 3442fc0932fdSFam Zheng bdrv_unref(target_bs); 3443fc0932fdSFam Zheng goto out; 3444fc0932fdSFam Zheng } 3445fc0932fdSFam Zheng } 3446fc0932fdSFam Zheng 344781206a89SPavel Butsykin if (backup->has_bitmap) { 344881206a89SPavel Butsykin bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); 3449d58d8453SJohn Snow if (!bmap) { 345081206a89SPavel Butsykin error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); 34510702d3d8SMax Reitz bdrv_unref(target_bs); 3452d58d8453SJohn Snow goto out; 3453d58d8453SJohn Snow } 34544f43e953SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_qmp_locked(bmap)) { 34554f43e953SVladimir Sementsov-Ogievskiy error_setg(errp, 34564f43e953SVladimir Sementsov-Ogievskiy "Bitmap '%s' is currently locked and cannot be used for " 34574f43e953SVladimir Sementsov-Ogievskiy "backup", backup->bitmap); 34584f43e953SVladimir Sementsov-Ogievskiy goto out; 34594f43e953SVladimir Sementsov-Ogievskiy } 3460d58d8453SJohn Snow } 3461b40dacdcSJohn Snow if (!backup->auto_finalize) { 3462bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_FINALIZE; 3463b40dacdcSJohn Snow } 3464b40dacdcSJohn Snow if (!backup->auto_dismiss) { 3465bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_DISMISS; 3466b40dacdcSJohn Snow } 3467d58d8453SJohn Snow 3468111049a4SJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3469111049a4SJohn Snow backup->sync, bmap, backup->compress, 3470111049a4SJohn Snow backup->on_source_error, backup->on_target_error, 3471b40dacdcSJohn Snow job_flags, NULL, NULL, txn, &local_err); 34724f6fd349SFam Zheng bdrv_unref(target_bs); 34735c438bc6SKevin Wolf if (local_err != NULL) { 347499a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3475761731b1SStefan Hajnoczi goto out; 347699a9addfSStefan Hajnoczi } 3477761731b1SStefan Hajnoczi 3478761731b1SStefan Hajnoczi out: 3479761731b1SStefan Hajnoczi aio_context_release(aio_context); 3480111049a4SJohn Snow return job; 348199a9addfSStefan Hajnoczi } 348299a9addfSStefan Hajnoczi 348381206a89SPavel Butsykin void qmp_drive_backup(DriveBackup *arg, Error **errp) 348478f51fdeSJohn Snow { 3485111049a4SJohn Snow 3486111049a4SJohn Snow BlockJob *job; 3487111049a4SJohn Snow job = do_drive_backup(arg, NULL, errp); 3488111049a4SJohn Snow if (job) { 3489da01ff7fSKevin Wolf job_start(&job->job); 3490111049a4SJohn Snow } 349178f51fdeSJohn Snow } 349278f51fdeSJohn Snow 3493c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3494c13163fbSBenoît Canet { 3495d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3496c13163fbSBenoît Canet } 3497c13163fbSBenoît Canet 349862c9e416SKevin Wolf BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn, 3499111049a4SJohn Snow Error **errp) 3500c29c1dd3SFam Zheng { 3501c29c1dd3SFam Zheng BlockDriverState *bs; 3502c29c1dd3SFam Zheng BlockDriverState *target_bs; 3503c29c1dd3SFam Zheng Error *local_err = NULL; 3504c29c1dd3SFam Zheng AioContext *aio_context; 3505111049a4SJohn Snow BlockJob *job = NULL; 3506bb02b65cSKevin Wolf int job_flags = JOB_DEFAULT; 3507c29c1dd3SFam Zheng 3508dc7a4a9eSPavel Butsykin if (!backup->has_speed) { 3509dc7a4a9eSPavel Butsykin backup->speed = 0; 3510c29c1dd3SFam Zheng } 3511dc7a4a9eSPavel Butsykin if (!backup->has_on_source_error) { 3512dc7a4a9eSPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3513c29c1dd3SFam Zheng } 3514dc7a4a9eSPavel Butsykin if (!backup->has_on_target_error) { 3515dc7a4a9eSPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3516dc7a4a9eSPavel Butsykin } 3517dc7a4a9eSPavel Butsykin if (!backup->has_job_id) { 3518dc7a4a9eSPavel Butsykin backup->job_id = NULL; 3519c29c1dd3SFam Zheng } 3520b40dacdcSJohn Snow if (!backup->has_auto_finalize) { 3521b40dacdcSJohn Snow backup->auto_finalize = true; 3522b40dacdcSJohn Snow } 3523b40dacdcSJohn Snow if (!backup->has_auto_dismiss) { 3524b40dacdcSJohn Snow backup->auto_dismiss = true; 3525b40dacdcSJohn Snow } 35263b7b1236SPavel Butsykin if (!backup->has_compress) { 35273b7b1236SPavel Butsykin backup->compress = false; 35283b7b1236SPavel Butsykin } 3529c29c1dd3SFam Zheng 3530930fe17fSVladimir Sementsov-Ogievskiy bs = bdrv_lookup_bs(backup->device, backup->device, errp); 3531cef34eebSKevin Wolf if (!bs) { 3532111049a4SJohn Snow return NULL; 3533c29c1dd3SFam Zheng } 3534c29c1dd3SFam Zheng 3535cef34eebSKevin Wolf aio_context = bdrv_get_aio_context(bs); 3536c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3537c29c1dd3SFam Zheng 3538dc7a4a9eSPavel Butsykin target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); 35390d978913SFam Zheng if (!target_bs) { 3540c29c1dd3SFam Zheng goto out; 3541c29c1dd3SFam Zheng } 35425433c24fSMax Reitz 3543efd75567SFam Zheng if (bdrv_get_aio_context(target_bs) != aio_context) { 3544efd75567SFam Zheng if (!bdrv_has_blk(target_bs)) { 3545efd75567SFam Zheng /* The target BDS is not attached, we can safely move it to another 3546efd75567SFam Zheng * AioContext. */ 3547c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3548efd75567SFam Zheng } else { 3549efd75567SFam Zheng error_setg(errp, "Target is attached to a different thread from " 3550efd75567SFam Zheng "source."); 3551efd75567SFam Zheng goto out; 3552efd75567SFam Zheng } 3553efd75567SFam Zheng } 3554b40dacdcSJohn Snow if (!backup->auto_finalize) { 3555bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_FINALIZE; 3556b40dacdcSJohn Snow } 3557b40dacdcSJohn Snow if (!backup->auto_dismiss) { 3558bb02b65cSKevin Wolf job_flags |= JOB_MANUAL_DISMISS; 3559b40dacdcSJohn Snow } 3560111049a4SJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3561111049a4SJohn Snow backup->sync, NULL, backup->compress, 3562111049a4SJohn Snow backup->on_source_error, backup->on_target_error, 3563b40dacdcSJohn Snow job_flags, NULL, NULL, txn, &local_err); 3564c29c1dd3SFam Zheng if (local_err != NULL) { 3565c29c1dd3SFam Zheng error_propagate(errp, local_err); 3566c29c1dd3SFam Zheng } 3567c29c1dd3SFam Zheng out: 3568c29c1dd3SFam Zheng aio_context_release(aio_context); 3569111049a4SJohn Snow return job; 3570c29c1dd3SFam Zheng } 3571c29c1dd3SFam Zheng 3572dc7a4a9eSPavel Butsykin void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp) 357378f51fdeSJohn Snow { 3574111049a4SJohn Snow BlockJob *job; 3575111049a4SJohn Snow job = do_blockdev_backup(arg, NULL, errp); 3576111049a4SJohn Snow if (job) { 3577da01ff7fSKevin Wolf job_start(&job->job); 3578111049a4SJohn Snow } 357978f51fdeSJohn Snow } 358078f51fdeSJohn Snow 35814193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 35824193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 35834193cdd7SFam Zheng **/ 358471aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, 35854193cdd7SFam Zheng BlockDriverState *target, 358609158f00SBenoît Canet bool has_replaces, const char *replaces, 3587d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3588274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 3589b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3590eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 359108e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 35924193cdd7SFam Zheng bool has_on_source_error, 35934193cdd7SFam Zheng BlockdevOnError on_source_error, 35944193cdd7SFam Zheng bool has_on_target_error, 35954193cdd7SFam Zheng BlockdevOnError on_target_error, 35960fc9f8eaSFam Zheng bool has_unmap, bool unmap, 35976cdbceb1SKevin Wolf bool has_filter_node_name, 35986cdbceb1SKevin Wolf const char *filter_node_name, 3599481debaaSMax Reitz bool has_copy_mode, MirrorCopyMode copy_mode, 3600*a6b58adeSJohn Snow bool has_auto_finalize, bool auto_finalize, 3601*a6b58adeSJohn Snow bool has_auto_dismiss, bool auto_dismiss, 3602b952b558SPaolo Bonzini Error **errp) 3603d9b902dbSPaolo Bonzini { 3604a1999b33SJohn Snow int job_flags = JOB_DEFAULT; 3605d9b902dbSPaolo Bonzini 3606d9b902dbSPaolo Bonzini if (!has_speed) { 3607d9b902dbSPaolo Bonzini speed = 0; 3608d9b902dbSPaolo Bonzini } 3609b952b558SPaolo Bonzini if (!has_on_source_error) { 3610b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3611b952b558SPaolo Bonzini } 3612b952b558SPaolo Bonzini if (!has_on_target_error) { 3613b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3614b952b558SPaolo Bonzini } 3615eee13dfeSPaolo Bonzini if (!has_granularity) { 3616eee13dfeSPaolo Bonzini granularity = 0; 3617eee13dfeSPaolo Bonzini } 361808e4ed6cSPaolo Bonzini if (!has_buf_size) { 361948ac0a4dSWen Congyang buf_size = 0; 362008e4ed6cSPaolo Bonzini } 36210fc9f8eaSFam Zheng if (!has_unmap) { 36220fc9f8eaSFam Zheng unmap = true; 36230fc9f8eaSFam Zheng } 36246cdbceb1SKevin Wolf if (!has_filter_node_name) { 36256cdbceb1SKevin Wolf filter_node_name = NULL; 36266cdbceb1SKevin Wolf } 3627481debaaSMax Reitz if (!has_copy_mode) { 3628481debaaSMax Reitz copy_mode = MIRROR_COPY_MODE_BACKGROUND; 3629481debaaSMax Reitz } 3630*a6b58adeSJohn Snow if (has_auto_finalize && !auto_finalize) { 3631*a6b58adeSJohn Snow job_flags |= JOB_MANUAL_FINALIZE; 3632*a6b58adeSJohn Snow } 3633*a6b58adeSJohn Snow if (has_auto_dismiss && !auto_dismiss) { 3634*a6b58adeSJohn Snow job_flags |= JOB_MANUAL_DISMISS; 3635*a6b58adeSJohn Snow } 363608e4ed6cSPaolo Bonzini 3637eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3638c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 36393cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3640eee13dfeSPaolo Bonzini return; 3641eee13dfeSPaolo Bonzini } 3642eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3643c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3644c6bd8c70SMarkus Armbruster "power of 2"); 3645eee13dfeSPaolo Bonzini return; 3646eee13dfeSPaolo Bonzini } 3647d9b902dbSPaolo Bonzini 36484193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 36494193cdd7SFam Zheng return; 36504193cdd7SFam Zheng } 3651e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3652e40e5027SFam Zheng return; 3653e40e5027SFam Zheng } 36544193cdd7SFam Zheng 36554193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 36564193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 36574193cdd7SFam Zheng } 36584193cdd7SFam Zheng 36594193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 36604193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 36614193cdd7SFam Zheng */ 366271aa9867SAlberto Garcia mirror_start(job_id, bs, target, 3663a1999b33SJohn Snow has_replaces ? replaces : NULL, job_flags, 3664274fcceeSMax Reitz speed, granularity, buf_size, sync, backing_mode, 36656cdbceb1SKevin Wolf on_source_error, on_target_error, unmap, filter_node_name, 3666481debaaSMax Reitz copy_mode, errp); 36674193cdd7SFam Zheng } 36684193cdd7SFam Zheng 3669faecd40aSEric Blake void qmp_drive_mirror(DriveMirror *arg, Error **errp) 36704193cdd7SFam Zheng { 36714193cdd7SFam Zheng BlockDriverState *bs; 36724193cdd7SFam Zheng BlockDriverState *source, *target_bs; 36734193cdd7SFam Zheng AioContext *aio_context; 3674274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 36754193cdd7SFam Zheng Error *local_err = NULL; 36764193cdd7SFam Zheng QDict *options = NULL; 36774193cdd7SFam Zheng int flags; 36784193cdd7SFam Zheng int64_t size; 3679faecd40aSEric Blake const char *format = arg->format; 36804193cdd7SFam Zheng 36810524e93aSKevin Wolf bs = qmp_get_root_bs(arg->device, errp); 36820524e93aSKevin Wolf if (!bs) { 3683d9b902dbSPaolo Bonzini return; 3684d9b902dbSPaolo Bonzini } 3685d9b902dbSPaolo Bonzini 3686cb2af917SPaolo Bonzini /* Early check to avoid creating target */ 3687cb2af917SPaolo Bonzini if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 3688cb2af917SPaolo Bonzini return; 3689cb2af917SPaolo Bonzini } 3690cb2af917SPaolo Bonzini 36910524e93aSKevin Wolf aio_context = bdrv_get_aio_context(bs); 36925a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 36935a7e7a0bSStefan Hajnoczi 3694faecd40aSEric Blake if (!arg->has_mode) { 3695faecd40aSEric Blake arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 36964193cdd7SFam Zheng } 3697d9b902dbSPaolo Bonzini 3698faecd40aSEric Blake if (!arg->has_format) { 3699faecd40aSEric Blake format = (arg->mode == NEW_IMAGE_MODE_EXISTING 3700faecd40aSEric Blake ? NULL : bs->drv->format_name); 3701d9b902dbSPaolo Bonzini } 3702d9b902dbSPaolo Bonzini 370361de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3704760e0063SKevin Wolf source = backing_bs(bs); 3705faecd40aSEric Blake if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { 3706faecd40aSEric Blake arg->sync = MIRROR_SYNC_MODE_FULL; 3707d9b902dbSPaolo Bonzini } 3708faecd40aSEric Blake if (arg->sync == MIRROR_SYNC_MODE_NONE) { 3709117e0c82SMax Reitz source = bs; 3710117e0c82SMax Reitz } 3711d9b902dbSPaolo Bonzini 3712ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3713ac3c5d83SStefan Hajnoczi if (size < 0) { 3714ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 37155a7e7a0bSStefan Hajnoczi goto out; 3716ac3c5d83SStefan Hajnoczi } 3717ac3c5d83SStefan Hajnoczi 3718faecd40aSEric Blake if (arg->has_replaces) { 371909158f00SBenoît Canet BlockDriverState *to_replace_bs; 37205a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 37215a7e7a0bSStefan Hajnoczi int64_t replace_size; 372209158f00SBenoît Canet 3723faecd40aSEric Blake if (!arg->has_node_name) { 372409158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 372509158f00SBenoît Canet " named node of the graph"); 37265a7e7a0bSStefan Hajnoczi goto out; 372709158f00SBenoît Canet } 372809158f00SBenoît Canet 3729faecd40aSEric Blake to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err); 373009158f00SBenoît Canet 373109158f00SBenoît Canet if (!to_replace_bs) { 373209158f00SBenoît Canet error_propagate(errp, local_err); 37335a7e7a0bSStefan Hajnoczi goto out; 373409158f00SBenoît Canet } 373509158f00SBenoît Canet 37365a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 37375a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 37385a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 37395a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 37405a7e7a0bSStefan Hajnoczi 37415a7e7a0bSStefan Hajnoczi if (size != replace_size) { 374209158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 374309158f00SBenoît Canet "different size"); 37445a7e7a0bSStefan Hajnoczi goto out; 374509158f00SBenoît Canet } 374609158f00SBenoît Canet } 374709158f00SBenoît Canet 3748faecd40aSEric Blake if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { 3749274fcceeSMax Reitz backing_mode = MIRROR_SOURCE_BACKING_CHAIN; 3750274fcceeSMax Reitz } else { 3751274fcceeSMax Reitz backing_mode = MIRROR_OPEN_BACKING_CHAIN; 3752274fcceeSMax Reitz } 3753274fcceeSMax Reitz 37542a32c6e8SJohn Snow /* Don't open backing image in create() */ 37552a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING; 37562a32c6e8SJohn Snow 3757faecd40aSEric Blake if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) 3758faecd40aSEric Blake && arg->mode != NEW_IMAGE_MODE_EXISTING) 375914526864SMax Reitz { 3760d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3761e6641719SMax Reitz assert(format); 3762faecd40aSEric Blake bdrv_img_create(arg->target, format, 37639217283dSFam Zheng NULL, NULL, NULL, size, flags, false, &local_err); 3764d9b902dbSPaolo Bonzini } else { 3765faecd40aSEric Blake switch (arg->mode) { 3766d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3767d9b902dbSPaolo Bonzini break; 3768d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3769d9b902dbSPaolo Bonzini /* create new image with backing file */ 3770faecd40aSEric Blake bdrv_img_create(arg->target, format, 3771d9b902dbSPaolo Bonzini source->filename, 3772d9b902dbSPaolo Bonzini source->drv->format_name, 37739217283dSFam Zheng NULL, size, flags, false, &local_err); 3774d9b902dbSPaolo Bonzini break; 3775d9b902dbSPaolo Bonzini default: 3776d9b902dbSPaolo Bonzini abort(); 3777d9b902dbSPaolo Bonzini } 3778d9b902dbSPaolo Bonzini } 3779d9b902dbSPaolo Bonzini 378084d18f06SMarkus Armbruster if (local_err) { 3781cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 37825a7e7a0bSStefan Hajnoczi goto out; 3783d9b902dbSPaolo Bonzini } 3784d9b902dbSPaolo Bonzini 37854c828dc6SBenoît Canet options = qdict_new(); 3786faecd40aSEric Blake if (arg->has_node_name) { 378746f5ac20SEric Blake qdict_put_str(options, "node-name", arg->node_name); 37884c828dc6SBenoît Canet } 3789e6641719SMax Reitz if (format) { 379046f5ac20SEric Blake qdict_put_str(options, "driver", format); 3791e6641719SMax Reitz } 37924c828dc6SBenoît Canet 3793b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3794b812f671SPaolo Bonzini * file. 3795b812f671SPaolo Bonzini */ 37962a32c6e8SJohn Snow target_bs = bdrv_open(arg->target, NULL, options, flags, errp); 37975b363937SMax Reitz if (!target_bs) { 37985a7e7a0bSStefan Hajnoczi goto out; 3799d9b902dbSPaolo Bonzini } 3800d9b902dbSPaolo Bonzini 38015a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 38025a7e7a0bSStefan Hajnoczi 3803faecd40aSEric Blake blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, 3804faecd40aSEric Blake arg->has_replaces, arg->replaces, arg->sync, 3805faecd40aSEric Blake backing_mode, arg->has_speed, arg->speed, 3806faecd40aSEric Blake arg->has_granularity, arg->granularity, 3807faecd40aSEric Blake arg->has_buf_size, arg->buf_size, 3808faecd40aSEric Blake arg->has_on_source_error, arg->on_source_error, 3809faecd40aSEric Blake arg->has_on_target_error, arg->on_target_error, 3810faecd40aSEric Blake arg->has_unmap, arg->unmap, 38116cdbceb1SKevin Wolf false, NULL, 3812481debaaSMax Reitz arg->has_copy_mode, arg->copy_mode, 3813*a6b58adeSJohn Snow arg->has_auto_finalize, arg->auto_finalize, 3814*a6b58adeSJohn Snow arg->has_auto_dismiss, arg->auto_dismiss, 38154193cdd7SFam Zheng &local_err); 3816e253f4b8SKevin Wolf bdrv_unref(target_bs); 3817d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 38185a7e7a0bSStefan Hajnoczi out: 38195a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3820d9b902dbSPaolo Bonzini } 3821d9b902dbSPaolo Bonzini 382271aa9867SAlberto Garcia void qmp_blockdev_mirror(bool has_job_id, const char *job_id, 382371aa9867SAlberto Garcia const char *device, const char *target, 3824df92562eSFam Zheng bool has_replaces, const char *replaces, 3825df92562eSFam Zheng MirrorSyncMode sync, 3826df92562eSFam Zheng bool has_speed, int64_t speed, 3827df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3828df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3829df92562eSFam Zheng bool has_on_source_error, 3830df92562eSFam Zheng BlockdevOnError on_source_error, 3831df92562eSFam Zheng bool has_on_target_error, 3832df92562eSFam Zheng BlockdevOnError on_target_error, 38336cdbceb1SKevin Wolf bool has_filter_node_name, 38346cdbceb1SKevin Wolf const char *filter_node_name, 3835481debaaSMax Reitz bool has_copy_mode, MirrorCopyMode copy_mode, 3836*a6b58adeSJohn Snow bool has_auto_finalize, bool auto_finalize, 3837*a6b58adeSJohn Snow bool has_auto_dismiss, bool auto_dismiss, 3838df92562eSFam Zheng Error **errp) 3839df92562eSFam Zheng { 3840df92562eSFam Zheng BlockDriverState *bs; 3841df92562eSFam Zheng BlockDriverState *target_bs; 3842df92562eSFam Zheng AioContext *aio_context; 3843274fcceeSMax Reitz BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; 3844df92562eSFam Zheng Error *local_err = NULL; 3845df92562eSFam Zheng 384607eec652SKevin Wolf bs = qmp_get_root_bs(device, errp); 3847df92562eSFam Zheng if (!bs) { 3848df92562eSFam Zheng return; 3849df92562eSFam Zheng } 3850df92562eSFam Zheng 3851df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3852df92562eSFam Zheng if (!target_bs) { 3853df92562eSFam Zheng return; 3854df92562eSFam Zheng } 3855df92562eSFam Zheng 3856df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3857df92562eSFam Zheng aio_context_acquire(aio_context); 3858df92562eSFam Zheng 3859df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3860df92562eSFam Zheng 386171aa9867SAlberto Garcia blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 3862274fcceeSMax Reitz has_replaces, replaces, sync, backing_mode, 3863df92562eSFam Zheng has_speed, speed, 3864df92562eSFam Zheng has_granularity, granularity, 3865df92562eSFam Zheng has_buf_size, buf_size, 3866df92562eSFam Zheng has_on_source_error, on_source_error, 3867df92562eSFam Zheng has_on_target_error, on_target_error, 3868df92562eSFam Zheng true, true, 38696cdbceb1SKevin Wolf has_filter_node_name, filter_node_name, 3870481debaaSMax Reitz has_copy_mode, copy_mode, 3871*a6b58adeSJohn Snow has_auto_finalize, auto_finalize, 3872*a6b58adeSJohn Snow has_auto_dismiss, auto_dismiss, 3873df92562eSFam Zheng &local_err); 3874df92562eSFam Zheng error_propagate(errp, local_err); 3875df92562eSFam Zheng 3876df92562eSFam Zheng aio_context_release(aio_context); 3877df92562eSFam Zheng } 3878df92562eSFam Zheng 38793ddf3efeSAlberto Garcia /* Get a block job using its ID and acquire its AioContext */ 38803ddf3efeSAlberto Garcia static BlockJob *find_block_job(const char *id, AioContext **aio_context, 388124d6bffeSMarkus Armbruster Error **errp) 38822d47c6e9SStefan Hajnoczi { 38833ddf3efeSAlberto Garcia BlockJob *job; 38843ddf3efeSAlberto Garcia 38853ddf3efeSAlberto Garcia assert(id != NULL); 38862d47c6e9SStefan Hajnoczi 38875433c24fSMax Reitz *aio_context = NULL; 38885433c24fSMax Reitz 38893ddf3efeSAlberto Garcia job = block_job_get(id); 38903ddf3efeSAlberto Garcia 38913ddf3efeSAlberto Garcia if (!job) { 38923ddf3efeSAlberto Garcia error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 38933ddf3efeSAlberto Garcia "Block job '%s' not found", id); 38943ddf3efeSAlberto Garcia return NULL; 38952d47c6e9SStefan Hajnoczi } 38963d948cdfSStefan Hajnoczi 38973ddf3efeSAlberto Garcia *aio_context = blk_get_aio_context(job->blk); 38983d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 38993d948cdfSStefan Hajnoczi 39003ddf3efeSAlberto Garcia return job; 39012d47c6e9SStefan Hajnoczi } 39022d47c6e9SStefan Hajnoczi 3903882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 39042d47c6e9SStefan Hajnoczi { 39053d948cdfSStefan Hajnoczi AioContext *aio_context; 390624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 39072d47c6e9SStefan Hajnoczi 39082d47c6e9SStefan Hajnoczi if (!job) { 39092d47c6e9SStefan Hajnoczi return; 39102d47c6e9SStefan Hajnoczi } 39112d47c6e9SStefan Hajnoczi 3912882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 39133d948cdfSStefan Hajnoczi aio_context_release(aio_context); 39142d47c6e9SStefan Hajnoczi } 3915370521a1SStefan Hajnoczi 39166e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 39176e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 39186e37fb81SPaolo Bonzini { 39193d948cdfSStefan Hajnoczi AioContext *aio_context; 392024d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 39216e37fb81SPaolo Bonzini 39226e37fb81SPaolo Bonzini if (!job) { 39236e37fb81SPaolo Bonzini return; 39246e37fb81SPaolo Bonzini } 39253d948cdfSStefan Hajnoczi 39263d948cdfSStefan Hajnoczi if (!has_force) { 39273d948cdfSStefan Hajnoczi force = false; 39283d948cdfSStefan Hajnoczi } 39293d948cdfSStefan Hajnoczi 3930b15de828SKevin Wolf if (job_user_paused(&job->job) && !force) { 3931f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3932f231b88dSCole Robinson device); 39333d948cdfSStefan Hajnoczi goto out; 39346e37fb81SPaolo Bonzini } 39356e37fb81SPaolo Bonzini 39366e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 39373d70ff53SKevin Wolf job_user_cancel(&job->job, force, errp); 39383d948cdfSStefan Hajnoczi out: 39393d948cdfSStefan Hajnoczi aio_context_release(aio_context); 39406e37fb81SPaolo Bonzini } 39416e37fb81SPaolo Bonzini 39426e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3943370521a1SStefan Hajnoczi { 39443d948cdfSStefan Hajnoczi AioContext *aio_context; 394524d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3946370521a1SStefan Hajnoczi 39470ec4dfb8SJohn Snow if (!job) { 3948370521a1SStefan Hajnoczi return; 3949370521a1SStefan Hajnoczi } 39506e37fb81SPaolo Bonzini 39516e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 3952b15de828SKevin Wolf job_user_pause(&job->job, errp); 39533d948cdfSStefan Hajnoczi aio_context_release(aio_context); 39546e37fb81SPaolo Bonzini } 39556e37fb81SPaolo Bonzini 39566e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 39576e37fb81SPaolo Bonzini { 39583d948cdfSStefan Hajnoczi AioContext *aio_context; 395924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 39606e37fb81SPaolo Bonzini 39610ec4dfb8SJohn Snow if (!job) { 39628acc72a4SPaolo Bonzini return; 39638acc72a4SPaolo Bonzini } 3964370521a1SStefan Hajnoczi 39656e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 3966b15de828SKevin Wolf job_user_resume(&job->job, errp); 39673d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3968370521a1SStefan Hajnoczi } 3969fb5458cdSStefan Hajnoczi 3970aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3971aeae883bSPaolo Bonzini { 39723d948cdfSStefan Hajnoczi AioContext *aio_context; 397324d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3974aeae883bSPaolo Bonzini 3975aeae883bSPaolo Bonzini if (!job) { 3976aeae883bSPaolo Bonzini return; 3977aeae883bSPaolo Bonzini } 3978aeae883bSPaolo Bonzini 3979aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 39803453d972SKevin Wolf job_complete(&job->job, errp); 39813d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3982aeae883bSPaolo Bonzini } 3983aeae883bSPaolo Bonzini 398411b61fbcSJohn Snow void qmp_block_job_finalize(const char *id, Error **errp) 398511b61fbcSJohn Snow { 398611b61fbcSJohn Snow AioContext *aio_context; 398711b61fbcSJohn Snow BlockJob *job = find_block_job(id, &aio_context, errp); 398811b61fbcSJohn Snow 398911b61fbcSJohn Snow if (!job) { 399011b61fbcSJohn Snow return; 399111b61fbcSJohn Snow } 399211b61fbcSJohn Snow 399311b61fbcSJohn Snow trace_qmp_block_job_finalize(job); 39947eaa8fb5SKevin Wolf job_finalize(&job->job, errp); 399511b61fbcSJohn Snow aio_context_release(aio_context); 399611b61fbcSJohn Snow } 399711b61fbcSJohn Snow 399875f71059SJohn Snow void qmp_block_job_dismiss(const char *id, Error **errp) 399975f71059SJohn Snow { 400075f71059SJohn Snow AioContext *aio_context; 40015f9a6a08SKevin Wolf BlockJob *bjob = find_block_job(id, &aio_context, errp); 40025f9a6a08SKevin Wolf Job *job; 400375f71059SJohn Snow 40045f9a6a08SKevin Wolf if (!bjob) { 400575f71059SJohn Snow return; 400675f71059SJohn Snow } 400775f71059SJohn Snow 40085f9a6a08SKevin Wolf trace_qmp_block_job_dismiss(bjob); 40095f9a6a08SKevin Wolf job = &bjob->job; 40105f9a6a08SKevin Wolf job_dismiss(&job, errp); 401175f71059SJohn Snow aio_context_release(aio_context); 401275f71059SJohn Snow } 401375f71059SJohn Snow 4014fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 4015fa40e656SJeff Cody const char *image_node_name, 4016fa40e656SJeff Cody const char *backing_file, 4017fa40e656SJeff Cody Error **errp) 4018fa40e656SJeff Cody { 4019fa40e656SJeff Cody BlockDriverState *bs = NULL; 4020729962f6SStefan Hajnoczi AioContext *aio_context; 4021fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 4022fa40e656SJeff Cody Error *local_err = NULL; 4023fa40e656SJeff Cody bool ro; 4024fa40e656SJeff Cody int open_flags; 4025fa40e656SJeff Cody int ret; 4026fa40e656SJeff Cody 40277b5dca3fSKevin Wolf bs = qmp_get_root_bs(device, errp); 40287b5dca3fSKevin Wolf if (!bs) { 4029fa40e656SJeff Cody return; 4030fa40e656SJeff Cody } 4031fa40e656SJeff Cody 40327b5dca3fSKevin Wolf aio_context = bdrv_get_aio_context(bs); 4033729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 4034729962f6SStefan Hajnoczi 4035fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 4036fa40e656SJeff Cody if (local_err) { 4037fa40e656SJeff Cody error_propagate(errp, local_err); 4038729962f6SStefan Hajnoczi goto out; 4039fa40e656SJeff Cody } 4040fa40e656SJeff Cody 4041fa40e656SJeff Cody if (!image_bs) { 4042fa40e656SJeff Cody error_setg(errp, "image file not found"); 4043729962f6SStefan Hajnoczi goto out; 4044fa40e656SJeff Cody } 4045fa40e656SJeff Cody 4046fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 4047fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 4048fa40e656SJeff Cody "without a backing file"); 4049729962f6SStefan Hajnoczi goto out; 4050fa40e656SJeff Cody } 4051fa40e656SJeff Cody 4052fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 4053fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 4054fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 4055729962f6SStefan Hajnoczi goto out; 4056fa40e656SJeff Cody } 4057fa40e656SJeff Cody 4058fa40e656SJeff Cody /* final sanity check */ 4059fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 4060fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 4061fa40e656SJeff Cody device); 4062729962f6SStefan Hajnoczi goto out; 4063fa40e656SJeff Cody } 4064fa40e656SJeff Cody 4065fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 4066fa40e656SJeff Cody open_flags = image_bs->open_flags; 4067fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 4068fa40e656SJeff Cody 4069fa40e656SJeff Cody if (ro) { 4070fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 4071fa40e656SJeff Cody if (local_err) { 4072fa40e656SJeff Cody error_propagate(errp, local_err); 4073729962f6SStefan Hajnoczi goto out; 4074fa40e656SJeff Cody } 4075fa40e656SJeff Cody } 4076fa40e656SJeff Cody 4077fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 4078fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 4079fa40e656SJeff Cody 4080fa40e656SJeff Cody if (ret < 0) { 4081fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 4082fa40e656SJeff Cody backing_file); 4083fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 4084fa40e656SJeff Cody * appropriate */ 4085fa40e656SJeff Cody } 4086fa40e656SJeff Cody 4087fa40e656SJeff Cody if (ro) { 4088fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 4089621ff94dSEduardo Habkost error_propagate(errp, local_err); 4090fa40e656SJeff Cody } 4091729962f6SStefan Hajnoczi 4092729962f6SStefan Hajnoczi out: 4093729962f6SStefan Hajnoczi aio_context_release(aio_context); 4094fa40e656SJeff Cody } 4095fa40e656SJeff Cody 4096abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 4097abb21ac3SKevin Wolf { 4098abb21ac3SKevin Wolf QemuOpts *opts; 4099abb21ac3SKevin Wolf QDict *qdict; 4100abb21ac3SKevin Wolf Error *local_err = NULL; 4101abb21ac3SKevin Wolf 4102abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 4103abb21ac3SKevin Wolf if (!opts) { 4104abb21ac3SKevin Wolf return; 4105abb21ac3SKevin Wolf } 4106abb21ac3SKevin Wolf 4107abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 4108abb21ac3SKevin Wolf 4109abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 4110cb3e7f08SMarc-André Lureau qobject_unref(qdict); 4111abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 4112abb21ac3SKevin Wolf goto out; 4113abb21ac3SKevin Wolf } 4114abb21ac3SKevin Wolf 4115abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 4116abb21ac3SKevin Wolf if (!bs) { 4117abb21ac3SKevin Wolf error_report_err(local_err); 4118abb21ac3SKevin Wolf goto out; 4119abb21ac3SKevin Wolf } 4120abb21ac3SKevin Wolf 4121abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4122abb21ac3SKevin Wolf 4123abb21ac3SKevin Wolf out: 4124abb21ac3SKevin Wolf qemu_opts_del(opts); 4125abb21ac3SKevin Wolf } 4126abb21ac3SKevin Wolf 4127d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 4128d26c9a15SKevin Wolf { 4129be4b67bcSMax Reitz BlockDriverState *bs; 4130d26c9a15SKevin Wolf QObject *obj; 41317d5e199aSDaniel P. Berrange Visitor *v = qobject_output_visitor_new(&obj); 4132d26c9a15SKevin Wolf QDict *qdict; 4133d26c9a15SKevin Wolf Error *local_err = NULL; 4134d26c9a15SKevin Wolf 41353b098d56SEric Blake visit_type_BlockdevOptions(v, NULL, &options, &local_err); 413684d18f06SMarkus Armbruster if (local_err) { 4137d26c9a15SKevin Wolf error_propagate(errp, local_err); 4138d26c9a15SKevin Wolf goto fail; 4139d26c9a15SKevin Wolf } 4140d26c9a15SKevin Wolf 41413b098d56SEric Blake visit_complete(v, &obj); 41427dc847ebSMax Reitz qdict = qobject_to(QDict, obj); 4143d26c9a15SKevin Wolf 4144d26c9a15SKevin Wolf qdict_flatten(qdict); 4145d26c9a15SKevin Wolf 4146be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 41479ec8873eSKevin Wolf error_setg(errp, "'node-name' must be specified for the root node"); 4148be4b67bcSMax Reitz goto fail; 4149be4b67bcSMax Reitz } 4150be4b67bcSMax Reitz 4151bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 4152bd745e23SMax Reitz if (!bs) { 4153be4b67bcSMax Reitz goto fail; 4154be4b67bcSMax Reitz } 41559c4218e9SMax Reitz 41569c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4157be4b67bcSMax Reitz 4158d26c9a15SKevin Wolf fail: 41593b098d56SEric Blake visit_free(v); 4160d26c9a15SKevin Wolf } 4161d26c9a15SKevin Wolf 416279b7a77eSMarkus Armbruster void qmp_blockdev_del(const char *node_name, Error **errp) 416381b936aeSAlberto Garcia { 416481b936aeSAlberto Garcia AioContext *aio_context; 416581b936aeSAlberto Garcia BlockDriverState *bs; 416681b936aeSAlberto Garcia 416781b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 416881b936aeSAlberto Garcia if (!bs) { 416981b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 417081b936aeSAlberto Garcia return; 417181b936aeSAlberto Garcia } 41721f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 4173e467da7bSKevin Wolf error_setg(errp, "Node %s is in use", node_name); 417481b936aeSAlberto Garcia return; 417581b936aeSAlberto Garcia } 417681b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 417781b936aeSAlberto Garcia aio_context_acquire(aio_context); 417881b936aeSAlberto Garcia 417981b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 418081b936aeSAlberto Garcia goto out; 418181b936aeSAlberto Garcia } 418281b936aeSAlberto Garcia 41839ec8873eSKevin Wolf if (!bs->monitor_list.tqe_prev) { 41849c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 41859c4218e9SMax Reitz bs->node_name); 41869c4218e9SMax Reitz goto out; 41879c4218e9SMax Reitz } 41889c4218e9SMax Reitz 41899c4218e9SMax Reitz if (bs->refcnt > 1) { 419081b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 419181b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 419281b936aeSAlberto Garcia goto out; 419381b936aeSAlberto Garcia } 419481b936aeSAlberto Garcia 41959c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 419681b936aeSAlberto Garcia bdrv_unref(bs); 419781b936aeSAlberto Garcia 419881b936aeSAlberto Garcia out: 419981b936aeSAlberto Garcia aio_context_release(aio_context); 420081b936aeSAlberto Garcia } 420181b936aeSAlberto Garcia 42027f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 42037f821597SWen Congyang const char *child_name) 42047f821597SWen Congyang { 42057f821597SWen Congyang BdrvChild *child; 42067f821597SWen Congyang 42077f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 42087f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 42097f821597SWen Congyang return child; 42107f821597SWen Congyang } 42117f821597SWen Congyang } 42127f821597SWen Congyang 42137f821597SWen Congyang return NULL; 42147f821597SWen Congyang } 42157f821597SWen Congyang 42167f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 42177f821597SWen Congyang const char *child, bool has_node, 42187f821597SWen Congyang const char *node, Error **errp) 42197f821597SWen Congyang { 42207f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 42217f821597SWen Congyang BdrvChild *p_child; 42227f821597SWen Congyang 42237f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 42247f821597SWen Congyang if (!parent_bs) { 42257f821597SWen Congyang return; 42267f821597SWen Congyang } 42277f821597SWen Congyang 42287f821597SWen Congyang if (has_child == has_node) { 42297f821597SWen Congyang if (has_child) { 42307f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 42317f821597SWen Congyang } else { 42327f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 42337f821597SWen Congyang } 42347f821597SWen Congyang return; 42357f821597SWen Congyang } 42367f821597SWen Congyang 42377f821597SWen Congyang if (has_child) { 42387f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 42397f821597SWen Congyang if (!p_child) { 42407f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 42417f821597SWen Congyang parent, child); 42427f821597SWen Congyang return; 42437f821597SWen Congyang } 42447f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 42457f821597SWen Congyang } 42467f821597SWen Congyang 42477f821597SWen Congyang if (has_node) { 42487f821597SWen Congyang new_bs = bdrv_find_node(node); 42497f821597SWen Congyang if (!new_bs) { 42507f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 42517f821597SWen Congyang return; 42527f821597SWen Congyang } 42537f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 42547f821597SWen Congyang } 42557f821597SWen Congyang } 42567f821597SWen Congyang 4257fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4258fb5458cdSStefan Hajnoczi { 4259fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4260f0f55dedSAlberto Garcia BlockJob *job; 4261fea68bb6SMarkus Armbruster 4262f0f55dedSAlberto Garcia for (job = block_job_next(NULL); job; job = block_job_next(job)) { 4263559b935fSJohn Snow BlockJobInfoList *elem; 4264559b935fSJohn Snow AioContext *aio_context; 426569691e72SStefan Hajnoczi 4266559b935fSJohn Snow if (block_job_is_internal(job)) { 4267559b935fSJohn Snow continue; 4268559b935fSJohn Snow } 4269559b935fSJohn Snow elem = g_new0(BlockJobInfoList, 1); 4270559b935fSJohn Snow aio_context = blk_get_aio_context(job->blk); 427169691e72SStefan Hajnoczi aio_context_acquire(aio_context); 4272559b935fSJohn Snow elem->value = block_job_query(job, errp); 4273f0f55dedSAlberto Garcia aio_context_release(aio_context); 4274559b935fSJohn Snow if (!elem->value) { 4275559b935fSJohn Snow g_free(elem); 4276559b935fSJohn Snow qapi_free_BlockJobInfoList(head); 4277559b935fSJohn Snow return NULL; 4278559b935fSJohn Snow } 4279fea68bb6SMarkus Armbruster *p_next = elem; 4280fea68bb6SMarkus Armbruster p_next = &elem->next; 4281fea68bb6SMarkus Armbruster } 428269691e72SStefan Hajnoczi 4283fea68bb6SMarkus Armbruster return head; 4284fb5458cdSStefan Hajnoczi } 42854d454574SPaolo Bonzini 4286ca00bbb1SStefan Hajnoczi void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread, 4287882e9b89SStefan Hajnoczi bool has_force, bool force, Error **errp) 4288ca00bbb1SStefan Hajnoczi { 4289ca00bbb1SStefan Hajnoczi AioContext *old_context; 4290ca00bbb1SStefan Hajnoczi AioContext *new_context; 4291ca00bbb1SStefan Hajnoczi BlockDriverState *bs; 4292ca00bbb1SStefan Hajnoczi 4293ca00bbb1SStefan Hajnoczi bs = bdrv_find_node(node_name); 4294ca00bbb1SStefan Hajnoczi if (!bs) { 4295ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find node %s", node_name); 4296ca00bbb1SStefan Hajnoczi return; 4297ca00bbb1SStefan Hajnoczi } 4298ca00bbb1SStefan Hajnoczi 4299882e9b89SStefan Hajnoczi /* Protects against accidents. */ 4300882e9b89SStefan Hajnoczi if (!(has_force && force) && bdrv_has_blk(bs)) { 4301882e9b89SStefan Hajnoczi error_setg(errp, "Node %s is associated with a BlockBackend and could " 4302882e9b89SStefan Hajnoczi "be in use (use force=true to override this check)", 4303882e9b89SStefan Hajnoczi node_name); 4304ca00bbb1SStefan Hajnoczi return; 4305ca00bbb1SStefan Hajnoczi } 4306ca00bbb1SStefan Hajnoczi 4307ca00bbb1SStefan Hajnoczi if (iothread->type == QTYPE_QSTRING) { 4308ca00bbb1SStefan Hajnoczi IOThread *obj = iothread_by_id(iothread->u.s); 4309ca00bbb1SStefan Hajnoczi if (!obj) { 4310ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find iothread %s", iothread->u.s); 4311ca00bbb1SStefan Hajnoczi return; 4312ca00bbb1SStefan Hajnoczi } 4313ca00bbb1SStefan Hajnoczi 4314ca00bbb1SStefan Hajnoczi new_context = iothread_get_aio_context(obj); 4315ca00bbb1SStefan Hajnoczi } else { 4316ca00bbb1SStefan Hajnoczi new_context = qemu_get_aio_context(); 4317ca00bbb1SStefan Hajnoczi } 4318ca00bbb1SStefan Hajnoczi 4319ca00bbb1SStefan Hajnoczi old_context = bdrv_get_aio_context(bs); 4320ca00bbb1SStefan Hajnoczi aio_context_acquire(old_context); 4321ca00bbb1SStefan Hajnoczi 4322ca00bbb1SStefan Hajnoczi bdrv_set_aio_context(bs, new_context); 4323ca00bbb1SStefan Hajnoczi 4324ca00bbb1SStefan Hajnoczi aio_context_release(old_context); 4325ca00bbb1SStefan Hajnoczi } 4326ca00bbb1SStefan Hajnoczi 43277e5c776dSVladimir Sementsov-Ogievskiy void qmp_x_block_latency_histogram_set( 43287e5c776dSVladimir Sementsov-Ogievskiy const char *device, 43297e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries, uint64List *boundaries, 43307e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_read, uint64List *boundaries_read, 43317e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_write, uint64List *boundaries_write, 43327e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_flush, uint64List *boundaries_flush, 43337e5c776dSVladimir Sementsov-Ogievskiy Error **errp) 43347e5c776dSVladimir Sementsov-Ogievskiy { 43357e5c776dSVladimir Sementsov-Ogievskiy BlockBackend *blk = blk_by_name(device); 43367e5c776dSVladimir Sementsov-Ogievskiy BlockAcctStats *stats; 43377e5c776dSVladimir Sementsov-Ogievskiy 43387e5c776dSVladimir Sementsov-Ogievskiy if (!blk) { 43397e5c776dSVladimir Sementsov-Ogievskiy error_setg(errp, "Device '%s' not found", device); 43407e5c776dSVladimir Sementsov-Ogievskiy return; 43417e5c776dSVladimir Sementsov-Ogievskiy } 43427e5c776dSVladimir Sementsov-Ogievskiy stats = blk_get_stats(blk); 43437e5c776dSVladimir Sementsov-Ogievskiy 43447e5c776dSVladimir Sementsov-Ogievskiy if (!has_boundaries && !has_boundaries_read && !has_boundaries_write && 43457e5c776dSVladimir Sementsov-Ogievskiy !has_boundaries_flush) 43467e5c776dSVladimir Sementsov-Ogievskiy { 43477e5c776dSVladimir Sementsov-Ogievskiy block_latency_histograms_clear(stats); 43487e5c776dSVladimir Sementsov-Ogievskiy return; 43497e5c776dSVladimir Sementsov-Ogievskiy } 43507e5c776dSVladimir Sementsov-Ogievskiy 43517e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_read) { 43527e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 43537e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_READ, 43547e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_read ? boundaries_read : boundaries); 43557e5c776dSVladimir Sementsov-Ogievskiy } 43567e5c776dSVladimir Sementsov-Ogievskiy 43577e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_write) { 43587e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 43597e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_WRITE, 43607e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_write ? boundaries_write : boundaries); 43617e5c776dSVladimir Sementsov-Ogievskiy } 43627e5c776dSVladimir Sementsov-Ogievskiy 43637e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_flush) { 43647e5c776dSVladimir Sementsov-Ogievskiy block_latency_histogram_set( 43657e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_FLUSH, 43667e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_flush ? boundaries_flush : boundaries); 43677e5c776dSVladimir Sementsov-Ogievskiy } 43687e5c776dSVladimir Sementsov-Ogievskiy } 43697e5c776dSVladimir Sementsov-Ogievskiy 43700006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 43714d454574SPaolo Bonzini .name = "drive", 43720006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 43734d454574SPaolo Bonzini .desc = { 43744d454574SPaolo Bonzini { 43754d454574SPaolo Bonzini .name = "snapshot", 43764d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 43774d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 43784d454574SPaolo Bonzini },{ 43794d454574SPaolo Bonzini .name = "aio", 43804d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 43814d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 43824d454574SPaolo Bonzini },{ 4383e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4384e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4385e4b24b49SKevin Wolf .help = "Enable writeback mode", 4386e4b24b49SKevin Wolf },{ 43874d454574SPaolo Bonzini .name = "format", 43884d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 43894d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 43904d454574SPaolo Bonzini },{ 43914d454574SPaolo Bonzini .name = "rerror", 43924d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 43934d454574SPaolo Bonzini .help = "read error action", 43944d454574SPaolo Bonzini },{ 43954d454574SPaolo Bonzini .name = "werror", 43964d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 43974d454574SPaolo Bonzini .help = "write error action", 43984d454574SPaolo Bonzini },{ 43994e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 44004d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 44014d454574SPaolo Bonzini .help = "open drive file as read-only", 4402a2a7862cSPradeep Jagadeesh }, 4403a2a7862cSPradeep Jagadeesh 4404a2a7862cSPradeep Jagadeesh THROTTLE_OPTS, 4405a2a7862cSPradeep Jagadeesh 4406a2a7862cSPradeep Jagadeesh { 440776f4afb4SAlberto Garcia .name = "throttling.group", 440876f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 440976f4afb4SAlberto Garcia .help = "name of the block throttling group", 441076f4afb4SAlberto Garcia },{ 44114d454574SPaolo Bonzini .name = "copy-on-read", 44124d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 44134d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4414465bee1dSPeter Lieven },{ 4415465bee1dSPeter Lieven .name = "detect-zeroes", 4416465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4417465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4418362e9299SAlberto Garcia },{ 4419362e9299SAlberto Garcia .name = "stats-account-invalid", 4420362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4421362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4422362e9299SAlberto Garcia "in the statistics", 4423362e9299SAlberto Garcia },{ 4424362e9299SAlberto Garcia .name = "stats-account-failed", 4425362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4426362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4427362e9299SAlberto Garcia "in the statistics", 44284d454574SPaolo Bonzini }, 44294d454574SPaolo Bonzini { /* end of list */ } 44304d454574SPaolo Bonzini }, 44314d454574SPaolo Bonzini }; 44320006383eSKevin Wolf 44330006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 44340006383eSKevin Wolf .name = "drive", 44350006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 44360006383eSKevin Wolf .desc = { 4437492fdc6fSKevin Wolf /* 4438492fdc6fSKevin Wolf * no elements => accept any params 4439492fdc6fSKevin Wolf * validation will happen later 4440492fdc6fSKevin Wolf */ 44410006383eSKevin Wolf { /* end of list */ } 44420006383eSKevin Wolf }, 44430006383eSKevin Wolf }; 4444