1666daa68SMarkus Armbruster /* 2666daa68SMarkus Armbruster * QEMU host block devices 3666daa68SMarkus Armbruster * 4666daa68SMarkus Armbruster * Copyright (c) 2003-2008 Fabrice Bellard 5666daa68SMarkus Armbruster * 6666daa68SMarkus Armbruster * This work is licensed under the terms of the GNU GPL, version 2 or 7666daa68SMarkus Armbruster * later. See the COPYING file in the top-level directory. 83618a094SMarkus Armbruster * 93618a094SMarkus Armbruster * This file incorporates work covered by the following copyright and 103618a094SMarkus Armbruster * permission notice: 113618a094SMarkus Armbruster * 123618a094SMarkus Armbruster * Copyright (c) 2003-2008 Fabrice Bellard 133618a094SMarkus Armbruster * 143618a094SMarkus Armbruster * Permission is hereby granted, free of charge, to any person obtaining a copy 153618a094SMarkus Armbruster * of this software and associated documentation files (the "Software"), to deal 163618a094SMarkus Armbruster * in the Software without restriction, including without limitation the rights 173618a094SMarkus Armbruster * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 183618a094SMarkus Armbruster * copies of the Software, and to permit persons to whom the Software is 193618a094SMarkus Armbruster * furnished to do so, subject to the following conditions: 203618a094SMarkus Armbruster * 213618a094SMarkus Armbruster * The above copyright notice and this permission notice shall be included in 223618a094SMarkus Armbruster * all copies or substantial portions of the Software. 233618a094SMarkus Armbruster * 243618a094SMarkus Armbruster * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 253618a094SMarkus Armbruster * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 263618a094SMarkus Armbruster * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 273618a094SMarkus Armbruster * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 283618a094SMarkus Armbruster * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 293618a094SMarkus Armbruster * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 303618a094SMarkus Armbruster * THE SOFTWARE. 31666daa68SMarkus Armbruster */ 32666daa68SMarkus Armbruster 33d38ea87aSPeter Maydell #include "qemu/osdep.h" 3426f54e9aSMarkus Armbruster #include "sysemu/block-backend.h" 359c17d615SPaolo Bonzini #include "sysemu/blockdev.h" 360d09e41aSPaolo Bonzini #include "hw/block/block.h" 37737e150eSPaolo Bonzini #include "block/blockjob.h" 3876f4afb4SAlberto Garcia #include "block/throttle-groups.h" 3983c9089eSPaolo Bonzini #include "monitor/monitor.h" 40d49b6836SMarkus Armbruster #include "qemu/error-report.h" 411de7afc9SPaolo Bonzini #include "qemu/option.h" 421de7afc9SPaolo Bonzini #include "qemu/config-file.h" 437b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h" 44d26c9a15SKevin Wolf #include "qapi-visit.h" 45cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 46b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h" 479e7dac7cSPeter Lieven #include "qapi/util.h" 489c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 49737e150eSPaolo Bonzini #include "block/block_int.h" 50a4dea8a9SLuiz Capitulino #include "qmp-commands.h" 510ab8ed18SDaniel P. Berrange #include "block/trace.h" 529c17d615SPaolo Bonzini #include "sysemu/arch_init.h" 53f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 54f348b6d1SVeronia Bahaa #include "qemu/help_option.h" 55a2a7862cSPradeep Jagadeesh #include "qemu/throttle-options.h" 56666daa68SMarkus Armbruster 579c4218e9SMax Reitz static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = 589c4218e9SMax Reitz QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); 599c4218e9SMax Reitz 60b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id, 61b33945cfSKevin Wolf bool force, Error **errp); 62bf18bee5SColin Lord 631960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 641960966dSMarkus Armbruster [IF_NONE] = "none", 651960966dSMarkus Armbruster [IF_IDE] = "ide", 661960966dSMarkus Armbruster [IF_SCSI] = "scsi", 671960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 681960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 691960966dSMarkus Armbruster [IF_MTD] = "mtd", 701960966dSMarkus Armbruster [IF_SD] = "sd", 711960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 721960966dSMarkus Armbruster [IF_XEN] = "xen", 731960966dSMarkus Armbruster }; 741960966dSMarkus Armbruster 7521dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = { 7627d6bf40SMarkus Armbruster /* 7727d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 7827d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 7927d6bf40SMarkus Armbruster * 80547cb157SWei Jiangang * All controllers used to implement if=T drives need to support 8127d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 8227d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 8327d6bf40SMarkus Armbruster * values. 8427d6bf40SMarkus Armbruster * 8527d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 8627d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 8727d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 8827d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 8927d6bf40SMarkus Armbruster */ 9027d6bf40SMarkus Armbruster [IF_IDE] = 2, 9127d6bf40SMarkus Armbruster [IF_SCSI] = 7, 921960966dSMarkus Armbruster }; 931960966dSMarkus Armbruster 9421dff8cfSJohn Snow /** 9521dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides 9621dff8cfSJohn Snow * of the default, global values. 9721dff8cfSJohn Snow */ 9821dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs) 9921dff8cfSJohn Snow { 10018e46a03SMarkus Armbruster BlockBackend *blk; 10121dff8cfSJohn Snow DriveInfo *dinfo; 10221dff8cfSJohn Snow 10321dff8cfSJohn Snow if (max_devs <= 0) { 10421dff8cfSJohn Snow return; 10521dff8cfSJohn Snow } 10621dff8cfSJohn Snow 10718e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 10818e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 10921dff8cfSJohn Snow if (dinfo->type == type) { 11021dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of" 11121dff8cfSJohn Snow " the %s interface, because a drive of that type has" 11221dff8cfSJohn Snow " already been added.\n", if_name[type]); 11321dff8cfSJohn Snow g_assert_not_reached(); 11421dff8cfSJohn Snow } 11521dff8cfSJohn Snow } 11621dff8cfSJohn Snow 11721dff8cfSJohn Snow if_max_devs[type] = max_devs; 11821dff8cfSJohn Snow } 11921dff8cfSJohn Snow 12014bafc54SMarkus Armbruster /* 12114bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 12214bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 12314bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 12414bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 12514bafc54SMarkus Armbruster * when deletion is actually safe. 12614bafc54SMarkus Armbruster */ 1274be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk) 12814bafc54SMarkus Armbruster { 12918e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 1304be74634SMarkus Armbruster BlockDriverState *bs = blk_bs(blk); 13191fddb0dSStefan Hajnoczi AioContext *aio_context; 13214bafc54SMarkus Armbruster 13326f8b3a8SMarkus Armbruster if (!dinfo) { 1342d246f01SKevin Wolf return; 1352d246f01SKevin Wolf } 1362d246f01SKevin Wolf 1375433c24fSMax Reitz if (bs) { 13891fddb0dSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 13991fddb0dSStefan Hajnoczi aio_context_acquire(aio_context); 14091fddb0dSStefan Hajnoczi 14112bde0eeSPaolo Bonzini if (bs->job) { 14212bde0eeSPaolo Bonzini block_job_cancel(bs->job); 14312bde0eeSPaolo Bonzini } 14491fddb0dSStefan Hajnoczi 14591fddb0dSStefan Hajnoczi aio_context_release(aio_context); 1465433c24fSMax Reitz } 14791fddb0dSStefan Hajnoczi 14814bafc54SMarkus Armbruster dinfo->auto_del = 1; 14914bafc54SMarkus Armbruster } 15014bafc54SMarkus Armbruster 1514be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk) 15214bafc54SMarkus Armbruster { 15318e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 15414bafc54SMarkus Armbruster 1550fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 156efaa7c4eSMax Reitz monitor_remove_blk(blk); 157b9fe8a7aSMarkus Armbruster blk_unref(blk); 15814bafc54SMarkus Armbruster } 15914bafc54SMarkus Armbruster } 16014bafc54SMarkus Armbruster 161d8f94e1bSJohn Snow /** 162d8f94e1bSJohn Snow * Returns the current mapping of how many units per bus 163d8f94e1bSJohn Snow * a particular interface can support. 164d8f94e1bSJohn Snow * 165d8f94e1bSJohn Snow * A positive integer indicates n units per bus. 166d8f94e1bSJohn Snow * 0 implies the mapping has not been established. 167d8f94e1bSJohn Snow * -1 indicates an invalid BlockInterfaceType was given. 168d8f94e1bSJohn Snow */ 169d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type) 170d8f94e1bSJohn Snow { 171d8f94e1bSJohn Snow if (type >= IF_IDE && type < IF_COUNT) { 172d8f94e1bSJohn Snow return if_max_devs[type]; 173d8f94e1bSJohn Snow } 174d8f94e1bSJohn Snow 175d8f94e1bSJohn Snow return -1; 176d8f94e1bSJohn Snow } 177d8f94e1bSJohn Snow 178505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 179505a7fb1SMarkus Armbruster { 180505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 181505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 182505a7fb1SMarkus Armbruster } 183505a7fb1SMarkus Armbruster 184505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 185505a7fb1SMarkus Armbruster { 186505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 187505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 188505a7fb1SMarkus Armbruster } 189505a7fb1SMarkus Armbruster 1902292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 1912292ddaeSMarkus Armbruster { 19270b94331SMarkus Armbruster return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 1932292ddaeSMarkus Armbruster } 1942292ddaeSMarkus Armbruster 1952292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 1965645b0f4SMarkus Armbruster const char *optstr) 197666daa68SMarkus Armbruster { 198666daa68SMarkus Armbruster QemuOpts *opts; 199666daa68SMarkus Armbruster 2002292ddaeSMarkus Armbruster opts = drive_def(optstr); 201666daa68SMarkus Armbruster if (!opts) { 202666daa68SMarkus Armbruster return NULL; 203666daa68SMarkus Armbruster } 2042292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 205f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort); 2062292ddaeSMarkus Armbruster } 2072292ddaeSMarkus Armbruster if (index >= 0) { 208a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort); 2092292ddaeSMarkus Armbruster } 210666daa68SMarkus Armbruster if (file) 211f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort); 212666daa68SMarkus Armbruster return opts; 213666daa68SMarkus Armbruster } 214666daa68SMarkus Armbruster 215666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 216666daa68SMarkus Armbruster { 21718e46a03SMarkus Armbruster BlockBackend *blk; 218666daa68SMarkus Armbruster DriveInfo *dinfo; 219666daa68SMarkus Armbruster 22018e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 22118e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 22218e46a03SMarkus Armbruster if (dinfo && dinfo->type == type 22318e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) { 224666daa68SMarkus Armbruster return dinfo; 225666daa68SMarkus Armbruster } 22618e46a03SMarkus Armbruster } 227666daa68SMarkus Armbruster 228666daa68SMarkus Armbruster return NULL; 229666daa68SMarkus Armbruster } 230666daa68SMarkus Armbruster 231720b8dc0SMarkus Armbruster void drive_check_orphaned(void) 232a66c9dc7SJohn Snow { 23318e46a03SMarkus Armbruster BlockBackend *blk; 234a66c9dc7SJohn Snow DriveInfo *dinfo; 235664cc623SMarkus Armbruster Location loc; 236720b8dc0SMarkus Armbruster bool orphans = false; 237a66c9dc7SJohn Snow 23818e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 23918e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 240a7f53e26SMarkus Armbruster if (!blk_get_attached_dev(blk) && !dinfo->is_default && 241a66c9dc7SJohn Snow dinfo->type != IF_NONE) { 242664cc623SMarkus Armbruster loc_push_none(&loc); 243664cc623SMarkus Armbruster qemu_opts_loc_restore(dinfo->opts); 244720b8dc0SMarkus Armbruster error_report("machine type does not support" 245664cc623SMarkus Armbruster " if=%s,bus=%d,unit=%d", 2465433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit); 247664cc623SMarkus Armbruster loc_pop(&loc); 248720b8dc0SMarkus Armbruster orphans = true; 249a66c9dc7SJohn Snow } 250a66c9dc7SJohn Snow } 251a66c9dc7SJohn Snow 252720b8dc0SMarkus Armbruster if (orphans) { 253720b8dc0SMarkus Armbruster exit(1); 254720b8dc0SMarkus Armbruster } 255a66c9dc7SJohn Snow } 256a66c9dc7SJohn Snow 257f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 258f1bd51acSMarkus Armbruster { 259f1bd51acSMarkus Armbruster return drive_get(type, 260f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 261f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 262f1bd51acSMarkus Armbruster } 263f1bd51acSMarkus Armbruster 264666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 265666daa68SMarkus Armbruster { 266666daa68SMarkus Armbruster int max_bus; 26718e46a03SMarkus Armbruster BlockBackend *blk; 268666daa68SMarkus Armbruster DriveInfo *dinfo; 269666daa68SMarkus Armbruster 270666daa68SMarkus Armbruster max_bus = -1; 27118e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 27218e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 27318e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 274666daa68SMarkus Armbruster max_bus = dinfo->bus; 275666daa68SMarkus Armbruster } 27618e46a03SMarkus Armbruster } 277666daa68SMarkus Armbruster return max_bus; 278666daa68SMarkus Armbruster } 279666daa68SMarkus Armbruster 28013839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 28113839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 28213839974SMarkus Armbruster appropriate bus. */ 28313839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 28413839974SMarkus Armbruster { 28513839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 28613839974SMarkus Armbruster 28713839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 28813839974SMarkus Armbruster } 28913839974SMarkus Armbruster 290666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 291666daa68SMarkus Armbruster { 292807105a7SMarkus Armbruster error_printf(" %s", name); 293666daa68SMarkus Armbruster } 294666daa68SMarkus Armbruster 295aa398a5cSStefan Hajnoczi typedef struct { 296aa398a5cSStefan Hajnoczi QEMUBH *bh; 297fa510ebfSFam Zheng BlockDriverState *bs; 298fa510ebfSFam Zheng } BDRVPutRefBH; 299aa398a5cSStefan Hajnoczi 300b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 301666daa68SMarkus Armbruster { 302666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 30392aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 304666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 30592aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 306666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 30792aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 308666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 30992aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 310666daa68SMarkus Armbruster } else { 311b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 312666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 313666daa68SMarkus Armbruster return -1; 314666daa68SMarkus Armbruster } 315666daa68SMarkus Armbruster } 316666daa68SMarkus Armbruster 31740119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 31840119effSAlberto Garcia Error **errp) 31940119effSAlberto Garcia { 32040119effSAlberto Garcia const QListEntry *entry; 32140119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 32240119effSAlberto Garcia switch (qobject_type(entry->value)) { 32340119effSAlberto Garcia 32440119effSAlberto Garcia case QTYPE_QSTRING: { 32540119effSAlberto Garcia unsigned long long length; 32640119effSAlberto Garcia const char *str = qstring_get_str(qobject_to_qstring(entry->value)); 32740119effSAlberto Garcia if (parse_uint_full(str, &length, 10) == 0 && 32840119effSAlberto Garcia length > 0 && length <= UINT_MAX) { 32940119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 33040119effSAlberto Garcia } else { 33140119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str); 33240119effSAlberto Garcia return false; 33340119effSAlberto Garcia } 33440119effSAlberto Garcia break; 33540119effSAlberto Garcia } 33640119effSAlberto Garcia 33740119effSAlberto Garcia case QTYPE_QINT: { 33840119effSAlberto Garcia int64_t length = qint_get_int(qobject_to_qint(entry->value)); 33940119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) { 34040119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 34140119effSAlberto Garcia } else { 34240119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length); 34340119effSAlberto Garcia return false; 34440119effSAlberto Garcia } 34540119effSAlberto Garcia break; 34640119effSAlberto Garcia } 34740119effSAlberto Garcia 34840119effSAlberto Garcia default: 34940119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid"); 35040119effSAlberto Garcia return false; 35140119effSAlberto Garcia } 35240119effSAlberto Garcia } 35340119effSAlberto Garcia return true; 35440119effSAlberto Garcia } 35540119effSAlberto Garcia 35633cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 35733cb7dc8SKevin Wolf 358fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 359fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 360fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 361fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 362fbf8175eSMax Reitz { 363fbf8175eSMax Reitz Error *local_error = NULL; 364fbf8175eSMax Reitz const char *aio; 365fbf8175eSMax Reitz 366fbf8175eSMax Reitz if (bdrv_flags) { 367fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 368fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 369fbf8175eSMax Reitz } 370fbf8175eSMax Reitz 371fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 372fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 373fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 374fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 375fbf8175eSMax Reitz /* this is the default */ 376fbf8175eSMax Reitz } else { 377fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 378fbf8175eSMax Reitz return; 379fbf8175eSMax Reitz } 380fbf8175eSMax Reitz } 381fbf8175eSMax Reitz } 382fbf8175eSMax Reitz 383fbf8175eSMax Reitz /* disk I/O throttling */ 384fbf8175eSMax Reitz if (throttling_group) { 385fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 386fbf8175eSMax Reitz } 387fbf8175eSMax Reitz 388fbf8175eSMax Reitz if (throttle_cfg) { 3891588ab5dSAlberto Garcia throttle_config_init(throttle_cfg); 390fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 391fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 392fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 393fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 394fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 395fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 396fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 397fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 398fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 399fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 400fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 401fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 402fbf8175eSMax Reitz 403fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 404fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 405fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 406fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 407fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 408fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 409fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 410fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 411fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 412fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 413fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 414fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 415fbf8175eSMax Reitz 4168a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = 4178a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); 4188a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = 4198a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); 4208a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = 4218a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); 4228a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = 4238a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); 4248a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = 4258a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); 4268a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = 4278a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); 4288a0fc18dSAlberto Garcia 429fbf8175eSMax Reitz throttle_cfg->op_size = 430fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 431fbf8175eSMax Reitz 432d5851089SAlberto Garcia if (!throttle_is_valid(throttle_cfg, errp)) { 433fbf8175eSMax Reitz return; 434fbf8175eSMax Reitz } 435fbf8175eSMax Reitz } 436fbf8175eSMax Reitz 437fbf8175eSMax Reitz if (detect_zeroes) { 438fbf8175eSMax Reitz *detect_zeroes = 439fbf8175eSMax Reitz qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, 440fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 4417fb1cf16SEric Blake BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX, 442fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 443fbf8175eSMax Reitz &local_error); 444fbf8175eSMax Reitz if (local_error) { 445fbf8175eSMax Reitz error_propagate(errp, local_error); 446fbf8175eSMax Reitz return; 447fbf8175eSMax Reitz } 448fbf8175eSMax Reitz } 449fbf8175eSMax Reitz } 450fbf8175eSMax Reitz 451f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 45218e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 453b681072dSKevin Wolf Error **errp) 454666daa68SMarkus Armbruster { 455666daa68SMarkus Armbruster const char *buf; 456666daa68SMarkus Armbruster int bdrv_flags = 0; 457666daa68SMarkus Armbruster int on_read_error, on_write_error; 458362e9299SAlberto Garcia bool account_invalid, account_failed; 459f87a0e29SAlberto Garcia bool writethrough, read_only; 46026f54e9aSMarkus Armbruster BlockBackend *blk; 461a0f1eab1SMarkus Armbruster BlockDriverState *bs; 462cc0681c4SBenoît Canet ThrottleConfig cfg; 463666daa68SMarkus Armbruster int snapshot = 0; 464c546194fSStefan Hajnoczi Error *error = NULL; 4650006383eSKevin Wolf QemuOpts *opts; 46640119effSAlberto Garcia QDict *interval_dict = NULL; 46740119effSAlberto Garcia QList *interval_list = NULL; 4680006383eSKevin Wolf const char *id; 469fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 470fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 471fbf8175eSMax Reitz const char *throttling_group = NULL; 472666daa68SMarkus Armbruster 473f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 474f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 475f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4760006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 47784d18f06SMarkus Armbruster if (error) { 478b681072dSKevin Wolf error_propagate(errp, error); 4796376f952SMarkus Armbruster goto err_no_opts; 4800006383eSKevin Wolf } 4810006383eSKevin Wolf 4820006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 48384d18f06SMarkus Armbruster if (error) { 484b681072dSKevin Wolf error_propagate(errp, error); 485ec9c10d2SStefan Hajnoczi goto early_err; 4860006383eSKevin Wolf } 4870006383eSKevin Wolf 4880006383eSKevin Wolf if (id) { 4890006383eSKevin Wolf qdict_del(bs_opts, "id"); 4900006383eSKevin Wolf } 4910006383eSKevin Wolf 492666daa68SMarkus Armbruster /* extract parameters */ 493666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 494666daa68SMarkus Armbruster 495362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 496362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 497362e9299SAlberto Garcia 498e4b24b49SKevin Wolf writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); 499e4b24b49SKevin Wolf 500ff356ee4SAlberto Garcia id = qemu_opts_id(opts); 501ff356ee4SAlberto Garcia 50240119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 50340119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 50440119effSAlberto Garcia 50540119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 50640119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 50740119effSAlberto Garcia qdict_first(interval_dict)->key); 50840119effSAlberto Garcia goto early_err; 50940119effSAlberto Garcia } 5102be5506fSAlberto Garcia 511fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 512fbf8175eSMax Reitz &detect_zeroes, &error); 513fbf8175eSMax Reitz if (error) { 514fbf8175eSMax Reitz error_propagate(errp, error); 515ec9c10d2SStefan Hajnoczi goto early_err; 516a9384affSPaolo Bonzini } 517666daa68SMarkus Armbruster 518666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 519c8057f95SPeter Maydell if (is_help_option(buf)) { 520807105a7SMarkus Armbruster error_printf("Supported formats:"); 521666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 522807105a7SMarkus Armbruster error_printf("\n"); 523ec9c10d2SStefan Hajnoczi goto early_err; 524666daa68SMarkus Armbruster } 52574fe54f2SKevin Wolf 526e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 527e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 528ec9c10d2SStefan Hajnoczi goto early_err; 5296db5f5d6SMike Qiu } 530*46f5ac20SEric Blake qdict_put_str(bs_opts, "driver", buf); 531666daa68SMarkus Armbruster } 532666daa68SMarkus Armbruster 53392aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 534666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 535b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 53684d18f06SMarkus Armbruster if (error) { 537b681072dSKevin Wolf error_propagate(errp, error); 538ec9c10d2SStefan Hajnoczi goto early_err; 539666daa68SMarkus Armbruster } 540666daa68SMarkus Armbruster } 541666daa68SMarkus Armbruster 54292aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 543666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 544b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 54584d18f06SMarkus Armbruster if (error) { 546b681072dSKevin Wolf error_propagate(errp, error); 547ec9c10d2SStefan Hajnoczi goto early_err; 548666daa68SMarkus Armbruster } 549666daa68SMarkus Armbruster } 550666daa68SMarkus Armbruster 551666daa68SMarkus Armbruster if (snapshot) { 55291a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 553666daa68SMarkus Armbruster } 554666daa68SMarkus Armbruster 555f87a0e29SAlberto Garcia read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false); 556f87a0e29SAlberto Garcia 5575ec18f8cSMax Reitz /* init */ 55839c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5595ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5605ec18f8cSMax Reitz 5616d0eb64dSKevin Wolf blk = blk_new(0, BLK_PERM_ALL); 5625ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5635ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 564f87a0e29SAlberto Garcia blk_rs->read_only = read_only; 5655ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5665ec18f8cSMax Reitz 5675ec18f8cSMax Reitz QDECREF(bs_opts); 5685ec18f8cSMax Reitz } else { 5695ec18f8cSMax Reitz if (file && !*file) { 5705ec18f8cSMax Reitz file = NULL; 5715ec18f8cSMax Reitz } 5725ec18f8cSMax Reitz 57391a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 57491a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 57591a097e7SKevin Wolf * Apply the defaults here instead. */ 57691a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 57791a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 578f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, 579f87a0e29SAlberto Garcia read_only ? "on" : "off"); 58072e775c7SKevin Wolf assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); 58191a097e7SKevin Wolf 58212d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 58312d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 58412d5ee3aSKevin Wolf } 58512d5ee3aSKevin Wolf 586efaa7c4eSMax Reitz blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); 587e4342ce5SMax Reitz if (!blk) { 588e4342ce5SMax Reitz goto err_no_bs_opts; 589e4342ce5SMax Reitz } 590e4342ce5SMax Reitz bs = blk_bs(blk); 5910006383eSKevin Wolf 592e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 593e4342ce5SMax Reitz 594a0f1eab1SMarkus Armbruster if (bdrv_key_required(bs)) { 595666daa68SMarkus Armbruster autostart = 0; 596a0f1eab1SMarkus Armbruster } 597362e9299SAlberto Garcia 598362e9299SAlberto Garcia block_acct_init(blk_get_stats(blk), account_invalid, account_failed); 5992be5506fSAlberto Garcia 60040119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6012be5506fSAlberto Garcia blk_unref(blk); 6022be5506fSAlberto Garcia blk = NULL; 6032be5506fSAlberto Garcia goto err_no_bs_opts; 6042be5506fSAlberto Garcia } 6052be5506fSAlberto Garcia } 6065ec18f8cSMax Reitz 6077ca7f0f6SKevin Wolf /* disk I/O throttling */ 6087ca7f0f6SKevin Wolf if (throttle_enabled(&cfg)) { 6097ca7f0f6SKevin Wolf if (!throttling_group) { 610ff356ee4SAlberto Garcia throttling_group = id; 6117ca7f0f6SKevin Wolf } 6127ca7f0f6SKevin Wolf blk_io_limits_enable(blk, throttling_group); 6137ca7f0f6SKevin Wolf blk_set_io_limits(blk, &cfg); 6147ca7f0f6SKevin Wolf } 6157ca7f0f6SKevin Wolf 616e4b24b49SKevin Wolf blk_set_enable_write_cache(blk, !writethrough); 6175ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6180006383eSKevin Wolf 619ff356ee4SAlberto Garcia if (!monitor_add_blk(blk, id, errp)) { 620efaa7c4eSMax Reitz blk_unref(blk); 621efaa7c4eSMax Reitz blk = NULL; 622efaa7c4eSMax Reitz goto err_no_bs_opts; 623efaa7c4eSMax Reitz } 624efaa7c4eSMax Reitz 625e4342ce5SMax Reitz err_no_bs_opts: 6260006383eSKevin Wolf qemu_opts_del(opts); 62740119effSAlberto Garcia QDECREF(interval_dict); 62840119effSAlberto Garcia QDECREF(interval_list); 62918e46a03SMarkus Armbruster return blk; 630a9ae2bffSMarkus Armbruster 631ec9c10d2SStefan Hajnoczi early_err: 632ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 63340119effSAlberto Garcia QDECREF(interval_dict); 63440119effSAlberto Garcia QDECREF(interval_list); 6356376f952SMarkus Armbruster err_no_opts: 6366376f952SMarkus Armbruster QDECREF(bs_opts); 637a9ae2bffSMarkus Armbruster return NULL; 638666daa68SMarkus Armbruster } 639666daa68SMarkus Armbruster 640bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 641bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 642bd745e23SMax Reitz { 643bd745e23SMax Reitz int bdrv_flags = 0; 644bd745e23SMax Reitz 645a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 646a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults. 647a81d6164SKevin Wolf * Apply the defaults here instead. */ 648a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 649a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 650f87a0e29SAlberto Garcia qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off"); 651a81d6164SKevin Wolf 65212d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 65312d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 65412d5ee3aSKevin Wolf } 65512d5ee3aSKevin Wolf 65674e1ae7cSKevin Wolf return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); 657bd745e23SMax Reitz } 658bd745e23SMax Reitz 6599c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void) 6609c4218e9SMax Reitz { 6619c4218e9SMax Reitz BlockDriverState *bs, *next_bs; 6629c4218e9SMax Reitz 6639c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { 6649c4218e9SMax Reitz AioContext *ctx = bdrv_get_aio_context(bs); 6659c4218e9SMax Reitz 6669c4218e9SMax Reitz aio_context_acquire(ctx); 6679c4218e9SMax Reitz bdrv_unref(bs); 6689c4218e9SMax Reitz aio_context_release(ctx); 6699c4218e9SMax Reitz } 6709c4218e9SMax Reitz } 6719c4218e9SMax Reitz 672262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */ 673262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) 674262b4e8fSMax Reitz { 675262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list) 676262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states); 677262b4e8fSMax Reitz } 678262b4e8fSMax Reitz 6795abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 6805abbf0eeSKevin Wolf Error **errp) 68157975222SKevin Wolf { 68257975222SKevin Wolf const char *value; 68357975222SKevin Wolf 68457975222SKevin Wolf value = qemu_opt_get(opts, from); 68557975222SKevin Wolf if (value) { 6865abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 6875abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 6885abbf0eeSKevin Wolf "same time", to, from); 6895abbf0eeSKevin Wolf return; 6905abbf0eeSKevin Wolf } 69120d6cd47SJun Li } 69220d6cd47SJun Li 69320d6cd47SJun Li /* rename all items in opts */ 69420d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 695f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 69657975222SKevin Wolf qemu_opt_unset(opts, from); 69757975222SKevin Wolf } 69857975222SKevin Wolf } 69957975222SKevin Wolf 70033cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 70133cb7dc8SKevin Wolf .name = "drive", 70233cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 70333cb7dc8SKevin Wolf .desc = { 70433cb7dc8SKevin Wolf { 70587a899c5SKevin Wolf .name = "bus", 70687a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 70787a899c5SKevin Wolf .help = "bus number", 70887a899c5SKevin Wolf },{ 70987a899c5SKevin Wolf .name = "unit", 71087a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 71187a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 71287a899c5SKevin Wolf },{ 71387a899c5SKevin Wolf .name = "index", 71487a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 71587a899c5SKevin Wolf .help = "index number", 71687a899c5SKevin Wolf },{ 71733cb7dc8SKevin Wolf .name = "media", 71833cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 71933cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 720593d464bSKevin Wolf },{ 721593d464bSKevin Wolf .name = "if", 722593d464bSKevin Wolf .type = QEMU_OPT_STRING, 723593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 724b41a7338SKevin Wolf },{ 725b41a7338SKevin Wolf .name = "cyls", 726b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 727b41a7338SKevin Wolf .help = "number of cylinders (ide disk geometry)", 728b41a7338SKevin Wolf },{ 729b41a7338SKevin Wolf .name = "heads", 730b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 731b41a7338SKevin Wolf .help = "number of heads (ide disk geometry)", 732b41a7338SKevin Wolf },{ 733b41a7338SKevin Wolf .name = "secs", 734b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 735b41a7338SKevin Wolf .help = "number of sectors (ide disk geometry)", 736b41a7338SKevin Wolf },{ 737b41a7338SKevin Wolf .name = "trans", 738b41a7338SKevin Wolf .type = QEMU_OPT_STRING, 739b41a7338SKevin Wolf .help = "chs translation (auto, lba, none)", 74026929298SKevin Wolf },{ 74126929298SKevin Wolf .name = "boot", 74226929298SKevin Wolf .type = QEMU_OPT_BOOL, 74326929298SKevin Wolf .help = "(deprecated, ignored)", 744394c7d4dSKevin Wolf },{ 745394c7d4dSKevin Wolf .name = "addr", 746394c7d4dSKevin Wolf .type = QEMU_OPT_STRING, 747394c7d4dSKevin Wolf .help = "pci address (virtio only)", 748d095b465SMax Reitz },{ 749bcf83158SKevin Wolf .name = "serial", 750bcf83158SKevin Wolf .type = QEMU_OPT_STRING, 751bcf83158SKevin Wolf .help = "disk serial number", 752bcf83158SKevin Wolf },{ 753d095b465SMax Reitz .name = "file", 754d095b465SMax Reitz .type = QEMU_OPT_STRING, 755d095b465SMax Reitz .help = "file name", 75633cb7dc8SKevin Wolf }, 7570ebd24e0SKevin Wolf 7580ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 7590ebd24e0SKevin Wolf { 7604e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 7610ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7620ebd24e0SKevin Wolf .help = "open drive file as read-only", 7630ebd24e0SKevin Wolf },{ 764ee13ed1cSKevin Wolf .name = "rerror", 765ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 766ee13ed1cSKevin Wolf .help = "read error action", 767ee13ed1cSKevin Wolf },{ 768ee13ed1cSKevin Wolf .name = "werror", 769ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 770ee13ed1cSKevin Wolf .help = "write error action", 771ee13ed1cSKevin Wolf },{ 7720ebd24e0SKevin Wolf .name = "copy-on-read", 7730ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7740ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 7750ebd24e0SKevin Wolf }, 7760ebd24e0SKevin Wolf 77733cb7dc8SKevin Wolf { /* end of list */ } 77833cb7dc8SKevin Wolf }, 77933cb7dc8SKevin Wolf }; 78033cb7dc8SKevin Wolf 78160e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 78257975222SKevin Wolf { 78329c4e2b5SKevin Wolf const char *value; 78418e46a03SMarkus Armbruster BlockBackend *blk; 78533cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 786f298d071SKevin Wolf QDict *bs_opts; 78733cb7dc8SKevin Wolf QemuOpts *legacy_opts; 78833cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 789593d464bSKevin Wolf BlockInterfaceType type; 790b41a7338SKevin Wolf int cyls, heads, secs, translation; 79187a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 792394c7d4dSKevin Wolf const char *devaddr; 793ee13ed1cSKevin Wolf const char *werror, *rerror; 794a7fdbcf0SFam Zheng bool read_only = false; 795a7fdbcf0SFam Zheng bool copy_on_read; 796bcf83158SKevin Wolf const char *serial; 797d095b465SMax Reitz const char *filename; 79833cb7dc8SKevin Wolf Error *local_err = NULL; 799247147fbSKevin Wolf int i; 80029c4e2b5SKevin Wolf 80157975222SKevin Wolf /* Change legacy command line options into QMP ones */ 802247147fbSKevin Wolf static const struct { 803247147fbSKevin Wolf const char *from; 804247147fbSKevin Wolf const char *to; 805247147fbSKevin Wolf } opt_renames[] = { 806247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 807247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 808247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 80957975222SKevin Wolf 810247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 811247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 812247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 81357975222SKevin Wolf 814247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 815247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 816247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8173e9fab69SBenoît Canet 818247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 819247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 820247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8213e9fab69SBenoît Canet 822247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8232024c1dfSBenoît Canet 82476f4afb4SAlberto Garcia { "group", "throttling.group" }, 82576f4afb4SAlberto Garcia 8264e200cf8SAlberto Garcia { "readonly", BDRV_OPT_READ_ONLY }, 827247147fbSKevin Wolf }; 828247147fbSKevin Wolf 829247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8305abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8315abbf0eeSKevin Wolf &local_err); 8325abbf0eeSKevin Wolf if (local_err) { 833565f65d2SMarkus Armbruster error_report_err(local_err); 8345abbf0eeSKevin Wolf return NULL; 8355abbf0eeSKevin Wolf } 836247147fbSKevin Wolf } 8370f227a94SKevin Wolf 83829c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 83929c4e2b5SKevin Wolf if (value) { 84029c4e2b5SKevin Wolf int flags = 0; 84104feb4a5SKevin Wolf bool writethrough; 84229c4e2b5SKevin Wolf 84304feb4a5SKevin Wolf if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { 84429c4e2b5SKevin Wolf error_report("invalid cache option"); 84529c4e2b5SKevin Wolf return NULL; 84629c4e2b5SKevin Wolf } 84729c4e2b5SKevin Wolf 84829c4e2b5SKevin Wolf /* Specific options take precedence */ 84954861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 85054861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 85104feb4a5SKevin Wolf !writethrough, &error_abort); 85229c4e2b5SKevin Wolf } 85354861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 85454861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 855cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 85629c4e2b5SKevin Wolf } 85754861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 85854861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 859cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 86029c4e2b5SKevin Wolf } 86129c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 86229c4e2b5SKevin Wolf } 86329c4e2b5SKevin Wolf 864f298d071SKevin Wolf /* Get a QDict for processing the options */ 865f298d071SKevin Wolf bs_opts = qdict_new(); 866f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 867f298d071SKevin Wolf 86887ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 86987ea75d5SPeter Crosthwaite &error_abort); 87033cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 87184d18f06SMarkus Armbruster if (local_err) { 872565f65d2SMarkus Armbruster error_report_err(local_err); 87333cb7dc8SKevin Wolf goto fail; 87433cb7dc8SKevin Wolf } 87533cb7dc8SKevin Wolf 87626929298SKevin Wolf /* Deprecated option boot=[on|off] */ 87726929298SKevin Wolf if (qemu_opt_get(legacy_opts, "boot") != NULL) { 87826929298SKevin Wolf fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 87926929298SKevin Wolf "ignored. Future versions will reject this parameter. Please " 88026929298SKevin Wolf "update your scripts.\n"); 88126929298SKevin Wolf } 88226929298SKevin Wolf 88333cb7dc8SKevin Wolf /* Media type */ 88433cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 88533cb7dc8SKevin Wolf if (value) { 88633cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 88733cb7dc8SKevin Wolf media = MEDIA_DISK; 88833cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 88933cb7dc8SKevin Wolf media = MEDIA_CDROM; 890a7fdbcf0SFam Zheng read_only = true; 89133cb7dc8SKevin Wolf } else { 89233cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 89333cb7dc8SKevin Wolf goto fail; 89433cb7dc8SKevin Wolf } 89533cb7dc8SKevin Wolf } 89633cb7dc8SKevin Wolf 8970ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 8984e200cf8SAlberto Garcia read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false); 8990ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 9000ebd24e0SKevin Wolf 9010ebd24e0SKevin Wolf if (read_only && copy_on_read) { 9020ebd24e0SKevin Wolf error_report("warning: disabling copy-on-read on read-only drive"); 9030ebd24e0SKevin Wolf copy_on_read = false; 9040ebd24e0SKevin Wolf } 9050ebd24e0SKevin Wolf 906*46f5ac20SEric Blake qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off"); 907*46f5ac20SEric Blake qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off"); 9080ebd24e0SKevin Wolf 909593d464bSKevin Wolf /* Controller type */ 910593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 911593d464bSKevin Wolf if (value) { 912593d464bSKevin Wolf for (type = 0; 913593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 914593d464bSKevin Wolf type++) { 915593d464bSKevin Wolf } 916593d464bSKevin Wolf if (type == IF_COUNT) { 917593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 918593d464bSKevin Wolf goto fail; 919593d464bSKevin Wolf } 920593d464bSKevin Wolf } else { 921593d464bSKevin Wolf type = block_default_type; 922593d464bSKevin Wolf } 923593d464bSKevin Wolf 924b41a7338SKevin Wolf /* Geometry */ 925b41a7338SKevin Wolf cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 926b41a7338SKevin Wolf heads = qemu_opt_get_number(legacy_opts, "heads", 0); 927b41a7338SKevin Wolf secs = qemu_opt_get_number(legacy_opts, "secs", 0); 928b41a7338SKevin Wolf 929b41a7338SKevin Wolf if (cyls || heads || secs) { 930b41a7338SKevin Wolf if (cyls < 1) { 931b41a7338SKevin Wolf error_report("invalid physical cyls number"); 932b41a7338SKevin Wolf goto fail; 933b41a7338SKevin Wolf } 934b41a7338SKevin Wolf if (heads < 1) { 935b41a7338SKevin Wolf error_report("invalid physical heads number"); 936b41a7338SKevin Wolf goto fail; 937b41a7338SKevin Wolf } 938b41a7338SKevin Wolf if (secs < 1) { 939b41a7338SKevin Wolf error_report("invalid physical secs number"); 940b41a7338SKevin Wolf goto fail; 941b41a7338SKevin Wolf } 942b41a7338SKevin Wolf } 943b41a7338SKevin Wolf 944b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 945b41a7338SKevin Wolf value = qemu_opt_get(legacy_opts, "trans"); 946b41a7338SKevin Wolf if (value != NULL) { 947b41a7338SKevin Wolf if (!cyls) { 948b41a7338SKevin Wolf error_report("'%s' trans must be used with cyls, heads and secs", 949b41a7338SKevin Wolf value); 950b41a7338SKevin Wolf goto fail; 951b41a7338SKevin Wolf } 952b41a7338SKevin Wolf if (!strcmp(value, "none")) { 953b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_NONE; 954b41a7338SKevin Wolf } else if (!strcmp(value, "lba")) { 955b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_LBA; 956f31c41ffSPaolo Bonzini } else if (!strcmp(value, "large")) { 957f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_LARGE; 958f31c41ffSPaolo Bonzini } else if (!strcmp(value, "rechs")) { 959f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_RECHS; 960b41a7338SKevin Wolf } else if (!strcmp(value, "auto")) { 961b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 962b41a7338SKevin Wolf } else { 963b41a7338SKevin Wolf error_report("'%s' invalid translation type", value); 964b41a7338SKevin Wolf goto fail; 965b41a7338SKevin Wolf } 966b41a7338SKevin Wolf } 967b41a7338SKevin Wolf 968b41a7338SKevin Wolf if (media == MEDIA_CDROM) { 969b41a7338SKevin Wolf if (cyls || secs || heads) { 970b41a7338SKevin Wolf error_report("CHS can't be set with media=cdrom"); 971b41a7338SKevin Wolf goto fail; 972b41a7338SKevin Wolf } 973b41a7338SKevin Wolf } 974b41a7338SKevin Wolf 97587a899c5SKevin Wolf /* Device address specified by bus/unit or index. 97687a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 97787a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 97887a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 97987a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 98087a899c5SKevin Wolf 98187a899c5SKevin Wolf max_devs = if_max_devs[type]; 98287a899c5SKevin Wolf 98387a899c5SKevin Wolf if (index != -1) { 98487a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 98587a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 98687a899c5SKevin Wolf goto fail; 98787a899c5SKevin Wolf } 98887a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 98987a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 99087a899c5SKevin Wolf } 99187a899c5SKevin Wolf 99287a899c5SKevin Wolf if (unit_id == -1) { 99387a899c5SKevin Wolf unit_id = 0; 99487a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 99587a899c5SKevin Wolf unit_id++; 99687a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 99787a899c5SKevin Wolf unit_id -= max_devs; 99887a899c5SKevin Wolf bus_id++; 99987a899c5SKevin Wolf } 100087a899c5SKevin Wolf } 100187a899c5SKevin Wolf } 100287a899c5SKevin Wolf 100387a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 100487a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 100587a899c5SKevin Wolf goto fail; 100687a899c5SKevin Wolf } 100787a899c5SKevin Wolf 100887a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 100987a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 101087a899c5SKevin Wolf bus_id, unit_id, index); 101187a899c5SKevin Wolf goto fail; 101287a899c5SKevin Wolf } 101387a899c5SKevin Wolf 1014bcf83158SKevin Wolf /* Serial number */ 1015bcf83158SKevin Wolf serial = qemu_opt_get(legacy_opts, "serial"); 1016bcf83158SKevin Wolf 101787a899c5SKevin Wolf /* no id supplied -> create one */ 101887a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 101987a899c5SKevin Wolf char *new_id; 102087a899c5SKevin Wolf const char *mediastr = ""; 102187a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 102287a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 102387a899c5SKevin Wolf } 102487a899c5SKevin Wolf if (max_devs) { 102587a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 102687a899c5SKevin Wolf mediastr, unit_id); 102787a899c5SKevin Wolf } else { 102887a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 102987a899c5SKevin Wolf mediastr, unit_id); 103087a899c5SKevin Wolf } 1031*46f5ac20SEric Blake qdict_put_str(bs_opts, "id", new_id); 103287a899c5SKevin Wolf g_free(new_id); 103387a899c5SKevin Wolf } 103487a899c5SKevin Wolf 1035394c7d4dSKevin Wolf /* Add virtio block device */ 1036394c7d4dSKevin Wolf devaddr = qemu_opt_get(legacy_opts, "addr"); 1037394c7d4dSKevin Wolf if (devaddr && type != IF_VIRTIO) { 1038394c7d4dSKevin Wolf error_report("addr is not supported by this bus type"); 1039394c7d4dSKevin Wolf goto fail; 1040394c7d4dSKevin Wolf } 1041394c7d4dSKevin Wolf 1042394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 1043394c7d4dSKevin Wolf QemuOpts *devopts; 104487ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 104587ea75d5SPeter Crosthwaite &error_abort); 1046394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 10471f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1048394c7d4dSKevin Wolf } else { 1049f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1050394c7d4dSKevin Wolf } 1051f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1052f43e47dbSMarkus Armbruster &error_abort); 1053394c7d4dSKevin Wolf if (devaddr) { 1054f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1055394c7d4dSKevin Wolf } 1056394c7d4dSKevin Wolf } 1057394c7d4dSKevin Wolf 1058d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 1059d095b465SMax Reitz 1060ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 1061ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 1062ee13ed1cSKevin Wolf if (werror != NULL) { 1063ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1064ee13ed1cSKevin Wolf type != IF_NONE) { 1065ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 1066ee13ed1cSKevin Wolf goto fail; 1067ee13ed1cSKevin Wolf } 1068*46f5ac20SEric Blake qdict_put_str(bs_opts, "werror", werror); 1069ee13ed1cSKevin Wolf } 1070ee13ed1cSKevin Wolf 1071ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 1072ee13ed1cSKevin Wolf if (rerror != NULL) { 1073ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1074ee13ed1cSKevin Wolf type != IF_NONE) { 1075ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 1076ee13ed1cSKevin Wolf goto fail; 1077ee13ed1cSKevin Wolf } 1078*46f5ac20SEric Blake qdict_put_str(bs_opts, "rerror", rerror); 1079ee13ed1cSKevin Wolf } 1080ee13ed1cSKevin Wolf 10812d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 108218e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 10833cb0e25cSMarkus Armbruster bs_opts = NULL; 108418e46a03SMarkus Armbruster if (!blk) { 108584d18f06SMarkus Armbruster if (local_err) { 1086565f65d2SMarkus Armbruster error_report_err(local_err); 1087b681072dSKevin Wolf } 10882d246f01SKevin Wolf goto fail; 1089b681072dSKevin Wolf } else { 109084d18f06SMarkus Armbruster assert(!local_err); 10912d246f01SKevin Wolf } 10922d246f01SKevin Wolf 109326f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 109426f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1095f298d071SKevin Wolf dinfo->opts = all_opts; 10962d246f01SKevin Wolf 1097b41a7338SKevin Wolf dinfo->cyls = cyls; 1098b41a7338SKevin Wolf dinfo->heads = heads; 1099b41a7338SKevin Wolf dinfo->secs = secs; 1100b41a7338SKevin Wolf dinfo->trans = translation; 1101b41a7338SKevin Wolf 1102ee13ed1cSKevin Wolf dinfo->type = type; 110387a899c5SKevin Wolf dinfo->bus = bus_id; 110487a899c5SKevin Wolf dinfo->unit = unit_id; 1105394c7d4dSKevin Wolf dinfo->devaddr = devaddr; 1106bcf83158SKevin Wolf dinfo->serial = g_strdup(serial); 1107bcf83158SKevin Wolf 110826f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 110926f8b3a8SMarkus Armbruster 1110e34ef046SKevin Wolf switch(type) { 1111e34ef046SKevin Wolf case IF_IDE: 1112e34ef046SKevin Wolf case IF_SCSI: 1113e34ef046SKevin Wolf case IF_XEN: 1114e34ef046SKevin Wolf case IF_NONE: 1115e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1116e34ef046SKevin Wolf break; 1117e34ef046SKevin Wolf default: 1118e34ef046SKevin Wolf break; 1119e34ef046SKevin Wolf } 1120e34ef046SKevin Wolf 11212d246f01SKevin Wolf fail: 112233cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 11233cb0e25cSMarkus Armbruster QDECREF(bs_opts); 11242d246f01SKevin Wolf return dinfo; 112557975222SKevin Wolf } 112657975222SKevin Wolf 1127b6c1bae5SKevin Wolf static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) 1128b6c1bae5SKevin Wolf { 1129b6c1bae5SKevin Wolf BlockDriverState *bs; 1130b6c1bae5SKevin Wolf 1131b6c1bae5SKevin Wolf bs = bdrv_lookup_bs(name, name, errp); 1132b6c1bae5SKevin Wolf if (bs == NULL) { 1133b6c1bae5SKevin Wolf return NULL; 1134b6c1bae5SKevin Wolf } 1135b6c1bae5SKevin Wolf 1136b6c1bae5SKevin Wolf if (!bdrv_is_root_node(bs)) { 1137b6c1bae5SKevin Wolf error_setg(errp, "Need a root block node"); 1138b6c1bae5SKevin Wolf return NULL; 1139b6c1bae5SKevin Wolf } 1140b6c1bae5SKevin Wolf 1141b6c1bae5SKevin Wolf if (!bdrv_is_inserted(bs)) { 1142b6c1bae5SKevin Wolf error_setg(errp, "Device has no medium"); 1143b6c1bae5SKevin Wolf return NULL; 1144b6c1bae5SKevin Wolf } 1145b6c1bae5SKevin Wolf 1146b6c1bae5SKevin Wolf return bs; 1147b6c1bae5SKevin Wolf } 1148b6c1bae5SKevin Wolf 1149b33945cfSKevin Wolf static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id, 1150b33945cfSKevin Wolf Error **errp) 1151b33945cfSKevin Wolf { 1152b33945cfSKevin Wolf BlockBackend *blk; 1153b33945cfSKevin Wolf 1154b33945cfSKevin Wolf if (!blk_name == !qdev_id) { 1155b33945cfSKevin Wolf error_setg(errp, "Need exactly one of 'device' and 'id'"); 1156b33945cfSKevin Wolf return NULL; 1157b33945cfSKevin Wolf } 1158b33945cfSKevin Wolf 1159b33945cfSKevin Wolf if (qdev_id) { 1160b33945cfSKevin Wolf blk = blk_by_qdev_id(qdev_id, errp); 1161b33945cfSKevin Wolf } else { 1162b33945cfSKevin Wolf blk = blk_by_name(blk_name); 1163b33945cfSKevin Wolf if (blk == NULL) { 1164b33945cfSKevin Wolf error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1165b33945cfSKevin Wolf "Device '%s' not found", blk_name); 1166b33945cfSKevin Wolf } 1167b33945cfSKevin Wolf } 1168b33945cfSKevin Wolf 1169b33945cfSKevin Wolf return blk; 1170b33945cfSKevin Wolf } 1171b33945cfSKevin Wolf 11723e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1173666daa68SMarkus Armbruster { 1174666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1175a0e8544cSFam Zheng BlockBackend *blk; 11762d3735d3SStefan Hajnoczi int ret; 11772d3735d3SStefan Hajnoczi 1178e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1179da31d594SMax Reitz ret = blk_commit_all(); 1180e8877497SStefan Hajnoczi } else { 118184aa0140SStefan Hajnoczi BlockDriverState *bs; 118284aa0140SStefan Hajnoczi AioContext *aio_context; 118384aa0140SStefan Hajnoczi 1184a0e8544cSFam Zheng blk = blk_by_name(device); 1185a0e8544cSFam Zheng if (!blk) { 118658513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1187ac59eb95SMarkus Armbruster return; 11886ab4b5abSMarkus Armbruster } 11895433c24fSMax Reitz if (!blk_is_available(blk)) { 11905433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 11915433c24fSMax Reitz return; 11925433c24fSMax Reitz } 119384aa0140SStefan Hajnoczi 119484aa0140SStefan Hajnoczi bs = blk_bs(blk); 119584aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 119684aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 119784aa0140SStefan Hajnoczi 119884aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 119984aa0140SStefan Hajnoczi 120084aa0140SStefan Hajnoczi aio_context_release(aio_context); 12012d3735d3SStefan Hajnoczi } 120258513bdeSJeff Cody if (ret < 0) { 120358513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 120458513bdeSJeff Cody strerror(-ret)); 1205666daa68SMarkus Armbruster } 1206666daa68SMarkus Armbruster } 1207666daa68SMarkus Armbruster 120810f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 12096cc2a415SPaolo Bonzini { 1210c8a83e85SKevin Wolf TransactionActionList list; 12116cc2a415SPaolo Bonzini 121210f75907SEric Blake list.value = action; 12136cc2a415SPaolo Bonzini list.next = NULL; 121494d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 12156cc2a415SPaolo Bonzini } 12166cc2a415SPaolo Bonzini 12170901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 12180901f67eSBenoît Canet bool has_node_name, const char *node_name, 12190901f67eSBenoît Canet const char *snapshot_file, 12200901f67eSBenoît Canet bool has_snapshot_node_name, 12210901f67eSBenoît Canet const char *snapshot_node_name, 12226106e249SLuiz Capitulino bool has_format, const char *format, 12230901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1224f8882568SJes Sorensen { 1225a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 12260901f67eSBenoît Canet .has_device = has_device, 12276cc2a415SPaolo Bonzini .device = (char *) device, 12280901f67eSBenoît Canet .has_node_name = has_node_name, 12290901f67eSBenoît Canet .node_name = (char *) node_name, 12306cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 12310901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 12320901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 12336cc2a415SPaolo Bonzini .has_format = has_format, 12346cc2a415SPaolo Bonzini .format = (char *) format, 12356cc2a415SPaolo Bonzini .has_mode = has_mode, 12366cc2a415SPaolo Bonzini .mode = mode, 12376cc2a415SPaolo Bonzini }; 123810f75907SEric Blake TransactionAction action = { 123910f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 124032bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 124110f75907SEric Blake }; 124210f75907SEric Blake blockdev_do_action(&action, errp); 1243f8882568SJes Sorensen } 1244f8882568SJes Sorensen 124543de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 124643de7e2dSAlberto Garcia Error **errp) 124743de7e2dSAlberto Garcia { 124843de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 124943de7e2dSAlberto Garcia .node = (char *) node, 125043de7e2dSAlberto Garcia .overlay = (char *) overlay 125143de7e2dSAlberto Garcia }; 125210f75907SEric Blake TransactionAction action = { 125310f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 125432bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 125510f75907SEric Blake }; 125610f75907SEric Blake blockdev_do_action(&action, errp); 125743de7e2dSAlberto Garcia } 125843de7e2dSAlberto Garcia 1259f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1260f323bc9eSWenchao Xia const char *name, 1261f323bc9eSWenchao Xia Error **errp) 1262f323bc9eSWenchao Xia { 1263f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1264f323bc9eSWenchao Xia .device = (char *) device, 1265f323bc9eSWenchao Xia .name = (char *) name 1266f323bc9eSWenchao Xia }; 126710f75907SEric Blake TransactionAction action = { 126810f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 126932bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 127010f75907SEric Blake }; 127110f75907SEric Blake blockdev_do_action(&action, errp); 1272f323bc9eSWenchao Xia } 1273f323bc9eSWenchao Xia 127444e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 127544e3e053SWenchao Xia bool has_id, 127644e3e053SWenchao Xia const char *id, 127744e3e053SWenchao Xia bool has_name, 127844e3e053SWenchao Xia const char *name, 127944e3e053SWenchao Xia Error **errp) 128044e3e053SWenchao Xia { 1281a0e8544cSFam Zheng BlockDriverState *bs; 12824ef3982aSStefan Hajnoczi AioContext *aio_context; 128344e3e053SWenchao Xia QEMUSnapshotInfo sn; 128444e3e053SWenchao Xia Error *local_err = NULL; 128544e3e053SWenchao Xia SnapshotInfo *info = NULL; 128644e3e053SWenchao Xia int ret; 128744e3e053SWenchao Xia 12882dfb4c03SKevin Wolf bs = qmp_get_root_bs(device, errp); 12892dfb4c03SKevin Wolf if (!bs) { 129044e3e053SWenchao Xia return NULL; 129144e3e053SWenchao Xia } 12922dfb4c03SKevin Wolf aio_context = bdrv_get_aio_context(bs); 12935433c24fSMax Reitz aio_context_acquire(aio_context); 129444e3e053SWenchao Xia 129544e3e053SWenchao Xia if (!has_id) { 129644e3e053SWenchao Xia id = NULL; 129744e3e053SWenchao Xia } 129844e3e053SWenchao Xia 129944e3e053SWenchao Xia if (!has_name) { 130044e3e053SWenchao Xia name = NULL; 130144e3e053SWenchao Xia } 130244e3e053SWenchao Xia 130344e3e053SWenchao Xia if (!id && !name) { 130444e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 13055433c24fSMax Reitz goto out_aio_context; 130644e3e053SWenchao Xia } 130744e3e053SWenchao Xia 13080b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 13090b928854SStefan Hajnoczi goto out_aio_context; 13100b928854SStefan Hajnoczi } 13110b928854SStefan Hajnoczi 131244e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 131384d18f06SMarkus Armbruster if (local_err) { 131444e3e053SWenchao Xia error_propagate(errp, local_err); 13154ef3982aSStefan Hajnoczi goto out_aio_context; 131644e3e053SWenchao Xia } 131744e3e053SWenchao Xia if (!ret) { 131844e3e053SWenchao Xia error_setg(errp, 131944e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 132044e3e053SWenchao Xia "device '%s'", 132144e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 13224ef3982aSStefan Hajnoczi goto out_aio_context; 132344e3e053SWenchao Xia } 132444e3e053SWenchao Xia 132544e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 132684d18f06SMarkus Armbruster if (local_err) { 132744e3e053SWenchao Xia error_propagate(errp, local_err); 13284ef3982aSStefan Hajnoczi goto out_aio_context; 132944e3e053SWenchao Xia } 133044e3e053SWenchao Xia 13314ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13324ef3982aSStefan Hajnoczi 13335839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 133444e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 133544e3e053SWenchao Xia info->name = g_strdup(sn.name); 133644e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 133744e3e053SWenchao Xia info->date_sec = sn.date_sec; 133844e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 133944e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 134044e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 134144e3e053SWenchao Xia 134244e3e053SWenchao Xia return info; 13434ef3982aSStefan Hajnoczi 13444ef3982aSStefan Hajnoczi out_aio_context: 13454ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13464ef3982aSStefan Hajnoczi return NULL; 134744e3e053SWenchao Xia } 13488802d1fdSJeff Cody 1349341ebc2fSJohn Snow /** 1350341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1351341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1352341ebc2fSJohn Snow * the node reference and bitmap names. 1353341ebc2fSJohn Snow * 1354341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1355341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1356341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1357341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1358341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1359341ebc2fSJohn Snow * 1360341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1361341ebc2fSJohn Snow */ 1362341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1363341ebc2fSJohn Snow const char *name, 1364341ebc2fSJohn Snow BlockDriverState **pbs, 1365341ebc2fSJohn Snow AioContext **paio, 1366341ebc2fSJohn Snow Error **errp) 1367341ebc2fSJohn Snow { 1368341ebc2fSJohn Snow BlockDriverState *bs; 1369341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1370341ebc2fSJohn Snow AioContext *aio_context; 1371341ebc2fSJohn Snow 1372341ebc2fSJohn Snow if (!node) { 1373341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1374341ebc2fSJohn Snow return NULL; 1375341ebc2fSJohn Snow } 1376341ebc2fSJohn Snow if (!name) { 1377341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1378341ebc2fSJohn Snow return NULL; 1379341ebc2fSJohn Snow } 1380341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1381341ebc2fSJohn Snow if (!bs) { 1382341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1383341ebc2fSJohn Snow return NULL; 1384341ebc2fSJohn Snow } 1385341ebc2fSJohn Snow 1386341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 1387341ebc2fSJohn Snow aio_context_acquire(aio_context); 1388341ebc2fSJohn Snow 1389341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1390341ebc2fSJohn Snow if (!bitmap) { 1391341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 1392341ebc2fSJohn Snow goto fail; 1393341ebc2fSJohn Snow } 1394341ebc2fSJohn Snow 1395341ebc2fSJohn Snow if (pbs) { 1396341ebc2fSJohn Snow *pbs = bs; 1397341ebc2fSJohn Snow } 1398341ebc2fSJohn Snow if (paio) { 1399341ebc2fSJohn Snow *paio = aio_context; 1400341ebc2fSJohn Snow } else { 1401341ebc2fSJohn Snow aio_context_release(aio_context); 1402341ebc2fSJohn Snow } 1403341ebc2fSJohn Snow 1404341ebc2fSJohn Snow return bitmap; 1405341ebc2fSJohn Snow 1406341ebc2fSJohn Snow fail: 1407341ebc2fSJohn Snow aio_context_release(aio_context); 1408341ebc2fSJohn Snow return NULL; 1409341ebc2fSJohn Snow } 1410341ebc2fSJohn Snow 1411b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1412ba0c86a3SWenchao Xia 141350f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1414ba0c86a3SWenchao Xia 141550f43f0fSJohn Snow /** 141650f43f0fSJohn Snow * BlkActionOps: 141750f43f0fSJohn Snow * Table of operations that define an Action. 141850f43f0fSJohn Snow * 141950f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 142050f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 142150f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 142250f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 142350f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 142450f43f0fSJohn Snow * commit() or abort(). Can be NULL. 142550f43f0fSJohn Snow * 142650f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 142750f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1428ba0c86a3SWenchao Xia */ 142950f43f0fSJohn Snow typedef struct BlkActionOps { 143050f43f0fSJohn Snow size_t instance_size; 143150f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 143250f43f0fSJohn Snow void (*commit)(BlkActionState *common); 143350f43f0fSJohn Snow void (*abort)(BlkActionState *common); 143450f43f0fSJohn Snow void (*clean)(BlkActionState *common); 143550f43f0fSJohn Snow } BlkActionOps; 143650f43f0fSJohn Snow 143750f43f0fSJohn Snow /** 143850f43f0fSJohn Snow * BlkActionState: 143950f43f0fSJohn Snow * Describes one Action's state within a Transaction. 144050f43f0fSJohn Snow * 144150f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 144250f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 144394d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 144450f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 144550f43f0fSJohn Snow * 144650f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 144750f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 144850f43f0fSJohn Snow * base class. 144950f43f0fSJohn Snow */ 145050f43f0fSJohn Snow struct BlkActionState { 1451c8a83e85SKevin Wolf TransactionAction *action; 145250f43f0fSJohn Snow const BlkActionOps *ops; 145394d16a64SJohn Snow BlockJobTxn *block_job_txn; 145494d16a64SJohn Snow TransactionProperties *txn_props; 145550f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1456ba0c86a3SWenchao Xia }; 1457ba0c86a3SWenchao Xia 1458bbe86010SWenchao Xia /* internal snapshot private data */ 1459bbe86010SWenchao Xia typedef struct InternalSnapshotState { 146050f43f0fSJohn Snow BlkActionState common; 1461bbe86010SWenchao Xia BlockDriverState *bs; 14625d6e96efSStefan Hajnoczi AioContext *aio_context; 1463bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1464507306ccSFam Zheng bool created; 1465bbe86010SWenchao Xia } InternalSnapshotState; 1466bbe86010SWenchao Xia 146794d16a64SJohn Snow 146894d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 146994d16a64SJohn Snow { 147094d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 147194d16a64SJohn Snow error_setg(errp, 147294d16a64SJohn Snow "Action '%s' does not support Transaction property " 147394d16a64SJohn Snow "completion-mode = %s", 147494d16a64SJohn Snow TransactionActionKind_lookup[s->action->type], 147594d16a64SJohn Snow ActionCompletionMode_lookup[s->txn_props->completion_mode]); 147694d16a64SJohn Snow return -1; 147794d16a64SJohn Snow } 147894d16a64SJohn Snow return 0; 147994d16a64SJohn Snow } 148094d16a64SJohn Snow 148150f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1482bbe86010SWenchao Xia Error **errp) 1483bbe86010SWenchao Xia { 1484f70edf99SMarkus Armbruster Error *local_err = NULL; 1485bbe86010SWenchao Xia const char *device; 1486bbe86010SWenchao Xia const char *name; 1487bbe86010SWenchao Xia BlockDriverState *bs; 1488bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1489bbe86010SWenchao Xia bool ret; 1490bbe86010SWenchao Xia qemu_timeval tv; 1491bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1492bbe86010SWenchao Xia InternalSnapshotState *state; 1493bbe86010SWenchao Xia int ret1; 1494bbe86010SWenchao Xia 14956a8f9661SEric Blake g_assert(common->action->type == 1496bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 149732bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1498bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1499bbe86010SWenchao Xia 1500bbe86010SWenchao Xia /* 1. parse input */ 1501bbe86010SWenchao Xia device = internal->device; 1502bbe86010SWenchao Xia name = internal->name; 1503bbe86010SWenchao Xia 1504bbe86010SWenchao Xia /* 2. check for validation */ 150594d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 150694d16a64SJohn Snow return; 150794d16a64SJohn Snow } 150894d16a64SJohn Snow 150975dfd402SKevin Wolf bs = qmp_get_root_bs(device, errp); 151075dfd402SKevin Wolf if (!bs) { 1511bbe86010SWenchao Xia return; 1512bbe86010SWenchao Xia } 1513bbe86010SWenchao Xia 15145d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 151575dfd402SKevin Wolf state->aio_context = bdrv_get_aio_context(bs); 15165d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 15175d6e96efSStefan Hajnoczi 1518507306ccSFam Zheng state->bs = bs; 1519507306ccSFam Zheng bdrv_drained_begin(bs); 1520507306ccSFam Zheng 15213dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 15223dc7ca3cSStefan Hajnoczi return; 15233dc7ca3cSStefan Hajnoczi } 15243dc7ca3cSStefan Hajnoczi 1525bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 152681e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1527bbe86010SWenchao Xia return; 1528bbe86010SWenchao Xia } 1529bbe86010SWenchao Xia 1530bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 153181e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 153281e5f78aSAlberto Garcia "does not support internal snapshots", 153381e5f78aSAlberto Garcia bs->drv->format_name, device); 1534bbe86010SWenchao Xia return; 1535bbe86010SWenchao Xia } 1536bbe86010SWenchao Xia 1537bbe86010SWenchao Xia if (!strlen(name)) { 1538bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1539bbe86010SWenchao Xia return; 1540bbe86010SWenchao Xia } 1541bbe86010SWenchao Xia 1542bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1543f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1544f70edf99SMarkus Armbruster &local_err); 1545f70edf99SMarkus Armbruster if (local_err) { 1546f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1547bbe86010SWenchao Xia return; 1548bbe86010SWenchao Xia } else if (ret) { 1549bbe86010SWenchao Xia error_setg(errp, 1550bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1551bbe86010SWenchao Xia name, device); 1552bbe86010SWenchao Xia return; 1553bbe86010SWenchao Xia } 1554bbe86010SWenchao Xia 1555bbe86010SWenchao Xia /* 3. take the snapshot */ 1556bbe86010SWenchao Xia sn = &state->sn; 1557bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1558bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1559bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1560bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1561bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1562bbe86010SWenchao Xia 1563bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1564bbe86010SWenchao Xia if (ret1 < 0) { 1565bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1566bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1567bbe86010SWenchao Xia name, device); 1568bbe86010SWenchao Xia return; 1569bbe86010SWenchao Xia } 1570bbe86010SWenchao Xia 1571bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1572507306ccSFam Zheng state->created = true; 1573bbe86010SWenchao Xia } 1574bbe86010SWenchao Xia 157550f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1576bbe86010SWenchao Xia { 1577bbe86010SWenchao Xia InternalSnapshotState *state = 1578bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1579bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1580bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1581bbe86010SWenchao Xia Error *local_error = NULL; 1582bbe86010SWenchao Xia 1583507306ccSFam Zheng if (!state->created) { 1584bbe86010SWenchao Xia return; 1585bbe86010SWenchao Xia } 1586bbe86010SWenchao Xia 1587bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1588c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1589c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1590c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1591c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1592c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1593bbe86010SWenchao Xia } 1594bbe86010SWenchao Xia } 1595bbe86010SWenchao Xia 159650f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 15975d6e96efSStefan Hajnoczi { 15985d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 15995d6e96efSStefan Hajnoczi common, common); 16005d6e96efSStefan Hajnoczi 16015d6e96efSStefan Hajnoczi if (state->aio_context) { 1602507306ccSFam Zheng if (state->bs) { 1603507306ccSFam Zheng bdrv_drained_end(state->bs); 1604507306ccSFam Zheng } 16055d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16065d6e96efSStefan Hajnoczi } 16075d6e96efSStefan Hajnoczi } 16085d6e96efSStefan Hajnoczi 1609ba0c86a3SWenchao Xia /* external snapshot private data */ 1610ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 161150f43f0fSJohn Snow BlkActionState common; 16128802d1fdSJeff Cody BlockDriverState *old_bs; 16138802d1fdSJeff Cody BlockDriverState *new_bs; 16145d6e96efSStefan Hajnoczi AioContext *aio_context; 1615067acf28SKevin Wolf bool overlay_appended; 1616ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16178802d1fdSJeff Cody 161850f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16199b9877eeSWenchao Xia Error **errp) 16209b9877eeSWenchao Xia { 16215b363937SMax Reitz int flags = 0; 162243de7e2dSAlberto Garcia QDict *options = NULL; 16239b9877eeSWenchao Xia Error *local_err = NULL; 162443de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1625e2a31e87SWenchao Xia const char *device; 16260901f67eSBenoît Canet const char *node_name; 162743de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 162843de7e2dSAlberto Garcia const char *snapshot_ref; 162943de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1630e2a31e87SWenchao Xia const char *new_image_file; 1631ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1632ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1633c8a83e85SKevin Wolf TransactionAction *action = common->action; 16349b9877eeSWenchao Xia 163543de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 163643de7e2dSAlberto Garcia * purpose but a different set of parameters */ 163743de7e2dSAlberto Garcia switch (action->type) { 163843de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 163943de7e2dSAlberto Garcia { 164032bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 164143de7e2dSAlberto Garcia device = s->node; 164243de7e2dSAlberto Garcia node_name = s->node; 164343de7e2dSAlberto Garcia new_image_file = NULL; 164443de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1645e2a31e87SWenchao Xia } 164643de7e2dSAlberto Garcia break; 164743de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 164843de7e2dSAlberto Garcia { 164932bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 165043de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 165143de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 165243de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 165343de7e2dSAlberto Garcia snapshot_ref = NULL; 165443de7e2dSAlberto Garcia } 165543de7e2dSAlberto Garcia break; 165643de7e2dSAlberto Garcia default: 165743de7e2dSAlberto Garcia g_assert_not_reached(); 1658e2a31e87SWenchao Xia } 1659e2a31e87SWenchao Xia 1660e2a31e87SWenchao Xia /* start processing */ 166194d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 166294d16a64SJohn Snow return; 166394d16a64SJohn Snow } 166494d16a64SJohn Snow 166543de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 166643de7e2dSAlberto Garcia if (!state->old_bs) { 16679b9877eeSWenchao Xia return; 16689b9877eeSWenchao Xia } 16699b9877eeSWenchao Xia 16705d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 16715d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 16725d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1673da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 16745d6e96efSStefan Hajnoczi 1675ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1676c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 16779b9877eeSWenchao Xia return; 16789b9877eeSWenchao Xia } 16799b9877eeSWenchao Xia 16803718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 16813718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 16829b9877eeSWenchao Xia return; 16839b9877eeSWenchao Xia } 16849b9877eeSWenchao Xia 1685ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1686ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1687c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 16889b9877eeSWenchao Xia return; 16899b9877eeSWenchao Xia } 16909b9877eeSWenchao Xia } 16919b9877eeSWenchao Xia 1692212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1693c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1694f6186f49SBenoît Canet return; 1695f6186f49SBenoît Canet } 1696f6186f49SBenoît Canet 169743de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 169832bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 169943de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 170043de7e2dSAlberto Garcia enum NewImageMode mode; 170143de7e2dSAlberto Garcia const char *snapshot_node_name = 170243de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 170343de7e2dSAlberto Garcia 170443de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 170543de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 170643de7e2dSAlberto Garcia return; 170743de7e2dSAlberto Garcia } 170843de7e2dSAlberto Garcia 170943de7e2dSAlberto Garcia if (snapshot_node_name && 171043de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 171143de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 171243de7e2dSAlberto Garcia return; 171343de7e2dSAlberto Garcia } 171443de7e2dSAlberto Garcia 1715ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17164c844983SKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); 17179b9877eeSWenchao Xia 17189b9877eeSWenchao Xia /* create new image w/backing file */ 171943de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17209b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1721f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1722f86b8b58SKevin Wolf if (size < 0) { 1723f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 1724f86b8b58SKevin Wolf return; 1725f86b8b58SKevin Wolf } 17269b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1727ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1728ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 17299217283dSFam Zheng NULL, size, flags, false, &local_err); 173084d18f06SMarkus Armbruster if (local_err) { 17319b9877eeSWenchao Xia error_propagate(errp, local_err); 17329b9877eeSWenchao Xia return; 17339b9877eeSWenchao Xia } 17349b9877eeSWenchao Xia } 17359b9877eeSWenchao Xia 17360901f67eSBenoît Canet options = qdict_new(); 173743de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 1738*46f5ac20SEric Blake qdict_put_str(options, "node-name", snapshot_node_name); 17390901f67eSBenoît Canet } 1740*46f5ac20SEric Blake qdict_put_str(options, "driver", format); 17410901f67eSBenoît Canet 174243de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 174343de7e2dSAlberto Garcia } 174443de7e2dSAlberto Garcia 17455b363937SMax Reitz state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, 17465b363937SMax Reitz errp); 1747f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17485b363937SMax Reitz if (!state->new_bs) { 174943de7e2dSAlberto Garcia return; 175043de7e2dSAlberto Garcia } 175143de7e2dSAlberto Garcia 17521f0c461bSKevin Wolf if (bdrv_has_blk(state->new_bs)) { 1753e467da7bSKevin Wolf error_setg(errp, "The snapshot is already in use"); 175443de7e2dSAlberto Garcia return; 175543de7e2dSAlberto Garcia } 175643de7e2dSAlberto Garcia 175743de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 175843de7e2dSAlberto Garcia errp)) { 175943de7e2dSAlberto Garcia return; 176043de7e2dSAlberto Garcia } 176143de7e2dSAlberto Garcia 176243de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 176343de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 176408b24cfeSAlberto Garcia return; 176508b24cfeSAlberto Garcia } 176608b24cfeSAlberto Garcia 176708b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 176808b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 1769b2c2832cSKevin Wolf return; 1770b2c2832cSKevin Wolf } 1771b2c2832cSKevin Wolf 1772c26a5ab7SFam Zheng bdrv_set_aio_context(state->new_bs, state->aio_context); 1773c26a5ab7SFam Zheng 1774b2c2832cSKevin Wolf /* This removes our old bs and adds the new bs. This is an operation that 1775b2c2832cSKevin Wolf * can fail, so we need to do it in .prepare; undoing it for abort is 1776b2c2832cSKevin Wolf * always possible. */ 1777b2c2832cSKevin Wolf bdrv_ref(state->new_bs); 1778b2c2832cSKevin Wolf bdrv_append(state->new_bs, state->old_bs, &local_err); 1779b2c2832cSKevin Wolf if (local_err) { 1780b2c2832cSKevin Wolf error_propagate(errp, local_err); 1781b2c2832cSKevin Wolf return; 17829b9877eeSWenchao Xia } 1783067acf28SKevin Wolf state->overlay_appended = true; 17849b9877eeSWenchao Xia } 17859b9877eeSWenchao Xia 178650f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 17873b0047e8SWenchao Xia { 1788ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1789ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1790ba0c86a3SWenchao Xia 17913b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 17923b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 17933b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 17944c844983SKevin Wolf if (!state->old_bs->copy_on_read) { 1795dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 17963b0047e8SWenchao Xia NULL); 17973b0047e8SWenchao Xia } 17984c844983SKevin Wolf } 17993b0047e8SWenchao Xia 180050f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 180196b86bf7SWenchao Xia { 1802ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1803ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1804ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 1805067acf28SKevin Wolf if (state->overlay_appended) { 18065fe31c25SKevin Wolf bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); 1807b2c2832cSKevin Wolf } 180896b86bf7SWenchao Xia } 1809da763e83SFam Zheng } 1810da763e83SFam Zheng 181150f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1812da763e83SFam Zheng { 1813da763e83SFam Zheng ExternalSnapshotState *state = 1814da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 18155d6e96efSStefan Hajnoczi if (state->aio_context) { 1816da763e83SFam Zheng bdrv_drained_end(state->old_bs); 18175d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 1818b2c2832cSKevin Wolf bdrv_unref(state->new_bs); 18195d6e96efSStefan Hajnoczi } 182096b86bf7SWenchao Xia } 182196b86bf7SWenchao Xia 18223037f364SStefan Hajnoczi typedef struct DriveBackupState { 182350f43f0fSJohn Snow BlkActionState common; 18243037f364SStefan Hajnoczi BlockDriverState *bs; 18255d6e96efSStefan Hajnoczi AioContext *aio_context; 18263037f364SStefan Hajnoczi BlockJob *job; 18273037f364SStefan Hajnoczi } DriveBackupState; 18283037f364SStefan Hajnoczi 1829111049a4SJohn Snow static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, 183081206a89SPavel Butsykin Error **errp); 183178f51fdeSJohn Snow 183250f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18333037f364SStefan Hajnoczi { 18343037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1835b7e4fa22SKevin Wolf BlockDriverState *bs; 18363037f364SStefan Hajnoczi DriveBackup *backup; 18373037f364SStefan Hajnoczi Error *local_err = NULL; 18383037f364SStefan Hajnoczi 18396a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 184032bafa8fSEric Blake backup = common->action->u.drive_backup.data; 18413037f364SStefan Hajnoczi 1842b7e4fa22SKevin Wolf bs = qmp_get_root_bs(backup->device, errp); 1843b7e4fa22SKevin Wolf if (!bs) { 18441fdd4b7bSFam Zheng return; 18451fdd4b7bSFam Zheng } 18461fdd4b7bSFam Zheng 18475d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 1848b7e4fa22SKevin Wolf state->aio_context = bdrv_get_aio_context(bs); 18495d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1850b7e4fa22SKevin Wolf bdrv_drained_begin(bs); 1851b7e4fa22SKevin Wolf state->bs = bs; 18525d6e96efSStefan Hajnoczi 1853111049a4SJohn Snow state->job = do_drive_backup(backup, common->block_job_txn, &local_err); 185484d18f06SMarkus Armbruster if (local_err) { 18553037f364SStefan Hajnoczi error_propagate(errp, local_err); 18563037f364SStefan Hajnoczi return; 18573037f364SStefan Hajnoczi } 1858111049a4SJohn Snow } 18593037f364SStefan Hajnoczi 1860111049a4SJohn Snow static void drive_backup_commit(BlkActionState *common) 1861111049a4SJohn Snow { 1862111049a4SJohn Snow DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1863111049a4SJohn Snow assert(state->job); 1864111049a4SJohn Snow block_job_start(state->job); 18653037f364SStefan Hajnoczi } 18663037f364SStefan Hajnoczi 186750f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 18683037f364SStefan Hajnoczi { 18693037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18703037f364SStefan Hajnoczi 1871111049a4SJohn Snow if (state->job) { 1872111049a4SJohn Snow block_job_cancel_sync(state->job); 18733037f364SStefan Hajnoczi } 18743037f364SStefan Hajnoczi } 18753037f364SStefan Hajnoczi 187650f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 18775d6e96efSStefan Hajnoczi { 18785d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18795d6e96efSStefan Hajnoczi 18805d6e96efSStefan Hajnoczi if (state->aio_context) { 18811fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 18825d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18835d6e96efSStefan Hajnoczi } 18845d6e96efSStefan Hajnoczi } 18855d6e96efSStefan Hajnoczi 1886bd8baecdSFam Zheng typedef struct BlockdevBackupState { 188750f43f0fSJohn Snow BlkActionState common; 1888bd8baecdSFam Zheng BlockDriverState *bs; 1889bd8baecdSFam Zheng BlockJob *job; 1890bd8baecdSFam Zheng AioContext *aio_context; 1891bd8baecdSFam Zheng } BlockdevBackupState; 1892bd8baecdSFam Zheng 1893111049a4SJohn Snow static BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, 1894dc7a4a9eSPavel Butsykin Error **errp); 189578f51fdeSJohn Snow 189650f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1897bd8baecdSFam Zheng { 1898bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1899bd8baecdSFam Zheng BlockdevBackup *backup; 1900cef34eebSKevin Wolf BlockDriverState *bs, *target; 1901bd8baecdSFam Zheng Error *local_err = NULL; 1902bd8baecdSFam Zheng 19036a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 190432bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1905bd8baecdSFam Zheng 1906cef34eebSKevin Wolf bs = qmp_get_root_bs(backup->device, errp); 1907cef34eebSKevin Wolf if (!bs) { 1908ff52bf36SFam Zheng return; 1909ff52bf36SFam Zheng } 1910ff52bf36SFam Zheng 191139d990acSKevin Wolf target = bdrv_lookup_bs(backup->target, backup->target, errp); 19125433c24fSMax Reitz if (!target) { 1913bd8baecdSFam Zheng return; 1914bd8baecdSFam Zheng } 1915bd8baecdSFam Zheng 1916bd8baecdSFam Zheng /* AioContext is released in .clean() */ 1917cef34eebSKevin Wolf state->aio_context = bdrv_get_aio_context(bs); 191839d990acSKevin Wolf if (state->aio_context != bdrv_get_aio_context(target)) { 1919bd8baecdSFam Zheng state->aio_context = NULL; 1920bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1921bd8baecdSFam Zheng return; 1922bd8baecdSFam Zheng } 1923bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1924cef34eebSKevin Wolf state->bs = bs; 1925ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1926bd8baecdSFam Zheng 1927111049a4SJohn Snow state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err); 1928bd8baecdSFam Zheng if (local_err) { 1929bd8baecdSFam Zheng error_propagate(errp, local_err); 1930bd8baecdSFam Zheng return; 1931bd8baecdSFam Zheng } 1932111049a4SJohn Snow } 1933bd8baecdSFam Zheng 1934111049a4SJohn Snow static void blockdev_backup_commit(BlkActionState *common) 1935111049a4SJohn Snow { 1936111049a4SJohn Snow BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1937111049a4SJohn Snow assert(state->job); 1938111049a4SJohn Snow block_job_start(state->job); 1939bd8baecdSFam Zheng } 1940bd8baecdSFam Zheng 194150f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1942bd8baecdSFam Zheng { 1943bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1944bd8baecdSFam Zheng 1945111049a4SJohn Snow if (state->job) { 1946111049a4SJohn Snow block_job_cancel_sync(state->job); 1947bd8baecdSFam Zheng } 1948bd8baecdSFam Zheng } 1949bd8baecdSFam Zheng 195050f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 1951bd8baecdSFam Zheng { 1952bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1953bd8baecdSFam Zheng 1954bd8baecdSFam Zheng if (state->aio_context) { 1955ff52bf36SFam Zheng bdrv_drained_end(state->bs); 1956bd8baecdSFam Zheng aio_context_release(state->aio_context); 1957bd8baecdSFam Zheng } 1958bd8baecdSFam Zheng } 1959bd8baecdSFam Zheng 1960df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 196150f43f0fSJohn Snow BlkActionState common; 1962df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 1963df9a681dSFam Zheng BlockDriverState *bs; 1964df9a681dSFam Zheng AioContext *aio_context; 1965df9a681dSFam Zheng HBitmap *backup; 1966df9a681dSFam Zheng bool prepared; 1967df9a681dSFam Zheng } BlockDirtyBitmapState; 1968df9a681dSFam Zheng 196950f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 1970df9a681dSFam Zheng Error **errp) 1971df9a681dSFam Zheng { 1972df9a681dSFam Zheng Error *local_err = NULL; 1973df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 1974df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1975df9a681dSFam Zheng common, common); 1976df9a681dSFam Zheng 197794d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 197894d16a64SJohn Snow return; 197994d16a64SJohn Snow } 198094d16a64SJohn Snow 198132bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 1982df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 1983df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 1984df9a681dSFam Zheng action->has_granularity, action->granularity, 1985df9a681dSFam Zheng &local_err); 1986df9a681dSFam Zheng 1987df9a681dSFam Zheng if (!local_err) { 1988df9a681dSFam Zheng state->prepared = true; 1989df9a681dSFam Zheng } else { 1990df9a681dSFam Zheng error_propagate(errp, local_err); 1991df9a681dSFam Zheng } 1992df9a681dSFam Zheng } 1993df9a681dSFam Zheng 199450f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 1995df9a681dSFam Zheng { 1996df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 1997df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1998df9a681dSFam Zheng common, common); 1999df9a681dSFam Zheng 200032bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2001df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2002df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2003df9a681dSFam Zheng */ 2004df9a681dSFam Zheng if (state->prepared) { 2005df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2006df9a681dSFam Zheng } 2007df9a681dSFam Zheng } 2008df9a681dSFam Zheng 200950f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2010df9a681dSFam Zheng Error **errp) 2011df9a681dSFam Zheng { 2012df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2013df9a681dSFam Zheng common, common); 2014df9a681dSFam Zheng BlockDirtyBitmap *action; 2015df9a681dSFam Zheng 201694d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 201794d16a64SJohn Snow return; 201894d16a64SJohn Snow } 201994d16a64SJohn Snow 202032bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2021df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2022df9a681dSFam Zheng action->name, 2023df9a681dSFam Zheng &state->bs, 2024df9a681dSFam Zheng &state->aio_context, 2025df9a681dSFam Zheng errp); 2026df9a681dSFam Zheng if (!state->bitmap) { 2027df9a681dSFam Zheng return; 2028df9a681dSFam Zheng } 2029df9a681dSFam Zheng 2030df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2031df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2032df9a681dSFam Zheng return; 2033df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2034df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2035df9a681dSFam Zheng return; 2036df9a681dSFam Zheng } 2037df9a681dSFam Zheng 2038df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2039df9a681dSFam Zheng /* AioContext is released in .clean() */ 2040df9a681dSFam Zheng } 2041df9a681dSFam Zheng 204250f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2043df9a681dSFam Zheng { 2044df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2045df9a681dSFam Zheng common, common); 2046df9a681dSFam Zheng 2047184dd9c4SJohn Snow if (state->backup) { 2048df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2049df9a681dSFam Zheng } 2050184dd9c4SJohn Snow } 2051df9a681dSFam Zheng 205250f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2053df9a681dSFam Zheng { 2054df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2055df9a681dSFam Zheng common, common); 2056df9a681dSFam Zheng 2057df9a681dSFam Zheng hbitmap_free(state->backup); 2058df9a681dSFam Zheng } 2059df9a681dSFam Zheng 206050f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2061df9a681dSFam Zheng { 2062df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2063df9a681dSFam Zheng common, common); 2064df9a681dSFam Zheng 2065df9a681dSFam Zheng if (state->aio_context) { 2066df9a681dSFam Zheng aio_context_release(state->aio_context); 2067df9a681dSFam Zheng } 2068df9a681dSFam Zheng } 2069df9a681dSFam Zheng 207050f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 207178b18b78SStefan Hajnoczi { 207278b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 207378b18b78SStefan Hajnoczi } 207478b18b78SStefan Hajnoczi 207550f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 207678b18b78SStefan Hajnoczi { 2077dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 207878b18b78SStefan Hajnoczi } 207978b18b78SStefan Hajnoczi 208050f43f0fSJohn Snow static const BlkActionOps actions[] = { 208143de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 208243de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 208343de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 208443de7e2dSAlberto Garcia .commit = external_snapshot_commit, 208543de7e2dSAlberto Garcia .abort = external_snapshot_abort, 20864ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 208743de7e2dSAlberto Garcia }, 2088c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2089ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2090ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2091ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2092ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2093da763e83SFam Zheng .clean = external_snapshot_clean, 2094ba0c86a3SWenchao Xia }, 20953037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 20963037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 20973037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 2098111049a4SJohn Snow .commit = drive_backup_commit, 20993037f364SStefan Hajnoczi .abort = drive_backup_abort, 21005d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21013037f364SStefan Hajnoczi }, 2102bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2103bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2104bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2105111049a4SJohn Snow .commit = blockdev_backup_commit, 2106bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2107bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2108bd8baecdSFam Zheng }, 210978b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 211050f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 211178b18b78SStefan Hajnoczi .prepare = abort_prepare, 211278b18b78SStefan Hajnoczi .commit = abort_commit, 211378b18b78SStefan Hajnoczi }, 2114bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2115bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2116bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2117bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21185d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2119bbe86010SWenchao Xia }, 2120df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2121df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2122df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2123df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2124df9a681dSFam Zheng }, 2125df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2126df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2127df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2128df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2129df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2130df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2131df9a681dSFam Zheng } 2132ba0c86a3SWenchao Xia }; 2133ba0c86a3SWenchao Xia 213494d16a64SJohn Snow /** 213594d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 213694d16a64SJohn Snow * that structure with desired defaults if they are unset. 213794d16a64SJohn Snow */ 213894d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 213994d16a64SJohn Snow TransactionProperties *props) 214094d16a64SJohn Snow { 214194d16a64SJohn Snow if (!props) { 214294d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 214394d16a64SJohn Snow } 214494d16a64SJohn Snow 214594d16a64SJohn Snow if (!props->has_completion_mode) { 214694d16a64SJohn Snow props->has_completion_mode = true; 214794d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 214894d16a64SJohn Snow } 214994d16a64SJohn Snow 215094d16a64SJohn Snow return props; 215194d16a64SJohn Snow } 215294d16a64SJohn Snow 21538802d1fdSJeff Cody /* 2154b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2155b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 21568802d1fdSJeff Cody */ 215794d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 215894d16a64SJohn Snow bool has_props, 215994d16a64SJohn Snow struct TransactionProperties *props, 216094d16a64SJohn Snow Error **errp) 21618802d1fdSJeff Cody { 2162c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 216394d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 216450f43f0fSJohn Snow BlkActionState *state, *next; 216543e17041SLuiz Capitulino Error *local_err = NULL; 21668802d1fdSJeff Cody 216750f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 21688802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 21698802d1fdSJeff Cody 217094d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 217194d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 217294d16a64SJohn Snow */ 217394d16a64SJohn Snow props = get_transaction_properties(props); 217494d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 217594d16a64SJohn Snow block_job_txn = block_job_txn_new(); 217694d16a64SJohn Snow } 217794d16a64SJohn Snow 2178b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 21798802d1fdSJeff Cody bdrv_drain_all(); 21808802d1fdSJeff Cody 2181b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 21828802d1fdSJeff Cody while (NULL != dev_entry) { 2183c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 218450f43f0fSJohn Snow const BlkActionOps *ops; 218552e7c241SPaolo Bonzini 21868802d1fdSJeff Cody dev_info = dev_entry->value; 21878802d1fdSJeff Cody dev_entry = dev_entry->next; 21888802d1fdSJeff Cody 21896a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2190ba0c86a3SWenchao Xia 21916a8f9661SEric Blake ops = &actions[dev_info->type]; 2192aa3fe714SMax Reitz assert(ops->instance_size > 0); 2193aa3fe714SMax Reitz 2194ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2195ba5d6ab6SStefan Hajnoczi state->ops = ops; 2196ba5d6ab6SStefan Hajnoczi state->action = dev_info; 219794d16a64SJohn Snow state->block_job_txn = block_job_txn; 219894d16a64SJohn Snow state->txn_props = props; 2199ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22008802d1fdSJeff Cody 2201ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 220284d18f06SMarkus Armbruster if (local_err) { 22039b9877eeSWenchao Xia error_propagate(errp, local_err); 22049b9877eeSWenchao Xia goto delete_and_fail; 22059b9877eeSWenchao Xia } 220652e7c241SPaolo Bonzini } 22078802d1fdSJeff Cody 2208ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2209f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2210ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22118802d1fdSJeff Cody } 2212f9ea81e8SStefan Hajnoczi } 22138802d1fdSJeff Cody 22148802d1fdSJeff Cody /* success */ 22158802d1fdSJeff Cody goto exit; 22168802d1fdSJeff Cody 22178802d1fdSJeff Cody delete_and_fail: 2218b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2219ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2220ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2221ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2222ba0c86a3SWenchao Xia } 22238802d1fdSJeff Cody } 22248802d1fdSJeff Cody exit: 2225ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2226ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2227ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2228ba0c86a3SWenchao Xia } 2229ba5d6ab6SStefan Hajnoczi g_free(state); 22308802d1fdSJeff Cody } 223194d16a64SJohn Snow if (!has_props) { 223294d16a64SJohn Snow qapi_free_TransactionProperties(props); 223394d16a64SJohn Snow } 223494d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22358802d1fdSJeff Cody } 22368802d1fdSJeff Cody 2237fbe2d816SKevin Wolf void qmp_eject(bool has_device, const char *device, 2238fbe2d816SKevin Wolf bool has_id, const char *id, 2239fbe2d816SKevin Wolf bool has_force, bool force, Error **errp) 2240666daa68SMarkus Armbruster { 224138f54bd1SMax Reitz Error *local_err = NULL; 22423a3086b7SJohn Snow int rc; 2243666daa68SMarkus Armbruster 22443a3086b7SJohn Snow if (!has_force) { 22453a3086b7SJohn Snow force = false; 22463a3086b7SJohn Snow } 22473a3086b7SJohn Snow 2248fbe2d816SKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2249fbe2d816SKevin Wolf has_id ? id : NULL, 2250fbe2d816SKevin Wolf force, &local_err); 2251bf18bee5SColin Lord if (rc && rc != -ENOSYS) { 225238f54bd1SMax Reitz error_propagate(errp, local_err); 2253c245b6a3SLuiz Capitulino return; 2254666daa68SMarkus Armbruster } 2255bf18bee5SColin Lord error_free(local_err); 22563a3086b7SJohn Snow 2257fbe2d816SKevin Wolf qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp); 2258666daa68SMarkus Armbruster } 2259666daa68SMarkus Armbruster 226012d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 226112d3ba82SBenoît Canet bool has_node_name, const char *node_name, 226212d3ba82SBenoît Canet const char *password, Error **errp) 2263666daa68SMarkus Armbruster { 226412d3ba82SBenoît Canet Error *local_err = NULL; 2265666daa68SMarkus Armbruster BlockDriverState *bs; 2266e3442099SStefan Hajnoczi AioContext *aio_context; 2267666daa68SMarkus Armbruster 226812d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 226912d3ba82SBenoît Canet has_node_name ? node_name : NULL, 227012d3ba82SBenoît Canet &local_err); 227184d18f06SMarkus Armbruster if (local_err) { 227212d3ba82SBenoît Canet error_propagate(errp, local_err); 2273a4dea8a9SLuiz Capitulino return; 2274666daa68SMarkus Armbruster } 2275666daa68SMarkus Armbruster 2276e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2277e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2278e3442099SStefan Hajnoczi 22794d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2280666daa68SMarkus Armbruster 2281e3442099SStefan Hajnoczi aio_context_release(aio_context); 2282e3442099SStefan Hajnoczi } 2283e3442099SStefan Hajnoczi 2284bf18bee5SColin Lord /* 2285bf18bee5SColin Lord * Attempt to open the tray of @device. 2286bf18bee5SColin Lord * If @force, ignore its tray lock. 2287bf18bee5SColin Lord * Else, if the tray is locked, don't open it, but ask the guest to open it. 2288bf18bee5SColin Lord * On error, store an error through @errp and return -errno. 2289bf18bee5SColin Lord * If @device does not exist, return -ENODEV. 2290bf18bee5SColin Lord * If it has no removable media, return -ENOTSUP. 2291bf18bee5SColin Lord * If it has no tray, return -ENOSYS. 2292bf18bee5SColin Lord * If the guest was asked to open the tray, return -EINPROGRESS. 2293bf18bee5SColin Lord * Else, return 0. 22943a3086b7SJohn Snow */ 2295b33945cfSKevin Wolf static int do_open_tray(const char *blk_name, const char *qdev_id, 2296b33945cfSKevin Wolf bool force, Error **errp) 22977d8a9f71SMax Reitz { 22987d8a9f71SMax Reitz BlockBackend *blk; 2299b33945cfSKevin Wolf const char *device = qdev_id ?: blk_name; 23007d8a9f71SMax Reitz bool locked; 23017d8a9f71SMax Reitz 2302b33945cfSKevin Wolf blk = qmp_get_blk(blk_name, qdev_id, errp); 23037d8a9f71SMax Reitz if (!blk) { 23043a3086b7SJohn Snow return -ENODEV; 23057d8a9f71SMax Reitz } 23067d8a9f71SMax Reitz 23077d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23087d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23093a3086b7SJohn Snow return -ENOTSUP; 23107d8a9f71SMax Reitz } 23117d8a9f71SMax Reitz 231212c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 2313bf18bee5SColin Lord error_setg(errp, "Device '%s' does not have a tray", device); 2314bf18bee5SColin Lord return -ENOSYS; 231512c7ec87SMax Reitz } 231612c7ec87SMax Reitz 23177d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23183a3086b7SJohn Snow return 0; 23197d8a9f71SMax Reitz } 23207d8a9f71SMax Reitz 23217d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23227d8a9f71SMax Reitz if (locked) { 23237d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23247d8a9f71SMax Reitz } 23257d8a9f71SMax Reitz 23267d8a9f71SMax Reitz if (!locked || force) { 232739829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 23287d8a9f71SMax Reitz } 23293a3086b7SJohn Snow 23303a3086b7SJohn Snow if (locked && !force) { 2331bf18bee5SColin Lord error_setg(errp, "Device '%s' is locked and force was not specified, " 2332bf18bee5SColin Lord "wait for tray to open and try again", device); 2333bf18bee5SColin Lord return -EINPROGRESS; 23343a3086b7SJohn Snow } 23353a3086b7SJohn Snow 23363a3086b7SJohn Snow return 0; 23373a3086b7SJohn Snow } 23383a3086b7SJohn Snow 2339b33945cfSKevin Wolf void qmp_blockdev_open_tray(bool has_device, const char *device, 2340b33945cfSKevin Wolf bool has_id, const char *id, 2341b33945cfSKevin Wolf bool has_force, bool force, 23423a3086b7SJohn Snow Error **errp) 23433a3086b7SJohn Snow { 2344bf18bee5SColin Lord Error *local_err = NULL; 2345bf18bee5SColin Lord int rc; 2346bf18bee5SColin Lord 23473a3086b7SJohn Snow if (!has_force) { 23483a3086b7SJohn Snow force = false; 23493a3086b7SJohn Snow } 2350b33945cfSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 2351b33945cfSKevin Wolf has_id ? id : NULL, 2352b33945cfSKevin Wolf force, &local_err); 2353bf18bee5SColin Lord if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { 2354bf18bee5SColin Lord error_propagate(errp, local_err); 2355bf18bee5SColin Lord return; 2356bf18bee5SColin Lord } 2357bf18bee5SColin Lord error_free(local_err); 23587d8a9f71SMax Reitz } 23597d8a9f71SMax Reitz 2360b33945cfSKevin Wolf void qmp_blockdev_close_tray(bool has_device, const char *device, 2361b33945cfSKevin Wolf bool has_id, const char *id, 2362b33945cfSKevin Wolf Error **errp) 2363abaaf59dSMax Reitz { 2364abaaf59dSMax Reitz BlockBackend *blk; 236539829a01SKevin Wolf Error *local_err = NULL; 2366abaaf59dSMax Reitz 2367b33945cfSKevin Wolf device = has_device ? device : NULL; 2368b33945cfSKevin Wolf id = has_id ? id : NULL; 2369b33945cfSKevin Wolf 2370b33945cfSKevin Wolf blk = qmp_get_blk(device, id, errp); 2371abaaf59dSMax Reitz if (!blk) { 2372abaaf59dSMax Reitz return; 2373abaaf59dSMax Reitz } 2374abaaf59dSMax Reitz 2375abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2376b33945cfSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 2377abaaf59dSMax Reitz return; 2378abaaf59dSMax Reitz } 2379abaaf59dSMax Reitz 238012c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 238112c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 238212c7ec87SMax Reitz return; 238312c7ec87SMax Reitz } 238412c7ec87SMax Reitz 2385abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2386abaaf59dSMax Reitz return; 2387abaaf59dSMax Reitz } 2388abaaf59dSMax Reitz 238939829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 239039829a01SKevin Wolf if (local_err) { 239139829a01SKevin Wolf error_propagate(errp, local_err); 239239829a01SKevin Wolf return; 239339829a01SKevin Wolf } 2394abaaf59dSMax Reitz } 2395abaaf59dSMax Reitz 239600949babSKevin Wolf void qmp_x_blockdev_remove_medium(bool has_device, const char *device, 239700949babSKevin Wolf bool has_id, const char *id, Error **errp) 23982814f672SMax Reitz { 23992814f672SMax Reitz BlockBackend *blk; 24002814f672SMax Reitz BlockDriverState *bs; 24012814f672SMax Reitz AioContext *aio_context; 240200949babSKevin Wolf bool has_attached_device; 24032814f672SMax Reitz 240400949babSKevin Wolf device = has_device ? device : NULL; 240500949babSKevin Wolf id = has_id ? id : NULL; 240600949babSKevin Wolf 240700949babSKevin Wolf blk = qmp_get_blk(device, id, errp); 24082814f672SMax Reitz if (!blk) { 24092814f672SMax Reitz return; 24102814f672SMax Reitz } 24112814f672SMax Reitz 24122814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 241300949babSKevin Wolf has_attached_device = blk_get_attached_dev(blk); 24142814f672SMax Reitz 241500949babSKevin Wolf if (has_attached_device && !blk_dev_has_removable_media(blk)) { 241600949babSKevin Wolf error_setg(errp, "Device '%s' is not removable", device ?: id); 24172814f672SMax Reitz return; 24182814f672SMax Reitz } 24192814f672SMax Reitz 242000949babSKevin Wolf if (has_attached_device && blk_dev_has_tray(blk) && 242100949babSKevin Wolf !blk_dev_is_tray_open(blk)) 242200949babSKevin Wolf { 242300949babSKevin Wolf error_setg(errp, "Tray of device '%s' is not open", device ?: id); 24242814f672SMax Reitz return; 24252814f672SMax Reitz } 24262814f672SMax Reitz 24272814f672SMax Reitz bs = blk_bs(blk); 24282814f672SMax Reitz if (!bs) { 24292814f672SMax Reitz return; 24302814f672SMax Reitz } 24312814f672SMax Reitz 24322814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 24332814f672SMax Reitz aio_context_acquire(aio_context); 24342814f672SMax Reitz 24352814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 24362814f672SMax Reitz goto out; 24372814f672SMax Reitz } 24382814f672SMax Reitz 24392814f672SMax Reitz blk_remove_bs(blk); 24402814f672SMax Reitz 244112c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 244212c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 244312c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 244412c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 244512c7ec87SMax Reitz * value passed here (i.e. false). */ 244639829a01SKevin Wolf blk_dev_change_media_cb(blk, false, &error_abort); 244712c7ec87SMax Reitz } 244812c7ec87SMax Reitz 24492814f672SMax Reitz out: 24502814f672SMax Reitz aio_context_release(aio_context); 24512814f672SMax Reitz } 24522814f672SMax Reitz 2453716df217SKevin Wolf static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, 2454d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2455d1299882SMax Reitz { 245639829a01SKevin Wolf Error *local_err = NULL; 2457d1299882SMax Reitz bool has_device; 2458d7086422SKevin Wolf int ret; 2459d1299882SMax Reitz 2460d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2461d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2462d1299882SMax Reitz 2463d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2464716df217SKevin Wolf error_setg(errp, "Device is not removable"); 2465d1299882SMax Reitz return; 2466d1299882SMax Reitz } 2467d1299882SMax Reitz 246812c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2469716df217SKevin Wolf error_setg(errp, "Tray of the device is not open"); 2470d1299882SMax Reitz return; 2471d1299882SMax Reitz } 2472d1299882SMax Reitz 2473d1299882SMax Reitz if (blk_bs(blk)) { 2474716df217SKevin Wolf error_setg(errp, "There already is a medium in the device"); 2475d1299882SMax Reitz return; 2476d1299882SMax Reitz } 2477d1299882SMax Reitz 2478d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 2479d7086422SKevin Wolf if (ret < 0) { 2480d7086422SKevin Wolf return; 2481d7086422SKevin Wolf } 2482d1299882SMax Reitz 248312c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 248412c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 248512c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 248612c7ec87SMax Reitz * slot here. 248712c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 248812c7ec87SMax Reitz * value passed here (i.e. true). */ 248939829a01SKevin Wolf blk_dev_change_media_cb(blk, true, &local_err); 249039829a01SKevin Wolf if (local_err) { 249139829a01SKevin Wolf error_propagate(errp, local_err); 249239829a01SKevin Wolf blk_remove_bs(blk); 249339829a01SKevin Wolf return; 249439829a01SKevin Wolf } 249512c7ec87SMax Reitz } 2496d1299882SMax Reitz } 2497d1299882SMax Reitz 2498716df217SKevin Wolf void qmp_x_blockdev_insert_medium(bool has_device, const char *device, 2499716df217SKevin Wolf bool has_id, const char *id, 2500716df217SKevin Wolf const char *node_name, Error **errp) 2501d1299882SMax Reitz { 2502716df217SKevin Wolf BlockBackend *blk; 2503d1299882SMax Reitz BlockDriverState *bs; 2504d1299882SMax Reitz 2505716df217SKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 2506716df217SKevin Wolf has_id ? id : NULL, 2507716df217SKevin Wolf errp); 2508716df217SKevin Wolf if (!blk) { 2509716df217SKevin Wolf return; 2510716df217SKevin Wolf } 2511716df217SKevin Wolf 2512d1299882SMax Reitz bs = bdrv_find_node(node_name); 2513d1299882SMax Reitz if (!bs) { 2514d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2515d1299882SMax Reitz return; 2516d1299882SMax Reitz } 2517d1299882SMax Reitz 25181f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2519e467da7bSKevin Wolf error_setg(errp, "Node '%s' is already in use", node_name); 2520d1299882SMax Reitz return; 2521d1299882SMax Reitz } 2522d1299882SMax Reitz 2523716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, bs, errp); 2524d1299882SMax Reitz } 2525d1299882SMax Reitz 252670e2cb3bSKevin Wolf void qmp_blockdev_change_medium(bool has_device, const char *device, 252770e2cb3bSKevin Wolf bool has_id, const char *id, 252870e2cb3bSKevin Wolf const char *filename, 252924fb4133SMax Reitz bool has_format, const char *format, 253039ff43d9SMax Reitz bool has_read_only, 253139ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 253224fb4133SMax Reitz Error **errp) 2533de2c6c05SMax Reitz { 2534de2c6c05SMax Reitz BlockBackend *blk; 2535de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 25365b363937SMax Reitz int bdrv_flags; 2537b85114f8SKevin Wolf bool detect_zeroes; 253838a53d50SColin Lord int rc; 2539de2c6c05SMax Reitz QDict *options = NULL; 2540de2c6c05SMax Reitz Error *err = NULL; 2541de2c6c05SMax Reitz 254270e2cb3bSKevin Wolf blk = qmp_get_blk(has_device ? device : NULL, 254370e2cb3bSKevin Wolf has_id ? id : NULL, 254470e2cb3bSKevin Wolf errp); 2545de2c6c05SMax Reitz if (!blk) { 2546de2c6c05SMax Reitz goto fail; 2547de2c6c05SMax Reitz } 2548de2c6c05SMax Reitz 2549de2c6c05SMax Reitz if (blk_bs(blk)) { 2550de2c6c05SMax Reitz blk_update_root_state(blk); 2551de2c6c05SMax Reitz } 2552de2c6c05SMax Reitz 2553de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2554156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2555156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2556de2c6c05SMax Reitz 255739ff43d9SMax Reitz if (!has_read_only) { 255839ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 255939ff43d9SMax Reitz } 256039ff43d9SMax Reitz 256139ff43d9SMax Reitz switch (read_only) { 256239ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 256339ff43d9SMax Reitz break; 256439ff43d9SMax Reitz 256539ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 256639ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 256739ff43d9SMax Reitz break; 256839ff43d9SMax Reitz 256939ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 257039ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 257139ff43d9SMax Reitz break; 257239ff43d9SMax Reitz 257339ff43d9SMax Reitz default: 257439ff43d9SMax Reitz abort(); 257539ff43d9SMax Reitz } 257639ff43d9SMax Reitz 2577de2c6c05SMax Reitz options = qdict_new(); 2578b85114f8SKevin Wolf detect_zeroes = blk_get_detect_zeroes_from_root_state(blk); 2579*46f5ac20SEric Blake qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off"); 2580b85114f8SKevin Wolf 2581b85114f8SKevin Wolf if (has_format) { 2582*46f5ac20SEric Blake qdict_put_str(options, "driver", format); 2583de2c6c05SMax Reitz } 2584de2c6c05SMax Reitz 25855b363937SMax Reitz medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 25865b363937SMax Reitz if (!medium_bs) { 2587de2c6c05SMax Reitz goto fail; 2588de2c6c05SMax Reitz } 2589de2c6c05SMax Reitz 2590de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2591de2c6c05SMax Reitz if (err) { 2592de2c6c05SMax Reitz error_propagate(errp, err); 2593de2c6c05SMax Reitz goto fail; 2594de2c6c05SMax Reitz } 2595de2c6c05SMax Reitz 259670e2cb3bSKevin Wolf rc = do_open_tray(has_device ? device : NULL, 259770e2cb3bSKevin Wolf has_id ? id : NULL, 259870e2cb3bSKevin Wolf false, &err); 259938a53d50SColin Lord if (rc && rc != -ENOSYS) { 2600de2c6c05SMax Reitz error_propagate(errp, err); 2601de2c6c05SMax Reitz goto fail; 2602de2c6c05SMax Reitz } 260338a53d50SColin Lord error_free(err); 260438a53d50SColin Lord err = NULL; 2605de2c6c05SMax Reitz 260624df38b0SKevin Wolf qmp_x_blockdev_remove_medium(has_device, device, has_id, id, &err); 2607de2c6c05SMax Reitz if (err) { 2608de2c6c05SMax Reitz error_propagate(errp, err); 2609de2c6c05SMax Reitz goto fail; 2610de2c6c05SMax Reitz } 2611de2c6c05SMax Reitz 2612716df217SKevin Wolf qmp_blockdev_insert_anon_medium(blk, medium_bs, &err); 2613de2c6c05SMax Reitz if (err) { 2614de2c6c05SMax Reitz error_propagate(errp, err); 2615de2c6c05SMax Reitz goto fail; 2616de2c6c05SMax Reitz } 2617de2c6c05SMax Reitz 261870e2cb3bSKevin Wolf qmp_blockdev_close_tray(has_device, device, has_id, id, errp); 2619de2c6c05SMax Reitz 2620de2c6c05SMax Reitz fail: 2621de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2622de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2623de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2624de2c6c05SMax Reitz bdrv_unref(medium_bs); 2625de2c6c05SMax Reitz } 2626de2c6c05SMax Reitz 2627727f005eSZhi Yong Wu /* throttling disk I/O limits */ 26284dc9397bSEric Blake void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) 2629727f005eSZhi Yong Wu { 2630cc0681c4SBenoît Canet ThrottleConfig cfg; 2631727f005eSZhi Yong Wu BlockDriverState *bs; 2632a0e8544cSFam Zheng BlockBackend *blk; 2633b15446fdSStefan Hajnoczi AioContext *aio_context; 2634727f005eSZhi Yong Wu 26357a9877a0SKevin Wolf blk = qmp_get_blk(arg->has_device ? arg->device : NULL, 26367a9877a0SKevin Wolf arg->has_id ? arg->id : NULL, 26377a9877a0SKevin Wolf errp); 2638a0e8544cSFam Zheng if (!blk) { 263980047da5SLuiz Capitulino return; 2640727f005eSZhi Yong Wu } 26415433c24fSMax Reitz 26425433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 26435433c24fSMax Reitz aio_context_acquire(aio_context); 26445433c24fSMax Reitz 2645a0e8544cSFam Zheng bs = blk_bs(blk); 26465433c24fSMax Reitz if (!bs) { 26477a9877a0SKevin Wolf error_setg(errp, "Device has no medium"); 26485433c24fSMax Reitz goto out; 26495433c24fSMax Reitz } 2650727f005eSZhi Yong Wu 26511588ab5dSAlberto Garcia throttle_config_init(&cfg); 26524dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; 26534dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; 26544dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; 2655727f005eSZhi Yong Wu 26564dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; 26574dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; 26584dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; 2659cc0681c4SBenoît Canet 26604dc9397bSEric Blake if (arg->has_bps_max) { 26614dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; 26623e9fab69SBenoît Canet } 26634dc9397bSEric Blake if (arg->has_bps_rd_max) { 26644dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; 26653e9fab69SBenoît Canet } 26664dc9397bSEric Blake if (arg->has_bps_wr_max) { 26674dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; 26683e9fab69SBenoît Canet } 26694dc9397bSEric Blake if (arg->has_iops_max) { 26704dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; 26713e9fab69SBenoît Canet } 26724dc9397bSEric Blake if (arg->has_iops_rd_max) { 26734dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; 26743e9fab69SBenoît Canet } 26754dc9397bSEric Blake if (arg->has_iops_wr_max) { 26764dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; 26773e9fab69SBenoît Canet } 2678cc0681c4SBenoît Canet 26794dc9397bSEric Blake if (arg->has_bps_max_length) { 26804dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; 2681dce13204SAlberto Garcia } 26824dc9397bSEric Blake if (arg->has_bps_rd_max_length) { 26834dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; 2684dce13204SAlberto Garcia } 26854dc9397bSEric Blake if (arg->has_bps_wr_max_length) { 26864dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; 2687dce13204SAlberto Garcia } 26884dc9397bSEric Blake if (arg->has_iops_max_length) { 26894dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; 2690dce13204SAlberto Garcia } 26914dc9397bSEric Blake if (arg->has_iops_rd_max_length) { 26924dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; 2693dce13204SAlberto Garcia } 26944dc9397bSEric Blake if (arg->has_iops_wr_max_length) { 26954dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; 2696dce13204SAlberto Garcia } 2697dce13204SAlberto Garcia 26984dc9397bSEric Blake if (arg->has_iops_size) { 26994dc9397bSEric Blake cfg.op_size = arg->iops_size; 27002024c1dfSBenoît Canet } 2701cc0681c4SBenoît Canet 2702d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 27035433c24fSMax Reitz goto out; 2704727f005eSZhi Yong Wu } 2705727f005eSZhi Yong Wu 270676f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 270776f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 270876f4afb4SAlberto Garcia * just update the throttling group. */ 270997148076SKevin Wolf if (!blk_get_public(blk)->throttle_state) { 27104dc9397bSEric Blake blk_io_limits_enable(blk, 27117a9877a0SKevin Wolf arg->has_group ? arg->group : 27127a9877a0SKevin Wolf arg->has_device ? arg->device : 27137a9877a0SKevin Wolf arg->id); 27144dc9397bSEric Blake } else if (arg->has_group) { 27154dc9397bSEric Blake blk_io_limits_update_group(blk, arg->group); 2716727f005eSZhi Yong Wu } 271776f4afb4SAlberto Garcia /* Set the new throttling configuration */ 271897148076SKevin Wolf blk_set_io_limits(blk, &cfg); 271997148076SKevin Wolf } else if (blk_get_public(blk)->throttle_state) { 272076f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 272197148076SKevin Wolf blk_io_limits_disable(blk); 2722727f005eSZhi Yong Wu } 2723b15446fdSStefan Hajnoczi 27245433c24fSMax Reitz out: 2725b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2726727f005eSZhi Yong Wu } 2727727f005eSZhi Yong Wu 2728341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2729341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2730341ebc2fSJohn Snow Error **errp) 2731341ebc2fSJohn Snow { 2732341ebc2fSJohn Snow AioContext *aio_context; 2733341ebc2fSJohn Snow BlockDriverState *bs; 2734341ebc2fSJohn Snow 2735341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2736341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2737341ebc2fSJohn Snow return; 2738341ebc2fSJohn Snow } 2739341ebc2fSJohn Snow 2740341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2741341ebc2fSJohn Snow if (!bs) { 2742341ebc2fSJohn Snow return; 2743341ebc2fSJohn Snow } 2744341ebc2fSJohn Snow 2745341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2746341ebc2fSJohn Snow aio_context_acquire(aio_context); 2747341ebc2fSJohn Snow 2748341ebc2fSJohn Snow if (has_granularity) { 2749341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2750341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2751341ebc2fSJohn Snow "and at least 512"); 2752341ebc2fSJohn Snow goto out; 2753341ebc2fSJohn Snow } 2754341ebc2fSJohn Snow } else { 2755341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2756341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2757341ebc2fSJohn Snow } 2758341ebc2fSJohn Snow 2759341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2760341ebc2fSJohn Snow 2761341ebc2fSJohn Snow out: 2762341ebc2fSJohn Snow aio_context_release(aio_context); 2763341ebc2fSJohn Snow } 2764341ebc2fSJohn Snow 2765341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2766341ebc2fSJohn Snow Error **errp) 2767341ebc2fSJohn Snow { 2768341ebc2fSJohn Snow AioContext *aio_context; 2769341ebc2fSJohn Snow BlockDriverState *bs; 2770341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2771341ebc2fSJohn Snow 2772341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2773341ebc2fSJohn Snow if (!bitmap || !bs) { 2774341ebc2fSJohn Snow return; 2775341ebc2fSJohn Snow } 2776341ebc2fSJohn Snow 27779bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 27789bd2b08fSJohn Snow error_setg(errp, 27799bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 27809bd2b08fSJohn Snow name); 27819bd2b08fSJohn Snow goto out; 27829bd2b08fSJohn Snow } 278320dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2784341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2785341ebc2fSJohn Snow 27869bd2b08fSJohn Snow out: 2787341ebc2fSJohn Snow aio_context_release(aio_context); 2788341ebc2fSJohn Snow } 2789341ebc2fSJohn Snow 2790e74e6b78SJohn Snow /** 2791e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2792e74e6b78SJohn Snow * immediately after a full backup operation. 2793e74e6b78SJohn Snow */ 2794e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2795e74e6b78SJohn Snow Error **errp) 2796e74e6b78SJohn Snow { 2797e74e6b78SJohn Snow AioContext *aio_context; 2798e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2799e74e6b78SJohn Snow BlockDriverState *bs; 2800e74e6b78SJohn Snow 2801e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2802e74e6b78SJohn Snow if (!bitmap || !bs) { 2803e74e6b78SJohn Snow return; 2804e74e6b78SJohn Snow } 2805e74e6b78SJohn Snow 2806e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2807e74e6b78SJohn Snow error_setg(errp, 2808e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2809e74e6b78SJohn Snow name); 2810e74e6b78SJohn Snow goto out; 2811e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2812e74e6b78SJohn Snow error_setg(errp, 2813e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2814e74e6b78SJohn Snow name); 2815e74e6b78SJohn Snow goto out; 2816e74e6b78SJohn Snow } 2817e74e6b78SJohn Snow 2818df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2819e74e6b78SJohn Snow 2820e74e6b78SJohn Snow out: 2821e74e6b78SJohn Snow aio_context_release(aio_context); 2822e74e6b78SJohn Snow } 2823e74e6b78SJohn Snow 2824072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 28259063f814SRyan Harper { 28269063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 28277e7d56d9SMarkus Armbruster BlockBackend *blk; 28289063f814SRyan Harper BlockDriverState *bs; 28298ad4202bSStefan Hajnoczi AioContext *aio_context; 28303718d8abSFam Zheng Error *local_err = NULL; 28319063f814SRyan Harper 28322073d410SKevin Wolf bs = bdrv_find_node(id); 28332073d410SKevin Wolf if (bs) { 283479b7a77eSMarkus Armbruster qmp_blockdev_del(id, &local_err); 28352073d410SKevin Wolf if (local_err) { 28362073d410SKevin Wolf error_report_err(local_err); 28372073d410SKevin Wolf } 28382073d410SKevin Wolf return; 28392073d410SKevin Wolf } 28402073d410SKevin Wolf 28417e7d56d9SMarkus Armbruster blk = blk_by_name(id); 28427e7d56d9SMarkus Armbruster if (!blk) { 2843b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2844072ebe6bSMarkus Armbruster return; 28459063f814SRyan Harper } 28468ad4202bSStefan Hajnoczi 284726f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 284848f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 284948f364ddSMarkus Armbruster " is not supported"); 2850072ebe6bSMarkus Armbruster return; 285148f364ddSMarkus Armbruster } 285248f364ddSMarkus Armbruster 28535433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 28548ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 28558ad4202bSStefan Hajnoczi 28565433c24fSMax Reitz bs = blk_bs(blk); 28575433c24fSMax Reitz if (bs) { 28583718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2859565f65d2SMarkus Armbruster error_report_err(local_err); 28608ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2861072ebe6bSMarkus Armbruster return; 28628591675fSMarcelo Tosatti } 28639063f814SRyan Harper 2864938abd43SMax Reitz blk_remove_bs(blk); 28655433c24fSMax Reitz } 28669063f814SRyan Harper 28677c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 2868efaa7c4eSMax Reitz monitor_remove_blk(blk); 2869efaa7c4eSMax Reitz 28707c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 28717c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 2872d22b2f41SRyan Harper */ 2873a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 2874293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2875373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2876293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2877d22b2f41SRyan Harper } else { 2878b9fe8a7aSMarkus Armbruster blk_unref(blk); 2879d22b2f41SRyan Harper } 28809063f814SRyan Harper 28818ad4202bSStefan Hajnoczi aio_context_release(aio_context); 28829063f814SRyan Harper } 28836d4a2b3aSChristoph Hellwig 28843b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 28853b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 28863b1dbd11SBenoît Canet int64_t size, Error **errp) 28876d4a2b3aSChristoph Hellwig { 28883b1dbd11SBenoît Canet Error *local_err = NULL; 28897dad9ee6SKevin Wolf BlockBackend *blk = NULL; 28906d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2891927e0e76SStefan Hajnoczi AioContext *aio_context; 28928732901eSKevin Wolf int ret; 28936d4a2b3aSChristoph Hellwig 28943b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 28953b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 28963b1dbd11SBenoît Canet &local_err); 289784d18f06SMarkus Armbruster if (local_err) { 28983b1dbd11SBenoît Canet error_propagate(errp, local_err); 28993b1dbd11SBenoît Canet return; 29003b1dbd11SBenoît Canet } 29013b1dbd11SBenoît Canet 2902927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2903927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2904927e0e76SStefan Hajnoczi 29053b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2906c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2907927e0e76SStefan Hajnoczi goto out; 29086d4a2b3aSChristoph Hellwig } 29096d4a2b3aSChristoph Hellwig 29106d4a2b3aSChristoph Hellwig if (size < 0) { 2911c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2912927e0e76SStefan Hajnoczi goto out; 29136d4a2b3aSChristoph Hellwig } 29146d4a2b3aSChristoph Hellwig 29159c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2916c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2917927e0e76SStefan Hajnoczi goto out; 29189c75e168SJeff Cody } 29199c75e168SJeff Cody 29206d0eb64dSKevin Wolf blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL); 2921d7086422SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 2922d7086422SKevin Wolf if (ret < 0) { 2923d7086422SKevin Wolf goto out; 2924d7086422SKevin Wolf } 29257dad9ee6SKevin Wolf 292692b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 292792b7a08dSPeter Lieven bdrv_drain_all(); 292892b7a08dSPeter Lieven 2929ed3d2ec9SMax Reitz ret = blk_truncate(blk, size, errp); 2930927e0e76SStefan Hajnoczi 2931927e0e76SStefan Hajnoczi out: 29327dad9ee6SKevin Wolf blk_unref(blk); 2933927e0e76SStefan Hajnoczi aio_context_release(aio_context); 29346d4a2b3aSChristoph Hellwig } 293512bd451fSStefan Hajnoczi 29362323322eSAlberto Garcia void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, 293713d8cc51SJeff Cody bool has_base, const char *base, 2938312fe09cSAlberto Garcia bool has_base_node, const char *base_node, 293913d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 294013d8cc51SJeff Cody bool has_speed, int64_t speed, 29411d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 29421d809098SPaolo Bonzini Error **errp) 294312bd451fSStefan Hajnoczi { 2944554b6147SAlberto Garcia BlockDriverState *bs, *iter; 2945c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 2946f3e69bebSStefan Hajnoczi AioContext *aio_context; 2947fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 294813d8cc51SJeff Cody const char *base_name = NULL; 294912bd451fSStefan Hajnoczi 29501d809098SPaolo Bonzini if (!has_on_error) { 29511d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 29521d809098SPaolo Bonzini } 29531d809098SPaolo Bonzini 2954554b6147SAlberto Garcia bs = bdrv_lookup_bs(device, device, errp); 2955b6c1bae5SKevin Wolf if (!bs) { 295612bd451fSStefan Hajnoczi return; 295712bd451fSStefan Hajnoczi } 295812bd451fSStefan Hajnoczi 2959b6c1bae5SKevin Wolf aio_context = bdrv_get_aio_context(bs); 2960f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 2961f3e69bebSStefan Hajnoczi 2962312fe09cSAlberto Garcia if (has_base && has_base_node) { 2963312fe09cSAlberto Garcia error_setg(errp, "'base' and 'base-node' cannot be specified " 2964312fe09cSAlberto Garcia "at the same time"); 2965312fe09cSAlberto Garcia goto out; 2966312fe09cSAlberto Garcia } 2967312fe09cSAlberto Garcia 296813d8cc51SJeff Cody if (has_base) { 2969c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 2970c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 2971c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 2972f3e69bebSStefan Hajnoczi goto out; 297312bd451fSStefan Hajnoczi } 2974f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 297513d8cc51SJeff Cody base_name = base; 2976c8c3080fSMarcelo Tosatti } 297712bd451fSStefan Hajnoczi 2978312fe09cSAlberto Garcia if (has_base_node) { 2979312fe09cSAlberto Garcia base_bs = bdrv_lookup_bs(NULL, base_node, errp); 2980312fe09cSAlberto Garcia if (!base_bs) { 2981312fe09cSAlberto Garcia goto out; 2982312fe09cSAlberto Garcia } 2983312fe09cSAlberto Garcia if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { 2984312fe09cSAlberto Garcia error_setg(errp, "Node '%s' is not a backing image of '%s'", 2985312fe09cSAlberto Garcia base_node, device); 2986312fe09cSAlberto Garcia goto out; 2987312fe09cSAlberto Garcia } 2988312fe09cSAlberto Garcia assert(bdrv_get_aio_context(base_bs) == aio_context); 2989312fe09cSAlberto Garcia base_name = base_bs->filename; 2990312fe09cSAlberto Garcia } 2991312fe09cSAlberto Garcia 2992554b6147SAlberto Garcia /* Check for op blockers in the whole chain between bs and base */ 2993554b6147SAlberto Garcia for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) { 2994554b6147SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { 2995554b6147SAlberto Garcia goto out; 2996554b6147SAlberto Garcia } 2997554b6147SAlberto Garcia } 2998554b6147SAlberto Garcia 299913d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 300013d8cc51SJeff Cody * file, and specifying one is therefore an error */ 300113d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 300213d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 300313d8cc51SJeff Cody "entire chain"); 3004f3e69bebSStefan Hajnoczi goto out; 300513d8cc51SJeff Cody } 300613d8cc51SJeff Cody 300713d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 300813d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 300913d8cc51SJeff Cody 30102323322eSAlberto Garcia stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, 30118254b6d9SJohn Snow has_speed ? speed : 0, on_error, &local_err); 301284d18f06SMarkus Armbruster if (local_err) { 3013fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3014f3e69bebSStefan Hajnoczi goto out; 301512bd451fSStefan Hajnoczi } 301612bd451fSStefan Hajnoczi 301712bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3018f3e69bebSStefan Hajnoczi 3019f3e69bebSStefan Hajnoczi out: 3020f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 302112bd451fSStefan Hajnoczi } 30222d47c6e9SStefan Hajnoczi 3023fd62c609SAlberto Garcia void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, 30247676e2c5SJeff Cody bool has_base, const char *base, 30257676e2c5SJeff Cody bool has_top, const char *top, 302654e26900SJeff Cody bool has_backing_file, const char *backing_file, 3027ed61fc10SJeff Cody bool has_speed, int64_t speed, 30280db832f4SKevin Wolf bool has_filter_node_name, const char *filter_node_name, 3029ed61fc10SJeff Cody Error **errp) 3030ed61fc10SJeff Cody { 3031ed61fc10SJeff Cody BlockDriverState *bs; 3032058223a6SAlberto Garcia BlockDriverState *iter; 3033ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 30349e85cd5cSStefan Hajnoczi AioContext *aio_context; 3035ed61fc10SJeff Cody Error *local_err = NULL; 3036ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3037ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3038ed61fc10SJeff Cody */ 303992aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3040ed61fc10SJeff Cody 304154504663SMax Reitz if (!has_speed) { 304254504663SMax Reitz speed = 0; 304354504663SMax Reitz } 30440db832f4SKevin Wolf if (!has_filter_node_name) { 30450db832f4SKevin Wolf filter_node_name = NULL; 30460db832f4SKevin Wolf } 304754504663SMax Reitz 30487676e2c5SJeff Cody /* Important Note: 30497676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 30507676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 30517676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 30527676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 30531d13b167SKevin Wolf bs = qmp_get_root_bs(device, &local_err); 30541d13b167SKevin Wolf if (!bs) { 30551d13b167SKevin Wolf bs = bdrv_lookup_bs(device, device, NULL); 30561d13b167SKevin Wolf if (!bs) { 30571d13b167SKevin Wolf error_free(local_err); 305875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 305975158ebbSMarkus Armbruster "Device '%s' not found", device); 30601d13b167SKevin Wolf } else { 30611d13b167SKevin Wolf error_propagate(errp, local_err); 30621d13b167SKevin Wolf } 3063ed61fc10SJeff Cody return; 3064ed61fc10SJeff Cody } 3065ed61fc10SJeff Cody 30661d13b167SKevin Wolf aio_context = bdrv_get_aio_context(bs); 30679e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 30689e85cd5cSStefan Hajnoczi 3069bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 30709e85cd5cSStefan Hajnoczi goto out; 3071628ff683SFam Zheng } 3072628ff683SFam Zheng 3073ed61fc10SJeff Cody /* default top_bs is the active layer */ 3074ed61fc10SJeff Cody top_bs = bs; 3075ed61fc10SJeff Cody 30767676e2c5SJeff Cody if (has_top && top) { 3077ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3078ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3079ed61fc10SJeff Cody } 3080ed61fc10SJeff Cody } 3081ed61fc10SJeff Cody 3082ed61fc10SJeff Cody if (top_bs == NULL) { 3083ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 30849e85cd5cSStefan Hajnoczi goto out; 3085ed61fc10SJeff Cody } 3086ed61fc10SJeff Cody 30879e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 30889e85cd5cSStefan Hajnoczi 3089d5208c45SJeff Cody if (has_base && base) { 3090d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3091d5208c45SJeff Cody } else { 3092d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3093d5208c45SJeff Cody } 3094d5208c45SJeff Cody 3095d5208c45SJeff Cody if (base_bs == NULL) { 3096c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 30979e85cd5cSStefan Hajnoczi goto out; 3098d5208c45SJeff Cody } 3099d5208c45SJeff Cody 31009e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 31019e85cd5cSStefan Hajnoczi 3102058223a6SAlberto Garcia for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) { 3103058223a6SAlberto Garcia if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3104bb00021dSFam Zheng goto out; 3105bb00021dSFam Zheng } 3106058223a6SAlberto Garcia } 3107bb00021dSFam Zheng 31087676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 31097676e2c5SJeff Cody if (top_bs == base_bs) { 31107676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 31119e85cd5cSStefan Hajnoczi goto out; 31127676e2c5SJeff Cody } 31137676e2c5SJeff Cody 311420a63d2cSFam Zheng if (top_bs == bs) { 311554e26900SJeff Cody if (has_backing_file) { 311654e26900SJeff Cody error_setg(errp, "'backing-file' specified," 311754e26900SJeff Cody " but 'top' is the active layer"); 31189e85cd5cSStefan Hajnoczi goto out; 311954e26900SJeff Cody } 312047970dfbSJohn Snow commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, 31210db832f4SKevin Wolf BLOCK_JOB_DEFAULT, speed, on_error, 312278bbd910SFam Zheng filter_node_name, NULL, NULL, false, &local_err); 312320a63d2cSFam Zheng } else { 3124058223a6SAlberto Garcia BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); 3125058223a6SAlberto Garcia if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3126058223a6SAlberto Garcia goto out; 3127058223a6SAlberto Garcia } 3128fd62c609SAlberto Garcia commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed, 31298254b6d9SJohn Snow on_error, has_backing_file ? backing_file : NULL, 31300db832f4SKevin Wolf filter_node_name, &local_err); 313120a63d2cSFam Zheng } 3132ed61fc10SJeff Cody if (local_err != NULL) { 3133ed61fc10SJeff Cody error_propagate(errp, local_err); 31349e85cd5cSStefan Hajnoczi goto out; 3135ed61fc10SJeff Cody } 31369e85cd5cSStefan Hajnoczi 31379e85cd5cSStefan Hajnoczi out: 31389e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3139ed61fc10SJeff Cody } 3140ed61fc10SJeff Cody 3141111049a4SJohn Snow static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, 3142111049a4SJohn Snow Error **errp) 314399a9addfSStefan Hajnoczi { 314499a9addfSStefan Hajnoczi BlockDriverState *bs; 314599a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3146fc5d3f84SIan Main BlockDriverState *source = NULL; 3147111049a4SJohn Snow BlockJob *job = NULL; 3148d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3149761731b1SStefan Hajnoczi AioContext *aio_context; 3150e6641719SMax Reitz QDict *options = NULL; 315199a9addfSStefan Hajnoczi Error *local_err = NULL; 315299a9addfSStefan Hajnoczi int flags; 315399a9addfSStefan Hajnoczi int64_t size; 315499a9addfSStefan Hajnoczi 315581206a89SPavel Butsykin if (!backup->has_speed) { 315681206a89SPavel Butsykin backup->speed = 0; 315799a9addfSStefan Hajnoczi } 315881206a89SPavel Butsykin if (!backup->has_on_source_error) { 315981206a89SPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 316099a9addfSStefan Hajnoczi } 316181206a89SPavel Butsykin if (!backup->has_on_target_error) { 316281206a89SPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 316399a9addfSStefan Hajnoczi } 316481206a89SPavel Butsykin if (!backup->has_mode) { 316581206a89SPavel Butsykin backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 316681206a89SPavel Butsykin } 316781206a89SPavel Butsykin if (!backup->has_job_id) { 316881206a89SPavel Butsykin backup->job_id = NULL; 316999a9addfSStefan Hajnoczi } 317013b9414bSPavel Butsykin if (!backup->has_compress) { 317113b9414bSPavel Butsykin backup->compress = false; 317213b9414bSPavel Butsykin } 317399a9addfSStefan Hajnoczi 317481206a89SPavel Butsykin bs = qmp_get_root_bs(backup->device, errp); 3175b7e4fa22SKevin Wolf if (!bs) { 3176111049a4SJohn Snow return NULL; 317799a9addfSStefan Hajnoczi } 317899a9addfSStefan Hajnoczi 3179b7e4fa22SKevin Wolf aio_context = bdrv_get_aio_context(bs); 3180761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3181761731b1SStefan Hajnoczi 318281206a89SPavel Butsykin if (!backup->has_format) { 318381206a89SPavel Butsykin backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? 318481206a89SPavel Butsykin NULL : (char*) bs->drv->format_name; 318599a9addfSStefan Hajnoczi } 318699a9addfSStefan Hajnoczi 3187c29c1dd3SFam Zheng /* Early check to avoid creating target */ 31883718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3189761731b1SStefan Hajnoczi goto out; 319099a9addfSStefan Hajnoczi } 319199a9addfSStefan Hajnoczi 319261de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 319399a9addfSStefan Hajnoczi 3194fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3195fc5d3f84SIan Main * on top of. */ 319681206a89SPavel Butsykin if (backup->sync == MIRROR_SYNC_MODE_TOP) { 3197760e0063SKevin Wolf source = backing_bs(bs); 3198fc5d3f84SIan Main if (!source) { 319981206a89SPavel Butsykin backup->sync = MIRROR_SYNC_MODE_FULL; 3200fc5d3f84SIan Main } 3201fc5d3f84SIan Main } 320281206a89SPavel Butsykin if (backup->sync == MIRROR_SYNC_MODE_NONE) { 3203fc5d3f84SIan Main source = bs; 3204fc5d3f84SIan Main } 3205fc5d3f84SIan Main 320699a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 320799a9addfSStefan Hajnoczi if (size < 0) { 320899a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3209761731b1SStefan Hajnoczi goto out; 321099a9addfSStefan Hajnoczi } 321199a9addfSStefan Hajnoczi 321281206a89SPavel Butsykin if (backup->mode != NEW_IMAGE_MODE_EXISTING) { 321381206a89SPavel Butsykin assert(backup->format); 3214fc5d3f84SIan Main if (source) { 321581206a89SPavel Butsykin bdrv_img_create(backup->target, backup->format, source->filename, 3216fc5d3f84SIan Main source->drv->format_name, NULL, 32179217283dSFam Zheng size, flags, false, &local_err); 3218fc5d3f84SIan Main } else { 321981206a89SPavel Butsykin bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, 32209217283dSFam Zheng size, flags, false, &local_err); 3221fc5d3f84SIan Main } 322299a9addfSStefan Hajnoczi } 322399a9addfSStefan Hajnoczi 322484d18f06SMarkus Armbruster if (local_err) { 322599a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3226761731b1SStefan Hajnoczi goto out; 322799a9addfSStefan Hajnoczi } 322899a9addfSStefan Hajnoczi 322981206a89SPavel Butsykin if (backup->format) { 3230e6641719SMax Reitz options = qdict_new(); 3231*46f5ac20SEric Blake qdict_put_str(options, "driver", backup->format); 3232e6641719SMax Reitz } 3233e6641719SMax Reitz 323481206a89SPavel Butsykin target_bs = bdrv_open(backup->target, NULL, options, flags, errp); 32355b363937SMax Reitz if (!target_bs) { 3236761731b1SStefan Hajnoczi goto out; 323799a9addfSStefan Hajnoczi } 323899a9addfSStefan Hajnoczi 3239761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3240761731b1SStefan Hajnoczi 324181206a89SPavel Butsykin if (backup->has_bitmap) { 324281206a89SPavel Butsykin bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); 3243d58d8453SJohn Snow if (!bmap) { 324481206a89SPavel Butsykin error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); 32450702d3d8SMax Reitz bdrv_unref(target_bs); 3246d58d8453SJohn Snow goto out; 3247d58d8453SJohn Snow } 3248d58d8453SJohn Snow } 3249d58d8453SJohn Snow 3250111049a4SJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3251111049a4SJohn Snow backup->sync, bmap, backup->compress, 3252111049a4SJohn Snow backup->on_source_error, backup->on_target_error, 3253111049a4SJohn Snow BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err); 32544f6fd349SFam Zheng bdrv_unref(target_bs); 32555c438bc6SKevin Wolf if (local_err != NULL) { 325699a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3257761731b1SStefan Hajnoczi goto out; 325899a9addfSStefan Hajnoczi } 3259761731b1SStefan Hajnoczi 3260761731b1SStefan Hajnoczi out: 3261761731b1SStefan Hajnoczi aio_context_release(aio_context); 3262111049a4SJohn Snow return job; 326399a9addfSStefan Hajnoczi } 326499a9addfSStefan Hajnoczi 326581206a89SPavel Butsykin void qmp_drive_backup(DriveBackup *arg, Error **errp) 326678f51fdeSJohn Snow { 3267111049a4SJohn Snow 3268111049a4SJohn Snow BlockJob *job; 3269111049a4SJohn Snow job = do_drive_backup(arg, NULL, errp); 3270111049a4SJohn Snow if (job) { 3271111049a4SJohn Snow block_job_start(job); 3272111049a4SJohn Snow } 327378f51fdeSJohn Snow } 327478f51fdeSJohn Snow 3275c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3276c13163fbSBenoît Canet { 3277d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3278c13163fbSBenoît Canet } 3279c13163fbSBenoît Canet 3280111049a4SJohn Snow BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, 3281111049a4SJohn Snow Error **errp) 3282c29c1dd3SFam Zheng { 3283c29c1dd3SFam Zheng BlockDriverState *bs; 3284c29c1dd3SFam Zheng BlockDriverState *target_bs; 3285c29c1dd3SFam Zheng Error *local_err = NULL; 3286c29c1dd3SFam Zheng AioContext *aio_context; 3287111049a4SJohn Snow BlockJob *job = NULL; 3288c29c1dd3SFam Zheng 3289dc7a4a9eSPavel Butsykin if (!backup->has_speed) { 3290dc7a4a9eSPavel Butsykin backup->speed = 0; 3291c29c1dd3SFam Zheng } 3292dc7a4a9eSPavel Butsykin if (!backup->has_on_source_error) { 3293dc7a4a9eSPavel Butsykin backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3294c29c1dd3SFam Zheng } 3295dc7a4a9eSPavel Butsykin if (!backup->has_on_target_error) { 3296dc7a4a9eSPavel Butsykin backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3297dc7a4a9eSPavel Butsykin } 3298dc7a4a9eSPavel Butsykin if (!backup->has_job_id) { 3299dc7a4a9eSPavel Butsykin backup->job_id = NULL; 3300c29c1dd3SFam Zheng } 33013b7b1236SPavel Butsykin if (!backup->has_compress) { 33023b7b1236SPavel Butsykin backup->compress = false; 33033b7b1236SPavel Butsykin } 3304c29c1dd3SFam Zheng 3305dc7a4a9eSPavel Butsykin bs = qmp_get_root_bs(backup->device, errp); 3306cef34eebSKevin Wolf if (!bs) { 3307111049a4SJohn Snow return NULL; 3308c29c1dd3SFam Zheng } 3309c29c1dd3SFam Zheng 3310cef34eebSKevin Wolf aio_context = bdrv_get_aio_context(bs); 3311c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3312c29c1dd3SFam Zheng 3313dc7a4a9eSPavel Butsykin target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); 33140d978913SFam Zheng if (!target_bs) { 3315c29c1dd3SFam Zheng goto out; 3316c29c1dd3SFam Zheng } 33175433c24fSMax Reitz 3318efd75567SFam Zheng if (bdrv_get_aio_context(target_bs) != aio_context) { 3319efd75567SFam Zheng if (!bdrv_has_blk(target_bs)) { 3320efd75567SFam Zheng /* The target BDS is not attached, we can safely move it to another 3321efd75567SFam Zheng * AioContext. */ 3322c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3323efd75567SFam Zheng } else { 3324efd75567SFam Zheng error_setg(errp, "Target is attached to a different thread from " 3325efd75567SFam Zheng "source."); 3326efd75567SFam Zheng goto out; 3327efd75567SFam Zheng } 3328efd75567SFam Zheng } 3329111049a4SJohn Snow job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3330111049a4SJohn Snow backup->sync, NULL, backup->compress, 3331111049a4SJohn Snow backup->on_source_error, backup->on_target_error, 3332111049a4SJohn Snow BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err); 3333c29c1dd3SFam Zheng if (local_err != NULL) { 3334c29c1dd3SFam Zheng error_propagate(errp, local_err); 3335c29c1dd3SFam Zheng } 3336c29c1dd3SFam Zheng out: 3337c29c1dd3SFam Zheng aio_context_release(aio_context); 3338111049a4SJohn Snow return job; 3339c29c1dd3SFam Zheng } 3340c29c1dd3SFam Zheng 3341dc7a4a9eSPavel Butsykin void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp) 334278f51fdeSJohn Snow { 3343111049a4SJohn Snow BlockJob *job; 3344111049a4SJohn Snow job = do_blockdev_backup(arg, NULL, errp); 3345111049a4SJohn Snow if (job) { 3346111049a4SJohn Snow block_job_start(job); 3347111049a4SJohn Snow } 334878f51fdeSJohn Snow } 334978f51fdeSJohn Snow 33504193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 33514193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 33524193cdd7SFam Zheng **/ 335371aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, 33544193cdd7SFam Zheng BlockDriverState *target, 335509158f00SBenoît Canet bool has_replaces, const char *replaces, 3356d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3357274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 3358b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3359eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 336008e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 33614193cdd7SFam Zheng bool has_on_source_error, 33624193cdd7SFam Zheng BlockdevOnError on_source_error, 33634193cdd7SFam Zheng bool has_on_target_error, 33644193cdd7SFam Zheng BlockdevOnError on_target_error, 33650fc9f8eaSFam Zheng bool has_unmap, bool unmap, 33666cdbceb1SKevin Wolf bool has_filter_node_name, 33676cdbceb1SKevin Wolf const char *filter_node_name, 3368b952b558SPaolo Bonzini Error **errp) 3369d9b902dbSPaolo Bonzini { 3370d9b902dbSPaolo Bonzini 3371d9b902dbSPaolo Bonzini if (!has_speed) { 3372d9b902dbSPaolo Bonzini speed = 0; 3373d9b902dbSPaolo Bonzini } 3374b952b558SPaolo Bonzini if (!has_on_source_error) { 3375b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3376b952b558SPaolo Bonzini } 3377b952b558SPaolo Bonzini if (!has_on_target_error) { 3378b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3379b952b558SPaolo Bonzini } 3380eee13dfeSPaolo Bonzini if (!has_granularity) { 3381eee13dfeSPaolo Bonzini granularity = 0; 3382eee13dfeSPaolo Bonzini } 338308e4ed6cSPaolo Bonzini if (!has_buf_size) { 338448ac0a4dSWen Congyang buf_size = 0; 338508e4ed6cSPaolo Bonzini } 33860fc9f8eaSFam Zheng if (!has_unmap) { 33870fc9f8eaSFam Zheng unmap = true; 33880fc9f8eaSFam Zheng } 33896cdbceb1SKevin Wolf if (!has_filter_node_name) { 33906cdbceb1SKevin Wolf filter_node_name = NULL; 33916cdbceb1SKevin Wolf } 339208e4ed6cSPaolo Bonzini 3393eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3394c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 33953cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3396eee13dfeSPaolo Bonzini return; 3397eee13dfeSPaolo Bonzini } 3398eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3399c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3400c6bd8c70SMarkus Armbruster "power of 2"); 3401eee13dfeSPaolo Bonzini return; 3402eee13dfeSPaolo Bonzini } 3403d9b902dbSPaolo Bonzini 34044193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 34054193cdd7SFam Zheng return; 34064193cdd7SFam Zheng } 3407e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3408e40e5027SFam Zheng return; 3409e40e5027SFam Zheng } 34104193cdd7SFam Zheng 34114193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 34124193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 34134193cdd7SFam Zheng } 34144193cdd7SFam Zheng 34154193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 34164193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 34174193cdd7SFam Zheng */ 341871aa9867SAlberto Garcia mirror_start(job_id, bs, target, 34194193cdd7SFam Zheng has_replaces ? replaces : NULL, 3420274fcceeSMax Reitz speed, granularity, buf_size, sync, backing_mode, 34216cdbceb1SKevin Wolf on_source_error, on_target_error, unmap, filter_node_name, 34226cdbceb1SKevin Wolf errp); 34234193cdd7SFam Zheng } 34244193cdd7SFam Zheng 3425faecd40aSEric Blake void qmp_drive_mirror(DriveMirror *arg, Error **errp) 34264193cdd7SFam Zheng { 34274193cdd7SFam Zheng BlockDriverState *bs; 34284193cdd7SFam Zheng BlockDriverState *source, *target_bs; 34294193cdd7SFam Zheng AioContext *aio_context; 3430274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 34314193cdd7SFam Zheng Error *local_err = NULL; 34324193cdd7SFam Zheng QDict *options = NULL; 34334193cdd7SFam Zheng int flags; 34344193cdd7SFam Zheng int64_t size; 3435faecd40aSEric Blake const char *format = arg->format; 34364193cdd7SFam Zheng 34370524e93aSKevin Wolf bs = qmp_get_root_bs(arg->device, errp); 34380524e93aSKevin Wolf if (!bs) { 3439d9b902dbSPaolo Bonzini return; 3440d9b902dbSPaolo Bonzini } 3441d9b902dbSPaolo Bonzini 34420524e93aSKevin Wolf aio_context = bdrv_get_aio_context(bs); 34435a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 34445a7e7a0bSStefan Hajnoczi 3445faecd40aSEric Blake if (!arg->has_mode) { 3446faecd40aSEric Blake arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 34474193cdd7SFam Zheng } 3448d9b902dbSPaolo Bonzini 3449faecd40aSEric Blake if (!arg->has_format) { 3450faecd40aSEric Blake format = (arg->mode == NEW_IMAGE_MODE_EXISTING 3451faecd40aSEric Blake ? NULL : bs->drv->format_name); 3452d9b902dbSPaolo Bonzini } 3453d9b902dbSPaolo Bonzini 345461de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3455760e0063SKevin Wolf source = backing_bs(bs); 3456faecd40aSEric Blake if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { 3457faecd40aSEric Blake arg->sync = MIRROR_SYNC_MODE_FULL; 3458d9b902dbSPaolo Bonzini } 3459faecd40aSEric Blake if (arg->sync == MIRROR_SYNC_MODE_NONE) { 3460117e0c82SMax Reitz source = bs; 3461117e0c82SMax Reitz } 3462d9b902dbSPaolo Bonzini 3463ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3464ac3c5d83SStefan Hajnoczi if (size < 0) { 3465ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 34665a7e7a0bSStefan Hajnoczi goto out; 3467ac3c5d83SStefan Hajnoczi } 3468ac3c5d83SStefan Hajnoczi 3469faecd40aSEric Blake if (arg->has_replaces) { 347009158f00SBenoît Canet BlockDriverState *to_replace_bs; 34715a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 34725a7e7a0bSStefan Hajnoczi int64_t replace_size; 347309158f00SBenoît Canet 3474faecd40aSEric Blake if (!arg->has_node_name) { 347509158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 347609158f00SBenoît Canet " named node of the graph"); 34775a7e7a0bSStefan Hajnoczi goto out; 347809158f00SBenoît Canet } 347909158f00SBenoît Canet 3480faecd40aSEric Blake to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err); 348109158f00SBenoît Canet 348209158f00SBenoît Canet if (!to_replace_bs) { 348309158f00SBenoît Canet error_propagate(errp, local_err); 34845a7e7a0bSStefan Hajnoczi goto out; 348509158f00SBenoît Canet } 348609158f00SBenoît Canet 34875a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 34885a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 34895a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 34905a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 34915a7e7a0bSStefan Hajnoczi 34925a7e7a0bSStefan Hajnoczi if (size != replace_size) { 349309158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 349409158f00SBenoît Canet "different size"); 34955a7e7a0bSStefan Hajnoczi goto out; 349609158f00SBenoît Canet } 349709158f00SBenoît Canet } 349809158f00SBenoît Canet 3499faecd40aSEric Blake if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { 3500274fcceeSMax Reitz backing_mode = MIRROR_SOURCE_BACKING_CHAIN; 3501274fcceeSMax Reitz } else { 3502274fcceeSMax Reitz backing_mode = MIRROR_OPEN_BACKING_CHAIN; 3503274fcceeSMax Reitz } 3504274fcceeSMax Reitz 3505faecd40aSEric Blake if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) 3506faecd40aSEric Blake && arg->mode != NEW_IMAGE_MODE_EXISTING) 350714526864SMax Reitz { 3508d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3509e6641719SMax Reitz assert(format); 3510faecd40aSEric Blake bdrv_img_create(arg->target, format, 35119217283dSFam Zheng NULL, NULL, NULL, size, flags, false, &local_err); 3512d9b902dbSPaolo Bonzini } else { 3513faecd40aSEric Blake switch (arg->mode) { 3514d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3515d9b902dbSPaolo Bonzini break; 3516d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3517d9b902dbSPaolo Bonzini /* create new image with backing file */ 3518faecd40aSEric Blake bdrv_img_create(arg->target, format, 3519d9b902dbSPaolo Bonzini source->filename, 3520d9b902dbSPaolo Bonzini source->drv->format_name, 35219217283dSFam Zheng NULL, size, flags, false, &local_err); 3522d9b902dbSPaolo Bonzini break; 3523d9b902dbSPaolo Bonzini default: 3524d9b902dbSPaolo Bonzini abort(); 3525d9b902dbSPaolo Bonzini } 3526d9b902dbSPaolo Bonzini } 3527d9b902dbSPaolo Bonzini 352884d18f06SMarkus Armbruster if (local_err) { 3529cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 35305a7e7a0bSStefan Hajnoczi goto out; 3531d9b902dbSPaolo Bonzini } 3532d9b902dbSPaolo Bonzini 35334c828dc6SBenoît Canet options = qdict_new(); 3534faecd40aSEric Blake if (arg->has_node_name) { 3535*46f5ac20SEric Blake qdict_put_str(options, "node-name", arg->node_name); 35364c828dc6SBenoît Canet } 3537e6641719SMax Reitz if (format) { 3538*46f5ac20SEric Blake qdict_put_str(options, "driver", format); 3539e6641719SMax Reitz } 35404c828dc6SBenoît Canet 3541b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3542b812f671SPaolo Bonzini * file. 3543b812f671SPaolo Bonzini */ 3544faecd40aSEric Blake target_bs = bdrv_open(arg->target, NULL, options, 3545faecd40aSEric Blake flags | BDRV_O_NO_BACKING, errp); 35465b363937SMax Reitz if (!target_bs) { 35475a7e7a0bSStefan Hajnoczi goto out; 3548d9b902dbSPaolo Bonzini } 3549d9b902dbSPaolo Bonzini 35505a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 35515a7e7a0bSStefan Hajnoczi 3552faecd40aSEric Blake blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, 3553faecd40aSEric Blake arg->has_replaces, arg->replaces, arg->sync, 3554faecd40aSEric Blake backing_mode, arg->has_speed, arg->speed, 3555faecd40aSEric Blake arg->has_granularity, arg->granularity, 3556faecd40aSEric Blake arg->has_buf_size, arg->buf_size, 3557faecd40aSEric Blake arg->has_on_source_error, arg->on_source_error, 3558faecd40aSEric Blake arg->has_on_target_error, arg->on_target_error, 3559faecd40aSEric Blake arg->has_unmap, arg->unmap, 35606cdbceb1SKevin Wolf false, NULL, 35614193cdd7SFam Zheng &local_err); 3562e253f4b8SKevin Wolf bdrv_unref(target_bs); 3563d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 35645a7e7a0bSStefan Hajnoczi out: 35655a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3566d9b902dbSPaolo Bonzini } 3567d9b902dbSPaolo Bonzini 356871aa9867SAlberto Garcia void qmp_blockdev_mirror(bool has_job_id, const char *job_id, 356971aa9867SAlberto Garcia const char *device, const char *target, 3570df92562eSFam Zheng bool has_replaces, const char *replaces, 3571df92562eSFam Zheng MirrorSyncMode sync, 3572df92562eSFam Zheng bool has_speed, int64_t speed, 3573df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3574df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3575df92562eSFam Zheng bool has_on_source_error, 3576df92562eSFam Zheng BlockdevOnError on_source_error, 3577df92562eSFam Zheng bool has_on_target_error, 3578df92562eSFam Zheng BlockdevOnError on_target_error, 35796cdbceb1SKevin Wolf bool has_filter_node_name, 35806cdbceb1SKevin Wolf const char *filter_node_name, 3581df92562eSFam Zheng Error **errp) 3582df92562eSFam Zheng { 3583df92562eSFam Zheng BlockDriverState *bs; 3584df92562eSFam Zheng BlockDriverState *target_bs; 3585df92562eSFam Zheng AioContext *aio_context; 3586274fcceeSMax Reitz BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; 3587df92562eSFam Zheng Error *local_err = NULL; 3588df92562eSFam Zheng 358907eec652SKevin Wolf bs = qmp_get_root_bs(device, errp); 3590df92562eSFam Zheng if (!bs) { 3591df92562eSFam Zheng return; 3592df92562eSFam Zheng } 3593df92562eSFam Zheng 3594df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3595df92562eSFam Zheng if (!target_bs) { 3596df92562eSFam Zheng return; 3597df92562eSFam Zheng } 3598df92562eSFam Zheng 3599df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3600df92562eSFam Zheng aio_context_acquire(aio_context); 3601df92562eSFam Zheng 3602df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3603df92562eSFam Zheng 360471aa9867SAlberto Garcia blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 3605274fcceeSMax Reitz has_replaces, replaces, sync, backing_mode, 3606df92562eSFam Zheng has_speed, speed, 3607df92562eSFam Zheng has_granularity, granularity, 3608df92562eSFam Zheng has_buf_size, buf_size, 3609df92562eSFam Zheng has_on_source_error, on_source_error, 3610df92562eSFam Zheng has_on_target_error, on_target_error, 3611df92562eSFam Zheng true, true, 36126cdbceb1SKevin Wolf has_filter_node_name, filter_node_name, 3613df92562eSFam Zheng &local_err); 3614df92562eSFam Zheng error_propagate(errp, local_err); 3615df92562eSFam Zheng 3616df92562eSFam Zheng aio_context_release(aio_context); 3617df92562eSFam Zheng } 3618df92562eSFam Zheng 36193ddf3efeSAlberto Garcia /* Get a block job using its ID and acquire its AioContext */ 36203ddf3efeSAlberto Garcia static BlockJob *find_block_job(const char *id, AioContext **aio_context, 362124d6bffeSMarkus Armbruster Error **errp) 36222d47c6e9SStefan Hajnoczi { 36233ddf3efeSAlberto Garcia BlockJob *job; 36243ddf3efeSAlberto Garcia 36253ddf3efeSAlberto Garcia assert(id != NULL); 36262d47c6e9SStefan Hajnoczi 36275433c24fSMax Reitz *aio_context = NULL; 36285433c24fSMax Reitz 36293ddf3efeSAlberto Garcia job = block_job_get(id); 36303ddf3efeSAlberto Garcia 36313ddf3efeSAlberto Garcia if (!job) { 36323ddf3efeSAlberto Garcia error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 36333ddf3efeSAlberto Garcia "Block job '%s' not found", id); 36343ddf3efeSAlberto Garcia return NULL; 36352d47c6e9SStefan Hajnoczi } 36363d948cdfSStefan Hajnoczi 36373ddf3efeSAlberto Garcia *aio_context = blk_get_aio_context(job->blk); 36383d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 36393d948cdfSStefan Hajnoczi 36403ddf3efeSAlberto Garcia return job; 36412d47c6e9SStefan Hajnoczi } 36422d47c6e9SStefan Hajnoczi 3643882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 36442d47c6e9SStefan Hajnoczi { 36453d948cdfSStefan Hajnoczi AioContext *aio_context; 364624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36472d47c6e9SStefan Hajnoczi 36482d47c6e9SStefan Hajnoczi if (!job) { 36492d47c6e9SStefan Hajnoczi return; 36502d47c6e9SStefan Hajnoczi } 36512d47c6e9SStefan Hajnoczi 3652882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 36533d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36542d47c6e9SStefan Hajnoczi } 3655370521a1SStefan Hajnoczi 36566e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 36576e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 36586e37fb81SPaolo Bonzini { 36593d948cdfSStefan Hajnoczi AioContext *aio_context; 366024d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36616e37fb81SPaolo Bonzini 36626e37fb81SPaolo Bonzini if (!job) { 36636e37fb81SPaolo Bonzini return; 36646e37fb81SPaolo Bonzini } 36653d948cdfSStefan Hajnoczi 36663d948cdfSStefan Hajnoczi if (!has_force) { 36673d948cdfSStefan Hajnoczi force = false; 36683d948cdfSStefan Hajnoczi } 36693d948cdfSStefan Hajnoczi 36700df4ba58SJohn Snow if (block_job_user_paused(job) && !force) { 3671f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3672f231b88dSCole Robinson device); 36733d948cdfSStefan Hajnoczi goto out; 36746e37fb81SPaolo Bonzini } 36756e37fb81SPaolo Bonzini 36766e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 36776e37fb81SPaolo Bonzini block_job_cancel(job); 36783d948cdfSStefan Hajnoczi out: 36793d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36806e37fb81SPaolo Bonzini } 36816e37fb81SPaolo Bonzini 36826e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3683370521a1SStefan Hajnoczi { 36843d948cdfSStefan Hajnoczi AioContext *aio_context; 368524d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3686370521a1SStefan Hajnoczi 36870df4ba58SJohn Snow if (!job || block_job_user_paused(job)) { 3688370521a1SStefan Hajnoczi return; 3689370521a1SStefan Hajnoczi } 36906e37fb81SPaolo Bonzini 36916e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 36920df4ba58SJohn Snow block_job_user_pause(job); 36933d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36946e37fb81SPaolo Bonzini } 36956e37fb81SPaolo Bonzini 36966e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 36976e37fb81SPaolo Bonzini { 36983d948cdfSStefan Hajnoczi AioContext *aio_context; 369924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37006e37fb81SPaolo Bonzini 37010df4ba58SJohn Snow if (!job || !block_job_user_paused(job)) { 37028acc72a4SPaolo Bonzini return; 37038acc72a4SPaolo Bonzini } 3704370521a1SStefan Hajnoczi 37056e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 370617bd51f9SStefan Hajnoczi block_job_iostatus_reset(job); 37070df4ba58SJohn Snow block_job_user_resume(job); 37083d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3709370521a1SStefan Hajnoczi } 3710fb5458cdSStefan Hajnoczi 3711aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3712aeae883bSPaolo Bonzini { 37133d948cdfSStefan Hajnoczi AioContext *aio_context; 371424d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3715aeae883bSPaolo Bonzini 3716aeae883bSPaolo Bonzini if (!job) { 3717aeae883bSPaolo Bonzini return; 3718aeae883bSPaolo Bonzini } 3719aeae883bSPaolo Bonzini 3720aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3721aeae883bSPaolo Bonzini block_job_complete(job, errp); 37223d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3723aeae883bSPaolo Bonzini } 3724aeae883bSPaolo Bonzini 3725fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3726fa40e656SJeff Cody const char *image_node_name, 3727fa40e656SJeff Cody const char *backing_file, 3728fa40e656SJeff Cody Error **errp) 3729fa40e656SJeff Cody { 3730fa40e656SJeff Cody BlockDriverState *bs = NULL; 3731729962f6SStefan Hajnoczi AioContext *aio_context; 3732fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3733fa40e656SJeff Cody Error *local_err = NULL; 3734fa40e656SJeff Cody bool ro; 3735fa40e656SJeff Cody int open_flags; 3736fa40e656SJeff Cody int ret; 3737fa40e656SJeff Cody 37387b5dca3fSKevin Wolf bs = qmp_get_root_bs(device, errp); 37397b5dca3fSKevin Wolf if (!bs) { 3740fa40e656SJeff Cody return; 3741fa40e656SJeff Cody } 3742fa40e656SJeff Cody 37437b5dca3fSKevin Wolf aio_context = bdrv_get_aio_context(bs); 3744729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3745729962f6SStefan Hajnoczi 3746fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3747fa40e656SJeff Cody if (local_err) { 3748fa40e656SJeff Cody error_propagate(errp, local_err); 3749729962f6SStefan Hajnoczi goto out; 3750fa40e656SJeff Cody } 3751fa40e656SJeff Cody 3752fa40e656SJeff Cody if (!image_bs) { 3753fa40e656SJeff Cody error_setg(errp, "image file not found"); 3754729962f6SStefan Hajnoczi goto out; 3755fa40e656SJeff Cody } 3756fa40e656SJeff Cody 3757fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3758fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3759fa40e656SJeff Cody "without a backing file"); 3760729962f6SStefan Hajnoczi goto out; 3761fa40e656SJeff Cody } 3762fa40e656SJeff Cody 3763fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3764fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3765fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3766729962f6SStefan Hajnoczi goto out; 3767fa40e656SJeff Cody } 3768fa40e656SJeff Cody 3769fa40e656SJeff Cody /* final sanity check */ 3770fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3771fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3772fa40e656SJeff Cody device); 3773729962f6SStefan Hajnoczi goto out; 3774fa40e656SJeff Cody } 3775fa40e656SJeff Cody 3776fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3777fa40e656SJeff Cody open_flags = image_bs->open_flags; 3778fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3779fa40e656SJeff Cody 3780fa40e656SJeff Cody if (ro) { 3781fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3782fa40e656SJeff Cody if (local_err) { 3783fa40e656SJeff Cody error_propagate(errp, local_err); 3784729962f6SStefan Hajnoczi goto out; 3785fa40e656SJeff Cody } 3786fa40e656SJeff Cody } 3787fa40e656SJeff Cody 3788fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3789fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3790fa40e656SJeff Cody 3791fa40e656SJeff Cody if (ret < 0) { 3792fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3793fa40e656SJeff Cody backing_file); 3794fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3795fa40e656SJeff Cody * appropriate */ 3796fa40e656SJeff Cody } 3797fa40e656SJeff Cody 3798fa40e656SJeff Cody if (ro) { 3799fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3800621ff94dSEduardo Habkost error_propagate(errp, local_err); 3801fa40e656SJeff Cody } 3802729962f6SStefan Hajnoczi 3803729962f6SStefan Hajnoczi out: 3804729962f6SStefan Hajnoczi aio_context_release(aio_context); 3805fa40e656SJeff Cody } 3806fa40e656SJeff Cody 3807abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 3808abb21ac3SKevin Wolf { 3809abb21ac3SKevin Wolf QemuOpts *opts; 3810abb21ac3SKevin Wolf QDict *qdict; 3811abb21ac3SKevin Wolf Error *local_err = NULL; 3812abb21ac3SKevin Wolf 3813abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 3814abb21ac3SKevin Wolf if (!opts) { 3815abb21ac3SKevin Wolf return; 3816abb21ac3SKevin Wolf } 3817abb21ac3SKevin Wolf 3818abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 3819abb21ac3SKevin Wolf 3820abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 3821f8746fb8SKevin Wolf QDECREF(qdict); 3822abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 3823abb21ac3SKevin Wolf goto out; 3824abb21ac3SKevin Wolf } 3825abb21ac3SKevin Wolf 3826abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 3827abb21ac3SKevin Wolf if (!bs) { 3828abb21ac3SKevin Wolf error_report_err(local_err); 3829abb21ac3SKevin Wolf goto out; 3830abb21ac3SKevin Wolf } 3831abb21ac3SKevin Wolf 3832abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3833abb21ac3SKevin Wolf 3834abb21ac3SKevin Wolf out: 3835abb21ac3SKevin Wolf qemu_opts_del(opts); 3836abb21ac3SKevin Wolf } 3837abb21ac3SKevin Wolf 3838d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3839d26c9a15SKevin Wolf { 3840be4b67bcSMax Reitz BlockDriverState *bs; 3841d26c9a15SKevin Wolf QObject *obj; 38427d5e199aSDaniel P. Berrange Visitor *v = qobject_output_visitor_new(&obj); 3843d26c9a15SKevin Wolf QDict *qdict; 3844d26c9a15SKevin Wolf Error *local_err = NULL; 3845d26c9a15SKevin Wolf 38463b098d56SEric Blake visit_type_BlockdevOptions(v, NULL, &options, &local_err); 384784d18f06SMarkus Armbruster if (local_err) { 3848d26c9a15SKevin Wolf error_propagate(errp, local_err); 3849d26c9a15SKevin Wolf goto fail; 3850d26c9a15SKevin Wolf } 3851d26c9a15SKevin Wolf 38523b098d56SEric Blake visit_complete(v, &obj); 3853d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3854d26c9a15SKevin Wolf 3855d26c9a15SKevin Wolf qdict_flatten(qdict); 3856d26c9a15SKevin Wolf 3857be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 38589ec8873eSKevin Wolf error_setg(errp, "'node-name' must be specified for the root node"); 3859be4b67bcSMax Reitz goto fail; 3860be4b67bcSMax Reitz } 3861be4b67bcSMax Reitz 3862bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3863bd745e23SMax Reitz if (!bs) { 3864be4b67bcSMax Reitz goto fail; 3865be4b67bcSMax Reitz } 38669c4218e9SMax Reitz 38679c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3868be4b67bcSMax Reitz 3869be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 38709c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 3871be4b67bcSMax Reitz bdrv_unref(bs); 38728ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 38738ae8e904SKevin Wolf goto fail; 38748ae8e904SKevin Wolf } 38758ae8e904SKevin Wolf 3876d26c9a15SKevin Wolf fail: 38773b098d56SEric Blake visit_free(v); 3878d26c9a15SKevin Wolf } 3879d26c9a15SKevin Wolf 388079b7a77eSMarkus Armbruster void qmp_blockdev_del(const char *node_name, Error **errp) 388181b936aeSAlberto Garcia { 388281b936aeSAlberto Garcia AioContext *aio_context; 388381b936aeSAlberto Garcia BlockDriverState *bs; 388481b936aeSAlberto Garcia 388581b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 388681b936aeSAlberto Garcia if (!bs) { 388781b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 388881b936aeSAlberto Garcia return; 388981b936aeSAlberto Garcia } 38901f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 3891e467da7bSKevin Wolf error_setg(errp, "Node %s is in use", node_name); 389281b936aeSAlberto Garcia return; 389381b936aeSAlberto Garcia } 389481b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 389581b936aeSAlberto Garcia aio_context_acquire(aio_context); 389681b936aeSAlberto Garcia 389781b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 389881b936aeSAlberto Garcia goto out; 389981b936aeSAlberto Garcia } 390081b936aeSAlberto Garcia 39019ec8873eSKevin Wolf if (!bs->monitor_list.tqe_prev) { 39029c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 39039c4218e9SMax Reitz bs->node_name); 39049c4218e9SMax Reitz goto out; 39059c4218e9SMax Reitz } 39069c4218e9SMax Reitz 39079c4218e9SMax Reitz if (bs->refcnt > 1) { 390881b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 390981b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 391081b936aeSAlberto Garcia goto out; 391181b936aeSAlberto Garcia } 391281b936aeSAlberto Garcia 39139c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 391481b936aeSAlberto Garcia bdrv_unref(bs); 391581b936aeSAlberto Garcia 391681b936aeSAlberto Garcia out: 391781b936aeSAlberto Garcia aio_context_release(aio_context); 391881b936aeSAlberto Garcia } 391981b936aeSAlberto Garcia 39207f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 39217f821597SWen Congyang const char *child_name) 39227f821597SWen Congyang { 39237f821597SWen Congyang BdrvChild *child; 39247f821597SWen Congyang 39257f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 39267f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 39277f821597SWen Congyang return child; 39287f821597SWen Congyang } 39297f821597SWen Congyang } 39307f821597SWen Congyang 39317f821597SWen Congyang return NULL; 39327f821597SWen Congyang } 39337f821597SWen Congyang 39347f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 39357f821597SWen Congyang const char *child, bool has_node, 39367f821597SWen Congyang const char *node, Error **errp) 39377f821597SWen Congyang { 39387f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 39397f821597SWen Congyang BdrvChild *p_child; 39407f821597SWen Congyang 39417f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 39427f821597SWen Congyang if (!parent_bs) { 39437f821597SWen Congyang return; 39447f821597SWen Congyang } 39457f821597SWen Congyang 39467f821597SWen Congyang if (has_child == has_node) { 39477f821597SWen Congyang if (has_child) { 39487f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 39497f821597SWen Congyang } else { 39507f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 39517f821597SWen Congyang } 39527f821597SWen Congyang return; 39537f821597SWen Congyang } 39547f821597SWen Congyang 39557f821597SWen Congyang if (has_child) { 39567f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 39577f821597SWen Congyang if (!p_child) { 39587f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 39597f821597SWen Congyang parent, child); 39607f821597SWen Congyang return; 39617f821597SWen Congyang } 39627f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 39637f821597SWen Congyang } 39647f821597SWen Congyang 39657f821597SWen Congyang if (has_node) { 39667f821597SWen Congyang new_bs = bdrv_find_node(node); 39677f821597SWen Congyang if (!new_bs) { 39687f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 39697f821597SWen Congyang return; 39707f821597SWen Congyang } 39717f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 39727f821597SWen Congyang } 39737f821597SWen Congyang } 39747f821597SWen Congyang 3975fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 3976fb5458cdSStefan Hajnoczi { 3977fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 3978f0f55dedSAlberto Garcia BlockJob *job; 3979fea68bb6SMarkus Armbruster 3980f0f55dedSAlberto Garcia for (job = block_job_next(NULL); job; job = block_job_next(job)) { 3981559b935fSJohn Snow BlockJobInfoList *elem; 3982559b935fSJohn Snow AioContext *aio_context; 398369691e72SStefan Hajnoczi 3984559b935fSJohn Snow if (block_job_is_internal(job)) { 3985559b935fSJohn Snow continue; 3986559b935fSJohn Snow } 3987559b935fSJohn Snow elem = g_new0(BlockJobInfoList, 1); 3988559b935fSJohn Snow aio_context = blk_get_aio_context(job->blk); 398969691e72SStefan Hajnoczi aio_context_acquire(aio_context); 3990559b935fSJohn Snow elem->value = block_job_query(job, errp); 3991f0f55dedSAlberto Garcia aio_context_release(aio_context); 3992559b935fSJohn Snow if (!elem->value) { 3993559b935fSJohn Snow g_free(elem); 3994559b935fSJohn Snow qapi_free_BlockJobInfoList(head); 3995559b935fSJohn Snow return NULL; 3996559b935fSJohn Snow } 3997fea68bb6SMarkus Armbruster *p_next = elem; 3998fea68bb6SMarkus Armbruster p_next = &elem->next; 3999fea68bb6SMarkus Armbruster } 400069691e72SStefan Hajnoczi 4001fea68bb6SMarkus Armbruster return head; 4002fb5458cdSStefan Hajnoczi } 40034d454574SPaolo Bonzini 40040006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 40054d454574SPaolo Bonzini .name = "drive", 40060006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 40074d454574SPaolo Bonzini .desc = { 40084d454574SPaolo Bonzini { 40094d454574SPaolo Bonzini .name = "snapshot", 40104d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 40114d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 40124d454574SPaolo Bonzini },{ 40134d454574SPaolo Bonzini .name = "aio", 40144d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 40154d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 40164d454574SPaolo Bonzini },{ 4017e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4018e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4019e4b24b49SKevin Wolf .help = "Enable writeback mode", 4020e4b24b49SKevin Wolf },{ 40214d454574SPaolo Bonzini .name = "format", 40224d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 40234d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 40244d454574SPaolo Bonzini },{ 40254d454574SPaolo Bonzini .name = "rerror", 40264d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 40274d454574SPaolo Bonzini .help = "read error action", 40284d454574SPaolo Bonzini },{ 40294d454574SPaolo Bonzini .name = "werror", 40304d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 40314d454574SPaolo Bonzini .help = "write error action", 40324d454574SPaolo Bonzini },{ 40334e200cf8SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 40344d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 40354d454574SPaolo Bonzini .help = "open drive file as read-only", 4036a2a7862cSPradeep Jagadeesh }, 4037a2a7862cSPradeep Jagadeesh 4038a2a7862cSPradeep Jagadeesh THROTTLE_OPTS, 4039a2a7862cSPradeep Jagadeesh 4040a2a7862cSPradeep Jagadeesh { 404176f4afb4SAlberto Garcia .name = "throttling.group", 404276f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 404376f4afb4SAlberto Garcia .help = "name of the block throttling group", 404476f4afb4SAlberto Garcia },{ 40454d454574SPaolo Bonzini .name = "copy-on-read", 40464d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 40474d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4048465bee1dSPeter Lieven },{ 4049465bee1dSPeter Lieven .name = "detect-zeroes", 4050465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4051465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4052362e9299SAlberto Garcia },{ 4053362e9299SAlberto Garcia .name = "stats-account-invalid", 4054362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4055362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4056362e9299SAlberto Garcia "in the statistics", 4057362e9299SAlberto Garcia },{ 4058362e9299SAlberto Garcia .name = "stats-account-failed", 4059362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4060362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4061362e9299SAlberto Garcia "in the statistics", 40624d454574SPaolo Bonzini }, 40634d454574SPaolo Bonzini { /* end of list */ } 40644d454574SPaolo Bonzini }, 40654d454574SPaolo Bonzini }; 40660006383eSKevin Wolf 40670006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 40680006383eSKevin Wolf .name = "drive", 40690006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 40700006383eSKevin Wolf .desc = { 4071492fdc6fSKevin Wolf /* 4072492fdc6fSKevin Wolf * no elements => accept any params 4073492fdc6fSKevin Wolf * validation will happen later 4074492fdc6fSKevin Wolf */ 40750006383eSKevin Wolf { /* end of list */ } 40760006383eSKevin Wolf }, 40770006383eSKevin Wolf }; 4078