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" 62b39847a5SPavel Dovgalyuk #include "sysemu/replay.h" 63f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 64f348b6d1SVeronia Bahaa #include "qemu/help_option.h" 65db725815SMarkus Armbruster #include "qemu/main-loop.h" 66a2a7862cSPradeep Jagadeesh #include "qemu/throttle-options.h" 67666daa68SMarkus Armbruster 6889802d5aSMaxim Levitsky QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = 699c4218e9SMax Reitz QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); 709c4218e9SMax Reitz 7189802d5aSMaxim Levitsky void bdrv_set_monitor_owned(BlockDriverState *bs) 7289802d5aSMaxim Levitsky { 7389802d5aSMaxim Levitsky QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 7489802d5aSMaxim Levitsky } 7589802d5aSMaxim Levitsky 761960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 771960966dSMarkus Armbruster [IF_NONE] = "none", 781960966dSMarkus Armbruster [IF_IDE] = "ide", 791960966dSMarkus Armbruster [IF_SCSI] = "scsi", 801960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 811960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 821960966dSMarkus Armbruster [IF_MTD] = "mtd", 831960966dSMarkus Armbruster [IF_SD] = "sd", 841960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 851960966dSMarkus Armbruster [IF_XEN] = "xen", 861960966dSMarkus Armbruster }; 871960966dSMarkus Armbruster 8821dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = { 8927d6bf40SMarkus Armbruster /* 9027d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 9127d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 9227d6bf40SMarkus Armbruster * 93547cb157SWei Jiangang * All controllers used to implement if=T drives need to support 9427d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 9527d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 9627d6bf40SMarkus Armbruster * values. 9727d6bf40SMarkus Armbruster * 9827d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 9927d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 10027d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 10127d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 10227d6bf40SMarkus Armbruster */ 10327d6bf40SMarkus Armbruster [IF_IDE] = 2, 10427d6bf40SMarkus Armbruster [IF_SCSI] = 7, 1051960966dSMarkus Armbruster }; 1061960966dSMarkus Armbruster 10721dff8cfSJohn Snow /** 10821dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides 10921dff8cfSJohn Snow * of the default, global values. 11021dff8cfSJohn Snow */ 11121dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs) 11221dff8cfSJohn Snow { 11318e46a03SMarkus Armbruster BlockBackend *blk; 11421dff8cfSJohn Snow DriveInfo *dinfo; 11521dff8cfSJohn Snow 11621dff8cfSJohn Snow if (max_devs <= 0) { 11721dff8cfSJohn Snow return; 11821dff8cfSJohn Snow } 11921dff8cfSJohn Snow 12018e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 12118e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 12221dff8cfSJohn Snow if (dinfo->type == type) { 12321dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of" 12421dff8cfSJohn Snow " the %s interface, because a drive of that type has" 12521dff8cfSJohn Snow " already been added.\n", if_name[type]); 12621dff8cfSJohn Snow g_assert_not_reached(); 12721dff8cfSJohn Snow } 12821dff8cfSJohn Snow } 12921dff8cfSJohn Snow 13021dff8cfSJohn Snow if_max_devs[type] = max_devs; 13121dff8cfSJohn Snow } 13221dff8cfSJohn Snow 13314bafc54SMarkus Armbruster /* 13414bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 13514bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 13614bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 13714bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 13814bafc54SMarkus Armbruster * when deletion is actually safe. 13914bafc54SMarkus Armbruster */ 1404be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk) 14114bafc54SMarkus Armbruster { 14218e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 1438164102fSVladimir Sementsov-Ogievskiy BlockJob *job; 14414bafc54SMarkus Armbruster 14526f8b3a8SMarkus Armbruster if (!dinfo) { 1462d246f01SKevin Wolf return; 1472d246f01SKevin Wolf } 1482d246f01SKevin Wolf 1498164102fSVladimir Sementsov-Ogievskiy for (job = block_job_next(NULL); job; job = block_job_next(job)) { 1508164102fSVladimir Sementsov-Ogievskiy if (block_job_has_bdrv(job, blk_bs(blk))) { 1518164102fSVladimir Sementsov-Ogievskiy AioContext *aio_context = job->job.aio_context; 15291fddb0dSStefan Hajnoczi aio_context_acquire(aio_context); 15391fddb0dSStefan Hajnoczi 1548164102fSVladimir Sementsov-Ogievskiy job_cancel(&job->job, false); 15591fddb0dSStefan Hajnoczi 15691fddb0dSStefan Hajnoczi aio_context_release(aio_context); 1575433c24fSMax Reitz } 1588164102fSVladimir Sementsov-Ogievskiy } 15991fddb0dSStefan Hajnoczi 16014bafc54SMarkus Armbruster dinfo->auto_del = 1; 16114bafc54SMarkus Armbruster } 16214bafc54SMarkus Armbruster 1634be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk) 16414bafc54SMarkus Armbruster { 16518e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 16614bafc54SMarkus Armbruster 1670fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 168efaa7c4eSMax Reitz monitor_remove_blk(blk); 169b9fe8a7aSMarkus Armbruster blk_unref(blk); 17014bafc54SMarkus Armbruster } 17114bafc54SMarkus Armbruster } 17214bafc54SMarkus Armbruster 173d8f94e1bSJohn Snow /** 174d8f94e1bSJohn Snow * Returns the current mapping of how many units per bus 175d8f94e1bSJohn Snow * a particular interface can support. 176d8f94e1bSJohn Snow * 177d8f94e1bSJohn Snow * A positive integer indicates n units per bus. 178d8f94e1bSJohn Snow * 0 implies the mapping has not been established. 179d8f94e1bSJohn Snow * -1 indicates an invalid BlockInterfaceType was given. 180d8f94e1bSJohn Snow */ 181d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type) 182d8f94e1bSJohn Snow { 183d8f94e1bSJohn Snow if (type >= IF_IDE && type < IF_COUNT) { 184d8f94e1bSJohn Snow return if_max_devs[type]; 185d8f94e1bSJohn Snow } 186d8f94e1bSJohn Snow 187d8f94e1bSJohn Snow return -1; 188d8f94e1bSJohn Snow } 189d8f94e1bSJohn Snow 190505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 191505a7fb1SMarkus Armbruster { 192505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 193505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 194505a7fb1SMarkus Armbruster } 195505a7fb1SMarkus Armbruster 196505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 197505a7fb1SMarkus Armbruster { 198505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 199505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 200505a7fb1SMarkus Armbruster } 201505a7fb1SMarkus Armbruster 2022292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 2032292ddaeSMarkus Armbruster { 20470b94331SMarkus Armbruster return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 2052292ddaeSMarkus Armbruster } 2062292ddaeSMarkus Armbruster 2072292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 2085645b0f4SMarkus Armbruster const char *optstr) 209666daa68SMarkus Armbruster { 210666daa68SMarkus Armbruster QemuOpts *opts; 211666daa68SMarkus Armbruster 2122292ddaeSMarkus Armbruster opts = drive_def(optstr); 213666daa68SMarkus Armbruster if (!opts) { 214666daa68SMarkus Armbruster return NULL; 215666daa68SMarkus Armbruster } 2162292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 217f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort); 2182292ddaeSMarkus Armbruster } 2192292ddaeSMarkus Armbruster if (index >= 0) { 220a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort); 2212292ddaeSMarkus Armbruster } 222666daa68SMarkus Armbruster if (file) 223f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort); 224666daa68SMarkus Armbruster return opts; 225666daa68SMarkus Armbruster } 226666daa68SMarkus Armbruster 227666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 228666daa68SMarkus Armbruster { 22918e46a03SMarkus Armbruster BlockBackend *blk; 230666daa68SMarkus Armbruster DriveInfo *dinfo; 231666daa68SMarkus Armbruster 23218e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 23318e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 23418e46a03SMarkus Armbruster if (dinfo && dinfo->type == type 23518e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) { 236666daa68SMarkus Armbruster return dinfo; 237666daa68SMarkus Armbruster } 23818e46a03SMarkus Armbruster } 239666daa68SMarkus Armbruster 240666daa68SMarkus Armbruster return NULL; 241666daa68SMarkus Armbruster } 242666daa68SMarkus Armbruster 243a1b40bdaSMarkus Armbruster void drive_mark_claimed_by_board(void) 244a1b40bdaSMarkus Armbruster { 245a1b40bdaSMarkus Armbruster BlockBackend *blk; 246a1b40bdaSMarkus Armbruster DriveInfo *dinfo; 247a1b40bdaSMarkus Armbruster 248a1b40bdaSMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 249a1b40bdaSMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 250a1b40bdaSMarkus Armbruster if (dinfo && blk_get_attached_dev(blk)) { 251a1b40bdaSMarkus Armbruster dinfo->claimed_by_board = true; 252a1b40bdaSMarkus Armbruster } 253a1b40bdaSMarkus Armbruster } 254a1b40bdaSMarkus Armbruster } 255a1b40bdaSMarkus Armbruster 256720b8dc0SMarkus Armbruster void drive_check_orphaned(void) 257a66c9dc7SJohn Snow { 25818e46a03SMarkus Armbruster BlockBackend *blk; 259a66c9dc7SJohn Snow DriveInfo *dinfo; 260664cc623SMarkus Armbruster Location loc; 261720b8dc0SMarkus Armbruster bool orphans = false; 262a66c9dc7SJohn Snow 26318e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 26418e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 265a1b40bdaSMarkus Armbruster if (dinfo->is_default || dinfo->type == IF_NONE) { 266a1b40bdaSMarkus Armbruster continue; 267a1b40bdaSMarkus Armbruster } 268a1b40bdaSMarkus Armbruster if (!blk_get_attached_dev(blk)) { 269664cc623SMarkus Armbruster loc_push_none(&loc); 270664cc623SMarkus Armbruster qemu_opts_loc_restore(dinfo->opts); 271720b8dc0SMarkus Armbruster error_report("machine type does not support" 272664cc623SMarkus Armbruster " if=%s,bus=%d,unit=%d", 2735433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit); 274664cc623SMarkus Armbruster loc_pop(&loc); 275720b8dc0SMarkus Armbruster orphans = true; 276a1b40bdaSMarkus Armbruster continue; 277a1b40bdaSMarkus Armbruster } 278a1b40bdaSMarkus Armbruster if (!dinfo->claimed_by_board && dinfo->type != IF_VIRTIO) { 279a1b40bdaSMarkus Armbruster loc_push_none(&loc); 280a1b40bdaSMarkus Armbruster qemu_opts_loc_restore(dinfo->opts); 281a1b40bdaSMarkus Armbruster warn_report("bogus if=%s is deprecated, use if=none", 282a1b40bdaSMarkus Armbruster if_name[dinfo->type]); 283a1b40bdaSMarkus Armbruster loc_pop(&loc); 284a66c9dc7SJohn Snow } 285a66c9dc7SJohn Snow } 286a66c9dc7SJohn Snow 287720b8dc0SMarkus Armbruster if (orphans) { 288720b8dc0SMarkus Armbruster exit(1); 289720b8dc0SMarkus Armbruster } 290a66c9dc7SJohn Snow } 291a66c9dc7SJohn Snow 292f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 293f1bd51acSMarkus Armbruster { 294f1bd51acSMarkus Armbruster return drive_get(type, 295f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 296f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 297f1bd51acSMarkus Armbruster } 298f1bd51acSMarkus Armbruster 299666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 300666daa68SMarkus Armbruster { 301666daa68SMarkus Armbruster int max_bus; 30218e46a03SMarkus Armbruster BlockBackend *blk; 303666daa68SMarkus Armbruster DriveInfo *dinfo; 304666daa68SMarkus Armbruster 305666daa68SMarkus Armbruster max_bus = -1; 30618e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 30718e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 30818e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 309666daa68SMarkus Armbruster max_bus = dinfo->bus; 310666daa68SMarkus Armbruster } 31118e46a03SMarkus Armbruster } 312666daa68SMarkus Armbruster return max_bus; 313666daa68SMarkus Armbruster } 314666daa68SMarkus Armbruster 31513839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 31613839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 31713839974SMarkus Armbruster appropriate bus. */ 31813839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 31913839974SMarkus Armbruster { 32013839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 32113839974SMarkus Armbruster 32213839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 32313839974SMarkus Armbruster } 32413839974SMarkus Armbruster 325666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 326666daa68SMarkus Armbruster { 327cdcd4361SMarkus Armbruster qemu_printf(" %s", name); 328666daa68SMarkus Armbruster } 329666daa68SMarkus Armbruster 330aa398a5cSStefan Hajnoczi typedef struct { 331aa398a5cSStefan Hajnoczi QEMUBH *bh; 332fa510ebfSFam Zheng BlockDriverState *bs; 333fa510ebfSFam Zheng } BDRVPutRefBH; 334aa398a5cSStefan Hajnoczi 335b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 336666daa68SMarkus Armbruster { 337666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 33892aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 339666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 34092aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 341666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 34292aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 343666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 34492aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 345666daa68SMarkus Armbruster } else { 346b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 347666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 348666daa68SMarkus Armbruster return -1; 349666daa68SMarkus Armbruster } 350666daa68SMarkus Armbruster } 351666daa68SMarkus Armbruster 35240119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 35340119effSAlberto Garcia Error **errp) 35440119effSAlberto Garcia { 35540119effSAlberto Garcia const QListEntry *entry; 35640119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 35740119effSAlberto Garcia switch (qobject_type(entry->value)) { 35840119effSAlberto Garcia 35940119effSAlberto Garcia case QTYPE_QSTRING: { 36040119effSAlberto Garcia unsigned long long length; 3617dc847ebSMax Reitz const char *str = qstring_get_str(qobject_to(QString, 3627dc847ebSMax Reitz entry->value)); 36340119effSAlberto Garcia if (parse_uint_full(str, &length, 10) == 0 && 36440119effSAlberto Garcia length > 0 && length <= UINT_MAX) { 36540119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 36640119effSAlberto Garcia } else { 36740119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str); 36840119effSAlberto Garcia return false; 36940119effSAlberto Garcia } 37040119effSAlberto Garcia break; 37140119effSAlberto Garcia } 37240119effSAlberto Garcia 37301b2ffceSMarc-André Lureau case QTYPE_QNUM: { 3747dc847ebSMax Reitz int64_t length = qnum_get_int(qobject_to(QNum, entry->value)); 37501b2ffceSMarc-André Lureau 37640119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) { 37740119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 37840119effSAlberto Garcia } else { 37940119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length); 38040119effSAlberto Garcia return false; 38140119effSAlberto Garcia } 38240119effSAlberto Garcia break; 38340119effSAlberto Garcia } 38440119effSAlberto Garcia 38540119effSAlberto Garcia default: 38640119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid"); 38740119effSAlberto Garcia return false; 38840119effSAlberto Garcia } 38940119effSAlberto Garcia } 39040119effSAlberto Garcia return true; 39140119effSAlberto Garcia } 39240119effSAlberto Garcia 39333cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 39433cb7dc8SKevin Wolf 395fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 396fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 397fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 398fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 399fbf8175eSMax Reitz { 400fbf8175eSMax Reitz Error *local_error = NULL; 401fbf8175eSMax Reitz const char *aio; 402fbf8175eSMax Reitz 403fbf8175eSMax Reitz if (bdrv_flags) { 404fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 405fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 406fbf8175eSMax Reitz } 407fbf8175eSMax Reitz 408fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 409f80f2673SAarushi Mehta if (bdrv_parse_aio(aio, bdrv_flags) < 0) { 410fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 411fbf8175eSMax Reitz return; 412fbf8175eSMax Reitz } 413fbf8175eSMax Reitz } 414fbf8175eSMax Reitz } 415fbf8175eSMax Reitz 416fbf8175eSMax Reitz /* disk I/O throttling */ 417fbf8175eSMax Reitz if (throttling_group) { 418fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 419fbf8175eSMax Reitz } 420fbf8175eSMax Reitz 421fbf8175eSMax Reitz if (throttle_cfg) { 4221588ab5dSAlberto Garcia throttle_config_init(throttle_cfg); 423fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 424fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 425fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 426fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 427fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 428fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 429fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 430fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 431fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 432fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 433fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 434fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 435fbf8175eSMax Reitz 436fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 437fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 438fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 439fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 440fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 441fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 442fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 443fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 444fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 445fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 446fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 447fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 448fbf8175eSMax Reitz 4498a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = 4508a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); 4518a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = 4528a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); 4538a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = 4548a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); 4558a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = 4568a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); 4578a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = 4588a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); 4598a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = 4608a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); 4618a0fc18dSAlberto Garcia 462fbf8175eSMax Reitz throttle_cfg->op_size = 463fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 464fbf8175eSMax Reitz 465d5851089SAlberto Garcia if (!throttle_is_valid(throttle_cfg, errp)) { 466fbf8175eSMax Reitz return; 467fbf8175eSMax Reitz } 468fbf8175eSMax Reitz } 469fbf8175eSMax Reitz 470fbf8175eSMax Reitz if (detect_zeroes) { 471fbf8175eSMax Reitz *detect_zeroes = 472f7abe0ecSMarc-André Lureau qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, 473fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 474fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 475fbf8175eSMax Reitz &local_error); 476fbf8175eSMax Reitz if (local_error) { 477fbf8175eSMax Reitz error_propagate(errp, local_error); 478fbf8175eSMax Reitz return; 479fbf8175eSMax Reitz } 480fbf8175eSMax Reitz } 481fbf8175eSMax Reitz } 482fbf8175eSMax Reitz 483f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 48418e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 485b681072dSKevin Wolf Error **errp) 486666daa68SMarkus Armbruster { 487666daa68SMarkus Armbruster const char *buf; 488666daa68SMarkus Armbruster int bdrv_flags = 0; 489666daa68SMarkus Armbruster int on_read_error, on_write_error; 490362e9299SAlberto Garcia bool account_invalid, account_failed; 491f87a0e29SAlberto Garcia bool writethrough, read_only; 49226f54e9aSMarkus Armbruster BlockBackend *blk; 493a0f1eab1SMarkus Armbruster BlockDriverState *bs; 494cc0681c4SBenoît Canet ThrottleConfig cfg; 495666daa68SMarkus Armbruster int snapshot = 0; 496c546194fSStefan Hajnoczi Error *error = NULL; 4970006383eSKevin Wolf QemuOpts *opts; 49840119effSAlberto Garcia QDict *interval_dict = NULL; 49940119effSAlberto Garcia QList *interval_list = NULL; 5000006383eSKevin Wolf const char *id; 501fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 502fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 503fbf8175eSMax Reitz const char *throttling_group = NULL; 504666daa68SMarkus Armbruster 505f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 506f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 507f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 508c6ecec43SMarkus Armbruster opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, errp); 509c6ecec43SMarkus Armbruster if (!opts) { 5106376f952SMarkus Armbruster goto err_no_opts; 5110006383eSKevin Wolf } 5120006383eSKevin Wolf 513668f62ecSMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, bs_opts, errp)) { 514ec9c10d2SStefan Hajnoczi goto early_err; 5150006383eSKevin Wolf } 5160006383eSKevin Wolf 5170006383eSKevin Wolf if (id) { 5180006383eSKevin Wolf qdict_del(bs_opts, "id"); 5190006383eSKevin Wolf } 5200006383eSKevin Wolf 521666daa68SMarkus Armbruster /* extract parameters */ 522666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 523666daa68SMarkus Armbruster 524362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 525362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 526362e9299SAlberto Garcia 527e4b24b49SKevin Wolf writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); 528e4b24b49SKevin Wolf 529ff356ee4SAlberto Garcia id = qemu_opts_id(opts); 530ff356ee4SAlberto Garcia 53140119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 53240119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 53340119effSAlberto Garcia 53440119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 53540119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 53640119effSAlberto Garcia qdict_first(interval_dict)->key); 53740119effSAlberto Garcia goto early_err; 53840119effSAlberto Garcia } 5392be5506fSAlberto Garcia 540fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 541fbf8175eSMax Reitz &detect_zeroes, &error); 542fbf8175eSMax Reitz if (error) { 543fbf8175eSMax Reitz error_propagate(errp, error); 544ec9c10d2SStefan Hajnoczi goto early_err; 545a9384affSPaolo Bonzini } 546666daa68SMarkus Armbruster 547666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 548c8057f95SPeter Maydell if (is_help_option(buf)) { 549cdcd4361SMarkus Armbruster qemu_printf("Supported formats:"); 5509ac404c5SAndrey Shinkevich bdrv_iterate_format(bdrv_format_print, NULL, false); 551cdcd4361SMarkus Armbruster qemu_printf("\nSupported formats (read-only):"); 5529ac404c5SAndrey Shinkevich bdrv_iterate_format(bdrv_format_print, NULL, true); 553cdcd4361SMarkus Armbruster qemu_printf("\n"); 554ec9c10d2SStefan Hajnoczi goto early_err; 555666daa68SMarkus Armbruster } 55674fe54f2SKevin Wolf 557e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 558e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 559ec9c10d2SStefan Hajnoczi goto early_err; 5606db5f5d6SMike Qiu } 56146f5ac20SEric Blake qdict_put_str(bs_opts, "driver", buf); 562666daa68SMarkus Armbruster } 563666daa68SMarkus Armbruster 56492aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 565666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 566b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 56784d18f06SMarkus Armbruster if (error) { 568b681072dSKevin Wolf error_propagate(errp, error); 569ec9c10d2SStefan Hajnoczi goto early_err; 570666daa68SMarkus Armbruster } 571666daa68SMarkus Armbruster } 572666daa68SMarkus Armbruster 57392aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 574666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 575b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 57684d18f06SMarkus Armbruster if (error) { 577b681072dSKevin Wolf error_propagate(errp, error); 578ec9c10d2SStefan Hajnoczi goto early_err; 579666daa68SMarkus Armbruster } 580666daa68SMarkus Armbruster } 581666daa68SMarkus Armbruster 582666daa68SMarkus Armbruster if (snapshot) { 58391a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 584666daa68SMarkus Armbruster } 585666daa68SMarkus Armbruster 586f87a0e29SAlberto Garcia read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false); 587f87a0e29SAlberto Garcia 5885ec18f8cSMax Reitz /* init */ 58939c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5905ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5915ec18f8cSMax Reitz 592d861ab3aSKevin Wolf blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); 5935ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5945ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 595f87a0e29SAlberto Garcia blk_rs->read_only = read_only; 5965ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5975ec18f8cSMax Reitz 598cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 5995ec18f8cSMax Reitz } else { 6005ec18f8cSMax Reitz if (file && !*file) { 6015ec18f8cSMax Reitz file = NULL; 6025ec18f8cSMax Reitz } 6035ec18f8cSMax Reitz 60491a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 60591a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 60691a097e7SKevin Wolf * Apply the defaults here instead. */ 60791a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 60891a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 609f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, 610f87a0e29SAlberto Garcia read_only ? "on" : "off"); 6119384a444SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on"); 61272e775c7SKevin Wolf assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); 61391a097e7SKevin Wolf 61412d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 61512d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 61612d5ee3aSKevin Wolf } 61712d5ee3aSKevin Wolf 618efaa7c4eSMax Reitz blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); 619e4342ce5SMax Reitz if (!blk) { 620e4342ce5SMax Reitz goto err_no_bs_opts; 621e4342ce5SMax Reitz } 622e4342ce5SMax Reitz bs = blk_bs(blk); 6230006383eSKevin Wolf 624e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 625e4342ce5SMax Reitz 6269caa6f3dSPaolo Bonzini block_acct_setup(blk_get_stats(blk), account_invalid, account_failed); 6272be5506fSAlberto Garcia 62840119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6292be5506fSAlberto Garcia blk_unref(blk); 6302be5506fSAlberto Garcia blk = NULL; 6312be5506fSAlberto Garcia goto err_no_bs_opts; 6322be5506fSAlberto Garcia } 6332be5506fSAlberto Garcia } 6345ec18f8cSMax Reitz 6357ca7f0f6SKevin Wolf /* disk I/O throttling */ 6367ca7f0f6SKevin Wolf if (throttle_enabled(&cfg)) { 6377ca7f0f6SKevin Wolf if (!throttling_group) { 638ff356ee4SAlberto Garcia throttling_group = id; 6397ca7f0f6SKevin Wolf } 6407ca7f0f6SKevin Wolf blk_io_limits_enable(blk, throttling_group); 6417ca7f0f6SKevin Wolf blk_set_io_limits(blk, &cfg); 6427ca7f0f6SKevin Wolf } 6437ca7f0f6SKevin Wolf 644e4b24b49SKevin Wolf blk_set_enable_write_cache(blk, !writethrough); 6455ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6460006383eSKevin Wolf 647ff356ee4SAlberto Garcia if (!monitor_add_blk(blk, id, errp)) { 648efaa7c4eSMax Reitz blk_unref(blk); 649efaa7c4eSMax Reitz blk = NULL; 650efaa7c4eSMax Reitz goto err_no_bs_opts; 651efaa7c4eSMax Reitz } 652efaa7c4eSMax Reitz 653e4342ce5SMax Reitz err_no_bs_opts: 6540006383eSKevin Wolf qemu_opts_del(opts); 655cb3e7f08SMarc-André Lureau qobject_unref(interval_dict); 656cb3e7f08SMarc-André Lureau qobject_unref(interval_list); 65718e46a03SMarkus Armbruster return blk; 658a9ae2bffSMarkus Armbruster 659ec9c10d2SStefan Hajnoczi early_err: 660ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 661cb3e7f08SMarc-André Lureau qobject_unref(interval_dict); 662cb3e7f08SMarc-André Lureau qobject_unref(interval_list); 6636376f952SMarkus Armbruster err_no_opts: 664cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 665a9ae2bffSMarkus Armbruster return NULL; 666666daa68SMarkus Armbruster } 667666daa68SMarkus Armbruster 668bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 66989802d5aSMaxim Levitsky BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 670bd745e23SMax Reitz { 671bd745e23SMax Reitz int bdrv_flags = 0; 672bd745e23SMax Reitz 673a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 674a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults. 675a81d6164SKevin Wolf * Apply the defaults here instead. */ 676a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 677a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 678f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off"); 679a81d6164SKevin Wolf 68012d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 68112d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 68212d5ee3aSKevin Wolf } 68312d5ee3aSKevin Wolf 68474e1ae7cSKevin Wolf return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); 685bd745e23SMax Reitz } 686bd745e23SMax Reitz 6879c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void) 6889c4218e9SMax Reitz { 6899c4218e9SMax Reitz BlockDriverState *bs, *next_bs; 6909c4218e9SMax Reitz 6919c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { 6929c4218e9SMax Reitz AioContext *ctx = bdrv_get_aio_context(bs); 6939c4218e9SMax Reitz 6949c4218e9SMax Reitz aio_context_acquire(ctx); 6959c4218e9SMax Reitz bdrv_unref(bs); 6969c4218e9SMax Reitz aio_context_release(ctx); 6979c4218e9SMax Reitz } 6989c4218e9SMax Reitz } 6999c4218e9SMax Reitz 700262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */ 701262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) 702262b4e8fSMax Reitz { 703262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list) 704262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states); 705262b4e8fSMax Reitz } 706262b4e8fSMax Reitz 707c75d7f71SMarkus Armbruster static bool qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 7085abbf0eeSKevin Wolf Error **errp) 70957975222SKevin Wolf { 71057975222SKevin Wolf const char *value; 71157975222SKevin Wolf 71257975222SKevin Wolf value = qemu_opt_get(opts, from); 71357975222SKevin Wolf if (value) { 7145abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 7155abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 7165abbf0eeSKevin Wolf "same time", to, from); 717c75d7f71SMarkus Armbruster return false; 7185abbf0eeSKevin Wolf } 71920d6cd47SJun Li } 72020d6cd47SJun Li 72120d6cd47SJun Li /* rename all items in opts */ 72220d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 723f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 72457975222SKevin Wolf qemu_opt_unset(opts, from); 72557975222SKevin Wolf } 726c75d7f71SMarkus Armbruster return true; 72757975222SKevin Wolf } 72857975222SKevin Wolf 72933cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 73033cb7dc8SKevin Wolf .name = "drive", 73133cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 73233cb7dc8SKevin Wolf .desc = { 73333cb7dc8SKevin Wolf { 73487a899c5SKevin Wolf .name = "bus", 73587a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 73687a899c5SKevin Wolf .help = "bus number", 73787a899c5SKevin Wolf },{ 73887a899c5SKevin Wolf .name = "unit", 73987a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 74087a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 74187a899c5SKevin Wolf },{ 74287a899c5SKevin Wolf .name = "index", 74387a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 74487a899c5SKevin Wolf .help = "index number", 74587a899c5SKevin Wolf },{ 74633cb7dc8SKevin Wolf .name = "media", 74733cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 74833cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 749593d464bSKevin Wolf },{ 750593d464bSKevin Wolf .name = "if", 751593d464bSKevin Wolf .type = QEMU_OPT_STRING, 752593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 753b41a7338SKevin Wolf },{ 754d095b465SMax Reitz .name = "file", 755d095b465SMax Reitz .type = QEMU_OPT_STRING, 756d095b465SMax Reitz .help = "file name", 75733cb7dc8SKevin Wolf }, 7580ebd24e0SKevin Wolf 7590ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 7600ebd24e0SKevin Wolf { 7614e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 7620ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7630ebd24e0SKevin Wolf .help = "open drive file as read-only", 7640ebd24e0SKevin Wolf },{ 765ee13ed1cSKevin Wolf .name = "rerror", 766ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 767ee13ed1cSKevin Wolf .help = "read error action", 768ee13ed1cSKevin Wolf },{ 769ee13ed1cSKevin Wolf .name = "werror", 770ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 771ee13ed1cSKevin Wolf .help = "write error action", 772ee13ed1cSKevin Wolf },{ 7730ebd24e0SKevin Wolf .name = "copy-on-read", 7740ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7750ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 7760ebd24e0SKevin Wolf }, 7770ebd24e0SKevin Wolf 77833cb7dc8SKevin Wolf { /* end of list */ } 77933cb7dc8SKevin Wolf }, 78033cb7dc8SKevin Wolf }; 78133cb7dc8SKevin Wolf 782c4f26c9fSMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type, 783c4f26c9fSMarkus Armbruster Error **errp) 78457975222SKevin Wolf { 78529c4e2b5SKevin Wolf const char *value; 78618e46a03SMarkus Armbruster BlockBackend *blk; 78733cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 788f298d071SKevin Wolf QDict *bs_opts; 78933cb7dc8SKevin Wolf QemuOpts *legacy_opts; 79033cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 791593d464bSKevin Wolf BlockInterfaceType type; 79287a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 793ee13ed1cSKevin Wolf const char *werror, *rerror; 794a7fdbcf0SFam Zheng bool read_only = false; 795a7fdbcf0SFam Zheng bool copy_on_read; 796d095b465SMax Reitz const char *filename; 797247147fbSKevin Wolf int i; 79829c4e2b5SKevin Wolf 79957975222SKevin Wolf /* Change legacy command line options into QMP ones */ 800247147fbSKevin Wolf static const struct { 801247147fbSKevin Wolf const char *from; 802247147fbSKevin Wolf const char *to; 803247147fbSKevin Wolf } opt_renames[] = { 804247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 805247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 806247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 80757975222SKevin Wolf 808247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 809247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 810247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 81157975222SKevin Wolf 812247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 813247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 814247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8153e9fab69SBenoît Canet 816247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 817247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 818247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8193e9fab69SBenoît Canet 820247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8212024c1dfSBenoît Canet 82276f4afb4SAlberto Garcia { "group", "throttling.group" }, 82376f4afb4SAlberto Garcia 8244e200cf8SAlberto Garcia { "readonly", BDRV_OPT_READ_ONLY }, 825247147fbSKevin Wolf }; 826247147fbSKevin Wolf 827247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 828235e59cfSMarkus Armbruster if (!qemu_opt_rename(all_opts, opt_renames[i].from, 829668f62ecSMarkus Armbruster opt_renames[i].to, errp)) { 8305abbf0eeSKevin Wolf return NULL; 8315abbf0eeSKevin Wolf } 832247147fbSKevin Wolf } 8330f227a94SKevin Wolf 83429c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 83529c4e2b5SKevin Wolf if (value) { 83629c4e2b5SKevin Wolf int flags = 0; 83704feb4a5SKevin Wolf bool writethrough; 83829c4e2b5SKevin Wolf 83904feb4a5SKevin Wolf if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { 840c4f26c9fSMarkus Armbruster error_setg(errp, "invalid cache option"); 84129c4e2b5SKevin Wolf return NULL; 84229c4e2b5SKevin Wolf } 84329c4e2b5SKevin Wolf 84429c4e2b5SKevin Wolf /* Specific options take precedence */ 84554861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 84654861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 84704feb4a5SKevin Wolf !writethrough, &error_abort); 84829c4e2b5SKevin Wolf } 84954861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 85054861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 851cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 85229c4e2b5SKevin Wolf } 85354861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 85454861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 855cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 85629c4e2b5SKevin Wolf } 85729c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 85829c4e2b5SKevin Wolf } 85929c4e2b5SKevin Wolf 860f298d071SKevin Wolf /* Get a QDict for processing the options */ 861f298d071SKevin Wolf bs_opts = qdict_new(); 862f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 863f298d071SKevin Wolf 86487ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 86587ea75d5SPeter Crosthwaite &error_abort); 866af175e85SMarkus Armbruster if (!qemu_opts_absorb_qdict(legacy_opts, bs_opts, errp)) { 86733cb7dc8SKevin Wolf goto fail; 86833cb7dc8SKevin Wolf } 86933cb7dc8SKevin Wolf 87033cb7dc8SKevin Wolf /* Media type */ 87133cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 87233cb7dc8SKevin Wolf if (value) { 87333cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 87433cb7dc8SKevin Wolf media = MEDIA_DISK; 87533cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 87633cb7dc8SKevin Wolf media = MEDIA_CDROM; 877a7fdbcf0SFam Zheng read_only = true; 87833cb7dc8SKevin Wolf } else { 879c4f26c9fSMarkus Armbruster error_setg(errp, "'%s' invalid media", value); 88033cb7dc8SKevin Wolf goto fail; 88133cb7dc8SKevin Wolf } 88233cb7dc8SKevin Wolf } 88333cb7dc8SKevin Wolf 8840ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 8854e200cf8SAlberto Garcia read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false); 8860ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 8870ebd24e0SKevin Wolf 8880ebd24e0SKevin Wolf if (read_only && copy_on_read) { 8893dc6f869SAlistair Francis warn_report("disabling copy-on-read on read-only drive"); 8900ebd24e0SKevin Wolf copy_on_read = false; 8910ebd24e0SKevin Wolf } 8920ebd24e0SKevin Wolf 89346f5ac20SEric Blake qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off"); 89446f5ac20SEric Blake qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off"); 8950ebd24e0SKevin Wolf 896593d464bSKevin Wolf /* Controller type */ 897593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 898593d464bSKevin Wolf if (value) { 899593d464bSKevin Wolf for (type = 0; 900593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 901593d464bSKevin Wolf type++) { 902593d464bSKevin Wolf } 903593d464bSKevin Wolf if (type == IF_COUNT) { 904c4f26c9fSMarkus Armbruster error_setg(errp, "unsupported bus type '%s'", value); 905593d464bSKevin Wolf goto fail; 906593d464bSKevin Wolf } 907593d464bSKevin Wolf } else { 908593d464bSKevin Wolf type = block_default_type; 909593d464bSKevin Wolf } 910593d464bSKevin Wolf 91187a899c5SKevin Wolf /* Device address specified by bus/unit or index. 91287a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 91387a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 91487a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 91587a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 91687a899c5SKevin Wolf 91787a899c5SKevin Wolf max_devs = if_max_devs[type]; 91887a899c5SKevin Wolf 91987a899c5SKevin Wolf if (index != -1) { 92087a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 921c4f26c9fSMarkus Armbruster error_setg(errp, "index cannot be used with bus and unit"); 92287a899c5SKevin Wolf goto fail; 92387a899c5SKevin Wolf } 92487a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 92587a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 92687a899c5SKevin Wolf } 92787a899c5SKevin Wolf 92887a899c5SKevin Wolf if (unit_id == -1) { 92987a899c5SKevin Wolf unit_id = 0; 93087a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 93187a899c5SKevin Wolf unit_id++; 93287a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 93387a899c5SKevin Wolf unit_id -= max_devs; 93487a899c5SKevin Wolf bus_id++; 93587a899c5SKevin Wolf } 93687a899c5SKevin Wolf } 93787a899c5SKevin Wolf } 93887a899c5SKevin Wolf 93987a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 940c4f26c9fSMarkus Armbruster error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1); 94187a899c5SKevin Wolf goto fail; 94287a899c5SKevin Wolf } 94387a899c5SKevin Wolf 94487a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 945c4f26c9fSMarkus Armbruster error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists", 94687a899c5SKevin Wolf bus_id, unit_id, index); 94787a899c5SKevin Wolf goto fail; 94887a899c5SKevin Wolf } 94987a899c5SKevin Wolf 95087a899c5SKevin Wolf /* no id supplied -> create one */ 95187a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 95287a899c5SKevin Wolf char *new_id; 95387a899c5SKevin Wolf const char *mediastr = ""; 95487a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 95587a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 95687a899c5SKevin Wolf } 95787a899c5SKevin Wolf if (max_devs) { 95887a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 95987a899c5SKevin Wolf mediastr, unit_id); 96087a899c5SKevin Wolf } else { 96187a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 96287a899c5SKevin Wolf mediastr, unit_id); 96387a899c5SKevin Wolf } 96446f5ac20SEric Blake qdict_put_str(bs_opts, "id", new_id); 96587a899c5SKevin Wolf g_free(new_id); 96687a899c5SKevin Wolf } 96787a899c5SKevin Wolf 968394c7d4dSKevin Wolf /* Add virtio block device */ 969394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 970394c7d4dSKevin Wolf QemuOpts *devopts; 97187ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 97287ea75d5SPeter Crosthwaite &error_abort); 973394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 9741f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 975394c7d4dSKevin Wolf } else { 976f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 977394c7d4dSKevin Wolf } 978f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 979f43e47dbSMarkus Armbruster &error_abort); 980394c7d4dSKevin Wolf } 981394c7d4dSKevin Wolf 982d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 983d095b465SMax Reitz 984ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 985ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 986ee13ed1cSKevin Wolf if (werror != NULL) { 987ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 988ee13ed1cSKevin Wolf type != IF_NONE) { 989c4f26c9fSMarkus Armbruster error_setg(errp, "werror is not supported by this bus type"); 990ee13ed1cSKevin Wolf goto fail; 991ee13ed1cSKevin Wolf } 99246f5ac20SEric Blake qdict_put_str(bs_opts, "werror", werror); 993ee13ed1cSKevin Wolf } 994ee13ed1cSKevin Wolf 995ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 996ee13ed1cSKevin Wolf if (rerror != NULL) { 997ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 998ee13ed1cSKevin Wolf type != IF_NONE) { 999c4f26c9fSMarkus Armbruster error_setg(errp, "rerror is not supported by this bus type"); 1000ee13ed1cSKevin Wolf goto fail; 1001ee13ed1cSKevin Wolf } 100246f5ac20SEric Blake qdict_put_str(bs_opts, "rerror", rerror); 1003ee13ed1cSKevin Wolf } 1004ee13ed1cSKevin Wolf 10052d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 1006992861fbSMarkus Armbruster blk = blockdev_init(filename, bs_opts, errp); 10073cb0e25cSMarkus Armbruster bs_opts = NULL; 100818e46a03SMarkus Armbruster if (!blk) { 10092d246f01SKevin Wolf goto fail; 10102d246f01SKevin Wolf } 10112d246f01SKevin Wolf 101226f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 101326f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1014f298d071SKevin Wolf dinfo->opts = all_opts; 10152d246f01SKevin Wolf 1016ee13ed1cSKevin Wolf dinfo->type = type; 101787a899c5SKevin Wolf dinfo->bus = bus_id; 101887a899c5SKevin Wolf dinfo->unit = unit_id; 1019bcf83158SKevin Wolf 102026f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 102126f8b3a8SMarkus Armbruster 1022e34ef046SKevin Wolf switch(type) { 1023e34ef046SKevin Wolf case IF_IDE: 1024e34ef046SKevin Wolf case IF_SCSI: 1025e34ef046SKevin Wolf case IF_XEN: 1026e34ef046SKevin Wolf case IF_NONE: 1027e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1028e34ef046SKevin Wolf break; 1029e34ef046SKevin Wolf default: 1030e34ef046SKevin Wolf break; 1031e34ef046SKevin Wolf } 1032e34ef046SKevin Wolf 10332d246f01SKevin Wolf fail: 103433cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 1035cb3e7f08SMarc-André Lureau qobject_unref(bs_opts); 10362d246f01SKevin Wolf return dinfo; 103757975222SKevin Wolf } 103857975222SKevin Wolf 1039b6c1bae5SKevin Wolf static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) 1040b6c1bae5SKevin Wolf { 1041b6c1bae5SKevin Wolf BlockDriverState *bs; 1042b6c1bae5SKevin Wolf 1043b6c1bae5SKevin Wolf bs = bdrv_lookup_bs(name, name, errp); 1044b6c1bae5SKevin Wolf if (bs == NULL) { 1045b6c1bae5SKevin Wolf return NULL; 1046b6c1bae5SKevin Wolf } 1047b6c1bae5SKevin Wolf 1048b6c1bae5SKevin Wolf if (!bdrv_is_root_node(bs)) { 1049b6c1bae5SKevin Wolf error_setg(errp, "Need a root block node"); 1050b6c1bae5SKevin Wolf return NULL; 1051b6c1bae5SKevin Wolf } 1052b6c1bae5SKevin Wolf 1053b6c1bae5SKevin Wolf if (!bdrv_is_inserted(bs)) { 1054b6c1bae5SKevin Wolf error_setg(errp, "Device has no medium"); 1055b6c1bae5SKevin Wolf return NULL; 1056b6c1bae5SKevin Wolf } 1057b6c1bae5SKevin Wolf 1058b6c1bae5SKevin Wolf return bs; 1059b6c1bae5SKevin Wolf } 1060b6c1bae5SKevin Wolf 106110f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 10626cc2a415SPaolo Bonzini { 1063c8a83e85SKevin Wolf TransactionActionList list; 10646cc2a415SPaolo Bonzini 106510f75907SEric Blake list.value = action; 10666cc2a415SPaolo Bonzini list.next = NULL; 106794d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 10686cc2a415SPaolo Bonzini } 10696cc2a415SPaolo Bonzini 10700901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 10710901f67eSBenoît Canet bool has_node_name, const char *node_name, 10720901f67eSBenoît Canet const char *snapshot_file, 10730901f67eSBenoît Canet bool has_snapshot_node_name, 10740901f67eSBenoît Canet const char *snapshot_node_name, 10756106e249SLuiz Capitulino bool has_format, const char *format, 10760901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1077f8882568SJes Sorensen { 1078a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 10790901f67eSBenoît Canet .has_device = has_device, 10806cc2a415SPaolo Bonzini .device = (char *) device, 10810901f67eSBenoît Canet .has_node_name = has_node_name, 10820901f67eSBenoît Canet .node_name = (char *) node_name, 10836cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 10840901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 10850901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 10866cc2a415SPaolo Bonzini .has_format = has_format, 10876cc2a415SPaolo Bonzini .format = (char *) format, 10886cc2a415SPaolo Bonzini .has_mode = has_mode, 10896cc2a415SPaolo Bonzini .mode = mode, 10906cc2a415SPaolo Bonzini }; 109110f75907SEric Blake TransactionAction action = { 109210f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 109332bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 109410f75907SEric Blake }; 109510f75907SEric Blake blockdev_do_action(&action, errp); 1096f8882568SJes Sorensen } 1097f8882568SJes Sorensen 109843de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 109943de7e2dSAlberto Garcia Error **errp) 110043de7e2dSAlberto Garcia { 110143de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 110243de7e2dSAlberto Garcia .node = (char *) node, 110343de7e2dSAlberto Garcia .overlay = (char *) overlay 110443de7e2dSAlberto Garcia }; 110510f75907SEric Blake TransactionAction action = { 110610f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 110732bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 110810f75907SEric Blake }; 110910f75907SEric Blake blockdev_do_action(&action, errp); 111043de7e2dSAlberto Garcia } 111143de7e2dSAlberto Garcia 1112f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1113f323bc9eSWenchao Xia const char *name, 1114f323bc9eSWenchao Xia Error **errp) 1115f323bc9eSWenchao Xia { 1116f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1117f323bc9eSWenchao Xia .device = (char *) device, 1118f323bc9eSWenchao Xia .name = (char *) name 1119f323bc9eSWenchao Xia }; 112010f75907SEric Blake TransactionAction action = { 112110f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 112232bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 112310f75907SEric Blake }; 112410f75907SEric Blake blockdev_do_action(&action, errp); 1125f323bc9eSWenchao Xia } 1126f323bc9eSWenchao Xia 112744e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 112844e3e053SWenchao Xia bool has_id, 112944e3e053SWenchao Xia const char *id, 113044e3e053SWenchao Xia bool has_name, 113144e3e053SWenchao Xia const char *name, 113244e3e053SWenchao Xia Error **errp) 113344e3e053SWenchao Xia { 1134a0e8544cSFam Zheng BlockDriverState *bs; 11354ef3982aSStefan Hajnoczi AioContext *aio_context; 113644e3e053SWenchao Xia QEMUSnapshotInfo sn; 113744e3e053SWenchao Xia Error *local_err = NULL; 113844e3e053SWenchao Xia SnapshotInfo *info = NULL; 113944e3e053SWenchao Xia int ret; 114044e3e053SWenchao Xia 11412dfb4c03SKevin Wolf bs = qmp_get_root_bs(device, errp); 11422dfb4c03SKevin Wolf if (!bs) { 114344e3e053SWenchao Xia return NULL; 114444e3e053SWenchao Xia } 11452dfb4c03SKevin Wolf aio_context = bdrv_get_aio_context(bs); 11465433c24fSMax Reitz aio_context_acquire(aio_context); 114744e3e053SWenchao Xia 114844e3e053SWenchao Xia if (!has_id) { 114944e3e053SWenchao Xia id = NULL; 115044e3e053SWenchao Xia } 115144e3e053SWenchao Xia 115244e3e053SWenchao Xia if (!has_name) { 115344e3e053SWenchao Xia name = NULL; 115444e3e053SWenchao Xia } 115544e3e053SWenchao Xia 115644e3e053SWenchao Xia if (!id && !name) { 115744e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 11585433c24fSMax Reitz goto out_aio_context; 115944e3e053SWenchao Xia } 116044e3e053SWenchao Xia 11610b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 11620b928854SStefan Hajnoczi goto out_aio_context; 11630b928854SStefan Hajnoczi } 11640b928854SStefan Hajnoczi 116544e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 116684d18f06SMarkus Armbruster if (local_err) { 116744e3e053SWenchao Xia error_propagate(errp, local_err); 11684ef3982aSStefan Hajnoczi goto out_aio_context; 116944e3e053SWenchao Xia } 117044e3e053SWenchao Xia if (!ret) { 117144e3e053SWenchao Xia error_setg(errp, 117244e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 117344e3e053SWenchao Xia "device '%s'", 117444e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 11754ef3982aSStefan Hajnoczi goto out_aio_context; 117644e3e053SWenchao Xia } 117744e3e053SWenchao Xia 117844e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 117984d18f06SMarkus Armbruster if (local_err) { 118044e3e053SWenchao Xia error_propagate(errp, local_err); 11814ef3982aSStefan Hajnoczi goto out_aio_context; 118244e3e053SWenchao Xia } 118344e3e053SWenchao Xia 11844ef3982aSStefan Hajnoczi aio_context_release(aio_context); 11854ef3982aSStefan Hajnoczi 11865839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 118744e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 118844e3e053SWenchao Xia info->name = g_strdup(sn.name); 118944e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 119044e3e053SWenchao Xia info->date_sec = sn.date_sec; 119144e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 119244e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 119344e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 1194b39847a5SPavel Dovgalyuk if (sn.icount != -1ULL) { 1195b39847a5SPavel Dovgalyuk info->icount = sn.icount; 1196b39847a5SPavel Dovgalyuk info->has_icount = true; 1197b39847a5SPavel Dovgalyuk } 119844e3e053SWenchao Xia 119944e3e053SWenchao Xia return info; 12004ef3982aSStefan Hajnoczi 12014ef3982aSStefan Hajnoczi out_aio_context: 12024ef3982aSStefan Hajnoczi aio_context_release(aio_context); 12034ef3982aSStefan Hajnoczi return NULL; 120444e3e053SWenchao Xia } 12058802d1fdSJeff Cody 1206b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1207ba0c86a3SWenchao Xia 120850f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1209ba0c86a3SWenchao Xia 121050f43f0fSJohn Snow /** 121150f43f0fSJohn Snow * BlkActionOps: 121250f43f0fSJohn Snow * Table of operations that define an Action. 121350f43f0fSJohn Snow * 121450f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 121550f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 121650f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 121750f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 121850f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 121950f43f0fSJohn Snow * commit() or abort(). Can be NULL. 122050f43f0fSJohn Snow * 122150f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 122250f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1223ba0c86a3SWenchao Xia */ 122450f43f0fSJohn Snow typedef struct BlkActionOps { 122550f43f0fSJohn Snow size_t instance_size; 122650f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 122750f43f0fSJohn Snow void (*commit)(BlkActionState *common); 122850f43f0fSJohn Snow void (*abort)(BlkActionState *common); 122950f43f0fSJohn Snow void (*clean)(BlkActionState *common); 123050f43f0fSJohn Snow } BlkActionOps; 123150f43f0fSJohn Snow 123250f43f0fSJohn Snow /** 123350f43f0fSJohn Snow * BlkActionState: 123450f43f0fSJohn Snow * Describes one Action's state within a Transaction. 123550f43f0fSJohn Snow * 123650f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 123750f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 123894d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 123950f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 124050f43f0fSJohn Snow * 124150f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 124250f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 124350f43f0fSJohn Snow * base class. 124450f43f0fSJohn Snow */ 124550f43f0fSJohn Snow struct BlkActionState { 1246c8a83e85SKevin Wolf TransactionAction *action; 124750f43f0fSJohn Snow const BlkActionOps *ops; 124862c9e416SKevin Wolf JobTxn *block_job_txn; 124994d16a64SJohn Snow TransactionProperties *txn_props; 1250f4de0f8cSJohn Snow QTAILQ_ENTRY(BlkActionState) entry; 1251ba0c86a3SWenchao Xia }; 1252ba0c86a3SWenchao Xia 1253bbe86010SWenchao Xia /* internal snapshot private data */ 1254bbe86010SWenchao Xia typedef struct InternalSnapshotState { 125550f43f0fSJohn Snow BlkActionState common; 1256bbe86010SWenchao Xia BlockDriverState *bs; 1257bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1258507306ccSFam Zheng bool created; 1259bbe86010SWenchao Xia } InternalSnapshotState; 1260bbe86010SWenchao Xia 126194d16a64SJohn Snow 126294d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 126394d16a64SJohn Snow { 126494d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 126594d16a64SJohn Snow error_setg(errp, 126694d16a64SJohn Snow "Action '%s' does not support Transaction property " 126794d16a64SJohn Snow "completion-mode = %s", 1268977c736fSMarkus Armbruster TransactionActionKind_str(s->action->type), 1269977c736fSMarkus Armbruster ActionCompletionMode_str(s->txn_props->completion_mode)); 127094d16a64SJohn Snow return -1; 127194d16a64SJohn Snow } 127294d16a64SJohn Snow return 0; 127394d16a64SJohn Snow } 127494d16a64SJohn Snow 127550f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1276bbe86010SWenchao Xia Error **errp) 1277bbe86010SWenchao Xia { 1278f70edf99SMarkus Armbruster Error *local_err = NULL; 1279bbe86010SWenchao Xia const char *device; 1280bbe86010SWenchao Xia const char *name; 1281bbe86010SWenchao Xia BlockDriverState *bs; 1282bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1283bbe86010SWenchao Xia bool ret; 1284bbe86010SWenchao Xia qemu_timeval tv; 1285bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1286bbe86010SWenchao Xia InternalSnapshotState *state; 1287a36e458cSStefan Hajnoczi AioContext *aio_context; 1288bbe86010SWenchao Xia int ret1; 1289bbe86010SWenchao Xia 12906a8f9661SEric Blake g_assert(common->action->type == 1291bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 129232bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1293bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1294bbe86010SWenchao Xia 1295bbe86010SWenchao Xia /* 1. parse input */ 1296bbe86010SWenchao Xia device = internal->device; 1297bbe86010SWenchao Xia name = internal->name; 1298bbe86010SWenchao Xia 1299bbe86010SWenchao Xia /* 2. check for validation */ 130094d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 130194d16a64SJohn Snow return; 130294d16a64SJohn Snow } 130394d16a64SJohn Snow 130475dfd402SKevin Wolf bs = qmp_get_root_bs(device, errp); 130575dfd402SKevin Wolf if (!bs) { 1306bbe86010SWenchao Xia return; 1307bbe86010SWenchao Xia } 1308bbe86010SWenchao Xia 1309a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 1310a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 13115d6e96efSStefan Hajnoczi 1312507306ccSFam Zheng state->bs = bs; 1313a36e458cSStefan Hajnoczi 1314a36e458cSStefan Hajnoczi /* Paired with .clean() */ 1315507306ccSFam Zheng bdrv_drained_begin(bs); 1316507306ccSFam Zheng 13173dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 1318a36e458cSStefan Hajnoczi goto out; 13193dc7ca3cSStefan Hajnoczi } 13203dc7ca3cSStefan Hajnoczi 1321bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 132281e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1323a36e458cSStefan Hajnoczi goto out; 1324bbe86010SWenchao Xia } 1325bbe86010SWenchao Xia 1326bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 132781e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 132881e5f78aSAlberto Garcia "does not support internal snapshots", 132981e5f78aSAlberto Garcia bs->drv->format_name, device); 1330a36e458cSStefan Hajnoczi goto out; 1331bbe86010SWenchao Xia } 1332bbe86010SWenchao Xia 1333bbe86010SWenchao Xia if (!strlen(name)) { 1334bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1335a36e458cSStefan Hajnoczi goto out; 1336bbe86010SWenchao Xia } 1337bbe86010SWenchao Xia 1338bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1339f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1340f70edf99SMarkus Armbruster &local_err); 1341f70edf99SMarkus Armbruster if (local_err) { 1342f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1343a36e458cSStefan Hajnoczi goto out; 1344bbe86010SWenchao Xia } else if (ret) { 1345bbe86010SWenchao Xia error_setg(errp, 1346bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1347bbe86010SWenchao Xia name, device); 1348a36e458cSStefan Hajnoczi goto out; 1349bbe86010SWenchao Xia } 1350bbe86010SWenchao Xia 1351bbe86010SWenchao Xia /* 3. take the snapshot */ 1352bbe86010SWenchao Xia sn = &state->sn; 1353bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1354bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1355bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1356bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1357bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1358b39847a5SPavel Dovgalyuk if (replay_mode != REPLAY_MODE_NONE) { 1359b39847a5SPavel Dovgalyuk sn->icount = replay_get_current_icount(); 1360b39847a5SPavel Dovgalyuk } else { 1361b39847a5SPavel Dovgalyuk sn->icount = -1ULL; 1362b39847a5SPavel Dovgalyuk } 1363bbe86010SWenchao Xia 1364bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1365bbe86010SWenchao Xia if (ret1 < 0) { 1366bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1367bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1368bbe86010SWenchao Xia name, device); 1369a36e458cSStefan Hajnoczi goto out; 1370bbe86010SWenchao Xia } 1371bbe86010SWenchao Xia 1372bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1373507306ccSFam Zheng state->created = true; 1374a36e458cSStefan Hajnoczi 1375a36e458cSStefan Hajnoczi out: 1376a36e458cSStefan Hajnoczi aio_context_release(aio_context); 1377bbe86010SWenchao Xia } 1378bbe86010SWenchao Xia 137950f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1380bbe86010SWenchao Xia { 1381bbe86010SWenchao Xia InternalSnapshotState *state = 1382bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1383bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1384bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1385a36e458cSStefan Hajnoczi AioContext *aio_context; 1386bbe86010SWenchao Xia Error *local_error = NULL; 1387bbe86010SWenchao Xia 1388507306ccSFam Zheng if (!state->created) { 1389bbe86010SWenchao Xia return; 1390bbe86010SWenchao Xia } 1391bbe86010SWenchao Xia 1392a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1393a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 1394a36e458cSStefan Hajnoczi 1395bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1396c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1397c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1398c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1399c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1400c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1401bbe86010SWenchao Xia } 1402a36e458cSStefan Hajnoczi 1403a36e458cSStefan Hajnoczi aio_context_release(aio_context); 1404bbe86010SWenchao Xia } 1405bbe86010SWenchao Xia 140650f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 14075d6e96efSStefan Hajnoczi { 14085d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 14095d6e96efSStefan Hajnoczi common, common); 1410a36e458cSStefan Hajnoczi AioContext *aio_context; 14115d6e96efSStefan Hajnoczi 1412a36e458cSStefan Hajnoczi if (!state->bs) { 1413a36e458cSStefan Hajnoczi return; 1414a36e458cSStefan Hajnoczi } 1415a36e458cSStefan Hajnoczi 1416a36e458cSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1417a36e458cSStefan Hajnoczi aio_context_acquire(aio_context); 1418a36e458cSStefan Hajnoczi 1419507306ccSFam Zheng bdrv_drained_end(state->bs); 1420a36e458cSStefan Hajnoczi 1421a36e458cSStefan Hajnoczi aio_context_release(aio_context); 14225d6e96efSStefan Hajnoczi } 14235d6e96efSStefan Hajnoczi 1424ba0c86a3SWenchao Xia /* external snapshot private data */ 1425ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 142650f43f0fSJohn Snow BlkActionState common; 14278802d1fdSJeff Cody BlockDriverState *old_bs; 14288802d1fdSJeff Cody BlockDriverState *new_bs; 1429067acf28SKevin Wolf bool overlay_appended; 1430ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 14318802d1fdSJeff Cody 143250f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 14339b9877eeSWenchao Xia Error **errp) 14349b9877eeSWenchao Xia { 14355b363937SMax Reitz int flags = 0; 143643de7e2dSAlberto Garcia QDict *options = NULL; 14379b9877eeSWenchao Xia Error *local_err = NULL; 143843de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1439e2a31e87SWenchao Xia const char *device; 14400901f67eSBenoît Canet const char *node_name; 144143de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 144243de7e2dSAlberto Garcia const char *snapshot_ref; 144343de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1444e2a31e87SWenchao Xia const char *new_image_file; 1445ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1446ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1447c8a83e85SKevin Wolf TransactionAction *action = common->action; 14482d24b60bSStefan Hajnoczi AioContext *aio_context; 1449d29d3d1fSKevin Wolf uint64_t perm, shared; 14509b9877eeSWenchao Xia 145143de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 145243de7e2dSAlberto Garcia * purpose but a different set of parameters */ 145343de7e2dSAlberto Garcia switch (action->type) { 145443de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 145543de7e2dSAlberto Garcia { 145632bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 145743de7e2dSAlberto Garcia device = s->node; 145843de7e2dSAlberto Garcia node_name = s->node; 145943de7e2dSAlberto Garcia new_image_file = NULL; 146043de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1461e2a31e87SWenchao Xia } 146243de7e2dSAlberto Garcia break; 146343de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 146443de7e2dSAlberto Garcia { 146532bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 146643de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 146743de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 146843de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 146943de7e2dSAlberto Garcia snapshot_ref = NULL; 147043de7e2dSAlberto Garcia } 147143de7e2dSAlberto Garcia break; 147243de7e2dSAlberto Garcia default: 147343de7e2dSAlberto Garcia g_assert_not_reached(); 1474e2a31e87SWenchao Xia } 1475e2a31e87SWenchao Xia 1476e2a31e87SWenchao Xia /* start processing */ 147794d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 147894d16a64SJohn Snow return; 147994d16a64SJohn Snow } 148094d16a64SJohn Snow 148143de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 148243de7e2dSAlberto Garcia if (!state->old_bs) { 14839b9877eeSWenchao Xia return; 14849b9877eeSWenchao Xia } 14859b9877eeSWenchao Xia 14862d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 14872d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 14882d24b60bSStefan Hajnoczi 14892d24b60bSStefan Hajnoczi /* Paired with .clean() */ 1490da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 14915d6e96efSStefan Hajnoczi 1492ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1493c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 14942d24b60bSStefan Hajnoczi goto out; 14959b9877eeSWenchao Xia } 14969b9877eeSWenchao Xia 14973718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 14983718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 14992d24b60bSStefan Hajnoczi goto out; 15009b9877eeSWenchao Xia } 15019b9877eeSWenchao Xia 1502ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1503ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1504c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 15052d24b60bSStefan Hajnoczi goto out; 15069b9877eeSWenchao Xia } 15079b9877eeSWenchao Xia } 15089b9877eeSWenchao Xia 150943de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 151032bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 151143de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 151243de7e2dSAlberto Garcia enum NewImageMode mode; 151343de7e2dSAlberto Garcia const char *snapshot_node_name = 151443de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 151543de7e2dSAlberto Garcia 151643de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 1517a2bb6f8cSMax Reitz error_setg(errp, "New overlay node name missing"); 15182d24b60bSStefan Hajnoczi goto out; 151943de7e2dSAlberto Garcia } 152043de7e2dSAlberto Garcia 152143de7e2dSAlberto Garcia if (snapshot_node_name && 152243de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 1523a2bb6f8cSMax Reitz error_setg(errp, "New overlay node name already in use"); 15242d24b60bSStefan Hajnoczi goto out; 152543de7e2dSAlberto Garcia } 152643de7e2dSAlberto Garcia 1527ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 15282a32c6e8SJohn Snow flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ); 15292a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING; 15309b9877eeSWenchao Xia 15319b9877eeSWenchao Xia /* create new image w/backing file */ 153243de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 15339b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1534f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1535f86b8b58SKevin Wolf if (size < 0) { 1536f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 15372d24b60bSStefan Hajnoczi goto out; 1538f86b8b58SKevin Wolf } 1539f30c66baSMax Reitz bdrv_refresh_filename(state->old_bs); 15409b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1541ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1542ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 15439217283dSFam Zheng NULL, size, flags, false, &local_err); 154484d18f06SMarkus Armbruster if (local_err) { 15459b9877eeSWenchao Xia error_propagate(errp, local_err); 15462d24b60bSStefan Hajnoczi goto out; 15479b9877eeSWenchao Xia } 15489b9877eeSWenchao Xia } 15499b9877eeSWenchao Xia 15500901f67eSBenoît Canet options = qdict_new(); 1551d52e1a0eSPeter Maydell if (snapshot_node_name) { 155246f5ac20SEric Blake qdict_put_str(options, "node-name", snapshot_node_name); 15530901f67eSBenoît Canet } 155446f5ac20SEric Blake qdict_put_str(options, "driver", format); 155543de7e2dSAlberto Garcia } 155643de7e2dSAlberto Garcia 15575b363937SMax Reitz state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, 15585b363937SMax Reitz errp); 1559f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 15605b363937SMax Reitz if (!state->new_bs) { 15612d24b60bSStefan Hajnoczi goto out; 156243de7e2dSAlberto Garcia } 156343de7e2dSAlberto Garcia 1564d29d3d1fSKevin Wolf /* 1565d29d3d1fSKevin Wolf * Allow attaching a backing file to an overlay that's already in use only 1566d29d3d1fSKevin Wolf * if the parents don't assume that they are already seeing a valid image. 1567d29d3d1fSKevin Wolf * (Specifically, allow it as a mirror target, which is write-only access.) 1568d29d3d1fSKevin Wolf */ 1569d29d3d1fSKevin Wolf bdrv_get_cumulative_perm(state->new_bs, &perm, &shared); 1570d29d3d1fSKevin Wolf if (perm & BLK_PERM_CONSISTENT_READ) { 1571a2bb6f8cSMax Reitz error_setg(errp, "The overlay is already in use"); 15722d24b60bSStefan Hajnoczi goto out; 157343de7e2dSAlberto Garcia } 157443de7e2dSAlberto Garcia 15757cc734a9SMax Reitz if (state->new_bs->drv->is_filter) { 15767cc734a9SMax Reitz error_setg(errp, "Filters cannot be used as overlays"); 15777cc734a9SMax Reitz goto out; 15787cc734a9SMax Reitz } 15797cc734a9SMax Reitz 15807cc734a9SMax Reitz if (bdrv_cow_child(state->new_bs)) { 1581a2bb6f8cSMax Reitz error_setg(errp, "The overlay already has a backing image"); 15822d24b60bSStefan Hajnoczi goto out; 158308b24cfeSAlberto Garcia } 158408b24cfeSAlberto Garcia 158508b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 1586a2bb6f8cSMax Reitz error_setg(errp, "The overlay does not support backing images"); 15872d24b60bSStefan Hajnoczi goto out; 1588b2c2832cSKevin Wolf } 1589b2c2832cSKevin Wolf 1590b2c2832cSKevin Wolf /* This removes our old bs and adds the new bs. This is an operation that 1591b2c2832cSKevin Wolf * can fail, so we need to do it in .prepare; undoing it for abort is 1592b2c2832cSKevin Wolf * always possible. */ 1593b2c2832cSKevin Wolf bdrv_ref(state->new_bs); 1594b2c2832cSKevin Wolf bdrv_append(state->new_bs, state->old_bs, &local_err); 1595b2c2832cSKevin Wolf if (local_err) { 1596b2c2832cSKevin Wolf error_propagate(errp, local_err); 15972d24b60bSStefan Hajnoczi goto out; 15989b9877eeSWenchao Xia } 1599067acf28SKevin Wolf state->overlay_appended = true; 16002d24b60bSStefan Hajnoczi 16012d24b60bSStefan Hajnoczi out: 16022d24b60bSStefan Hajnoczi aio_context_release(aio_context); 16039b9877eeSWenchao Xia } 16049b9877eeSWenchao Xia 160550f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 16063b0047e8SWenchao Xia { 1607ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1608ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 16092d24b60bSStefan Hajnoczi AioContext *aio_context; 16102d24b60bSStefan Hajnoczi 16112d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 16122d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 1613ba0c86a3SWenchao Xia 16143b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 16153b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 16163b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 1617d73415a3SStefan Hajnoczi if (!qatomic_read(&state->old_bs->copy_on_read)) { 16181b57774fSAlberto Garcia bdrv_reopen_set_read_only(state->old_bs, true, NULL); 16193b0047e8SWenchao Xia } 16202d24b60bSStefan Hajnoczi 16212d24b60bSStefan Hajnoczi aio_context_release(aio_context); 16224c844983SKevin Wolf } 16233b0047e8SWenchao Xia 162450f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 162596b86bf7SWenchao Xia { 1626ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1627ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1628ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 1629067acf28SKevin Wolf if (state->overlay_appended) { 16302d24b60bSStefan Hajnoczi AioContext *aio_context; 1631377410f6SSergio Lopez AioContext *tmp_context; 1632377410f6SSergio Lopez int ret; 16332d24b60bSStefan Hajnoczi 16342d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 16352d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 16362d24b60bSStefan Hajnoczi 1637719fc28cSJeff Cody bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd() 1638719fc28cSJeff Cody close state->old_bs; we need it */ 1639719fc28cSJeff Cody bdrv_set_backing_hd(state->new_bs, NULL, &error_abort); 1640377410f6SSergio Lopez 1641377410f6SSergio Lopez /* 1642377410f6SSergio Lopez * The call to bdrv_set_backing_hd() above returns state->old_bs to 1643377410f6SSergio Lopez * the main AioContext. As we're still going to be using it, return 1644377410f6SSergio Lopez * it to the AioContext it was before. 1645377410f6SSergio Lopez */ 1646377410f6SSergio Lopez tmp_context = bdrv_get_aio_context(state->old_bs); 1647377410f6SSergio Lopez if (aio_context != tmp_context) { 1648377410f6SSergio Lopez aio_context_release(aio_context); 1649377410f6SSergio Lopez aio_context_acquire(tmp_context); 1650377410f6SSergio Lopez 1651377410f6SSergio Lopez ret = bdrv_try_set_aio_context(state->old_bs, 1652377410f6SSergio Lopez aio_context, NULL); 1653377410f6SSergio Lopez assert(ret == 0); 1654377410f6SSergio Lopez 1655377410f6SSergio Lopez aio_context_release(tmp_context); 1656377410f6SSergio Lopez aio_context_acquire(aio_context); 1657377410f6SSergio Lopez } 1658377410f6SSergio Lopez 16595fe31c25SKevin Wolf bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); 1660719fc28cSJeff Cody bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */ 16612d24b60bSStefan Hajnoczi 16622d24b60bSStefan Hajnoczi aio_context_release(aio_context); 1663b2c2832cSKevin Wolf } 166496b86bf7SWenchao Xia } 1665da763e83SFam Zheng } 1666da763e83SFam Zheng 166750f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1668da763e83SFam Zheng { 1669da763e83SFam Zheng ExternalSnapshotState *state = 1670da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 16712d24b60bSStefan Hajnoczi AioContext *aio_context; 16722d24b60bSStefan Hajnoczi 16732d24b60bSStefan Hajnoczi if (!state->old_bs) { 16742d24b60bSStefan Hajnoczi return; 16752d24b60bSStefan Hajnoczi } 16762d24b60bSStefan Hajnoczi 16772d24b60bSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->old_bs); 16782d24b60bSStefan Hajnoczi aio_context_acquire(aio_context); 16792d24b60bSStefan Hajnoczi 1680da763e83SFam Zheng bdrv_drained_end(state->old_bs); 1681b2c2832cSKevin Wolf bdrv_unref(state->new_bs); 16822d24b60bSStefan Hajnoczi 16832d24b60bSStefan Hajnoczi aio_context_release(aio_context); 168496b86bf7SWenchao Xia } 168596b86bf7SWenchao Xia 16863037f364SStefan Hajnoczi typedef struct DriveBackupState { 168750f43f0fSJohn Snow BlkActionState common; 16883037f364SStefan Hajnoczi BlockDriverState *bs; 16893037f364SStefan Hajnoczi BlockJob *job; 16903037f364SStefan Hajnoczi } DriveBackupState; 16913037f364SStefan Hajnoczi 16922288ccfaSSergio Lopez static BlockJob *do_backup_common(BackupCommon *backup, 16932288ccfaSSergio Lopez BlockDriverState *bs, 16942288ccfaSSergio Lopez BlockDriverState *target_bs, 16952288ccfaSSergio Lopez AioContext *aio_context, 16962288ccfaSSergio Lopez JobTxn *txn, Error **errp); 169778f51fdeSJohn Snow 169850f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 16993037f364SStefan Hajnoczi { 17003037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 17013037f364SStefan Hajnoczi DriveBackup *backup; 17022288ccfaSSergio Lopez BlockDriverState *bs; 17032288ccfaSSergio Lopez BlockDriverState *target_bs; 17042288ccfaSSergio Lopez BlockDriverState *source = NULL; 170566d56054SStefan Hajnoczi AioContext *aio_context; 17063ea67e08SSergio Lopez AioContext *old_context; 17072288ccfaSSergio Lopez QDict *options; 17083037f364SStefan Hajnoczi Error *local_err = NULL; 17092288ccfaSSergio Lopez int flags; 17102288ccfaSSergio Lopez int64_t size; 17112288ccfaSSergio Lopez bool set_backing_hd = false; 17123ea67e08SSergio Lopez int ret; 17133037f364SStefan Hajnoczi 17146a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 171532bafa8fSEric Blake backup = common->action->u.drive_backup.data; 17163037f364SStefan Hajnoczi 17172288ccfaSSergio Lopez if (!backup->has_mode) { 17182288ccfaSSergio Lopez backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17192288ccfaSSergio Lopez } 17202288ccfaSSergio Lopez 172185c9d133SVladimir Sementsov-Ogievskiy bs = bdrv_lookup_bs(backup->device, backup->device, errp); 1722b7e4fa22SKevin Wolf if (!bs) { 17231fdd4b7bSFam Zheng return; 17241fdd4b7bSFam Zheng } 17251fdd4b7bSFam Zheng 17262288ccfaSSergio Lopez if (!bs->drv) { 17272288ccfaSSergio Lopez error_setg(errp, "Device has no medium"); 17282288ccfaSSergio Lopez return; 17292288ccfaSSergio Lopez } 17302288ccfaSSergio Lopez 173166d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 173266d56054SStefan Hajnoczi aio_context_acquire(aio_context); 173366d56054SStefan Hajnoczi 173466d56054SStefan Hajnoczi /* Paired with .clean() */ 1735b7e4fa22SKevin Wolf bdrv_drained_begin(bs); 173666d56054SStefan Hajnoczi 17372288ccfaSSergio Lopez if (!backup->has_format) { 17382288ccfaSSergio Lopez backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? 17392288ccfaSSergio Lopez NULL : (char *) bs->drv->format_name; 17402288ccfaSSergio Lopez } 17415d6e96efSStefan Hajnoczi 17422288ccfaSSergio Lopez /* Early check to avoid creating target */ 17432288ccfaSSergio Lopez if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 17442288ccfaSSergio Lopez goto out; 17452288ccfaSSergio Lopez } 17462288ccfaSSergio Lopez 17472288ccfaSSergio Lopez flags = bs->open_flags | BDRV_O_RDWR; 17482288ccfaSSergio Lopez 17492288ccfaSSergio Lopez /* 17502288ccfaSSergio Lopez * See if we have a backing HD we can use to create our new image 17512288ccfaSSergio Lopez * on top of. 17522288ccfaSSergio Lopez */ 17532288ccfaSSergio Lopez if (backup->sync == MIRROR_SYNC_MODE_TOP) { 17542b088c60SMax Reitz /* 17552b088c60SMax Reitz * Backup will not replace the source by the target, so none 17562b088c60SMax Reitz * of the filters skipped here will be removed (in contrast to 17572b088c60SMax Reitz * mirror). Therefore, we can skip all of them when looking 17582b088c60SMax Reitz * for the first COW relationship. 17592b088c60SMax Reitz */ 17602b088c60SMax Reitz source = bdrv_cow_bs(bdrv_skip_filters(bs)); 17612288ccfaSSergio Lopez if (!source) { 17622288ccfaSSergio Lopez backup->sync = MIRROR_SYNC_MODE_FULL; 17632288ccfaSSergio Lopez } 17642288ccfaSSergio Lopez } 17652288ccfaSSergio Lopez if (backup->sync == MIRROR_SYNC_MODE_NONE) { 17662288ccfaSSergio Lopez source = bs; 17672288ccfaSSergio Lopez flags |= BDRV_O_NO_BACKING; 17682288ccfaSSergio Lopez set_backing_hd = true; 17692288ccfaSSergio Lopez } 17702288ccfaSSergio Lopez 17712288ccfaSSergio Lopez size = bdrv_getlength(bs); 17722288ccfaSSergio Lopez if (size < 0) { 17732288ccfaSSergio Lopez error_setg_errno(errp, -size, "bdrv_getlength failed"); 17742288ccfaSSergio Lopez goto out; 17752288ccfaSSergio Lopez } 17762288ccfaSSergio Lopez 17772288ccfaSSergio Lopez if (backup->mode != NEW_IMAGE_MODE_EXISTING) { 17782288ccfaSSergio Lopez assert(backup->format); 17792288ccfaSSergio Lopez if (source) { 17802b088c60SMax Reitz /* Implicit filters should not appear in the filename */ 17812b088c60SMax Reitz BlockDriverState *explicit_backing = 17822b088c60SMax Reitz bdrv_skip_implicit_filters(source); 17832b088c60SMax Reitz 17842b088c60SMax Reitz bdrv_refresh_filename(explicit_backing); 17852b088c60SMax Reitz bdrv_img_create(backup->target, backup->format, 17862b088c60SMax Reitz explicit_backing->filename, 17872b088c60SMax Reitz explicit_backing->drv->format_name, NULL, 17882288ccfaSSergio Lopez size, flags, false, &local_err); 17892288ccfaSSergio Lopez } else { 17902288ccfaSSergio Lopez bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, 17912288ccfaSSergio Lopez size, flags, false, &local_err); 17922288ccfaSSergio Lopez } 17932288ccfaSSergio Lopez } 17942288ccfaSSergio Lopez 179584d18f06SMarkus Armbruster if (local_err) { 17963037f364SStefan Hajnoczi error_propagate(errp, local_err); 179766d56054SStefan Hajnoczi goto out; 17983037f364SStefan Hajnoczi } 179966d56054SStefan Hajnoczi 18002288ccfaSSergio Lopez options = qdict_new(); 18012288ccfaSSergio Lopez qdict_put_str(options, "discard", "unmap"); 18022288ccfaSSergio Lopez qdict_put_str(options, "detect-zeroes", "unmap"); 18032288ccfaSSergio Lopez if (backup->format) { 18042288ccfaSSergio Lopez qdict_put_str(options, "driver", backup->format); 18052288ccfaSSergio Lopez } 18062288ccfaSSergio Lopez 18072288ccfaSSergio Lopez target_bs = bdrv_open(backup->target, NULL, options, flags, errp); 18082288ccfaSSergio Lopez if (!target_bs) { 18092288ccfaSSergio Lopez goto out; 18102288ccfaSSergio Lopez } 18112288ccfaSSergio Lopez 18123ea67e08SSergio Lopez /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ 18133ea67e08SSergio Lopez old_context = bdrv_get_aio_context(target_bs); 18143ea67e08SSergio Lopez aio_context_release(aio_context); 18153ea67e08SSergio Lopez aio_context_acquire(old_context); 18163ea67e08SSergio Lopez 18173ea67e08SSergio Lopez ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); 18183ea67e08SSergio Lopez if (ret < 0) { 18193ea67e08SSergio Lopez bdrv_unref(target_bs); 18203ea67e08SSergio Lopez aio_context_release(old_context); 18213ea67e08SSergio Lopez return; 18223ea67e08SSergio Lopez } 18233ea67e08SSergio Lopez 18243ea67e08SSergio Lopez aio_context_release(old_context); 18253ea67e08SSergio Lopez aio_context_acquire(aio_context); 18263ea67e08SSergio Lopez 18272288ccfaSSergio Lopez if (set_backing_hd) { 18282288ccfaSSergio Lopez bdrv_set_backing_hd(target_bs, source, &local_err); 18292288ccfaSSergio Lopez if (local_err) { 183094237657SPan Nengyuan error_propagate(errp, local_err); 18312288ccfaSSergio Lopez goto unref; 18322288ccfaSSergio Lopez } 18332288ccfaSSergio Lopez } 18342288ccfaSSergio Lopez 18352288ccfaSSergio Lopez state->bs = bs; 18362288ccfaSSergio Lopez 18372288ccfaSSergio Lopez state->job = do_backup_common(qapi_DriveBackup_base(backup), 18382288ccfaSSergio Lopez bs, target_bs, aio_context, 18392288ccfaSSergio Lopez common->block_job_txn, errp); 18402288ccfaSSergio Lopez 18412288ccfaSSergio Lopez unref: 18422288ccfaSSergio Lopez bdrv_unref(target_bs); 184366d56054SStefan Hajnoczi out: 184466d56054SStefan Hajnoczi aio_context_release(aio_context); 1845111049a4SJohn Snow } 18463037f364SStefan Hajnoczi 1847111049a4SJohn Snow static void drive_backup_commit(BlkActionState *common) 1848111049a4SJohn Snow { 1849111049a4SJohn Snow DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 185066d56054SStefan Hajnoczi AioContext *aio_context; 185166d56054SStefan Hajnoczi 185266d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 185366d56054SStefan Hajnoczi aio_context_acquire(aio_context); 185466d56054SStefan Hajnoczi 1855111049a4SJohn Snow assert(state->job); 1856da01ff7fSKevin Wolf job_start(&state->job->job); 185766d56054SStefan Hajnoczi 185866d56054SStefan Hajnoczi aio_context_release(aio_context); 18593037f364SStefan Hajnoczi } 18603037f364SStefan Hajnoczi 186150f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 18623037f364SStefan Hajnoczi { 18633037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18643037f364SStefan Hajnoczi 1865111049a4SJohn Snow if (state->job) { 186666d56054SStefan Hajnoczi AioContext *aio_context; 186766d56054SStefan Hajnoczi 186866d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 186966d56054SStefan Hajnoczi aio_context_acquire(aio_context); 187066d56054SStefan Hajnoczi 18713d70ff53SKevin Wolf job_cancel_sync(&state->job->job); 187266d56054SStefan Hajnoczi 187366d56054SStefan Hajnoczi aio_context_release(aio_context); 18743037f364SStefan Hajnoczi } 18753037f364SStefan Hajnoczi } 18763037f364SStefan Hajnoczi 187750f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 18785d6e96efSStefan Hajnoczi { 18795d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 188066d56054SStefan Hajnoczi AioContext *aio_context; 18815d6e96efSStefan Hajnoczi 188266d56054SStefan Hajnoczi if (!state->bs) { 188366d56054SStefan Hajnoczi return; 18845d6e96efSStefan Hajnoczi } 188566d56054SStefan Hajnoczi 188666d56054SStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 188766d56054SStefan Hajnoczi aio_context_acquire(aio_context); 188866d56054SStefan Hajnoczi 188966d56054SStefan Hajnoczi bdrv_drained_end(state->bs); 189066d56054SStefan Hajnoczi 189166d56054SStefan Hajnoczi aio_context_release(aio_context); 18925d6e96efSStefan Hajnoczi } 18935d6e96efSStefan Hajnoczi 1894bd8baecdSFam Zheng typedef struct BlockdevBackupState { 189550f43f0fSJohn Snow BlkActionState common; 1896bd8baecdSFam Zheng BlockDriverState *bs; 1897bd8baecdSFam Zheng BlockJob *job; 1898bd8baecdSFam Zheng } BlockdevBackupState; 1899bd8baecdSFam Zheng 190050f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1901bd8baecdSFam Zheng { 1902bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1903bd8baecdSFam Zheng BlockdevBackup *backup; 19045b7bfe51SSergio Lopez BlockDriverState *bs; 19055b7bfe51SSergio Lopez BlockDriverState *target_bs; 1906edd5adeeSStefan Hajnoczi AioContext *aio_context; 19073ea67e08SSergio Lopez AioContext *old_context; 19083ea67e08SSergio Lopez int ret; 1909bd8baecdSFam Zheng 19106a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 191132bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1912bd8baecdSFam Zheng 1913930fe17fSVladimir Sementsov-Ogievskiy bs = bdrv_lookup_bs(backup->device, backup->device, errp); 1914cef34eebSKevin Wolf if (!bs) { 1915ff52bf36SFam Zheng return; 1916ff52bf36SFam Zheng } 1917ff52bf36SFam Zheng 19185b7bfe51SSergio Lopez target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); 19195b7bfe51SSergio Lopez if (!target_bs) { 1920bd8baecdSFam Zheng return; 1921bd8baecdSFam Zheng } 1922bd8baecdSFam Zheng 19233ea67e08SSergio Lopez /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ 1924edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 19253ea67e08SSergio Lopez old_context = bdrv_get_aio_context(target_bs); 19263ea67e08SSergio Lopez aio_context_acquire(old_context); 19273ea67e08SSergio Lopez 19283ea67e08SSergio Lopez ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); 19293ea67e08SSergio Lopez if (ret < 0) { 19303ea67e08SSergio Lopez aio_context_release(old_context); 19313ea67e08SSergio Lopez return; 19323ea67e08SSergio Lopez } 19333ea67e08SSergio Lopez 19343ea67e08SSergio Lopez aio_context_release(old_context); 1935edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1936cef34eebSKevin Wolf state->bs = bs; 1937edd5adeeSStefan Hajnoczi 1938edd5adeeSStefan Hajnoczi /* Paired with .clean() */ 1939ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1940bd8baecdSFam Zheng 19415b7bfe51SSergio Lopez state->job = do_backup_common(qapi_BlockdevBackup_base(backup), 19425b7bfe51SSergio Lopez bs, target_bs, aio_context, 19435b7bfe51SSergio Lopez common->block_job_txn, errp); 1944edd5adeeSStefan Hajnoczi 1945edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 1946111049a4SJohn Snow } 1947bd8baecdSFam Zheng 1948111049a4SJohn Snow static void blockdev_backup_commit(BlkActionState *common) 1949111049a4SJohn Snow { 1950111049a4SJohn Snow BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1951edd5adeeSStefan Hajnoczi AioContext *aio_context; 1952edd5adeeSStefan Hajnoczi 1953edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1954edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1955edd5adeeSStefan Hajnoczi 1956111049a4SJohn Snow assert(state->job); 1957da01ff7fSKevin Wolf job_start(&state->job->job); 1958edd5adeeSStefan Hajnoczi 1959edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 1960bd8baecdSFam Zheng } 1961bd8baecdSFam Zheng 196250f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1963bd8baecdSFam Zheng { 1964bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1965bd8baecdSFam Zheng 1966111049a4SJohn Snow if (state->job) { 1967edd5adeeSStefan Hajnoczi AioContext *aio_context; 1968edd5adeeSStefan Hajnoczi 1969edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1970edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1971edd5adeeSStefan Hajnoczi 19723d70ff53SKevin Wolf job_cancel_sync(&state->job->job); 1973edd5adeeSStefan Hajnoczi 1974edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 1975bd8baecdSFam Zheng } 1976bd8baecdSFam Zheng } 1977bd8baecdSFam Zheng 197850f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 1979bd8baecdSFam Zheng { 1980bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1981edd5adeeSStefan Hajnoczi AioContext *aio_context; 1982bd8baecdSFam Zheng 1983edd5adeeSStefan Hajnoczi if (!state->bs) { 1984edd5adeeSStefan Hajnoczi return; 1985bd8baecdSFam Zheng } 1986edd5adeeSStefan Hajnoczi 1987edd5adeeSStefan Hajnoczi aio_context = bdrv_get_aio_context(state->bs); 1988edd5adeeSStefan Hajnoczi aio_context_acquire(aio_context); 1989edd5adeeSStefan Hajnoczi 1990edd5adeeSStefan Hajnoczi bdrv_drained_end(state->bs); 1991edd5adeeSStefan Hajnoczi 1992edd5adeeSStefan Hajnoczi aio_context_release(aio_context); 1993bd8baecdSFam Zheng } 1994bd8baecdSFam Zheng 1995df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 199650f43f0fSJohn Snow BlkActionState common; 1997df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 1998df9a681dSFam Zheng BlockDriverState *bs; 1999df9a681dSFam Zheng HBitmap *backup; 2000df9a681dSFam Zheng bool prepared; 2001c6490447SVladimir Sementsov-Ogievskiy bool was_enabled; 2002df9a681dSFam Zheng } BlockDirtyBitmapState; 2003df9a681dSFam Zheng 200450f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 2005df9a681dSFam Zheng Error **errp) 2006df9a681dSFam Zheng { 2007df9a681dSFam Zheng Error *local_err = NULL; 2008df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2009df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2010df9a681dSFam Zheng common, common); 2011df9a681dSFam Zheng 201294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 201394d16a64SJohn Snow return; 201494d16a64SJohn Snow } 201594d16a64SJohn Snow 201632bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2017df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2018df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2019df9a681dSFam Zheng action->has_granularity, action->granularity, 2020fd5ae4ccSVladimir Sementsov-Ogievskiy action->has_persistent, action->persistent, 20210e2b7f09SJohn Snow action->has_disabled, action->disabled, 2022df9a681dSFam Zheng &local_err); 2023df9a681dSFam Zheng 2024df9a681dSFam Zheng if (!local_err) { 2025df9a681dSFam Zheng state->prepared = true; 2026df9a681dSFam Zheng } else { 2027df9a681dSFam Zheng error_propagate(errp, local_err); 2028df9a681dSFam Zheng } 2029df9a681dSFam Zheng } 2030df9a681dSFam Zheng 203150f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2032df9a681dSFam Zheng { 2033df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2034df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2035df9a681dSFam Zheng common, common); 2036df9a681dSFam Zheng 203732bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2038df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2039df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2040df9a681dSFam Zheng */ 2041df9a681dSFam Zheng if (state->prepared) { 2042df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2043df9a681dSFam Zheng } 2044df9a681dSFam Zheng } 2045df9a681dSFam Zheng 204650f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2047df9a681dSFam Zheng Error **errp) 2048df9a681dSFam Zheng { 2049df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2050df9a681dSFam Zheng common, common); 2051df9a681dSFam Zheng BlockDirtyBitmap *action; 2052df9a681dSFam Zheng 205394d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 205494d16a64SJohn Snow return; 205594d16a64SJohn Snow } 205694d16a64SJohn Snow 205732bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2058df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2059df9a681dSFam Zheng action->name, 2060df9a681dSFam Zheng &state->bs, 2061df9a681dSFam Zheng errp); 2062df9a681dSFam Zheng if (!state->bitmap) { 2063df9a681dSFam Zheng return; 2064df9a681dSFam Zheng } 2065df9a681dSFam Zheng 20663ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) { 2067d6883bc9SVladimir Sementsov-Ogievskiy return; 2068df9a681dSFam Zheng } 2069df9a681dSFam Zheng 2070df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2071df9a681dSFam Zheng } 2072df9a681dSFam Zheng 20735c4cf8b2SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_restore(BlkActionState *common) 2074df9a681dSFam Zheng { 2075df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2076df9a681dSFam Zheng common, common); 2077df9a681dSFam Zheng 2078184dd9c4SJohn Snow if (state->backup) { 207956bd6624SVladimir Sementsov-Ogievskiy bdrv_restore_dirty_bitmap(state->bitmap, state->backup); 2080df9a681dSFam Zheng } 2081184dd9c4SJohn Snow } 2082df9a681dSFam Zheng 20835c4cf8b2SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_free_backup(BlkActionState *common) 2084df9a681dSFam Zheng { 2085df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2086df9a681dSFam Zheng common, common); 2087df9a681dSFam Zheng 2088df9a681dSFam Zheng hbitmap_free(state->backup); 2089df9a681dSFam Zheng } 2090df9a681dSFam Zheng 2091c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_prepare(BlkActionState *common, 2092c6490447SVladimir Sementsov-Ogievskiy Error **errp) 2093c6490447SVladimir Sementsov-Ogievskiy { 2094c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmap *action; 2095c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2096c6490447SVladimir Sementsov-Ogievskiy common, common); 2097c6490447SVladimir Sementsov-Ogievskiy 2098c6490447SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 2099c6490447SVladimir Sementsov-Ogievskiy return; 2100c6490447SVladimir Sementsov-Ogievskiy } 2101c6490447SVladimir Sementsov-Ogievskiy 21020e2b7f09SJohn Snow action = common->action->u.block_dirty_bitmap_enable.data; 2103c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, 2104c6490447SVladimir Sementsov-Ogievskiy action->name, 2105c6490447SVladimir Sementsov-Ogievskiy NULL, 2106c6490447SVladimir Sementsov-Ogievskiy errp); 2107c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) { 2108c6490447SVladimir Sementsov-Ogievskiy return; 2109c6490447SVladimir Sementsov-Ogievskiy } 2110c6490447SVladimir Sementsov-Ogievskiy 21113ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { 2112b053bb55SJohn Snow return; 2113b053bb55SJohn Snow } 2114b053bb55SJohn Snow 2115c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); 2116c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap); 2117c6490447SVladimir Sementsov-Ogievskiy } 2118c6490447SVladimir Sementsov-Ogievskiy 2119c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_enable_abort(BlkActionState *common) 2120c6490447SVladimir Sementsov-Ogievskiy { 2121c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2122c6490447SVladimir Sementsov-Ogievskiy common, common); 2123c6490447SVladimir Sementsov-Ogievskiy 2124c6490447SVladimir Sementsov-Ogievskiy if (!state->was_enabled) { 2125c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap); 2126c6490447SVladimir Sementsov-Ogievskiy } 2127c6490447SVladimir Sementsov-Ogievskiy } 2128c6490447SVladimir Sementsov-Ogievskiy 2129c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_prepare(BlkActionState *common, 2130c6490447SVladimir Sementsov-Ogievskiy Error **errp) 2131c6490447SVladimir Sementsov-Ogievskiy { 2132c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmap *action; 2133c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2134c6490447SVladimir Sementsov-Ogievskiy common, common); 2135c6490447SVladimir Sementsov-Ogievskiy 2136c6490447SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 2137c6490447SVladimir Sementsov-Ogievskiy return; 2138c6490447SVladimir Sementsov-Ogievskiy } 2139c6490447SVladimir Sementsov-Ogievskiy 21400e2b7f09SJohn Snow action = common->action->u.block_dirty_bitmap_disable.data; 2141c6490447SVladimir Sementsov-Ogievskiy state->bitmap = block_dirty_bitmap_lookup(action->node, 2142c6490447SVladimir Sementsov-Ogievskiy action->name, 2143c6490447SVladimir Sementsov-Ogievskiy NULL, 2144c6490447SVladimir Sementsov-Ogievskiy errp); 2145c6490447SVladimir Sementsov-Ogievskiy if (!state->bitmap) { 2146c6490447SVladimir Sementsov-Ogievskiy return; 2147c6490447SVladimir Sementsov-Ogievskiy } 2148c6490447SVladimir Sementsov-Ogievskiy 21493ae96d66SJohn Snow if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { 2150b053bb55SJohn Snow return; 2151b053bb55SJohn Snow } 2152b053bb55SJohn Snow 2153c6490447SVladimir Sementsov-Ogievskiy state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); 2154c6490447SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(state->bitmap); 2155c6490447SVladimir Sementsov-Ogievskiy } 2156c6490447SVladimir Sementsov-Ogievskiy 2157c6490447SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_disable_abort(BlkActionState *common) 2158c6490447SVladimir Sementsov-Ogievskiy { 2159c6490447SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2160c6490447SVladimir Sementsov-Ogievskiy common, common); 2161c6490447SVladimir Sementsov-Ogievskiy 2162c6490447SVladimir Sementsov-Ogievskiy if (state->was_enabled) { 2163c6490447SVladimir Sementsov-Ogievskiy bdrv_enable_dirty_bitmap(state->bitmap); 2164c6490447SVladimir Sementsov-Ogievskiy } 2165c6490447SVladimir Sementsov-Ogievskiy } 2166c6490447SVladimir Sementsov-Ogievskiy 21676fd2e407SVladimir Sementsov-Ogievskiy static void block_dirty_bitmap_merge_prepare(BlkActionState *common, 21686fd2e407SVladimir Sementsov-Ogievskiy Error **errp) 21696fd2e407SVladimir Sementsov-Ogievskiy { 21706fd2e407SVladimir Sementsov-Ogievskiy BlockDirtyBitmapMerge *action; 21716fd2e407SVladimir Sementsov-Ogievskiy BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 21726fd2e407SVladimir Sementsov-Ogievskiy common, common); 21736fd2e407SVladimir Sementsov-Ogievskiy 21746fd2e407SVladimir Sementsov-Ogievskiy if (action_check_completion_mode(common, errp) < 0) { 21756fd2e407SVladimir Sementsov-Ogievskiy return; 21766fd2e407SVladimir Sementsov-Ogievskiy } 21776fd2e407SVladimir Sementsov-Ogievskiy 21780e2b7f09SJohn Snow action = common->action->u.block_dirty_bitmap_merge.data; 2179360d4e4eSJohn Snow 2180c6996cf9SEric Blake state->bitmap = block_dirty_bitmap_merge(action->node, action->target, 2181360d4e4eSJohn Snow action->bitmaps, &state->backup, 21826fd2e407SVladimir Sementsov-Ogievskiy errp); 21836fd2e407SVladimir Sementsov-Ogievskiy } 21846fd2e407SVladimir Sementsov-Ogievskiy 2185c4e4b0faSJohn Snow static void block_dirty_bitmap_remove_prepare(BlkActionState *common, 2186c4e4b0faSJohn Snow Error **errp) 2187c4e4b0faSJohn Snow { 2188c4e4b0faSJohn Snow BlockDirtyBitmap *action; 2189c4e4b0faSJohn Snow BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2190c4e4b0faSJohn Snow common, common); 2191c4e4b0faSJohn Snow 2192c4e4b0faSJohn Snow if (action_check_completion_mode(common, errp) < 0) { 2193c4e4b0faSJohn Snow return; 2194c4e4b0faSJohn Snow } 2195c4e4b0faSJohn Snow 2196c4e4b0faSJohn Snow action = common->action->u.block_dirty_bitmap_remove.data; 2197c4e4b0faSJohn Snow 2198c6996cf9SEric Blake state->bitmap = block_dirty_bitmap_remove(action->node, action->name, 2199c4e4b0faSJohn Snow false, &state->bs, errp); 2200c4e4b0faSJohn Snow if (state->bitmap) { 2201c4e4b0faSJohn Snow bdrv_dirty_bitmap_skip_store(state->bitmap, true); 2202c4e4b0faSJohn Snow bdrv_dirty_bitmap_set_busy(state->bitmap, true); 2203c4e4b0faSJohn Snow } 2204c4e4b0faSJohn Snow } 2205c4e4b0faSJohn Snow 2206c4e4b0faSJohn Snow static void block_dirty_bitmap_remove_abort(BlkActionState *common) 2207c4e4b0faSJohn Snow { 2208c4e4b0faSJohn Snow BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2209c4e4b0faSJohn Snow common, common); 2210c4e4b0faSJohn Snow 2211c4e4b0faSJohn Snow if (state->bitmap) { 2212c4e4b0faSJohn Snow bdrv_dirty_bitmap_skip_store(state->bitmap, false); 2213c4e4b0faSJohn Snow bdrv_dirty_bitmap_set_busy(state->bitmap, false); 2214c4e4b0faSJohn Snow } 2215c4e4b0faSJohn Snow } 2216c4e4b0faSJohn Snow 2217c4e4b0faSJohn Snow static void block_dirty_bitmap_remove_commit(BlkActionState *common) 2218c4e4b0faSJohn Snow { 2219c4e4b0faSJohn Snow BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2220c4e4b0faSJohn Snow common, common); 2221c4e4b0faSJohn Snow 2222c4e4b0faSJohn Snow bdrv_dirty_bitmap_set_busy(state->bitmap, false); 22235deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(state->bitmap); 2224c4e4b0faSJohn Snow } 2225c4e4b0faSJohn Snow 222650f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 222778b18b78SStefan Hajnoczi { 222878b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 222978b18b78SStefan Hajnoczi } 223078b18b78SStefan Hajnoczi 223150f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 223278b18b78SStefan Hajnoczi { 2233dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 223478b18b78SStefan Hajnoczi } 223578b18b78SStefan Hajnoczi 223650f43f0fSJohn Snow static const BlkActionOps actions[] = { 223743de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 223843de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 223943de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 224043de7e2dSAlberto Garcia .commit = external_snapshot_commit, 224143de7e2dSAlberto Garcia .abort = external_snapshot_abort, 22424ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 224343de7e2dSAlberto Garcia }, 2244c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2245ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2246ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2247ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2248ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2249da763e83SFam Zheng .clean = external_snapshot_clean, 2250ba0c86a3SWenchao Xia }, 22513037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 22523037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 22533037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 2254111049a4SJohn Snow .commit = drive_backup_commit, 22553037f364SStefan Hajnoczi .abort = drive_backup_abort, 22565d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 22573037f364SStefan Hajnoczi }, 2258bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2259bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2260bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2261111049a4SJohn Snow .commit = blockdev_backup_commit, 2262bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2263bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2264bd8baecdSFam Zheng }, 226578b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 226650f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 226778b18b78SStefan Hajnoczi .prepare = abort_prepare, 226878b18b78SStefan Hajnoczi .commit = abort_commit, 226978b18b78SStefan Hajnoczi }, 2270bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2271bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2272bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2273bbe86010SWenchao Xia .abort = internal_snapshot_abort, 22745d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2275bbe86010SWenchao Xia }, 2276df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2277df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2278df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2279df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2280df9a681dSFam Zheng }, 2281df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2282df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2283df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 22845c4cf8b2SVladimir Sementsov-Ogievskiy .commit = block_dirty_bitmap_free_backup, 22855c4cf8b2SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_restore, 2286c6490447SVladimir Sementsov-Ogievskiy }, 22870e2b7f09SJohn Snow [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = { 2288c6490447SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 2289c6490447SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_enable_prepare, 2290c6490447SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_enable_abort, 2291c6490447SVladimir Sementsov-Ogievskiy }, 22920e2b7f09SJohn Snow [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = { 2293c6490447SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 2294c6490447SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_disable_prepare, 2295c6490447SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_disable_abort, 229666da04ddSJohn Snow }, 22970e2b7f09SJohn Snow [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = { 22986fd2e407SVladimir Sementsov-Ogievskiy .instance_size = sizeof(BlockDirtyBitmapState), 22996fd2e407SVladimir Sementsov-Ogievskiy .prepare = block_dirty_bitmap_merge_prepare, 23006fd2e407SVladimir Sementsov-Ogievskiy .commit = block_dirty_bitmap_free_backup, 23016fd2e407SVladimir Sementsov-Ogievskiy .abort = block_dirty_bitmap_restore, 23026fd2e407SVladimir Sementsov-Ogievskiy }, 2303c4e4b0faSJohn Snow [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = { 2304c4e4b0faSJohn Snow .instance_size = sizeof(BlockDirtyBitmapState), 2305c4e4b0faSJohn Snow .prepare = block_dirty_bitmap_remove_prepare, 2306c4e4b0faSJohn Snow .commit = block_dirty_bitmap_remove_commit, 2307c4e4b0faSJohn Snow .abort = block_dirty_bitmap_remove_abort, 2308c4e4b0faSJohn Snow }, 230966da04ddSJohn Snow /* Where are transactions for MIRROR, COMMIT and STREAM? 231066da04ddSJohn Snow * Although these blockjobs use transaction callbacks like the backup job, 231166da04ddSJohn Snow * these jobs do not necessarily adhere to transaction semantics. 231266da04ddSJohn Snow * These jobs may not fully undo all of their actions on abort, nor do they 231366da04ddSJohn Snow * necessarily work in transactions with more than one job in them. 231466da04ddSJohn Snow */ 2315ba0c86a3SWenchao Xia }; 2316ba0c86a3SWenchao Xia 231794d16a64SJohn Snow /** 231894d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 231994d16a64SJohn Snow * that structure with desired defaults if they are unset. 232094d16a64SJohn Snow */ 232194d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 232294d16a64SJohn Snow TransactionProperties *props) 232394d16a64SJohn Snow { 232494d16a64SJohn Snow if (!props) { 232594d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 232694d16a64SJohn Snow } 232794d16a64SJohn Snow 232894d16a64SJohn Snow if (!props->has_completion_mode) { 232994d16a64SJohn Snow props->has_completion_mode = true; 233094d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 233194d16a64SJohn Snow } 233294d16a64SJohn Snow 233394d16a64SJohn Snow return props; 233494d16a64SJohn Snow } 233594d16a64SJohn Snow 23368802d1fdSJeff Cody /* 2337b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2338b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 23398802d1fdSJeff Cody */ 234094d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 234194d16a64SJohn Snow bool has_props, 234294d16a64SJohn Snow struct TransactionProperties *props, 234394d16a64SJohn Snow Error **errp) 23448802d1fdSJeff Cody { 2345c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 234662c9e416SKevin Wolf JobTxn *block_job_txn = NULL; 234750f43f0fSJohn Snow BlkActionState *state, *next; 234843e17041SLuiz Capitulino Error *local_err = NULL; 23498802d1fdSJeff Cody 2350f4de0f8cSJohn Snow QTAILQ_HEAD(, BlkActionState) snap_bdrv_states; 2351f4de0f8cSJohn Snow QTAILQ_INIT(&snap_bdrv_states); 23528802d1fdSJeff Cody 235394d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 235462c9e416SKevin Wolf * If not, we don't really need to make a JobTxn. 235594d16a64SJohn Snow */ 235694d16a64SJohn Snow props = get_transaction_properties(props); 235794d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 23587eaa8fb5SKevin Wolf block_job_txn = job_txn_new(); 235994d16a64SJohn Snow } 236094d16a64SJohn Snow 2361b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 23628802d1fdSJeff Cody bdrv_drain_all(); 23638802d1fdSJeff Cody 2364b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 23658802d1fdSJeff Cody while (NULL != dev_entry) { 2366c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 236750f43f0fSJohn Snow const BlkActionOps *ops; 236852e7c241SPaolo Bonzini 23698802d1fdSJeff Cody dev_info = dev_entry->value; 23708802d1fdSJeff Cody dev_entry = dev_entry->next; 23718802d1fdSJeff Cody 23726a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2373ba0c86a3SWenchao Xia 23746a8f9661SEric Blake ops = &actions[dev_info->type]; 2375aa3fe714SMax Reitz assert(ops->instance_size > 0); 2376aa3fe714SMax Reitz 2377ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2378ba5d6ab6SStefan Hajnoczi state->ops = ops; 2379ba5d6ab6SStefan Hajnoczi state->action = dev_info; 238094d16a64SJohn Snow state->block_job_txn = block_job_txn; 238194d16a64SJohn Snow state->txn_props = props; 2382f4de0f8cSJohn Snow QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 23838802d1fdSJeff Cody 2384ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 238584d18f06SMarkus Armbruster if (local_err) { 23869b9877eeSWenchao Xia error_propagate(errp, local_err); 23879b9877eeSWenchao Xia goto delete_and_fail; 23889b9877eeSWenchao Xia } 238952e7c241SPaolo Bonzini } 23908802d1fdSJeff Cody 2391f4de0f8cSJohn Snow QTAILQ_FOREACH(state, &snap_bdrv_states, entry) { 2392f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2393ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 23948802d1fdSJeff Cody } 2395f9ea81e8SStefan Hajnoczi } 23968802d1fdSJeff Cody 23978802d1fdSJeff Cody /* success */ 23988802d1fdSJeff Cody goto exit; 23998802d1fdSJeff Cody 24008802d1fdSJeff Cody delete_and_fail: 2401b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2402f4de0f8cSJohn Snow QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) { 2403ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2404ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2405ba0c86a3SWenchao Xia } 24068802d1fdSJeff Cody } 24078802d1fdSJeff Cody exit: 2408f4de0f8cSJohn Snow QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2409ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2410ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2411ba0c86a3SWenchao Xia } 2412ba5d6ab6SStefan Hajnoczi g_free(state); 24138802d1fdSJeff Cody } 241494d16a64SJohn Snow if (!has_props) { 241594d16a64SJohn Snow qapi_free_TransactionProperties(props); 241694d16a64SJohn Snow } 24177eaa8fb5SKevin Wolf job_txn_unref(block_job_txn); 24188802d1fdSJeff Cody } 24198802d1fdSJeff Cody 242012d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 242112d3ba82SBenoît Canet bool has_node_name, const char *node_name, 242212d3ba82SBenoît Canet const char *password, Error **errp) 2423666daa68SMarkus Armbruster { 2424c01c214bSDaniel P. Berrange error_setg(errp, 2425c01c214bSDaniel P. Berrange "Setting block passwords directly is no longer supported"); 2426e3442099SStefan Hajnoczi } 2427e3442099SStefan Hajnoczi 2428a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, 2429a3b52535SVladimir Sementsov-Ogievskiy const char *name, 2430a3b52535SVladimir Sementsov-Ogievskiy Error **errp) 2431a3b52535SVladimir Sementsov-Ogievskiy { 2432a3b52535SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap; 2433a3b52535SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 2434a3b52535SVladimir Sementsov-Ogievskiy BlockDirtyBitmapSha256 *ret = NULL; 2435a3b52535SVladimir Sementsov-Ogievskiy char *sha256; 2436a3b52535SVladimir Sementsov-Ogievskiy 2437a3b52535SVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); 2438a3b52535SVladimir Sementsov-Ogievskiy if (!bitmap || !bs) { 2439a3b52535SVladimir Sementsov-Ogievskiy return NULL; 2440a3b52535SVladimir Sementsov-Ogievskiy } 2441a3b52535SVladimir Sementsov-Ogievskiy 2442a3b52535SVladimir Sementsov-Ogievskiy sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp); 2443a3b52535SVladimir Sementsov-Ogievskiy if (sha256 == NULL) { 2444a3b52535SVladimir Sementsov-Ogievskiy return NULL; 2445a3b52535SVladimir Sementsov-Ogievskiy } 2446a3b52535SVladimir Sementsov-Ogievskiy 2447a3b52535SVladimir Sementsov-Ogievskiy ret = g_new(BlockDirtyBitmapSha256, 1); 2448a3b52535SVladimir Sementsov-Ogievskiy ret->sha256 = sha256; 2449a3b52535SVladimir Sementsov-Ogievskiy 2450a3b52535SVladimir Sementsov-Ogievskiy return ret; 2451a3b52535SVladimir Sementsov-Ogievskiy } 2452a3b52535SVladimir Sementsov-Ogievskiy 2453eb94b81aSKevin Wolf void coroutine_fn qmp_block_resize(bool has_device, const char *device, 24543b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 24553b1dbd11SBenoît Canet int64_t size, Error **errp) 24566d4a2b3aSChristoph Hellwig { 24573b1dbd11SBenoît Canet Error *local_err = NULL; 2458d9dbf25fSKevin Wolf BlockBackend *blk; 24596d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2460eb94b81aSKevin Wolf AioContext *old_ctx; 24616d4a2b3aSChristoph Hellwig 24623b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 24633b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 24643b1dbd11SBenoît Canet &local_err); 246584d18f06SMarkus Armbruster if (local_err) { 24663b1dbd11SBenoît Canet error_propagate(errp, local_err); 24673b1dbd11SBenoît Canet return; 24683b1dbd11SBenoît Canet } 24693b1dbd11SBenoît Canet 24706d4a2b3aSChristoph Hellwig if (size < 0) { 2471c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2472d9dbf25fSKevin Wolf return; 24736d4a2b3aSChristoph Hellwig } 24746d4a2b3aSChristoph Hellwig 24759c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2476c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2477d9dbf25fSKevin Wolf return; 24789c75e168SJeff Cody } 24799c75e168SJeff Cody 2480a3aeeab5SEric Blake blk = blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL, errp); 2481a3aeeab5SEric Blake if (!blk) { 2482d9dbf25fSKevin Wolf return; 2483d7086422SKevin Wolf } 24847dad9ee6SKevin Wolf 24858089eab2SKevin Wolf bdrv_co_lock(bs); 2486698bdfa0SJohn Snow bdrv_drained_begin(bs); 24878089eab2SKevin Wolf bdrv_co_unlock(bs); 24888089eab2SKevin Wolf 2489eb94b81aSKevin Wolf old_ctx = bdrv_co_enter(bs); 2490a3aeeab5SEric Blake blk_truncate(blk, size, false, PREALLOC_MODE_OFF, 0, errp); 2491eb94b81aSKevin Wolf bdrv_co_leave(bs, old_ctx); 2492927e0e76SStefan Hajnoczi 2493eb94b81aSKevin Wolf bdrv_co_lock(bs); 24948089eab2SKevin Wolf bdrv_drained_end(bs); 24957dad9ee6SKevin Wolf blk_unref(blk); 2496eb94b81aSKevin Wolf bdrv_co_unlock(bs); 24976d4a2b3aSChristoph Hellwig } 249812bd451fSStefan Hajnoczi 24992323322eSAlberto Garcia void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, 250013d8cc51SJeff Cody bool has_base, const char *base, 2501312fe09cSAlberto Garcia bool has_base_node, const char *base_node, 250213d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 25037f4a396dSVladimir Sementsov-Ogievskiy bool has_bottom, const char *bottom, 250413d8cc51SJeff Cody bool has_speed, int64_t speed, 25051d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 2506880747a8SAndrey Shinkevich bool has_filter_node_name, const char *filter_node_name, 2507241ca1abSJohn Snow bool has_auto_finalize, bool auto_finalize, 2508241ca1abSJohn Snow bool has_auto_dismiss, bool auto_dismiss, 25091d809098SPaolo Bonzini Error **errp) 251012bd451fSStefan Hajnoczi { 25117f4a396dSVladimir Sementsov-Ogievskiy BlockDriverState *bs, *iter, *iter_end; 2512c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 25137f4a396dSVladimir Sementsov-Ogievskiy BlockDriverState *bottom_bs = NULL; 2514f3e69bebSStefan Hajnoczi AioContext *aio_context; 2515fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 2516cf6320dfSJohn Snow int job_flags = JOB_DEFAULT; 251712bd451fSStefan Hajnoczi 25187f4a396dSVladimir Sementsov-Ogievskiy if (has_base && has_base_node) { 25197f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "'base' and 'base-node' cannot be specified " 25207f4a396dSVladimir Sementsov-Ogievskiy "at the same time"); 25217f4a396dSVladimir Sementsov-Ogievskiy return; 25227f4a396dSVladimir Sementsov-Ogievskiy } 25237f4a396dSVladimir Sementsov-Ogievskiy 25247f4a396dSVladimir Sementsov-Ogievskiy if (has_base && has_bottom) { 25257f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "'base' and 'bottom' cannot be specified " 25267f4a396dSVladimir Sementsov-Ogievskiy "at the same time"); 25277f4a396dSVladimir Sementsov-Ogievskiy return; 25287f4a396dSVladimir Sementsov-Ogievskiy } 25297f4a396dSVladimir Sementsov-Ogievskiy 25307f4a396dSVladimir Sementsov-Ogievskiy if (has_bottom && has_base_node) { 25317f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "'bottom' and 'base-node' cannot be specified " 25327f4a396dSVladimir Sementsov-Ogievskiy "at the same time"); 25337f4a396dSVladimir Sementsov-Ogievskiy return; 25347f4a396dSVladimir Sementsov-Ogievskiy } 25357f4a396dSVladimir Sementsov-Ogievskiy 25361d809098SPaolo Bonzini if (!has_on_error) { 25371d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 25381d809098SPaolo Bonzini } 25391d809098SPaolo Bonzini 2540554b6147SAlberto Garcia bs = bdrv_lookup_bs(device, device, errp); 2541b6c1bae5SKevin Wolf if (!bs) { 254212bd451fSStefan Hajnoczi return; 254312bd451fSStefan Hajnoczi } 254412bd451fSStefan Hajnoczi 2545b6c1bae5SKevin Wolf aio_context = bdrv_get_aio_context(bs); 2546f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 2547f3e69bebSStefan Hajnoczi 254813d8cc51SJeff Cody if (has_base) { 2549c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 2550c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 2551b0d7be2aSMarkus Armbruster error_setg(errp, "Can't find '%s' in the backing chain", base); 2552f3e69bebSStefan Hajnoczi goto out; 255312bd451fSStefan Hajnoczi } 2554f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 2555c8c3080fSMarcelo Tosatti } 255612bd451fSStefan Hajnoczi 2557312fe09cSAlberto Garcia if (has_base_node) { 2558312fe09cSAlberto Garcia base_bs = bdrv_lookup_bs(NULL, base_node, errp); 2559312fe09cSAlberto Garcia if (!base_bs) { 2560312fe09cSAlberto Garcia goto out; 2561312fe09cSAlberto Garcia } 2562312fe09cSAlberto Garcia if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { 2563312fe09cSAlberto Garcia error_setg(errp, "Node '%s' is not a backing image of '%s'", 2564312fe09cSAlberto Garcia base_node, device); 2565312fe09cSAlberto Garcia goto out; 2566312fe09cSAlberto Garcia } 2567312fe09cSAlberto Garcia assert(bdrv_get_aio_context(base_bs) == aio_context); 2568f30c66baSMax Reitz bdrv_refresh_filename(base_bs); 2569312fe09cSAlberto Garcia } 2570312fe09cSAlberto Garcia 25717f4a396dSVladimir Sementsov-Ogievskiy if (has_bottom) { 25727f4a396dSVladimir Sementsov-Ogievskiy bottom_bs = bdrv_lookup_bs(NULL, bottom, errp); 25737f4a396dSVladimir Sementsov-Ogievskiy if (!bottom_bs) { 25747f4a396dSVladimir Sementsov-Ogievskiy goto out; 25757f4a396dSVladimir Sementsov-Ogievskiy } 25767f4a396dSVladimir Sementsov-Ogievskiy if (!bottom_bs->drv) { 25777f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "Node '%s' is not open", bottom); 25787f4a396dSVladimir Sementsov-Ogievskiy goto out; 25797f4a396dSVladimir Sementsov-Ogievskiy } 25807f4a396dSVladimir Sementsov-Ogievskiy if (bottom_bs->drv->is_filter) { 25817f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "Node '%s' is a filter, use a non-filter node " 25827f4a396dSVladimir Sementsov-Ogievskiy "as 'bottom'", bottom); 25837f4a396dSVladimir Sementsov-Ogievskiy goto out; 25847f4a396dSVladimir Sementsov-Ogievskiy } 25857f4a396dSVladimir Sementsov-Ogievskiy if (!bdrv_chain_contains(bs, bottom_bs)) { 25867f4a396dSVladimir Sementsov-Ogievskiy error_setg(errp, "Node '%s' is not in a chain starting from '%s'", 25877f4a396dSVladimir Sementsov-Ogievskiy bottom, device); 25887f4a396dSVladimir Sementsov-Ogievskiy goto out; 25897f4a396dSVladimir Sementsov-Ogievskiy } 25907f4a396dSVladimir Sementsov-Ogievskiy assert(bdrv_get_aio_context(bottom_bs) == aio_context); 25917f4a396dSVladimir Sementsov-Ogievskiy } 25927f4a396dSVladimir Sementsov-Ogievskiy 25937f4a396dSVladimir Sementsov-Ogievskiy /* 25947f4a396dSVladimir Sementsov-Ogievskiy * Check for op blockers in the whole chain between bs and base (or bottom) 25957f4a396dSVladimir Sementsov-Ogievskiy */ 25967f4a396dSVladimir Sementsov-Ogievskiy iter_end = has_bottom ? bdrv_filter_or_cow_bs(bottom_bs) : base_bs; 25977f4a396dSVladimir Sementsov-Ogievskiy for (iter = bs; iter && iter != iter_end; 259867acfd21SMax Reitz iter = bdrv_filter_or_cow_bs(iter)) 259967acfd21SMax Reitz { 2600554b6147SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { 2601554b6147SAlberto Garcia goto out; 2602554b6147SAlberto Garcia } 2603554b6147SAlberto Garcia } 2604554b6147SAlberto Garcia 260513d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 260613d8cc51SJeff Cody * file, and specifying one is therefore an error */ 260713d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 260813d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 260913d8cc51SJeff Cody "entire chain"); 2610f3e69bebSStefan Hajnoczi goto out; 261113d8cc51SJeff Cody } 261213d8cc51SJeff Cody 2613241ca1abSJohn Snow if (has_auto_finalize && !auto_finalize) { 2614241ca1abSJohn Snow job_flags |= JOB_MANUAL_FINALIZE; 2615241ca1abSJohn Snow } 2616241ca1abSJohn Snow if (has_auto_dismiss && !auto_dismiss) { 2617241ca1abSJohn Snow job_flags |= JOB_MANUAL_DISMISS; 2618241ca1abSJohn Snow } 2619241ca1abSJohn Snow 2620000e5a1cSAndrey Shinkevich stream_start(has_job_id ? job_id : NULL, bs, base_bs, backing_file, 26217f4a396dSVladimir Sementsov-Ogievskiy bottom_bs, job_flags, has_speed ? speed : 0, on_error, 2622880747a8SAndrey Shinkevich filter_node_name, &local_err); 262384d18f06SMarkus Armbruster if (local_err) { 2624fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 2625f3e69bebSStefan Hajnoczi goto out; 262612bd451fSStefan Hajnoczi } 262712bd451fSStefan Hajnoczi 2628b23c580cSVladimir Sementsov-Ogievskiy trace_qmp_block_stream(bs); 2629f3e69bebSStefan Hajnoczi 2630f3e69bebSStefan Hajnoczi out: 2631f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 263212bd451fSStefan Hajnoczi } 26332d47c6e9SStefan Hajnoczi 2634fd62c609SAlberto Garcia void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, 26353c605f40SKevin Wolf bool has_base_node, const char *base_node, 26367676e2c5SJeff Cody bool has_base, const char *base, 26373c605f40SKevin Wolf bool has_top_node, const char *top_node, 26387676e2c5SJeff Cody bool has_top, const char *top, 263954e26900SJeff Cody bool has_backing_file, const char *backing_file, 2640ed61fc10SJeff Cody bool has_speed, int64_t speed, 26418faad1c7SKevin Wolf bool has_on_error, BlockdevOnError on_error, 26420db832f4SKevin Wolf bool has_filter_node_name, const char *filter_node_name, 264396fbf534SJohn Snow bool has_auto_finalize, bool auto_finalize, 264496fbf534SJohn Snow bool has_auto_dismiss, bool auto_dismiss, 2645ed61fc10SJeff Cody Error **errp) 2646ed61fc10SJeff Cody { 2647ed61fc10SJeff Cody BlockDriverState *bs; 2648058223a6SAlberto Garcia BlockDriverState *iter; 2649ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 26509e85cd5cSStefan Hajnoczi AioContext *aio_context; 2651ed61fc10SJeff Cody Error *local_err = NULL; 26525360782dSJohn Snow int job_flags = JOB_DEFAULT; 265305ea385aSMax Reitz uint64_t top_perm, top_shared; 2654ed61fc10SJeff Cody 265554504663SMax Reitz if (!has_speed) { 265654504663SMax Reitz speed = 0; 265754504663SMax Reitz } 26588faad1c7SKevin Wolf if (!has_on_error) { 26598faad1c7SKevin Wolf on_error = BLOCKDEV_ON_ERROR_REPORT; 26608faad1c7SKevin Wolf } 26610db832f4SKevin Wolf if (!has_filter_node_name) { 26620db832f4SKevin Wolf filter_node_name = NULL; 26630db832f4SKevin Wolf } 266496fbf534SJohn Snow if (has_auto_finalize && !auto_finalize) { 266596fbf534SJohn Snow job_flags |= JOB_MANUAL_FINALIZE; 266696fbf534SJohn Snow } 266796fbf534SJohn Snow if (has_auto_dismiss && !auto_dismiss) { 266896fbf534SJohn Snow job_flags |= JOB_MANUAL_DISMISS; 266996fbf534SJohn Snow } 267054504663SMax Reitz 26717676e2c5SJeff Cody /* Important Note: 26727676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 26737676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 26747676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 26757676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 26761d13b167SKevin Wolf bs = qmp_get_root_bs(device, &local_err); 26771d13b167SKevin Wolf if (!bs) { 26781d13b167SKevin Wolf bs = bdrv_lookup_bs(device, device, NULL); 26791d13b167SKevin Wolf if (!bs) { 26801d13b167SKevin Wolf error_free(local_err); 268175158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 268275158ebbSMarkus Armbruster "Device '%s' not found", device); 26831d13b167SKevin Wolf } else { 26841d13b167SKevin Wolf error_propagate(errp, local_err); 26851d13b167SKevin Wolf } 2686ed61fc10SJeff Cody return; 2687ed61fc10SJeff Cody } 2688ed61fc10SJeff Cody 26891d13b167SKevin Wolf aio_context = bdrv_get_aio_context(bs); 26909e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 26919e85cd5cSStefan Hajnoczi 2692bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 26939e85cd5cSStefan Hajnoczi goto out; 2694628ff683SFam Zheng } 2695628ff683SFam Zheng 2696ed61fc10SJeff Cody /* default top_bs is the active layer */ 2697ed61fc10SJeff Cody top_bs = bs; 2698ed61fc10SJeff Cody 26993c605f40SKevin Wolf if (has_top_node && has_top) { 27003c605f40SKevin Wolf error_setg(errp, "'top-node' and 'top' are mutually exclusive"); 27013c605f40SKevin Wolf goto out; 27023c605f40SKevin Wolf } else if (has_top_node) { 27033c605f40SKevin Wolf top_bs = bdrv_lookup_bs(NULL, top_node, errp); 27043c605f40SKevin Wolf if (top_bs == NULL) { 27053c605f40SKevin Wolf goto out; 27063c605f40SKevin Wolf } 27073c605f40SKevin Wolf if (!bdrv_chain_contains(bs, top_bs)) { 27083c605f40SKevin Wolf error_setg(errp, "'%s' is not in this backing file chain", 27093c605f40SKevin Wolf top_node); 27103c605f40SKevin Wolf goto out; 27113c605f40SKevin Wolf } 27123c605f40SKevin Wolf } else if (has_top && top) { 2713f30c66baSMax Reitz /* This strcmp() is just a shortcut, there is no need to 2714f30c66baSMax Reitz * refresh @bs's filename. If it mismatches, 2715f30c66baSMax Reitz * bdrv_find_backing_image() will do the refresh and may still 2716f30c66baSMax Reitz * return @bs. */ 2717ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 2718ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 2719ed61fc10SJeff Cody } 2720ed61fc10SJeff Cody } 2721ed61fc10SJeff Cody 2722ed61fc10SJeff Cody if (top_bs == NULL) { 2723ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 27249e85cd5cSStefan Hajnoczi goto out; 2725ed61fc10SJeff Cody } 2726ed61fc10SJeff Cody 27279e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 27289e85cd5cSStefan Hajnoczi 27293c605f40SKevin Wolf if (has_base_node && has_base) { 27303c605f40SKevin Wolf error_setg(errp, "'base-node' and 'base' are mutually exclusive"); 27313c605f40SKevin Wolf goto out; 27323c605f40SKevin Wolf } else if (has_base_node) { 27333c605f40SKevin Wolf base_bs = bdrv_lookup_bs(NULL, base_node, errp); 27343c605f40SKevin Wolf if (base_bs == NULL) { 27353c605f40SKevin Wolf goto out; 27363c605f40SKevin Wolf } 27373c605f40SKevin Wolf if (!bdrv_chain_contains(top_bs, base_bs)) { 27383c605f40SKevin Wolf error_setg(errp, "'%s' is not in this backing file chain", 27393c605f40SKevin Wolf base_node); 27403c605f40SKevin Wolf goto out; 27413c605f40SKevin Wolf } 27423c605f40SKevin Wolf } else if (has_base && base) { 2743d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 2744b0d7be2aSMarkus Armbruster if (base_bs == NULL) { 2745b0d7be2aSMarkus Armbruster error_setg(errp, "Can't find '%s' in the backing chain", base); 2746b0d7be2aSMarkus Armbruster goto out; 2747b0d7be2aSMarkus Armbruster } 2748d5208c45SJeff Cody } else { 2749d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 2750d5208c45SJeff Cody if (base_bs == NULL) { 2751b0d7be2aSMarkus Armbruster error_setg(errp, "There is no backimg image"); 27529e85cd5cSStefan Hajnoczi goto out; 2753d5208c45SJeff Cody } 2754b0d7be2aSMarkus Armbruster } 2755d5208c45SJeff Cody 27569e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 27579e85cd5cSStefan Hajnoczi 27589a71b9deSMax Reitz for (iter = top_bs; iter != bdrv_filter_or_cow_bs(base_bs); 27599a71b9deSMax Reitz iter = bdrv_filter_or_cow_bs(iter)) 27609a71b9deSMax Reitz { 2761058223a6SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 2762bb00021dSFam Zheng goto out; 2763bb00021dSFam Zheng } 2764058223a6SAlberto Garcia } 2765bb00021dSFam Zheng 27667676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 27677676e2c5SJeff Cody if (top_bs == base_bs) { 27687676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 27699e85cd5cSStefan Hajnoczi goto out; 27707676e2c5SJeff Cody } 27717676e2c5SJeff Cody 277205ea385aSMax Reitz /* 277305ea385aSMax Reitz * Active commit is required if and only if someone has taken a 277405ea385aSMax Reitz * WRITE permission on the top node. Historically, we have always 277505ea385aSMax Reitz * used active commit for top nodes, so continue that practice 277605ea385aSMax Reitz * lest we possibly break clients that rely on this behavior, e.g. 277705ea385aSMax Reitz * to later attach this node to a writing parent. 277805ea385aSMax Reitz * (Active commit is never really wrong.) 277905ea385aSMax Reitz */ 278005ea385aSMax Reitz bdrv_get_cumulative_perm(top_bs, &top_perm, &top_shared); 278105ea385aSMax Reitz if (top_perm & BLK_PERM_WRITE || 278205ea385aSMax Reitz bdrv_skip_filters(top_bs) == bdrv_skip_filters(bs)) 278305ea385aSMax Reitz { 278454e26900SJeff Cody if (has_backing_file) { 278505ea385aSMax Reitz if (bdrv_skip_filters(top_bs) == bdrv_skip_filters(bs)) { 278654e26900SJeff Cody error_setg(errp, "'backing-file' specified," 278754e26900SJeff Cody " but 'top' is the active layer"); 278805ea385aSMax Reitz } else { 278905ea385aSMax Reitz error_setg(errp, "'backing-file' specified, but 'top' has a " 279005ea385aSMax Reitz "writer on it"); 279105ea385aSMax Reitz } 27929e85cd5cSStefan Hajnoczi goto out; 279354e26900SJeff Cody } 279405ea385aSMax Reitz if (!has_job_id) { 279505ea385aSMax Reitz /* 279605ea385aSMax Reitz * Emulate here what block_job_create() does, because it 279705ea385aSMax Reitz * is possible that @bs != @top_bs (the block job should 279805ea385aSMax Reitz * be named after @bs, even if @top_bs is the actual 279905ea385aSMax Reitz * source) 280005ea385aSMax Reitz */ 280105ea385aSMax Reitz job_id = bdrv_get_device_name(bs); 280205ea385aSMax Reitz } 280305ea385aSMax Reitz commit_active_start(job_id, top_bs, base_bs, job_flags, speed, on_error, 280478bbd910SFam Zheng filter_node_name, NULL, NULL, false, &local_err); 280520a63d2cSFam Zheng } else { 2806058223a6SAlberto Garcia BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); 2807058223a6SAlberto Garcia if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 2808058223a6SAlberto Garcia goto out; 2809058223a6SAlberto Garcia } 28105360782dSJohn Snow commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags, 28115360782dSJohn Snow speed, on_error, has_backing_file ? backing_file : NULL, 28120db832f4SKevin Wolf filter_node_name, &local_err); 281320a63d2cSFam Zheng } 2814ed61fc10SJeff Cody if (local_err != NULL) { 2815ed61fc10SJeff Cody error_propagate(errp, local_err); 28169e85cd5cSStefan Hajnoczi goto out; 2817ed61fc10SJeff Cody } 28189e85cd5cSStefan Hajnoczi 28199e85cd5cSStefan Hajnoczi out: 28209e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 2821ed61fc10SJeff Cody } 2822ed61fc10SJeff Cody 28237b0b870bSJohn Snow /* Common QMP interface for drive-backup and blockdev-backup */ 28247b0b870bSJohn Snow static BlockJob *do_backup_common(BackupCommon *backup, 28257b0b870bSJohn Snow BlockDriverState *bs, 28267b0b870bSJohn Snow BlockDriverState *target_bs, 28277b0b870bSJohn Snow AioContext *aio_context, 28287b0b870bSJohn Snow JobTxn *txn, Error **errp) 282999a9addfSStefan Hajnoczi { 2830111049a4SJohn Snow BlockJob *job = NULL; 2831d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 28326a30f663SVladimir Sementsov-Ogievskiy BackupPerf perf = { .max_workers = 64 }; 28337b0b870bSJohn Snow int job_flags = JOB_DEFAULT; 283499a9addfSStefan Hajnoczi 283581206a89SPavel Butsykin if (!backup->has_speed) { 283681206a89SPavel Butsykin backup->speed = 0; 283799a9addfSStefan Hajnoczi } 283881206a89SPavel Butsykin if (!backup->has_on_source_error) { 283981206a89SPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 284099a9addfSStefan Hajnoczi } 284181206a89SPavel Butsykin if (!backup->has_on_target_error) { 284281206a89SPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 284399a9addfSStefan Hajnoczi } 284481206a89SPavel Butsykin if (!backup->has_job_id) { 284581206a89SPavel Butsykin backup->job_id = NULL; 284699a9addfSStefan Hajnoczi } 2847b40dacdcSJohn Snow if (!backup->has_auto_finalize) { 2848b40dacdcSJohn Snow backup->auto_finalize = true; 2849b40dacdcSJohn Snow } 2850b40dacdcSJohn Snow if (!backup->has_auto_dismiss) { 2851b40dacdcSJohn Snow backup->auto_dismiss = true; 2852b40dacdcSJohn Snow } 285313b9414bSPavel Butsykin if (!backup->has_compress) { 285413b9414bSPavel Butsykin backup->compress = false; 285513b9414bSPavel Butsykin } 285699a9addfSStefan Hajnoczi 285786c6a3b6SVladimir Sementsov-Ogievskiy if (backup->x_perf) { 285886c6a3b6SVladimir Sementsov-Ogievskiy if (backup->x_perf->has_use_copy_range) { 285986c6a3b6SVladimir Sementsov-Ogievskiy perf.use_copy_range = backup->x_perf->use_copy_range; 286086c6a3b6SVladimir Sementsov-Ogievskiy } 28612c59fd83SVladimir Sementsov-Ogievskiy if (backup->x_perf->has_max_workers) { 28622c59fd83SVladimir Sementsov-Ogievskiy perf.max_workers = backup->x_perf->max_workers; 28632c59fd83SVladimir Sementsov-Ogievskiy } 28642c59fd83SVladimir Sementsov-Ogievskiy if (backup->x_perf->has_max_chunk) { 28652c59fd83SVladimir Sementsov-Ogievskiy perf.max_chunk = backup->x_perf->max_chunk; 28662c59fd83SVladimir Sementsov-Ogievskiy } 286786c6a3b6SVladimir Sementsov-Ogievskiy } 286886c6a3b6SVladimir Sementsov-Ogievskiy 2869a6c9365aSJohn Snow if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) || 2870a6c9365aSJohn Snow (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) { 2871a6c9365aSJohn Snow /* done before desugaring 'incremental' to print the right message */ 2872a6c9365aSJohn Snow if (!backup->has_bitmap) { 2873a6c9365aSJohn Snow error_setg(errp, "must provide a valid bitmap name for " 2874a6c9365aSJohn Snow "'%s' sync mode", MirrorSyncMode_str(backup->sync)); 2875a6c9365aSJohn Snow return NULL; 2876a6c9365aSJohn Snow } 2877a6c9365aSJohn Snow } 2878a6c9365aSJohn Snow 2879c8b56501SJohn Snow if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) { 2880c8b56501SJohn Snow if (backup->has_bitmap_mode && 2881c8b56501SJohn Snow backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) { 2882c8b56501SJohn Snow error_setg(errp, "Bitmap sync mode must be '%s' " 2883c8b56501SJohn Snow "when using sync mode '%s'", 2884c8b56501SJohn Snow BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS), 2885c8b56501SJohn Snow MirrorSyncMode_str(backup->sync)); 2886c8b56501SJohn Snow return NULL; 2887c8b56501SJohn Snow } 2888c8b56501SJohn Snow backup->has_bitmap_mode = true; 2889c8b56501SJohn Snow backup->sync = MIRROR_SYNC_MODE_BITMAP; 2890c8b56501SJohn Snow backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS; 2891c8b56501SJohn Snow } 2892c8b56501SJohn Snow 28937b0b870bSJohn Snow if (backup->has_bitmap) { 28947b0b870bSJohn Snow bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); 28957b0b870bSJohn Snow if (!bmap) { 28967b0b870bSJohn Snow error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); 28977b0b870bSJohn Snow return NULL; 28987b0b870bSJohn Snow } 2899c8b56501SJohn Snow if (!backup->has_bitmap_mode) { 2900c8b56501SJohn Snow error_setg(errp, "Bitmap sync mode must be given " 2901c8b56501SJohn Snow "when providing a bitmap"); 2902c8b56501SJohn Snow return NULL; 2903c8b56501SJohn Snow } 2904b30ffbefSJohn Snow if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) { 29057b0b870bSJohn Snow return NULL; 29067b0b870bSJohn Snow } 29071a2b8b40SJohn Snow 29081a2b8b40SJohn Snow /* This does not produce a useful bitmap artifact: */ 29091a2b8b40SJohn Snow if (backup->sync == MIRROR_SYNC_MODE_NONE) { 29101a2b8b40SJohn Snow error_setg(errp, "sync mode '%s' does not produce meaningful bitmap" 29111a2b8b40SJohn Snow " outputs", MirrorSyncMode_str(backup->sync)); 29121a2b8b40SJohn Snow return NULL; 29131a2b8b40SJohn Snow } 29141a2b8b40SJohn Snow 29151a2b8b40SJohn Snow /* If the bitmap isn't used for input or output, this is useless: */ 29161a2b8b40SJohn Snow if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER && 29171a2b8b40SJohn Snow backup->sync != MIRROR_SYNC_MODE_BITMAP) { 29181a2b8b40SJohn Snow error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect" 29191a2b8b40SJohn Snow " when combined with sync mode '%s'", 29201a2b8b40SJohn Snow BitmapSyncMode_str(backup->bitmap_mode), 29211a2b8b40SJohn Snow MirrorSyncMode_str(backup->sync)); 29221a2b8b40SJohn Snow return NULL; 29231a2b8b40SJohn Snow } 29241a2b8b40SJohn Snow } 29251a2b8b40SJohn Snow 29261a2b8b40SJohn Snow if (!backup->has_bitmap && backup->has_bitmap_mode) { 29271a2b8b40SJohn Snow error_setg(errp, "Cannot specify bitmap sync mode without a bitmap"); 29281a2b8b40SJohn Snow return NULL; 29297b0b870bSJohn Snow } 29307b0b870bSJohn Snow 29317b0b870bSJohn Snow if (!backup->auto_finalize) { 29327b0b870bSJohn Snow job_flags |= JOB_MANUAL_FINALIZE; 29337b0b870bSJohn Snow } 29347b0b870bSJohn Snow if (!backup->auto_dismiss) { 29357b0b870bSJohn Snow job_flags |= JOB_MANUAL_DISMISS; 29367b0b870bSJohn Snow } 29377b0b870bSJohn Snow 29387b0b870bSJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 2939c8b56501SJohn Snow backup->sync, bmap, backup->bitmap_mode, 2940c8b56501SJohn Snow backup->compress, 294100e30f05SVladimir Sementsov-Ogievskiy backup->filter_node_name, 294286c6a3b6SVladimir Sementsov-Ogievskiy &perf, 2943c8b56501SJohn Snow backup->on_source_error, 2944c8b56501SJohn Snow backup->on_target_error, 29457b0b870bSJohn Snow job_flags, NULL, NULL, txn, errp); 29467b0b870bSJohn Snow return job; 29477b0b870bSJohn Snow } 29487b0b870bSJohn Snow 29492288ccfaSSergio Lopez void qmp_drive_backup(DriveBackup *backup, Error **errp) 29507b0b870bSJohn Snow { 29512288ccfaSSergio Lopez TransactionAction action = { 29522288ccfaSSergio Lopez .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP, 29532288ccfaSSergio Lopez .u.drive_backup.data = backup, 29542288ccfaSSergio Lopez }; 29552288ccfaSSergio Lopez blockdev_do_action(&action, errp); 295678f51fdeSJohn Snow } 295778f51fdeSJohn Snow 2958facda544SPeter Krempa BlockDeviceInfoList *qmp_query_named_block_nodes(bool has_flat, 2959facda544SPeter Krempa bool flat, 2960facda544SPeter Krempa Error **errp) 2961c13163fbSBenoît Canet { 2962facda544SPeter Krempa bool return_flat = has_flat && flat; 2963facda544SPeter Krempa 2964facda544SPeter Krempa return bdrv_named_nodes_list(return_flat, errp); 2965c13163fbSBenoît Canet } 2966c13163fbSBenoît Canet 29675d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp) 29685d3b4e99SVladimir Sementsov-Ogievskiy { 29695d3b4e99SVladimir Sementsov-Ogievskiy return bdrv_get_xdbg_block_graph(errp); 29705d3b4e99SVladimir Sementsov-Ogievskiy } 29715d3b4e99SVladimir Sementsov-Ogievskiy 29725b7bfe51SSergio Lopez void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp) 2973c29c1dd3SFam Zheng { 29745b7bfe51SSergio Lopez TransactionAction action = { 29755b7bfe51SSergio Lopez .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP, 29765b7bfe51SSergio Lopez .u.blockdev_backup.data = backup, 29775b7bfe51SSergio Lopez }; 29785b7bfe51SSergio Lopez blockdev_do_action(&action, errp); 297978f51fdeSJohn Snow } 298078f51fdeSJohn Snow 29814193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 29824193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 29834193cdd7SFam Zheng **/ 298471aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, 29854193cdd7SFam Zheng BlockDriverState *target, 298609158f00SBenoît Canet bool has_replaces, const char *replaces, 2987d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 2988274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 2989cdf3bc93SMax Reitz bool zero_target, 2990b952b558SPaolo Bonzini bool has_speed, int64_t speed, 2991eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 299208e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 29934193cdd7SFam Zheng bool has_on_source_error, 29944193cdd7SFam Zheng BlockdevOnError on_source_error, 29954193cdd7SFam Zheng bool has_on_target_error, 29964193cdd7SFam Zheng BlockdevOnError on_target_error, 29970fc9f8eaSFam Zheng bool has_unmap, bool unmap, 29986cdbceb1SKevin Wolf bool has_filter_node_name, 29996cdbceb1SKevin Wolf const char *filter_node_name, 3000481debaaSMax Reitz bool has_copy_mode, MirrorCopyMode copy_mode, 3001a6b58adeSJohn Snow bool has_auto_finalize, bool auto_finalize, 3002a6b58adeSJohn Snow bool has_auto_dismiss, bool auto_dismiss, 3003b952b558SPaolo Bonzini Error **errp) 3004d9b902dbSPaolo Bonzini { 30053f072a7fSMax Reitz BlockDriverState *unfiltered_bs; 3006a1999b33SJohn Snow int job_flags = JOB_DEFAULT; 3007d9b902dbSPaolo Bonzini 3008d9b902dbSPaolo Bonzini if (!has_speed) { 3009d9b902dbSPaolo Bonzini speed = 0; 3010d9b902dbSPaolo Bonzini } 3011b952b558SPaolo Bonzini if (!has_on_source_error) { 3012b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3013b952b558SPaolo Bonzini } 3014b952b558SPaolo Bonzini if (!has_on_target_error) { 3015b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3016b952b558SPaolo Bonzini } 3017eee13dfeSPaolo Bonzini if (!has_granularity) { 3018eee13dfeSPaolo Bonzini granularity = 0; 3019eee13dfeSPaolo Bonzini } 302008e4ed6cSPaolo Bonzini if (!has_buf_size) { 302148ac0a4dSWen Congyang buf_size = 0; 302208e4ed6cSPaolo Bonzini } 30230fc9f8eaSFam Zheng if (!has_unmap) { 30240fc9f8eaSFam Zheng unmap = true; 30250fc9f8eaSFam Zheng } 30266cdbceb1SKevin Wolf if (!has_filter_node_name) { 30276cdbceb1SKevin Wolf filter_node_name = NULL; 30286cdbceb1SKevin Wolf } 3029481debaaSMax Reitz if (!has_copy_mode) { 3030481debaaSMax Reitz copy_mode = MIRROR_COPY_MODE_BACKGROUND; 3031481debaaSMax Reitz } 3032a6b58adeSJohn Snow if (has_auto_finalize && !auto_finalize) { 3033a6b58adeSJohn Snow job_flags |= JOB_MANUAL_FINALIZE; 3034a6b58adeSJohn Snow } 3035a6b58adeSJohn Snow if (has_auto_dismiss && !auto_dismiss) { 3036a6b58adeSJohn Snow job_flags |= JOB_MANUAL_DISMISS; 3037a6b58adeSJohn Snow } 303808e4ed6cSPaolo Bonzini 3039eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3040c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 30413cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3042eee13dfeSPaolo Bonzini return; 3043eee13dfeSPaolo Bonzini } 3044eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3045c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 30466cc0667dSMarkus Armbruster "a power of 2"); 3047eee13dfeSPaolo Bonzini return; 3048eee13dfeSPaolo Bonzini } 3049d9b902dbSPaolo Bonzini 30504193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 30514193cdd7SFam Zheng return; 30524193cdd7SFam Zheng } 3053e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3054e40e5027SFam Zheng return; 3055e40e5027SFam Zheng } 30564193cdd7SFam Zheng 30573f072a7fSMax Reitz if (!bdrv_backing_chain_next(bs) && sync == MIRROR_SYNC_MODE_TOP) { 30584193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 30594193cdd7SFam Zheng } 30604193cdd7SFam Zheng 30613f072a7fSMax Reitz if (!has_replaces) { 30623f072a7fSMax Reitz /* We want to mirror from @bs, but keep implicit filters on top */ 30633f072a7fSMax Reitz unfiltered_bs = bdrv_skip_implicit_filters(bs); 30643f072a7fSMax Reitz if (unfiltered_bs != bs) { 30653f072a7fSMax Reitz replaces = unfiltered_bs->node_name; 30663f072a7fSMax Reitz has_replaces = true; 30673f072a7fSMax Reitz } 30683f072a7fSMax Reitz } 30693f072a7fSMax Reitz 307074ce9e46SMax Reitz if (has_replaces) { 307174ce9e46SMax Reitz BlockDriverState *to_replace_bs; 307274ce9e46SMax Reitz AioContext *replace_aio_context; 307374ce9e46SMax Reitz int64_t bs_size, replace_size; 307474ce9e46SMax Reitz 307574ce9e46SMax Reitz bs_size = bdrv_getlength(bs); 307674ce9e46SMax Reitz if (bs_size < 0) { 307774ce9e46SMax Reitz error_setg_errno(errp, -bs_size, "Failed to query device's size"); 307874ce9e46SMax Reitz return; 307974ce9e46SMax Reitz } 308074ce9e46SMax Reitz 308174ce9e46SMax Reitz to_replace_bs = check_to_replace_node(bs, replaces, errp); 308274ce9e46SMax Reitz if (!to_replace_bs) { 308374ce9e46SMax Reitz return; 308474ce9e46SMax Reitz } 308574ce9e46SMax Reitz 308674ce9e46SMax Reitz replace_aio_context = bdrv_get_aio_context(to_replace_bs); 308774ce9e46SMax Reitz aio_context_acquire(replace_aio_context); 308874ce9e46SMax Reitz replace_size = bdrv_getlength(to_replace_bs); 308974ce9e46SMax Reitz aio_context_release(replace_aio_context); 309074ce9e46SMax Reitz 309174ce9e46SMax Reitz if (replace_size < 0) { 309274ce9e46SMax Reitz error_setg_errno(errp, -replace_size, 309374ce9e46SMax Reitz "Failed to query the replacement node's size"); 309474ce9e46SMax Reitz return; 309574ce9e46SMax Reitz } 309674ce9e46SMax Reitz if (bs_size != replace_size) { 309774ce9e46SMax Reitz error_setg(errp, "cannot replace image with a mirror image of " 309874ce9e46SMax Reitz "different size"); 309974ce9e46SMax Reitz return; 310074ce9e46SMax Reitz } 310174ce9e46SMax Reitz } 310274ce9e46SMax Reitz 31034193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 31044193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 31054193cdd7SFam Zheng */ 310671aa9867SAlberto Garcia mirror_start(job_id, bs, target, 3107a1999b33SJohn Snow has_replaces ? replaces : NULL, job_flags, 3108cdf3bc93SMax Reitz speed, granularity, buf_size, sync, backing_mode, zero_target, 31096cdbceb1SKevin Wolf on_source_error, on_target_error, unmap, filter_node_name, 3110481debaaSMax Reitz copy_mode, errp); 31114193cdd7SFam Zheng } 31124193cdd7SFam Zheng 3113faecd40aSEric Blake void qmp_drive_mirror(DriveMirror *arg, Error **errp) 31144193cdd7SFam Zheng { 31154193cdd7SFam Zheng BlockDriverState *bs; 31163f072a7fSMax Reitz BlockDriverState *target_backing_bs, *target_bs; 31174193cdd7SFam Zheng AioContext *aio_context; 31183ea67e08SSergio Lopez AioContext *old_context; 3119274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 31204193cdd7SFam Zheng Error *local_err = NULL; 31214193cdd7SFam Zheng QDict *options = NULL; 31224193cdd7SFam Zheng int flags; 31234193cdd7SFam Zheng int64_t size; 3124faecd40aSEric Blake const char *format = arg->format; 3125cdf3bc93SMax Reitz bool zero_target; 31268ed7d42fSKevin Wolf int ret; 31274193cdd7SFam Zheng 31280524e93aSKevin Wolf bs = qmp_get_root_bs(arg->device, errp); 31290524e93aSKevin Wolf if (!bs) { 3130d9b902dbSPaolo Bonzini return; 3131d9b902dbSPaolo Bonzini } 3132d9b902dbSPaolo Bonzini 3133cb2af917SPaolo Bonzini /* Early check to avoid creating target */ 3134cb2af917SPaolo Bonzini if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 3135cb2af917SPaolo Bonzini return; 3136cb2af917SPaolo Bonzini } 3137cb2af917SPaolo Bonzini 31380524e93aSKevin Wolf aio_context = bdrv_get_aio_context(bs); 31395a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 31405a7e7a0bSStefan Hajnoczi 3141faecd40aSEric Blake if (!arg->has_mode) { 3142faecd40aSEric Blake arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 31434193cdd7SFam Zheng } 3144d9b902dbSPaolo Bonzini 3145faecd40aSEric Blake if (!arg->has_format) { 3146faecd40aSEric Blake format = (arg->mode == NEW_IMAGE_MODE_EXISTING 3147faecd40aSEric Blake ? NULL : bs->drv->format_name); 3148d9b902dbSPaolo Bonzini } 3149d9b902dbSPaolo Bonzini 315061de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 31513f072a7fSMax Reitz target_backing_bs = bdrv_cow_bs(bdrv_skip_filters(bs)); 31523f072a7fSMax Reitz if (!target_backing_bs && arg->sync == MIRROR_SYNC_MODE_TOP) { 3153faecd40aSEric Blake arg->sync = MIRROR_SYNC_MODE_FULL; 3154d9b902dbSPaolo Bonzini } 3155faecd40aSEric Blake if (arg->sync == MIRROR_SYNC_MODE_NONE) { 31563f072a7fSMax Reitz target_backing_bs = bs; 3157117e0c82SMax Reitz } 3158d9b902dbSPaolo Bonzini 3159ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3160ac3c5d83SStefan Hajnoczi if (size < 0) { 3161ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 31625a7e7a0bSStefan Hajnoczi goto out; 3163ac3c5d83SStefan Hajnoczi } 3164ac3c5d83SStefan Hajnoczi 3165faecd40aSEric Blake if (arg->has_replaces) { 3166faecd40aSEric Blake if (!arg->has_node_name) { 316709158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 316809158f00SBenoît Canet " named node of the graph"); 31695a7e7a0bSStefan Hajnoczi goto out; 317009158f00SBenoît Canet } 317109158f00SBenoît Canet } 317209158f00SBenoît Canet 3173faecd40aSEric Blake if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { 3174274fcceeSMax Reitz backing_mode = MIRROR_SOURCE_BACKING_CHAIN; 3175274fcceeSMax Reitz } else { 3176274fcceeSMax Reitz backing_mode = MIRROR_OPEN_BACKING_CHAIN; 3177274fcceeSMax Reitz } 3178274fcceeSMax Reitz 31792a32c6e8SJohn Snow /* Don't open backing image in create() */ 31802a32c6e8SJohn Snow flags |= BDRV_O_NO_BACKING; 31812a32c6e8SJohn Snow 31823f072a7fSMax Reitz if ((arg->sync == MIRROR_SYNC_MODE_FULL || !target_backing_bs) 3183faecd40aSEric Blake && arg->mode != NEW_IMAGE_MODE_EXISTING) 318414526864SMax Reitz { 3185d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3186e6641719SMax Reitz assert(format); 3187faecd40aSEric Blake bdrv_img_create(arg->target, format, 31889217283dSFam Zheng NULL, NULL, NULL, size, flags, false, &local_err); 3189d9b902dbSPaolo Bonzini } else { 31903f072a7fSMax Reitz /* Implicit filters should not appear in the filename */ 31913f072a7fSMax Reitz BlockDriverState *explicit_backing = 31923f072a7fSMax Reitz bdrv_skip_implicit_filters(target_backing_bs); 31933f072a7fSMax Reitz 3194faecd40aSEric Blake switch (arg->mode) { 3195d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3196d9b902dbSPaolo Bonzini break; 3197d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3198d9b902dbSPaolo Bonzini /* create new image with backing file */ 31993f072a7fSMax Reitz bdrv_refresh_filename(explicit_backing); 3200faecd40aSEric Blake bdrv_img_create(arg->target, format, 32013f072a7fSMax Reitz explicit_backing->filename, 32023f072a7fSMax Reitz explicit_backing->drv->format_name, 32039217283dSFam Zheng NULL, size, flags, false, &local_err); 3204d9b902dbSPaolo Bonzini break; 3205d9b902dbSPaolo Bonzini default: 3206d9b902dbSPaolo Bonzini abort(); 3207d9b902dbSPaolo Bonzini } 3208d9b902dbSPaolo Bonzini } 3209d9b902dbSPaolo Bonzini 321084d18f06SMarkus Armbruster if (local_err) { 3211cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 32125a7e7a0bSStefan Hajnoczi goto out; 3213d9b902dbSPaolo Bonzini } 3214d9b902dbSPaolo Bonzini 32154c828dc6SBenoît Canet options = qdict_new(); 3216faecd40aSEric Blake if (arg->has_node_name) { 321746f5ac20SEric Blake qdict_put_str(options, "node-name", arg->node_name); 32184c828dc6SBenoît Canet } 3219e6641719SMax Reitz if (format) { 322046f5ac20SEric Blake qdict_put_str(options, "driver", format); 3221e6641719SMax Reitz } 32224c828dc6SBenoît Canet 3223b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3224b812f671SPaolo Bonzini * file. 3225b812f671SPaolo Bonzini */ 32262a32c6e8SJohn Snow target_bs = bdrv_open(arg->target, NULL, options, flags, errp); 32275b363937SMax Reitz if (!target_bs) { 32285a7e7a0bSStefan Hajnoczi goto out; 3229d9b902dbSPaolo Bonzini } 3230d9b902dbSPaolo Bonzini 3231cdf3bc93SMax Reitz zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL && 3232cdf3bc93SMax Reitz (arg->mode == NEW_IMAGE_MODE_EXISTING || 3233cdf3bc93SMax Reitz !bdrv_has_zero_init(target_bs))); 3234cdf3bc93SMax Reitz 32353ea67e08SSergio Lopez 32363ea67e08SSergio Lopez /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ 32373ea67e08SSergio Lopez old_context = bdrv_get_aio_context(target_bs); 32383ea67e08SSergio Lopez aio_context_release(aio_context); 32393ea67e08SSergio Lopez aio_context_acquire(old_context); 32403ea67e08SSergio Lopez 32418ed7d42fSKevin Wolf ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); 32428ed7d42fSKevin Wolf if (ret < 0) { 32438ed7d42fSKevin Wolf bdrv_unref(target_bs); 32443ea67e08SSergio Lopez aio_context_release(old_context); 32453ea67e08SSergio Lopez return; 32468ed7d42fSKevin Wolf } 32475a7e7a0bSStefan Hajnoczi 32483ea67e08SSergio Lopez aio_context_release(old_context); 32493ea67e08SSergio Lopez aio_context_acquire(aio_context); 32503ea67e08SSergio Lopez 3251faecd40aSEric Blake blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, 3252faecd40aSEric Blake arg->has_replaces, arg->replaces, arg->sync, 3253cdf3bc93SMax Reitz backing_mode, zero_target, 3254cdf3bc93SMax Reitz arg->has_speed, arg->speed, 3255faecd40aSEric Blake arg->has_granularity, arg->granularity, 3256faecd40aSEric Blake arg->has_buf_size, arg->buf_size, 3257faecd40aSEric Blake arg->has_on_source_error, arg->on_source_error, 3258faecd40aSEric Blake arg->has_on_target_error, arg->on_target_error, 3259faecd40aSEric Blake arg->has_unmap, arg->unmap, 32606cdbceb1SKevin Wolf false, NULL, 3261481debaaSMax Reitz arg->has_copy_mode, arg->copy_mode, 3262a6b58adeSJohn Snow arg->has_auto_finalize, arg->auto_finalize, 3263a6b58adeSJohn Snow arg->has_auto_dismiss, arg->auto_dismiss, 3264992861fbSMarkus Armbruster errp); 3265e253f4b8SKevin Wolf bdrv_unref(target_bs); 32665a7e7a0bSStefan Hajnoczi out: 32675a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3268d9b902dbSPaolo Bonzini } 3269d9b902dbSPaolo Bonzini 327071aa9867SAlberto Garcia void qmp_blockdev_mirror(bool has_job_id, const char *job_id, 327171aa9867SAlberto Garcia const char *device, const char *target, 3272df92562eSFam Zheng bool has_replaces, const char *replaces, 3273df92562eSFam Zheng MirrorSyncMode sync, 3274df92562eSFam Zheng bool has_speed, int64_t speed, 3275df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3276df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3277df92562eSFam Zheng bool has_on_source_error, 3278df92562eSFam Zheng BlockdevOnError on_source_error, 3279df92562eSFam Zheng bool has_on_target_error, 3280df92562eSFam Zheng BlockdevOnError on_target_error, 32816cdbceb1SKevin Wolf bool has_filter_node_name, 32826cdbceb1SKevin Wolf const char *filter_node_name, 3283481debaaSMax Reitz bool has_copy_mode, MirrorCopyMode copy_mode, 3284a6b58adeSJohn Snow bool has_auto_finalize, bool auto_finalize, 3285a6b58adeSJohn Snow bool has_auto_dismiss, bool auto_dismiss, 3286df92562eSFam Zheng Error **errp) 3287df92562eSFam Zheng { 3288df92562eSFam Zheng BlockDriverState *bs; 3289df92562eSFam Zheng BlockDriverState *target_bs; 3290df92562eSFam Zheng AioContext *aio_context; 32913ea67e08SSergio Lopez AioContext *old_context; 3292274fcceeSMax Reitz BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; 3293cdf3bc93SMax Reitz bool zero_target; 32948ed7d42fSKevin Wolf int ret; 3295df92562eSFam Zheng 329607eec652SKevin Wolf bs = qmp_get_root_bs(device, errp); 3297df92562eSFam Zheng if (!bs) { 3298df92562eSFam Zheng return; 3299df92562eSFam Zheng } 3300df92562eSFam Zheng 3301df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3302df92562eSFam Zheng if (!target_bs) { 3303df92562eSFam Zheng return; 3304df92562eSFam Zheng } 3305df92562eSFam Zheng 3306cdf3bc93SMax Reitz zero_target = (sync == MIRROR_SYNC_MODE_FULL); 3307cdf3bc93SMax Reitz 33083ea67e08SSergio Lopez /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ 33093ea67e08SSergio Lopez old_context = bdrv_get_aio_context(target_bs); 3310df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 33113ea67e08SSergio Lopez aio_context_acquire(old_context); 3312df92562eSFam Zheng 33138ed7d42fSKevin Wolf ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); 33143ea67e08SSergio Lopez 33153ea67e08SSergio Lopez aio_context_release(old_context); 33163ea67e08SSergio Lopez aio_context_acquire(aio_context); 33173ea67e08SSergio Lopez 33188ed7d42fSKevin Wolf if (ret < 0) { 33198ed7d42fSKevin Wolf goto out; 33208ed7d42fSKevin Wolf } 3321df92562eSFam Zheng 332271aa9867SAlberto Garcia blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 3323274fcceeSMax Reitz has_replaces, replaces, sync, backing_mode, 3324cdf3bc93SMax Reitz zero_target, has_speed, speed, 3325df92562eSFam Zheng has_granularity, granularity, 3326df92562eSFam Zheng has_buf_size, buf_size, 3327df92562eSFam Zheng has_on_source_error, on_source_error, 3328df92562eSFam Zheng has_on_target_error, on_target_error, 3329df92562eSFam Zheng true, true, 33306cdbceb1SKevin Wolf has_filter_node_name, filter_node_name, 3331481debaaSMax Reitz has_copy_mode, copy_mode, 3332a6b58adeSJohn Snow has_auto_finalize, auto_finalize, 3333a6b58adeSJohn Snow has_auto_dismiss, auto_dismiss, 3334992861fbSMarkus Armbruster errp); 33358ed7d42fSKevin Wolf out: 3336df92562eSFam Zheng aio_context_release(aio_context); 3337df92562eSFam Zheng } 3338df92562eSFam Zheng 33393ddf3efeSAlberto Garcia /* Get a block job using its ID and acquire its AioContext */ 33403ddf3efeSAlberto Garcia static BlockJob *find_block_job(const char *id, AioContext **aio_context, 334124d6bffeSMarkus Armbruster Error **errp) 33422d47c6e9SStefan Hajnoczi { 33433ddf3efeSAlberto Garcia BlockJob *job; 33443ddf3efeSAlberto Garcia 33453ddf3efeSAlberto Garcia assert(id != NULL); 33462d47c6e9SStefan Hajnoczi 33475433c24fSMax Reitz *aio_context = NULL; 33485433c24fSMax Reitz 33493ddf3efeSAlberto Garcia job = block_job_get(id); 33503ddf3efeSAlberto Garcia 33513ddf3efeSAlberto Garcia if (!job) { 33523ddf3efeSAlberto Garcia error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 33533ddf3efeSAlberto Garcia "Block job '%s' not found", id); 33543ddf3efeSAlberto Garcia return NULL; 33552d47c6e9SStefan Hajnoczi } 33563d948cdfSStefan Hajnoczi 33573ddf3efeSAlberto Garcia *aio_context = blk_get_aio_context(job->blk); 33583d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 33593d948cdfSStefan Hajnoczi 33603ddf3efeSAlberto Garcia return job; 33612d47c6e9SStefan Hajnoczi } 33622d47c6e9SStefan Hajnoczi 3363882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 33642d47c6e9SStefan Hajnoczi { 33653d948cdfSStefan Hajnoczi AioContext *aio_context; 336624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 33672d47c6e9SStefan Hajnoczi 33682d47c6e9SStefan Hajnoczi if (!job) { 33692d47c6e9SStefan Hajnoczi return; 33702d47c6e9SStefan Hajnoczi } 33712d47c6e9SStefan Hajnoczi 3372882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 33733d948cdfSStefan Hajnoczi aio_context_release(aio_context); 33742d47c6e9SStefan Hajnoczi } 3375370521a1SStefan Hajnoczi 33766e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 33776e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 33786e37fb81SPaolo Bonzini { 33793d948cdfSStefan Hajnoczi AioContext *aio_context; 338024d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 33816e37fb81SPaolo Bonzini 33826e37fb81SPaolo Bonzini if (!job) { 33836e37fb81SPaolo Bonzini return; 33846e37fb81SPaolo Bonzini } 33853d948cdfSStefan Hajnoczi 33863d948cdfSStefan Hajnoczi if (!has_force) { 33873d948cdfSStefan Hajnoczi force = false; 33883d948cdfSStefan Hajnoczi } 33893d948cdfSStefan Hajnoczi 3390b15de828SKevin Wolf if (job_user_paused(&job->job) && !force) { 3391f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3392f231b88dSCole Robinson device); 33933d948cdfSStefan Hajnoczi goto out; 33946e37fb81SPaolo Bonzini } 33956e37fb81SPaolo Bonzini 33966e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 33973d70ff53SKevin Wolf job_user_cancel(&job->job, force, errp); 33983d948cdfSStefan Hajnoczi out: 33993d948cdfSStefan Hajnoczi aio_context_release(aio_context); 34006e37fb81SPaolo Bonzini } 34016e37fb81SPaolo Bonzini 34026e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3403370521a1SStefan Hajnoczi { 34043d948cdfSStefan Hajnoczi AioContext *aio_context; 340524d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3406370521a1SStefan Hajnoczi 34070ec4dfb8SJohn Snow if (!job) { 3408370521a1SStefan Hajnoczi return; 3409370521a1SStefan Hajnoczi } 34106e37fb81SPaolo Bonzini 34116e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 3412b15de828SKevin Wolf job_user_pause(&job->job, errp); 34133d948cdfSStefan Hajnoczi aio_context_release(aio_context); 34146e37fb81SPaolo Bonzini } 34156e37fb81SPaolo Bonzini 34166e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 34176e37fb81SPaolo Bonzini { 34183d948cdfSStefan Hajnoczi AioContext *aio_context; 341924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 34206e37fb81SPaolo Bonzini 34210ec4dfb8SJohn Snow if (!job) { 34228acc72a4SPaolo Bonzini return; 34238acc72a4SPaolo Bonzini } 3424370521a1SStefan Hajnoczi 34256e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 3426b15de828SKevin Wolf job_user_resume(&job->job, errp); 34273d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3428370521a1SStefan Hajnoczi } 3429fb5458cdSStefan Hajnoczi 3430aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3431aeae883bSPaolo Bonzini { 34323d948cdfSStefan Hajnoczi AioContext *aio_context; 343324d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3434aeae883bSPaolo Bonzini 3435aeae883bSPaolo Bonzini if (!job) { 3436aeae883bSPaolo Bonzini return; 3437aeae883bSPaolo Bonzini } 3438aeae883bSPaolo Bonzini 3439aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 34403453d972SKevin Wolf job_complete(&job->job, errp); 34413d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3442aeae883bSPaolo Bonzini } 3443aeae883bSPaolo Bonzini 344411b61fbcSJohn Snow void qmp_block_job_finalize(const char *id, Error **errp) 344511b61fbcSJohn Snow { 344611b61fbcSJohn Snow AioContext *aio_context; 344711b61fbcSJohn Snow BlockJob *job = find_block_job(id, &aio_context, errp); 344811b61fbcSJohn Snow 344911b61fbcSJohn Snow if (!job) { 345011b61fbcSJohn Snow return; 345111b61fbcSJohn Snow } 345211b61fbcSJohn Snow 345311b61fbcSJohn Snow trace_qmp_block_job_finalize(job); 3454b660a84bSStefan Reiter job_ref(&job->job); 34557eaa8fb5SKevin Wolf job_finalize(&job->job, errp); 3456b660a84bSStefan Reiter 3457b660a84bSStefan Reiter /* 3458b660a84bSStefan Reiter * Job's context might have changed via job_finalize (and job_txn_apply 3459b660a84bSStefan Reiter * automatically acquires the new one), so make sure we release the correct 3460b660a84bSStefan Reiter * one. 3461b660a84bSStefan Reiter */ 3462b660a84bSStefan Reiter aio_context = blk_get_aio_context(job->blk); 3463b660a84bSStefan Reiter job_unref(&job->job); 346411b61fbcSJohn Snow aio_context_release(aio_context); 346511b61fbcSJohn Snow } 346611b61fbcSJohn Snow 346775f71059SJohn Snow void qmp_block_job_dismiss(const char *id, Error **errp) 346875f71059SJohn Snow { 346975f71059SJohn Snow AioContext *aio_context; 34705f9a6a08SKevin Wolf BlockJob *bjob = find_block_job(id, &aio_context, errp); 34715f9a6a08SKevin Wolf Job *job; 347275f71059SJohn Snow 34735f9a6a08SKevin Wolf if (!bjob) { 347475f71059SJohn Snow return; 347575f71059SJohn Snow } 347675f71059SJohn Snow 34775f9a6a08SKevin Wolf trace_qmp_block_job_dismiss(bjob); 34785f9a6a08SKevin Wolf job = &bjob->job; 34795f9a6a08SKevin Wolf job_dismiss(&job, errp); 348075f71059SJohn Snow aio_context_release(aio_context); 348175f71059SJohn Snow } 348275f71059SJohn Snow 3483fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3484fa40e656SJeff Cody const char *image_node_name, 3485fa40e656SJeff Cody const char *backing_file, 3486fa40e656SJeff Cody Error **errp) 3487fa40e656SJeff Cody { 3488fa40e656SJeff Cody BlockDriverState *bs = NULL; 3489729962f6SStefan Hajnoczi AioContext *aio_context; 3490fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3491fa40e656SJeff Cody Error *local_err = NULL; 3492fa40e656SJeff Cody bool ro; 3493fa40e656SJeff Cody int ret; 3494fa40e656SJeff Cody 34957b5dca3fSKevin Wolf bs = qmp_get_root_bs(device, errp); 34967b5dca3fSKevin Wolf if (!bs) { 3497fa40e656SJeff Cody return; 3498fa40e656SJeff Cody } 3499fa40e656SJeff Cody 35007b5dca3fSKevin Wolf aio_context = bdrv_get_aio_context(bs); 3501729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3502729962f6SStefan Hajnoczi 3503fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3504fa40e656SJeff Cody if (local_err) { 3505fa40e656SJeff Cody error_propagate(errp, local_err); 3506729962f6SStefan Hajnoczi goto out; 3507fa40e656SJeff Cody } 3508fa40e656SJeff Cody 3509fa40e656SJeff Cody if (!image_bs) { 3510fa40e656SJeff Cody error_setg(errp, "image file not found"); 3511729962f6SStefan Hajnoczi goto out; 3512fa40e656SJeff Cody } 3513fa40e656SJeff Cody 3514fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3515fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3516fa40e656SJeff Cody "without a backing file"); 3517729962f6SStefan Hajnoczi goto out; 3518fa40e656SJeff Cody } 3519fa40e656SJeff Cody 3520fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3521fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3522fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3523729962f6SStefan Hajnoczi goto out; 3524fa40e656SJeff Cody } 3525fa40e656SJeff Cody 3526fa40e656SJeff Cody /* final sanity check */ 3527fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3528fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3529fa40e656SJeff Cody device); 3530729962f6SStefan Hajnoczi goto out; 3531fa40e656SJeff Cody } 3532fa40e656SJeff Cody 3533fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3534fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3535fa40e656SJeff Cody 3536fa40e656SJeff Cody if (ro) { 3537051a60f6SAlberto Garcia if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) { 3538729962f6SStefan Hajnoczi goto out; 3539fa40e656SJeff Cody } 3540fa40e656SJeff Cody } 3541fa40e656SJeff Cody 3542fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3543e54ee1b3SEric Blake image_bs->drv ? image_bs->drv->format_name : "", 3544e54ee1b3SEric Blake false); 3545fa40e656SJeff Cody 3546fa40e656SJeff Cody if (ret < 0) { 3547fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3548fa40e656SJeff Cody backing_file); 3549fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3550fa40e656SJeff Cody * appropriate */ 3551fa40e656SJeff Cody } 3552fa40e656SJeff Cody 3553fa40e656SJeff Cody if (ro) { 3554992861fbSMarkus Armbruster bdrv_reopen_set_read_only(image_bs, true, errp); 3555fa40e656SJeff Cody } 3556729962f6SStefan Hajnoczi 3557729962f6SStefan Hajnoczi out: 3558729962f6SStefan Hajnoczi aio_context_release(aio_context); 3559fa40e656SJeff Cody } 3560fa40e656SJeff Cody 3561d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3562d26c9a15SKevin Wolf { 3563be4b67bcSMax Reitz BlockDriverState *bs; 3564d26c9a15SKevin Wolf QObject *obj; 35657d5e199aSDaniel P. Berrange Visitor *v = qobject_output_visitor_new(&obj); 3566d26c9a15SKevin Wolf QDict *qdict; 3567d26c9a15SKevin Wolf 35681f584248SMarkus Armbruster visit_type_BlockdevOptions(v, NULL, &options, &error_abort); 35693b098d56SEric Blake visit_complete(v, &obj); 35707dc847ebSMax Reitz qdict = qobject_to(QDict, obj); 3571d26c9a15SKevin Wolf 3572d26c9a15SKevin Wolf qdict_flatten(qdict); 3573d26c9a15SKevin Wolf 3574be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 35759ec8873eSKevin Wolf error_setg(errp, "'node-name' must be specified for the root node"); 3576be4b67bcSMax Reitz goto fail; 3577be4b67bcSMax Reitz } 3578be4b67bcSMax Reitz 3579bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3580bd745e23SMax Reitz if (!bs) { 3581be4b67bcSMax Reitz goto fail; 3582be4b67bcSMax Reitz } 35839c4218e9SMax Reitz 358489802d5aSMaxim Levitsky bdrv_set_monitor_owned(bs); 3585be4b67bcSMax Reitz 3586d26c9a15SKevin Wolf fail: 35873b098d56SEric Blake visit_free(v); 3588d26c9a15SKevin Wolf } 3589d26c9a15SKevin Wolf 35901479c730SAlberto Garcia void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp) 35911479c730SAlberto Garcia { 35921479c730SAlberto Garcia BlockDriverState *bs; 35931479c730SAlberto Garcia AioContext *ctx; 35941479c730SAlberto Garcia QObject *obj; 35951479c730SAlberto Garcia Visitor *v = qobject_output_visitor_new(&obj); 35961479c730SAlberto Garcia BlockReopenQueue *queue; 35971479c730SAlberto Garcia QDict *qdict; 35981479c730SAlberto Garcia 35991479c730SAlberto Garcia /* Check for the selected node name */ 36001479c730SAlberto Garcia if (!options->has_node_name) { 36011479c730SAlberto Garcia error_setg(errp, "Node name not specified"); 36021479c730SAlberto Garcia goto fail; 36031479c730SAlberto Garcia } 36041479c730SAlberto Garcia 36051479c730SAlberto Garcia bs = bdrv_find_node(options->node_name); 36061479c730SAlberto Garcia if (!bs) { 36071479c730SAlberto Garcia error_setg(errp, "Cannot find node named '%s'", options->node_name); 36081479c730SAlberto Garcia goto fail; 36091479c730SAlberto Garcia } 36101479c730SAlberto Garcia 36111479c730SAlberto Garcia /* Put all options in a QDict and flatten it */ 36121f584248SMarkus Armbruster visit_type_BlockdevOptions(v, NULL, &options, &error_abort); 36131479c730SAlberto Garcia visit_complete(v, &obj); 36141479c730SAlberto Garcia qdict = qobject_to(QDict, obj); 36151479c730SAlberto Garcia 36161479c730SAlberto Garcia qdict_flatten(qdict); 36171479c730SAlberto Garcia 36181479c730SAlberto Garcia /* Perform the reopen operation */ 36191479c730SAlberto Garcia ctx = bdrv_get_aio_context(bs); 36201479c730SAlberto Garcia aio_context_acquire(ctx); 36211479c730SAlberto Garcia bdrv_subtree_drained_begin(bs); 36221479c730SAlberto Garcia queue = bdrv_reopen_queue(NULL, bs, qdict, false); 36231479c730SAlberto Garcia bdrv_reopen_multiple(queue, errp); 36241479c730SAlberto Garcia bdrv_subtree_drained_end(bs); 36251479c730SAlberto Garcia aio_context_release(ctx); 36261479c730SAlberto Garcia 36271479c730SAlberto Garcia fail: 36281479c730SAlberto Garcia visit_free(v); 36291479c730SAlberto Garcia } 36301479c730SAlberto Garcia 363179b7a77eSMarkus Armbruster void qmp_blockdev_del(const char *node_name, Error **errp) 363281b936aeSAlberto Garcia { 363381b936aeSAlberto Garcia AioContext *aio_context; 363481b936aeSAlberto Garcia BlockDriverState *bs; 363581b936aeSAlberto Garcia 363681b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 363781b936aeSAlberto Garcia if (!bs) { 363881b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 363981b936aeSAlberto Garcia return; 364081b936aeSAlberto Garcia } 36411f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 3642e467da7bSKevin Wolf error_setg(errp, "Node %s is in use", node_name); 364381b936aeSAlberto Garcia return; 364481b936aeSAlberto Garcia } 364581b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 364681b936aeSAlberto Garcia aio_context_acquire(aio_context); 364781b936aeSAlberto Garcia 364881b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 364981b936aeSAlberto Garcia goto out; 365081b936aeSAlberto Garcia } 365181b936aeSAlberto Garcia 365270537ed5SPaolo Bonzini if (!QTAILQ_IN_USE(bs, monitor_list)) { 36539c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 36549c4218e9SMax Reitz bs->node_name); 36559c4218e9SMax Reitz goto out; 36569c4218e9SMax Reitz } 36579c4218e9SMax Reitz 36589c4218e9SMax Reitz if (bs->refcnt > 1) { 365981b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 366081b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 366181b936aeSAlberto Garcia goto out; 366281b936aeSAlberto Garcia } 366381b936aeSAlberto Garcia 36649c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 366581b936aeSAlberto Garcia bdrv_unref(bs); 366681b936aeSAlberto Garcia 366781b936aeSAlberto Garcia out: 366881b936aeSAlberto Garcia aio_context_release(aio_context); 366981b936aeSAlberto Garcia } 367081b936aeSAlberto Garcia 36717f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 36727f821597SWen Congyang const char *child_name) 36737f821597SWen Congyang { 36747f821597SWen Congyang BdrvChild *child; 36757f821597SWen Congyang 36767f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 36777f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 36787f821597SWen Congyang return child; 36797f821597SWen Congyang } 36807f821597SWen Congyang } 36817f821597SWen Congyang 36827f821597SWen Congyang return NULL; 36837f821597SWen Congyang } 36847f821597SWen Congyang 36857f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 36867f821597SWen Congyang const char *child, bool has_node, 36877f821597SWen Congyang const char *node, Error **errp) 36887f821597SWen Congyang { 36897f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 36907f821597SWen Congyang BdrvChild *p_child; 36917f821597SWen Congyang 36927f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 36937f821597SWen Congyang if (!parent_bs) { 36947f821597SWen Congyang return; 36957f821597SWen Congyang } 36967f821597SWen Congyang 36977f821597SWen Congyang if (has_child == has_node) { 36987f821597SWen Congyang if (has_child) { 36997f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 37007f821597SWen Congyang } else { 37017f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 37027f821597SWen Congyang } 37037f821597SWen Congyang return; 37047f821597SWen Congyang } 37057f821597SWen Congyang 37067f821597SWen Congyang if (has_child) { 37077f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 37087f821597SWen Congyang if (!p_child) { 37097f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 37107f821597SWen Congyang parent, child); 37117f821597SWen Congyang return; 37127f821597SWen Congyang } 37137f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 37147f821597SWen Congyang } 37157f821597SWen Congyang 37167f821597SWen Congyang if (has_node) { 37177f821597SWen Congyang new_bs = bdrv_find_node(node); 37187f821597SWen Congyang if (!new_bs) { 37197f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 37207f821597SWen Congyang return; 37217f821597SWen Congyang } 37227f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 37237f821597SWen Congyang } 37247f821597SWen Congyang } 37257f821597SWen Congyang 3726fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 3727fb5458cdSStefan Hajnoczi { 3728*c3033fd3SEric Blake BlockJobInfoList *head = NULL, **tail = &head; 3729f0f55dedSAlberto Garcia BlockJob *job; 3730fea68bb6SMarkus Armbruster 3731f0f55dedSAlberto Garcia for (job = block_job_next(NULL); job; job = block_job_next(job)) { 3732*c3033fd3SEric Blake BlockJobInfo *value; 3733559b935fSJohn Snow AioContext *aio_context; 373469691e72SStefan Hajnoczi 3735559b935fSJohn Snow if (block_job_is_internal(job)) { 3736559b935fSJohn Snow continue; 3737559b935fSJohn Snow } 3738559b935fSJohn Snow aio_context = blk_get_aio_context(job->blk); 373969691e72SStefan Hajnoczi aio_context_acquire(aio_context); 3740*c3033fd3SEric Blake value = block_job_query(job, errp); 3741f0f55dedSAlberto Garcia aio_context_release(aio_context); 3742*c3033fd3SEric Blake if (!value) { 3743559b935fSJohn Snow qapi_free_BlockJobInfoList(head); 3744559b935fSJohn Snow return NULL; 3745559b935fSJohn Snow } 3746*c3033fd3SEric Blake QAPI_LIST_APPEND(tail, value); 3747fea68bb6SMarkus Armbruster } 374869691e72SStefan Hajnoczi 3749fea68bb6SMarkus Armbruster return head; 3750fb5458cdSStefan Hajnoczi } 37514d454574SPaolo Bonzini 3752ca00bbb1SStefan Hajnoczi void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread, 3753882e9b89SStefan Hajnoczi bool has_force, bool force, Error **errp) 3754ca00bbb1SStefan Hajnoczi { 3755ca00bbb1SStefan Hajnoczi AioContext *old_context; 3756ca00bbb1SStefan Hajnoczi AioContext *new_context; 3757ca00bbb1SStefan Hajnoczi BlockDriverState *bs; 3758ca00bbb1SStefan Hajnoczi 3759ca00bbb1SStefan Hajnoczi bs = bdrv_find_node(node_name); 3760ca00bbb1SStefan Hajnoczi if (!bs) { 3761ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find node %s", node_name); 3762ca00bbb1SStefan Hajnoczi return; 3763ca00bbb1SStefan Hajnoczi } 3764ca00bbb1SStefan Hajnoczi 3765882e9b89SStefan Hajnoczi /* Protects against accidents. */ 3766882e9b89SStefan Hajnoczi if (!(has_force && force) && bdrv_has_blk(bs)) { 3767882e9b89SStefan Hajnoczi error_setg(errp, "Node %s is associated with a BlockBackend and could " 3768882e9b89SStefan Hajnoczi "be in use (use force=true to override this check)", 3769882e9b89SStefan Hajnoczi node_name); 3770ca00bbb1SStefan Hajnoczi return; 3771ca00bbb1SStefan Hajnoczi } 3772ca00bbb1SStefan Hajnoczi 3773ca00bbb1SStefan Hajnoczi if (iothread->type == QTYPE_QSTRING) { 3774ca00bbb1SStefan Hajnoczi IOThread *obj = iothread_by_id(iothread->u.s); 3775ca00bbb1SStefan Hajnoczi if (!obj) { 3776ca00bbb1SStefan Hajnoczi error_setg(errp, "Cannot find iothread %s", iothread->u.s); 3777ca00bbb1SStefan Hajnoczi return; 3778ca00bbb1SStefan Hajnoczi } 3779ca00bbb1SStefan Hajnoczi 3780ca00bbb1SStefan Hajnoczi new_context = iothread_get_aio_context(obj); 3781ca00bbb1SStefan Hajnoczi } else { 3782ca00bbb1SStefan Hajnoczi new_context = qemu_get_aio_context(); 3783ca00bbb1SStefan Hajnoczi } 3784ca00bbb1SStefan Hajnoczi 3785ca00bbb1SStefan Hajnoczi old_context = bdrv_get_aio_context(bs); 3786ca00bbb1SStefan Hajnoczi aio_context_acquire(old_context); 3787ca00bbb1SStefan Hajnoczi 37888ed7d42fSKevin Wolf bdrv_try_set_aio_context(bs, new_context, errp); 3789ca00bbb1SStefan Hajnoczi 3790ca00bbb1SStefan Hajnoczi aio_context_release(old_context); 3791ca00bbb1SStefan Hajnoczi } 3792ca00bbb1SStefan Hajnoczi 37930006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 37944d454574SPaolo Bonzini .name = "drive", 37950006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 37964d454574SPaolo Bonzini .desc = { 37974d454574SPaolo Bonzini { 37984d454574SPaolo Bonzini .name = "snapshot", 37994d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 38004d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 38014d454574SPaolo Bonzini },{ 38024d454574SPaolo Bonzini .name = "aio", 38034d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 3804f80f2673SAarushi Mehta .help = "host AIO implementation (threads, native, io_uring)", 38054d454574SPaolo Bonzini },{ 3806e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 3807e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 3808e4b24b49SKevin Wolf .help = "Enable writeback mode", 3809e4b24b49SKevin Wolf },{ 38104d454574SPaolo Bonzini .name = "format", 38114d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 38124d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 38134d454574SPaolo Bonzini },{ 38144d454574SPaolo Bonzini .name = "rerror", 38154d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 38164d454574SPaolo Bonzini .help = "read error action", 38174d454574SPaolo Bonzini },{ 38184d454574SPaolo Bonzini .name = "werror", 38194d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 38204d454574SPaolo Bonzini .help = "write error action", 38214d454574SPaolo Bonzini },{ 38224e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 38234d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 38244d454574SPaolo Bonzini .help = "open drive file as read-only", 3825a2a7862cSPradeep Jagadeesh }, 3826a2a7862cSPradeep Jagadeesh 3827a2a7862cSPradeep Jagadeesh THROTTLE_OPTS, 3828a2a7862cSPradeep Jagadeesh 3829a2a7862cSPradeep Jagadeesh { 383076f4afb4SAlberto Garcia .name = "throttling.group", 383176f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 383276f4afb4SAlberto Garcia .help = "name of the block throttling group", 383376f4afb4SAlberto Garcia },{ 38344d454574SPaolo Bonzini .name = "copy-on-read", 38354d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 38364d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 3837465bee1dSPeter Lieven },{ 3838465bee1dSPeter Lieven .name = "detect-zeroes", 3839465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 3840465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 3841362e9299SAlberto Garcia },{ 3842362e9299SAlberto Garcia .name = "stats-account-invalid", 3843362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 3844362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 3845362e9299SAlberto Garcia "in the statistics", 3846362e9299SAlberto Garcia },{ 3847362e9299SAlberto Garcia .name = "stats-account-failed", 3848362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 3849362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 3850362e9299SAlberto Garcia "in the statistics", 38514d454574SPaolo Bonzini }, 38524d454574SPaolo Bonzini { /* end of list */ } 38534d454574SPaolo Bonzini }, 38544d454574SPaolo Bonzini }; 38550006383eSKevin Wolf 38560006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 38570006383eSKevin Wolf .name = "drive", 38580006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 38590006383eSKevin Wolf .desc = { 3860492fdc6fSKevin Wolf /* 3861492fdc6fSKevin Wolf * no elements => accept any params 3862492fdc6fSKevin Wolf * validation will happen later 3863492fdc6fSKevin Wolf */ 38640006383eSKevin Wolf { /* end of list */ } 38650006383eSKevin Wolf }, 38660006383eSKevin Wolf }; 3867