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" 43cdcd4361SMarkus Armbruster #include "qemu/qemu-print.h" 441de7afc9SPaolo Bonzini #include "qemu/config-file.h" 459af23989SMarkus Armbruster #include "qapi/qapi-commands-block.h" 469af23989SMarkus Armbruster #include "qapi/qapi-commands-transaction.h" 479af23989SMarkus Armbruster #include "qapi/qapi-visit-block-core.h" 48452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 4915280c36SMarkus Armbruster #include "qapi/qmp/qnum.h" 506b673957SMarkus Armbruster #include "qapi/qmp/qstring.h" 51e688df6bSMarkus Armbruster #include "qapi/error.h" 52cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 5347e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h" 54b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h" 559c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 56ca00bbb1SStefan Hajnoczi #include "sysemu/iothread.h" 57737e150eSPaolo Bonzini #include "block/block_int.h" 580ab8ed18SDaniel P. Berrange #include "block/trace.h" 599c17d615SPaolo Bonzini #include "sysemu/arch_init.h" 60c616f16eSThomas Huth #include "sysemu/qtest.h" 6154d31236SMarkus Armbruster #include "sysemu/runstate.h" 62f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 63f348b6d1SVeronia Bahaa #include "qemu/help_option.h" 64db725815SMarkus Armbruster #include "qemu/main-loop.h" 65a2a7862cSPradeep Jagadeesh #include "qemu/throttle-options.h" 66666daa68SMarkus Armbruster 679c4218e9SMax Reitz static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = 689c4218e9SMax Reitz QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); 699c4218e9SMax Reitz 70b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id, 71b33945cfSKevin Wolf bool force, Error **errp); 7282fcf66eSMax Reitz static void blockdev_remove_medium(bool has_device, const char *device, 7382fcf66eSMax Reitz bool has_id, const char *id, Error **errp); 7482fcf66eSMax Reitz static void blockdev_insert_medium(bool has_device, const char *device, 7582fcf66eSMax Reitz bool has_id, const char *id, 7682fcf66eSMax Reitz const char *node_name, Error **errp); 77bf18bee5SColin Lord 781960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 791960966dSMarkus Armbruster [IF_NONE] = "none", 801960966dSMarkus Armbruster [IF_IDE] = "ide", 811960966dSMarkus Armbruster [IF_SCSI] = "scsi", 821960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 831960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 841960966dSMarkus Armbruster [IF_MTD] = "mtd", 851960966dSMarkus Armbruster [IF_SD] = "sd", 861960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 871960966dSMarkus Armbruster [IF_XEN] = "xen", 881960966dSMarkus Armbruster }; 891960966dSMarkus Armbruster 9021dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = { 9127d6bf40SMarkus Armbruster /* 9227d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 9327d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 9427d6bf40SMarkus Armbruster * 95547cb157SWei Jiangang * All controllers used to implement if=T drives need to support 9627d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 9727d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 9827d6bf40SMarkus Armbruster * values. 9927d6bf40SMarkus Armbruster * 10027d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 10127d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 10227d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 10327d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 10427d6bf40SMarkus Armbruster */ 10527d6bf40SMarkus Armbruster [IF_IDE] = 2, 10627d6bf40SMarkus Armbruster [IF_SCSI] = 7, 1071960966dSMarkus Armbruster }; 1081960966dSMarkus Armbruster 10921dff8cfSJohn Snow /** 11021dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides 11121dff8cfSJohn Snow * of the default, global values. 11221dff8cfSJohn Snow */ 11321dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs) 11421dff8cfSJohn Snow { 11518e46a03SMarkus Armbruster BlockBackend *blk; 11621dff8cfSJohn Snow DriveInfo *dinfo; 11721dff8cfSJohn Snow 11821dff8cfSJohn Snow if (max_devs <= 0) { 11921dff8cfSJohn Snow return; 12021dff8cfSJohn Snow } 12121dff8cfSJohn Snow 12218e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 12318e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 12421dff8cfSJohn Snow if (dinfo->type == type) { 12521dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of" 12621dff8cfSJohn Snow " the %s interface, because a drive of that type has" 12721dff8cfSJohn Snow " already been added.\n", if_name[type]); 12821dff8cfSJohn Snow g_assert_not_reached(); 12921dff8cfSJohn Snow } 13021dff8cfSJohn Snow } 13121dff8cfSJohn Snow 13221dff8cfSJohn Snow if_max_devs[type] = max_devs; 13321dff8cfSJohn Snow } 13421dff8cfSJohn Snow 13514bafc54SMarkus Armbruster /* 13614bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 13714bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 13814bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 13914bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 14014bafc54SMarkus Armbruster * when deletion is actually safe. 14114bafc54SMarkus Armbruster */ 1424be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk) 14314bafc54SMarkus Armbruster { 14418e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 1458164102fSVladimir Sementsov-Ogievskiy BlockJob *job; 14614bafc54SMarkus Armbruster 14726f8b3a8SMarkus Armbruster if (!dinfo) { 1482d246f01SKevin Wolf return; 1492d246f01SKevin Wolf } 1502d246f01SKevin Wolf 1518164102fSVladimir Sementsov-Ogievskiy for (job = block_job_next(NULL); job; job = block_job_next(job)) { 1528164102fSVladimir Sementsov-Ogievskiy if (block_job_has_bdrv(job, blk_bs(blk))) { 1538164102fSVladimir Sementsov-Ogievskiy AioContext *aio_context = job->job.aio_context; 15491fddb0dSStefan Hajnoczi aio_context_acquire(aio_context); 15591fddb0dSStefan Hajnoczi 1568164102fSVladimir Sementsov-Ogievskiy job_cancel(&job->job, false); 15791fddb0dSStefan Hajnoczi 15891fddb0dSStefan Hajnoczi aio_context_release(aio_context); 1595433c24fSMax Reitz } 1608164102fSVladimir Sementsov-Ogievskiy } 16191fddb0dSStefan Hajnoczi 16214bafc54SMarkus Armbruster dinfo->auto_del = 1; 16314bafc54SMarkus Armbruster } 16414bafc54SMarkus Armbruster 1654be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk) 16614bafc54SMarkus Armbruster { 16718e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 16814bafc54SMarkus Armbruster 1690fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 170efaa7c4eSMax Reitz monitor_remove_blk(blk); 171b9fe8a7aSMarkus Armbruster blk_unref(blk); 17214bafc54SMarkus Armbruster } 17314bafc54SMarkus Armbruster } 17414bafc54SMarkus Armbruster 175d8f94e1bSJohn Snow /** 176d8f94e1bSJohn Snow * Returns the current mapping of how many units per bus 177d8f94e1bSJohn Snow * a particular interface can support. 178d8f94e1bSJohn Snow * 179d8f94e1bSJohn Snow * A positive integer indicates n units per bus. 180d8f94e1bSJohn Snow * 0 implies the mapping has not been established. 181d8f94e1bSJohn Snow * -1 indicates an invalid BlockInterfaceType was given. 182d8f94e1bSJohn Snow */ 183d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type) 184d8f94e1bSJohn Snow { 185d8f94e1bSJohn Snow if (type >= IF_IDE && type < IF_COUNT) { 186d8f94e1bSJohn Snow return if_max_devs[type]; 187d8f94e1bSJohn Snow } 188d8f94e1bSJohn Snow 189d8f94e1bSJohn Snow return -1; 190d8f94e1bSJohn Snow } 191d8f94e1bSJohn Snow 192505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 193505a7fb1SMarkus Armbruster { 194505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 195505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 196505a7fb1SMarkus Armbruster } 197505a7fb1SMarkus Armbruster 198505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 199505a7fb1SMarkus Armbruster { 200505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 201505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 202505a7fb1SMarkus Armbruster } 203505a7fb1SMarkus Armbruster 2042292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 2052292ddaeSMarkus Armbruster { 20670b94331SMarkus Armbruster return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 2072292ddaeSMarkus Armbruster } 2082292ddaeSMarkus Armbruster 2092292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 2105645b0f4SMarkus Armbruster const char *optstr) 211666daa68SMarkus Armbruster { 212666daa68SMarkus Armbruster QemuOpts *opts; 213666daa68SMarkus Armbruster 2142292ddaeSMarkus Armbruster opts = drive_def(optstr); 215666daa68SMarkus Armbruster if (!opts) { 216666daa68SMarkus Armbruster return NULL; 217666daa68SMarkus Armbruster } 2182292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 219f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort); 2202292ddaeSMarkus Armbruster } 2212292ddaeSMarkus Armbruster if (index >= 0) { 222a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort); 2232292ddaeSMarkus Armbruster } 224666daa68SMarkus Armbruster if (file) 225f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort); 226666daa68SMarkus Armbruster return opts; 227666daa68SMarkus Armbruster } 228666daa68SMarkus Armbruster 229666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 230666daa68SMarkus Armbruster { 23118e46a03SMarkus Armbruster BlockBackend *blk; 232666daa68SMarkus Armbruster DriveInfo *dinfo; 233666daa68SMarkus Armbruster 23418e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 23518e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 23618e46a03SMarkus Armbruster if (dinfo && dinfo->type == type 23718e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) { 238666daa68SMarkus Armbruster return dinfo; 239666daa68SMarkus Armbruster } 24018e46a03SMarkus Armbruster } 241666daa68SMarkus Armbruster 242666daa68SMarkus Armbruster return NULL; 243666daa68SMarkus Armbruster } 244666daa68SMarkus Armbruster 245720b8dc0SMarkus Armbruster void drive_check_orphaned(void) 246a66c9dc7SJohn Snow { 24718e46a03SMarkus Armbruster BlockBackend *blk; 248a66c9dc7SJohn Snow DriveInfo *dinfo; 249664cc623SMarkus Armbruster Location loc; 250720b8dc0SMarkus Armbruster bool orphans = false; 251a66c9dc7SJohn Snow 25218e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 25318e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 254a7f53e26SMarkus Armbruster if (!blk_get_attached_dev(blk) && !dinfo->is_default && 255a66c9dc7SJohn Snow dinfo->type != IF_NONE) { 256664cc623SMarkus Armbruster loc_push_none(&loc); 257664cc623SMarkus Armbruster qemu_opts_loc_restore(dinfo->opts); 258720b8dc0SMarkus Armbruster error_report("machine type does not support" 259664cc623SMarkus Armbruster " if=%s,bus=%d,unit=%d", 2605433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit); 261664cc623SMarkus Armbruster loc_pop(&loc); 262720b8dc0SMarkus Armbruster orphans = true; 263a66c9dc7SJohn Snow } 264a66c9dc7SJohn Snow } 265a66c9dc7SJohn Snow 266720b8dc0SMarkus Armbruster if (orphans) { 267720b8dc0SMarkus Armbruster exit(1); 268720b8dc0SMarkus Armbruster } 269a66c9dc7SJohn Snow } 270a66c9dc7SJohn Snow 271f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 272f1bd51acSMarkus Armbruster { 273f1bd51acSMarkus Armbruster return drive_get(type, 274f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 275f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 276f1bd51acSMarkus Armbruster } 277f1bd51acSMarkus Armbruster 278666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 279666daa68SMarkus Armbruster { 280666daa68SMarkus Armbruster int max_bus; 28118e46a03SMarkus Armbruster BlockBackend *blk; 282666daa68SMarkus Armbruster DriveInfo *dinfo; 283666daa68SMarkus Armbruster 284666daa68SMarkus Armbruster max_bus = -1; 28518e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 28618e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 28718e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 288666daa68SMarkus Armbruster max_bus = dinfo->bus; 289666daa68SMarkus Armbruster } 29018e46a03SMarkus Armbruster } 291666daa68SMarkus Armbruster return max_bus; 292666daa68SMarkus Armbruster } 293666daa68SMarkus Armbruster 29413839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 29513839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 29613839974SMarkus Armbruster appropriate bus. */ 29713839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 29813839974SMarkus Armbruster { 29913839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 30013839974SMarkus Armbruster 30113839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 30213839974SMarkus Armbruster } 30313839974SMarkus Armbruster 304666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 305666daa68SMarkus Armbruster { 306cdcd4361SMarkus Armbruster qemu_printf(" %s", name); 307666daa68SMarkus Armbruster } 308666daa68SMarkus Armbruster 309aa398a5cSStefan Hajnoczi typedef struct { 310aa398a5cSStefan Hajnoczi QEMUBH *bh; 311fa510ebfSFam Zheng BlockDriverState *bs; 312fa510ebfSFam Zheng } BDRVPutRefBH; 313aa398a5cSStefan Hajnoczi 314b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 315666daa68SMarkus Armbruster { 316666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 31792aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 318666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 31992aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 320666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 32192aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 322666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 32392aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 324666daa68SMarkus Armbruster } else { 325b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 326666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 327666daa68SMarkus Armbruster return -1; 328666daa68SMarkus Armbruster } 329666daa68SMarkus Armbruster } 330666daa68SMarkus Armbruster 33140119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 33240119effSAlberto Garcia Error **errp) 33340119effSAlberto Garcia { 33440119effSAlberto Garcia const QListEntry *entry; 33540119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 33640119effSAlberto Garcia switch (qobject_type(entry->value)) { 33740119effSAlberto Garcia 33840119effSAlberto Garcia case QTYPE_QSTRING: { 33940119effSAlberto Garcia unsigned long long length; 3407dc847ebSMax Reitz const char *str = qstring_get_str(qobject_to(QString, 3417dc847ebSMax Reitz entry->value)); 34240119effSAlberto Garcia if (parse_uint_full(str, &length, 10) == 0 && 34340119effSAlberto Garcia length > 0 && length <= UINT_MAX) { 34440119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 34540119effSAlberto Garcia } else { 34640119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str); 34740119effSAlberto Garcia return false; 34840119effSAlberto Garcia } 34940119effSAlberto Garcia break; 35040119effSAlberto Garcia } 35140119effSAlberto Garcia 35201b2ffceSMarc-André Lureau case QTYPE_QNUM: { 3537dc847ebSMax Reitz int64_t length = qnum_get_int(qobject_to(QNum, entry->value)); 35401b2ffceSMarc-André Lureau 35540119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) { 35640119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 35740119effSAlberto Garcia } else { 35840119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length); 35940119effSAlberto Garcia return false; 36040119effSAlberto Garcia } 36140119effSAlberto Garcia break; 36240119effSAlberto Garcia } 36340119effSAlberto Garcia 36440119effSAlberto Garcia default: 36540119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid"); 36640119effSAlberto Garcia return false; 36740119effSAlberto Garcia } 36840119effSAlberto Garcia } 36940119effSAlberto Garcia return true; 37040119effSAlberto Garcia } 37140119effSAlberto Garcia 37233cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 37333cb7dc8SKevin Wolf 374fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 375fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 376fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 377fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 378fbf8175eSMax Reitz { 379fbf8175eSMax Reitz Error *local_error = NULL; 380fbf8175eSMax Reitz const char *aio; 381fbf8175eSMax Reitz 382fbf8175eSMax Reitz if (bdrv_flags) { 383fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 384fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 385fbf8175eSMax Reitz } 386fbf8175eSMax Reitz 387fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 388fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 389fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 390fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 391fbf8175eSMax Reitz /* this is the default */ 392fbf8175eSMax Reitz } else { 393fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 394fbf8175eSMax Reitz return; 395fbf8175eSMax Reitz } 396fbf8175eSMax Reitz } 397fbf8175eSMax Reitz } 398fbf8175eSMax Reitz 399fbf8175eSMax Reitz /* disk I/O throttling */ 400fbf8175eSMax Reitz if (throttling_group) { 401fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 402fbf8175eSMax Reitz } 403fbf8175eSMax Reitz 404fbf8175eSMax Reitz if (throttle_cfg) { 4051588ab5dSAlberto Garcia throttle_config_init(throttle_cfg); 406fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 407fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 408fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 409fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 410fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 411fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 412fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 413fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 414fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 415fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 416fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 417fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 418fbf8175eSMax Reitz 419fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 420fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 421fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 422fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 423fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 424fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 425fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 426fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 427fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 428fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 429fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 430fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 431fbf8175eSMax Reitz 4328a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = 4338a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); 4348a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = 4358a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); 4368a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = 4378a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); 4388a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = 4398a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); 4408a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = 4418a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); 4428a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = 4438a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); 4448a0fc18dSAlberto Garcia 445fbf8175eSMax Reitz throttle_cfg->op_size = 446fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 447fbf8175eSMax Reitz 448d5851089SAlberto Garcia if (!throttle_is_valid(throttle_cfg, errp)) { 449fbf8175eSMax Reitz return; 450fbf8175eSMax Reitz } 451fbf8175eSMax Reitz } 452fbf8175eSMax Reitz 453fbf8175eSMax Reitz if (detect_zeroes) { 454fbf8175eSMax Reitz *detect_zeroes = 455f7abe0ecSMarc-André Lureau qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, 456fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 457fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 458fbf8175eSMax Reitz &local_error); 459fbf8175eSMax Reitz if (local_error) { 460fbf8175eSMax Reitz error_propagate(errp, local_error); 461fbf8175eSMax Reitz return; 462fbf8175eSMax Reitz } 463fbf8175eSMax Reitz } 464fbf8175eSMax Reitz } 465fbf8175eSMax Reitz 466f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 46718e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 468b681072dSKevin Wolf Error **errp) 469666daa68SMarkus Armbruster { 470666daa68SMarkus Armbruster const char *buf; 471666daa68SMarkus Armbruster int bdrv_flags = 0; 472666daa68SMarkus Armbruster int on_read_error, on_write_error; 473362e9299SAlberto Garcia bool account_invalid, account_failed; 474f87a0e29SAlberto Garcia bool writethrough, read_only; 47526f54e9aSMarkus Armbruster BlockBackend *blk; 476a0f1eab1SMarkus Armbruster BlockDriverState *bs; 477cc0681c4SBenoît Canet ThrottleConfig cfg; 478666daa68SMarkus Armbruster int snapshot = 0; 479c546194fSStefan Hajnoczi Error *error = NULL; 4800006383eSKevin Wolf QemuOpts *opts; 48140119effSAlberto Garcia QDict *interval_dict = NULL; 48240119effSAlberto Garcia QList *interval_list = NULL; 4830006383eSKevin Wolf const char *id; 484fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 485fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 486fbf8175eSMax Reitz const char *throttling_group = NULL; 487666daa68SMarkus Armbruster 488f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 489f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 490f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4910006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 49284d18f06SMarkus Armbruster if (error) { 493b681072dSKevin Wolf error_propagate(errp, error); 4946376f952SMarkus Armbruster goto err_no_opts; 4950006383eSKevin Wolf } 4960006383eSKevin Wolf 4970006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49884d18f06SMarkus Armbruster if (error) { 499b681072dSKevin Wolf error_propagate(errp, error); 500ec9c10d2SStefan Hajnoczi goto early_err; 5010006383eSKevin Wolf } 5020006383eSKevin Wolf 5030006383eSKevin Wolf if (id) { 5040006383eSKevin Wolf qdict_del(bs_opts, "id"); 5050006383eSKevin Wolf } 5060006383eSKevin Wolf 507666daa68SMarkus Armbruster /* extract parameters */ 508666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 509666daa68SMarkus Armbruster 510362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 511362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 512362e9299SAlberto Garcia 513e4b24b49SKevin Wolf writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); 514e4b24b49SKevin Wolf 515ff356ee4SAlberto Garcia id = qemu_opts_id(opts); 516ff356ee4SAlberto Garcia 51740119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51840119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51940119effSAlberto Garcia 52040119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 52140119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 52240119effSAlberto Garcia qdict_first(interval_dict)->key); 52340119effSAlberto Garcia goto early_err; 52440119effSAlberto Garcia } 5252be5506fSAlberto Garcia 526fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 527fbf8175eSMax Reitz &detect_zeroes, &error); 528fbf8175eSMax Reitz if (error) { 529fbf8175eSMax Reitz error_propagate(errp, error); 530ec9c10d2SStefan Hajnoczi goto early_err; 531a9384affSPaolo Bonzini } 532666daa68SMarkus Armbruster 533666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 534c8057f95SPeter Maydell if (is_help_option(buf)) { 535cdcd4361SMarkus Armbruster qemu_printf("Supported formats:"); 5369ac404c5SAndrey Shinkevich bdrv_iterate_format(bdrv_format_print, NULL, false); 537cdcd4361SMarkus Armbruster qemu_printf("\nSupported formats (read-only):"); 5389ac404c5SAndrey Shinkevich bdrv_iterate_format(bdrv_format_print, NULL, true); 539cdcd4361SMarkus Armbruster qemu_printf("\n"); 540ec9c10d2SStefan Hajnoczi goto early_err; 541666daa68SMarkus Armbruster } 54274fe54f2SKevin Wolf 543e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 544e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 545ec9c10d2SStefan Hajnoczi goto early_err; 5466db5f5d6SMike Qiu } 54746f5ac20SEric Blake qdict_put_str(bs_opts, "driver", buf); 548666daa68SMarkus Armbruster } 549666daa68SMarkus Armbruster 55092aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 551666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 552b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 55384d18f06SMarkus Armbruster if (error) { 554b681072dSKevin Wolf error_propagate(errp, error); 555ec9c10d2SStefan Hajnoczi goto early_err; 556666daa68SMarkus Armbruster } 557666daa68SMarkus Armbruster } 558666daa68SMarkus Armbruster 55992aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 560666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 561b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 56284d18f06SMarkus Armbruster if (error) { 563b681072dSKevin Wolf error_propagate(errp, error); 564ec9c10d2SStefan Hajnoczi goto early_err; 565666daa68SMarkus Armbruster } 566666daa68SMarkus Armbruster } 567666daa68SMarkus Armbruster 568666daa68SMarkus Armbruster if (snapshot) { 56991a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 570666daa68SMarkus Armbruster } 571666daa68SMarkus Armbruster 572f87a0e29SAlberto Garcia read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false); 573f87a0e29SAlberto Garcia 5745ec18f8cSMax Reitz /* init */ 57539c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5765ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5775ec18f8cSMax Reitz 578d861ab3aSKevin Wolf blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); 5795ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5805ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 581f87a0e29SAlberto Garcia blk_rs->read_only = read_only; 5825ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5835ec18f8cSMax Reitz 584cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 5855ec18f8cSMax Reitz } else { 5865ec18f8cSMax Reitz if (file && !*file) { 5875ec18f8cSMax Reitz file = NULL; 5885ec18f8cSMax Reitz } 5895ec18f8cSMax Reitz 59091a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 59191a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 59291a097e7SKevin Wolf * Apply the defaults here instead. */ 59391a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 59491a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 595f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, 596f87a0e29SAlberto Garcia read_only ? "on" : "off"); 5979384a444SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on"); 59872e775c7SKevin Wolf assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); 59991a097e7SKevin Wolf 60012d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 60112d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 60212d5ee3aSKevin Wolf } 60312d5ee3aSKevin Wolf 604efaa7c4eSMax Reitz blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); 605e4342ce5SMax Reitz if (!blk) { 606e4342ce5SMax Reitz goto err_no_bs_opts; 607e4342ce5SMax Reitz } 608e4342ce5SMax Reitz bs = blk_bs(blk); 6090006383eSKevin Wolf 610e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 611e4342ce5SMax Reitz 6129caa6f3dSPaolo Bonzini block_acct_setup(blk_get_stats(blk), account_invalid, account_failed); 6132be5506fSAlberto Garcia 61440119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6152be5506fSAlberto Garcia blk_unref(blk); 6162be5506fSAlberto Garcia blk = NULL; 6172be5506fSAlberto Garcia goto err_no_bs_opts; 6182be5506fSAlberto Garcia } 6192be5506fSAlberto Garcia } 6205ec18f8cSMax Reitz 6217ca7f0f6SKevin Wolf /* disk I/O throttling */ 6227ca7f0f6SKevin Wolf if (throttle_enabled(&cfg)) { 6237ca7f0f6SKevin Wolf if (!throttling_group) { 624ff356ee4SAlberto Garcia throttling_group = id; 6257ca7f0f6SKevin Wolf } 6267ca7f0f6SKevin Wolf blk_io_limits_enable(blk, throttling_group); 6277ca7f0f6SKevin Wolf blk_set_io_limits(blk, &cfg); 6287ca7f0f6SKevin Wolf } 6297ca7f0f6SKevin Wolf 630e4b24b49SKevin Wolf blk_set_enable_write_cache(blk, !writethrough); 6315ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6320006383eSKevin Wolf 633ff356ee4SAlberto Garcia if (!monitor_add_blk(blk, id, errp)) { 634efaa7c4eSMax Reitz blk_unref(blk); 635efaa7c4eSMax Reitz blk = NULL; 636efaa7c4eSMax Reitz goto err_no_bs_opts; 637efaa7c4eSMax Reitz } 638efaa7c4eSMax Reitz 639e4342ce5SMax Reitz err_no_bs_opts: 6400006383eSKevin Wolf qemu_opts_del(opts); 641cb3e7f08SMarc-André Lureau qobject_unref(interval_dict); 642cb3e7f08SMarc-André Lureau qobject_unref(interval_list); 64318e46a03SMarkus Armbruster return blk; 644a9ae2bffSMarkus Armbruster 645ec9c10d2SStefan Hajnoczi early_err: 646ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 647cb3e7f08SMarc-André Lureau qobject_unref(interval_dict); 648cb3e7f08SMarc-André Lureau qobject_unref(interval_list); 6496376f952SMarkus Armbruster err_no_opts: 650cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 651a9ae2bffSMarkus Armbruster return NULL; 652666daa68SMarkus Armbruster } 653666daa68SMarkus Armbruster 654bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 655bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 656bd745e23SMax Reitz { 657bd745e23SMax Reitz int bdrv_flags = 0; 658bd745e23SMax Reitz 659a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 660a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults. 661a81d6164SKevin Wolf * Apply the defaults here instead. */ 662a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 663a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 664f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off"); 665a81d6164SKevin Wolf 66612d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 66712d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 66812d5ee3aSKevin Wolf } 66912d5ee3aSKevin Wolf 67074e1ae7cSKevin Wolf return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); 671bd745e23SMax Reitz } 672bd745e23SMax Reitz 6739c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void) 6749c4218e9SMax Reitz { 6759c4218e9SMax Reitz BlockDriverState *bs, *next_bs; 6769c4218e9SMax Reitz 6779c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { 6789c4218e9SMax Reitz AioContext *ctx = bdrv_get_aio_context(bs); 6799c4218e9SMax Reitz 6809c4218e9SMax Reitz aio_context_acquire(ctx); 6819c4218e9SMax Reitz bdrv_unref(bs); 6829c4218e9SMax Reitz aio_context_release(ctx); 6839c4218e9SMax Reitz } 6849c4218e9SMax Reitz } 6859c4218e9SMax Reitz 686262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */ 687262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) 688262b4e8fSMax Reitz { 689262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list) 690262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states); 691262b4e8fSMax Reitz } 692262b4e8fSMax Reitz 6935abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 6945abbf0eeSKevin Wolf Error **errp) 69557975222SKevin Wolf { 69657975222SKevin Wolf const char *value; 69757975222SKevin Wolf 69857975222SKevin Wolf value = qemu_opt_get(opts, from); 69957975222SKevin Wolf if (value) { 7005abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 7015abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 7025abbf0eeSKevin Wolf "same time", to, from); 7035abbf0eeSKevin Wolf return; 7045abbf0eeSKevin Wolf } 70520d6cd47SJun Li } 70620d6cd47SJun Li 70720d6cd47SJun Li /* rename all items in opts */ 70820d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 709f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 71057975222SKevin Wolf qemu_opt_unset(opts, from); 71157975222SKevin Wolf } 71257975222SKevin Wolf } 71357975222SKevin Wolf 71433cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 71533cb7dc8SKevin Wolf .name = "drive", 71633cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 71733cb7dc8SKevin Wolf .desc = { 71833cb7dc8SKevin Wolf { 71987a899c5SKevin Wolf .name = "bus", 72087a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 72187a899c5SKevin Wolf .help = "bus number", 72287a899c5SKevin Wolf },{ 72387a899c5SKevin Wolf .name = "unit", 72487a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 72587a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 72687a899c5SKevin Wolf },{ 72787a899c5SKevin Wolf .name = "index", 72887a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 72987a899c5SKevin Wolf .help = "index number", 73087a899c5SKevin Wolf },{ 73133cb7dc8SKevin Wolf .name = "media", 73233cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 73333cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 734593d464bSKevin Wolf },{ 735593d464bSKevin Wolf .name = "if", 736593d464bSKevin Wolf .type = QEMU_OPT_STRING, 737593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 738b41a7338SKevin Wolf },{ 739d095b465SMax Reitz .name = "file", 740d095b465SMax Reitz .type = QEMU_OPT_STRING, 741d095b465SMax Reitz .help = "file name", 74233cb7dc8SKevin Wolf }, 7430ebd24e0SKevin Wolf 7440ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 7450ebd24e0SKevin Wolf { 7464e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 7470ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7480ebd24e0SKevin Wolf .help = "open drive file as read-only", 7490ebd24e0SKevin Wolf },{ 750ee13ed1cSKevin Wolf .name = "rerror", 751ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 752ee13ed1cSKevin Wolf .help = "read error action", 753ee13ed1cSKevin Wolf },{ 754ee13ed1cSKevin Wolf .name = "werror", 755ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 756ee13ed1cSKevin Wolf .help = "write error action", 757ee13ed1cSKevin Wolf },{ 7580ebd24e0SKevin Wolf .name = "copy-on-read", 7590ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7600ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 7610ebd24e0SKevin Wolf }, 7620ebd24e0SKevin Wolf 76333cb7dc8SKevin Wolf { /* end of list */ } 76433cb7dc8SKevin Wolf }, 76533cb7dc8SKevin Wolf }; 76633cb7dc8SKevin Wolf 767c4f26c9fSMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type, 768c4f26c9fSMarkus Armbruster Error **errp) 76957975222SKevin Wolf { 77029c4e2b5SKevin Wolf const char *value; 77118e46a03SMarkus Armbruster BlockBackend *blk; 77233cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 773f298d071SKevin Wolf QDict *bs_opts; 77433cb7dc8SKevin Wolf QemuOpts *legacy_opts; 77533cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 776593d464bSKevin Wolf BlockInterfaceType type; 77787a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 778ee13ed1cSKevin Wolf const char *werror, *rerror; 779a7fdbcf0SFam Zheng bool read_only = false; 780a7fdbcf0SFam Zheng bool copy_on_read; 781d095b465SMax Reitz const char *filename; 78233cb7dc8SKevin Wolf Error *local_err = NULL; 783247147fbSKevin Wolf int i; 78429c4e2b5SKevin Wolf 78557975222SKevin Wolf /* Change legacy command line options into QMP ones */ 786247147fbSKevin Wolf static const struct { 787247147fbSKevin Wolf const char *from; 788247147fbSKevin Wolf const char *to; 789247147fbSKevin Wolf } opt_renames[] = { 790247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 791247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 792247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 79357975222SKevin Wolf 794247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 795247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 796247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 79757975222SKevin Wolf 798247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 799247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 800247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8013e9fab69SBenoît Canet 802247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 803247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 804247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8053e9fab69SBenoît Canet 806247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8072024c1dfSBenoît Canet 80876f4afb4SAlberto Garcia { "group", "throttling.group" }, 80976f4afb4SAlberto Garcia 8104e200cf8SAlberto Garcia { "readonly", BDRV_OPT_READ_ONLY }, 811247147fbSKevin Wolf }; 812247147fbSKevin Wolf 813247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8145abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8155abbf0eeSKevin Wolf &local_err); 8165abbf0eeSKevin Wolf if (local_err) { 817c4f26c9fSMarkus Armbruster error_propagate(errp, local_err); 8185abbf0eeSKevin Wolf return NULL; 8195abbf0eeSKevin Wolf } 820247147fbSKevin Wolf } 8210f227a94SKevin Wolf 82229c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 82329c4e2b5SKevin Wolf if (value) { 82429c4e2b5SKevin Wolf int flags = 0; 82504feb4a5SKevin Wolf bool writethrough; 82629c4e2b5SKevin Wolf 82704feb4a5SKevin Wolf if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { 828c4f26c9fSMarkus Armbruster error_setg(errp, "invalid cache option"); 82929c4e2b5SKevin Wolf return NULL; 83029c4e2b5SKevin Wolf } 83129c4e2b5SKevin Wolf 83229c4e2b5SKevin Wolf /* Specific options take precedence */ 83354861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 83454861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 83504feb4a5SKevin Wolf !writethrough, &error_abort); 83629c4e2b5SKevin Wolf } 83754861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 83854861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 839cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 84029c4e2b5SKevin Wolf } 84154861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 84254861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 843cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 84429c4e2b5SKevin Wolf } 84529c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 84629c4e2b5SKevin Wolf } 84729c4e2b5SKevin Wolf 848f298d071SKevin Wolf /* Get a QDict for processing the options */ 849f298d071SKevin Wolf bs_opts = qdict_new(); 850f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 851f298d071SKevin Wolf 85287ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 85387ea75d5SPeter Crosthwaite &error_abort); 85433cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 85584d18f06SMarkus Armbruster if (local_err) { 856c4f26c9fSMarkus Armbruster error_propagate(errp, local_err); 85733cb7dc8SKevin Wolf goto fail; 85833cb7dc8SKevin Wolf } 85933cb7dc8SKevin Wolf 86033cb7dc8SKevin Wolf /* Media type */ 86133cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 86233cb7dc8SKevin Wolf if (value) { 86333cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 86433cb7dc8SKevin Wolf media = MEDIA_DISK; 86533cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 86633cb7dc8SKevin Wolf media = MEDIA_CDROM; 867a7fdbcf0SFam Zheng read_only = true; 86833cb7dc8SKevin Wolf } else { 869c4f26c9fSMarkus Armbruster error_setg(errp, "'%s' invalid media", value); 87033cb7dc8SKevin Wolf goto fail; 87133cb7dc8SKevin Wolf } 87233cb7dc8SKevin Wolf } 87333cb7dc8SKevin Wolf 8740ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 8754e200cf8SAlberto Garcia read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false); 8760ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 8770ebd24e0SKevin Wolf 8780ebd24e0SKevin Wolf if (read_only && copy_on_read) { 8793dc6f869SAlistair Francis warn_report("disabling copy-on-read on read-only drive"); 8800ebd24e0SKevin Wolf copy_on_read = false; 8810ebd24e0SKevin Wolf } 8820ebd24e0SKevin Wolf 88346f5ac20SEric Blake qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off"); 88446f5ac20SEric Blake qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off"); 8850ebd24e0SKevin Wolf 886593d464bSKevin Wolf /* Controller type */ 887593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 888593d464bSKevin Wolf if (value) { 889593d464bSKevin Wolf for (type = 0; 890593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 891593d464bSKevin Wolf type++) { 892593d464bSKevin Wolf } 893593d464bSKevin Wolf if (type == IF_COUNT) { 894c4f26c9fSMarkus Armbruster error_setg(errp, "unsupported bus type '%s'", value); 895593d464bSKevin Wolf goto fail; 896593d464bSKevin Wolf } 897593d464bSKevin Wolf } else { 898593d464bSKevin Wolf type = block_default_type; 899593d464bSKevin Wolf } 900593d464bSKevin Wolf 90187a899c5SKevin Wolf /* Device address specified by bus/unit or index. 90287a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 90387a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 90487a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 90587a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 90687a899c5SKevin Wolf 90787a899c5SKevin Wolf max_devs = if_max_devs[type]; 90887a899c5SKevin Wolf 90987a899c5SKevin Wolf if (index != -1) { 91087a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 911c4f26c9fSMarkus Armbruster error_setg(errp, "index cannot be used with bus and unit"); 91287a899c5SKevin Wolf goto fail; 91387a899c5SKevin Wolf } 91487a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 91587a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 91687a899c5SKevin Wolf } 91787a899c5SKevin Wolf 91887a899c5SKevin Wolf if (unit_id == -1) { 91987a899c5SKevin Wolf unit_id = 0; 92087a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 92187a899c5SKevin Wolf unit_id++; 92287a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 92387a899c5SKevin Wolf unit_id -= max_devs; 92487a899c5SKevin Wolf bus_id++; 92587a899c5SKevin Wolf } 92687a899c5SKevin Wolf } 92787a899c5SKevin Wolf } 92887a899c5SKevin Wolf 92987a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 930c4f26c9fSMarkus Armbruster error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1); 93187a899c5SKevin Wolf goto fail; 93287a899c5SKevin Wolf } 93387a899c5SKevin Wolf 93487a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 935c4f26c9fSMarkus Armbruster error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists", 93687a899c5SKevin Wolf bus_id, unit_id, index); 93787a899c5SKevin Wolf goto fail; 93887a899c5SKevin Wolf } 93987a899c5SKevin Wolf 94087a899c5SKevin Wolf /* no id supplied -> create one */ 94187a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 94287a899c5SKevin Wolf char *new_id; 94387a899c5SKevin Wolf const char *mediastr = ""; 94487a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 94587a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 94687a899c5SKevin Wolf } 94787a899c5SKevin Wolf if (max_devs) { 94887a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 94987a899c5SKevin Wolf mediastr, unit_id); 95087a899c5SKevin Wolf } else { 95187a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 95287a899c5SKevin Wolf mediastr, unit_id); 95387a899c5SKevin Wolf } 95446f5ac20SEric Blake qdict_put_str(bs_opts, "id", new_id); 95587a899c5SKevin Wolf g_free(new_id); 95687a899c5SKevin Wolf } 95787a899c5SKevin Wolf 958394c7d4dSKevin Wolf /* Add virtio block device */ 959394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 960394c7d4dSKevin Wolf QemuOpts *devopts; 96187ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 96287ea75d5SPeter Crosthwaite &error_abort); 963394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 9641f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 965394c7d4dSKevin Wolf } else { 966f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 967394c7d4dSKevin Wolf } 968f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 969f43e47dbSMarkus Armbruster &error_abort); 970394c7d4dSKevin Wolf } 971394c7d4dSKevin Wolf 972d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 973d095b465SMax Reitz 974ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 975ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 976ee13ed1cSKevin Wolf if (werror != NULL) { 977ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 978ee13ed1cSKevin Wolf type != IF_NONE) { 979c4f26c9fSMarkus Armbruster error_setg(errp, "werror is not supported by this bus type"); 980ee13ed1cSKevin Wolf goto fail; 981ee13ed1cSKevin Wolf } 98246f5ac20SEric Blake qdict_put_str(bs_opts, "werror", werror); 983ee13ed1cSKevin Wolf } 984ee13ed1cSKevin Wolf 985ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 986ee13ed1cSKevin Wolf if (rerror != NULL) { 987ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 988ee13ed1cSKevin Wolf type != IF_NONE) { 989c4f26c9fSMarkus Armbruster error_setg(errp, "rerror is not supported by this bus type"); 990ee13ed1cSKevin Wolf goto fail; 991ee13ed1cSKevin Wolf } 99246f5ac20SEric Blake qdict_put_str(bs_opts, "rerror", rerror); 993ee13ed1cSKevin Wolf } 994ee13ed1cSKevin Wolf 9952d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 99618e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 9973cb0e25cSMarkus Armbruster bs_opts = NULL; 99818e46a03SMarkus Armbruster if (!blk) { 999c4f26c9fSMarkus Armbruster error_propagate(errp, local_err); 10002d246f01SKevin Wolf goto fail; 1001b681072dSKevin Wolf } else { 100284d18f06SMarkus Armbruster assert(!local_err); 10032d246f01SKevin Wolf } 10042d246f01SKevin Wolf 100526f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 100626f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1007f298d071SKevin Wolf dinfo->opts = all_opts; 10082d246f01SKevin Wolf 1009ee13ed1cSKevin Wolf dinfo->type = type; 101087a899c5SKevin Wolf dinfo->bus = bus_id; 101187a899c5SKevin Wolf dinfo->unit = unit_id; 1012bcf83158SKevin Wolf 101326f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 101426f8b3a8SMarkus Armbruster 1015e34ef046SKevin Wolf switch(type) { 1016e34ef046SKevin Wolf case IF_IDE: 1017e34ef046SKevin Wolf case IF_SCSI: 1018e34ef046SKevin Wolf case IF_XEN: 1019e34ef046SKevin Wolf case IF_NONE: 1020e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1021e34ef046SKevin Wolf break; 1022e34ef046SKevin Wolf default: 1023e34ef046SKevin Wolf break; 1024e34ef046SKevin Wolf } 1025e34ef046SKevin Wolf 10262d246f01SKevin Wolf fail: 102733cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 1028cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 10292d246f01SKevin Wolf return dinfo; 103057975222SKevin Wolf } 103157975222SKevin Wolf 1032b6c1bae5SKevin Wolf static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) 1033b6c1bae5SKevin Wolf { 1034b6c1bae5SKevin Wolf BlockDriverState *bs; 1035b6c1bae5SKevin Wolf 1036b6c1bae5SKevin Wolf bs = bdrv_lookup_bs(name, name, errp); 1037b6c1bae5SKevin Wolf if (bs == NULL) { 1038b6c1bae5SKevin Wolf return NULL; 1039b6c1bae5SKevin Wolf } 1040b6c1bae5SKevin Wolf 1041b6c1bae5SKevin Wolf if (!bdrv_is_root_node(bs)) { 1042b6c1bae5SKevin Wolf error_setg(errp, "Need a root block node"); 1043b6c1bae5SKevin Wolf return NULL; 1044b6c1bae5SKevin Wolf } 1045b6c1bae5SKevin Wolf 1046b6c1bae5SKevin Wolf if (!bdrv_is_inserted(bs)) { 1047b6c1bae5SKevin Wolf error_setg(errp, "Device has no medium"); 1048b6c1bae5SKevin Wolf return NULL; 1049b6c1bae5SKevin Wolf } 1050b6c1bae5SKevin Wolf 1051b6c1bae5SKevin Wolf return bs; 1052b6c1bae5SKevin Wolf } 1053b6c1bae5SKevin Wolf 1054b33945cfSKevin Wolf static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id, 1055b33945cfSKevin Wolf Error **errp) 1056b33945cfSKevin Wolf { 1057b33945cfSKevin Wolf BlockBackend *blk; 1058b33945cfSKevin Wolf 1059b33945cfSKevin Wolf if (!blk_name == !qdev_id) { 1060b33945cfSKevin Wolf error_setg(errp, "Need exactly one of 'device' and 'id'"); 1061b33945cfSKevin Wolf return NULL; 1062b33945cfSKevin Wolf } 1063b33945cfSKevin Wolf 1064b33945cfSKevin Wolf if (qdev_id) { 1065b33945cfSKevin Wolf blk = blk_by_qdev_id(qdev_id, errp); 1066b33945cfSKevin Wolf } else { 1067b33945cfSKevin Wolf blk = blk_by_name(blk_name); 1068b33945cfSKevin Wolf if (blk == NULL) { 1069b33945cfSKevin Wolf error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1070b33945cfSKevin Wolf "Device '%s' not found", blk_name); 1071b33945cfSKevin Wolf } 1072b33945cfSKevin Wolf } 1073b33945cfSKevin Wolf 1074b33945cfSKevin Wolf return blk; 1075b33945cfSKevin Wolf } 1076b33945cfSKevin Wolf 10773e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1078666daa68SMarkus Armbruster { 1079666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1080a0e8544cSFam Zheng BlockBackend *blk; 10812d3735d3SStefan Hajnoczi int ret; 10822d3735d3SStefan Hajnoczi 1083e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1084da31d594SMax Reitz ret = blk_commit_all(); 1085e8877497SStefan Hajnoczi } else { 108684aa0140SStefan Hajnoczi BlockDriverState *bs; 108784aa0140SStefan Hajnoczi AioContext *aio_context; 108884aa0140SStefan Hajnoczi 1089a0e8544cSFam Zheng blk = blk_by_name(device); 1090a0e8544cSFam Zheng if (!blk) { 109146741111SKevin Wolf error_report("Device '%s' not found", device); 1092ac59eb95SMarkus Armbruster return; 10936ab4b5abSMarkus Armbruster } 10945433c24fSMax Reitz if (!blk_is_available(blk)) { 109546741111SKevin Wolf error_report("Device '%s' has no medium", device); 10965433c24fSMax Reitz return; 10975433c24fSMax Reitz } 109884aa0140SStefan Hajnoczi 109984aa0140SStefan Hajnoczi bs = blk_bs(blk); 110084aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 110184aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 110284aa0140SStefan Hajnoczi 110384aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 110484aa0140SStefan Hajnoczi 110584aa0140SStefan Hajnoczi aio_context_release(aio_context); 11062d3735d3SStefan Hajnoczi } 110758513bdeSJeff Cody if (ret < 0) { 110846741111SKevin Wolf error_report("'commit' error for '%s': %s", device, strerror(-ret)); 1109666daa68SMarkus Armbruster } 1110666daa68SMarkus Armbruster } 1111666daa68SMarkus Armbruster 111210f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 11136cc2a415SPaolo Bonzini { 1114c8a83e85SKevin Wolf TransactionActionList list; 11156cc2a415SPaolo Bonzini 111610f75907SEric Blake list.value = action; 11176cc2a415SPaolo Bonzini list.next = NULL; 111894d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 11196cc2a415SPaolo Bonzini } 11206cc2a415SPaolo Bonzini 11210901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 11220901f67eSBenoît Canet bool has_node_name, const char *node_name, 11230901f67eSBenoît Canet const char *snapshot_file, 11240901f67eSBenoît Canet bool has_snapshot_node_name, 11250901f67eSBenoît Canet const char *snapshot_node_name, 11266106e249SLuiz Capitulino bool has_format, const char *format, 11270901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1128f8882568SJes Sorensen { 1129a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 11300901f67eSBenoît Canet .has_device = has_device, 11316cc2a415SPaolo Bonzini .device = (char *) device, 11320901f67eSBenoît Canet .has_node_name = has_node_name, 11330901f67eSBenoît Canet .node_name = (char *) node_name, 11346cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 11350901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 11360901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 11376cc2a415SPaolo Bonzini .has_format = has_format, 11386cc2a415SPaolo Bonzini .format = (char *) format, 11396cc2a415SPaolo Bonzini .has_mode = has_mode, 11406cc2a415SPaolo Bonzini .mode = mode, 11416cc2a415SPaolo Bonzini }; 114210f75907SEric Blake TransactionAction action = { 114310f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 114432bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 114510f75907SEric Blake }; 114610f75907SEric Blake blockdev_do_action(&action, errp); 1147f8882568SJes Sorensen } 1148f8882568SJes Sorensen 114943de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 115043de7e2dSAlberto Garcia Error **errp) 115143de7e2dSAlberto Garcia { 115243de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 115343de7e2dSAlberto Garcia .node = (char *) node, 115443de7e2dSAlberto Garcia .overlay = (char *) overlay 115543de7e2dSAlberto Garcia }; 115610f75907SEric Blake TransactionAction action = { 115710f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 115832bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 115910f75907SEric Blake }; 116010f75907SEric Blake blockdev_do_action(&action, errp); 116143de7e2dSAlberto Garcia } 116243de7e2dSAlberto Garcia 1163f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1164f323bc9eSWenchao Xia const char *name, 1165f323bc9eSWenchao Xia Error **errp) 1166f323bc9eSWenchao Xia { 1167f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1168f323bc9eSWenchao Xia .device = (char *) device, 1169f323bc9eSWenchao Xia .name = (char *) name 1170f323bc9eSWenchao Xia }; 117110f75907SEric Blake TransactionAction action = { 117210f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 117332bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 117410f75907SEric Blake }; 117510f75907SEric Blake blockdev_do_action(&action, errp); 1176f323bc9eSWenchao Xia } 1177f323bc9eSWenchao Xia 117844e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 117944e3e053SWenchao Xia bool has_id, 118044e3e053SWenchao Xia const char *id, 118144e3e053SWenchao Xia bool has_name, 118244e3e053SWenchao Xia const char *name, 118344e3e053SWenchao Xia Error **errp) 118444e3e053SWenchao Xia { 1185a0e8544cSFam Zheng BlockDriverState *bs; 11864ef3982aSStefan Hajnoczi AioContext *aio_context; 118744e3e053SWenchao Xia QEMUSnapshotInfo sn; 118844e3e053SWenchao Xia Error *local_err = NULL; 118944e3e053SWenchao Xia SnapshotInfo *info = NULL; 119044e3e053SWenchao Xia int ret; 119144e3e053SWenchao Xia 11922dfb4c03SKevin Wolf bs = qmp_get_root_bs(device, errp); 11932dfb4c03SKevin Wolf if (!bs) { 119444e3e053SWenchao Xia return NULL; 119544e3e053SWenchao Xia } 11962dfb4c03SKevin Wolf aio_context = bdrv_get_aio_context(bs); 11975433c24fSMax Reitz aio_context_acquire(aio_context); 119844e3e053SWenchao Xia 119944e3e053SWenchao Xia if (!has_id) { 120044e3e053SWenchao Xia id = NULL; 120144e3e053SWenchao Xia } 120244e3e053SWenchao Xia 120344e3e053SWenchao Xia if (!has_name) { 120444e3e053SWenchao Xia name = NULL; 120544e3e053SWenchao Xia } 120644e3e053SWenchao Xia 120744e3e053SWenchao Xia if (!id && !name) { 120844e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 12095433c24fSMax Reitz goto out_aio_context; 121044e3e053SWenchao Xia } 121144e3e053SWenchao Xia 12120b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 12130b928854SStefan Hajnoczi goto out_aio_context; 12140b928854SStefan Hajnoczi } 12150b928854SStefan Hajnoczi 121644e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 121784d18f06SMarkus Armbruster if (local_err) { 121844e3e053SWenchao Xia error_propagate(errp, local_err); 12194ef3982aSStefan Hajnoczi goto out_aio_context; 122044e3e053SWenchao Xia } 122144e3e053SWenchao Xia if (!ret) { 122244e3e053SWenchao Xia error_setg(errp, 122344e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 122444e3e053SWenchao Xia "device '%s'", 122544e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 12264ef3982aSStefan Hajnoczi goto out_aio_context; 122744e3e053SWenchao Xia } 122844e3e053SWenchao Xia 122944e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 123084d18f06SMarkus Armbruster if (local_err) { 123144e3e053SWenchao Xia error_propagate(errp, local_err); 12324ef3982aSStefan Hajnoczi goto out_aio_context; 123344e3e053SWenchao Xia } 123444e3e053SWenchao Xia 12354ef3982aSStefan Hajnoczi aio_context_release(aio_context); 12364ef3982aSStefan Hajnoczi 12375839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 123844e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 123944e3e053SWenchao Xia info->name = g_strdup(sn.name); 124044e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 124144e3e053SWenchao Xia info->date_sec = sn.date_sec; 124244e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 124344e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 124444e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 124544e3e053SWenchao Xia 124644e3e053SWenchao Xia return info; 12474ef3982aSStefan Hajnoczi 12484ef3982aSStefan Hajnoczi out_aio_context: 12494ef3982aSStefan Hajnoczi aio_context_release(aio_context); 12504ef3982aSStefan Hajnoczi return NULL; 125144e3e053SWenchao Xia } 12528802d1fdSJeff Cody 1253341ebc2fSJohn Snow /** 1254341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1255341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1256341ebc2fSJohn Snow * the node reference and bitmap names. 1257341ebc2fSJohn Snow * 1258341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1259341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1260341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1261341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1262341ebc2fSJohn Snow * 1263341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1264341ebc2fSJohn Snow */ 1265341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1266341ebc2fSJohn Snow const char *name, 1267341ebc2fSJohn Snow BlockDriverState **pbs, 1268341ebc2fSJohn Snow Error **errp) 1269341ebc2fSJohn Snow { 1270341ebc2fSJohn Snow BlockDriverState *bs; 1271341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1272341ebc2fSJohn Snow 1273341ebc2fSJohn Snow if (!node) { 1274341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1275341ebc2fSJohn Snow return NULL; 1276341ebc2fSJohn Snow } 1277341ebc2fSJohn Snow if (!name) { 1278341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1279341ebc2fSJohn Snow return NULL; 1280341ebc2fSJohn Snow } 1281341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1282341ebc2fSJohn Snow if (!bs) { 1283341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1284341ebc2fSJohn Snow return NULL; 1285341ebc2fSJohn Snow } 1286341ebc2fSJohn Snow 1287341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1288341ebc2fSJohn Snow if (!bitmap) { 1289341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 12902119882cSPaolo Bonzini return NULL; 1291341ebc2fSJohn Snow } 1292341ebc2fSJohn Snow 1293341ebc2fSJohn Snow if (pbs) { 1294341ebc2fSJohn Snow *pbs = bs; 1295341ebc2fSJohn Snow } 1296341ebc2fSJohn Snow 1297341ebc2fSJohn Snow return bitmap; 1298341ebc2fSJohn Snow } 1299341ebc2fSJohn Snow 1300b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1301ba0c86a3SWenchao Xia 130250f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1303ba0c86a3SWenchao Xia 130450f43f0fSJohn Snow /** 130550f43f0fSJohn Snow * BlkActionOps: 130650f43f0fSJohn Snow * Table of operations that define an Action. 130750f43f0fSJohn Snow * 130850f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 130950f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 131050f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 131150f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 131250f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 131350f43f0fSJohn Snow * commit() or abort(). Can be NULL. 131450f43f0fSJohn Snow * 131550f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 131650f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1317ba0c86a3SWenchao Xia */ 131850f43f0fSJohn Snow typedef struct BlkActionOps { 131950f43f0fSJohn Snow size_t instance_size; 132050f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 132150f43f0fSJohn Snow void (*commit)(BlkActionState *common); 132250f43f0fSJohn Snow void (*abort)(BlkActionState *common); 132350f43f0fSJohn Snow void (*clean)(BlkActionState *common); 132450f43f0fSJohn Snow } BlkActionOps; 132550f43f0fSJohn Snow 132650f43f0fSJohn Snow /** 132750f43f0fSJohn Snow * BlkActionState: 132850f43f0fSJohn Snow * Describes one Action's state within a Transaction. 132950f43f0fSJohn Snow * 133050f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 133150f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 133294d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 133350f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 133450f43f0fSJohn Snow * 133550f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 133650f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 133750f43f0fSJohn Snow * base class. 133850f43f0fSJohn Snow */ 133950f43f0fSJohn Snow struct BlkActionState { 1340c8a83e85SKevin Wolf TransactionAction *action; 134150f43f0fSJohn Snow const BlkActionOps *ops; 134262c9e416SKevin Wolf JobTxn *block_job_txn; 134394d16a64SJohn Snow TransactionProperties *txn_props; 1344f4de0f8cSJohn Snow QTAILQ_ENTRY(BlkActionState) entry; 1345ba0c86a3SWenchao Xia }; 1346ba0c86a3SWenchao Xia 1347bbe86010SWenchao Xia /* internal snapshot private data */ 1348bbe86010SWenchao Xia typedef struct InternalSnapshotState { 134950f43f0fSJohn Snow BlkActionState common; 1350bbe86010SWenchao Xia BlockDriverState *bs; 1351bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1352507306ccSFam Zheng bool created; 1353bbe86010SWenchao Xia } InternalSnapshotState; 1354bbe86010SWenchao Xia 135594d16a64SJohn Snow 135694d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 135794d16a64SJohn Snow { 135894d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 135994d16a64SJohn Snow error_setg(errp, 136094d16a64SJohn Snow "Action '%s' does not support Transaction property " 136194d16a64SJohn Snow "completion-mode = %s", 1362977c736fSMarkus Armbruster TransactionActionKind_str(s->action->type), 1363977c736fSMarkus Armbruster ActionCompletionMode_str(s->txn_props->completion_mode)); 136494d16a64SJohn Snow return -1; 136594d16a64SJohn Snow } 136694d16a64SJohn Snow return 0; 136794d16a64SJohn Snow } 136894d16a64SJohn Snow 136950f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1370bbe86010SWenchao Xia Error **errp) 1371bbe86010SWenchao Xia { 1372f70edf99SMarkus Armbruster Error *local_err = NULL; 1373bbe86010SWenchao Xia const char *device; 1374bbe86010SWenchao Xia const char *name; 1375bbe86010SWenchao Xia BlockDriverState *bs; 1376bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1377bbe86010SWenchao Xia bool ret; 1378bbe86010SWenchao Xia qemu_timeval tv; 1379bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1380bbe86010SWenchao Xia InternalSnapshotState *state; 1381a36e458cSStefan Hajnoczi AioContext *aio_context; 1382bbe86010SWenchao Xia int ret1; 1383bbe86010SWenchao Xia 13846a8f9661SEric Blake g_assert(common->action->type == 1385bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 138632bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1387bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1388bbe86010SWenchao Xia 1389bbe86010SWenchao Xia /* 1. parse input */ 1390bbe86010SWenchao Xia device = internal->device; 1391bbe86010SWenchao Xia name = internal->name; 1392bbe86010SWenchao Xia 1393bbe86010SWenchao Xia /* 2. check for validation */ 139494d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 139594d16a64SJohn Snow return; 139694d16a64SJohn Snow } 139794d16a64SJohn Snow 139875dfd402SKevin Wolf bs = qmp_get_root_bs(device, errp); 139975dfd402SKevin Wolf if (!bs) { 1400bbe86010SWenchao Xia return; 1401bbe86010SWenchao Xia } 1402bbe86010SWenchao Xia 1403a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 1404a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 14055d6e96efSStefan Hajnoczi 1406507306ccSFam Zheng state->bs = bs; 1407a36e458cSStefan Hajnoczi 1408a36e458cSStefan Hajnoczi /* Paired with .clean() */ 1409507306ccSFam Zheng bdrv_drained_begin(bs); 1410507306ccSFam Zheng 14113dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 1412a36e458cSStefan Hajnoczi goto out; 14133dc7ca3cSStefan Hajnoczi } 14143dc7ca3cSStefan Hajnoczi 1415bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 141681e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1417a36e458cSStefan Hajnoczi goto out; 1418bbe86010SWenchao Xia } 1419bbe86010SWenchao Xia 1420bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 142181e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 142281e5f78aSAlberto Garcia "does not support internal snapshots", 142381e5f78aSAlberto Garcia bs->drv->format_name, device); 1424a36e458cSStefan Hajnoczi goto out; 1425bbe86010SWenchao Xia } 1426bbe86010SWenchao Xia 1427bbe86010SWenchao Xia if (!strlen(name)) { 1428bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1429a36e458cSStefan Hajnoczi goto out; 1430bbe86010SWenchao Xia } 1431bbe86010SWenchao Xia 1432bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1433f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1434f70edf99SMarkus Armbruster &local_err); 1435f70edf99SMarkus Armbruster if (local_err) { 1436f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1437a36e458cSStefan Hajnoczi goto out; 1438bbe86010SWenchao Xia } else if (ret) { 1439bbe86010SWenchao Xia error_setg(errp, 1440bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1441bbe86010SWenchao Xia name, device); 1442a36e458cSStefan Hajnoczi goto out; 1443bbe86010SWenchao Xia } 1444bbe86010SWenchao Xia 1445bbe86010SWenchao Xia /* 3. take the snapshot */ 1446bbe86010SWenchao Xia sn = &state->sn; 1447bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1448bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1449bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1450bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1451bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1452bbe86010SWenchao Xia 1453bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1454bbe86010SWenchao Xia if (ret1 < 0) { 1455bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1456bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1457bbe86010SWenchao Xia name, device); 1458a36e458cSStefan Hajnoczi goto out; 1459bbe86010SWenchao Xia } 1460bbe86010SWenchao Xia 1461bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1462507306ccSFam Zheng state->created = true; 1463a36e458cSStefan Hajnoczi 1464a36e458cSStefan Hajnoczi out: 1465a36e458cSStefan Hajnoczi aio_context_release(aio_context); 1466bbe86010SWenchao Xia } 1467bbe86010SWenchao Xia 146850f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1469bbe86010SWenchao Xia { 1470bbe86010SWenchao Xia InternalSnapshotState *state = 1471bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1472bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1473bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1474a36e458cSStefan Hajnoczi AioContext *aio_context; 1475bbe86010SWenchao Xia Error *local_error = NULL; 1476bbe86010SWenchao Xia 1477507306ccSFam Zheng if (!state->created) { 1478bbe86010SWenchao Xia return; 1479bbe86010SWenchao Xia } 1480bbe86010SWenchao Xia 1481a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1482a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 1483a36e458cSStefan Hajnoczi 1484bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1485c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1486c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1487c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1488c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1489c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1490bbe86010SWenchao Xia } 1491a36e458cSStefan Hajnoczi 1492a36e458cSStefan Hajnoczi aio_context_release(aio_context); 1493bbe86010SWenchao Xia } 1494bbe86010SWenchao Xia 149550f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 14965d6e96efSStefan Hajnoczi { 14975d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 14985d6e96efSStefan Hajnoczi common, common); 1499a36e458cSStefan Hajnoczi AioContext *aio_context; 15005d6e96efSStefan Hajnoczi 1501a36e458cSStefan Hajnoczi if (!state->bs) { 1502a36e458cSStefan Hajnoczi return; 1503a36e458cSStefan Hajnoczi } 1504a36e458cSStefan Hajnoczi 1505a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1506a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 1507a36e458cSStefan Hajnoczi 1508507306ccSFam Zheng bdrv_drained_end(state->bs); 1509a36e458cSStefan Hajnoczi 1510a36e458cSStefan Hajnoczi aio_context_release(aio_context); 15115d6e96efSStefan Hajnoczi } 15125d6e96efSStefan Hajnoczi 1513ba0c86a3SWenchao Xia /* external snapshot private data */ 1514ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 151550f43f0fSJohn Snow BlkActionState common; 15168802d1fdSJeff Cody BlockDriverState *old_bs; 15178802d1fdSJeff Cody BlockDriverState *new_bs; 1518067acf28SKevin Wolf bool overlay_appended; 1519ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 15208802d1fdSJeff Cody 152150f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 15229b9877eeSWenchao Xia Error **errp) 15239b9877eeSWenchao Xia { 15245b363937SMax Reitz int flags = 0; 152543de7e2dSAlberto Garcia QDict *options = NULL; 15269b9877eeSWenchao Xia Error *local_err = NULL; 152743de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1528e2a31e87SWenchao Xia const char *device; 15290901f67eSBenoît Canet const char *node_name; 153043de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 153143de7e2dSAlberto Garcia const char *snapshot_ref; 153243de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1533e2a31e87SWenchao Xia const char *new_image_file; 1534ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1535ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1536c8a83e85SKevin Wolf TransactionAction *action = common->action; 15372d24b60bSStefan Hajnoczi AioContext *aio_context; 15388ed7d42fSKevin Wolf int ret; 15399b9877eeSWenchao Xia 154043de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 154143de7e2dSAlberto Garcia * purpose but a different set of parameters */ 154243de7e2dSAlberto Garcia switch (action->type) { 154343de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 154443de7e2dSAlberto Garcia { 154532bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 154643de7e2dSAlberto Garcia device = s->node; 154743de7e2dSAlberto Garcia node_name = s->node; 154843de7e2dSAlberto Garcia new_image_file = NULL; 154943de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1550e2a31e87SWenchao Xia } 155143de7e2dSAlberto Garcia break; 155243de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 155343de7e2dSAlberto Garcia { 155432bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 155543de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 155643de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 155743de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 155843de7e2dSAlberto Garcia snapshot_ref = NULL; 155943de7e2dSAlberto Garcia } 156043de7e2dSAlberto Garcia break; 156143de7e2dSAlberto Garcia default: 156243de7e2dSAlberto Garcia g_assert_not_reached(); 1563e2a31e87SWenchao Xia } 1564e2a31e87SWenchao Xia 1565e2a31e87SWenchao Xia /* start processing */ 156694d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 156794d16a64SJohn Snow return; 156894d16a64SJohn Snow } 156994d16a64SJohn Snow 157043de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 157143de7e2dSAlberto Garcia if (!state->old_bs) { 15729b9877eeSWenchao Xia return; 15739b9877eeSWenchao Xia } 15749b9877eeSWenchao Xia 15752d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 15762d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 15772d24b60bSStefan Hajnoczi 15782d24b60bSStefan Hajnoczi /* Paired with .clean() */ 1579da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 15805d6e96efSStefan Hajnoczi 1581ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1582c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 15832d24b60bSStefan Hajnoczi goto out; 15849b9877eeSWenchao Xia } 15859b9877eeSWenchao Xia 15863718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 15873718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 15882d24b60bSStefan Hajnoczi goto out; 15899b9877eeSWenchao Xia } 15909b9877eeSWenchao Xia 1591ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1592ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1593c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 15942d24b60bSStefan Hajnoczi goto out; 15959b9877eeSWenchao Xia } 15969b9877eeSWenchao Xia } 15979b9877eeSWenchao Xia 1598212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1599c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 16002d24b60bSStefan Hajnoczi goto out; 1601f6186f49SBenoît Canet } 1602f6186f49SBenoît Canet 160343de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 160432bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 160543de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 160643de7e2dSAlberto Garcia enum NewImageMode mode; 160743de7e2dSAlberto Garcia const char *snapshot_node_name = 160843de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 160943de7e2dSAlberto Garcia 161043de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 1611a2bb6f8cSMax Reitz error_setg(errp, "New overlay node name missing"); 16122d24b60bSStefan Hajnoczi goto out; 161343de7e2dSAlberto Garcia } 161443de7e2dSAlberto Garcia 161543de7e2dSAlberto Garcia if (snapshot_node_name && 161643de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 1617a2bb6f8cSMax Reitz error_setg(errp, "New overlay node name already in use"); 16182d24b60bSStefan Hajnoczi goto out; 161943de7e2dSAlberto Garcia } 162043de7e2dSAlberto Garcia 1621ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 16222a32c6e8SJohn Snow flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ); 16232a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING; 16249b9877eeSWenchao Xia 16259b9877eeSWenchao Xia /* create new image w/backing file */ 162643de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 16279b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1628f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1629f86b8b58SKevin Wolf if (size < 0) { 1630f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 16312d24b60bSStefan Hajnoczi goto out; 1632f86b8b58SKevin Wolf } 1633f30c66baSMax Reitz bdrv_refresh_filename(state->old_bs); 16349b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1635ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1636ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 16379217283dSFam Zheng NULL, size, flags, false, &local_err); 163884d18f06SMarkus Armbruster if (local_err) { 16399b9877eeSWenchao Xia error_propagate(errp, local_err); 16402d24b60bSStefan Hajnoczi goto out; 16419b9877eeSWenchao Xia } 16429b9877eeSWenchao Xia } 16439b9877eeSWenchao Xia 16440901f67eSBenoît Canet options = qdict_new(); 1645d52e1a0eSPeter Maydell if (snapshot_node_name) { 164646f5ac20SEric Blake qdict_put_str(options, "node-name", snapshot_node_name); 16470901f67eSBenoît Canet } 164846f5ac20SEric Blake qdict_put_str(options, "driver", format); 164943de7e2dSAlberto Garcia } 165043de7e2dSAlberto Garcia 16515b363937SMax Reitz state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, 16525b363937SMax Reitz errp); 1653f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 16545b363937SMax Reitz if (!state->new_bs) { 16552d24b60bSStefan Hajnoczi goto out; 165643de7e2dSAlberto Garcia } 165743de7e2dSAlberto Garcia 16581f0c461bSKevin Wolf if (bdrv_has_blk(state->new_bs)) { 1659a2bb6f8cSMax Reitz error_setg(errp, "The overlay is already in use"); 16602d24b60bSStefan Hajnoczi goto out; 166143de7e2dSAlberto Garcia } 166243de7e2dSAlberto Garcia 166343de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 166443de7e2dSAlberto Garcia errp)) { 16652d24b60bSStefan Hajnoczi goto out; 166643de7e2dSAlberto Garcia } 166743de7e2dSAlberto Garcia 166843de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 1669a2bb6f8cSMax Reitz error_setg(errp, "The overlay already has a backing image"); 16702d24b60bSStefan Hajnoczi goto out; 167108b24cfeSAlberto Garcia } 167208b24cfeSAlberto Garcia 167308b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 1674a2bb6f8cSMax Reitz error_setg(errp, "The overlay does not support backing images"); 16752d24b60bSStefan Hajnoczi goto out; 1676b2c2832cSKevin Wolf } 1677b2c2832cSKevin Wolf 16788ed7d42fSKevin Wolf ret = bdrv_try_set_aio_context(state->new_bs, aio_context, errp); 16798ed7d42fSKevin Wolf if (ret < 0) { 16808ed7d42fSKevin Wolf goto out; 16818ed7d42fSKevin Wolf } 1682c26a5ab7SFam Zheng 1683b2c2832cSKevin Wolf /* This removes our old bs and adds the new bs. This is an operation that 1684b2c2832cSKevin Wolf * can fail, so we need to do it in .prepare; undoing it for abort is 1685b2c2832cSKevin Wolf * always possible. */ 1686b2c2832cSKevin Wolf bdrv_ref(state->new_bs); 1687b2c2832cSKevin Wolf bdrv_append(state->new_bs, state->old_bs, &local_err); 1688b2c2832cSKevin Wolf if (local_err) { 1689b2c2832cSKevin Wolf error_propagate(errp, local_err); 16902d24b60bSStefan Hajnoczi goto out; 16919b9877eeSWenchao Xia } 1692067acf28SKevin Wolf state->overlay_appended = true; 16932d24b60bSStefan Hajnoczi 16942d24b60bSStefan Hajnoczi out: 16952d24b60bSStefan Hajnoczi aio_context_release(aio_context); 16969b9877eeSWenchao Xia } 16979b9877eeSWenchao Xia 169850f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 16993b0047e8SWenchao Xia { 1700ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1701ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 17022d24b60bSStefan Hajnoczi AioContext *aio_context; 17032d24b60bSStefan Hajnoczi 17042d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 17052d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 1706ba0c86a3SWenchao Xia 17073b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 17083b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 17093b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 1710d3faa13eSPaolo Bonzini if (!atomic_read(&state->old_bs->copy_on_read)) { 17111b57774fSAlberto Garcia bdrv_reopen_set_read_only(state->old_bs, true, NULL); 17123b0047e8SWenchao Xia } 17132d24b60bSStefan Hajnoczi 17142d24b60bSStefan Hajnoczi aio_context_release(aio_context); 17154c844983SKevin Wolf } 17163b0047e8SWenchao Xia 171750f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 171896b86bf7SWenchao Xia { 1719ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1720ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1721ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 1722067acf28SKevin Wolf if (state->overlay_appended) { 17232d24b60bSStefan Hajnoczi AioContext *aio_context; 17242d24b60bSStefan Hajnoczi 17252d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 17262d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 17272d24b60bSStefan Hajnoczi 1728719fc28cSJeff Cody bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd() 1729719fc28cSJeff Cody close state->old_bs; we need it */ 1730719fc28cSJeff Cody bdrv_set_backing_hd(state->new_bs, NULL, &error_abort); 17315fe31c25SKevin Wolf bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); 1732719fc28cSJeff Cody bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */ 17332d24b60bSStefan Hajnoczi 17342d24b60bSStefan Hajnoczi aio_context_release(aio_context); 1735b2c2832cSKevin Wolf } 173696b86bf7SWenchao Xia } 1737da763e83SFam Zheng } 1738da763e83SFam Zheng 173950f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1740da763e83SFam Zheng { 1741da763e83SFam Zheng ExternalSnapshotState *state = 1742da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 17432d24b60bSStefan Hajnoczi AioContext *aio_context; 17442d24b60bSStefan Hajnoczi 17452d24b60bSStefan Hajnoczi if (!state->old_bs) { 17462d24b60bSStefan Hajnoczi return; 17472d24b60bSStefan Hajnoczi } 17482d24b60bSStefan Hajnoczi 17492d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 17502d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 17512d24b60bSStefan Hajnoczi 1752da763e83SFam Zheng bdrv_drained_end(state->old_bs); 1753b2c2832cSKevin Wolf bdrv_unref(state->new_bs); 17542d24b60bSStefan Hajnoczi 17552d24b60bSStefan Hajnoczi aio_context_release(aio_context); 175696b86bf7SWenchao Xia } 175796b86bf7SWenchao Xia 17583037f364SStefan Hajnoczi typedef struct DriveBackupState { 175950f43f0fSJohn Snow BlkActionState common; 17603037f364SStefan Hajnoczi BlockDriverState *bs; 17613037f364SStefan Hajnoczi BlockJob *job; 17623037f364SStefan Hajnoczi } DriveBackupState; 17633037f364SStefan Hajnoczi 17642288ccfaSSergio Lopez static BlockJob *do_backup_common(BackupCommon *backup, 17652288ccfaSSergio Lopez BlockDriverState *bs, 17662288ccfaSSergio Lopez BlockDriverState *target_bs, 17672288ccfaSSergio Lopez AioContext *aio_context, 17682288ccfaSSergio Lopez JobTxn *txn, Error **errp); 176978f51fdeSJohn Snow 177050f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 17713037f364SStefan Hajnoczi { 17723037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 17733037f364SStefan Hajnoczi DriveBackup *backup; 17742288ccfaSSergio Lopez BlockDriverState *bs; 17752288ccfaSSergio Lopez BlockDriverState *target_bs; 17762288ccfaSSergio Lopez BlockDriverState *source = NULL; 177766d56054SStefan Hajnoczi AioContext *aio_context; 17782288ccfaSSergio Lopez QDict *options; 17793037f364SStefan Hajnoczi Error *local_err = NULL; 17802288ccfaSSergio Lopez int flags; 17812288ccfaSSergio Lopez int64_t size; 17822288ccfaSSergio Lopez bool set_backing_hd = false; 17833037f364SStefan Hajnoczi 17846a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 178532bafa8fSEric Blake backup = common->action->u.drive_backup.data; 17863037f364SStefan Hajnoczi 17872288ccfaSSergio Lopez if (!backup->has_mode) { 17882288ccfaSSergio Lopez backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17892288ccfaSSergio Lopez } 17902288ccfaSSergio Lopez 179185c9d133SVladimir Sementsov-Ogievskiy bs = bdrv_lookup_bs(backup->device, backup->device, errp); 1792b7e4fa22SKevin Wolf if (!bs) { 17931fdd4b7bSFam Zheng return; 17941fdd4b7bSFam Zheng } 17951fdd4b7bSFam Zheng 17962288ccfaSSergio Lopez if (!bs->drv) { 17972288ccfaSSergio Lopez error_setg(errp, "Device has no medium"); 17982288ccfaSSergio Lopez return; 17992288ccfaSSergio Lopez } 18002288ccfaSSergio Lopez 180166d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 180266d56054SStefan Hajnoczi aio_context_acquire(aio_context); 180366d56054SStefan Hajnoczi 180466d56054SStefan Hajnoczi /* Paired with .clean() */ 1805b7e4fa22SKevin Wolf bdrv_drained_begin(bs); 180666d56054SStefan Hajnoczi 18072288ccfaSSergio Lopez if (!backup->has_format) { 18082288ccfaSSergio Lopez backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? 18092288ccfaSSergio Lopez NULL : (char *) bs->drv->format_name; 18102288ccfaSSergio Lopez } 18115d6e96efSStefan Hajnoczi 18122288ccfaSSergio Lopez /* Early check to avoid creating target */ 18132288ccfaSSergio Lopez if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 18142288ccfaSSergio Lopez goto out; 18152288ccfaSSergio Lopez } 18162288ccfaSSergio Lopez 18172288ccfaSSergio Lopez flags = bs->open_flags | BDRV_O_RDWR; 18182288ccfaSSergio Lopez 18192288ccfaSSergio Lopez /* 18202288ccfaSSergio Lopez * See if we have a backing HD we can use to create our new image 18212288ccfaSSergio Lopez * on top of. 18222288ccfaSSergio Lopez */ 18232288ccfaSSergio Lopez if (backup->sync == MIRROR_SYNC_MODE_TOP) { 18242288ccfaSSergio Lopez source = backing_bs(bs); 18252288ccfaSSergio Lopez if (!source) { 18262288ccfaSSergio Lopez backup->sync = MIRROR_SYNC_MODE_FULL; 18272288ccfaSSergio Lopez } 18282288ccfaSSergio Lopez } 18292288ccfaSSergio Lopez if (backup->sync == MIRROR_SYNC_MODE_NONE) { 18302288ccfaSSergio Lopez source = bs; 18312288ccfaSSergio Lopez flags |= BDRV_O_NO_BACKING; 18322288ccfaSSergio Lopez set_backing_hd = true; 18332288ccfaSSergio Lopez } 18342288ccfaSSergio Lopez 18352288ccfaSSergio Lopez size = bdrv_getlength(bs); 18362288ccfaSSergio Lopez if (size < 0) { 18372288ccfaSSergio Lopez error_setg_errno(errp, -size, "bdrv_getlength failed"); 18382288ccfaSSergio Lopez goto out; 18392288ccfaSSergio Lopez } 18402288ccfaSSergio Lopez 18412288ccfaSSergio Lopez if (backup->mode != NEW_IMAGE_MODE_EXISTING) { 18422288ccfaSSergio Lopez assert(backup->format); 18432288ccfaSSergio Lopez if (source) { 18442288ccfaSSergio Lopez bdrv_refresh_filename(source); 18452288ccfaSSergio Lopez bdrv_img_create(backup->target, backup->format, source->filename, 18462288ccfaSSergio Lopez source->drv->format_name, NULL, 18472288ccfaSSergio Lopez size, flags, false, &local_err); 18482288ccfaSSergio Lopez } else { 18492288ccfaSSergio Lopez bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, 18502288ccfaSSergio Lopez size, flags, false, &local_err); 18512288ccfaSSergio Lopez } 18522288ccfaSSergio Lopez } 18532288ccfaSSergio Lopez 185484d18f06SMarkus Armbruster if (local_err) { 18553037f364SStefan Hajnoczi error_propagate(errp, local_err); 185666d56054SStefan Hajnoczi goto out; 18573037f364SStefan Hajnoczi } 185866d56054SStefan Hajnoczi 18592288ccfaSSergio Lopez options = qdict_new(); 18602288ccfaSSergio Lopez qdict_put_str(options, "discard", "unmap"); 18612288ccfaSSergio Lopez qdict_put_str(options, "detect-zeroes", "unmap"); 18622288ccfaSSergio Lopez if (backup->format) { 18632288ccfaSSergio Lopez qdict_put_str(options, "driver", backup->format); 18642288ccfaSSergio Lopez } 18652288ccfaSSergio Lopez 18662288ccfaSSergio Lopez target_bs = bdrv_open(backup->target, NULL, options, flags, errp); 18672288ccfaSSergio Lopez if (!target_bs) { 18682288ccfaSSergio Lopez goto out; 18692288ccfaSSergio Lopez } 18702288ccfaSSergio Lopez 18712288ccfaSSergio Lopez if (set_backing_hd) { 18722288ccfaSSergio Lopez bdrv_set_backing_hd(target_bs, source, &local_err); 18732288ccfaSSergio Lopez if (local_err) { 18742288ccfaSSergio Lopez goto unref; 18752288ccfaSSergio Lopez } 18762288ccfaSSergio Lopez } 18772288ccfaSSergio Lopez 18782288ccfaSSergio Lopez state->bs = bs; 18792288ccfaSSergio Lopez 18802288ccfaSSergio Lopez state->job = do_backup_common(qapi_DriveBackup_base(backup), 18812288ccfaSSergio Lopez bs, target_bs, aio_context, 18822288ccfaSSergio Lopez common->block_job_txn, errp); 18832288ccfaSSergio Lopez 18842288ccfaSSergio Lopez unref: 18852288ccfaSSergio Lopez bdrv_unref(target_bs); 188666d56054SStefan Hajnoczi out: 188766d56054SStefan Hajnoczi aio_context_release(aio_context); 1888111049a4SJohn Snow } 18893037f364SStefan Hajnoczi 1890111049a4SJohn Snow static void drive_backup_commit(BlkActionState *common) 1891111049a4SJohn Snow { 1892111049a4SJohn Snow DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 189366d56054SStefan Hajnoczi AioContext *aio_context; 189466d56054SStefan Hajnoczi 189566d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 189666d56054SStefan Hajnoczi aio_context_acquire(aio_context); 189766d56054SStefan Hajnoczi 1898111049a4SJohn Snow assert(state->job); 1899da01ff7fSKevin Wolf job_start(&state->job->job); 190066d56054SStefan Hajnoczi 190166d56054SStefan Hajnoczi aio_context_release(aio_context); 19023037f364SStefan Hajnoczi } 19033037f364SStefan Hajnoczi 190450f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 19053037f364SStefan Hajnoczi { 19063037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19073037f364SStefan Hajnoczi 1908111049a4SJohn Snow if (state->job) { 190966d56054SStefan Hajnoczi AioContext *aio_context; 191066d56054SStefan Hajnoczi 191166d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 191266d56054SStefan Hajnoczi aio_context_acquire(aio_context); 191366d56054SStefan Hajnoczi 19143d70ff53SKevin Wolf job_cancel_sync(&state->job->job); 191566d56054SStefan Hajnoczi 191666d56054SStefan Hajnoczi aio_context_release(aio_context); 19173037f364SStefan Hajnoczi } 19183037f364SStefan Hajnoczi } 19193037f364SStefan Hajnoczi 192050f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 19215d6e96efSStefan Hajnoczi { 19225d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 192366d56054SStefan Hajnoczi AioContext *aio_context; 19245d6e96efSStefan Hajnoczi 192566d56054SStefan Hajnoczi if (!state->bs) { 192666d56054SStefan Hajnoczi return; 19275d6e96efSStefan Hajnoczi } 192866d56054SStefan Hajnoczi 192966d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 193066d56054SStefan Hajnoczi aio_context_acquire(aio_context); 193166d56054SStefan Hajnoczi 193266d56054SStefan Hajnoczi bdrv_drained_end(state->bs); 193366d56054SStefan Hajnoczi 193466d56054SStefan Hajnoczi aio_context_release(aio_context); 19355d6e96efSStefan Hajnoczi } 19365d6e96efSStefan Hajnoczi 1937bd8baecdSFam Zheng typedef struct BlockdevBackupState { 193850f43f0fSJohn Snow BlkActionState common; 1939bd8baecdSFam Zheng BlockDriverState *bs; 1940bd8baecdSFam Zheng BlockJob *job; 1941bd8baecdSFam Zheng } BlockdevBackupState; 1942bd8baecdSFam Zheng 194350f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1944bd8baecdSFam Zheng { 1945bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1946bd8baecdSFam Zheng BlockdevBackup *backup; 1947*5b7bfe51SSergio Lopez BlockDriverState *bs; 1948*5b7bfe51SSergio Lopez BlockDriverState *target_bs; 1949edd5adeeSStefan Hajnoczi AioContext *aio_context; 1950bd8baecdSFam Zheng 19516a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 195232bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1953bd8baecdSFam Zheng 1954930fe17fSVladimir Sementsov-Ogievskiy bs = bdrv_lookup_bs(backup->device, backup->device, errp); 1955cef34eebSKevin Wolf if (!bs) { 1956ff52bf36SFam Zheng return; 1957ff52bf36SFam Zheng } 1958ff52bf36SFam Zheng 1959*5b7bfe51SSergio Lopez target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); 1960*5b7bfe51SSergio Lopez if (!target_bs) { 1961bd8baecdSFam Zheng return; 1962bd8baecdSFam Zheng } 1963bd8baecdSFam Zheng 1964edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 1965edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1966cef34eebSKevin Wolf state->bs = bs; 1967edd5adeeSStefan Hajnoczi 1968edd5adeeSStefan Hajnoczi /* Paired with .clean() */ 1969ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1970bd8baecdSFam Zheng 1971*5b7bfe51SSergio Lopez state->job = do_backup_common(qapi_BlockdevBackup_base(backup), 1972*5b7bfe51SSergio Lopez bs, target_bs, aio_context, 1973*5b7bfe51SSergio Lopez common->block_job_txn, errp); 1974edd5adeeSStefan Hajnoczi 1975edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 1976111049a4SJohn Snow } 1977bd8baecdSFam Zheng 1978111049a4SJohn Snow static void blockdev_backup_commit(BlkActionState *common) 1979111049a4SJohn Snow { 1980111049a4SJohn Snow BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1981edd5adeeSStefan Hajnoczi AioContext *aio_context; 1982edd5adeeSStefan Hajnoczi 1983edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1984edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1985edd5adeeSStefan Hajnoczi 1986111049a4SJohn Snow assert(state->job); 1987da01ff7fSKevin Wolf job_start(&state->job->job); 1988edd5adeeSStefan Hajnoczi 1989edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 1990bd8baecdSFam Zheng } 1991bd8baecdSFam Zheng 199250f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1993bd8baecdSFam Zheng { 1994bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1995bd8baecdSFam Zheng 1996111049a4SJohn Snow if (state->job) { 1997edd5adeeSStefan Hajnoczi AioContext *aio_context; 1998edd5adeeSStefan Hajnoczi 1999edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 2000edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 2001edd5adeeSStefan Hajnoczi 20023d70ff53SKevin Wolf job_cancel_sync(&state->job->job); 2003edd5adeeSStefan Hajnoczi 2004edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 2005bd8baecdSFam Zheng } 2006bd8baecdSFam Zheng } 2007bd8baecdSFam Zheng 200850f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 2009bd8baecdSFam Zheng { 2010bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 2011edd5adeeSStefan Hajnoczi AioContext *aio_context; 2012bd8baecdSFam Zheng 2013edd5adeeSStefan Hajnoczi if (!state->bs) { 2014edd5adeeSStefan Hajnoczi return; 2015bd8baecdSFam Zheng } 2016edd5adeeSStefan Hajnoczi 2017edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 2018edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 2019edd5adeeSStefan Hajnoczi 2020edd5adeeSStefan Hajnoczi bdrv_drained_end(state->bs); 2021edd5adeeSStefan Hajnoczi 2022edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 2023bd8baecdSFam Zheng } 2024bd8baecdSFam Zheng 2025df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 202650f43f0fSJohn Snow BlkActionState common; 2027df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 2028df9a681dSFam Zheng BlockDriverState *bs; 2029df9a681dSFam Zheng HBitmap *backup; 2030df9a681dSFam Zheng bool prepared; 2031c6490447SVladimir Sementsov-Ogievskiy bool was_enabled; 2032df9a681dSFam Zheng } BlockDirtyBitmapState; 2033df9a681dSFam Zheng 203450f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 2035df9a681dSFam Zheng Error **errp) 2036df9a681dSFam Zheng { 2037df9a681dSFam Zheng Error *local_err = NULL; 2038df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2039df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2040df9a681dSFam Zheng common, common); 2041df9a681dSFam Zheng 204294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 204394d16a64SJohn Snow return; 204494d16a64SJohn Snow } 204594d16a64SJohn Snow 204632bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2047df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2048df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2049df9a681dSFam Zheng action->has_granularity, action->granularity, 2050fd5ae4ccSVladimir Sementsov-Ogievskiy action->has_persistent, action->persistent, 20510e2b7f09SJohn Snow action->has_disabled, action->disabled, 2052df9a681dSFam Zheng &local_err); 2053df9a681dSFam Zheng 2054df9a681dSFam Zheng if (!local_err) { 2055df9a681dSFam Zheng state->prepared = true; 2056df9a681dSFam Zheng } else { 2057df9a681dSFam Zheng error_propagate(errp, local_err); 2058df9a681dSFam Zheng } 2059df9a681dSFam Zheng } 2060df9a681dSFam Zheng 206150f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2062df9a681dSFam Zheng { 2063df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2064df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2065df9a681dSFam Zheng common, common); 2066df9a681dSFam Zheng 206732bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2068df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2069df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2070df9a681dSFam Zheng */ 2071df9a681dSFam Zheng if (state->prepared) { 2072df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2073df9a681dSFam Zheng } 2074df9a681dSFam Zheng } 2075df9a681dSFam Zheng 207650f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2077df9a681dSFam Zheng Error **errp) 2078df9a681dSFam Zheng { 2079df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2080df9a681dSFam Zheng common, common); 2081df9a681dSFam Zheng BlockDirtyBitmap *action; 2082df9a681dSFam Zheng 208394d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 208494d16a64SJohn Snow return; 208594d16a64SJohn Snow } 208694d16a64SJohn Snow 208732bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2088df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2089df9a681dSFam Zheng action->name, 2090df9a681dSFam Zheng &state->bs, 2091df9a681dSFam Zheng errp); 2092df9a681dSFam Zheng if (!state->bitmap) { 2093df9a681dSFam Zheng return; 2094df9a681dSFam Zheng } 2095df9a681dSFam Zheng 20963ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) { 2097d6883bc9SVladimir Sementsov-Ogievskiy return; 2098df9a681dSFam Zheng } 2099df9a681dSFam Zheng 2100df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2101df9a681dSFam Zheng } 2102df9a681dSFam Zheng 21035c4cf8b2SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_restore(BlkActionState *common) 2104df9a681dSFam Zheng { 2105df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2106df9a681dSFam Zheng common, common); 2107df9a681dSFam Zheng 2108184dd9c4SJohn Snow if (state->backup) { 210956bd6624SVladimir Sementsov-Ogievskiy bdrv_restore_dirty_bitmap(state->bitmap, state->backup); 2110df9a681dSFam Zheng } 2111184dd9c4SJohn Snow } 2112df9a681dSFam Zheng 21135c4cf8b2SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_free_backup(BlkActionState *common) 2114df9a681dSFam Zheng { 2115df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2116df9a681dSFam Zheng common, common); 2117df9a681dSFam Zheng 2118df9a681dSFam Zheng hbitmap_free(state->backup); 2119df9a681dSFam Zheng } 2120df9a681dSFam Zheng 2121c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_prepare(BlkActionState *common, 2122c6490447SVladimir Sementsov-Ogievskiy Error **errp) 2123c6490447SVladimir Sementsov-Ogievskiy { 2124c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmap *action; 2125c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2126c6490447SVladimir Sementsov-Ogievskiy common, common); 2127c6490447SVladimir Sementsov-Ogievskiy 2128c6490447SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 2129c6490447SVladimir Sementsov-Ogievskiy return; 2130c6490447SVladimir Sementsov-Ogievskiy } 2131c6490447SVladimir Sementsov-Ogievskiy 21320e2b7f09SJohn Snow action = common->action->u.block_dirty_bitmap_enable.data; 2133c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, 2134c6490447SVladimir Sementsov-Ogievskiy action->name, 2135c6490447SVladimir Sementsov-Ogievskiy NULL, 2136c6490447SVladimir Sementsov-Ogievskiy errp); 2137c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) { 2138c6490447SVladimir Sementsov-Ogievskiy return; 2139c6490447SVladimir Sementsov-Ogievskiy } 2140c6490447SVladimir Sementsov-Ogievskiy 21413ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { 2142b053bb55SJohn Snow return; 2143b053bb55SJohn Snow } 2144b053bb55SJohn Snow 2145c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); 2146c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap); 2147c6490447SVladimir Sementsov-Ogievskiy } 2148c6490447SVladimir Sementsov-Ogievskiy 2149c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_abort(BlkActionState *common) 2150c6490447SVladimir Sementsov-Ogievskiy { 2151c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2152c6490447SVladimir Sementsov-Ogievskiy common, common); 2153c6490447SVladimir Sementsov-Ogievskiy 2154c6490447SVladimir Sementsov-Ogievskiy if (!state->was_enabled) { 2155c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap); 2156c6490447SVladimir Sementsov-Ogievskiy } 2157c6490447SVladimir Sementsov-Ogievskiy } 2158c6490447SVladimir Sementsov-Ogievskiy 2159c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_prepare(BlkActionState *common, 2160c6490447SVladimir Sementsov-Ogievskiy Error **errp) 2161c6490447SVladimir Sementsov-Ogievskiy { 2162c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmap *action; 2163c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2164c6490447SVladimir Sementsov-Ogievskiy common, common); 2165c6490447SVladimir Sementsov-Ogievskiy 2166c6490447SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 2167c6490447SVladimir Sementsov-Ogievskiy return; 2168c6490447SVladimir Sementsov-Ogievskiy } 2169c6490447SVladimir Sementsov-Ogievskiy 21700e2b7f09SJohn Snow action = common->action->u.block_dirty_bitmap_disable.data; 2171c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, 2172c6490447SVladimir Sementsov-Ogievskiy action->name, 2173c6490447SVladimir Sementsov-Ogievskiy NULL, 2174c6490447SVladimir Sementsov-Ogievskiy errp); 2175c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) { 2176c6490447SVladimir Sementsov-Ogievskiy return; 2177c6490447SVladimir Sementsov-Ogievskiy } 2178c6490447SVladimir Sementsov-Ogievskiy 21793ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { 2180b053bb55SJohn Snow return; 2181b053bb55SJohn Snow } 2182b053bb55SJohn Snow 2183c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); 2184c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap); 2185c6490447SVladimir Sementsov-Ogievskiy } 2186c6490447SVladimir Sementsov-Ogievskiy 2187c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_abort(BlkActionState *common) 2188c6490447SVladimir Sementsov-Ogievskiy { 2189c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2190c6490447SVladimir Sementsov-Ogievskiy common, common); 2191c6490447SVladimir Sementsov-Ogievskiy 2192c6490447SVladimir Sementsov-Ogievskiy if (state->was_enabled) { 2193c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap); 2194c6490447SVladimir Sementsov-Ogievskiy } 2195c6490447SVladimir Sementsov-Ogievskiy } 2196c6490447SVladimir Sementsov-Ogievskiy 2197eff0829bSVladimir Sementsov-Ogievskiy static BdrvDirtyBitmap *do_block_dirty_bitmap_merge( 2198eff0829bSVladimir Sementsov-Ogievskiy const char *node, const char *target, 2199eff0829bSVladimir Sementsov-Ogievskiy BlockDirtyBitmapMergeSourceList *bitmaps, 2200eff0829bSVladimir Sementsov-Ogievskiy HBitmap **backup, Error **errp); 2201360d4e4eSJohn Snow 22026fd2e407SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_merge_prepare(BlkActionState *common, 22036fd2e407SVladimir Sementsov-Ogievskiy Error **errp) 22046fd2e407SVladimir Sementsov-Ogievskiy { 22056fd2e407SVladimir Sementsov-Ogievskiy BlockDirtyBitmapMerge *action; 22066fd2e407SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 22076fd2e407SVladimir Sementsov-Ogievskiy common, common); 22086fd2e407SVladimir Sementsov-Ogievskiy 22096fd2e407SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 22106fd2e407SVladimir Sementsov-Ogievskiy return; 22116fd2e407SVladimir Sementsov-Ogievskiy } 22126fd2e407SVladimir Sementsov-Ogievskiy 22130e2b7f09SJohn Snow action = common->action->u.block_dirty_bitmap_merge.data; 2214360d4e4eSJohn Snow 2215360d4e4eSJohn Snow state->bitmap = do_block_dirty_bitmap_merge(action->node, action->target, 2216360d4e4eSJohn Snow action->bitmaps, &state->backup, 22176fd2e407SVladimir Sementsov-Ogievskiy errp); 22186fd2e407SVladimir Sementsov-Ogievskiy } 22196fd2e407SVladimir Sementsov-Ogievskiy 2220c4e4b0faSJohn Snow static BdrvDirtyBitmap *do_block_dirty_bitmap_remove( 2221c4e4b0faSJohn Snow const char *node, const char *name, bool release, 2222c4e4b0faSJohn Snow BlockDriverState **bitmap_bs, Error **errp); 2223c4e4b0faSJohn Snow 2224c4e4b0faSJohn Snow static void block_dirty_bitmap_remove_prepare(BlkActionState *common, 2225c4e4b0faSJohn Snow Error **errp) 2226c4e4b0faSJohn Snow { 2227c4e4b0faSJohn Snow BlockDirtyBitmap *action; 2228c4e4b0faSJohn Snow BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2229c4e4b0faSJohn Snow common, common); 2230c4e4b0faSJohn Snow 2231c4e4b0faSJohn Snow if (action_check_completion_mode(common, errp) < 0) { 2232c4e4b0faSJohn Snow return; 2233c4e4b0faSJohn Snow } 2234c4e4b0faSJohn Snow 2235c4e4b0faSJohn Snow action = common->action->u.block_dirty_bitmap_remove.data; 2236c4e4b0faSJohn Snow 2237c4e4b0faSJohn Snow state->bitmap = do_block_dirty_bitmap_remove(action->node, action->name, 2238c4e4b0faSJohn Snow false, &state->bs, errp); 2239c4e4b0faSJohn Snow if (state->bitmap) { 2240c4e4b0faSJohn Snow bdrv_dirty_bitmap_skip_store(state->bitmap, true); 2241c4e4b0faSJohn Snow bdrv_dirty_bitmap_set_busy(state->bitmap, true); 2242c4e4b0faSJohn Snow } 2243c4e4b0faSJohn Snow } 2244c4e4b0faSJohn Snow 2245c4e4b0faSJohn Snow static void block_dirty_bitmap_remove_abort(BlkActionState *common) 2246c4e4b0faSJohn Snow { 2247c4e4b0faSJohn Snow BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2248c4e4b0faSJohn Snow common, common); 2249c4e4b0faSJohn Snow 2250c4e4b0faSJohn Snow if (state->bitmap) { 2251c4e4b0faSJohn Snow bdrv_dirty_bitmap_skip_store(state->bitmap, false); 2252c4e4b0faSJohn Snow bdrv_dirty_bitmap_set_busy(state->bitmap, false); 2253c4e4b0faSJohn Snow } 2254c4e4b0faSJohn Snow } 2255c4e4b0faSJohn Snow 2256c4e4b0faSJohn Snow static void block_dirty_bitmap_remove_commit(BlkActionState *common) 2257c4e4b0faSJohn Snow { 2258c4e4b0faSJohn Snow BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2259c4e4b0faSJohn Snow common, common); 2260c4e4b0faSJohn Snow 2261c4e4b0faSJohn Snow bdrv_dirty_bitmap_set_busy(state->bitmap, false); 22625deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(state->bitmap); 2263c4e4b0faSJohn Snow } 2264c4e4b0faSJohn Snow 226550f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 226678b18b78SStefan Hajnoczi { 226778b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 226878b18b78SStefan Hajnoczi } 226978b18b78SStefan Hajnoczi 227050f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 227178b18b78SStefan Hajnoczi { 2272dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 227378b18b78SStefan Hajnoczi } 227478b18b78SStefan Hajnoczi 227550f43f0fSJohn Snow static const BlkActionOps actions[] = { 227643de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 227743de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 227843de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 227943de7e2dSAlberto Garcia .commit = external_snapshot_commit, 228043de7e2dSAlberto Garcia .abort = external_snapshot_abort, 22814ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 228243de7e2dSAlberto Garcia }, 2283c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2284ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2285ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2286ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2287ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2288da763e83SFam Zheng .clean = external_snapshot_clean, 2289ba0c86a3SWenchao Xia }, 22903037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 22913037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 22923037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 2293111049a4SJohn Snow .commit = drive_backup_commit, 22943037f364SStefan Hajnoczi .abort = drive_backup_abort, 22955d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 22963037f364SStefan Hajnoczi }, 2297bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2298bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2299bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2300111049a4SJohn Snow .commit = blockdev_backup_commit, 2301bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2302bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2303bd8baecdSFam Zheng }, 230478b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 230550f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 230678b18b78SStefan Hajnoczi .prepare = abort_prepare, 230778b18b78SStefan Hajnoczi .commit = abort_commit, 230878b18b78SStefan Hajnoczi }, 2309bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2310bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2311bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2312bbe86010SWenchao Xia .abort = internal_snapshot_abort, 23135d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2314bbe86010SWenchao Xia }, 2315df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2316df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2317df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2318df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2319df9a681dSFam Zheng }, 2320df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2321df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2322df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 23235c4cf8b2SVladimir Sementsov-Ogievskiy .commit = block_dirty_bitmap_free_backup, 23245c4cf8b2SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_restore, 2325c6490447SVladimir Sementsov-Ogievskiy }, 23260e2b7f09SJohn Snow [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = { 2327c6490447SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 2328c6490447SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_enable_prepare, 2329c6490447SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_enable_abort, 2330c6490447SVladimir Sementsov-Ogievskiy }, 23310e2b7f09SJohn Snow [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = { 2332c6490447SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 2333c6490447SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_disable_prepare, 2334c6490447SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_disable_abort, 233566da04ddSJohn Snow }, 23360e2b7f09SJohn Snow [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = { 23376fd2e407SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 23386fd2e407SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_merge_prepare, 23396fd2e407SVladimir Sementsov-Ogievskiy .commit = block_dirty_bitmap_free_backup, 23406fd2e407SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_restore, 23416fd2e407SVladimir Sementsov-Ogievskiy }, 2342c4e4b0faSJohn Snow [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = { 2343c4e4b0faSJohn Snow .instance_size = sizeof(BlockDirtyBitmapState), 2344c4e4b0faSJohn Snow .prepare = block_dirty_bitmap_remove_prepare, 2345c4e4b0faSJohn Snow .commit = block_dirty_bitmap_remove_commit, 2346c4e4b0faSJohn Snow .abort = block_dirty_bitmap_remove_abort, 2347c4e4b0faSJohn Snow }, 234866da04ddSJohn Snow /* Where are transactions for MIRROR, COMMIT and STREAM? 234966da04ddSJohn Snow * Although these blockjobs use transaction callbacks like the backup job, 235066da04ddSJohn Snow * these jobs do not necessarily adhere to transaction semantics. 235166da04ddSJohn Snow * These jobs may not fully undo all of their actions on abort, nor do they 235266da04ddSJohn Snow * necessarily work in transactions with more than one job in them. 235366da04ddSJohn Snow */ 2354ba0c86a3SWenchao Xia }; 2355ba0c86a3SWenchao Xia 235694d16a64SJohn Snow /** 235794d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 235894d16a64SJohn Snow * that structure with desired defaults if they are unset. 235994d16a64SJohn Snow */ 236094d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 236194d16a64SJohn Snow TransactionProperties *props) 236294d16a64SJohn Snow { 236394d16a64SJohn Snow if (!props) { 236494d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 236594d16a64SJohn Snow } 236694d16a64SJohn Snow 236794d16a64SJohn Snow if (!props->has_completion_mode) { 236894d16a64SJohn Snow props->has_completion_mode = true; 236994d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 237094d16a64SJohn Snow } 237194d16a64SJohn Snow 237294d16a64SJohn Snow return props; 237394d16a64SJohn Snow } 237494d16a64SJohn Snow 23758802d1fdSJeff Cody /* 2376b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2377b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 23788802d1fdSJeff Cody */ 237994d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 238094d16a64SJohn Snow bool has_props, 238194d16a64SJohn Snow struct TransactionProperties *props, 238294d16a64SJohn Snow Error **errp) 23838802d1fdSJeff Cody { 2384c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 238562c9e416SKevin Wolf JobTxn *block_job_txn = NULL; 238650f43f0fSJohn Snow BlkActionState *state, *next; 238743e17041SLuiz Capitulino Error *local_err = NULL; 23888802d1fdSJeff Cody 2389f4de0f8cSJohn Snow QTAILQ_HEAD(, BlkActionState) snap_bdrv_states; 2390f4de0f8cSJohn Snow QTAILQ_INIT(&snap_bdrv_states); 23918802d1fdSJeff Cody 239294d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 239362c9e416SKevin Wolf * If not, we don't really need to make a JobTxn. 239494d16a64SJohn Snow */ 239594d16a64SJohn Snow props = get_transaction_properties(props); 239694d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 23977eaa8fb5SKevin Wolf block_job_txn = job_txn_new(); 239894d16a64SJohn Snow } 239994d16a64SJohn Snow 2400b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 24018802d1fdSJeff Cody bdrv_drain_all(); 24028802d1fdSJeff Cody 2403b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 24048802d1fdSJeff Cody while (NULL != dev_entry) { 2405c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 240650f43f0fSJohn Snow const BlkActionOps *ops; 240752e7c241SPaolo Bonzini 24088802d1fdSJeff Cody dev_info = dev_entry->value; 24098802d1fdSJeff Cody dev_entry = dev_entry->next; 24108802d1fdSJeff Cody 24116a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2412ba0c86a3SWenchao Xia 24136a8f9661SEric Blake ops = &actions[dev_info->type]; 2414aa3fe714SMax Reitz assert(ops->instance_size > 0); 2415aa3fe714SMax Reitz 2416ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2417ba5d6ab6SStefan Hajnoczi state->ops = ops; 2418ba5d6ab6SStefan Hajnoczi state->action = dev_info; 241994d16a64SJohn Snow state->block_job_txn = block_job_txn; 242094d16a64SJohn Snow state->txn_props = props; 2421f4de0f8cSJohn Snow QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 24228802d1fdSJeff Cody 2423ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 242484d18f06SMarkus Armbruster if (local_err) { 24259b9877eeSWenchao Xia error_propagate(errp, local_err); 24269b9877eeSWenchao Xia goto delete_and_fail; 24279b9877eeSWenchao Xia } 242852e7c241SPaolo Bonzini } 24298802d1fdSJeff Cody 2430f4de0f8cSJohn Snow QTAILQ_FOREACH(state, &snap_bdrv_states, entry) { 2431f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2432ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 24338802d1fdSJeff Cody } 2434f9ea81e8SStefan Hajnoczi } 24358802d1fdSJeff Cody 24368802d1fdSJeff Cody /* success */ 24378802d1fdSJeff Cody goto exit; 24388802d1fdSJeff Cody 24398802d1fdSJeff Cody delete_and_fail: 2440b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2441f4de0f8cSJohn Snow QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) { 2442ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2443ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2444ba0c86a3SWenchao Xia } 24458802d1fdSJeff Cody } 24468802d1fdSJeff Cody exit: 2447f4de0f8cSJohn Snow QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2448ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2449ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2450ba0c86a3SWenchao Xia } 2451ba5d6ab6SStefan Hajnoczi g_free(state); 24528802d1fdSJeff Cody } 245394d16a64SJohn Snow if (!has_props) { 245494d16a64SJohn Snow qapi_free_TransactionProperties(props); 245594d16a64SJohn Snow } 24567eaa8fb5SKevin Wolf job_txn_unref(block_job_txn); 24578802d1fdSJeff Cody } 24588802d1fdSJeff Cody 2459fbe2d816SKevin Wolf void qmp_eject(bool has_device, const char *device, 2460fbe2d816SKevin Wolf bool has_id, const char *id, 2461fbe2d816SKevin Wolf bool has_force, bool force, Error **errp) 2462666daa68SMarkus Armbruster { 246338f54bd1SMax Reitz Error *local_err = NULL; 24643a3086b7SJohn Snow int rc; 2465666daa68SMarkus Armbruster 24663a3086b7SJohn Snow if (!has_force) { 24673a3086b7SJohn Snow force = false; 24683a3086b7SJohn Snow } 24693a3086b7SJohn Snow 2470fbe2d816SKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2471fbe2d816SKevin Wolf has_id ? id : NULL, 2472fbe2d816SKevin Wolf force, &local_err); 2473bf18bee5SColin Lord if (rc && rc != -ENOSYS) { 247438f54bd1SMax Reitz error_propagate(errp, local_err); 2475c245b6a3SLuiz Capitulino return; 2476666daa68SMarkus Armbruster } 2477bf18bee5SColin Lord error_free(local_err); 24783a3086b7SJohn Snow 247982fcf66eSMax Reitz blockdev_remove_medium(has_device, device, has_id, id, errp); 2480666daa68SMarkus Armbruster } 2481666daa68SMarkus Armbruster 248212d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 248312d3ba82SBenoît Canet bool has_node_name, const char *node_name, 248412d3ba82SBenoît Canet const char *password, Error **errp) 2485666daa68SMarkus Armbruster { 2486c01c214bSDaniel P. Berrange error_setg(errp, 2487c01c214bSDaniel P. Berrange "Setting block passwords directly is no longer supported"); 2488e3442099SStefan Hajnoczi } 2489e3442099SStefan Hajnoczi 2490bf18bee5SColin Lord /* 2491bf18bee5SColin Lord * Attempt to open the tray of @device. 2492bf18bee5SColin Lord * If @force, ignore its tray lock. 2493bf18bee5SColin Lord * Else, if the tray is locked, don't open it, but ask the guest to open it. 2494bf18bee5SColin Lord * On error, store an error through @errp and return -errno. 2495bf18bee5SColin Lord * If @device does not exist, return -ENODEV. 2496bf18bee5SColin Lord * If it has no removable media, return -ENOTSUP. 2497bf18bee5SColin Lord * If it has no tray, return -ENOSYS. 2498bf18bee5SColin Lord * If the guest was asked to open the tray, return -EINPROGRESS. 2499bf18bee5SColin Lord * Else, return 0. 25003a3086b7SJohn Snow */ 2501b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id, 2502b33945cfSKevin Wolf bool force, Error **errp) 25037d8a9f71SMax Reitz { 25047d8a9f71SMax Reitz BlockBackend *blk; 2505b33945cfSKevin Wolf const char *device = qdev_id ?: blk_name; 25067d8a9f71SMax Reitz bool locked; 25077d8a9f71SMax Reitz 2508b33945cfSKevin Wolf blk = qmp_get_blk(blk_name, qdev_id, errp); 25097d8a9f71SMax Reitz if (!blk) { 25103a3086b7SJohn Snow return -ENODEV; 25117d8a9f71SMax Reitz } 25127d8a9f71SMax Reitz 25137d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 25147d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 25153a3086b7SJohn Snow return -ENOTSUP; 25167d8a9f71SMax Reitz } 25177d8a9f71SMax Reitz 251812c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 2519bf18bee5SColin Lord error_setg(errp, "Device '%s' does not have a tray", device); 2520bf18bee5SColin Lord return -ENOSYS; 252112c7ec87SMax Reitz } 252212c7ec87SMax Reitz 25237d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 25243a3086b7SJohn Snow return 0; 25257d8a9f71SMax Reitz } 25267d8a9f71SMax Reitz 25277d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 25287d8a9f71SMax Reitz if (locked) { 25297d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 25307d8a9f71SMax Reitz } 25317d8a9f71SMax Reitz 25327d8a9f71SMax Reitz if (!locked || force) { 253339829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 25347d8a9f71SMax Reitz } 25353a3086b7SJohn Snow 25363a3086b7SJohn Snow if (locked && !force) { 2537bf18bee5SColin Lord error_setg(errp, "Device '%s' is locked and force was not specified, " 2538bf18bee5SColin Lord "wait for tray to open and try again", device); 2539bf18bee5SColin Lord return -EINPROGRESS; 25403a3086b7SJohn Snow } 25413a3086b7SJohn Snow 25423a3086b7SJohn Snow return 0; 25433a3086b7SJohn Snow } 25443a3086b7SJohn Snow 2545b33945cfSKevin Wolf void qmp_blockdev_open_tray(bool has_device, const char *device, 2546b33945cfSKevin Wolf bool has_id, const char *id, 2547b33945cfSKevin Wolf bool has_force, bool force, 25483a3086b7SJohn Snow Error **errp) 25493a3086b7SJohn Snow { 2550bf18bee5SColin Lord Error *local_err = NULL; 2551bf18bee5SColin Lord int rc; 2552bf18bee5SColin Lord 25533a3086b7SJohn Snow if (!has_force) { 25543a3086b7SJohn Snow force = false; 25553a3086b7SJohn Snow } 2556b33945cfSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2557b33945cfSKevin Wolf has_id ? id : NULL, 2558b33945cfSKevin Wolf force, &local_err); 2559bf18bee5SColin Lord if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { 2560bf18bee5SColin Lord error_propagate(errp, local_err); 2561bf18bee5SColin Lord return; 2562bf18bee5SColin Lord } 2563bf18bee5SColin Lord error_free(local_err); 25647d8a9f71SMax Reitz } 25657d8a9f71SMax Reitz 2566b33945cfSKevin Wolf void qmp_blockdev_close_tray(bool has_device, const char *device, 2567b33945cfSKevin Wolf bool has_id, const char *id, 2568b33945cfSKevin Wolf Error **errp) 2569abaaf59dSMax Reitz { 2570abaaf59dSMax Reitz BlockBackend *blk; 257139829a01SKevin Wolf Error *local_err = NULL; 2572abaaf59dSMax Reitz 2573b33945cfSKevin Wolf device = has_device ? device : NULL; 2574b33945cfSKevin Wolf id = has_id ? id : NULL; 2575b33945cfSKevin Wolf 2576b33945cfSKevin Wolf blk = qmp_get_blk(device, id, errp); 2577abaaf59dSMax Reitz if (!blk) { 2578abaaf59dSMax Reitz return; 2579abaaf59dSMax Reitz } 2580abaaf59dSMax Reitz 2581abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2582b33945cfSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 2583abaaf59dSMax Reitz return; 2584abaaf59dSMax Reitz } 2585abaaf59dSMax Reitz 258612c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 258712c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 258812c7ec87SMax Reitz return; 258912c7ec87SMax Reitz } 259012c7ec87SMax Reitz 2591abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2592abaaf59dSMax Reitz return; 2593abaaf59dSMax Reitz } 2594abaaf59dSMax Reitz 259539829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 259639829a01SKevin Wolf if (local_err) { 259739829a01SKevin Wolf error_propagate(errp, local_err); 259839829a01SKevin Wolf return; 259939829a01SKevin Wolf } 2600abaaf59dSMax Reitz } 2601abaaf59dSMax Reitz 260282fcf66eSMax Reitz static void blockdev_remove_medium(bool has_device, const char *device, 260300949babSKevin Wolf bool has_id, const char *id, Error **errp) 26042814f672SMax Reitz { 26052814f672SMax Reitz BlockBackend *blk; 26062814f672SMax Reitz BlockDriverState *bs; 26072814f672SMax Reitz AioContext *aio_context; 260800949babSKevin Wolf bool has_attached_device; 26092814f672SMax Reitz 261000949babSKevin Wolf device = has_device ? device : NULL; 261100949babSKevin Wolf id = has_id ? id : NULL; 261200949babSKevin Wolf 261300949babSKevin Wolf blk = qmp_get_blk(device, id, errp); 26142814f672SMax Reitz if (!blk) { 26152814f672SMax Reitz return; 26162814f672SMax Reitz } 26172814f672SMax Reitz 26182814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 261900949babSKevin Wolf has_attached_device = blk_get_attached_dev(blk); 26202814f672SMax Reitz 262100949babSKevin Wolf if (has_attached_device && !blk_dev_has_removable_media(blk)) { 262200949babSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 26232814f672SMax Reitz return; 26242814f672SMax Reitz } 26252814f672SMax Reitz 262600949babSKevin Wolf if (has_attached_device && blk_dev_has_tray(blk) && 262700949babSKevin Wolf !blk_dev_is_tray_open(blk)) 262800949babSKevin Wolf { 262900949babSKevin Wolf error_setg(errp, "Tray of device '%s' is not open", device ?: id); 26302814f672SMax Reitz return; 26312814f672SMax Reitz } 26322814f672SMax Reitz 26332814f672SMax Reitz bs = blk_bs(blk); 26342814f672SMax Reitz if (!bs) { 26352814f672SMax Reitz return; 26362814f672SMax Reitz } 26372814f672SMax Reitz 26382814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 26392814f672SMax Reitz aio_context_acquire(aio_context); 26402814f672SMax Reitz 26412814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 26422814f672SMax Reitz goto out; 26432814f672SMax Reitz } 26442814f672SMax Reitz 26452814f672SMax Reitz blk_remove_bs(blk); 26462814f672SMax Reitz 264712c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 264812c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 264912c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 265012c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 265112c7ec87SMax Reitz * value passed here (i.e. false). */ 265239829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 265312c7ec87SMax Reitz } 265412c7ec87SMax Reitz 26552814f672SMax Reitz out: 26562814f672SMax Reitz aio_context_release(aio_context); 26572814f672SMax Reitz } 26582814f672SMax Reitz 265934ce1111SMax Reitz void qmp_blockdev_remove_medium(const char *id, Error **errp) 266082fcf66eSMax Reitz { 266182fcf66eSMax Reitz blockdev_remove_medium(false, NULL, true, id, errp); 266282fcf66eSMax Reitz } 266382fcf66eSMax Reitz 2664716df217SKevin Wolf static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, 2665d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2666d1299882SMax Reitz { 266739829a01SKevin Wolf Error *local_err = NULL; 2668d1299882SMax Reitz bool has_device; 2669d7086422SKevin Wolf int ret; 2670d1299882SMax Reitz 2671d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2672d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2673d1299882SMax Reitz 2674d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2675716df217SKevin Wolf error_setg(errp, "Device is not removable"); 2676d1299882SMax Reitz return; 2677d1299882SMax Reitz } 2678d1299882SMax Reitz 267912c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2680716df217SKevin Wolf error_setg(errp, "Tray of the device is not open"); 2681d1299882SMax Reitz return; 2682d1299882SMax Reitz } 2683d1299882SMax Reitz 2684d1299882SMax Reitz if (blk_bs(blk)) { 2685716df217SKevin Wolf error_setg(errp, "There already is a medium in the device"); 2686d1299882SMax Reitz return; 2687d1299882SMax Reitz } 2688d1299882SMax Reitz 2689d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 2690d7086422SKevin Wolf if (ret < 0) { 2691d7086422SKevin Wolf return; 2692d7086422SKevin Wolf } 2693d1299882SMax Reitz 269412c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 269512c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 269612c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 269712c7ec87SMax Reitz * slot here. 269812c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 269912c7ec87SMax Reitz * value passed here (i.e. true). */ 270039829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 270139829a01SKevin Wolf if (local_err) { 270239829a01SKevin Wolf error_propagate(errp, local_err); 270339829a01SKevin Wolf blk_remove_bs(blk); 270439829a01SKevin Wolf return; 270539829a01SKevin Wolf } 270612c7ec87SMax Reitz } 2707d1299882SMax Reitz } 2708d1299882SMax Reitz 270982fcf66eSMax Reitz static void blockdev_insert_medium(bool has_device, const char *device, 2710716df217SKevin Wolf bool has_id, const char *id, 2711716df217SKevin Wolf const char *node_name, Error **errp) 2712d1299882SMax Reitz { 2713716df217SKevin Wolf BlockBackend *blk; 2714d1299882SMax Reitz BlockDriverState *bs; 2715d1299882SMax Reitz 2716716df217SKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 2717716df217SKevin Wolf has_id ? id : NULL, 2718716df217SKevin Wolf errp); 2719716df217SKevin Wolf if (!blk) { 2720716df217SKevin Wolf return; 2721716df217SKevin Wolf } 2722716df217SKevin Wolf 2723d1299882SMax Reitz bs = bdrv_find_node(node_name); 2724d1299882SMax Reitz if (!bs) { 2725d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2726d1299882SMax Reitz return; 2727d1299882SMax Reitz } 2728d1299882SMax Reitz 27291f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2730e467da7bSKevin Wolf error_setg(errp, "Node '%s' is already in use", node_name); 2731d1299882SMax Reitz return; 2732d1299882SMax Reitz } 2733d1299882SMax Reitz 2734716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, bs, errp); 2735d1299882SMax Reitz } 2736d1299882SMax Reitz 273734ce1111SMax Reitz void qmp_blockdev_insert_medium(const char *id, const char *node_name, 273882fcf66eSMax Reitz Error **errp) 273982fcf66eSMax Reitz { 274082fcf66eSMax Reitz blockdev_insert_medium(false, NULL, true, id, node_name, errp); 274182fcf66eSMax Reitz } 274282fcf66eSMax Reitz 274370e2cb3bSKevin Wolf void qmp_blockdev_change_medium(bool has_device, const char *device, 274470e2cb3bSKevin Wolf bool has_id, const char *id, 274570e2cb3bSKevin Wolf const char *filename, 274624fb4133SMax Reitz bool has_format, const char *format, 274739ff43d9SMax Reitz bool has_read_only, 274839ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 274924fb4133SMax Reitz Error **errp) 2750de2c6c05SMax Reitz { 2751de2c6c05SMax Reitz BlockBackend *blk; 2752de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 27535b363937SMax Reitz int bdrv_flags; 2754b85114f8SKevin Wolf bool detect_zeroes; 275538a53d50SColin Lord int rc; 2756de2c6c05SMax Reitz QDict *options = NULL; 2757de2c6c05SMax Reitz Error *err = NULL; 2758de2c6c05SMax Reitz 275970e2cb3bSKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 276070e2cb3bSKevin Wolf has_id ? id : NULL, 276170e2cb3bSKevin Wolf errp); 2762de2c6c05SMax Reitz if (!blk) { 2763de2c6c05SMax Reitz goto fail; 2764de2c6c05SMax Reitz } 2765de2c6c05SMax Reitz 2766de2c6c05SMax Reitz if (blk_bs(blk)) { 2767de2c6c05SMax Reitz blk_update_root_state(blk); 2768de2c6c05SMax Reitz } 2769de2c6c05SMax Reitz 2770de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2771156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2772e35bdc12SKevin Wolf BDRV_O_PROTOCOL | BDRV_O_AUTO_RDONLY); 2773de2c6c05SMax Reitz 277439ff43d9SMax Reitz if (!has_read_only) { 277539ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 277639ff43d9SMax Reitz } 277739ff43d9SMax Reitz 277839ff43d9SMax Reitz switch (read_only) { 277939ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 278039ff43d9SMax Reitz break; 278139ff43d9SMax Reitz 278239ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 278339ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 278439ff43d9SMax Reitz break; 278539ff43d9SMax Reitz 278639ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 278739ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 278839ff43d9SMax Reitz break; 278939ff43d9SMax Reitz 279039ff43d9SMax Reitz default: 279139ff43d9SMax Reitz abort(); 279239ff43d9SMax Reitz } 279339ff43d9SMax Reitz 2794de2c6c05SMax Reitz options = qdict_new(); 2795b85114f8SKevin Wolf detect_zeroes = blk_get_detect_zeroes_from_root_state(blk); 279646f5ac20SEric Blake qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off"); 2797b85114f8SKevin Wolf 2798b85114f8SKevin Wolf if (has_format) { 279946f5ac20SEric Blake qdict_put_str(options, "driver", format); 2800de2c6c05SMax Reitz } 2801de2c6c05SMax Reitz 28025b363937SMax Reitz medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 28035b363937SMax Reitz if (!medium_bs) { 2804de2c6c05SMax Reitz goto fail; 2805de2c6c05SMax Reitz } 2806de2c6c05SMax Reitz 280770e2cb3bSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 280870e2cb3bSKevin Wolf has_id ? id : NULL, 280970e2cb3bSKevin Wolf false, &err); 281038a53d50SColin Lord if (rc && rc != -ENOSYS) { 2811de2c6c05SMax Reitz error_propagate(errp, err); 2812de2c6c05SMax Reitz goto fail; 2813de2c6c05SMax Reitz } 281438a53d50SColin Lord error_free(err); 281538a53d50SColin Lord err = NULL; 2816de2c6c05SMax Reitz 281782fcf66eSMax Reitz blockdev_remove_medium(has_device, device, has_id, id, &err); 2818de2c6c05SMax Reitz if (err) { 2819de2c6c05SMax Reitz error_propagate(errp, err); 2820de2c6c05SMax Reitz goto fail; 2821de2c6c05SMax Reitz } 2822de2c6c05SMax Reitz 2823716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, medium_bs, &err); 2824de2c6c05SMax Reitz if (err) { 2825de2c6c05SMax Reitz error_propagate(errp, err); 2826de2c6c05SMax Reitz goto fail; 2827de2c6c05SMax Reitz } 2828de2c6c05SMax Reitz 282970e2cb3bSKevin Wolf qmp_blockdev_close_tray(has_device, device, has_id, id, errp); 2830de2c6c05SMax Reitz 2831de2c6c05SMax Reitz fail: 2832de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2833de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2834de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2835de2c6c05SMax Reitz bdrv_unref(medium_bs); 2836de2c6c05SMax Reitz } 2837de2c6c05SMax Reitz 2838727f005eSZhi Yong Wu /* throttling disk I/O limits */ 28394dc9397bSEric Blake void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) 2840727f005eSZhi Yong Wu { 2841cc0681c4SBenoît Canet ThrottleConfig cfg; 2842727f005eSZhi Yong Wu BlockDriverState *bs; 2843a0e8544cSFam Zheng BlockBackend *blk; 2844b15446fdSStefan Hajnoczi AioContext *aio_context; 2845727f005eSZhi Yong Wu 28467a9877a0SKevin Wolf blk = qmp_get_blk(arg->has_device ? arg->device : NULL, 28477a9877a0SKevin Wolf arg->has_id ? arg->id : NULL, 28487a9877a0SKevin Wolf errp); 2849a0e8544cSFam Zheng if (!blk) { 285080047da5SLuiz Capitulino return; 2851727f005eSZhi Yong Wu } 28525433c24fSMax Reitz 28535433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 28545433c24fSMax Reitz aio_context_acquire(aio_context); 28555433c24fSMax Reitz 2856a0e8544cSFam Zheng bs = blk_bs(blk); 28575433c24fSMax Reitz if (!bs) { 28587a9877a0SKevin Wolf error_setg(errp, "Device has no medium"); 28595433c24fSMax Reitz goto out; 28605433c24fSMax Reitz } 2861727f005eSZhi Yong Wu 28621588ab5dSAlberto Garcia throttle_config_init(&cfg); 28634dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; 28644dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; 28654dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; 2866727f005eSZhi Yong Wu 28674dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; 28684dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; 28694dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; 2870cc0681c4SBenoît Canet 28714dc9397bSEric Blake if (arg->has_bps_max) { 28724dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; 28733e9fab69SBenoît Canet } 28744dc9397bSEric Blake if (arg->has_bps_rd_max) { 28754dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; 28763e9fab69SBenoît Canet } 28774dc9397bSEric Blake if (arg->has_bps_wr_max) { 28784dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; 28793e9fab69SBenoît Canet } 28804dc9397bSEric Blake if (arg->has_iops_max) { 28814dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; 28823e9fab69SBenoît Canet } 28834dc9397bSEric Blake if (arg->has_iops_rd_max) { 28844dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; 28853e9fab69SBenoît Canet } 28864dc9397bSEric Blake if (arg->has_iops_wr_max) { 28874dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; 28883e9fab69SBenoît Canet } 2889cc0681c4SBenoît Canet 28904dc9397bSEric Blake if (arg->has_bps_max_length) { 28914dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; 2892dce13204SAlberto Garcia } 28934dc9397bSEric Blake if (arg->has_bps_rd_max_length) { 28944dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; 2895dce13204SAlberto Garcia } 28964dc9397bSEric Blake if (arg->has_bps_wr_max_length) { 28974dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; 2898dce13204SAlberto Garcia } 28994dc9397bSEric Blake if (arg->has_iops_max_length) { 29004dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; 2901dce13204SAlberto Garcia } 29024dc9397bSEric Blake if (arg->has_iops_rd_max_length) { 29034dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; 2904dce13204SAlberto Garcia } 29054dc9397bSEric Blake if (arg->has_iops_wr_max_length) { 29064dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; 2907dce13204SAlberto Garcia } 2908dce13204SAlberto Garcia 29094dc9397bSEric Blake if (arg->has_iops_size) { 29104dc9397bSEric Blake cfg.op_size = arg->iops_size; 29112024c1dfSBenoît Canet } 2912cc0681c4SBenoît Canet 2913d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 29145433c24fSMax Reitz goto out; 2915727f005eSZhi Yong Wu } 2916727f005eSZhi Yong Wu 291776f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 291876f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 291976f4afb4SAlberto Garcia * just update the throttling group. */ 2920022cdc9fSManos Pitsidianakis if (!blk_get_public(blk)->throttle_group_member.throttle_state) { 29214dc9397bSEric Blake blk_io_limits_enable(blk, 29227a9877a0SKevin Wolf arg->has_group ? arg->group : 29237a9877a0SKevin Wolf arg->has_device ? arg->device : 29247a9877a0SKevin Wolf arg->id); 29254dc9397bSEric Blake } else if (arg->has_group) { 29264dc9397bSEric Blake blk_io_limits_update_group(blk, arg->group); 2927727f005eSZhi Yong Wu } 292876f4afb4SAlberto Garcia /* Set the new throttling configuration */ 292997148076SKevin Wolf blk_set_io_limits(blk, &cfg); 2930022cdc9fSManos Pitsidianakis } else if (blk_get_public(blk)->throttle_group_member.throttle_state) { 293176f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 293297148076SKevin Wolf blk_io_limits_disable(blk); 2933727f005eSZhi Yong Wu } 2934b15446fdSStefan Hajnoczi 29355433c24fSMax Reitz out: 2936b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2937727f005eSZhi Yong Wu } 2938727f005eSZhi Yong Wu 2939341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2940341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2941fd5ae4ccSVladimir Sementsov-Ogievskiy bool has_persistent, bool persistent, 2942a6e2ca5fSVladimir Sementsov-Ogievskiy bool has_disabled, bool disabled, 2943341ebc2fSJohn Snow Error **errp) 2944341ebc2fSJohn Snow { 2945341ebc2fSJohn Snow BlockDriverState *bs; 2946fd5ae4ccSVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 2947341ebc2fSJohn Snow 2948341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2949341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2950341ebc2fSJohn Snow return; 2951341ebc2fSJohn Snow } 2952341ebc2fSJohn Snow 2953341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2954341ebc2fSJohn Snow if (!bs) { 2955341ebc2fSJohn Snow return; 2956341ebc2fSJohn Snow } 2957341ebc2fSJohn Snow 2958341ebc2fSJohn Snow if (has_granularity) { 2959341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2960341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2961341ebc2fSJohn Snow "and at least 512"); 29622119882cSPaolo Bonzini return; 2963341ebc2fSJohn Snow } 2964341ebc2fSJohn Snow } else { 2965341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2966341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2967341ebc2fSJohn Snow } 2968341ebc2fSJohn Snow 2969fd5ae4ccSVladimir Sementsov-Ogievskiy if (!has_persistent) { 2970fd5ae4ccSVladimir Sementsov-Ogievskiy persistent = false; 2971fd5ae4ccSVladimir Sementsov-Ogievskiy } 2972eb738bb5SVladimir Sementsov-Ogievskiy 2973a6e2ca5fSVladimir Sementsov-Ogievskiy if (!has_disabled) { 2974a6e2ca5fSVladimir Sementsov-Ogievskiy disabled = false; 2975a6e2ca5fSVladimir Sementsov-Ogievskiy } 2976a6e2ca5fSVladimir Sementsov-Ogievskiy 2977d2c3080eSVladimir Sementsov-Ogievskiy if (persistent && 2978d2c3080eSVladimir Sementsov-Ogievskiy !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp)) 2979d2c3080eSVladimir Sementsov-Ogievskiy { 29802899f41eSVladimir Sementsov-Ogievskiy return; 29810a6c86d0SJohn Snow } 2982fd5ae4ccSVladimir Sementsov-Ogievskiy 2983fd5ae4ccSVladimir Sementsov-Ogievskiy bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2984eb738bb5SVladimir Sementsov-Ogievskiy if (bitmap == NULL) { 29852899f41eSVladimir Sementsov-Ogievskiy return; 2986fd5ae4ccSVladimir Sementsov-Ogievskiy } 2987eb738bb5SVladimir Sementsov-Ogievskiy 2988a6e2ca5fSVladimir Sementsov-Ogievskiy if (disabled) { 2989a6e2ca5fSVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(bitmap); 2990a6e2ca5fSVladimir Sementsov-Ogievskiy } 2991a6e2ca5fSVladimir Sementsov-Ogievskiy 2992796a3798SEric Blake bdrv_dirty_bitmap_set_persistence(bitmap, persistent); 2993341ebc2fSJohn Snow } 2994341ebc2fSJohn Snow 2995c4e4b0faSJohn Snow static BdrvDirtyBitmap *do_block_dirty_bitmap_remove( 2996c4e4b0faSJohn Snow const char *node, const char *name, bool release, 2997c4e4b0faSJohn Snow BlockDriverState **bitmap_bs, Error **errp) 2998341ebc2fSJohn Snow { 2999341ebc2fSJohn Snow BlockDriverState *bs; 3000341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 3001341ebc2fSJohn Snow 30022119882cSPaolo Bonzini bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 3003341ebc2fSJohn Snow if (!bitmap || !bs) { 3004c4e4b0faSJohn Snow return NULL; 3005341ebc2fSJohn Snow } 3006341ebc2fSJohn Snow 3007c3edf13cSJohn Snow if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY | BDRV_BITMAP_RO, 3008c3edf13cSJohn Snow errp)) { 3009c4e4b0faSJohn Snow return NULL; 30109bd2b08fSJohn Snow } 30115c36c1afSVladimir Sementsov-Ogievskiy 3012d2c3080eSVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_get_persistence(bitmap) && 3013d2c3080eSVladimir Sementsov-Ogievskiy bdrv_remove_persistent_dirty_bitmap(bs, name, errp) < 0) 3014d2c3080eSVladimir Sementsov-Ogievskiy { 3015c4e4b0faSJohn Snow return NULL; 30165c36c1afSVladimir Sementsov-Ogievskiy } 30175c36c1afSVladimir Sementsov-Ogievskiy 3018c4e4b0faSJohn Snow if (release) { 30195deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(bitmap); 3020341ebc2fSJohn Snow } 3021341ebc2fSJohn Snow 3022c4e4b0faSJohn Snow if (bitmap_bs) { 3023c4e4b0faSJohn Snow *bitmap_bs = bs; 3024c4e4b0faSJohn Snow } 3025c4e4b0faSJohn Snow 3026c4e4b0faSJohn Snow return release ? NULL : bitmap; 3027c4e4b0faSJohn Snow } 3028c4e4b0faSJohn Snow 3029c4e4b0faSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 3030c4e4b0faSJohn Snow Error **errp) 3031c4e4b0faSJohn Snow { 3032c4e4b0faSJohn Snow do_block_dirty_bitmap_remove(node, name, true, NULL, errp); 3033c4e4b0faSJohn Snow } 3034c4e4b0faSJohn Snow 3035e74e6b78SJohn Snow /** 3036e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 3037e74e6b78SJohn Snow * immediately after a full backup operation. 3038e74e6b78SJohn Snow */ 3039e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 3040e74e6b78SJohn Snow Error **errp) 3041e74e6b78SJohn Snow { 3042e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 3043e74e6b78SJohn Snow BlockDriverState *bs; 3044e74e6b78SJohn Snow 30452119882cSPaolo Bonzini bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 3046e74e6b78SJohn Snow if (!bitmap || !bs) { 3047e74e6b78SJohn Snow return; 3048e74e6b78SJohn Snow } 3049e74e6b78SJohn Snow 30503ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) { 3051d6883bc9SVladimir Sementsov-Ogievskiy return; 3052e74e6b78SJohn Snow } 3053e74e6b78SJohn Snow 3054df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 3055e74e6b78SJohn Snow } 3056e74e6b78SJohn Snow 30570e2b7f09SJohn Snow void qmp_block_dirty_bitmap_enable(const char *node, const char *name, 30585c5d2e50SVladimir Sementsov-Ogievskiy Error **errp) 30595c5d2e50SVladimir Sementsov-Ogievskiy { 30605c5d2e50SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 30615c5d2e50SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 30625c5d2e50SVladimir Sementsov-Ogievskiy 30635c5d2e50SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 30645c5d2e50SVladimir Sementsov-Ogievskiy if (!bitmap) { 30655c5d2e50SVladimir Sementsov-Ogievskiy return; 30665c5d2e50SVladimir Sementsov-Ogievskiy } 30675c5d2e50SVladimir Sementsov-Ogievskiy 30683ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { 30695c5d2e50SVladimir Sementsov-Ogievskiy return; 30705c5d2e50SVladimir Sementsov-Ogievskiy } 30715c5d2e50SVladimir Sementsov-Ogievskiy 30725c5d2e50SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(bitmap); 30735c5d2e50SVladimir Sementsov-Ogievskiy } 30745c5d2e50SVladimir Sementsov-Ogievskiy 30750e2b7f09SJohn Snow void qmp_block_dirty_bitmap_disable(const char *node, const char *name, 30765c5d2e50SVladimir Sementsov-Ogievskiy Error **errp) 30775c5d2e50SVladimir Sementsov-Ogievskiy { 30785c5d2e50SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 30795c5d2e50SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 30805c5d2e50SVladimir Sementsov-Ogievskiy 30815c5d2e50SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 30825c5d2e50SVladimir Sementsov-Ogievskiy if (!bitmap) { 30835c5d2e50SVladimir Sementsov-Ogievskiy return; 30845c5d2e50SVladimir Sementsov-Ogievskiy } 30855c5d2e50SVladimir Sementsov-Ogievskiy 30863ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { 30875c5d2e50SVladimir Sementsov-Ogievskiy return; 30885c5d2e50SVladimir Sementsov-Ogievskiy } 30895c5d2e50SVladimir Sementsov-Ogievskiy 30905c5d2e50SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(bitmap); 30915c5d2e50SVladimir Sementsov-Ogievskiy } 30925c5d2e50SVladimir Sementsov-Ogievskiy 3093eff0829bSVladimir Sementsov-Ogievskiy static BdrvDirtyBitmap *do_block_dirty_bitmap_merge( 3094eff0829bSVladimir Sementsov-Ogievskiy const char *node, const char *target, 3095eff0829bSVladimir Sementsov-Ogievskiy BlockDirtyBitmapMergeSourceList *bitmaps, 3096eff0829bSVladimir Sementsov-Ogievskiy HBitmap **backup, Error **errp) 3097b598e531SVladimir Sementsov-Ogievskiy { 3098b598e531SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 3099360d4e4eSJohn Snow BdrvDirtyBitmap *dst, *src, *anon; 3100eff0829bSVladimir Sementsov-Ogievskiy BlockDirtyBitmapMergeSourceList *lst; 3101360d4e4eSJohn Snow Error *local_err = NULL; 3102b598e531SVladimir Sementsov-Ogievskiy 3103360d4e4eSJohn Snow dst = block_dirty_bitmap_lookup(node, target, &bs, errp); 3104b598e531SVladimir Sementsov-Ogievskiy if (!dst) { 3105360d4e4eSJohn Snow return NULL; 3106b598e531SVladimir Sementsov-Ogievskiy } 3107b598e531SVladimir Sementsov-Ogievskiy 3108360d4e4eSJohn Snow anon = bdrv_create_dirty_bitmap(bs, bdrv_dirty_bitmap_granularity(dst), 3109360d4e4eSJohn Snow NULL, errp); 3110360d4e4eSJohn Snow if (!anon) { 3111360d4e4eSJohn Snow return NULL; 3112360d4e4eSJohn Snow } 3113360d4e4eSJohn Snow 3114360d4e4eSJohn Snow for (lst = bitmaps; lst; lst = lst->next) { 3115eff0829bSVladimir Sementsov-Ogievskiy switch (lst->value->type) { 3116eff0829bSVladimir Sementsov-Ogievskiy const char *name, *node; 3117eff0829bSVladimir Sementsov-Ogievskiy case QTYPE_QSTRING: 3118eff0829bSVladimir Sementsov-Ogievskiy name = lst->value->u.local; 3119eff0829bSVladimir Sementsov-Ogievskiy src = bdrv_find_dirty_bitmap(bs, name); 3120b598e531SVladimir Sementsov-Ogievskiy if (!src) { 3121eff0829bSVladimir Sementsov-Ogievskiy error_setg(errp, "Dirty bitmap '%s' not found", name); 3122360d4e4eSJohn Snow dst = NULL; 3123360d4e4eSJohn Snow goto out; 3124b598e531SVladimir Sementsov-Ogievskiy } 3125eff0829bSVladimir Sementsov-Ogievskiy break; 3126eff0829bSVladimir Sementsov-Ogievskiy case QTYPE_QDICT: 3127eff0829bSVladimir Sementsov-Ogievskiy node = lst->value->u.external.node; 3128eff0829bSVladimir Sementsov-Ogievskiy name = lst->value->u.external.name; 3129eff0829bSVladimir Sementsov-Ogievskiy src = block_dirty_bitmap_lookup(node, name, NULL, errp); 3130eff0829bSVladimir Sementsov-Ogievskiy if (!src) { 3131eff0829bSVladimir Sementsov-Ogievskiy dst = NULL; 3132eff0829bSVladimir Sementsov-Ogievskiy goto out; 3133eff0829bSVladimir Sementsov-Ogievskiy } 3134eff0829bSVladimir Sementsov-Ogievskiy break; 3135eff0829bSVladimir Sementsov-Ogievskiy default: 3136eff0829bSVladimir Sementsov-Ogievskiy abort(); 3137eff0829bSVladimir Sementsov-Ogievskiy } 3138b598e531SVladimir Sementsov-Ogievskiy 3139360d4e4eSJohn Snow bdrv_merge_dirty_bitmap(anon, src, NULL, &local_err); 3140360d4e4eSJohn Snow if (local_err) { 3141360d4e4eSJohn Snow error_propagate(errp, local_err); 3142360d4e4eSJohn Snow dst = NULL; 3143360d4e4eSJohn Snow goto out; 3144360d4e4eSJohn Snow } 3145360d4e4eSJohn Snow } 3146360d4e4eSJohn Snow 3147360d4e4eSJohn Snow /* Merge into dst; dst is unchanged on failure. */ 3148360d4e4eSJohn Snow bdrv_merge_dirty_bitmap(dst, anon, backup, errp); 3149360d4e4eSJohn Snow 3150360d4e4eSJohn Snow out: 31515deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(anon); 3152360d4e4eSJohn Snow return dst; 3153360d4e4eSJohn Snow } 3154360d4e4eSJohn Snow 31550e2b7f09SJohn Snow void qmp_block_dirty_bitmap_merge(const char *node, const char *target, 3156eff0829bSVladimir Sementsov-Ogievskiy BlockDirtyBitmapMergeSourceList *bitmaps, 3157eff0829bSVladimir Sementsov-Ogievskiy Error **errp) 3158360d4e4eSJohn Snow { 3159360d4e4eSJohn Snow do_block_dirty_bitmap_merge(node, target, bitmaps, NULL, errp); 3160b598e531SVladimir Sementsov-Ogievskiy } 3161b598e531SVladimir Sementsov-Ogievskiy 3162a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, 3163a3b52535SVladimir Sementsov-Ogievskiy const char *name, 3164a3b52535SVladimir Sementsov-Ogievskiy Error **errp) 3165a3b52535SVladimir Sementsov-Ogievskiy { 3166a3b52535SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 3167a3b52535SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 3168a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *ret = NULL; 3169a3b52535SVladimir Sementsov-Ogievskiy char *sha256; 3170a3b52535SVladimir Sementsov-Ogievskiy 3171a3b52535SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 3172a3b52535SVladimir Sementsov-Ogievskiy if (!bitmap || !bs) { 3173a3b52535SVladimir Sementsov-Ogievskiy return NULL; 3174a3b52535SVladimir Sementsov-Ogievskiy } 3175a3b52535SVladimir Sementsov-Ogievskiy 3176a3b52535SVladimir Sementsov-Ogievskiy sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp); 3177a3b52535SVladimir Sementsov-Ogievskiy if (sha256 == NULL) { 3178a3b52535SVladimir Sementsov-Ogievskiy return NULL; 3179a3b52535SVladimir Sementsov-Ogievskiy } 3180a3b52535SVladimir Sementsov-Ogievskiy 3181a3b52535SVladimir Sementsov-Ogievskiy ret = g_new(BlockDirtyBitmapSha256, 1); 3182a3b52535SVladimir Sementsov-Ogievskiy ret->sha256 = sha256; 3183a3b52535SVladimir Sementsov-Ogievskiy 3184a3b52535SVladimir Sementsov-Ogievskiy return ret; 3185a3b52535SVladimir Sementsov-Ogievskiy } 3186a3b52535SVladimir Sementsov-Ogievskiy 3187072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 31889063f814SRyan Harper { 31899063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 31907e7d56d9SMarkus Armbruster BlockBackend *blk; 31919063f814SRyan Harper BlockDriverState *bs; 31928ad4202bSStefan Hajnoczi AioContext *aio_context; 31933718d8abSFam Zheng Error *local_err = NULL; 31949063f814SRyan Harper 31952073d410SKevin Wolf bs = bdrv_find_node(id); 31962073d410SKevin Wolf if (bs) { 319779b7a77eSMarkus Armbruster qmp_blockdev_del(id, &local_err); 31982073d410SKevin Wolf if (local_err) { 31992073d410SKevin Wolf error_report_err(local_err); 32002073d410SKevin Wolf } 32012073d410SKevin Wolf return; 32022073d410SKevin Wolf } 32032073d410SKevin Wolf 32047e7d56d9SMarkus Armbruster blk = blk_by_name(id); 32057e7d56d9SMarkus Armbruster if (!blk) { 3206b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 3207072ebe6bSMarkus Armbruster return; 32089063f814SRyan Harper } 32098ad4202bSStefan Hajnoczi 321026f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 321148f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 321248f364ddSMarkus Armbruster " is not supported"); 3213072ebe6bSMarkus Armbruster return; 321448f364ddSMarkus Armbruster } 321548f364ddSMarkus Armbruster 32165433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 32178ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 32188ad4202bSStefan Hajnoczi 32195433c24fSMax Reitz bs = blk_bs(blk); 32205433c24fSMax Reitz if (bs) { 32213718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 3222565f65d2SMarkus Armbruster error_report_err(local_err); 32238ad4202bSStefan Hajnoczi aio_context_release(aio_context); 3224072ebe6bSMarkus Armbruster return; 32258591675fSMarcelo Tosatti } 32269063f814SRyan Harper 3227938abd43SMax Reitz blk_remove_bs(blk); 32285433c24fSMax Reitz } 32299063f814SRyan Harper 32307c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 3231efaa7c4eSMax Reitz monitor_remove_blk(blk); 3232efaa7c4eSMax Reitz 32337c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 32347c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 3235d22b2f41SRyan Harper */ 3236a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 3237293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 3238373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 3239293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 3240d22b2f41SRyan Harper } else { 3241b9fe8a7aSMarkus Armbruster blk_unref(blk); 3242d22b2f41SRyan Harper } 32439063f814SRyan Harper 32448ad4202bSStefan Hajnoczi aio_context_release(aio_context); 32459063f814SRyan Harper } 32466d4a2b3aSChristoph Hellwig 32473b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 32483b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 32493b1dbd11SBenoît Canet int64_t size, Error **errp) 32506d4a2b3aSChristoph Hellwig { 32513b1dbd11SBenoît Canet Error *local_err = NULL; 32527dad9ee6SKevin Wolf BlockBackend *blk = NULL; 32536d4a2b3aSChristoph Hellwig BlockDriverState *bs; 3254927e0e76SStefan Hajnoczi AioContext *aio_context; 32558732901eSKevin Wolf int ret; 32566d4a2b3aSChristoph Hellwig 32573b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 32583b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 32593b1dbd11SBenoît Canet &local_err); 326084d18f06SMarkus Armbruster if (local_err) { 32613b1dbd11SBenoît Canet error_propagate(errp, local_err); 32623b1dbd11SBenoît Canet return; 32633b1dbd11SBenoît Canet } 32643b1dbd11SBenoît Canet 3265927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 3266927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 3267927e0e76SStefan Hajnoczi 32683b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 3269c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 3270927e0e76SStefan Hajnoczi goto out; 32716d4a2b3aSChristoph Hellwig } 32726d4a2b3aSChristoph Hellwig 32736d4a2b3aSChristoph Hellwig if (size < 0) { 3274c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 3275927e0e76SStefan Hajnoczi goto out; 32766d4a2b3aSChristoph Hellwig } 32776d4a2b3aSChristoph Hellwig 32789c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 3279c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 3280927e0e76SStefan Hajnoczi goto out; 32819c75e168SJeff Cody } 32829c75e168SJeff Cody 3283d861ab3aSKevin Wolf blk = blk_new(bdrv_get_aio_context(bs), BLK_PERM_RESIZE, BLK_PERM_ALL); 3284d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 3285d7086422SKevin Wolf if (ret < 0) { 3286d7086422SKevin Wolf goto out; 3287d7086422SKevin Wolf } 32887dad9ee6SKevin Wolf 3289698bdfa0SJohn Snow bdrv_drained_begin(bs); 3290c80d8b06SMax Reitz ret = blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp); 3291698bdfa0SJohn Snow bdrv_drained_end(bs); 3292927e0e76SStefan Hajnoczi 3293927e0e76SStefan Hajnoczi out: 32947dad9ee6SKevin Wolf blk_unref(blk); 3295927e0e76SStefan Hajnoczi aio_context_release(aio_context); 32966d4a2b3aSChristoph Hellwig } 329712bd451fSStefan Hajnoczi 32982323322eSAlberto Garcia void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, 329913d8cc51SJeff Cody bool has_base, const char *base, 3300312fe09cSAlberto Garcia bool has_base_node, const char *base_node, 330113d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 330213d8cc51SJeff Cody bool has_speed, int64_t speed, 33031d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 3304241ca1abSJohn Snow bool has_auto_finalize, bool auto_finalize, 3305241ca1abSJohn Snow bool has_auto_dismiss, bool auto_dismiss, 33061d809098SPaolo Bonzini Error **errp) 330712bd451fSStefan Hajnoczi { 3308554b6147SAlberto Garcia BlockDriverState *bs, *iter; 3309c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 3310f3e69bebSStefan Hajnoczi AioContext *aio_context; 3311fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 331213d8cc51SJeff Cody const char *base_name = NULL; 3313cf6320dfSJohn Snow int job_flags = JOB_DEFAULT; 331412bd451fSStefan Hajnoczi 33151d809098SPaolo Bonzini if (!has_on_error) { 33161d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 33171d809098SPaolo Bonzini } 33181d809098SPaolo Bonzini 3319554b6147SAlberto Garcia bs = bdrv_lookup_bs(device, device, errp); 3320b6c1bae5SKevin Wolf if (!bs) { 332112bd451fSStefan Hajnoczi return; 332212bd451fSStefan Hajnoczi } 332312bd451fSStefan Hajnoczi 3324b6c1bae5SKevin Wolf aio_context = bdrv_get_aio_context(bs); 3325f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 3326f3e69bebSStefan Hajnoczi 3327312fe09cSAlberto Garcia if (has_base && has_base_node) { 3328312fe09cSAlberto Garcia error_setg(errp, "'base' and 'base-node' cannot be specified " 3329312fe09cSAlberto Garcia "at the same time"); 3330312fe09cSAlberto Garcia goto out; 3331312fe09cSAlberto Garcia } 3332312fe09cSAlberto Garcia 333313d8cc51SJeff Cody if (has_base) { 3334c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3335c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3336c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3337f3e69bebSStefan Hajnoczi goto out; 333812bd451fSStefan Hajnoczi } 3339f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 334013d8cc51SJeff Cody base_name = base; 3341c8c3080fSMarcelo Tosatti } 334212bd451fSStefan Hajnoczi 3343312fe09cSAlberto Garcia if (has_base_node) { 3344312fe09cSAlberto Garcia base_bs = bdrv_lookup_bs(NULL, base_node, errp); 3345312fe09cSAlberto Garcia if (!base_bs) { 3346312fe09cSAlberto Garcia goto out; 3347312fe09cSAlberto Garcia } 3348312fe09cSAlberto Garcia if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { 3349312fe09cSAlberto Garcia error_setg(errp, "Node '%s' is not a backing image of '%s'", 3350312fe09cSAlberto Garcia base_node, device); 3351312fe09cSAlberto Garcia goto out; 3352312fe09cSAlberto Garcia } 3353312fe09cSAlberto Garcia assert(bdrv_get_aio_context(base_bs) == aio_context); 3354f30c66baSMax Reitz bdrv_refresh_filename(base_bs); 3355312fe09cSAlberto Garcia base_name = base_bs->filename; 3356312fe09cSAlberto Garcia } 3357312fe09cSAlberto Garcia 3358554b6147SAlberto Garcia /* Check for op blockers in the whole chain between bs and base */ 3359554b6147SAlberto Garcia for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) { 3360554b6147SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { 3361554b6147SAlberto Garcia goto out; 3362554b6147SAlberto Garcia } 3363554b6147SAlberto Garcia } 3364554b6147SAlberto Garcia 336513d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 336613d8cc51SJeff Cody * file, and specifying one is therefore an error */ 336713d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 336813d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 336913d8cc51SJeff Cody "entire chain"); 3370f3e69bebSStefan Hajnoczi goto out; 337113d8cc51SJeff Cody } 337213d8cc51SJeff Cody 337313d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 337413d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 337513d8cc51SJeff Cody 3376241ca1abSJohn Snow if (has_auto_finalize && !auto_finalize) { 3377241ca1abSJohn Snow job_flags |= JOB_MANUAL_FINALIZE; 3378241ca1abSJohn Snow } 3379241ca1abSJohn Snow if (has_auto_dismiss && !auto_dismiss) { 3380241ca1abSJohn Snow job_flags |= JOB_MANUAL_DISMISS; 3381241ca1abSJohn Snow } 3382241ca1abSJohn Snow 33832323322eSAlberto Garcia stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, 3384cf6320dfSJohn Snow job_flags, has_speed ? speed : 0, on_error, &local_err); 338584d18f06SMarkus Armbruster if (local_err) { 3386fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3387f3e69bebSStefan Hajnoczi goto out; 338812bd451fSStefan Hajnoczi } 338912bd451fSStefan Hajnoczi 3390b23c580cSVladimir Sementsov-Ogievskiy trace_qmp_block_stream(bs); 3391f3e69bebSStefan Hajnoczi 3392f3e69bebSStefan Hajnoczi out: 3393f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 339412bd451fSStefan Hajnoczi } 33952d47c6e9SStefan Hajnoczi 3396fd62c609SAlberto Garcia void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, 33973c605f40SKevin Wolf bool has_base_node, const char *base_node, 33987676e2c5SJeff Cody bool has_base, const char *base, 33993c605f40SKevin Wolf bool has_top_node, const char *top_node, 34007676e2c5SJeff Cody bool has_top, const char *top, 340154e26900SJeff Cody bool has_backing_file, const char *backing_file, 3402ed61fc10SJeff Cody bool has_speed, int64_t speed, 34030db832f4SKevin Wolf bool has_filter_node_name, const char *filter_node_name, 340496fbf534SJohn Snow bool has_auto_finalize, bool auto_finalize, 340596fbf534SJohn Snow bool has_auto_dismiss, bool auto_dismiss, 3406ed61fc10SJeff Cody Error **errp) 3407ed61fc10SJeff Cody { 3408ed61fc10SJeff Cody BlockDriverState *bs; 3409058223a6SAlberto Garcia BlockDriverState *iter; 3410ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 34119e85cd5cSStefan Hajnoczi AioContext *aio_context; 3412ed61fc10SJeff Cody Error *local_err = NULL; 3413ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3414ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3415ed61fc10SJeff Cody */ 341692aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 34175360782dSJohn Snow int job_flags = JOB_DEFAULT; 3418ed61fc10SJeff Cody 341954504663SMax Reitz if (!has_speed) { 342054504663SMax Reitz speed = 0; 342154504663SMax Reitz } 34220db832f4SKevin Wolf if (!has_filter_node_name) { 34230db832f4SKevin Wolf filter_node_name = NULL; 34240db832f4SKevin Wolf } 342596fbf534SJohn Snow if (has_auto_finalize && !auto_finalize) { 342696fbf534SJohn Snow job_flags |= JOB_MANUAL_FINALIZE; 342796fbf534SJohn Snow } 342896fbf534SJohn Snow if (has_auto_dismiss && !auto_dismiss) { 342996fbf534SJohn Snow job_flags |= JOB_MANUAL_DISMISS; 343096fbf534SJohn Snow } 343154504663SMax Reitz 34327676e2c5SJeff Cody /* Important Note: 34337676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 34347676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 34357676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 34367676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 34371d13b167SKevin Wolf bs = qmp_get_root_bs(device, &local_err); 34381d13b167SKevin Wolf if (!bs) { 34391d13b167SKevin Wolf bs = bdrv_lookup_bs(device, device, NULL); 34401d13b167SKevin Wolf if (!bs) { 34411d13b167SKevin Wolf error_free(local_err); 344275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 344375158ebbSMarkus Armbruster "Device '%s' not found", device); 34441d13b167SKevin Wolf } else { 34451d13b167SKevin Wolf error_propagate(errp, local_err); 34461d13b167SKevin Wolf } 3447ed61fc10SJeff Cody return; 3448ed61fc10SJeff Cody } 3449ed61fc10SJeff Cody 34501d13b167SKevin Wolf aio_context = bdrv_get_aio_context(bs); 34519e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 34529e85cd5cSStefan Hajnoczi 3453bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 34549e85cd5cSStefan Hajnoczi goto out; 3455628ff683SFam Zheng } 3456628ff683SFam Zheng 3457ed61fc10SJeff Cody /* default top_bs is the active layer */ 3458ed61fc10SJeff Cody top_bs = bs; 3459ed61fc10SJeff Cody 34603c605f40SKevin Wolf if (has_top_node && has_top) { 34613c605f40SKevin Wolf error_setg(errp, "'top-node' and 'top' are mutually exclusive"); 34623c605f40SKevin Wolf goto out; 34633c605f40SKevin Wolf } else if (has_top_node) { 34643c605f40SKevin Wolf top_bs = bdrv_lookup_bs(NULL, top_node, errp); 34653c605f40SKevin Wolf if (top_bs == NULL) { 34663c605f40SKevin Wolf goto out; 34673c605f40SKevin Wolf } 34683c605f40SKevin Wolf if (!bdrv_chain_contains(bs, top_bs)) { 34693c605f40SKevin Wolf error_setg(errp, "'%s' is not in this backing file chain", 34703c605f40SKevin Wolf top_node); 34713c605f40SKevin Wolf goto out; 34723c605f40SKevin Wolf } 34733c605f40SKevin Wolf } else if (has_top && top) { 3474f30c66baSMax Reitz /* This strcmp() is just a shortcut, there is no need to 3475f30c66baSMax Reitz * refresh @bs's filename. If it mismatches, 3476f30c66baSMax Reitz * bdrv_find_backing_image() will do the refresh and may still 3477f30c66baSMax Reitz * return @bs. */ 3478ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3479ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3480ed61fc10SJeff Cody } 3481ed61fc10SJeff Cody } 3482ed61fc10SJeff Cody 3483ed61fc10SJeff Cody if (top_bs == NULL) { 3484ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 34859e85cd5cSStefan Hajnoczi goto out; 3486ed61fc10SJeff Cody } 3487ed61fc10SJeff Cody 34889e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 34899e85cd5cSStefan Hajnoczi 34903c605f40SKevin Wolf if (has_base_node && has_base) { 34913c605f40SKevin Wolf error_setg(errp, "'base-node' and 'base' are mutually exclusive"); 34923c605f40SKevin Wolf goto out; 34933c605f40SKevin Wolf } else if (has_base_node) { 34943c605f40SKevin Wolf base_bs = bdrv_lookup_bs(NULL, base_node, errp); 34953c605f40SKevin Wolf if (base_bs == NULL) { 34963c605f40SKevin Wolf goto out; 34973c605f40SKevin Wolf } 34983c605f40SKevin Wolf if (!bdrv_chain_contains(top_bs, base_bs)) { 34993c605f40SKevin Wolf error_setg(errp, "'%s' is not in this backing file chain", 35003c605f40SKevin Wolf base_node); 35013c605f40SKevin Wolf goto out; 35023c605f40SKevin Wolf } 35033c605f40SKevin Wolf } else if (has_base && base) { 3504d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3505d5208c45SJeff Cody } else { 3506d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3507d5208c45SJeff Cody } 3508d5208c45SJeff Cody 3509d5208c45SJeff Cody if (base_bs == NULL) { 3510c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 35119e85cd5cSStefan Hajnoczi goto out; 3512d5208c45SJeff Cody } 3513d5208c45SJeff Cody 35149e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 35159e85cd5cSStefan Hajnoczi 3516058223a6SAlberto Garcia for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) { 3517058223a6SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3518bb00021dSFam Zheng goto out; 3519bb00021dSFam Zheng } 3520058223a6SAlberto Garcia } 3521bb00021dSFam Zheng 35227676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 35237676e2c5SJeff Cody if (top_bs == base_bs) { 35247676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 35259e85cd5cSStefan Hajnoczi goto out; 35267676e2c5SJeff Cody } 35277676e2c5SJeff Cody 352820a63d2cSFam Zheng if (top_bs == bs) { 352954e26900SJeff Cody if (has_backing_file) { 353054e26900SJeff Cody error_setg(errp, "'backing-file' specified," 353154e26900SJeff Cody " but 'top' is the active layer"); 35329e85cd5cSStefan Hajnoczi goto out; 353354e26900SJeff Cody } 353447970dfbSJohn Snow commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, 35355360782dSJohn Snow job_flags, speed, on_error, 353678bbd910SFam Zheng filter_node_name, NULL, NULL, false, &local_err); 353720a63d2cSFam Zheng } else { 3538058223a6SAlberto Garcia BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); 3539058223a6SAlberto Garcia if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3540058223a6SAlberto Garcia goto out; 3541058223a6SAlberto Garcia } 35425360782dSJohn Snow commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags, 35435360782dSJohn Snow speed, on_error, has_backing_file ? backing_file : NULL, 35440db832f4SKevin Wolf filter_node_name, &local_err); 354520a63d2cSFam Zheng } 3546ed61fc10SJeff Cody if (local_err != NULL) { 3547ed61fc10SJeff Cody error_propagate(errp, local_err); 35489e85cd5cSStefan Hajnoczi goto out; 3549ed61fc10SJeff Cody } 35509e85cd5cSStefan Hajnoczi 35519e85cd5cSStefan Hajnoczi out: 35529e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3553ed61fc10SJeff Cody } 3554ed61fc10SJeff Cody 35557b0b870bSJohn Snow /* Common QMP interface for drive-backup and blockdev-backup */ 35567b0b870bSJohn Snow static BlockJob *do_backup_common(BackupCommon *backup, 35577b0b870bSJohn Snow BlockDriverState *bs, 35587b0b870bSJohn Snow BlockDriverState *target_bs, 35597b0b870bSJohn Snow AioContext *aio_context, 35607b0b870bSJohn Snow JobTxn *txn, Error **errp) 356199a9addfSStefan Hajnoczi { 3562111049a4SJohn Snow BlockJob *job = NULL; 3563d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 35647b0b870bSJohn Snow int job_flags = JOB_DEFAULT; 35658ed7d42fSKevin Wolf int ret; 356699a9addfSStefan Hajnoczi 356781206a89SPavel Butsykin if (!backup->has_speed) { 356881206a89SPavel Butsykin backup->speed = 0; 356999a9addfSStefan Hajnoczi } 357081206a89SPavel Butsykin if (!backup->has_on_source_error) { 357181206a89SPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 357299a9addfSStefan Hajnoczi } 357381206a89SPavel Butsykin if (!backup->has_on_target_error) { 357481206a89SPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 357599a9addfSStefan Hajnoczi } 357681206a89SPavel Butsykin if (!backup->has_job_id) { 357781206a89SPavel Butsykin backup->job_id = NULL; 357899a9addfSStefan Hajnoczi } 3579b40dacdcSJohn Snow if (!backup->has_auto_finalize) { 3580b40dacdcSJohn Snow backup->auto_finalize = true; 3581b40dacdcSJohn Snow } 3582b40dacdcSJohn Snow if (!backup->has_auto_dismiss) { 3583b40dacdcSJohn Snow backup->auto_dismiss = true; 3584b40dacdcSJohn Snow } 358513b9414bSPavel Butsykin if (!backup->has_compress) { 358613b9414bSPavel Butsykin backup->compress = false; 358713b9414bSPavel Butsykin } 358899a9addfSStefan Hajnoczi 35897b0b870bSJohn Snow ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); 35907b0b870bSJohn Snow if (ret < 0) { 35917b0b870bSJohn Snow return NULL; 35927b0b870bSJohn Snow } 35937b0b870bSJohn Snow 3594a6c9365aSJohn Snow if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) || 3595a6c9365aSJohn Snow (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) { 3596a6c9365aSJohn Snow /* done before desugaring 'incremental' to print the right message */ 3597a6c9365aSJohn Snow if (!backup->has_bitmap) { 3598a6c9365aSJohn Snow error_setg(errp, "must provide a valid bitmap name for " 3599a6c9365aSJohn Snow "'%s' sync mode", MirrorSyncMode_str(backup->sync)); 3600a6c9365aSJohn Snow return NULL; 3601a6c9365aSJohn Snow } 3602a6c9365aSJohn Snow } 3603a6c9365aSJohn Snow 3604c8b56501SJohn Snow if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) { 3605c8b56501SJohn Snow if (backup->has_bitmap_mode && 3606c8b56501SJohn Snow backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) { 3607c8b56501SJohn Snow error_setg(errp, "Bitmap sync mode must be '%s' " 3608c8b56501SJohn Snow "when using sync mode '%s'", 3609c8b56501SJohn Snow BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS), 3610c8b56501SJohn Snow MirrorSyncMode_str(backup->sync)); 3611c8b56501SJohn Snow return NULL; 3612c8b56501SJohn Snow } 3613c8b56501SJohn Snow backup->has_bitmap_mode = true; 3614c8b56501SJohn Snow backup->sync = MIRROR_SYNC_MODE_BITMAP; 3615c8b56501SJohn Snow backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS; 3616c8b56501SJohn Snow } 3617c8b56501SJohn Snow 36187b0b870bSJohn Snow if (backup->has_bitmap) { 36197b0b870bSJohn Snow bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); 36207b0b870bSJohn Snow if (!bmap) { 36217b0b870bSJohn Snow error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); 36227b0b870bSJohn Snow return NULL; 36237b0b870bSJohn Snow } 3624c8b56501SJohn Snow if (!backup->has_bitmap_mode) { 3625c8b56501SJohn Snow error_setg(errp, "Bitmap sync mode must be given " 3626c8b56501SJohn Snow "when providing a bitmap"); 3627c8b56501SJohn Snow return NULL; 3628c8b56501SJohn Snow } 3629b30ffbefSJohn Snow if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) { 36307b0b870bSJohn Snow return NULL; 36317b0b870bSJohn Snow } 36321a2b8b40SJohn Snow 36331a2b8b40SJohn Snow /* This does not produce a useful bitmap artifact: */ 36341a2b8b40SJohn Snow if (backup->sync == MIRROR_SYNC_MODE_NONE) { 36351a2b8b40SJohn Snow error_setg(errp, "sync mode '%s' does not produce meaningful bitmap" 36361a2b8b40SJohn Snow " outputs", MirrorSyncMode_str(backup->sync)); 36371a2b8b40SJohn Snow return NULL; 36381a2b8b40SJohn Snow } 36391a2b8b40SJohn Snow 36401a2b8b40SJohn Snow /* If the bitmap isn't used for input or output, this is useless: */ 36411a2b8b40SJohn Snow if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER && 36421a2b8b40SJohn Snow backup->sync != MIRROR_SYNC_MODE_BITMAP) { 36431a2b8b40SJohn Snow error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect" 36441a2b8b40SJohn Snow " when combined with sync mode '%s'", 36451a2b8b40SJohn Snow BitmapSyncMode_str(backup->bitmap_mode), 36461a2b8b40SJohn Snow MirrorSyncMode_str(backup->sync)); 36471a2b8b40SJohn Snow return NULL; 36481a2b8b40SJohn Snow } 36491a2b8b40SJohn Snow } 36501a2b8b40SJohn Snow 36511a2b8b40SJohn Snow if (!backup->has_bitmap && backup->has_bitmap_mode) { 36521a2b8b40SJohn Snow error_setg(errp, "Cannot specify bitmap sync mode without a bitmap"); 36531a2b8b40SJohn Snow return NULL; 36547b0b870bSJohn Snow } 36557b0b870bSJohn Snow 36567b0b870bSJohn Snow if (!backup->auto_finalize) { 36577b0b870bSJohn Snow job_flags |= JOB_MANUAL_FINALIZE; 36587b0b870bSJohn Snow } 36597b0b870bSJohn Snow if (!backup->auto_dismiss) { 36607b0b870bSJohn Snow job_flags |= JOB_MANUAL_DISMISS; 36617b0b870bSJohn Snow } 36627b0b870bSJohn Snow 36637b0b870bSJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3664c8b56501SJohn Snow backup->sync, bmap, backup->bitmap_mode, 3665c8b56501SJohn Snow backup->compress, 366600e30f05SVladimir Sementsov-Ogievskiy backup->filter_node_name, 3667c8b56501SJohn Snow backup->on_source_error, 3668c8b56501SJohn Snow backup->on_target_error, 36697b0b870bSJohn Snow job_flags, NULL, NULL, txn, errp); 36707b0b870bSJohn Snow return job; 36717b0b870bSJohn Snow } 36727b0b870bSJohn Snow 36732288ccfaSSergio Lopez void qmp_drive_backup(DriveBackup *backup, Error **errp) 36747b0b870bSJohn Snow { 36752288ccfaSSergio Lopez TransactionAction action = { 36762288ccfaSSergio Lopez .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP, 36772288ccfaSSergio Lopez .u.drive_backup.data = backup, 36782288ccfaSSergio Lopez }; 36792288ccfaSSergio Lopez blockdev_do_action(&action, errp); 368078f51fdeSJohn Snow } 368178f51fdeSJohn Snow 3682c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3683c13163fbSBenoît Canet { 3684d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3685c13163fbSBenoît Canet } 3686c13163fbSBenoît Canet 36875d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp) 36885d3b4e99SVladimir Sementsov-Ogievskiy { 36895d3b4e99SVladimir Sementsov-Ogievskiy return bdrv_get_xdbg_block_graph(errp); 36905d3b4e99SVladimir Sementsov-Ogievskiy } 36915d3b4e99SVladimir Sementsov-Ogievskiy 3692*5b7bfe51SSergio Lopez void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp) 3693c29c1dd3SFam Zheng { 3694*5b7bfe51SSergio Lopez TransactionAction action = { 3695*5b7bfe51SSergio Lopez .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP, 3696*5b7bfe51SSergio Lopez .u.blockdev_backup.data = backup, 3697*5b7bfe51SSergio Lopez }; 3698*5b7bfe51SSergio Lopez blockdev_do_action(&action, errp); 369978f51fdeSJohn Snow } 370078f51fdeSJohn Snow 37014193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 37024193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 37034193cdd7SFam Zheng **/ 370471aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, 37054193cdd7SFam Zheng BlockDriverState *target, 370609158f00SBenoît Canet bool has_replaces, const char *replaces, 3707d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3708274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 3709cdf3bc93SMax Reitz bool zero_target, 3710b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3711eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 371208e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 37134193cdd7SFam Zheng bool has_on_source_error, 37144193cdd7SFam Zheng BlockdevOnError on_source_error, 37154193cdd7SFam Zheng bool has_on_target_error, 37164193cdd7SFam Zheng BlockdevOnError on_target_error, 37170fc9f8eaSFam Zheng bool has_unmap, bool unmap, 37186cdbceb1SKevin Wolf bool has_filter_node_name, 37196cdbceb1SKevin Wolf const char *filter_node_name, 3720481debaaSMax Reitz bool has_copy_mode, MirrorCopyMode copy_mode, 3721a6b58adeSJohn Snow bool has_auto_finalize, bool auto_finalize, 3722a6b58adeSJohn Snow bool has_auto_dismiss, bool auto_dismiss, 3723b952b558SPaolo Bonzini Error **errp) 3724d9b902dbSPaolo Bonzini { 3725a1999b33SJohn Snow int job_flags = JOB_DEFAULT; 3726d9b902dbSPaolo Bonzini 3727d9b902dbSPaolo Bonzini if (!has_speed) { 3728d9b902dbSPaolo Bonzini speed = 0; 3729d9b902dbSPaolo Bonzini } 3730b952b558SPaolo Bonzini if (!has_on_source_error) { 3731b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3732b952b558SPaolo Bonzini } 3733b952b558SPaolo Bonzini if (!has_on_target_error) { 3734b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3735b952b558SPaolo Bonzini } 3736eee13dfeSPaolo Bonzini if (!has_granularity) { 3737eee13dfeSPaolo Bonzini granularity = 0; 3738eee13dfeSPaolo Bonzini } 373908e4ed6cSPaolo Bonzini if (!has_buf_size) { 374048ac0a4dSWen Congyang buf_size = 0; 374108e4ed6cSPaolo Bonzini } 37420fc9f8eaSFam Zheng if (!has_unmap) { 37430fc9f8eaSFam Zheng unmap = true; 37440fc9f8eaSFam Zheng } 37456cdbceb1SKevin Wolf if (!has_filter_node_name) { 37466cdbceb1SKevin Wolf filter_node_name = NULL; 37476cdbceb1SKevin Wolf } 3748481debaaSMax Reitz if (!has_copy_mode) { 3749481debaaSMax Reitz copy_mode = MIRROR_COPY_MODE_BACKGROUND; 3750481debaaSMax Reitz } 3751a6b58adeSJohn Snow if (has_auto_finalize && !auto_finalize) { 3752a6b58adeSJohn Snow job_flags |= JOB_MANUAL_FINALIZE; 3753a6b58adeSJohn Snow } 3754a6b58adeSJohn Snow if (has_auto_dismiss && !auto_dismiss) { 3755a6b58adeSJohn Snow job_flags |= JOB_MANUAL_DISMISS; 3756a6b58adeSJohn Snow } 375708e4ed6cSPaolo Bonzini 3758eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3759c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 37603cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3761eee13dfeSPaolo Bonzini return; 3762eee13dfeSPaolo Bonzini } 3763eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3764c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3765c6bd8c70SMarkus Armbruster "power of 2"); 3766eee13dfeSPaolo Bonzini return; 3767eee13dfeSPaolo Bonzini } 3768d9b902dbSPaolo Bonzini 37694193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 37704193cdd7SFam Zheng return; 37714193cdd7SFam Zheng } 3772e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3773e40e5027SFam Zheng return; 3774e40e5027SFam Zheng } 37754193cdd7SFam Zheng 37764193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 37774193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 37784193cdd7SFam Zheng } 37794193cdd7SFam Zheng 378074ce9e46SMax Reitz if (has_replaces) { 378174ce9e46SMax Reitz BlockDriverState *to_replace_bs; 378274ce9e46SMax Reitz AioContext *replace_aio_context; 378374ce9e46SMax Reitz int64_t bs_size, replace_size; 378474ce9e46SMax Reitz 378574ce9e46SMax Reitz bs_size = bdrv_getlength(bs); 378674ce9e46SMax Reitz if (bs_size < 0) { 378774ce9e46SMax Reitz error_setg_errno(errp, -bs_size, "Failed to query device's size"); 378874ce9e46SMax Reitz return; 378974ce9e46SMax Reitz } 379074ce9e46SMax Reitz 379174ce9e46SMax Reitz to_replace_bs = check_to_replace_node(bs, replaces, errp); 379274ce9e46SMax Reitz if (!to_replace_bs) { 379374ce9e46SMax Reitz return; 379474ce9e46SMax Reitz } 379574ce9e46SMax Reitz 379674ce9e46SMax Reitz replace_aio_context = bdrv_get_aio_context(to_replace_bs); 379774ce9e46SMax Reitz aio_context_acquire(replace_aio_context); 379874ce9e46SMax Reitz replace_size = bdrv_getlength(to_replace_bs); 379974ce9e46SMax Reitz aio_context_release(replace_aio_context); 380074ce9e46SMax Reitz 380174ce9e46SMax Reitz if (replace_size < 0) { 380274ce9e46SMax Reitz error_setg_errno(errp, -replace_size, 380374ce9e46SMax Reitz "Failed to query the replacement node's size"); 380474ce9e46SMax Reitz return; 380574ce9e46SMax Reitz } 380674ce9e46SMax Reitz if (bs_size != replace_size) { 380774ce9e46SMax Reitz error_setg(errp, "cannot replace image with a mirror image of " 380874ce9e46SMax Reitz "different size"); 380974ce9e46SMax Reitz return; 381074ce9e46SMax Reitz } 381174ce9e46SMax Reitz } 381274ce9e46SMax Reitz 38134193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 38144193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 38154193cdd7SFam Zheng */ 381671aa9867SAlberto Garcia mirror_start(job_id, bs, target, 3817a1999b33SJohn Snow has_replaces ? replaces : NULL, job_flags, 3818cdf3bc93SMax Reitz speed, granularity, buf_size, sync, backing_mode, zero_target, 38196cdbceb1SKevin Wolf on_source_error, on_target_error, unmap, filter_node_name, 3820481debaaSMax Reitz copy_mode, errp); 38214193cdd7SFam Zheng } 38224193cdd7SFam Zheng 3823faecd40aSEric Blake void qmp_drive_mirror(DriveMirror *arg, Error **errp) 38244193cdd7SFam Zheng { 38254193cdd7SFam Zheng BlockDriverState *bs; 38264193cdd7SFam Zheng BlockDriverState *source, *target_bs; 38274193cdd7SFam Zheng AioContext *aio_context; 3828274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 38294193cdd7SFam Zheng Error *local_err = NULL; 38304193cdd7SFam Zheng QDict *options = NULL; 38314193cdd7SFam Zheng int flags; 38324193cdd7SFam Zheng int64_t size; 3833faecd40aSEric Blake const char *format = arg->format; 3834cdf3bc93SMax Reitz bool zero_target; 38358ed7d42fSKevin Wolf int ret; 38364193cdd7SFam Zheng 38370524e93aSKevin Wolf bs = qmp_get_root_bs(arg->device, errp); 38380524e93aSKevin Wolf if (!bs) { 3839d9b902dbSPaolo Bonzini return; 3840d9b902dbSPaolo Bonzini } 3841d9b902dbSPaolo Bonzini 3842cb2af917SPaolo Bonzini /* Early check to avoid creating target */ 3843cb2af917SPaolo Bonzini if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 3844cb2af917SPaolo Bonzini return; 3845cb2af917SPaolo Bonzini } 3846cb2af917SPaolo Bonzini 38470524e93aSKevin Wolf aio_context = bdrv_get_aio_context(bs); 38485a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 38495a7e7a0bSStefan Hajnoczi 3850faecd40aSEric Blake if (!arg->has_mode) { 3851faecd40aSEric Blake arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 38524193cdd7SFam Zheng } 3853d9b902dbSPaolo Bonzini 3854faecd40aSEric Blake if (!arg->has_format) { 3855faecd40aSEric Blake format = (arg->mode == NEW_IMAGE_MODE_EXISTING 3856faecd40aSEric Blake ? NULL : bs->drv->format_name); 3857d9b902dbSPaolo Bonzini } 3858d9b902dbSPaolo Bonzini 385961de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3860760e0063SKevin Wolf source = backing_bs(bs); 3861faecd40aSEric Blake if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { 3862faecd40aSEric Blake arg->sync = MIRROR_SYNC_MODE_FULL; 3863d9b902dbSPaolo Bonzini } 3864faecd40aSEric Blake if (arg->sync == MIRROR_SYNC_MODE_NONE) { 3865117e0c82SMax Reitz source = bs; 3866117e0c82SMax Reitz } 3867d9b902dbSPaolo Bonzini 3868ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3869ac3c5d83SStefan Hajnoczi if (size < 0) { 3870ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 38715a7e7a0bSStefan Hajnoczi goto out; 3872ac3c5d83SStefan Hajnoczi } 3873ac3c5d83SStefan Hajnoczi 3874faecd40aSEric Blake if (arg->has_replaces) { 3875faecd40aSEric Blake if (!arg->has_node_name) { 387609158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 387709158f00SBenoît Canet " named node of the graph"); 38785a7e7a0bSStefan Hajnoczi goto out; 387909158f00SBenoît Canet } 388009158f00SBenoît Canet } 388109158f00SBenoît Canet 3882faecd40aSEric Blake if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { 3883274fcceeSMax Reitz backing_mode = MIRROR_SOURCE_BACKING_CHAIN; 3884274fcceeSMax Reitz } else { 3885274fcceeSMax Reitz backing_mode = MIRROR_OPEN_BACKING_CHAIN; 3886274fcceeSMax Reitz } 3887274fcceeSMax Reitz 38882a32c6e8SJohn Snow /* Don't open backing image in create() */ 38892a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING; 38902a32c6e8SJohn Snow 3891faecd40aSEric Blake if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) 3892faecd40aSEric Blake && arg->mode != NEW_IMAGE_MODE_EXISTING) 389314526864SMax Reitz { 3894d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3895e6641719SMax Reitz assert(format); 3896faecd40aSEric Blake bdrv_img_create(arg->target, format, 38979217283dSFam Zheng NULL, NULL, NULL, size, flags, false, &local_err); 3898d9b902dbSPaolo Bonzini } else { 3899faecd40aSEric Blake switch (arg->mode) { 3900d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3901d9b902dbSPaolo Bonzini break; 3902d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3903d9b902dbSPaolo Bonzini /* create new image with backing file */ 3904f30c66baSMax Reitz bdrv_refresh_filename(source); 3905faecd40aSEric Blake bdrv_img_create(arg->target, format, 3906d9b902dbSPaolo Bonzini source->filename, 3907d9b902dbSPaolo Bonzini source->drv->format_name, 39089217283dSFam Zheng NULL, size, flags, false, &local_err); 3909d9b902dbSPaolo Bonzini break; 3910d9b902dbSPaolo Bonzini default: 3911d9b902dbSPaolo Bonzini abort(); 3912d9b902dbSPaolo Bonzini } 3913d9b902dbSPaolo Bonzini } 3914d9b902dbSPaolo Bonzini 391584d18f06SMarkus Armbruster if (local_err) { 3916cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 39175a7e7a0bSStefan Hajnoczi goto out; 3918d9b902dbSPaolo Bonzini } 3919d9b902dbSPaolo Bonzini 39204c828dc6SBenoît Canet options = qdict_new(); 3921faecd40aSEric Blake if (arg->has_node_name) { 392246f5ac20SEric Blake qdict_put_str(options, "node-name", arg->node_name); 39234c828dc6SBenoît Canet } 3924e6641719SMax Reitz if (format) { 392546f5ac20SEric Blake qdict_put_str(options, "driver", format); 3926e6641719SMax Reitz } 39274c828dc6SBenoît Canet 3928b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3929b812f671SPaolo Bonzini * file. 3930b812f671SPaolo Bonzini */ 39312a32c6e8SJohn Snow target_bs = bdrv_open(arg->target, NULL, options, flags, errp); 39325b363937SMax Reitz if (!target_bs) { 39335a7e7a0bSStefan Hajnoczi goto out; 3934d9b902dbSPaolo Bonzini } 3935d9b902dbSPaolo Bonzini 3936cdf3bc93SMax Reitz zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL && 3937cdf3bc93SMax Reitz (arg->mode == NEW_IMAGE_MODE_EXISTING || 3938cdf3bc93SMax Reitz !bdrv_has_zero_init(target_bs))); 3939cdf3bc93SMax Reitz 39408ed7d42fSKevin Wolf ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); 39418ed7d42fSKevin Wolf if (ret < 0) { 39428ed7d42fSKevin Wolf bdrv_unref(target_bs); 39438ed7d42fSKevin Wolf goto out; 39448ed7d42fSKevin Wolf } 39455a7e7a0bSStefan Hajnoczi 3946faecd40aSEric Blake blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, 3947faecd40aSEric Blake arg->has_replaces, arg->replaces, arg->sync, 3948cdf3bc93SMax Reitz backing_mode, zero_target, 3949cdf3bc93SMax Reitz arg->has_speed, arg->speed, 3950faecd40aSEric Blake arg->has_granularity, arg->granularity, 3951faecd40aSEric Blake arg->has_buf_size, arg->buf_size, 3952faecd40aSEric Blake arg->has_on_source_error, arg->on_source_error, 3953faecd40aSEric Blake arg->has_on_target_error, arg->on_target_error, 3954faecd40aSEric Blake arg->has_unmap, arg->unmap, 39556cdbceb1SKevin Wolf false, NULL, 3956481debaaSMax Reitz arg->has_copy_mode, arg->copy_mode, 3957a6b58adeSJohn Snow arg->has_auto_finalize, arg->auto_finalize, 3958a6b58adeSJohn Snow arg->has_auto_dismiss, arg->auto_dismiss, 39594193cdd7SFam Zheng &local_err); 3960e253f4b8SKevin Wolf bdrv_unref(target_bs); 3961d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 39625a7e7a0bSStefan Hajnoczi out: 39635a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3964d9b902dbSPaolo Bonzini } 3965d9b902dbSPaolo Bonzini 396671aa9867SAlberto Garcia void qmp_blockdev_mirror(bool has_job_id, const char *job_id, 396771aa9867SAlberto Garcia const char *device, const char *target, 3968df92562eSFam Zheng bool has_replaces, const char *replaces, 3969df92562eSFam Zheng MirrorSyncMode sync, 3970df92562eSFam Zheng bool has_speed, int64_t speed, 3971df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3972df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3973df92562eSFam Zheng bool has_on_source_error, 3974df92562eSFam Zheng BlockdevOnError on_source_error, 3975df92562eSFam Zheng bool has_on_target_error, 3976df92562eSFam Zheng BlockdevOnError on_target_error, 39776cdbceb1SKevin Wolf bool has_filter_node_name, 39786cdbceb1SKevin Wolf const char *filter_node_name, 3979481debaaSMax Reitz bool has_copy_mode, MirrorCopyMode copy_mode, 3980a6b58adeSJohn Snow bool has_auto_finalize, bool auto_finalize, 3981a6b58adeSJohn Snow bool has_auto_dismiss, bool auto_dismiss, 3982df92562eSFam Zheng Error **errp) 3983df92562eSFam Zheng { 3984df92562eSFam Zheng BlockDriverState *bs; 3985df92562eSFam Zheng BlockDriverState *target_bs; 3986df92562eSFam Zheng AioContext *aio_context; 3987274fcceeSMax Reitz BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; 3988df92562eSFam Zheng Error *local_err = NULL; 3989cdf3bc93SMax Reitz bool zero_target; 39908ed7d42fSKevin Wolf int ret; 3991df92562eSFam Zheng 399207eec652SKevin Wolf bs = qmp_get_root_bs(device, errp); 3993df92562eSFam Zheng if (!bs) { 3994df92562eSFam Zheng return; 3995df92562eSFam Zheng } 3996df92562eSFam Zheng 3997df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3998df92562eSFam Zheng if (!target_bs) { 3999df92562eSFam Zheng return; 4000df92562eSFam Zheng } 4001df92562eSFam Zheng 4002cdf3bc93SMax Reitz zero_target = (sync == MIRROR_SYNC_MODE_FULL); 4003cdf3bc93SMax Reitz 4004df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 4005df92562eSFam Zheng aio_context_acquire(aio_context); 4006df92562eSFam Zheng 40078ed7d42fSKevin Wolf ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); 40088ed7d42fSKevin Wolf if (ret < 0) { 40098ed7d42fSKevin Wolf goto out; 40108ed7d42fSKevin Wolf } 4011df92562eSFam Zheng 401271aa9867SAlberto Garcia blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 4013274fcceeSMax Reitz has_replaces, replaces, sync, backing_mode, 4014cdf3bc93SMax Reitz zero_target, has_speed, speed, 4015df92562eSFam Zheng has_granularity, granularity, 4016df92562eSFam Zheng has_buf_size, buf_size, 4017df92562eSFam Zheng has_on_source_error, on_source_error, 4018df92562eSFam Zheng has_on_target_error, on_target_error, 4019df92562eSFam Zheng true, true, 40206cdbceb1SKevin Wolf has_filter_node_name, filter_node_name, 4021481debaaSMax Reitz has_copy_mode, copy_mode, 4022a6b58adeSJohn Snow has_auto_finalize, auto_finalize, 4023a6b58adeSJohn Snow has_auto_dismiss, auto_dismiss, 4024df92562eSFam Zheng &local_err); 4025df92562eSFam Zheng error_propagate(errp, local_err); 40268ed7d42fSKevin Wolf out: 4027df92562eSFam Zheng aio_context_release(aio_context); 4028df92562eSFam Zheng } 4029df92562eSFam Zheng 40303ddf3efeSAlberto Garcia /* Get a block job using its ID and acquire its AioContext */ 40313ddf3efeSAlberto Garcia static BlockJob *find_block_job(const char *id, AioContext **aio_context, 403224d6bffeSMarkus Armbruster Error **errp) 40332d47c6e9SStefan Hajnoczi { 40343ddf3efeSAlberto Garcia BlockJob *job; 40353ddf3efeSAlberto Garcia 40363ddf3efeSAlberto Garcia assert(id != NULL); 40372d47c6e9SStefan Hajnoczi 40385433c24fSMax Reitz *aio_context = NULL; 40395433c24fSMax Reitz 40403ddf3efeSAlberto Garcia job = block_job_get(id); 40413ddf3efeSAlberto Garcia 40423ddf3efeSAlberto Garcia if (!job) { 40433ddf3efeSAlberto Garcia error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 40443ddf3efeSAlberto Garcia "Block job '%s' not found", id); 40453ddf3efeSAlberto Garcia return NULL; 40462d47c6e9SStefan Hajnoczi } 40473d948cdfSStefan Hajnoczi 40483ddf3efeSAlberto Garcia *aio_context = blk_get_aio_context(job->blk); 40493d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 40503d948cdfSStefan Hajnoczi 40513ddf3efeSAlberto Garcia return job; 40522d47c6e9SStefan Hajnoczi } 40532d47c6e9SStefan Hajnoczi 4054882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 40552d47c6e9SStefan Hajnoczi { 40563d948cdfSStefan Hajnoczi AioContext *aio_context; 405724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 40582d47c6e9SStefan Hajnoczi 40592d47c6e9SStefan Hajnoczi if (!job) { 40602d47c6e9SStefan Hajnoczi return; 40612d47c6e9SStefan Hajnoczi } 40622d47c6e9SStefan Hajnoczi 4063882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 40643d948cdfSStefan Hajnoczi aio_context_release(aio_context); 40652d47c6e9SStefan Hajnoczi } 4066370521a1SStefan Hajnoczi 40676e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 40686e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 40696e37fb81SPaolo Bonzini { 40703d948cdfSStefan Hajnoczi AioContext *aio_context; 407124d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 40726e37fb81SPaolo Bonzini 40736e37fb81SPaolo Bonzini if (!job) { 40746e37fb81SPaolo Bonzini return; 40756e37fb81SPaolo Bonzini } 40763d948cdfSStefan Hajnoczi 40773d948cdfSStefan Hajnoczi if (!has_force) { 40783d948cdfSStefan Hajnoczi force = false; 40793d948cdfSStefan Hajnoczi } 40803d948cdfSStefan Hajnoczi 4081b15de828SKevin Wolf if (job_user_paused(&job->job) && !force) { 4082f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 4083f231b88dSCole Robinson device); 40843d948cdfSStefan Hajnoczi goto out; 40856e37fb81SPaolo Bonzini } 40866e37fb81SPaolo Bonzini 40876e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 40883d70ff53SKevin Wolf job_user_cancel(&job->job, force, errp); 40893d948cdfSStefan Hajnoczi out: 40903d948cdfSStefan Hajnoczi aio_context_release(aio_context); 40916e37fb81SPaolo Bonzini } 40926e37fb81SPaolo Bonzini 40936e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 4094370521a1SStefan Hajnoczi { 40953d948cdfSStefan Hajnoczi AioContext *aio_context; 409624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 4097370521a1SStefan Hajnoczi 40980ec4dfb8SJohn Snow if (!job) { 4099370521a1SStefan Hajnoczi return; 4100370521a1SStefan Hajnoczi } 41016e37fb81SPaolo Bonzini 41026e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 4103b15de828SKevin Wolf job_user_pause(&job->job, errp); 41043d948cdfSStefan Hajnoczi aio_context_release(aio_context); 41056e37fb81SPaolo Bonzini } 41066e37fb81SPaolo Bonzini 41076e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 41086e37fb81SPaolo Bonzini { 41093d948cdfSStefan Hajnoczi AioContext *aio_context; 411024d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 41116e37fb81SPaolo Bonzini 41120ec4dfb8SJohn Snow if (!job) { 41138acc72a4SPaolo Bonzini return; 41148acc72a4SPaolo Bonzini } 4115370521a1SStefan Hajnoczi 41166e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 4117b15de828SKevin Wolf job_user_resume(&job->job, errp); 41183d948cdfSStefan Hajnoczi aio_context_release(aio_context); 4119370521a1SStefan Hajnoczi } 4120fb5458cdSStefan Hajnoczi 4121aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 4122aeae883bSPaolo Bonzini { 41233d948cdfSStefan Hajnoczi AioContext *aio_context; 412424d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 4125aeae883bSPaolo Bonzini 4126aeae883bSPaolo Bonzini if (!job) { 4127aeae883bSPaolo Bonzini return; 4128aeae883bSPaolo Bonzini } 4129aeae883bSPaolo Bonzini 4130aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 41313453d972SKevin Wolf job_complete(&job->job, errp); 41323d948cdfSStefan Hajnoczi aio_context_release(aio_context); 4133aeae883bSPaolo Bonzini } 4134aeae883bSPaolo Bonzini 413511b61fbcSJohn Snow void qmp_block_job_finalize(const char *id, Error **errp) 413611b61fbcSJohn Snow { 413711b61fbcSJohn Snow AioContext *aio_context; 413811b61fbcSJohn Snow BlockJob *job = find_block_job(id, &aio_context, errp); 413911b61fbcSJohn Snow 414011b61fbcSJohn Snow if (!job) { 414111b61fbcSJohn Snow return; 414211b61fbcSJohn Snow } 414311b61fbcSJohn Snow 414411b61fbcSJohn Snow trace_qmp_block_job_finalize(job); 41457eaa8fb5SKevin Wolf job_finalize(&job->job, errp); 414611b61fbcSJohn Snow aio_context_release(aio_context); 414711b61fbcSJohn Snow } 414811b61fbcSJohn Snow 414975f71059SJohn Snow void qmp_block_job_dismiss(const char *id, Error **errp) 415075f71059SJohn Snow { 415175f71059SJohn Snow AioContext *aio_context; 41525f9a6a08SKevin Wolf BlockJob *bjob = find_block_job(id, &aio_context, errp); 41535f9a6a08SKevin Wolf Job *job; 415475f71059SJohn Snow 41555f9a6a08SKevin Wolf if (!bjob) { 415675f71059SJohn Snow return; 415775f71059SJohn Snow } 415875f71059SJohn Snow 41595f9a6a08SKevin Wolf trace_qmp_block_job_dismiss(bjob); 41605f9a6a08SKevin Wolf job = &bjob->job; 41615f9a6a08SKevin Wolf job_dismiss(&job, errp); 416275f71059SJohn Snow aio_context_release(aio_context); 416375f71059SJohn Snow } 416475f71059SJohn Snow 4165fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 4166fa40e656SJeff Cody const char *image_node_name, 4167fa40e656SJeff Cody const char *backing_file, 4168fa40e656SJeff Cody Error **errp) 4169fa40e656SJeff Cody { 4170fa40e656SJeff Cody BlockDriverState *bs = NULL; 4171729962f6SStefan Hajnoczi AioContext *aio_context; 4172fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 4173fa40e656SJeff Cody Error *local_err = NULL; 4174fa40e656SJeff Cody bool ro; 4175fa40e656SJeff Cody int ret; 4176fa40e656SJeff Cody 41777b5dca3fSKevin Wolf bs = qmp_get_root_bs(device, errp); 41787b5dca3fSKevin Wolf if (!bs) { 4179fa40e656SJeff Cody return; 4180fa40e656SJeff Cody } 4181fa40e656SJeff Cody 41827b5dca3fSKevin Wolf aio_context = bdrv_get_aio_context(bs); 4183729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 4184729962f6SStefan Hajnoczi 4185fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 4186fa40e656SJeff Cody if (local_err) { 4187fa40e656SJeff Cody error_propagate(errp, local_err); 4188729962f6SStefan Hajnoczi goto out; 4189fa40e656SJeff Cody } 4190fa40e656SJeff Cody 4191fa40e656SJeff Cody if (!image_bs) { 4192fa40e656SJeff Cody error_setg(errp, "image file not found"); 4193729962f6SStefan Hajnoczi goto out; 4194fa40e656SJeff Cody } 4195fa40e656SJeff Cody 4196fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 4197fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 4198fa40e656SJeff Cody "without a backing file"); 4199729962f6SStefan Hajnoczi goto out; 4200fa40e656SJeff Cody } 4201fa40e656SJeff Cody 4202fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 4203fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 4204fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 4205729962f6SStefan Hajnoczi goto out; 4206fa40e656SJeff Cody } 4207fa40e656SJeff Cody 4208fa40e656SJeff Cody /* final sanity check */ 4209fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 4210fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 4211fa40e656SJeff Cody device); 4212729962f6SStefan Hajnoczi goto out; 4213fa40e656SJeff Cody } 4214fa40e656SJeff Cody 4215fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 4216fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 4217fa40e656SJeff Cody 4218fa40e656SJeff Cody if (ro) { 4219051a60f6SAlberto Garcia if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) { 4220729962f6SStefan Hajnoczi goto out; 4221fa40e656SJeff Cody } 4222fa40e656SJeff Cody } 4223fa40e656SJeff Cody 4224fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 4225fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 4226fa40e656SJeff Cody 4227fa40e656SJeff Cody if (ret < 0) { 4228fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 4229fa40e656SJeff Cody backing_file); 4230fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 4231fa40e656SJeff Cody * appropriate */ 4232fa40e656SJeff Cody } 4233fa40e656SJeff Cody 4234fa40e656SJeff Cody if (ro) { 4235051a60f6SAlberto Garcia bdrv_reopen_set_read_only(image_bs, true, &local_err); 4236621ff94dSEduardo Habkost error_propagate(errp, local_err); 4237fa40e656SJeff Cody } 4238729962f6SStefan Hajnoczi 4239729962f6SStefan Hajnoczi out: 4240729962f6SStefan Hajnoczi aio_context_release(aio_context); 4241fa40e656SJeff Cody } 4242fa40e656SJeff Cody 4243abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 4244abb21ac3SKevin Wolf { 4245abb21ac3SKevin Wolf QemuOpts *opts; 4246abb21ac3SKevin Wolf QDict *qdict; 4247abb21ac3SKevin Wolf Error *local_err = NULL; 4248abb21ac3SKevin Wolf 4249abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 4250abb21ac3SKevin Wolf if (!opts) { 4251abb21ac3SKevin Wolf return; 4252abb21ac3SKevin Wolf } 4253abb21ac3SKevin Wolf 4254abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 4255abb21ac3SKevin Wolf 4256abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 4257cb3e7f08SMarc-André Lureau qobject_unref(qdict); 4258abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 4259abb21ac3SKevin Wolf goto out; 4260abb21ac3SKevin Wolf } 4261abb21ac3SKevin Wolf 4262abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 4263abb21ac3SKevin Wolf if (!bs) { 4264abb21ac3SKevin Wolf error_report_err(local_err); 4265abb21ac3SKevin Wolf goto out; 4266abb21ac3SKevin Wolf } 4267abb21ac3SKevin Wolf 4268abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4269abb21ac3SKevin Wolf 4270abb21ac3SKevin Wolf out: 4271abb21ac3SKevin Wolf qemu_opts_del(opts); 4272abb21ac3SKevin Wolf } 4273abb21ac3SKevin Wolf 4274d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 4275d26c9a15SKevin Wolf { 4276be4b67bcSMax Reitz BlockDriverState *bs; 4277d26c9a15SKevin Wolf QObject *obj; 42787d5e199aSDaniel P. Berrange Visitor *v = qobject_output_visitor_new(&obj); 4279d26c9a15SKevin Wolf QDict *qdict; 4280d26c9a15SKevin Wolf Error *local_err = NULL; 4281d26c9a15SKevin Wolf 42823b098d56SEric Blake visit_type_BlockdevOptions(v, NULL, &options, &local_err); 428384d18f06SMarkus Armbruster if (local_err) { 4284d26c9a15SKevin Wolf error_propagate(errp, local_err); 4285d26c9a15SKevin Wolf goto fail; 4286d26c9a15SKevin Wolf } 4287d26c9a15SKevin Wolf 42883b098d56SEric Blake visit_complete(v, &obj); 42897dc847ebSMax Reitz qdict = qobject_to(QDict, obj); 4290d26c9a15SKevin Wolf 4291d26c9a15SKevin Wolf qdict_flatten(qdict); 4292d26c9a15SKevin Wolf 4293be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 42949ec8873eSKevin Wolf error_setg(errp, "'node-name' must be specified for the root node"); 4295be4b67bcSMax Reitz goto fail; 4296be4b67bcSMax Reitz } 4297be4b67bcSMax Reitz 4298bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 4299bd745e23SMax Reitz if (!bs) { 4300be4b67bcSMax Reitz goto fail; 4301be4b67bcSMax Reitz } 43029c4218e9SMax Reitz 43039c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4304be4b67bcSMax Reitz 4305d26c9a15SKevin Wolf fail: 43063b098d56SEric Blake visit_free(v); 4307d26c9a15SKevin Wolf } 4308d26c9a15SKevin Wolf 43091479c730SAlberto Garcia void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp) 43101479c730SAlberto Garcia { 43111479c730SAlberto Garcia BlockDriverState *bs; 43121479c730SAlberto Garcia AioContext *ctx; 43131479c730SAlberto Garcia QObject *obj; 43141479c730SAlberto Garcia Visitor *v = qobject_output_visitor_new(&obj); 43151479c730SAlberto Garcia Error *local_err = NULL; 43161479c730SAlberto Garcia BlockReopenQueue *queue; 43171479c730SAlberto Garcia QDict *qdict; 43181479c730SAlberto Garcia 43191479c730SAlberto Garcia /* Check for the selected node name */ 43201479c730SAlberto Garcia if (!options->has_node_name) { 43211479c730SAlberto Garcia error_setg(errp, "Node name not specified"); 43221479c730SAlberto Garcia goto fail; 43231479c730SAlberto Garcia } 43241479c730SAlberto Garcia 43251479c730SAlberto Garcia bs = bdrv_find_node(options->node_name); 43261479c730SAlberto Garcia if (!bs) { 43271479c730SAlberto Garcia error_setg(errp, "Cannot find node named '%s'", options->node_name); 43281479c730SAlberto Garcia goto fail; 43291479c730SAlberto Garcia } 43301479c730SAlberto Garcia 43311479c730SAlberto Garcia /* Put all options in a QDict and flatten it */ 43321479c730SAlberto Garcia visit_type_BlockdevOptions(v, NULL, &options, &local_err); 43331479c730SAlberto Garcia if (local_err) { 43341479c730SAlberto Garcia error_propagate(errp, local_err); 43351479c730SAlberto Garcia goto fail; 43361479c730SAlberto Garcia } 43371479c730SAlberto Garcia 43381479c730SAlberto Garcia visit_complete(v, &obj); 43391479c730SAlberto Garcia qdict = qobject_to(QDict, obj); 43401479c730SAlberto Garcia 43411479c730SAlberto Garcia qdict_flatten(qdict); 43421479c730SAlberto Garcia 43431479c730SAlberto Garcia /* Perform the reopen operation */ 43441479c730SAlberto Garcia ctx = bdrv_get_aio_context(bs); 43451479c730SAlberto Garcia aio_context_acquire(ctx); 43461479c730SAlberto Garcia bdrv_subtree_drained_begin(bs); 43471479c730SAlberto Garcia queue = bdrv_reopen_queue(NULL, bs, qdict, false); 43481479c730SAlberto Garcia bdrv_reopen_multiple(queue, errp); 43491479c730SAlberto Garcia bdrv_subtree_drained_end(bs); 43501479c730SAlberto Garcia aio_context_release(ctx); 43511479c730SAlberto Garcia 43521479c730SAlberto Garcia fail: 43531479c730SAlberto Garcia visit_free(v); 43541479c730SAlberto Garcia } 43551479c730SAlberto Garcia 435679b7a77eSMarkus Armbruster void qmp_blockdev_del(const char *node_name, Error **errp) 435781b936aeSAlberto Garcia { 435881b936aeSAlberto Garcia AioContext *aio_context; 435981b936aeSAlberto Garcia BlockDriverState *bs; 436081b936aeSAlberto Garcia 436181b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 436281b936aeSAlberto Garcia if (!bs) { 436381b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 436481b936aeSAlberto Garcia return; 436581b936aeSAlberto Garcia } 43661f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 4367e467da7bSKevin Wolf error_setg(errp, "Node %s is in use", node_name); 436881b936aeSAlberto Garcia return; 436981b936aeSAlberto Garcia } 437081b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 437181b936aeSAlberto Garcia aio_context_acquire(aio_context); 437281b936aeSAlberto Garcia 437381b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 437481b936aeSAlberto Garcia goto out; 437581b936aeSAlberto Garcia } 437681b936aeSAlberto Garcia 437770537ed5SPaolo Bonzini if (!QTAILQ_IN_USE(bs, monitor_list)) { 43789c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 43799c4218e9SMax Reitz bs->node_name); 43809c4218e9SMax Reitz goto out; 43819c4218e9SMax Reitz } 43829c4218e9SMax Reitz 43839c4218e9SMax Reitz if (bs->refcnt > 1) { 438481b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 438581b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 438681b936aeSAlberto Garcia goto out; 438781b936aeSAlberto Garcia } 438881b936aeSAlberto Garcia 43899c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 439081b936aeSAlberto Garcia bdrv_unref(bs); 439181b936aeSAlberto Garcia 439281b936aeSAlberto Garcia out: 439381b936aeSAlberto Garcia aio_context_release(aio_context); 439481b936aeSAlberto Garcia } 439581b936aeSAlberto Garcia 43967f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 43977f821597SWen Congyang const char *child_name) 43987f821597SWen Congyang { 43997f821597SWen Congyang BdrvChild *child; 44007f821597SWen Congyang 44017f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 44027f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 44037f821597SWen Congyang return child; 44047f821597SWen Congyang } 44057f821597SWen Congyang } 44067f821597SWen Congyang 44077f821597SWen Congyang return NULL; 44087f821597SWen Congyang } 44097f821597SWen Congyang 44107f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 44117f821597SWen Congyang const char *child, bool has_node, 44127f821597SWen Congyang const char *node, Error **errp) 44137f821597SWen Congyang { 44147f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 44157f821597SWen Congyang BdrvChild *p_child; 44167f821597SWen Congyang 44177f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 44187f821597SWen Congyang if (!parent_bs) { 44197f821597SWen Congyang return; 44207f821597SWen Congyang } 44217f821597SWen Congyang 44227f821597SWen Congyang if (has_child == has_node) { 44237f821597SWen Congyang if (has_child) { 44247f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 44257f821597SWen Congyang } else { 44267f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 44277f821597SWen Congyang } 44287f821597SWen Congyang return; 44297f821597SWen Congyang } 44307f821597SWen Congyang 44317f821597SWen Congyang if (has_child) { 44327f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 44337f821597SWen Congyang if (!p_child) { 44347f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 44357f821597SWen Congyang parent, child); 44367f821597SWen Congyang return; 44377f821597SWen Congyang } 44387f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 44397f821597SWen Congyang } 44407f821597SWen Congyang 44417f821597SWen Congyang if (has_node) { 44427f821597SWen Congyang new_bs = bdrv_find_node(node); 44437f821597SWen Congyang if (!new_bs) { 44447f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 44457f821597SWen Congyang return; 44467f821597SWen Congyang } 44477f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 44487f821597SWen Congyang } 44497f821597SWen Congyang } 44507f821597SWen Congyang 4451fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4452fb5458cdSStefan Hajnoczi { 4453fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4454f0f55dedSAlberto Garcia BlockJob *job; 4455fea68bb6SMarkus Armbruster 4456f0f55dedSAlberto Garcia for (job = block_job_next(NULL); job; job = block_job_next(job)) { 4457559b935fSJohn Snow BlockJobInfoList *elem; 4458559b935fSJohn Snow AioContext *aio_context; 445969691e72SStefan Hajnoczi 4460559b935fSJohn Snow if (block_job_is_internal(job)) { 4461559b935fSJohn Snow continue; 4462559b935fSJohn Snow } 4463559b935fSJohn Snow elem = g_new0(BlockJobInfoList, 1); 4464559b935fSJohn Snow aio_context = blk_get_aio_context(job->blk); 446569691e72SStefan Hajnoczi aio_context_acquire(aio_context); 4466559b935fSJohn Snow elem->value = block_job_query(job, errp); 4467f0f55dedSAlberto Garcia aio_context_release(aio_context); 4468559b935fSJohn Snow if (!elem->value) { 4469559b935fSJohn Snow g_free(elem); 4470559b935fSJohn Snow qapi_free_BlockJobInfoList(head); 4471559b935fSJohn Snow return NULL; 4472559b935fSJohn Snow } 4473fea68bb6SMarkus Armbruster *p_next = elem; 4474fea68bb6SMarkus Armbruster p_next = &elem->next; 4475fea68bb6SMarkus Armbruster } 447669691e72SStefan Hajnoczi 4477fea68bb6SMarkus Armbruster return head; 4478fb5458cdSStefan Hajnoczi } 44794d454574SPaolo Bonzini 4480ca00bbb1SStefan Hajnoczi void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread, 4481882e9b89SStefan Hajnoczi bool has_force, bool force, Error **errp) 4482ca00bbb1SStefan Hajnoczi { 4483ca00bbb1SStefan Hajnoczi AioContext *old_context; 4484ca00bbb1SStefan Hajnoczi AioContext *new_context; 4485ca00bbb1SStefan Hajnoczi BlockDriverState *bs; 4486ca00bbb1SStefan Hajnoczi 4487ca00bbb1SStefan Hajnoczi bs = bdrv_find_node(node_name); 4488ca00bbb1SStefan Hajnoczi if (!bs) { 4489ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find node %s", node_name); 4490ca00bbb1SStefan Hajnoczi return; 4491ca00bbb1SStefan Hajnoczi } 4492ca00bbb1SStefan Hajnoczi 4493882e9b89SStefan Hajnoczi /* Protects against accidents. */ 4494882e9b89SStefan Hajnoczi if (!(has_force && force) && bdrv_has_blk(bs)) { 4495882e9b89SStefan Hajnoczi error_setg(errp, "Node %s is associated with a BlockBackend and could " 4496882e9b89SStefan Hajnoczi "be in use (use force=true to override this check)", 4497882e9b89SStefan Hajnoczi node_name); 4498ca00bbb1SStefan Hajnoczi return; 4499ca00bbb1SStefan Hajnoczi } 4500ca00bbb1SStefan Hajnoczi 4501ca00bbb1SStefan Hajnoczi if (iothread->type == QTYPE_QSTRING) { 4502ca00bbb1SStefan Hajnoczi IOThread *obj = iothread_by_id(iothread->u.s); 4503ca00bbb1SStefan Hajnoczi if (!obj) { 4504ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find iothread %s", iothread->u.s); 4505ca00bbb1SStefan Hajnoczi return; 4506ca00bbb1SStefan Hajnoczi } 4507ca00bbb1SStefan Hajnoczi 4508ca00bbb1SStefan Hajnoczi new_context = iothread_get_aio_context(obj); 4509ca00bbb1SStefan Hajnoczi } else { 4510ca00bbb1SStefan Hajnoczi new_context = qemu_get_aio_context(); 4511ca00bbb1SStefan Hajnoczi } 4512ca00bbb1SStefan Hajnoczi 4513ca00bbb1SStefan Hajnoczi old_context = bdrv_get_aio_context(bs); 4514ca00bbb1SStefan Hajnoczi aio_context_acquire(old_context); 4515ca00bbb1SStefan Hajnoczi 45168ed7d42fSKevin Wolf bdrv_try_set_aio_context(bs, new_context, errp); 4517ca00bbb1SStefan Hajnoczi 4518ca00bbb1SStefan Hajnoczi aio_context_release(old_context); 4519ca00bbb1SStefan Hajnoczi } 4520ca00bbb1SStefan Hajnoczi 4521cb8aac37SVladimir Sementsov-Ogievskiy void qmp_block_latency_histogram_set( 4522af0a2265SVladimir Sementsov-Ogievskiy const char *id, 45237e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries, uint64List *boundaries, 45247e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_read, uint64List *boundaries_read, 45257e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_write, uint64List *boundaries_write, 45267e5c776dSVladimir Sementsov-Ogievskiy bool has_boundaries_flush, uint64List *boundaries_flush, 45277e5c776dSVladimir Sementsov-Ogievskiy Error **errp) 45287e5c776dSVladimir Sementsov-Ogievskiy { 4529af0a2265SVladimir Sementsov-Ogievskiy BlockBackend *blk = qmp_get_blk(NULL, id, errp); 45307e5c776dSVladimir Sementsov-Ogievskiy BlockAcctStats *stats; 453163d5341fSzhenwei pi int ret; 45327e5c776dSVladimir Sementsov-Ogievskiy 45337e5c776dSVladimir Sementsov-Ogievskiy if (!blk) { 45347e5c776dSVladimir Sementsov-Ogievskiy return; 45357e5c776dSVladimir Sementsov-Ogievskiy } 4536af0a2265SVladimir Sementsov-Ogievskiy 45377e5c776dSVladimir Sementsov-Ogievskiy stats = blk_get_stats(blk); 45387e5c776dSVladimir Sementsov-Ogievskiy 45397e5c776dSVladimir Sementsov-Ogievskiy if (!has_boundaries && !has_boundaries_read && !has_boundaries_write && 45407e5c776dSVladimir Sementsov-Ogievskiy !has_boundaries_flush) 45417e5c776dSVladimir Sementsov-Ogievskiy { 45427e5c776dSVladimir Sementsov-Ogievskiy block_latency_histograms_clear(stats); 45437e5c776dSVladimir Sementsov-Ogievskiy return; 45447e5c776dSVladimir Sementsov-Ogievskiy } 45457e5c776dSVladimir Sementsov-Ogievskiy 45467e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_read) { 454763d5341fSzhenwei pi ret = block_latency_histogram_set( 45487e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_READ, 45497e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_read ? boundaries_read : boundaries); 455063d5341fSzhenwei pi if (ret) { 4551af0a2265SVladimir Sementsov-Ogievskiy error_setg(errp, "Device '%s' set read boundaries fail", id); 455263d5341fSzhenwei pi return; 455363d5341fSzhenwei pi } 45547e5c776dSVladimir Sementsov-Ogievskiy } 45557e5c776dSVladimir Sementsov-Ogievskiy 45567e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_write) { 455763d5341fSzhenwei pi ret = block_latency_histogram_set( 45587e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_WRITE, 45597e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_write ? boundaries_write : boundaries); 456063d5341fSzhenwei pi if (ret) { 4561af0a2265SVladimir Sementsov-Ogievskiy error_setg(errp, "Device '%s' set write boundaries fail", id); 456263d5341fSzhenwei pi return; 456363d5341fSzhenwei pi } 45647e5c776dSVladimir Sementsov-Ogievskiy } 45657e5c776dSVladimir Sementsov-Ogievskiy 45667e5c776dSVladimir Sementsov-Ogievskiy if (has_boundaries || has_boundaries_flush) { 456763d5341fSzhenwei pi ret = block_latency_histogram_set( 45687e5c776dSVladimir Sementsov-Ogievskiy stats, BLOCK_ACCT_FLUSH, 45697e5c776dSVladimir Sementsov-Ogievskiy has_boundaries_flush ? boundaries_flush : boundaries); 457063d5341fSzhenwei pi if (ret) { 4571af0a2265SVladimir Sementsov-Ogievskiy error_setg(errp, "Device '%s' set flush boundaries fail", id); 457263d5341fSzhenwei pi return; 457363d5341fSzhenwei pi } 45747e5c776dSVladimir Sementsov-Ogievskiy } 45757e5c776dSVladimir Sementsov-Ogievskiy } 45767e5c776dSVladimir Sementsov-Ogievskiy 45770006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 45784d454574SPaolo Bonzini .name = "drive", 45790006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 45804d454574SPaolo Bonzini .desc = { 45814d454574SPaolo Bonzini { 45824d454574SPaolo Bonzini .name = "snapshot", 45834d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 45844d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 45854d454574SPaolo Bonzini },{ 45864d454574SPaolo Bonzini .name = "aio", 45874d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 45884d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 45894d454574SPaolo Bonzini },{ 4590e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4591e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4592e4b24b49SKevin Wolf .help = "Enable writeback mode", 4593e4b24b49SKevin Wolf },{ 45944d454574SPaolo Bonzini .name = "format", 45954d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 45964d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 45974d454574SPaolo Bonzini },{ 45984d454574SPaolo Bonzini .name = "rerror", 45994d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 46004d454574SPaolo Bonzini .help = "read error action", 46014d454574SPaolo Bonzini },{ 46024d454574SPaolo Bonzini .name = "werror", 46034d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 46044d454574SPaolo Bonzini .help = "write error action", 46054d454574SPaolo Bonzini },{ 46064e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 46074d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 46084d454574SPaolo Bonzini .help = "open drive file as read-only", 4609a2a7862cSPradeep Jagadeesh }, 4610a2a7862cSPradeep Jagadeesh 4611a2a7862cSPradeep Jagadeesh THROTTLE_OPTS, 4612a2a7862cSPradeep Jagadeesh 4613a2a7862cSPradeep Jagadeesh { 461476f4afb4SAlberto Garcia .name = "throttling.group", 461576f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 461676f4afb4SAlberto Garcia .help = "name of the block throttling group", 461776f4afb4SAlberto Garcia },{ 46184d454574SPaolo Bonzini .name = "copy-on-read", 46194d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 46204d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4621465bee1dSPeter Lieven },{ 4622465bee1dSPeter Lieven .name = "detect-zeroes", 4623465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4624465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4625362e9299SAlberto Garcia },{ 4626362e9299SAlberto Garcia .name = "stats-account-invalid", 4627362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4628362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4629362e9299SAlberto Garcia "in the statistics", 4630362e9299SAlberto Garcia },{ 4631362e9299SAlberto Garcia .name = "stats-account-failed", 4632362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4633362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4634362e9299SAlberto Garcia "in the statistics", 46354d454574SPaolo Bonzini }, 46364d454574SPaolo Bonzini { /* end of list */ } 46374d454574SPaolo Bonzini }, 46384d454574SPaolo Bonzini }; 46390006383eSKevin Wolf 46400006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 46410006383eSKevin Wolf .name = "drive", 46420006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 46430006383eSKevin Wolf .desc = { 4644492fdc6fSKevin Wolf /* 4645492fdc6fSKevin Wolf * no elements => accept any params 4646492fdc6fSKevin Wolf * validation will happen later 4647492fdc6fSKevin Wolf */ 46480006383eSKevin Wolf { /* end of list */ } 46490006383eSKevin Wolf }, 46500006383eSKevin Wolf }; 4651