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" 46d26c9a15SKevin Wolf #include "qapi/qmp-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" 5112bd451fSStefan Hajnoczi #include "trace.h" 529c17d615SPaolo Bonzini #include "sysemu/arch_init.h" 53f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 54f348b6d1SVeronia Bahaa #include "qemu/help_option.h" 55666daa68SMarkus Armbruster 569c4218e9SMax Reitz static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = 579c4218e9SMax Reitz QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); 589c4218e9SMax Reitz 59bf18bee5SColin Lord static int do_open_tray(const char *device, bool force, Error **errp); 60bf18bee5SColin Lord 611960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 621960966dSMarkus Armbruster [IF_NONE] = "none", 631960966dSMarkus Armbruster [IF_IDE] = "ide", 641960966dSMarkus Armbruster [IF_SCSI] = "scsi", 651960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 661960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 671960966dSMarkus Armbruster [IF_MTD] = "mtd", 681960966dSMarkus Armbruster [IF_SD] = "sd", 691960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 701960966dSMarkus Armbruster [IF_XEN] = "xen", 711960966dSMarkus Armbruster }; 721960966dSMarkus Armbruster 7321dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = { 7427d6bf40SMarkus Armbruster /* 7527d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 7627d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 7727d6bf40SMarkus Armbruster * 78547cb157SWei Jiangang * All controllers used to implement if=T drives need to support 7927d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 8027d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 8127d6bf40SMarkus Armbruster * values. 8227d6bf40SMarkus Armbruster * 8327d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 8427d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 8527d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 8627d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 8727d6bf40SMarkus Armbruster */ 8827d6bf40SMarkus Armbruster [IF_IDE] = 2, 8927d6bf40SMarkus Armbruster [IF_SCSI] = 7, 901960966dSMarkus Armbruster }; 911960966dSMarkus Armbruster 9221dff8cfSJohn Snow /** 9321dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides 9421dff8cfSJohn Snow * of the default, global values. 9521dff8cfSJohn Snow */ 9621dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs) 9721dff8cfSJohn Snow { 9818e46a03SMarkus Armbruster BlockBackend *blk; 9921dff8cfSJohn Snow DriveInfo *dinfo; 10021dff8cfSJohn Snow 10121dff8cfSJohn Snow if (max_devs <= 0) { 10221dff8cfSJohn Snow return; 10321dff8cfSJohn Snow } 10421dff8cfSJohn Snow 10518e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 10618e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 10721dff8cfSJohn Snow if (dinfo->type == type) { 10821dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of" 10921dff8cfSJohn Snow " the %s interface, because a drive of that type has" 11021dff8cfSJohn Snow " already been added.\n", if_name[type]); 11121dff8cfSJohn Snow g_assert_not_reached(); 11221dff8cfSJohn Snow } 11321dff8cfSJohn Snow } 11421dff8cfSJohn Snow 11521dff8cfSJohn Snow if_max_devs[type] = max_devs; 11621dff8cfSJohn Snow } 11721dff8cfSJohn Snow 11814bafc54SMarkus Armbruster /* 11914bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 12014bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 12114bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 12214bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 12314bafc54SMarkus Armbruster * when deletion is actually safe. 12414bafc54SMarkus Armbruster */ 1254be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk) 12614bafc54SMarkus Armbruster { 12718e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 1284be74634SMarkus Armbruster BlockDriverState *bs = blk_bs(blk); 12991fddb0dSStefan Hajnoczi AioContext *aio_context; 13014bafc54SMarkus Armbruster 13126f8b3a8SMarkus Armbruster if (!dinfo) { 1322d246f01SKevin Wolf return; 1332d246f01SKevin Wolf } 1342d246f01SKevin Wolf 1355433c24fSMax Reitz if (bs) { 13691fddb0dSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 13791fddb0dSStefan Hajnoczi aio_context_acquire(aio_context); 13891fddb0dSStefan Hajnoczi 13912bde0eeSPaolo Bonzini if (bs->job) { 14012bde0eeSPaolo Bonzini block_job_cancel(bs->job); 14112bde0eeSPaolo Bonzini } 14291fddb0dSStefan Hajnoczi 14391fddb0dSStefan Hajnoczi aio_context_release(aio_context); 1445433c24fSMax Reitz } 14591fddb0dSStefan Hajnoczi 14614bafc54SMarkus Armbruster dinfo->auto_del = 1; 14714bafc54SMarkus Armbruster } 14814bafc54SMarkus Armbruster 1494be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk) 15014bafc54SMarkus Armbruster { 15118e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 15214bafc54SMarkus Armbruster 1530fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 154efaa7c4eSMax Reitz monitor_remove_blk(blk); 155b9fe8a7aSMarkus Armbruster blk_unref(blk); 15614bafc54SMarkus Armbruster } 15714bafc54SMarkus Armbruster } 15814bafc54SMarkus Armbruster 159d8f94e1bSJohn Snow /** 160d8f94e1bSJohn Snow * Returns the current mapping of how many units per bus 161d8f94e1bSJohn Snow * a particular interface can support. 162d8f94e1bSJohn Snow * 163d8f94e1bSJohn Snow * A positive integer indicates n units per bus. 164d8f94e1bSJohn Snow * 0 implies the mapping has not been established. 165d8f94e1bSJohn Snow * -1 indicates an invalid BlockInterfaceType was given. 166d8f94e1bSJohn Snow */ 167d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type) 168d8f94e1bSJohn Snow { 169d8f94e1bSJohn Snow if (type >= IF_IDE && type < IF_COUNT) { 170d8f94e1bSJohn Snow return if_max_devs[type]; 171d8f94e1bSJohn Snow } 172d8f94e1bSJohn Snow 173d8f94e1bSJohn Snow return -1; 174d8f94e1bSJohn Snow } 175d8f94e1bSJohn Snow 176505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 177505a7fb1SMarkus Armbruster { 178505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 179505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 180505a7fb1SMarkus Armbruster } 181505a7fb1SMarkus Armbruster 182505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 183505a7fb1SMarkus Armbruster { 184505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 185505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 186505a7fb1SMarkus Armbruster } 187505a7fb1SMarkus Armbruster 1882292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 1892292ddaeSMarkus Armbruster { 19070b94331SMarkus Armbruster return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 1912292ddaeSMarkus Armbruster } 1922292ddaeSMarkus Armbruster 1932292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 1945645b0f4SMarkus Armbruster const char *optstr) 195666daa68SMarkus Armbruster { 196666daa68SMarkus Armbruster QemuOpts *opts; 197666daa68SMarkus Armbruster 1982292ddaeSMarkus Armbruster opts = drive_def(optstr); 199666daa68SMarkus Armbruster if (!opts) { 200666daa68SMarkus Armbruster return NULL; 201666daa68SMarkus Armbruster } 2022292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 203f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort); 2042292ddaeSMarkus Armbruster } 2052292ddaeSMarkus Armbruster if (index >= 0) { 206a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort); 2072292ddaeSMarkus Armbruster } 208666daa68SMarkus Armbruster if (file) 209f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort); 210666daa68SMarkus Armbruster return opts; 211666daa68SMarkus Armbruster } 212666daa68SMarkus Armbruster 213666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 214666daa68SMarkus Armbruster { 21518e46a03SMarkus Armbruster BlockBackend *blk; 216666daa68SMarkus Armbruster DriveInfo *dinfo; 217666daa68SMarkus Armbruster 21818e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 21918e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 22018e46a03SMarkus Armbruster if (dinfo && dinfo->type == type 22118e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) { 222666daa68SMarkus Armbruster return dinfo; 223666daa68SMarkus Armbruster } 22418e46a03SMarkus Armbruster } 225666daa68SMarkus Armbruster 226666daa68SMarkus Armbruster return NULL; 227666daa68SMarkus Armbruster } 228666daa68SMarkus Armbruster 229a66c9dc7SJohn Snow bool drive_check_orphaned(void) 230a66c9dc7SJohn Snow { 23118e46a03SMarkus Armbruster BlockBackend *blk; 232a66c9dc7SJohn Snow DriveInfo *dinfo; 233a66c9dc7SJohn Snow bool rs = false; 234a66c9dc7SJohn Snow 23518e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 23618e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 237a66c9dc7SJohn Snow /* If dinfo->bdrv->dev is NULL, it has no device attached. */ 238a66c9dc7SJohn Snow /* Unless this is a default drive, this may be an oversight. */ 239a7f53e26SMarkus Armbruster if (!blk_get_attached_dev(blk) && !dinfo->is_default && 240a66c9dc7SJohn Snow dinfo->type != IF_NONE) { 241a66c9dc7SJohn Snow fprintf(stderr, "Warning: Orphaned drive without device: " 242a66c9dc7SJohn Snow "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", 2435433c24fSMax Reitz blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "", 2445433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit); 245a66c9dc7SJohn Snow rs = true; 246a66c9dc7SJohn Snow } 247a66c9dc7SJohn Snow } 248a66c9dc7SJohn Snow 249a66c9dc7SJohn Snow return rs; 250a66c9dc7SJohn Snow } 251a66c9dc7SJohn Snow 252f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 253f1bd51acSMarkus Armbruster { 254f1bd51acSMarkus Armbruster return drive_get(type, 255f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 256f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 257f1bd51acSMarkus Armbruster } 258f1bd51acSMarkus Armbruster 259666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 260666daa68SMarkus Armbruster { 261666daa68SMarkus Armbruster int max_bus; 26218e46a03SMarkus Armbruster BlockBackend *blk; 263666daa68SMarkus Armbruster DriveInfo *dinfo; 264666daa68SMarkus Armbruster 265666daa68SMarkus Armbruster max_bus = -1; 26618e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 26718e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 26818e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 269666daa68SMarkus Armbruster max_bus = dinfo->bus; 270666daa68SMarkus Armbruster } 27118e46a03SMarkus Armbruster } 272666daa68SMarkus Armbruster return max_bus; 273666daa68SMarkus Armbruster } 274666daa68SMarkus Armbruster 27513839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 27613839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 27713839974SMarkus Armbruster appropriate bus. */ 27813839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 27913839974SMarkus Armbruster { 28013839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 28113839974SMarkus Armbruster 28213839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 28313839974SMarkus Armbruster } 28413839974SMarkus Armbruster 285666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 286666daa68SMarkus Armbruster { 287807105a7SMarkus Armbruster error_printf(" %s", name); 288666daa68SMarkus Armbruster } 289666daa68SMarkus Armbruster 290aa398a5cSStefan Hajnoczi typedef struct { 291aa398a5cSStefan Hajnoczi QEMUBH *bh; 292fa510ebfSFam Zheng BlockDriverState *bs; 293fa510ebfSFam Zheng } BDRVPutRefBH; 294aa398a5cSStefan Hajnoczi 295b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 296666daa68SMarkus Armbruster { 297666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 29892aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 299666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 30092aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 301666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 30292aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 303666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 30492aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 305666daa68SMarkus Armbruster } else { 306b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 307666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 308666daa68SMarkus Armbruster return -1; 309666daa68SMarkus Armbruster } 310666daa68SMarkus Armbruster } 311666daa68SMarkus Armbruster 31240119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 31340119effSAlberto Garcia Error **errp) 31440119effSAlberto Garcia { 31540119effSAlberto Garcia const QListEntry *entry; 31640119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 31740119effSAlberto Garcia switch (qobject_type(entry->value)) { 31840119effSAlberto Garcia 31940119effSAlberto Garcia case QTYPE_QSTRING: { 32040119effSAlberto Garcia unsigned long long length; 32140119effSAlberto Garcia const char *str = qstring_get_str(qobject_to_qstring(entry->value)); 32240119effSAlberto Garcia if (parse_uint_full(str, &length, 10) == 0 && 32340119effSAlberto Garcia length > 0 && length <= UINT_MAX) { 32440119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 32540119effSAlberto Garcia } else { 32640119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str); 32740119effSAlberto Garcia return false; 32840119effSAlberto Garcia } 32940119effSAlberto Garcia break; 33040119effSAlberto Garcia } 33140119effSAlberto Garcia 33240119effSAlberto Garcia case QTYPE_QINT: { 33340119effSAlberto Garcia int64_t length = qint_get_int(qobject_to_qint(entry->value)); 33440119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) { 33540119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 33640119effSAlberto Garcia } else { 33740119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length); 33840119effSAlberto Garcia return false; 33940119effSAlberto Garcia } 34040119effSAlberto Garcia break; 34140119effSAlberto Garcia } 34240119effSAlberto Garcia 34340119effSAlberto Garcia default: 34440119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid"); 34540119effSAlberto Garcia return false; 34640119effSAlberto Garcia } 34740119effSAlberto Garcia } 34840119effSAlberto Garcia return true; 34940119effSAlberto Garcia } 35040119effSAlberto Garcia 35133cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 35233cb7dc8SKevin Wolf 353fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 354fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 355fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 356fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 357fbf8175eSMax Reitz { 358fbf8175eSMax Reitz const char *discard; 359fbf8175eSMax Reitz Error *local_error = NULL; 360fbf8175eSMax Reitz const char *aio; 361fbf8175eSMax Reitz 362fbf8175eSMax Reitz if (bdrv_flags) { 363fbf8175eSMax Reitz if (!qemu_opt_get_bool(opts, "read-only", false)) { 364fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_RDWR; 365fbf8175eSMax Reitz } 366fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 367fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 368fbf8175eSMax Reitz } 369fbf8175eSMax Reitz 370fbf8175eSMax Reitz if ((discard = qemu_opt_get(opts, "discard")) != NULL) { 371fbf8175eSMax Reitz if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { 372fbf8175eSMax Reitz error_setg(errp, "Invalid discard option"); 373fbf8175eSMax Reitz return; 374fbf8175eSMax Reitz } 375fbf8175eSMax Reitz } 376fbf8175eSMax Reitz 377fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 378fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 379fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 380fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 381fbf8175eSMax Reitz /* this is the default */ 382fbf8175eSMax Reitz } else { 383fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 384fbf8175eSMax Reitz return; 385fbf8175eSMax Reitz } 386fbf8175eSMax Reitz } 387fbf8175eSMax Reitz } 388fbf8175eSMax Reitz 389fbf8175eSMax Reitz /* disk I/O throttling */ 390fbf8175eSMax Reitz if (throttling_group) { 391fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 392fbf8175eSMax Reitz } 393fbf8175eSMax Reitz 394fbf8175eSMax Reitz if (throttle_cfg) { 3951588ab5dSAlberto Garcia throttle_config_init(throttle_cfg); 396fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 397fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 398fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 399fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 400fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 401fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 402fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 403fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 404fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 405fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 406fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 407fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 408fbf8175eSMax Reitz 409fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 410fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 411fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 412fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 413fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 414fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 415fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 416fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 417fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 418fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 419fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 420fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 421fbf8175eSMax Reitz 4228a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = 4238a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); 4248a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = 4258a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); 4268a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = 4278a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); 4288a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = 4298a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); 4308a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = 4318a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); 4328a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = 4338a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); 4348a0fc18dSAlberto Garcia 435fbf8175eSMax Reitz throttle_cfg->op_size = 436fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 437fbf8175eSMax Reitz 438d5851089SAlberto Garcia if (!throttle_is_valid(throttle_cfg, errp)) { 439fbf8175eSMax Reitz return; 440fbf8175eSMax Reitz } 441fbf8175eSMax Reitz } 442fbf8175eSMax Reitz 443fbf8175eSMax Reitz if (detect_zeroes) { 444fbf8175eSMax Reitz *detect_zeroes = 445fbf8175eSMax Reitz qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, 446fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 4477fb1cf16SEric Blake BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX, 448fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 449fbf8175eSMax Reitz &local_error); 450fbf8175eSMax Reitz if (local_error) { 451fbf8175eSMax Reitz error_propagate(errp, local_error); 452fbf8175eSMax Reitz return; 453fbf8175eSMax Reitz } 454fbf8175eSMax Reitz 455fbf8175eSMax Reitz if (bdrv_flags && 456fbf8175eSMax Reitz *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 457fbf8175eSMax Reitz !(*bdrv_flags & BDRV_O_UNMAP)) 458fbf8175eSMax Reitz { 459fbf8175eSMax Reitz error_setg(errp, "setting detect-zeroes to unmap is not allowed " 460fbf8175eSMax Reitz "without setting discard operation to unmap"); 461fbf8175eSMax Reitz return; 462fbf8175eSMax Reitz } 463fbf8175eSMax Reitz } 464fbf8175eSMax Reitz } 465fbf8175eSMax Reitz 466f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 46718e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 468b681072dSKevin Wolf Error **errp) 469666daa68SMarkus Armbruster { 470666daa68SMarkus Armbruster const char *buf; 471666daa68SMarkus Armbruster int bdrv_flags = 0; 472666daa68SMarkus Armbruster int on_read_error, on_write_error; 473362e9299SAlberto Garcia bool account_invalid, account_failed; 474e4b24b49SKevin Wolf bool writethrough; 47526f54e9aSMarkus Armbruster BlockBackend *blk; 476a0f1eab1SMarkus Armbruster BlockDriverState *bs; 477cc0681c4SBenoît Canet ThrottleConfig cfg; 478666daa68SMarkus Armbruster int snapshot = 0; 479c546194fSStefan Hajnoczi Error *error = NULL; 4800006383eSKevin Wolf QemuOpts *opts; 48140119effSAlberto Garcia QDict *interval_dict = NULL; 48240119effSAlberto Garcia QList *interval_list = NULL; 4830006383eSKevin Wolf const char *id; 484fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 485fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 486fbf8175eSMax Reitz const char *throttling_group = NULL; 487666daa68SMarkus Armbruster 488f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 489f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 490f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4910006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 49284d18f06SMarkus Armbruster if (error) { 493b681072dSKevin Wolf error_propagate(errp, error); 4946376f952SMarkus Armbruster goto err_no_opts; 4950006383eSKevin Wolf } 4960006383eSKevin Wolf 4970006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49884d18f06SMarkus Armbruster if (error) { 499b681072dSKevin Wolf error_propagate(errp, error); 500ec9c10d2SStefan Hajnoczi goto early_err; 5010006383eSKevin Wolf } 5020006383eSKevin Wolf 5030006383eSKevin Wolf if (id) { 5040006383eSKevin Wolf qdict_del(bs_opts, "id"); 5050006383eSKevin Wolf } 5060006383eSKevin Wolf 507666daa68SMarkus Armbruster /* extract parameters */ 508666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 509666daa68SMarkus Armbruster 510362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 511362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 512362e9299SAlberto Garcia 513e4b24b49SKevin Wolf writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); 514e4b24b49SKevin Wolf 515*ff356ee4SAlberto Garcia id = qemu_opts_id(opts); 516*ff356ee4SAlberto Garcia 51740119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51840119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51940119effSAlberto Garcia 52040119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 52140119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 52240119effSAlberto Garcia qdict_first(interval_dict)->key); 52340119effSAlberto Garcia goto early_err; 52440119effSAlberto Garcia } 5252be5506fSAlberto Garcia 526fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 527fbf8175eSMax Reitz &detect_zeroes, &error); 528fbf8175eSMax Reitz if (error) { 529fbf8175eSMax Reitz error_propagate(errp, error); 530ec9c10d2SStefan Hajnoczi goto early_err; 531a9384affSPaolo Bonzini } 532666daa68SMarkus Armbruster 533666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 534c8057f95SPeter Maydell if (is_help_option(buf)) { 535807105a7SMarkus Armbruster error_printf("Supported formats:"); 536666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 537807105a7SMarkus Armbruster error_printf("\n"); 538ec9c10d2SStefan Hajnoczi goto early_err; 539666daa68SMarkus Armbruster } 54074fe54f2SKevin Wolf 541e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 542e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 543ec9c10d2SStefan Hajnoczi goto early_err; 5446db5f5d6SMike Qiu } 545e4342ce5SMax Reitz qdict_put(bs_opts, "driver", qstring_from_str(buf)); 546666daa68SMarkus Armbruster } 547666daa68SMarkus Armbruster 54892aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 549666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 550b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 55184d18f06SMarkus Armbruster if (error) { 552b681072dSKevin Wolf error_propagate(errp, error); 553ec9c10d2SStefan Hajnoczi goto early_err; 554666daa68SMarkus Armbruster } 555666daa68SMarkus Armbruster } 556666daa68SMarkus Armbruster 55792aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 558666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 559b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 56084d18f06SMarkus Armbruster if (error) { 561b681072dSKevin Wolf error_propagate(errp, error); 562ec9c10d2SStefan Hajnoczi goto early_err; 563666daa68SMarkus Armbruster } 564666daa68SMarkus Armbruster } 565666daa68SMarkus Armbruster 566666daa68SMarkus Armbruster if (snapshot) { 56791a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 568666daa68SMarkus Armbruster } 569666daa68SMarkus Armbruster 5705ec18f8cSMax Reitz /* init */ 57139c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5725ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5735ec18f8cSMax Reitz 574109525adSMax Reitz blk = blk_new(); 5755ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5765ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 577fbf8175eSMax Reitz blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); 5785ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5795ec18f8cSMax Reitz 5805ec18f8cSMax Reitz QDECREF(bs_opts); 5815ec18f8cSMax Reitz } else { 5825ec18f8cSMax Reitz if (file && !*file) { 5835ec18f8cSMax Reitz file = NULL; 5845ec18f8cSMax Reitz } 5855ec18f8cSMax Reitz 58691a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 58791a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 58891a097e7SKevin Wolf * Apply the defaults here instead. */ 58991a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 59091a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 59172e775c7SKevin Wolf assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); 59291a097e7SKevin Wolf 59312d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 59412d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 59512d5ee3aSKevin Wolf } 59612d5ee3aSKevin Wolf 597efaa7c4eSMax Reitz blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); 598e4342ce5SMax Reitz if (!blk) { 599e4342ce5SMax Reitz goto err_no_bs_opts; 600e4342ce5SMax Reitz } 601e4342ce5SMax Reitz bs = blk_bs(blk); 6020006383eSKevin Wolf 603e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 604e4342ce5SMax Reitz 605a0f1eab1SMarkus Armbruster if (bdrv_key_required(bs)) { 606666daa68SMarkus Armbruster autostart = 0; 607a0f1eab1SMarkus Armbruster } 608362e9299SAlberto Garcia 609362e9299SAlberto Garcia block_acct_init(blk_get_stats(blk), account_invalid, account_failed); 6102be5506fSAlberto Garcia 61140119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6122be5506fSAlberto Garcia blk_unref(blk); 6132be5506fSAlberto Garcia blk = NULL; 6142be5506fSAlberto Garcia goto err_no_bs_opts; 6152be5506fSAlberto Garcia } 6162be5506fSAlberto Garcia } 6175ec18f8cSMax Reitz 6187ca7f0f6SKevin Wolf /* disk I/O throttling */ 6197ca7f0f6SKevin Wolf if (throttle_enabled(&cfg)) { 6207ca7f0f6SKevin Wolf if (!throttling_group) { 621*ff356ee4SAlberto Garcia throttling_group = id; 6227ca7f0f6SKevin Wolf } 6237ca7f0f6SKevin Wolf blk_io_limits_enable(blk, throttling_group); 6247ca7f0f6SKevin Wolf blk_set_io_limits(blk, &cfg); 6257ca7f0f6SKevin Wolf } 6267ca7f0f6SKevin Wolf 627e4b24b49SKevin Wolf blk_set_enable_write_cache(blk, !writethrough); 6285ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6290006383eSKevin Wolf 630*ff356ee4SAlberto Garcia if (!monitor_add_blk(blk, id, errp)) { 631efaa7c4eSMax Reitz blk_unref(blk); 632efaa7c4eSMax Reitz blk = NULL; 633efaa7c4eSMax Reitz goto err_no_bs_opts; 634efaa7c4eSMax Reitz } 635efaa7c4eSMax Reitz 636e4342ce5SMax Reitz err_no_bs_opts: 6370006383eSKevin Wolf qemu_opts_del(opts); 63840119effSAlberto Garcia QDECREF(interval_dict); 63940119effSAlberto Garcia QDECREF(interval_list); 64018e46a03SMarkus Armbruster return blk; 641a9ae2bffSMarkus Armbruster 642ec9c10d2SStefan Hajnoczi early_err: 643ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 64440119effSAlberto Garcia QDECREF(interval_dict); 64540119effSAlberto Garcia QDECREF(interval_list); 6466376f952SMarkus Armbruster err_no_opts: 6476376f952SMarkus Armbruster QDECREF(bs_opts); 648a9ae2bffSMarkus Armbruster return NULL; 649666daa68SMarkus Armbruster } 650666daa68SMarkus Armbruster 651bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts; 652bd745e23SMax Reitz 653bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 654bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 655bd745e23SMax Reitz { 656bd745e23SMax Reitz BlockDriverState *bs; 657bd745e23SMax Reitz QemuOpts *opts; 658bd745e23SMax Reitz Error *local_error = NULL; 659bd745e23SMax Reitz BlockdevDetectZeroesOptions detect_zeroes; 660bd745e23SMax Reitz int bdrv_flags = 0; 661bd745e23SMax Reitz 662bd745e23SMax Reitz opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); 663bd745e23SMax Reitz if (!opts) { 664bd745e23SMax Reitz goto fail; 665bd745e23SMax Reitz } 666bd745e23SMax Reitz 667bd745e23SMax Reitz qemu_opts_absorb_qdict(opts, bs_opts, &local_error); 668bd745e23SMax Reitz if (local_error) { 669bd745e23SMax Reitz error_propagate(errp, local_error); 670bd745e23SMax Reitz goto fail; 671bd745e23SMax Reitz } 672bd745e23SMax Reitz 673bd745e23SMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, 674bd745e23SMax Reitz &detect_zeroes, &local_error); 675bd745e23SMax Reitz if (local_error) { 676bd745e23SMax Reitz error_propagate(errp, local_error); 677bd745e23SMax Reitz goto fail; 678bd745e23SMax Reitz } 679bd745e23SMax Reitz 680a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 681a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults. 682a81d6164SKevin Wolf * Apply the defaults here instead. */ 683a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 684a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 685a81d6164SKevin Wolf 68612d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 68712d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 68812d5ee3aSKevin Wolf } 68912d5ee3aSKevin Wolf 6905b363937SMax Reitz bs = bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); 6915b363937SMax Reitz if (!bs) { 692bd745e23SMax Reitz goto fail_no_bs_opts; 693bd745e23SMax Reitz } 694bd745e23SMax Reitz 695bd745e23SMax Reitz bs->detect_zeroes = detect_zeroes; 696bd745e23SMax Reitz 697bd745e23SMax Reitz fail_no_bs_opts: 698bd745e23SMax Reitz qemu_opts_del(opts); 699bd745e23SMax Reitz return bs; 700bd745e23SMax Reitz 701bd745e23SMax Reitz fail: 702bd745e23SMax Reitz qemu_opts_del(opts); 703bd745e23SMax Reitz QDECREF(bs_opts); 704bd745e23SMax Reitz return NULL; 705bd745e23SMax Reitz } 706bd745e23SMax Reitz 7079c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void) 7089c4218e9SMax Reitz { 7099c4218e9SMax Reitz BlockDriverState *bs, *next_bs; 7109c4218e9SMax Reitz 7119c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { 7129c4218e9SMax Reitz AioContext *ctx = bdrv_get_aio_context(bs); 7139c4218e9SMax Reitz 7149c4218e9SMax Reitz aio_context_acquire(ctx); 7159c4218e9SMax Reitz bdrv_unref(bs); 7169c4218e9SMax Reitz aio_context_release(ctx); 7179c4218e9SMax Reitz } 7189c4218e9SMax Reitz } 7199c4218e9SMax Reitz 720262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */ 721262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) 722262b4e8fSMax Reitz { 723262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list) 724262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states); 725262b4e8fSMax Reitz } 726262b4e8fSMax Reitz 7275abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 7285abbf0eeSKevin Wolf Error **errp) 72957975222SKevin Wolf { 73057975222SKevin Wolf const char *value; 73157975222SKevin Wolf 73257975222SKevin Wolf value = qemu_opt_get(opts, from); 73357975222SKevin Wolf if (value) { 7345abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 7355abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 7365abbf0eeSKevin Wolf "same time", to, from); 7375abbf0eeSKevin Wolf return; 7385abbf0eeSKevin Wolf } 73920d6cd47SJun Li } 74020d6cd47SJun Li 74120d6cd47SJun Li /* rename all items in opts */ 74220d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 743f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 74457975222SKevin Wolf qemu_opt_unset(opts, from); 74557975222SKevin Wolf } 74657975222SKevin Wolf } 74757975222SKevin Wolf 74833cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 74933cb7dc8SKevin Wolf .name = "drive", 75033cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 75133cb7dc8SKevin Wolf .desc = { 75233cb7dc8SKevin Wolf { 75387a899c5SKevin Wolf .name = "bus", 75487a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 75587a899c5SKevin Wolf .help = "bus number", 75687a899c5SKevin Wolf },{ 75787a899c5SKevin Wolf .name = "unit", 75887a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 75987a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 76087a899c5SKevin Wolf },{ 76187a899c5SKevin Wolf .name = "index", 76287a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 76387a899c5SKevin Wolf .help = "index number", 76487a899c5SKevin Wolf },{ 76533cb7dc8SKevin Wolf .name = "media", 76633cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 76733cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 768593d464bSKevin Wolf },{ 769593d464bSKevin Wolf .name = "if", 770593d464bSKevin Wolf .type = QEMU_OPT_STRING, 771593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 772b41a7338SKevin Wolf },{ 773b41a7338SKevin Wolf .name = "cyls", 774b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 775b41a7338SKevin Wolf .help = "number of cylinders (ide disk geometry)", 776b41a7338SKevin Wolf },{ 777b41a7338SKevin Wolf .name = "heads", 778b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 779b41a7338SKevin Wolf .help = "number of heads (ide disk geometry)", 780b41a7338SKevin Wolf },{ 781b41a7338SKevin Wolf .name = "secs", 782b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 783b41a7338SKevin Wolf .help = "number of sectors (ide disk geometry)", 784b41a7338SKevin Wolf },{ 785b41a7338SKevin Wolf .name = "trans", 786b41a7338SKevin Wolf .type = QEMU_OPT_STRING, 787b41a7338SKevin Wolf .help = "chs translation (auto, lba, none)", 78826929298SKevin Wolf },{ 78926929298SKevin Wolf .name = "boot", 79026929298SKevin Wolf .type = QEMU_OPT_BOOL, 79126929298SKevin Wolf .help = "(deprecated, ignored)", 792394c7d4dSKevin Wolf },{ 793394c7d4dSKevin Wolf .name = "addr", 794394c7d4dSKevin Wolf .type = QEMU_OPT_STRING, 795394c7d4dSKevin Wolf .help = "pci address (virtio only)", 796d095b465SMax Reitz },{ 797bcf83158SKevin Wolf .name = "serial", 798bcf83158SKevin Wolf .type = QEMU_OPT_STRING, 799bcf83158SKevin Wolf .help = "disk serial number", 800bcf83158SKevin Wolf },{ 801d095b465SMax Reitz .name = "file", 802d095b465SMax Reitz .type = QEMU_OPT_STRING, 803d095b465SMax Reitz .help = "file name", 80433cb7dc8SKevin Wolf }, 8050ebd24e0SKevin Wolf 8060ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 8070ebd24e0SKevin Wolf { 8080ebd24e0SKevin Wolf .name = "read-only", 8090ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8100ebd24e0SKevin Wolf .help = "open drive file as read-only", 8110ebd24e0SKevin Wolf },{ 812ee13ed1cSKevin Wolf .name = "rerror", 813ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 814ee13ed1cSKevin Wolf .help = "read error action", 815ee13ed1cSKevin Wolf },{ 816ee13ed1cSKevin Wolf .name = "werror", 817ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 818ee13ed1cSKevin Wolf .help = "write error action", 819ee13ed1cSKevin Wolf },{ 8200ebd24e0SKevin Wolf .name = "copy-on-read", 8210ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8220ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 8230ebd24e0SKevin Wolf }, 8240ebd24e0SKevin Wolf 82533cb7dc8SKevin Wolf { /* end of list */ } 82633cb7dc8SKevin Wolf }, 82733cb7dc8SKevin Wolf }; 82833cb7dc8SKevin Wolf 82960e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 83057975222SKevin Wolf { 83129c4e2b5SKevin Wolf const char *value; 83218e46a03SMarkus Armbruster BlockBackend *blk; 83333cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 834f298d071SKevin Wolf QDict *bs_opts; 83533cb7dc8SKevin Wolf QemuOpts *legacy_opts; 83633cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 837593d464bSKevin Wolf BlockInterfaceType type; 838b41a7338SKevin Wolf int cyls, heads, secs, translation; 83987a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 840394c7d4dSKevin Wolf const char *devaddr; 841ee13ed1cSKevin Wolf const char *werror, *rerror; 842a7fdbcf0SFam Zheng bool read_only = false; 843a7fdbcf0SFam Zheng bool copy_on_read; 844bcf83158SKevin Wolf const char *serial; 845d095b465SMax Reitz const char *filename; 84633cb7dc8SKevin Wolf Error *local_err = NULL; 847247147fbSKevin Wolf int i; 84829c4e2b5SKevin Wolf 84957975222SKevin Wolf /* Change legacy command line options into QMP ones */ 850247147fbSKevin Wolf static const struct { 851247147fbSKevin Wolf const char *from; 852247147fbSKevin Wolf const char *to; 853247147fbSKevin Wolf } opt_renames[] = { 854247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 855247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 856247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 85757975222SKevin Wolf 858247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 859247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 860247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 86157975222SKevin Wolf 862247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 863247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 864247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8653e9fab69SBenoît Canet 866247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 867247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 868247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8693e9fab69SBenoît Canet 870247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8712024c1dfSBenoît Canet 87276f4afb4SAlberto Garcia { "group", "throttling.group" }, 87376f4afb4SAlberto Garcia 874247147fbSKevin Wolf { "readonly", "read-only" }, 875247147fbSKevin Wolf }; 876247147fbSKevin Wolf 877247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8785abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8795abbf0eeSKevin Wolf &local_err); 8805abbf0eeSKevin Wolf if (local_err) { 881565f65d2SMarkus Armbruster error_report_err(local_err); 8825abbf0eeSKevin Wolf return NULL; 8835abbf0eeSKevin Wolf } 884247147fbSKevin Wolf } 8850f227a94SKevin Wolf 88629c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 88729c4e2b5SKevin Wolf if (value) { 88829c4e2b5SKevin Wolf int flags = 0; 88904feb4a5SKevin Wolf bool writethrough; 89029c4e2b5SKevin Wolf 89104feb4a5SKevin Wolf if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { 89229c4e2b5SKevin Wolf error_report("invalid cache option"); 89329c4e2b5SKevin Wolf return NULL; 89429c4e2b5SKevin Wolf } 89529c4e2b5SKevin Wolf 89629c4e2b5SKevin Wolf /* Specific options take precedence */ 89754861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 89854861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 89904feb4a5SKevin Wolf !writethrough, &error_abort); 90029c4e2b5SKevin Wolf } 90154861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 90254861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 903cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 90429c4e2b5SKevin Wolf } 90554861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 90654861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 907cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 90829c4e2b5SKevin Wolf } 90929c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 91029c4e2b5SKevin Wolf } 91129c4e2b5SKevin Wolf 912f298d071SKevin Wolf /* Get a QDict for processing the options */ 913f298d071SKevin Wolf bs_opts = qdict_new(); 914f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 915f298d071SKevin Wolf 91687ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 91787ea75d5SPeter Crosthwaite &error_abort); 91833cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 91984d18f06SMarkus Armbruster if (local_err) { 920565f65d2SMarkus Armbruster error_report_err(local_err); 92133cb7dc8SKevin Wolf goto fail; 92233cb7dc8SKevin Wolf } 92333cb7dc8SKevin Wolf 92426929298SKevin Wolf /* Deprecated option boot=[on|off] */ 92526929298SKevin Wolf if (qemu_opt_get(legacy_opts, "boot") != NULL) { 92626929298SKevin Wolf fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 92726929298SKevin Wolf "ignored. Future versions will reject this parameter. Please " 92826929298SKevin Wolf "update your scripts.\n"); 92926929298SKevin Wolf } 93026929298SKevin Wolf 93133cb7dc8SKevin Wolf /* Media type */ 93233cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 93333cb7dc8SKevin Wolf if (value) { 93433cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 93533cb7dc8SKevin Wolf media = MEDIA_DISK; 93633cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 93733cb7dc8SKevin Wolf media = MEDIA_CDROM; 938a7fdbcf0SFam Zheng read_only = true; 93933cb7dc8SKevin Wolf } else { 94033cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 94133cb7dc8SKevin Wolf goto fail; 94233cb7dc8SKevin Wolf } 94333cb7dc8SKevin Wolf } 94433cb7dc8SKevin Wolf 9450ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 946a7fdbcf0SFam Zheng read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); 9470ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 9480ebd24e0SKevin Wolf 9490ebd24e0SKevin Wolf if (read_only && copy_on_read) { 9500ebd24e0SKevin Wolf error_report("warning: disabling copy-on-read on read-only drive"); 9510ebd24e0SKevin Wolf copy_on_read = false; 9520ebd24e0SKevin Wolf } 9530ebd24e0SKevin Wolf 9540ebd24e0SKevin Wolf qdict_put(bs_opts, "read-only", 9550ebd24e0SKevin Wolf qstring_from_str(read_only ? "on" : "off")); 9560ebd24e0SKevin Wolf qdict_put(bs_opts, "copy-on-read", 9570ebd24e0SKevin Wolf qstring_from_str(copy_on_read ? "on" :"off")); 9580ebd24e0SKevin Wolf 959593d464bSKevin Wolf /* Controller type */ 960593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 961593d464bSKevin Wolf if (value) { 962593d464bSKevin Wolf for (type = 0; 963593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 964593d464bSKevin Wolf type++) { 965593d464bSKevin Wolf } 966593d464bSKevin Wolf if (type == IF_COUNT) { 967593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 968593d464bSKevin Wolf goto fail; 969593d464bSKevin Wolf } 970593d464bSKevin Wolf } else { 971593d464bSKevin Wolf type = block_default_type; 972593d464bSKevin Wolf } 973593d464bSKevin Wolf 974b41a7338SKevin Wolf /* Geometry */ 975b41a7338SKevin Wolf cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 976b41a7338SKevin Wolf heads = qemu_opt_get_number(legacy_opts, "heads", 0); 977b41a7338SKevin Wolf secs = qemu_opt_get_number(legacy_opts, "secs", 0); 978b41a7338SKevin Wolf 979b41a7338SKevin Wolf if (cyls || heads || secs) { 980b41a7338SKevin Wolf if (cyls < 1) { 981b41a7338SKevin Wolf error_report("invalid physical cyls number"); 982b41a7338SKevin Wolf goto fail; 983b41a7338SKevin Wolf } 984b41a7338SKevin Wolf if (heads < 1) { 985b41a7338SKevin Wolf error_report("invalid physical heads number"); 986b41a7338SKevin Wolf goto fail; 987b41a7338SKevin Wolf } 988b41a7338SKevin Wolf if (secs < 1) { 989b41a7338SKevin Wolf error_report("invalid physical secs number"); 990b41a7338SKevin Wolf goto fail; 991b41a7338SKevin Wolf } 992b41a7338SKevin Wolf } 993b41a7338SKevin Wolf 994b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 995b41a7338SKevin Wolf value = qemu_opt_get(legacy_opts, "trans"); 996b41a7338SKevin Wolf if (value != NULL) { 997b41a7338SKevin Wolf if (!cyls) { 998b41a7338SKevin Wolf error_report("'%s' trans must be used with cyls, heads and secs", 999b41a7338SKevin Wolf value); 1000b41a7338SKevin Wolf goto fail; 1001b41a7338SKevin Wolf } 1002b41a7338SKevin Wolf if (!strcmp(value, "none")) { 1003b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_NONE; 1004b41a7338SKevin Wolf } else if (!strcmp(value, "lba")) { 1005b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_LBA; 1006f31c41ffSPaolo Bonzini } else if (!strcmp(value, "large")) { 1007f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_LARGE; 1008f31c41ffSPaolo Bonzini } else if (!strcmp(value, "rechs")) { 1009f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_RECHS; 1010b41a7338SKevin Wolf } else if (!strcmp(value, "auto")) { 1011b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 1012b41a7338SKevin Wolf } else { 1013b41a7338SKevin Wolf error_report("'%s' invalid translation type", value); 1014b41a7338SKevin Wolf goto fail; 1015b41a7338SKevin Wolf } 1016b41a7338SKevin Wolf } 1017b41a7338SKevin Wolf 1018b41a7338SKevin Wolf if (media == MEDIA_CDROM) { 1019b41a7338SKevin Wolf if (cyls || secs || heads) { 1020b41a7338SKevin Wolf error_report("CHS can't be set with media=cdrom"); 1021b41a7338SKevin Wolf goto fail; 1022b41a7338SKevin Wolf } 1023b41a7338SKevin Wolf } 1024b41a7338SKevin Wolf 102587a899c5SKevin Wolf /* Device address specified by bus/unit or index. 102687a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 102787a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 102887a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 102987a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 103087a899c5SKevin Wolf 103187a899c5SKevin Wolf max_devs = if_max_devs[type]; 103287a899c5SKevin Wolf 103387a899c5SKevin Wolf if (index != -1) { 103487a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 103587a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 103687a899c5SKevin Wolf goto fail; 103787a899c5SKevin Wolf } 103887a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 103987a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 104087a899c5SKevin Wolf } 104187a899c5SKevin Wolf 104287a899c5SKevin Wolf if (unit_id == -1) { 104387a899c5SKevin Wolf unit_id = 0; 104487a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 104587a899c5SKevin Wolf unit_id++; 104687a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 104787a899c5SKevin Wolf unit_id -= max_devs; 104887a899c5SKevin Wolf bus_id++; 104987a899c5SKevin Wolf } 105087a899c5SKevin Wolf } 105187a899c5SKevin Wolf } 105287a899c5SKevin Wolf 105387a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 105487a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 105587a899c5SKevin Wolf goto fail; 105687a899c5SKevin Wolf } 105787a899c5SKevin Wolf 105887a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 105987a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 106087a899c5SKevin Wolf bus_id, unit_id, index); 106187a899c5SKevin Wolf goto fail; 106287a899c5SKevin Wolf } 106387a899c5SKevin Wolf 1064bcf83158SKevin Wolf /* Serial number */ 1065bcf83158SKevin Wolf serial = qemu_opt_get(legacy_opts, "serial"); 1066bcf83158SKevin Wolf 106787a899c5SKevin Wolf /* no id supplied -> create one */ 106887a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 106987a899c5SKevin Wolf char *new_id; 107087a899c5SKevin Wolf const char *mediastr = ""; 107187a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 107287a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 107387a899c5SKevin Wolf } 107487a899c5SKevin Wolf if (max_devs) { 107587a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 107687a899c5SKevin Wolf mediastr, unit_id); 107787a899c5SKevin Wolf } else { 107887a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 107987a899c5SKevin Wolf mediastr, unit_id); 108087a899c5SKevin Wolf } 108187a899c5SKevin Wolf qdict_put(bs_opts, "id", qstring_from_str(new_id)); 108287a899c5SKevin Wolf g_free(new_id); 108387a899c5SKevin Wolf } 108487a899c5SKevin Wolf 1085394c7d4dSKevin Wolf /* Add virtio block device */ 1086394c7d4dSKevin Wolf devaddr = qemu_opt_get(legacy_opts, "addr"); 1087394c7d4dSKevin Wolf if (devaddr && type != IF_VIRTIO) { 1088394c7d4dSKevin Wolf error_report("addr is not supported by this bus type"); 1089394c7d4dSKevin Wolf goto fail; 1090394c7d4dSKevin Wolf } 1091394c7d4dSKevin Wolf 1092394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 1093394c7d4dSKevin Wolf QemuOpts *devopts; 109487ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 109587ea75d5SPeter Crosthwaite &error_abort); 1096394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 10971f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1098394c7d4dSKevin Wolf } else { 1099f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1100394c7d4dSKevin Wolf } 1101f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1102f43e47dbSMarkus Armbruster &error_abort); 1103394c7d4dSKevin Wolf if (devaddr) { 1104f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1105394c7d4dSKevin Wolf } 1106394c7d4dSKevin Wolf } 1107394c7d4dSKevin Wolf 1108d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 1109d095b465SMax Reitz 1110ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 1111ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 1112ee13ed1cSKevin Wolf if (werror != NULL) { 1113ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1114ee13ed1cSKevin Wolf type != IF_NONE) { 1115ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 1116ee13ed1cSKevin Wolf goto fail; 1117ee13ed1cSKevin Wolf } 1118ee13ed1cSKevin Wolf qdict_put(bs_opts, "werror", qstring_from_str(werror)); 1119ee13ed1cSKevin Wolf } 1120ee13ed1cSKevin Wolf 1121ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 1122ee13ed1cSKevin Wolf if (rerror != NULL) { 1123ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1124ee13ed1cSKevin Wolf type != IF_NONE) { 1125ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 1126ee13ed1cSKevin Wolf goto fail; 1127ee13ed1cSKevin Wolf } 1128ee13ed1cSKevin Wolf qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); 1129ee13ed1cSKevin Wolf } 1130ee13ed1cSKevin Wolf 11312d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 113218e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 11333cb0e25cSMarkus Armbruster bs_opts = NULL; 113418e46a03SMarkus Armbruster if (!blk) { 113584d18f06SMarkus Armbruster if (local_err) { 1136565f65d2SMarkus Armbruster error_report_err(local_err); 1137b681072dSKevin Wolf } 11382d246f01SKevin Wolf goto fail; 1139b681072dSKevin Wolf } else { 114084d18f06SMarkus Armbruster assert(!local_err); 11412d246f01SKevin Wolf } 11422d246f01SKevin Wolf 114326f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 114426f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1145f298d071SKevin Wolf dinfo->opts = all_opts; 11462d246f01SKevin Wolf 1147b41a7338SKevin Wolf dinfo->cyls = cyls; 1148b41a7338SKevin Wolf dinfo->heads = heads; 1149b41a7338SKevin Wolf dinfo->secs = secs; 1150b41a7338SKevin Wolf dinfo->trans = translation; 1151b41a7338SKevin Wolf 1152ee13ed1cSKevin Wolf dinfo->type = type; 115387a899c5SKevin Wolf dinfo->bus = bus_id; 115487a899c5SKevin Wolf dinfo->unit = unit_id; 1155394c7d4dSKevin Wolf dinfo->devaddr = devaddr; 1156bcf83158SKevin Wolf dinfo->serial = g_strdup(serial); 1157bcf83158SKevin Wolf 115826f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 115926f8b3a8SMarkus Armbruster 1160e34ef046SKevin Wolf switch(type) { 1161e34ef046SKevin Wolf case IF_IDE: 1162e34ef046SKevin Wolf case IF_SCSI: 1163e34ef046SKevin Wolf case IF_XEN: 1164e34ef046SKevin Wolf case IF_NONE: 1165e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1166e34ef046SKevin Wolf break; 1167e34ef046SKevin Wolf default: 1168e34ef046SKevin Wolf break; 1169e34ef046SKevin Wolf } 1170e34ef046SKevin Wolf 11712d246f01SKevin Wolf fail: 117233cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 11733cb0e25cSMarkus Armbruster QDECREF(bs_opts); 11742d246f01SKevin Wolf return dinfo; 117557975222SKevin Wolf } 117657975222SKevin Wolf 11773e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1178666daa68SMarkus Armbruster { 1179666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1180a0e8544cSFam Zheng BlockBackend *blk; 11812d3735d3SStefan Hajnoczi int ret; 11822d3735d3SStefan Hajnoczi 1183e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1184da31d594SMax Reitz ret = blk_commit_all(); 1185e8877497SStefan Hajnoczi } else { 118684aa0140SStefan Hajnoczi BlockDriverState *bs; 118784aa0140SStefan Hajnoczi AioContext *aio_context; 118884aa0140SStefan Hajnoczi 1189a0e8544cSFam Zheng blk = blk_by_name(device); 1190a0e8544cSFam Zheng if (!blk) { 119158513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1192ac59eb95SMarkus Armbruster return; 11936ab4b5abSMarkus Armbruster } 11945433c24fSMax Reitz if (!blk_is_available(blk)) { 11955433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 11965433c24fSMax Reitz return; 11975433c24fSMax Reitz } 119884aa0140SStefan Hajnoczi 119984aa0140SStefan Hajnoczi bs = blk_bs(blk); 120084aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 120184aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 120284aa0140SStefan Hajnoczi 120384aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 120484aa0140SStefan Hajnoczi 120584aa0140SStefan Hajnoczi aio_context_release(aio_context); 12062d3735d3SStefan Hajnoczi } 120758513bdeSJeff Cody if (ret < 0) { 120858513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 120958513bdeSJeff Cody strerror(-ret)); 1210666daa68SMarkus Armbruster } 1211666daa68SMarkus Armbruster } 1212666daa68SMarkus Armbruster 121310f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 12146cc2a415SPaolo Bonzini { 1215c8a83e85SKevin Wolf TransactionActionList list; 12166cc2a415SPaolo Bonzini 121710f75907SEric Blake list.value = action; 12186cc2a415SPaolo Bonzini list.next = NULL; 121994d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 12206cc2a415SPaolo Bonzini } 12216cc2a415SPaolo Bonzini 12220901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 12230901f67eSBenoît Canet bool has_node_name, const char *node_name, 12240901f67eSBenoît Canet const char *snapshot_file, 12250901f67eSBenoît Canet bool has_snapshot_node_name, 12260901f67eSBenoît Canet const char *snapshot_node_name, 12276106e249SLuiz Capitulino bool has_format, const char *format, 12280901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1229f8882568SJes Sorensen { 1230a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 12310901f67eSBenoît Canet .has_device = has_device, 12326cc2a415SPaolo Bonzini .device = (char *) device, 12330901f67eSBenoît Canet .has_node_name = has_node_name, 12340901f67eSBenoît Canet .node_name = (char *) node_name, 12356cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 12360901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 12370901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 12386cc2a415SPaolo Bonzini .has_format = has_format, 12396cc2a415SPaolo Bonzini .format = (char *) format, 12406cc2a415SPaolo Bonzini .has_mode = has_mode, 12416cc2a415SPaolo Bonzini .mode = mode, 12426cc2a415SPaolo Bonzini }; 124310f75907SEric Blake TransactionAction action = { 124410f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 124532bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 124610f75907SEric Blake }; 124710f75907SEric Blake blockdev_do_action(&action, errp); 1248f8882568SJes Sorensen } 1249f8882568SJes Sorensen 125043de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 125143de7e2dSAlberto Garcia Error **errp) 125243de7e2dSAlberto Garcia { 125343de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 125443de7e2dSAlberto Garcia .node = (char *) node, 125543de7e2dSAlberto Garcia .overlay = (char *) overlay 125643de7e2dSAlberto Garcia }; 125710f75907SEric Blake TransactionAction action = { 125810f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 125932bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 126010f75907SEric Blake }; 126110f75907SEric Blake blockdev_do_action(&action, errp); 126243de7e2dSAlberto Garcia } 126343de7e2dSAlberto Garcia 1264f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1265f323bc9eSWenchao Xia const char *name, 1266f323bc9eSWenchao Xia Error **errp) 1267f323bc9eSWenchao Xia { 1268f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1269f323bc9eSWenchao Xia .device = (char *) device, 1270f323bc9eSWenchao Xia .name = (char *) name 1271f323bc9eSWenchao Xia }; 127210f75907SEric Blake TransactionAction action = { 127310f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 127432bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 127510f75907SEric Blake }; 127610f75907SEric Blake blockdev_do_action(&action, errp); 1277f323bc9eSWenchao Xia } 1278f323bc9eSWenchao Xia 127944e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 128044e3e053SWenchao Xia bool has_id, 128144e3e053SWenchao Xia const char *id, 128244e3e053SWenchao Xia bool has_name, 128344e3e053SWenchao Xia const char *name, 128444e3e053SWenchao Xia Error **errp) 128544e3e053SWenchao Xia { 1286a0e8544cSFam Zheng BlockDriverState *bs; 1287a0e8544cSFam Zheng BlockBackend *blk; 12884ef3982aSStefan Hajnoczi AioContext *aio_context; 128944e3e053SWenchao Xia QEMUSnapshotInfo sn; 129044e3e053SWenchao Xia Error *local_err = NULL; 129144e3e053SWenchao Xia SnapshotInfo *info = NULL; 129244e3e053SWenchao Xia int ret; 129344e3e053SWenchao Xia 1294a0e8544cSFam Zheng blk = blk_by_name(device); 1295a0e8544cSFam Zheng if (!blk) { 129675158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 129775158ebbSMarkus Armbruster "Device '%s' not found", device); 129844e3e053SWenchao Xia return NULL; 129944e3e053SWenchao Xia } 13005433c24fSMax Reitz 13015433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 13025433c24fSMax Reitz aio_context_acquire(aio_context); 130344e3e053SWenchao Xia 130444e3e053SWenchao Xia if (!has_id) { 130544e3e053SWenchao Xia id = NULL; 130644e3e053SWenchao Xia } 130744e3e053SWenchao Xia 130844e3e053SWenchao Xia if (!has_name) { 130944e3e053SWenchao Xia name = NULL; 131044e3e053SWenchao Xia } 131144e3e053SWenchao Xia 131244e3e053SWenchao Xia if (!id && !name) { 131344e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 13145433c24fSMax Reitz goto out_aio_context; 131544e3e053SWenchao Xia } 131644e3e053SWenchao Xia 13175433c24fSMax Reitz if (!blk_is_available(blk)) { 13185433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 13195433c24fSMax Reitz goto out_aio_context; 13205433c24fSMax Reitz } 13215433c24fSMax Reitz bs = blk_bs(blk); 13224ef3982aSStefan Hajnoczi 13230b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 13240b928854SStefan Hajnoczi goto out_aio_context; 13250b928854SStefan Hajnoczi } 13260b928854SStefan Hajnoczi 132744e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 132884d18f06SMarkus Armbruster if (local_err) { 132944e3e053SWenchao Xia error_propagate(errp, local_err); 13304ef3982aSStefan Hajnoczi goto out_aio_context; 133144e3e053SWenchao Xia } 133244e3e053SWenchao Xia if (!ret) { 133344e3e053SWenchao Xia error_setg(errp, 133444e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 133544e3e053SWenchao Xia "device '%s'", 133644e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 13374ef3982aSStefan Hajnoczi goto out_aio_context; 133844e3e053SWenchao Xia } 133944e3e053SWenchao Xia 134044e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 134184d18f06SMarkus Armbruster if (local_err) { 134244e3e053SWenchao Xia error_propagate(errp, local_err); 13434ef3982aSStefan Hajnoczi goto out_aio_context; 134444e3e053SWenchao Xia } 134544e3e053SWenchao Xia 13464ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13474ef3982aSStefan Hajnoczi 13485839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 134944e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 135044e3e053SWenchao Xia info->name = g_strdup(sn.name); 135144e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 135244e3e053SWenchao Xia info->date_sec = sn.date_sec; 135344e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 135444e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 135544e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 135644e3e053SWenchao Xia 135744e3e053SWenchao Xia return info; 13584ef3982aSStefan Hajnoczi 13594ef3982aSStefan Hajnoczi out_aio_context: 13604ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13614ef3982aSStefan Hajnoczi return NULL; 136244e3e053SWenchao Xia } 13638802d1fdSJeff Cody 1364341ebc2fSJohn Snow /** 1365341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1366341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1367341ebc2fSJohn Snow * the node reference and bitmap names. 1368341ebc2fSJohn Snow * 1369341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1370341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1371341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1372341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1373341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1374341ebc2fSJohn Snow * 1375341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1376341ebc2fSJohn Snow */ 1377341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1378341ebc2fSJohn Snow const char *name, 1379341ebc2fSJohn Snow BlockDriverState **pbs, 1380341ebc2fSJohn Snow AioContext **paio, 1381341ebc2fSJohn Snow Error **errp) 1382341ebc2fSJohn Snow { 1383341ebc2fSJohn Snow BlockDriverState *bs; 1384341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1385341ebc2fSJohn Snow AioContext *aio_context; 1386341ebc2fSJohn Snow 1387341ebc2fSJohn Snow if (!node) { 1388341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1389341ebc2fSJohn Snow return NULL; 1390341ebc2fSJohn Snow } 1391341ebc2fSJohn Snow if (!name) { 1392341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1393341ebc2fSJohn Snow return NULL; 1394341ebc2fSJohn Snow } 1395341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1396341ebc2fSJohn Snow if (!bs) { 1397341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1398341ebc2fSJohn Snow return NULL; 1399341ebc2fSJohn Snow } 1400341ebc2fSJohn Snow 1401341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 1402341ebc2fSJohn Snow aio_context_acquire(aio_context); 1403341ebc2fSJohn Snow 1404341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1405341ebc2fSJohn Snow if (!bitmap) { 1406341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 1407341ebc2fSJohn Snow goto fail; 1408341ebc2fSJohn Snow } 1409341ebc2fSJohn Snow 1410341ebc2fSJohn Snow if (pbs) { 1411341ebc2fSJohn Snow *pbs = bs; 1412341ebc2fSJohn Snow } 1413341ebc2fSJohn Snow if (paio) { 1414341ebc2fSJohn Snow *paio = aio_context; 1415341ebc2fSJohn Snow } else { 1416341ebc2fSJohn Snow aio_context_release(aio_context); 1417341ebc2fSJohn Snow } 1418341ebc2fSJohn Snow 1419341ebc2fSJohn Snow return bitmap; 1420341ebc2fSJohn Snow 1421341ebc2fSJohn Snow fail: 1422341ebc2fSJohn Snow aio_context_release(aio_context); 1423341ebc2fSJohn Snow return NULL; 1424341ebc2fSJohn Snow } 1425341ebc2fSJohn Snow 1426b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1427ba0c86a3SWenchao Xia 142850f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1429ba0c86a3SWenchao Xia 143050f43f0fSJohn Snow /** 143150f43f0fSJohn Snow * BlkActionOps: 143250f43f0fSJohn Snow * Table of operations that define an Action. 143350f43f0fSJohn Snow * 143450f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 143550f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 143650f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 143750f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 143850f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 143950f43f0fSJohn Snow * commit() or abort(). Can be NULL. 144050f43f0fSJohn Snow * 144150f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 144250f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1443ba0c86a3SWenchao Xia */ 144450f43f0fSJohn Snow typedef struct BlkActionOps { 144550f43f0fSJohn Snow size_t instance_size; 144650f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 144750f43f0fSJohn Snow void (*commit)(BlkActionState *common); 144850f43f0fSJohn Snow void (*abort)(BlkActionState *common); 144950f43f0fSJohn Snow void (*clean)(BlkActionState *common); 145050f43f0fSJohn Snow } BlkActionOps; 145150f43f0fSJohn Snow 145250f43f0fSJohn Snow /** 145350f43f0fSJohn Snow * BlkActionState: 145450f43f0fSJohn Snow * Describes one Action's state within a Transaction. 145550f43f0fSJohn Snow * 145650f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 145750f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 145894d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 145950f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 146050f43f0fSJohn Snow * 146150f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 146250f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 146350f43f0fSJohn Snow * base class. 146450f43f0fSJohn Snow */ 146550f43f0fSJohn Snow struct BlkActionState { 1466c8a83e85SKevin Wolf TransactionAction *action; 146750f43f0fSJohn Snow const BlkActionOps *ops; 146894d16a64SJohn Snow BlockJobTxn *block_job_txn; 146994d16a64SJohn Snow TransactionProperties *txn_props; 147050f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1471ba0c86a3SWenchao Xia }; 1472ba0c86a3SWenchao Xia 1473bbe86010SWenchao Xia /* internal snapshot private data */ 1474bbe86010SWenchao Xia typedef struct InternalSnapshotState { 147550f43f0fSJohn Snow BlkActionState common; 1476bbe86010SWenchao Xia BlockDriverState *bs; 14775d6e96efSStefan Hajnoczi AioContext *aio_context; 1478bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1479507306ccSFam Zheng bool created; 1480bbe86010SWenchao Xia } InternalSnapshotState; 1481bbe86010SWenchao Xia 148294d16a64SJohn Snow 148394d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 148494d16a64SJohn Snow { 148594d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 148694d16a64SJohn Snow error_setg(errp, 148794d16a64SJohn Snow "Action '%s' does not support Transaction property " 148894d16a64SJohn Snow "completion-mode = %s", 148994d16a64SJohn Snow TransactionActionKind_lookup[s->action->type], 149094d16a64SJohn Snow ActionCompletionMode_lookup[s->txn_props->completion_mode]); 149194d16a64SJohn Snow return -1; 149294d16a64SJohn Snow } 149394d16a64SJohn Snow return 0; 149494d16a64SJohn Snow } 149594d16a64SJohn Snow 149650f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1497bbe86010SWenchao Xia Error **errp) 1498bbe86010SWenchao Xia { 1499f70edf99SMarkus Armbruster Error *local_err = NULL; 1500bbe86010SWenchao Xia const char *device; 1501bbe86010SWenchao Xia const char *name; 1502a0e8544cSFam Zheng BlockBackend *blk; 1503bbe86010SWenchao Xia BlockDriverState *bs; 1504bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1505bbe86010SWenchao Xia bool ret; 1506bbe86010SWenchao Xia qemu_timeval tv; 1507bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1508bbe86010SWenchao Xia InternalSnapshotState *state; 1509bbe86010SWenchao Xia int ret1; 1510bbe86010SWenchao Xia 15116a8f9661SEric Blake g_assert(common->action->type == 1512bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 151332bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1514bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1515bbe86010SWenchao Xia 1516bbe86010SWenchao Xia /* 1. parse input */ 1517bbe86010SWenchao Xia device = internal->device; 1518bbe86010SWenchao Xia name = internal->name; 1519bbe86010SWenchao Xia 1520bbe86010SWenchao Xia /* 2. check for validation */ 152194d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 152294d16a64SJohn Snow return; 152394d16a64SJohn Snow } 152494d16a64SJohn Snow 1525a0e8544cSFam Zheng blk = blk_by_name(device); 1526a0e8544cSFam Zheng if (!blk) { 152775158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 152875158ebbSMarkus Armbruster "Device '%s' not found", device); 1529bbe86010SWenchao Xia return; 1530bbe86010SWenchao Xia } 1531bbe86010SWenchao Xia 15325d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 15335433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 15345d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 15355d6e96efSStefan Hajnoczi 15365433c24fSMax Reitz if (!blk_is_available(blk)) { 1537c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1538bbe86010SWenchao Xia return; 1539bbe86010SWenchao Xia } 15405433c24fSMax Reitz bs = blk_bs(blk); 1541bbe86010SWenchao Xia 1542507306ccSFam Zheng state->bs = bs; 1543507306ccSFam Zheng bdrv_drained_begin(bs); 1544507306ccSFam Zheng 15453dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 15463dc7ca3cSStefan Hajnoczi return; 15473dc7ca3cSStefan Hajnoczi } 15483dc7ca3cSStefan Hajnoczi 1549bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 155081e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1551bbe86010SWenchao Xia return; 1552bbe86010SWenchao Xia } 1553bbe86010SWenchao Xia 1554bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 155581e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 155681e5f78aSAlberto Garcia "does not support internal snapshots", 155781e5f78aSAlberto Garcia bs->drv->format_name, device); 1558bbe86010SWenchao Xia return; 1559bbe86010SWenchao Xia } 1560bbe86010SWenchao Xia 1561bbe86010SWenchao Xia if (!strlen(name)) { 1562bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1563bbe86010SWenchao Xia return; 1564bbe86010SWenchao Xia } 1565bbe86010SWenchao Xia 1566bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1567f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1568f70edf99SMarkus Armbruster &local_err); 1569f70edf99SMarkus Armbruster if (local_err) { 1570f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1571bbe86010SWenchao Xia return; 1572bbe86010SWenchao Xia } else if (ret) { 1573bbe86010SWenchao Xia error_setg(errp, 1574bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1575bbe86010SWenchao Xia name, device); 1576bbe86010SWenchao Xia return; 1577bbe86010SWenchao Xia } 1578bbe86010SWenchao Xia 1579bbe86010SWenchao Xia /* 3. take the snapshot */ 1580bbe86010SWenchao Xia sn = &state->sn; 1581bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1582bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1583bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1584bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1585bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1586bbe86010SWenchao Xia 1587bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1588bbe86010SWenchao Xia if (ret1 < 0) { 1589bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1590bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1591bbe86010SWenchao Xia name, device); 1592bbe86010SWenchao Xia return; 1593bbe86010SWenchao Xia } 1594bbe86010SWenchao Xia 1595bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1596507306ccSFam Zheng state->created = true; 1597bbe86010SWenchao Xia } 1598bbe86010SWenchao Xia 159950f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1600bbe86010SWenchao Xia { 1601bbe86010SWenchao Xia InternalSnapshotState *state = 1602bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1603bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1604bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1605bbe86010SWenchao Xia Error *local_error = NULL; 1606bbe86010SWenchao Xia 1607507306ccSFam Zheng if (!state->created) { 1608bbe86010SWenchao Xia return; 1609bbe86010SWenchao Xia } 1610bbe86010SWenchao Xia 1611bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1612c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1613c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1614c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1615c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1616c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1617bbe86010SWenchao Xia } 1618bbe86010SWenchao Xia } 1619bbe86010SWenchao Xia 162050f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 16215d6e96efSStefan Hajnoczi { 16225d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 16235d6e96efSStefan Hajnoczi common, common); 16245d6e96efSStefan Hajnoczi 16255d6e96efSStefan Hajnoczi if (state->aio_context) { 1626507306ccSFam Zheng if (state->bs) { 1627507306ccSFam Zheng bdrv_drained_end(state->bs); 1628507306ccSFam Zheng } 16295d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16305d6e96efSStefan Hajnoczi } 16315d6e96efSStefan Hajnoczi } 16325d6e96efSStefan Hajnoczi 1633ba0c86a3SWenchao Xia /* external snapshot private data */ 1634ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 163550f43f0fSJohn Snow BlkActionState common; 16368802d1fdSJeff Cody BlockDriverState *old_bs; 16378802d1fdSJeff Cody BlockDriverState *new_bs; 16385d6e96efSStefan Hajnoczi AioContext *aio_context; 1639ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16408802d1fdSJeff Cody 164150f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16429b9877eeSWenchao Xia Error **errp) 16439b9877eeSWenchao Xia { 16445b363937SMax Reitz int flags = 0; 164543de7e2dSAlberto Garcia QDict *options = NULL; 16469b9877eeSWenchao Xia Error *local_err = NULL; 164743de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1648e2a31e87SWenchao Xia const char *device; 16490901f67eSBenoît Canet const char *node_name; 165043de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 165143de7e2dSAlberto Garcia const char *snapshot_ref; 165243de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1653e2a31e87SWenchao Xia const char *new_image_file; 1654ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1655ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1656c8a83e85SKevin Wolf TransactionAction *action = common->action; 16579b9877eeSWenchao Xia 165843de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 165943de7e2dSAlberto Garcia * purpose but a different set of parameters */ 166043de7e2dSAlberto Garcia switch (action->type) { 166143de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 166243de7e2dSAlberto Garcia { 166332bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 166443de7e2dSAlberto Garcia device = s->node; 166543de7e2dSAlberto Garcia node_name = s->node; 166643de7e2dSAlberto Garcia new_image_file = NULL; 166743de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1668e2a31e87SWenchao Xia } 166943de7e2dSAlberto Garcia break; 167043de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 167143de7e2dSAlberto Garcia { 167232bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 167343de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 167443de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 167543de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 167643de7e2dSAlberto Garcia snapshot_ref = NULL; 167743de7e2dSAlberto Garcia } 167843de7e2dSAlberto Garcia break; 167943de7e2dSAlberto Garcia default: 168043de7e2dSAlberto Garcia g_assert_not_reached(); 1681e2a31e87SWenchao Xia } 1682e2a31e87SWenchao Xia 1683e2a31e87SWenchao Xia /* start processing */ 168494d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 168594d16a64SJohn Snow return; 168694d16a64SJohn Snow } 168794d16a64SJohn Snow 168843de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 168943de7e2dSAlberto Garcia if (!state->old_bs) { 16909b9877eeSWenchao Xia return; 16919b9877eeSWenchao Xia } 16929b9877eeSWenchao Xia 16935d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 16945d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 16955d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1696da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 16975d6e96efSStefan Hajnoczi 1698ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1699c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 17009b9877eeSWenchao Xia return; 17019b9877eeSWenchao Xia } 17029b9877eeSWenchao Xia 17033718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 17043718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 17059b9877eeSWenchao Xia return; 17069b9877eeSWenchao Xia } 17079b9877eeSWenchao Xia 1708ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1709ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1710c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 17119b9877eeSWenchao Xia return; 17129b9877eeSWenchao Xia } 17139b9877eeSWenchao Xia } 17149b9877eeSWenchao Xia 1715212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1716c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1717f6186f49SBenoît Canet return; 1718f6186f49SBenoît Canet } 1719f6186f49SBenoît Canet 172043de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 172132bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 172243de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 172343de7e2dSAlberto Garcia enum NewImageMode mode; 172443de7e2dSAlberto Garcia const char *snapshot_node_name = 172543de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 172643de7e2dSAlberto Garcia 172743de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 172843de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 172943de7e2dSAlberto Garcia return; 173043de7e2dSAlberto Garcia } 173143de7e2dSAlberto Garcia 173243de7e2dSAlberto Garcia if (snapshot_node_name && 173343de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 173443de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 173543de7e2dSAlberto Garcia return; 173643de7e2dSAlberto Garcia } 173743de7e2dSAlberto Garcia 1738ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17394c844983SKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); 17409b9877eeSWenchao Xia 17419b9877eeSWenchao Xia /* create new image w/backing file */ 174243de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17439b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1744f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1745f86b8b58SKevin Wolf if (size < 0) { 1746f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 1747f86b8b58SKevin Wolf return; 1748f86b8b58SKevin Wolf } 17499b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1750ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1751ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 1752f86b8b58SKevin Wolf NULL, size, flags, &local_err, false); 175384d18f06SMarkus Armbruster if (local_err) { 17549b9877eeSWenchao Xia error_propagate(errp, local_err); 17559b9877eeSWenchao Xia return; 17569b9877eeSWenchao Xia } 17579b9877eeSWenchao Xia } 17589b9877eeSWenchao Xia 17590901f67eSBenoît Canet options = qdict_new(); 176043de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 17610901f67eSBenoît Canet qdict_put(options, "node-name", 17620901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 17630901f67eSBenoît Canet } 1764e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 17650901f67eSBenoît Canet 176643de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 176743de7e2dSAlberto Garcia } 176843de7e2dSAlberto Garcia 17695b363937SMax Reitz state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, 17705b363937SMax Reitz errp); 1771f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17725b363937SMax Reitz if (!state->new_bs) { 177343de7e2dSAlberto Garcia return; 177443de7e2dSAlberto Garcia } 177543de7e2dSAlberto Garcia 17761f0c461bSKevin Wolf if (bdrv_has_blk(state->new_bs)) { 177743de7e2dSAlberto Garcia error_setg(errp, "The snapshot is already in use by %s", 17781f0c461bSKevin Wolf bdrv_get_parent_name(state->new_bs)); 177943de7e2dSAlberto Garcia return; 178043de7e2dSAlberto Garcia } 178143de7e2dSAlberto Garcia 178243de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 178343de7e2dSAlberto Garcia errp)) { 178443de7e2dSAlberto Garcia return; 178543de7e2dSAlberto Garcia } 178643de7e2dSAlberto Garcia 178743de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 178843de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 178908b24cfeSAlberto Garcia return; 179008b24cfeSAlberto Garcia } 179108b24cfeSAlberto Garcia 179208b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 179308b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 17949b9877eeSWenchao Xia } 17959b9877eeSWenchao Xia } 17969b9877eeSWenchao Xia 179750f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 17983b0047e8SWenchao Xia { 1799ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1800ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1801ba0c86a3SWenchao Xia 18025d6e96efSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, state->aio_context); 18035d6e96efSStefan Hajnoczi 1804ba5d6ab6SStefan Hajnoczi /* This removes our old bs and adds the new bs */ 1805ba5d6ab6SStefan Hajnoczi bdrv_append(state->new_bs, state->old_bs); 18063b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 18073b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 18083b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 18094c844983SKevin Wolf if (!state->old_bs->copy_on_read) { 1810dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 18113b0047e8SWenchao Xia NULL); 18123b0047e8SWenchao Xia } 18134c844983SKevin Wolf } 18143b0047e8SWenchao Xia 181550f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 181696b86bf7SWenchao Xia { 1817ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1818ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1819ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 18204f6fd349SFam Zheng bdrv_unref(state->new_bs); 182196b86bf7SWenchao Xia } 1822da763e83SFam Zheng } 1823da763e83SFam Zheng 182450f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1825da763e83SFam Zheng { 1826da763e83SFam Zheng ExternalSnapshotState *state = 1827da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 18285d6e96efSStefan Hajnoczi if (state->aio_context) { 1829da763e83SFam Zheng bdrv_drained_end(state->old_bs); 18305d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18315d6e96efSStefan Hajnoczi } 183296b86bf7SWenchao Xia } 183396b86bf7SWenchao Xia 18343037f364SStefan Hajnoczi typedef struct DriveBackupState { 183550f43f0fSJohn Snow BlkActionState common; 18363037f364SStefan Hajnoczi BlockDriverState *bs; 18375d6e96efSStefan Hajnoczi AioContext *aio_context; 18383037f364SStefan Hajnoczi BlockJob *job; 18393037f364SStefan Hajnoczi } DriveBackupState; 18403037f364SStefan Hajnoczi 184170559d49SAlberto Garcia static void do_drive_backup(const char *job_id, const char *device, 184270559d49SAlberto Garcia const char *target, bool has_format, 184370559d49SAlberto Garcia const char *format, enum MirrorSyncMode sync, 184478f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 184578f51fdeSJohn Snow bool has_speed, int64_t speed, 184678f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 184778f51fdeSJohn Snow bool has_on_source_error, 184878f51fdeSJohn Snow BlockdevOnError on_source_error, 184978f51fdeSJohn Snow bool has_on_target_error, 185078f51fdeSJohn Snow BlockdevOnError on_target_error, 185178f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 185278f51fdeSJohn Snow 185350f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18543037f364SStefan Hajnoczi { 18553037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1856a0e8544cSFam Zheng BlockBackend *blk; 18573037f364SStefan Hajnoczi DriveBackup *backup; 18583037f364SStefan Hajnoczi Error *local_err = NULL; 18593037f364SStefan Hajnoczi 18606a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 186132bafa8fSEric Blake backup = common->action->u.drive_backup.data; 18623037f364SStefan Hajnoczi 1863a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1864a0e8544cSFam Zheng if (!blk) { 186575158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 186675158ebbSMarkus Armbruster "Device '%s' not found", backup->device); 18675d6e96efSStefan Hajnoczi return; 18685d6e96efSStefan Hajnoczi } 18695d6e96efSStefan Hajnoczi 18701fdd4b7bSFam Zheng if (!blk_is_available(blk)) { 18711fdd4b7bSFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 18721fdd4b7bSFam Zheng return; 18731fdd4b7bSFam Zheng } 18741fdd4b7bSFam Zheng 18755d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 18765433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 18775d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 18781fdd4b7bSFam Zheng bdrv_drained_begin(blk_bs(blk)); 18791fdd4b7bSFam Zheng state->bs = blk_bs(blk); 18805d6e96efSStefan Hajnoczi 188170559d49SAlberto Garcia do_drive_backup(backup->has_job_id ? backup->job_id : NULL, 188270559d49SAlberto Garcia backup->device, backup->target, 18833037f364SStefan Hajnoczi backup->has_format, backup->format, 1884b53169eaSStefan Hajnoczi backup->sync, 18853037f364SStefan Hajnoczi backup->has_mode, backup->mode, 18863037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1887d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 18883037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 18893037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 189094d16a64SJohn Snow common->block_job_txn, &local_err); 189184d18f06SMarkus Armbruster if (local_err) { 18923037f364SStefan Hajnoczi error_propagate(errp, local_err); 18933037f364SStefan Hajnoczi return; 18943037f364SStefan Hajnoczi } 18953037f364SStefan Hajnoczi 18963037f364SStefan Hajnoczi state->job = state->bs->job; 18973037f364SStefan Hajnoczi } 18983037f364SStefan Hajnoczi 189950f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 19003037f364SStefan Hajnoczi { 19013037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19023037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 19033037f364SStefan Hajnoczi 19043037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 19053037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 19063037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 19073037f364SStefan Hajnoczi } 19083037f364SStefan Hajnoczi } 19093037f364SStefan Hajnoczi 191050f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 19115d6e96efSStefan Hajnoczi { 19125d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19135d6e96efSStefan Hajnoczi 19145d6e96efSStefan Hajnoczi if (state->aio_context) { 19151fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 19165d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 19175d6e96efSStefan Hajnoczi } 19185d6e96efSStefan Hajnoczi } 19195d6e96efSStefan Hajnoczi 1920bd8baecdSFam Zheng typedef struct BlockdevBackupState { 192150f43f0fSJohn Snow BlkActionState common; 1922bd8baecdSFam Zheng BlockDriverState *bs; 1923bd8baecdSFam Zheng BlockJob *job; 1924bd8baecdSFam Zheng AioContext *aio_context; 1925bd8baecdSFam Zheng } BlockdevBackupState; 1926bd8baecdSFam Zheng 192770559d49SAlberto Garcia static void do_blockdev_backup(const char *job_id, const char *device, 192870559d49SAlberto Garcia const char *target, enum MirrorSyncMode sync, 192978f51fdeSJohn Snow bool has_speed, int64_t speed, 193078f51fdeSJohn Snow bool has_on_source_error, 193178f51fdeSJohn Snow BlockdevOnError on_source_error, 193278f51fdeSJohn Snow bool has_on_target_error, 193378f51fdeSJohn Snow BlockdevOnError on_target_error, 193478f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 193578f51fdeSJohn Snow 193650f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1937bd8baecdSFam Zheng { 1938bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1939bd8baecdSFam Zheng BlockdevBackup *backup; 19405433c24fSMax Reitz BlockBackend *blk, *target; 1941bd8baecdSFam Zheng Error *local_err = NULL; 1942bd8baecdSFam Zheng 19436a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 194432bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1945bd8baecdSFam Zheng 1946a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1947a0e8544cSFam Zheng if (!blk) { 19485b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->device); 1949bd8baecdSFam Zheng return; 1950bd8baecdSFam Zheng } 1951bd8baecdSFam Zheng 1952ff52bf36SFam Zheng if (!blk_is_available(blk)) { 1953ff52bf36SFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1954ff52bf36SFam Zheng return; 1955ff52bf36SFam Zheng } 1956ff52bf36SFam Zheng 19575433c24fSMax Reitz target = blk_by_name(backup->target); 19585433c24fSMax Reitz if (!target) { 19595b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->target); 1960bd8baecdSFam Zheng return; 1961bd8baecdSFam Zheng } 1962bd8baecdSFam Zheng 1963bd8baecdSFam Zheng /* AioContext is released in .clean() */ 19645433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 19655433c24fSMax Reitz if (state->aio_context != blk_get_aio_context(target)) { 1966bd8baecdSFam Zheng state->aio_context = NULL; 1967bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1968bd8baecdSFam Zheng return; 1969bd8baecdSFam Zheng } 1970bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1971ff52bf36SFam Zheng state->bs = blk_bs(blk); 1972ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1973bd8baecdSFam Zheng 197470559d49SAlberto Garcia do_blockdev_backup(backup->has_job_id ? backup->job_id : NULL, 197570559d49SAlberto Garcia backup->device, backup->target, backup->sync, 1976bd8baecdSFam Zheng backup->has_speed, backup->speed, 1977bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1978bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 197994d16a64SJohn Snow common->block_job_txn, &local_err); 1980bd8baecdSFam Zheng if (local_err) { 1981bd8baecdSFam Zheng error_propagate(errp, local_err); 1982bd8baecdSFam Zheng return; 1983bd8baecdSFam Zheng } 1984bd8baecdSFam Zheng 1985bd8baecdSFam Zheng state->job = state->bs->job; 1986bd8baecdSFam Zheng } 1987bd8baecdSFam Zheng 198850f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1989bd8baecdSFam Zheng { 1990bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1991bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1992bd8baecdSFam Zheng 1993bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 1994bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 1995bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 1996bd8baecdSFam Zheng } 1997bd8baecdSFam Zheng } 1998bd8baecdSFam Zheng 199950f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 2000bd8baecdSFam Zheng { 2001bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 2002bd8baecdSFam Zheng 2003bd8baecdSFam Zheng if (state->aio_context) { 2004ff52bf36SFam Zheng bdrv_drained_end(state->bs); 2005bd8baecdSFam Zheng aio_context_release(state->aio_context); 2006bd8baecdSFam Zheng } 2007bd8baecdSFam Zheng } 2008bd8baecdSFam Zheng 2009df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 201050f43f0fSJohn Snow BlkActionState common; 2011df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 2012df9a681dSFam Zheng BlockDriverState *bs; 2013df9a681dSFam Zheng AioContext *aio_context; 2014df9a681dSFam Zheng HBitmap *backup; 2015df9a681dSFam Zheng bool prepared; 2016df9a681dSFam Zheng } BlockDirtyBitmapState; 2017df9a681dSFam Zheng 201850f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 2019df9a681dSFam Zheng Error **errp) 2020df9a681dSFam Zheng { 2021df9a681dSFam Zheng Error *local_err = NULL; 2022df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2023df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2024df9a681dSFam Zheng common, common); 2025df9a681dSFam Zheng 202694d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 202794d16a64SJohn Snow return; 202894d16a64SJohn Snow } 202994d16a64SJohn Snow 203032bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2031df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2032df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2033df9a681dSFam Zheng action->has_granularity, action->granularity, 2034df9a681dSFam Zheng &local_err); 2035df9a681dSFam Zheng 2036df9a681dSFam Zheng if (!local_err) { 2037df9a681dSFam Zheng state->prepared = true; 2038df9a681dSFam Zheng } else { 2039df9a681dSFam Zheng error_propagate(errp, local_err); 2040df9a681dSFam Zheng } 2041df9a681dSFam Zheng } 2042df9a681dSFam Zheng 204350f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2044df9a681dSFam Zheng { 2045df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2046df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2047df9a681dSFam Zheng common, common); 2048df9a681dSFam Zheng 204932bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2050df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2051df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2052df9a681dSFam Zheng */ 2053df9a681dSFam Zheng if (state->prepared) { 2054df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2055df9a681dSFam Zheng } 2056df9a681dSFam Zheng } 2057df9a681dSFam Zheng 205850f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2059df9a681dSFam Zheng Error **errp) 2060df9a681dSFam Zheng { 2061df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2062df9a681dSFam Zheng common, common); 2063df9a681dSFam Zheng BlockDirtyBitmap *action; 2064df9a681dSFam Zheng 206594d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 206694d16a64SJohn Snow return; 206794d16a64SJohn Snow } 206894d16a64SJohn Snow 206932bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2070df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2071df9a681dSFam Zheng action->name, 2072df9a681dSFam Zheng &state->bs, 2073df9a681dSFam Zheng &state->aio_context, 2074df9a681dSFam Zheng errp); 2075df9a681dSFam Zheng if (!state->bitmap) { 2076df9a681dSFam Zheng return; 2077df9a681dSFam Zheng } 2078df9a681dSFam Zheng 2079df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2080df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2081df9a681dSFam Zheng return; 2082df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2083df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2084df9a681dSFam Zheng return; 2085df9a681dSFam Zheng } 2086df9a681dSFam Zheng 2087df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2088df9a681dSFam Zheng /* AioContext is released in .clean() */ 2089df9a681dSFam Zheng } 2090df9a681dSFam Zheng 209150f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2092df9a681dSFam Zheng { 2093df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2094df9a681dSFam Zheng common, common); 2095df9a681dSFam Zheng 2096df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2097df9a681dSFam Zheng } 2098df9a681dSFam Zheng 209950f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2100df9a681dSFam Zheng { 2101df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2102df9a681dSFam Zheng common, common); 2103df9a681dSFam Zheng 2104df9a681dSFam Zheng hbitmap_free(state->backup); 2105df9a681dSFam Zheng } 2106df9a681dSFam Zheng 210750f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2108df9a681dSFam Zheng { 2109df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2110df9a681dSFam Zheng common, common); 2111df9a681dSFam Zheng 2112df9a681dSFam Zheng if (state->aio_context) { 2113df9a681dSFam Zheng aio_context_release(state->aio_context); 2114df9a681dSFam Zheng } 2115df9a681dSFam Zheng } 2116df9a681dSFam Zheng 211750f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 211878b18b78SStefan Hajnoczi { 211978b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 212078b18b78SStefan Hajnoczi } 212178b18b78SStefan Hajnoczi 212250f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 212378b18b78SStefan Hajnoczi { 2124dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 212578b18b78SStefan Hajnoczi } 212678b18b78SStefan Hajnoczi 212750f43f0fSJohn Snow static const BlkActionOps actions[] = { 212843de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 212943de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 213043de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 213143de7e2dSAlberto Garcia .commit = external_snapshot_commit, 213243de7e2dSAlberto Garcia .abort = external_snapshot_abort, 21334ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 213443de7e2dSAlberto Garcia }, 2135c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2136ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2137ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2138ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2139ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2140da763e83SFam Zheng .clean = external_snapshot_clean, 2141ba0c86a3SWenchao Xia }, 21423037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21433037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21443037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 21453037f364SStefan Hajnoczi .abort = drive_backup_abort, 21465d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21473037f364SStefan Hajnoczi }, 2148bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2149bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2150bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2151bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2152bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2153bd8baecdSFam Zheng }, 215478b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 215550f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 215678b18b78SStefan Hajnoczi .prepare = abort_prepare, 215778b18b78SStefan Hajnoczi .commit = abort_commit, 215878b18b78SStefan Hajnoczi }, 2159bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2160bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2161bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2162bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21635d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2164bbe86010SWenchao Xia }, 2165df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2166df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2167df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2168df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2169df9a681dSFam Zheng }, 2170df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2171df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2172df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2173df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2174df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2175df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2176df9a681dSFam Zheng } 2177ba0c86a3SWenchao Xia }; 2178ba0c86a3SWenchao Xia 217994d16a64SJohn Snow /** 218094d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 218194d16a64SJohn Snow * that structure with desired defaults if they are unset. 218294d16a64SJohn Snow */ 218394d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 218494d16a64SJohn Snow TransactionProperties *props) 218594d16a64SJohn Snow { 218694d16a64SJohn Snow if (!props) { 218794d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 218894d16a64SJohn Snow } 218994d16a64SJohn Snow 219094d16a64SJohn Snow if (!props->has_completion_mode) { 219194d16a64SJohn Snow props->has_completion_mode = true; 219294d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 219394d16a64SJohn Snow } 219494d16a64SJohn Snow 219594d16a64SJohn Snow return props; 219694d16a64SJohn Snow } 219794d16a64SJohn Snow 21988802d1fdSJeff Cody /* 2199b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2200b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 22018802d1fdSJeff Cody */ 220294d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 220394d16a64SJohn Snow bool has_props, 220494d16a64SJohn Snow struct TransactionProperties *props, 220594d16a64SJohn Snow Error **errp) 22068802d1fdSJeff Cody { 2207c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 220894d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 220950f43f0fSJohn Snow BlkActionState *state, *next; 221043e17041SLuiz Capitulino Error *local_err = NULL; 22118802d1fdSJeff Cody 221250f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 22138802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 22148802d1fdSJeff Cody 221594d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 221694d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 221794d16a64SJohn Snow */ 221894d16a64SJohn Snow props = get_transaction_properties(props); 221994d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 222094d16a64SJohn Snow block_job_txn = block_job_txn_new(); 222194d16a64SJohn Snow } 222294d16a64SJohn Snow 2223b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 22248802d1fdSJeff Cody bdrv_drain_all(); 22258802d1fdSJeff Cody 2226b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 22278802d1fdSJeff Cody while (NULL != dev_entry) { 2228c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 222950f43f0fSJohn Snow const BlkActionOps *ops; 223052e7c241SPaolo Bonzini 22318802d1fdSJeff Cody dev_info = dev_entry->value; 22328802d1fdSJeff Cody dev_entry = dev_entry->next; 22338802d1fdSJeff Cody 22346a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2235ba0c86a3SWenchao Xia 22366a8f9661SEric Blake ops = &actions[dev_info->type]; 2237aa3fe714SMax Reitz assert(ops->instance_size > 0); 2238aa3fe714SMax Reitz 2239ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2240ba5d6ab6SStefan Hajnoczi state->ops = ops; 2241ba5d6ab6SStefan Hajnoczi state->action = dev_info; 224294d16a64SJohn Snow state->block_job_txn = block_job_txn; 224394d16a64SJohn Snow state->txn_props = props; 2244ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22458802d1fdSJeff Cody 2246ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 224784d18f06SMarkus Armbruster if (local_err) { 22489b9877eeSWenchao Xia error_propagate(errp, local_err); 22499b9877eeSWenchao Xia goto delete_and_fail; 22509b9877eeSWenchao Xia } 225152e7c241SPaolo Bonzini } 22528802d1fdSJeff Cody 2253ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2254f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2255ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22568802d1fdSJeff Cody } 2257f9ea81e8SStefan Hajnoczi } 22588802d1fdSJeff Cody 22598802d1fdSJeff Cody /* success */ 22608802d1fdSJeff Cody goto exit; 22618802d1fdSJeff Cody 22628802d1fdSJeff Cody delete_and_fail: 2263b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2264ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2265ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2266ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2267ba0c86a3SWenchao Xia } 22688802d1fdSJeff Cody } 22698802d1fdSJeff Cody exit: 2270ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2271ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2272ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2273ba0c86a3SWenchao Xia } 2274ba5d6ab6SStefan Hajnoczi g_free(state); 22758802d1fdSJeff Cody } 227694d16a64SJohn Snow if (!has_props) { 227794d16a64SJohn Snow qapi_free_TransactionProperties(props); 227894d16a64SJohn Snow } 227994d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22808802d1fdSJeff Cody } 22818802d1fdSJeff Cody 2282c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 2283666daa68SMarkus Armbruster { 228438f54bd1SMax Reitz Error *local_err = NULL; 22853a3086b7SJohn Snow int rc; 2286666daa68SMarkus Armbruster 22873a3086b7SJohn Snow if (!has_force) { 22883a3086b7SJohn Snow force = false; 22893a3086b7SJohn Snow } 22903a3086b7SJohn Snow 22913a3086b7SJohn Snow rc = do_open_tray(device, force, &local_err); 2292bf18bee5SColin Lord if (rc && rc != -ENOSYS) { 229338f54bd1SMax Reitz error_propagate(errp, local_err); 2294c245b6a3SLuiz Capitulino return; 2295666daa68SMarkus Armbruster } 2296bf18bee5SColin Lord error_free(local_err); 22973a3086b7SJohn Snow 22986e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, errp); 2299666daa68SMarkus Armbruster } 2300666daa68SMarkus Armbruster 230112d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 230212d3ba82SBenoît Canet bool has_node_name, const char *node_name, 230312d3ba82SBenoît Canet const char *password, Error **errp) 2304666daa68SMarkus Armbruster { 230512d3ba82SBenoît Canet Error *local_err = NULL; 2306666daa68SMarkus Armbruster BlockDriverState *bs; 2307e3442099SStefan Hajnoczi AioContext *aio_context; 2308666daa68SMarkus Armbruster 230912d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 231012d3ba82SBenoît Canet has_node_name ? node_name : NULL, 231112d3ba82SBenoît Canet &local_err); 231284d18f06SMarkus Armbruster if (local_err) { 231312d3ba82SBenoît Canet error_propagate(errp, local_err); 2314a4dea8a9SLuiz Capitulino return; 2315666daa68SMarkus Armbruster } 2316666daa68SMarkus Armbruster 2317e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2318e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2319e3442099SStefan Hajnoczi 23204d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2321666daa68SMarkus Armbruster 2322e3442099SStefan Hajnoczi aio_context_release(aio_context); 2323e3442099SStefan Hajnoczi } 2324e3442099SStefan Hajnoczi 2325bf18bee5SColin Lord /* 2326bf18bee5SColin Lord * Attempt to open the tray of @device. 2327bf18bee5SColin Lord * If @force, ignore its tray lock. 2328bf18bee5SColin Lord * Else, if the tray is locked, don't open it, but ask the guest to open it. 2329bf18bee5SColin Lord * On error, store an error through @errp and return -errno. 2330bf18bee5SColin Lord * If @device does not exist, return -ENODEV. 2331bf18bee5SColin Lord * If it has no removable media, return -ENOTSUP. 2332bf18bee5SColin Lord * If it has no tray, return -ENOSYS. 2333bf18bee5SColin Lord * If the guest was asked to open the tray, return -EINPROGRESS. 2334bf18bee5SColin Lord * Else, return 0. 23353a3086b7SJohn Snow */ 23363a3086b7SJohn Snow static int do_open_tray(const char *device, bool force, Error **errp) 23377d8a9f71SMax Reitz { 23387d8a9f71SMax Reitz BlockBackend *blk; 23397d8a9f71SMax Reitz bool locked; 23407d8a9f71SMax Reitz 23417d8a9f71SMax Reitz blk = blk_by_name(device); 23427d8a9f71SMax Reitz if (!blk) { 23437d8a9f71SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 23447d8a9f71SMax Reitz "Device '%s' not found", device); 23453a3086b7SJohn Snow return -ENODEV; 23467d8a9f71SMax Reitz } 23477d8a9f71SMax Reitz 23487d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23497d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23503a3086b7SJohn Snow return -ENOTSUP; 23517d8a9f71SMax Reitz } 23527d8a9f71SMax Reitz 235312c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 2354bf18bee5SColin Lord error_setg(errp, "Device '%s' does not have a tray", device); 2355bf18bee5SColin Lord return -ENOSYS; 235612c7ec87SMax Reitz } 235712c7ec87SMax Reitz 23587d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23593a3086b7SJohn Snow return 0; 23607d8a9f71SMax Reitz } 23617d8a9f71SMax Reitz 23627d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23637d8a9f71SMax Reitz if (locked) { 23647d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23657d8a9f71SMax Reitz } 23667d8a9f71SMax Reitz 23677d8a9f71SMax Reitz if (!locked || force) { 23687d8a9f71SMax Reitz blk_dev_change_media_cb(blk, false); 23697d8a9f71SMax Reitz } 23703a3086b7SJohn Snow 23713a3086b7SJohn Snow if (locked && !force) { 2372bf18bee5SColin Lord error_setg(errp, "Device '%s' is locked and force was not specified, " 2373bf18bee5SColin Lord "wait for tray to open and try again", device); 2374bf18bee5SColin Lord return -EINPROGRESS; 23753a3086b7SJohn Snow } 23763a3086b7SJohn Snow 23773a3086b7SJohn Snow return 0; 23783a3086b7SJohn Snow } 23793a3086b7SJohn Snow 23803a3086b7SJohn Snow void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 23813a3086b7SJohn Snow Error **errp) 23823a3086b7SJohn Snow { 2383bf18bee5SColin Lord Error *local_err = NULL; 2384bf18bee5SColin Lord int rc; 2385bf18bee5SColin Lord 23863a3086b7SJohn Snow if (!has_force) { 23873a3086b7SJohn Snow force = false; 23883a3086b7SJohn Snow } 2389bf18bee5SColin Lord rc = do_open_tray(device, force, &local_err); 2390bf18bee5SColin Lord if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { 2391bf18bee5SColin Lord error_propagate(errp, local_err); 2392bf18bee5SColin Lord return; 2393bf18bee5SColin Lord } 2394bf18bee5SColin Lord error_free(local_err); 23957d8a9f71SMax Reitz } 23967d8a9f71SMax Reitz 2397abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2398abaaf59dSMax Reitz { 2399abaaf59dSMax Reitz BlockBackend *blk; 2400abaaf59dSMax Reitz 2401abaaf59dSMax Reitz blk = blk_by_name(device); 2402abaaf59dSMax Reitz if (!blk) { 2403abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2404abaaf59dSMax Reitz "Device '%s' not found", device); 2405abaaf59dSMax Reitz return; 2406abaaf59dSMax Reitz } 2407abaaf59dSMax Reitz 2408abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2409abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2410abaaf59dSMax Reitz return; 2411abaaf59dSMax Reitz } 2412abaaf59dSMax Reitz 241312c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 241412c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 241512c7ec87SMax Reitz return; 241612c7ec87SMax Reitz } 241712c7ec87SMax Reitz 2418abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2419abaaf59dSMax Reitz return; 2420abaaf59dSMax Reitz } 2421abaaf59dSMax Reitz 2422abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2423abaaf59dSMax Reitz } 2424abaaf59dSMax Reitz 24256e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 24262814f672SMax Reitz { 24272814f672SMax Reitz BlockBackend *blk; 24282814f672SMax Reitz BlockDriverState *bs; 24292814f672SMax Reitz AioContext *aio_context; 24302814f672SMax Reitz bool has_device; 24312814f672SMax Reitz 24322814f672SMax Reitz blk = blk_by_name(device); 24332814f672SMax Reitz if (!blk) { 24342814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 24352814f672SMax Reitz "Device '%s' not found", device); 24362814f672SMax Reitz return; 24372814f672SMax Reitz } 24382814f672SMax Reitz 24392814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 24402814f672SMax Reitz has_device = blk_get_attached_dev(blk); 24412814f672SMax Reitz 24422814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 24432814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 24442814f672SMax Reitz return; 24452814f672SMax Reitz } 24462814f672SMax Reitz 244712c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 24482814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 24492814f672SMax Reitz return; 24502814f672SMax Reitz } 24512814f672SMax Reitz 24522814f672SMax Reitz bs = blk_bs(blk); 24532814f672SMax Reitz if (!bs) { 24542814f672SMax Reitz return; 24552814f672SMax Reitz } 24562814f672SMax Reitz 24572814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 24582814f672SMax Reitz aio_context_acquire(aio_context); 24592814f672SMax Reitz 24602814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 24612814f672SMax Reitz goto out; 24622814f672SMax Reitz } 24632814f672SMax Reitz 24642814f672SMax Reitz blk_remove_bs(blk); 24652814f672SMax Reitz 246612c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 246712c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 246812c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 246912c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 247012c7ec87SMax Reitz * value passed here (i.e. false). */ 247112c7ec87SMax Reitz blk_dev_change_media_cb(blk, false); 247212c7ec87SMax Reitz } 247312c7ec87SMax Reitz 24742814f672SMax Reitz out: 24752814f672SMax Reitz aio_context_release(aio_context); 24762814f672SMax Reitz } 24772814f672SMax Reitz 2478d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device, 2479d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2480d1299882SMax Reitz { 2481d1299882SMax Reitz BlockBackend *blk; 2482d1299882SMax Reitz bool has_device; 2483d1299882SMax Reitz 2484d1299882SMax Reitz blk = blk_by_name(device); 2485d1299882SMax Reitz if (!blk) { 2486d1299882SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2487d1299882SMax Reitz "Device '%s' not found", device); 2488d1299882SMax Reitz return; 2489d1299882SMax Reitz } 2490d1299882SMax Reitz 2491d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2492d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2493d1299882SMax Reitz 2494d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2495d1299882SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2496d1299882SMax Reitz return; 2497d1299882SMax Reitz } 2498d1299882SMax Reitz 249912c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2500d1299882SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2501d1299882SMax Reitz return; 2502d1299882SMax Reitz } 2503d1299882SMax Reitz 2504d1299882SMax Reitz if (blk_bs(blk)) { 2505d1299882SMax Reitz error_setg(errp, "There already is a medium in device '%s'", device); 2506d1299882SMax Reitz return; 2507d1299882SMax Reitz } 2508d1299882SMax Reitz 2509d1299882SMax Reitz blk_insert_bs(blk, bs); 2510d1299882SMax Reitz 251112c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 251212c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 251312c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 251412c7ec87SMax Reitz * slot here. 251512c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 251612c7ec87SMax Reitz * value passed here (i.e. true). */ 251712c7ec87SMax Reitz blk_dev_change_media_cb(blk, true); 251812c7ec87SMax Reitz } 2519d1299882SMax Reitz } 2520d1299882SMax Reitz 25216e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2522d1299882SMax Reitz Error **errp) 2523d1299882SMax Reitz { 2524d1299882SMax Reitz BlockDriverState *bs; 2525d1299882SMax Reitz 2526d1299882SMax Reitz bs = bdrv_find_node(node_name); 2527d1299882SMax Reitz if (!bs) { 2528d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2529d1299882SMax Reitz return; 2530d1299882SMax Reitz } 2531d1299882SMax Reitz 25321f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2533d1299882SMax Reitz error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 25341f0c461bSKevin Wolf bdrv_get_parent_name(bs)); 2535d1299882SMax Reitz return; 2536d1299882SMax Reitz } 2537d1299882SMax Reitz 2538d1299882SMax Reitz qmp_blockdev_insert_anon_medium(device, bs, errp); 2539d1299882SMax Reitz } 2540d1299882SMax Reitz 254124fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename, 254224fb4133SMax Reitz bool has_format, const char *format, 254339ff43d9SMax Reitz bool has_read_only, 254439ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 254524fb4133SMax Reitz Error **errp) 2546de2c6c05SMax Reitz { 2547de2c6c05SMax Reitz BlockBackend *blk; 2548de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 25495b363937SMax Reitz int bdrv_flags; 255038a53d50SColin Lord int rc; 2551de2c6c05SMax Reitz QDict *options = NULL; 2552de2c6c05SMax Reitz Error *err = NULL; 2553de2c6c05SMax Reitz 2554de2c6c05SMax Reitz blk = blk_by_name(device); 2555de2c6c05SMax Reitz if (!blk) { 2556de2c6c05SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2557de2c6c05SMax Reitz "Device '%s' not found", device); 2558de2c6c05SMax Reitz goto fail; 2559de2c6c05SMax Reitz } 2560de2c6c05SMax Reitz 2561de2c6c05SMax Reitz if (blk_bs(blk)) { 2562de2c6c05SMax Reitz blk_update_root_state(blk); 2563de2c6c05SMax Reitz } 2564de2c6c05SMax Reitz 2565de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2566156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2567156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2568de2c6c05SMax Reitz 256939ff43d9SMax Reitz if (!has_read_only) { 257039ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 257139ff43d9SMax Reitz } 257239ff43d9SMax Reitz 257339ff43d9SMax Reitz switch (read_only) { 257439ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 257539ff43d9SMax Reitz break; 257639ff43d9SMax Reitz 257739ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 257839ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 257939ff43d9SMax Reitz break; 258039ff43d9SMax Reitz 258139ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 258239ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 258339ff43d9SMax Reitz break; 258439ff43d9SMax Reitz 258539ff43d9SMax Reitz default: 258639ff43d9SMax Reitz abort(); 258739ff43d9SMax Reitz } 258839ff43d9SMax Reitz 258924fb4133SMax Reitz if (has_format) { 2590de2c6c05SMax Reitz options = qdict_new(); 2591de2c6c05SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2592de2c6c05SMax Reitz } 2593de2c6c05SMax Reitz 25945b363937SMax Reitz medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 25955b363937SMax Reitz if (!medium_bs) { 2596de2c6c05SMax Reitz goto fail; 2597de2c6c05SMax Reitz } 2598de2c6c05SMax Reitz 2599de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2600de2c6c05SMax Reitz if (err) { 2601de2c6c05SMax Reitz error_propagate(errp, err); 2602de2c6c05SMax Reitz goto fail; 2603de2c6c05SMax Reitz } 2604de2c6c05SMax Reitz 260538a53d50SColin Lord rc = do_open_tray(device, false, &err); 260638a53d50SColin Lord if (rc && rc != -ENOSYS) { 2607de2c6c05SMax Reitz error_propagate(errp, err); 2608de2c6c05SMax Reitz goto fail; 2609de2c6c05SMax Reitz } 261038a53d50SColin Lord error_free(err); 261138a53d50SColin Lord err = NULL; 2612de2c6c05SMax Reitz 26136e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, &err); 2614de2c6c05SMax Reitz if (err) { 2615de2c6c05SMax Reitz error_propagate(errp, err); 2616de2c6c05SMax Reitz goto fail; 2617de2c6c05SMax Reitz } 2618de2c6c05SMax Reitz 2619de2c6c05SMax Reitz qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2620de2c6c05SMax Reitz if (err) { 2621de2c6c05SMax Reitz error_propagate(errp, err); 2622de2c6c05SMax Reitz goto fail; 2623de2c6c05SMax Reitz } 2624de2c6c05SMax Reitz 2625a5614993SKevin Wolf blk_apply_root_state(blk, medium_bs); 2626a5614993SKevin Wolf 2627de2c6c05SMax Reitz qmp_blockdev_close_tray(device, errp); 2628de2c6c05SMax Reitz 2629de2c6c05SMax Reitz fail: 2630de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2631de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2632de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2633de2c6c05SMax Reitz bdrv_unref(medium_bs); 2634de2c6c05SMax Reitz } 2635de2c6c05SMax Reitz 2636727f005eSZhi Yong Wu /* throttling disk I/O limits */ 263780047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 26383e9fab69SBenoît Canet int64_t bps_wr, 26393e9fab69SBenoît Canet int64_t iops, 26403e9fab69SBenoît Canet int64_t iops_rd, 26413e9fab69SBenoît Canet int64_t iops_wr, 26423e9fab69SBenoît Canet bool has_bps_max, 26433e9fab69SBenoît Canet int64_t bps_max, 26443e9fab69SBenoît Canet bool has_bps_rd_max, 26453e9fab69SBenoît Canet int64_t bps_rd_max, 26463e9fab69SBenoît Canet bool has_bps_wr_max, 26473e9fab69SBenoît Canet int64_t bps_wr_max, 26483e9fab69SBenoît Canet bool has_iops_max, 26493e9fab69SBenoît Canet int64_t iops_max, 26503e9fab69SBenoît Canet bool has_iops_rd_max, 26513e9fab69SBenoît Canet int64_t iops_rd_max, 26523e9fab69SBenoît Canet bool has_iops_wr_max, 26532024c1dfSBenoît Canet int64_t iops_wr_max, 2654dce13204SAlberto Garcia bool has_bps_max_length, 2655dce13204SAlberto Garcia int64_t bps_max_length, 2656dce13204SAlberto Garcia bool has_bps_rd_max_length, 2657dce13204SAlberto Garcia int64_t bps_rd_max_length, 2658dce13204SAlberto Garcia bool has_bps_wr_max_length, 2659dce13204SAlberto Garcia int64_t bps_wr_max_length, 2660dce13204SAlberto Garcia bool has_iops_max_length, 2661dce13204SAlberto Garcia int64_t iops_max_length, 2662dce13204SAlberto Garcia bool has_iops_rd_max_length, 2663dce13204SAlberto Garcia int64_t iops_rd_max_length, 2664dce13204SAlberto Garcia bool has_iops_wr_max_length, 2665dce13204SAlberto Garcia int64_t iops_wr_max_length, 26662024c1dfSBenoît Canet bool has_iops_size, 266776f4afb4SAlberto Garcia int64_t iops_size, 266876f4afb4SAlberto Garcia bool has_group, 266976f4afb4SAlberto Garcia const char *group, Error **errp) 2670727f005eSZhi Yong Wu { 2671cc0681c4SBenoît Canet ThrottleConfig cfg; 2672727f005eSZhi Yong Wu BlockDriverState *bs; 2673a0e8544cSFam Zheng BlockBackend *blk; 2674b15446fdSStefan Hajnoczi AioContext *aio_context; 2675727f005eSZhi Yong Wu 2676a0e8544cSFam Zheng blk = blk_by_name(device); 2677a0e8544cSFam Zheng if (!blk) { 267875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 267975158ebbSMarkus Armbruster "Device '%s' not found", device); 268080047da5SLuiz Capitulino return; 2681727f005eSZhi Yong Wu } 26825433c24fSMax Reitz 26835433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 26845433c24fSMax Reitz aio_context_acquire(aio_context); 26855433c24fSMax Reitz 2686a0e8544cSFam Zheng bs = blk_bs(blk); 26875433c24fSMax Reitz if (!bs) { 26885433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 26895433c24fSMax Reitz goto out; 26905433c24fSMax Reitz } 2691727f005eSZhi Yong Wu 26921588ab5dSAlberto Garcia throttle_config_init(&cfg); 2693cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2694cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2695cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2696727f005eSZhi Yong Wu 2697cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2698cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2699cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2700cc0681c4SBenoît Canet 27013e9fab69SBenoît Canet if (has_bps_max) { 27023e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 27033e9fab69SBenoît Canet } 27043e9fab69SBenoît Canet if (has_bps_rd_max) { 27053e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 27063e9fab69SBenoît Canet } 27073e9fab69SBenoît Canet if (has_bps_wr_max) { 27083e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 27093e9fab69SBenoît Canet } 27103e9fab69SBenoît Canet if (has_iops_max) { 27113e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 27123e9fab69SBenoît Canet } 27133e9fab69SBenoît Canet if (has_iops_rd_max) { 27143e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 27153e9fab69SBenoît Canet } 27163e9fab69SBenoît Canet if (has_iops_wr_max) { 27173e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 27183e9fab69SBenoît Canet } 2719cc0681c4SBenoît Canet 2720dce13204SAlberto Garcia if (has_bps_max_length) { 2721dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = bps_max_length; 2722dce13204SAlberto Garcia } 2723dce13204SAlberto Garcia if (has_bps_rd_max_length) { 2724dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_READ].burst_length = bps_rd_max_length; 2725dce13204SAlberto Garcia } 2726dce13204SAlberto Garcia if (has_bps_wr_max_length) { 2727dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_WRITE].burst_length = bps_wr_max_length; 2728dce13204SAlberto Garcia } 2729dce13204SAlberto Garcia if (has_iops_max_length) { 2730dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = iops_max_length; 2731dce13204SAlberto Garcia } 2732dce13204SAlberto Garcia if (has_iops_rd_max_length) { 2733dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_READ].burst_length = iops_rd_max_length; 2734dce13204SAlberto Garcia } 2735dce13204SAlberto Garcia if (has_iops_wr_max_length) { 2736dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_WRITE].burst_length = iops_wr_max_length; 2737dce13204SAlberto Garcia } 2738dce13204SAlberto Garcia 27392024c1dfSBenoît Canet if (has_iops_size) { 27402024c1dfSBenoît Canet cfg.op_size = iops_size; 27412024c1dfSBenoît Canet } 2742cc0681c4SBenoît Canet 2743d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 27445433c24fSMax Reitz goto out; 2745727f005eSZhi Yong Wu } 2746727f005eSZhi Yong Wu 274776f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 274876f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 274976f4afb4SAlberto Garcia * just update the throttling group. */ 275097148076SKevin Wolf if (!blk_get_public(blk)->throttle_state) { 275197148076SKevin Wolf blk_io_limits_enable(blk, has_group ? group : device); 275276f4afb4SAlberto Garcia } else if (has_group) { 275397148076SKevin Wolf blk_io_limits_update_group(blk, group); 2754727f005eSZhi Yong Wu } 275576f4afb4SAlberto Garcia /* Set the new throttling configuration */ 275697148076SKevin Wolf blk_set_io_limits(blk, &cfg); 275797148076SKevin Wolf } else if (blk_get_public(blk)->throttle_state) { 275876f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 275997148076SKevin Wolf blk_io_limits_disable(blk); 2760727f005eSZhi Yong Wu } 2761b15446fdSStefan Hajnoczi 27625433c24fSMax Reitz out: 2763b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2764727f005eSZhi Yong Wu } 2765727f005eSZhi Yong Wu 2766341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2767341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2768341ebc2fSJohn Snow Error **errp) 2769341ebc2fSJohn Snow { 2770341ebc2fSJohn Snow AioContext *aio_context; 2771341ebc2fSJohn Snow BlockDriverState *bs; 2772341ebc2fSJohn Snow 2773341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2774341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2775341ebc2fSJohn Snow return; 2776341ebc2fSJohn Snow } 2777341ebc2fSJohn Snow 2778341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2779341ebc2fSJohn Snow if (!bs) { 2780341ebc2fSJohn Snow return; 2781341ebc2fSJohn Snow } 2782341ebc2fSJohn Snow 2783341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2784341ebc2fSJohn Snow aio_context_acquire(aio_context); 2785341ebc2fSJohn Snow 2786341ebc2fSJohn Snow if (has_granularity) { 2787341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2788341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2789341ebc2fSJohn Snow "and at least 512"); 2790341ebc2fSJohn Snow goto out; 2791341ebc2fSJohn Snow } 2792341ebc2fSJohn Snow } else { 2793341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2794341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2795341ebc2fSJohn Snow } 2796341ebc2fSJohn Snow 2797341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2798341ebc2fSJohn Snow 2799341ebc2fSJohn Snow out: 2800341ebc2fSJohn Snow aio_context_release(aio_context); 2801341ebc2fSJohn Snow } 2802341ebc2fSJohn Snow 2803341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2804341ebc2fSJohn Snow Error **errp) 2805341ebc2fSJohn Snow { 2806341ebc2fSJohn Snow AioContext *aio_context; 2807341ebc2fSJohn Snow BlockDriverState *bs; 2808341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2809341ebc2fSJohn Snow 2810341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2811341ebc2fSJohn Snow if (!bitmap || !bs) { 2812341ebc2fSJohn Snow return; 2813341ebc2fSJohn Snow } 2814341ebc2fSJohn Snow 28159bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 28169bd2b08fSJohn Snow error_setg(errp, 28179bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 28189bd2b08fSJohn Snow name); 28199bd2b08fSJohn Snow goto out; 28209bd2b08fSJohn Snow } 282120dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2822341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2823341ebc2fSJohn Snow 28249bd2b08fSJohn Snow out: 2825341ebc2fSJohn Snow aio_context_release(aio_context); 2826341ebc2fSJohn Snow } 2827341ebc2fSJohn Snow 2828e74e6b78SJohn Snow /** 2829e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2830e74e6b78SJohn Snow * immediately after a full backup operation. 2831e74e6b78SJohn Snow */ 2832e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2833e74e6b78SJohn Snow Error **errp) 2834e74e6b78SJohn Snow { 2835e74e6b78SJohn Snow AioContext *aio_context; 2836e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2837e74e6b78SJohn Snow BlockDriverState *bs; 2838e74e6b78SJohn Snow 2839e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2840e74e6b78SJohn Snow if (!bitmap || !bs) { 2841e74e6b78SJohn Snow return; 2842e74e6b78SJohn Snow } 2843e74e6b78SJohn Snow 2844e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2845e74e6b78SJohn Snow error_setg(errp, 2846e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2847e74e6b78SJohn Snow name); 2848e74e6b78SJohn Snow goto out; 2849e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2850e74e6b78SJohn Snow error_setg(errp, 2851e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2852e74e6b78SJohn Snow name); 2853e74e6b78SJohn Snow goto out; 2854e74e6b78SJohn Snow } 2855e74e6b78SJohn Snow 2856df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2857e74e6b78SJohn Snow 2858e74e6b78SJohn Snow out: 2859e74e6b78SJohn Snow aio_context_release(aio_context); 2860e74e6b78SJohn Snow } 2861e74e6b78SJohn Snow 2862072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 28639063f814SRyan Harper { 28649063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 28657e7d56d9SMarkus Armbruster BlockBackend *blk; 28669063f814SRyan Harper BlockDriverState *bs; 28678ad4202bSStefan Hajnoczi AioContext *aio_context; 28683718d8abSFam Zheng Error *local_err = NULL; 28699063f814SRyan Harper 28702073d410SKevin Wolf bs = bdrv_find_node(id); 28712073d410SKevin Wolf if (bs) { 28722073d410SKevin Wolf qmp_x_blockdev_del(false, NULL, true, id, &local_err); 28732073d410SKevin Wolf if (local_err) { 28742073d410SKevin Wolf error_report_err(local_err); 28752073d410SKevin Wolf } 28762073d410SKevin Wolf return; 28772073d410SKevin Wolf } 28782073d410SKevin Wolf 28797e7d56d9SMarkus Armbruster blk = blk_by_name(id); 28807e7d56d9SMarkus Armbruster if (!blk) { 2881b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2882072ebe6bSMarkus Armbruster return; 28839063f814SRyan Harper } 28848ad4202bSStefan Hajnoczi 288526f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 288648f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 288748f364ddSMarkus Armbruster " is not supported"); 2888072ebe6bSMarkus Armbruster return; 288948f364ddSMarkus Armbruster } 289048f364ddSMarkus Armbruster 28915433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 28928ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 28938ad4202bSStefan Hajnoczi 28945433c24fSMax Reitz bs = blk_bs(blk); 28955433c24fSMax Reitz if (bs) { 28963718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2897565f65d2SMarkus Armbruster error_report_err(local_err); 28988ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2899072ebe6bSMarkus Armbruster return; 29008591675fSMarcelo Tosatti } 29019063f814SRyan Harper 2902938abd43SMax Reitz blk_remove_bs(blk); 29035433c24fSMax Reitz } 29049063f814SRyan Harper 29057c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 2906efaa7c4eSMax Reitz monitor_remove_blk(blk); 2907efaa7c4eSMax Reitz 29087c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 29097c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 2910d22b2f41SRyan Harper */ 2911a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 2912293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2913373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2914293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2915d22b2f41SRyan Harper } else { 2916b9fe8a7aSMarkus Armbruster blk_unref(blk); 2917d22b2f41SRyan Harper } 29189063f814SRyan Harper 29198ad4202bSStefan Hajnoczi aio_context_release(aio_context); 29209063f814SRyan Harper } 29216d4a2b3aSChristoph Hellwig 29223b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 29233b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 29243b1dbd11SBenoît Canet int64_t size, Error **errp) 29256d4a2b3aSChristoph Hellwig { 29263b1dbd11SBenoît Canet Error *local_err = NULL; 29276d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2928927e0e76SStefan Hajnoczi AioContext *aio_context; 29298732901eSKevin Wolf int ret; 29306d4a2b3aSChristoph Hellwig 29313b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 29323b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 29333b1dbd11SBenoît Canet &local_err); 293484d18f06SMarkus Armbruster if (local_err) { 29353b1dbd11SBenoît Canet error_propagate(errp, local_err); 29363b1dbd11SBenoît Canet return; 29373b1dbd11SBenoît Canet } 29383b1dbd11SBenoît Canet 2939927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2940927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2941927e0e76SStefan Hajnoczi 29423b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2943c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2944927e0e76SStefan Hajnoczi goto out; 29456d4a2b3aSChristoph Hellwig } 29466d4a2b3aSChristoph Hellwig 29476d4a2b3aSChristoph Hellwig if (size < 0) { 2948c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2949927e0e76SStefan Hajnoczi goto out; 29506d4a2b3aSChristoph Hellwig } 29516d4a2b3aSChristoph Hellwig 29529c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2953c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2954927e0e76SStefan Hajnoczi goto out; 29559c75e168SJeff Cody } 29569c75e168SJeff Cody 295792b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 295892b7a08dSPeter Lieven bdrv_drain_all(); 295992b7a08dSPeter Lieven 29608732901eSKevin Wolf ret = bdrv_truncate(bs, size); 29618732901eSKevin Wolf switch (ret) { 2962939a1cc3SStefan Hajnoczi case 0: 2963939a1cc3SStefan Hajnoczi break; 2964939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2965c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2966939a1cc3SStefan Hajnoczi break; 2967939a1cc3SStefan Hajnoczi case -ENOTSUP: 2968c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2969939a1cc3SStefan Hajnoczi break; 2970939a1cc3SStefan Hajnoczi case -EACCES: 297181e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2972939a1cc3SStefan Hajnoczi break; 2973939a1cc3SStefan Hajnoczi case -EBUSY: 2974c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2975939a1cc3SStefan Hajnoczi break; 2976939a1cc3SStefan Hajnoczi default: 29778732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2978939a1cc3SStefan Hajnoczi break; 29796d4a2b3aSChristoph Hellwig } 2980927e0e76SStefan Hajnoczi 2981927e0e76SStefan Hajnoczi out: 2982927e0e76SStefan Hajnoczi aio_context_release(aio_context); 29836d4a2b3aSChristoph Hellwig } 298412bd451fSStefan Hajnoczi 29859abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 298612bd451fSStefan Hajnoczi { 2987723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2988723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2989723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2990723c5d93SStefan Hajnoczi */ 2991723c5d93SStefan Hajnoczi 299212bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2993bcada37bSWenchao Xia const char *msg = NULL; 299412bd451fSStefan Hajnoczi 29959abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 299612bd451fSStefan Hajnoczi 299712bd451fSStefan Hajnoczi assert(bs->job); 2998bcada37bSWenchao Xia 299912bd451fSStefan Hajnoczi if (ret < 0) { 3000bcada37bSWenchao Xia msg = strerror(-ret); 300112bd451fSStefan Hajnoczi } 300212bd451fSStefan Hajnoczi 3003370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 3004bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 3005370521a1SStefan Hajnoczi } else { 3006bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 3007370521a1SStefan Hajnoczi } 300812bd451fSStefan Hajnoczi } 300912bd451fSStefan Hajnoczi 30102323322eSAlberto Garcia void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, 301113d8cc51SJeff Cody bool has_base, const char *base, 301213d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 301313d8cc51SJeff Cody bool has_speed, int64_t speed, 30141d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 30151d809098SPaolo Bonzini Error **errp) 301612bd451fSStefan Hajnoczi { 3017a0e8544cSFam Zheng BlockBackend *blk; 301812bd451fSStefan Hajnoczi BlockDriverState *bs; 3019c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 3020f3e69bebSStefan Hajnoczi AioContext *aio_context; 3021fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 302213d8cc51SJeff Cody const char *base_name = NULL; 302312bd451fSStefan Hajnoczi 30241d809098SPaolo Bonzini if (!has_on_error) { 30251d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 30261d809098SPaolo Bonzini } 30271d809098SPaolo Bonzini 3028a0e8544cSFam Zheng blk = blk_by_name(device); 3029a0e8544cSFam Zheng if (!blk) { 303075158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 303175158ebbSMarkus Armbruster "Device '%s' not found", device); 303212bd451fSStefan Hajnoczi return; 303312bd451fSStefan Hajnoczi } 303412bd451fSStefan Hajnoczi 30355433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3036f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 3037f3e69bebSStefan Hajnoczi 30385433c24fSMax Reitz if (!blk_is_available(blk)) { 30395433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 30405433c24fSMax Reitz goto out; 30415433c24fSMax Reitz } 30425433c24fSMax Reitz bs = blk_bs(blk); 30435433c24fSMax Reitz 3044628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 3045f3e69bebSStefan Hajnoczi goto out; 3046628ff683SFam Zheng } 3047628ff683SFam Zheng 304813d8cc51SJeff Cody if (has_base) { 3049c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3050c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3051c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3052f3e69bebSStefan Hajnoczi goto out; 305312bd451fSStefan Hajnoczi } 3054f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 305513d8cc51SJeff Cody base_name = base; 3056c8c3080fSMarcelo Tosatti } 305712bd451fSStefan Hajnoczi 305813d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 305913d8cc51SJeff Cody * file, and specifying one is therefore an error */ 306013d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 306113d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 306213d8cc51SJeff Cody "entire chain"); 3063f3e69bebSStefan Hajnoczi goto out; 306413d8cc51SJeff Cody } 306513d8cc51SJeff Cody 306613d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 306713d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 306813d8cc51SJeff Cody 30692323322eSAlberto Garcia stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, 30702323322eSAlberto Garcia has_speed ? speed : 0, on_error, block_job_cb, bs, &local_err); 307184d18f06SMarkus Armbruster if (local_err) { 3072fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3073f3e69bebSStefan Hajnoczi goto out; 307412bd451fSStefan Hajnoczi } 307512bd451fSStefan Hajnoczi 307612bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3077f3e69bebSStefan Hajnoczi 3078f3e69bebSStefan Hajnoczi out: 3079f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 308012bd451fSStefan Hajnoczi } 30812d47c6e9SStefan Hajnoczi 3082fd62c609SAlberto Garcia void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, 30837676e2c5SJeff Cody bool has_base, const char *base, 30847676e2c5SJeff Cody bool has_top, const char *top, 308554e26900SJeff Cody bool has_backing_file, const char *backing_file, 3086ed61fc10SJeff Cody bool has_speed, int64_t speed, 3087ed61fc10SJeff Cody Error **errp) 3088ed61fc10SJeff Cody { 3089a0e8544cSFam Zheng BlockBackend *blk; 3090ed61fc10SJeff Cody BlockDriverState *bs; 3091ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 30929e85cd5cSStefan Hajnoczi AioContext *aio_context; 3093ed61fc10SJeff Cody Error *local_err = NULL; 3094ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3095ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3096ed61fc10SJeff Cody */ 309792aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3098ed61fc10SJeff Cody 309954504663SMax Reitz if (!has_speed) { 310054504663SMax Reitz speed = 0; 310154504663SMax Reitz } 310254504663SMax Reitz 31037676e2c5SJeff Cody /* Important Note: 31047676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 31057676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 31067676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 31077676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 3108a0e8544cSFam Zheng blk = blk_by_name(device); 3109a0e8544cSFam Zheng if (!blk) { 311075158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 311175158ebbSMarkus Armbruster "Device '%s' not found", device); 3112ed61fc10SJeff Cody return; 3113ed61fc10SJeff Cody } 3114ed61fc10SJeff Cody 31155433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 31169e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 31179e85cd5cSStefan Hajnoczi 31185433c24fSMax Reitz if (!blk_is_available(blk)) { 31195433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 31205433c24fSMax Reitz goto out; 31215433c24fSMax Reitz } 31225433c24fSMax Reitz bs = blk_bs(blk); 31235433c24fSMax Reitz 3124bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 31259e85cd5cSStefan Hajnoczi goto out; 3126628ff683SFam Zheng } 3127628ff683SFam Zheng 3128ed61fc10SJeff Cody /* default top_bs is the active layer */ 3129ed61fc10SJeff Cody top_bs = bs; 3130ed61fc10SJeff Cody 31317676e2c5SJeff Cody if (has_top && top) { 3132ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3133ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3134ed61fc10SJeff Cody } 3135ed61fc10SJeff Cody } 3136ed61fc10SJeff Cody 3137ed61fc10SJeff Cody if (top_bs == NULL) { 3138ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 31399e85cd5cSStefan Hajnoczi goto out; 3140ed61fc10SJeff Cody } 3141ed61fc10SJeff Cody 31429e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 31439e85cd5cSStefan Hajnoczi 3144d5208c45SJeff Cody if (has_base && base) { 3145d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3146d5208c45SJeff Cody } else { 3147d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3148d5208c45SJeff Cody } 3149d5208c45SJeff Cody 3150d5208c45SJeff Cody if (base_bs == NULL) { 3151c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 31529e85cd5cSStefan Hajnoczi goto out; 3153d5208c45SJeff Cody } 3154d5208c45SJeff Cody 31559e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 31569e85cd5cSStefan Hajnoczi 3157bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3158bb00021dSFam Zheng goto out; 3159bb00021dSFam Zheng } 3160bb00021dSFam Zheng 31617676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 31627676e2c5SJeff Cody if (top_bs == base_bs) { 31637676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 31649e85cd5cSStefan Hajnoczi goto out; 31657676e2c5SJeff Cody } 31667676e2c5SJeff Cody 316720a63d2cSFam Zheng if (top_bs == bs) { 316854e26900SJeff Cody if (has_backing_file) { 316954e26900SJeff Cody error_setg(errp, "'backing-file' specified," 317054e26900SJeff Cody " but 'top' is the active layer"); 31719e85cd5cSStefan Hajnoczi goto out; 317254e26900SJeff Cody } 3173fd62c609SAlberto Garcia commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, speed, 3174fd62c609SAlberto Garcia on_error, block_job_cb, bs, &local_err); 317520a63d2cSFam Zheng } else { 3176fd62c609SAlberto Garcia commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed, 3177fd62c609SAlberto Garcia on_error, block_job_cb, bs, 317854e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 317920a63d2cSFam Zheng } 3180ed61fc10SJeff Cody if (local_err != NULL) { 3181ed61fc10SJeff Cody error_propagate(errp, local_err); 31829e85cd5cSStefan Hajnoczi goto out; 3183ed61fc10SJeff Cody } 31849e85cd5cSStefan Hajnoczi 31859e85cd5cSStefan Hajnoczi out: 31869e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3187ed61fc10SJeff Cody } 3188ed61fc10SJeff Cody 318970559d49SAlberto Garcia static void do_drive_backup(const char *job_id, const char *device, 319070559d49SAlberto Garcia const char *target, bool has_format, 319170559d49SAlberto Garcia const char *format, enum MirrorSyncMode sync, 319299a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 319399a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 3194d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 319578f51fdeSJohn Snow bool has_on_source_error, 319678f51fdeSJohn Snow BlockdevOnError on_source_error, 319778f51fdeSJohn Snow bool has_on_target_error, 319878f51fdeSJohn Snow BlockdevOnError on_target_error, 319978f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 320099a9addfSStefan Hajnoczi { 3201a0e8544cSFam Zheng BlockBackend *blk; 320299a9addfSStefan Hajnoczi BlockDriverState *bs; 320399a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3204fc5d3f84SIan Main BlockDriverState *source = NULL; 3205d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3206761731b1SStefan Hajnoczi AioContext *aio_context; 3207e6641719SMax Reitz QDict *options = NULL; 320899a9addfSStefan Hajnoczi Error *local_err = NULL; 320999a9addfSStefan Hajnoczi int flags; 321099a9addfSStefan Hajnoczi int64_t size; 321199a9addfSStefan Hajnoczi 321299a9addfSStefan Hajnoczi if (!has_speed) { 321399a9addfSStefan Hajnoczi speed = 0; 321499a9addfSStefan Hajnoczi } 321599a9addfSStefan Hajnoczi if (!has_on_source_error) { 321699a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 321799a9addfSStefan Hajnoczi } 321899a9addfSStefan Hajnoczi if (!has_on_target_error) { 321999a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 322099a9addfSStefan Hajnoczi } 322199a9addfSStefan Hajnoczi if (!has_mode) { 322299a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 322399a9addfSStefan Hajnoczi } 322499a9addfSStefan Hajnoczi 3225a0e8544cSFam Zheng blk = blk_by_name(device); 3226a0e8544cSFam Zheng if (!blk) { 322775158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 322875158ebbSMarkus Armbruster "Device '%s' not found", device); 322999a9addfSStefan Hajnoczi return; 323099a9addfSStefan Hajnoczi } 323199a9addfSStefan Hajnoczi 32325433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3233761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3234761731b1SStefan Hajnoczi 3235c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 3236c29c1dd3SFam Zheng * do an early check redundantly. */ 32375433c24fSMax Reitz if (!blk_is_available(blk)) { 3238c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3239761731b1SStefan Hajnoczi goto out; 324099a9addfSStefan Hajnoczi } 32415433c24fSMax Reitz bs = blk_bs(blk); 324299a9addfSStefan Hajnoczi 324399a9addfSStefan Hajnoczi if (!has_format) { 324499a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 324599a9addfSStefan Hajnoczi } 324699a9addfSStefan Hajnoczi 3247c29c1dd3SFam Zheng /* Early check to avoid creating target */ 32483718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3249761731b1SStefan Hajnoczi goto out; 325099a9addfSStefan Hajnoczi } 325199a9addfSStefan Hajnoczi 325261de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 325399a9addfSStefan Hajnoczi 3254fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3255fc5d3f84SIan Main * on top of. */ 3256fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 3257760e0063SKevin Wolf source = backing_bs(bs); 3258fc5d3f84SIan Main if (!source) { 3259fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 3260fc5d3f84SIan Main } 3261fc5d3f84SIan Main } 3262fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 3263fc5d3f84SIan Main source = bs; 3264fc5d3f84SIan Main } 3265fc5d3f84SIan Main 326699a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 326799a9addfSStefan Hajnoczi if (size < 0) { 326899a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3269761731b1SStefan Hajnoczi goto out; 327099a9addfSStefan Hajnoczi } 327199a9addfSStefan Hajnoczi 327299a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 3273e6641719SMax Reitz assert(format); 3274fc5d3f84SIan Main if (source) { 3275fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 3276fc5d3f84SIan Main source->drv->format_name, NULL, 3277fc5d3f84SIan Main size, flags, &local_err, false); 3278fc5d3f84SIan Main } else { 3279fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 3280fc5d3f84SIan Main size, flags, &local_err, false); 3281fc5d3f84SIan Main } 328299a9addfSStefan Hajnoczi } 328399a9addfSStefan Hajnoczi 328484d18f06SMarkus Armbruster if (local_err) { 328599a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3286761731b1SStefan Hajnoczi goto out; 328799a9addfSStefan Hajnoczi } 328899a9addfSStefan Hajnoczi 3289e6641719SMax Reitz if (format) { 3290e6641719SMax Reitz options = qdict_new(); 3291e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3292e6641719SMax Reitz } 3293e6641719SMax Reitz 32945b363937SMax Reitz target_bs = bdrv_open(target, NULL, options, flags, errp); 32955b363937SMax Reitz if (!target_bs) { 3296761731b1SStefan Hajnoczi goto out; 329799a9addfSStefan Hajnoczi } 329899a9addfSStefan Hajnoczi 3299761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3300761731b1SStefan Hajnoczi 3301d58d8453SJohn Snow if (has_bitmap) { 3302d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3303d58d8453SJohn Snow if (!bmap) { 3304d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 33050702d3d8SMax Reitz bdrv_unref(target_bs); 3306d58d8453SJohn Snow goto out; 3307d58d8453SJohn Snow } 3308d58d8453SJohn Snow } 3309d58d8453SJohn Snow 331070559d49SAlberto Garcia backup_start(job_id, bs, target_bs, speed, sync, bmap, 3311d58d8453SJohn Snow on_source_error, on_target_error, 331278f51fdeSJohn Snow block_job_cb, bs, txn, &local_err); 33134f6fd349SFam Zheng bdrv_unref(target_bs); 33145c438bc6SKevin Wolf if (local_err != NULL) { 331599a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3316761731b1SStefan Hajnoczi goto out; 331799a9addfSStefan Hajnoczi } 3318761731b1SStefan Hajnoczi 3319761731b1SStefan Hajnoczi out: 3320761731b1SStefan Hajnoczi aio_context_release(aio_context); 332199a9addfSStefan Hajnoczi } 332299a9addfSStefan Hajnoczi 332370559d49SAlberto Garcia void qmp_drive_backup(bool has_job_id, const char *job_id, 332470559d49SAlberto Garcia const char *device, const char *target, 332578f51fdeSJohn Snow bool has_format, const char *format, 332678f51fdeSJohn Snow enum MirrorSyncMode sync, 332778f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 332878f51fdeSJohn Snow bool has_speed, int64_t speed, 332978f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 333078f51fdeSJohn Snow bool has_on_source_error, BlockdevOnError on_source_error, 333178f51fdeSJohn Snow bool has_on_target_error, BlockdevOnError on_target_error, 333278f51fdeSJohn Snow Error **errp) 333378f51fdeSJohn Snow { 333470559d49SAlberto Garcia return do_drive_backup(has_job_id ? job_id : NULL, device, target, 333570559d49SAlberto Garcia has_format, format, sync, 333678f51fdeSJohn Snow has_mode, mode, has_speed, speed, 333778f51fdeSJohn Snow has_bitmap, bitmap, 333878f51fdeSJohn Snow has_on_source_error, on_source_error, 333978f51fdeSJohn Snow has_on_target_error, on_target_error, 334078f51fdeSJohn Snow NULL, errp); 334178f51fdeSJohn Snow } 334278f51fdeSJohn Snow 3343c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3344c13163fbSBenoît Canet { 3345d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3346c13163fbSBenoît Canet } 3347c13163fbSBenoît Canet 334870559d49SAlberto Garcia void do_blockdev_backup(const char *job_id, const char *device, 334970559d49SAlberto Garcia const char *target, enum MirrorSyncMode sync, 3350c29c1dd3SFam Zheng bool has_speed, int64_t speed, 3351c29c1dd3SFam Zheng bool has_on_source_error, 3352c29c1dd3SFam Zheng BlockdevOnError on_source_error, 3353c29c1dd3SFam Zheng bool has_on_target_error, 3354c29c1dd3SFam Zheng BlockdevOnError on_target_error, 335578f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 3356c29c1dd3SFam Zheng { 33570d978913SFam Zheng BlockBackend *blk; 3358c29c1dd3SFam Zheng BlockDriverState *bs; 3359c29c1dd3SFam Zheng BlockDriverState *target_bs; 3360c29c1dd3SFam Zheng Error *local_err = NULL; 3361c29c1dd3SFam Zheng AioContext *aio_context; 3362c29c1dd3SFam Zheng 3363c29c1dd3SFam Zheng if (!has_speed) { 3364c29c1dd3SFam Zheng speed = 0; 3365c29c1dd3SFam Zheng } 3366c29c1dd3SFam Zheng if (!has_on_source_error) { 3367c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3368c29c1dd3SFam Zheng } 3369c29c1dd3SFam Zheng if (!has_on_target_error) { 3370c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3371c29c1dd3SFam Zheng } 3372c29c1dd3SFam Zheng 3373a0e8544cSFam Zheng blk = blk_by_name(device); 3374a0e8544cSFam Zheng if (!blk) { 33755b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", device); 3376c29c1dd3SFam Zheng return; 3377c29c1dd3SFam Zheng } 3378c29c1dd3SFam Zheng 33795433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3380c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3381c29c1dd3SFam Zheng 33825433c24fSMax Reitz if (!blk_is_available(blk)) { 33835433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 33845433c24fSMax Reitz goto out; 33855433c24fSMax Reitz } 33865433c24fSMax Reitz bs = blk_bs(blk); 33875433c24fSMax Reitz 33880d978913SFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 33890d978913SFam Zheng if (!target_bs) { 3390c29c1dd3SFam Zheng goto out; 3391c29c1dd3SFam Zheng } 33925433c24fSMax Reitz 3393efd75567SFam Zheng if (bdrv_get_aio_context(target_bs) != aio_context) { 3394efd75567SFam Zheng if (!bdrv_has_blk(target_bs)) { 3395efd75567SFam Zheng /* The target BDS is not attached, we can safely move it to another 3396efd75567SFam Zheng * AioContext. */ 3397c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3398efd75567SFam Zheng } else { 3399efd75567SFam Zheng error_setg(errp, "Target is attached to a different thread from " 3400efd75567SFam Zheng "source."); 3401efd75567SFam Zheng goto out; 3402efd75567SFam Zheng } 3403efd75567SFam Zheng } 340470559d49SAlberto Garcia backup_start(job_id, bs, target_bs, speed, sync, NULL, on_source_error, 340578f51fdeSJohn Snow on_target_error, block_job_cb, bs, txn, &local_err); 3406c29c1dd3SFam Zheng if (local_err != NULL) { 3407c29c1dd3SFam Zheng error_propagate(errp, local_err); 3408c29c1dd3SFam Zheng } 3409c29c1dd3SFam Zheng out: 3410c29c1dd3SFam Zheng aio_context_release(aio_context); 3411c29c1dd3SFam Zheng } 3412c29c1dd3SFam Zheng 341370559d49SAlberto Garcia void qmp_blockdev_backup(bool has_job_id, const char *job_id, 341470559d49SAlberto Garcia const char *device, const char *target, 341578f51fdeSJohn Snow enum MirrorSyncMode sync, 341678f51fdeSJohn Snow bool has_speed, int64_t speed, 341778f51fdeSJohn Snow bool has_on_source_error, 341878f51fdeSJohn Snow BlockdevOnError on_source_error, 341978f51fdeSJohn Snow bool has_on_target_error, 342078f51fdeSJohn Snow BlockdevOnError on_target_error, 342178f51fdeSJohn Snow Error **errp) 342278f51fdeSJohn Snow { 342370559d49SAlberto Garcia do_blockdev_backup(has_job_id ? job_id : NULL, device, target, 342470559d49SAlberto Garcia sync, has_speed, speed, 342578f51fdeSJohn Snow has_on_source_error, on_source_error, 342678f51fdeSJohn Snow has_on_target_error, on_target_error, 342778f51fdeSJohn Snow NULL, errp); 342878f51fdeSJohn Snow } 342978f51fdeSJohn Snow 34304193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 34314193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 34324193cdd7SFam Zheng **/ 343371aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, 34344193cdd7SFam Zheng BlockDriverState *target, 343509158f00SBenoît Canet bool has_replaces, const char *replaces, 3436d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3437274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 3438b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3439eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 344008e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 34414193cdd7SFam Zheng bool has_on_source_error, 34424193cdd7SFam Zheng BlockdevOnError on_source_error, 34434193cdd7SFam Zheng bool has_on_target_error, 34444193cdd7SFam Zheng BlockdevOnError on_target_error, 34450fc9f8eaSFam Zheng bool has_unmap, bool unmap, 3446b952b558SPaolo Bonzini Error **errp) 3447d9b902dbSPaolo Bonzini { 3448d9b902dbSPaolo Bonzini 3449d9b902dbSPaolo Bonzini if (!has_speed) { 3450d9b902dbSPaolo Bonzini speed = 0; 3451d9b902dbSPaolo Bonzini } 3452b952b558SPaolo Bonzini if (!has_on_source_error) { 3453b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3454b952b558SPaolo Bonzini } 3455b952b558SPaolo Bonzini if (!has_on_target_error) { 3456b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3457b952b558SPaolo Bonzini } 3458eee13dfeSPaolo Bonzini if (!has_granularity) { 3459eee13dfeSPaolo Bonzini granularity = 0; 3460eee13dfeSPaolo Bonzini } 346108e4ed6cSPaolo Bonzini if (!has_buf_size) { 346248ac0a4dSWen Congyang buf_size = 0; 346308e4ed6cSPaolo Bonzini } 34640fc9f8eaSFam Zheng if (!has_unmap) { 34650fc9f8eaSFam Zheng unmap = true; 34660fc9f8eaSFam Zheng } 346708e4ed6cSPaolo Bonzini 3468eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3469c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 34703cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3471eee13dfeSPaolo Bonzini return; 3472eee13dfeSPaolo Bonzini } 3473eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3474c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3475c6bd8c70SMarkus Armbruster "power of 2"); 3476eee13dfeSPaolo Bonzini return; 3477eee13dfeSPaolo Bonzini } 3478d9b902dbSPaolo Bonzini 34794193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 34804193cdd7SFam Zheng return; 34814193cdd7SFam Zheng } 3482e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3483e40e5027SFam Zheng return; 3484e40e5027SFam Zheng } 34854193cdd7SFam Zheng 34864193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 34874193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 34884193cdd7SFam Zheng } 34894193cdd7SFam Zheng 34904193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 34914193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 34924193cdd7SFam Zheng */ 349371aa9867SAlberto Garcia mirror_start(job_id, bs, target, 34944193cdd7SFam Zheng has_replaces ? replaces : NULL, 3495274fcceeSMax Reitz speed, granularity, buf_size, sync, backing_mode, 34964193cdd7SFam Zheng on_source_error, on_target_error, unmap, 34974193cdd7SFam Zheng block_job_cb, bs, errp); 34984193cdd7SFam Zheng } 34994193cdd7SFam Zheng 350071aa9867SAlberto Garcia void qmp_drive_mirror(bool has_job_id, const char *job_id, const char *device, 350171aa9867SAlberto Garcia const char *target, bool has_format, const char *format, 35024193cdd7SFam Zheng bool has_node_name, const char *node_name, 35034193cdd7SFam Zheng bool has_replaces, const char *replaces, 35044193cdd7SFam Zheng enum MirrorSyncMode sync, 35054193cdd7SFam Zheng bool has_mode, enum NewImageMode mode, 35064193cdd7SFam Zheng bool has_speed, int64_t speed, 35074193cdd7SFam Zheng bool has_granularity, uint32_t granularity, 35084193cdd7SFam Zheng bool has_buf_size, int64_t buf_size, 35094193cdd7SFam Zheng bool has_on_source_error, BlockdevOnError on_source_error, 35104193cdd7SFam Zheng bool has_on_target_error, BlockdevOnError on_target_error, 35114193cdd7SFam Zheng bool has_unmap, bool unmap, 35124193cdd7SFam Zheng Error **errp) 35134193cdd7SFam Zheng { 35144193cdd7SFam Zheng BlockDriverState *bs; 35154193cdd7SFam Zheng BlockBackend *blk; 35164193cdd7SFam Zheng BlockDriverState *source, *target_bs; 35174193cdd7SFam Zheng AioContext *aio_context; 3518274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 35194193cdd7SFam Zheng Error *local_err = NULL; 35204193cdd7SFam Zheng QDict *options = NULL; 35214193cdd7SFam Zheng int flags; 35224193cdd7SFam Zheng int64_t size; 35234193cdd7SFam Zheng 3524a0e8544cSFam Zheng blk = blk_by_name(device); 3525a0e8544cSFam Zheng if (!blk) { 352675158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 352775158ebbSMarkus Armbruster "Device '%s' not found", device); 3528d9b902dbSPaolo Bonzini return; 3529d9b902dbSPaolo Bonzini } 3530d9b902dbSPaolo Bonzini 35315433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 35325a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 35335a7e7a0bSStefan Hajnoczi 35345433c24fSMax Reitz if (!blk_is_available(blk)) { 3535c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 35365a7e7a0bSStefan Hajnoczi goto out; 3537d9b902dbSPaolo Bonzini } 35385433c24fSMax Reitz bs = blk_bs(blk); 35394193cdd7SFam Zheng if (!has_mode) { 35404193cdd7SFam Zheng mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 35414193cdd7SFam Zheng } 3542d9b902dbSPaolo Bonzini 3543d9b902dbSPaolo Bonzini if (!has_format) { 3544d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3545d9b902dbSPaolo Bonzini } 3546d9b902dbSPaolo Bonzini 354761de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3548760e0063SKevin Wolf source = backing_bs(bs); 3549d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3550d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 3551d9b902dbSPaolo Bonzini } 3552117e0c82SMax Reitz if (sync == MIRROR_SYNC_MODE_NONE) { 3553117e0c82SMax Reitz source = bs; 3554117e0c82SMax Reitz } 3555d9b902dbSPaolo Bonzini 3556ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3557ac3c5d83SStefan Hajnoczi if (size < 0) { 3558ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 35595a7e7a0bSStefan Hajnoczi goto out; 3560ac3c5d83SStefan Hajnoczi } 3561ac3c5d83SStefan Hajnoczi 356209158f00SBenoît Canet if (has_replaces) { 356309158f00SBenoît Canet BlockDriverState *to_replace_bs; 35645a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 35655a7e7a0bSStefan Hajnoczi int64_t replace_size; 356609158f00SBenoît Canet 356709158f00SBenoît Canet if (!has_node_name) { 356809158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 356909158f00SBenoît Canet " named node of the graph"); 35705a7e7a0bSStefan Hajnoczi goto out; 357109158f00SBenoît Canet } 357209158f00SBenoît Canet 3573e12f3784SWen Congyang to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 357409158f00SBenoît Canet 357509158f00SBenoît Canet if (!to_replace_bs) { 357609158f00SBenoît Canet error_propagate(errp, local_err); 35775a7e7a0bSStefan Hajnoczi goto out; 357809158f00SBenoît Canet } 357909158f00SBenoît Canet 35805a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 35815a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 35825a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 35835a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 35845a7e7a0bSStefan Hajnoczi 35855a7e7a0bSStefan Hajnoczi if (size != replace_size) { 358609158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 358709158f00SBenoît Canet "different size"); 35885a7e7a0bSStefan Hajnoczi goto out; 358909158f00SBenoît Canet } 359009158f00SBenoît Canet } 359109158f00SBenoît Canet 3592274fcceeSMax Reitz if (mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { 3593274fcceeSMax Reitz backing_mode = MIRROR_SOURCE_BACKING_CHAIN; 3594274fcceeSMax Reitz } else { 3595274fcceeSMax Reitz backing_mode = MIRROR_OPEN_BACKING_CHAIN; 3596274fcceeSMax Reitz } 3597274fcceeSMax Reitz 359814526864SMax Reitz if ((sync == MIRROR_SYNC_MODE_FULL || !source) 359914526864SMax Reitz && mode != NEW_IMAGE_MODE_EXISTING) 360014526864SMax Reitz { 3601d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3602e6641719SMax Reitz assert(format); 3603cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3604f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3605d9b902dbSPaolo Bonzini } else { 3606d9b902dbSPaolo Bonzini switch (mode) { 3607d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3608d9b902dbSPaolo Bonzini break; 3609d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3610d9b902dbSPaolo Bonzini /* create new image with backing file */ 3611cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3612d9b902dbSPaolo Bonzini source->filename, 3613d9b902dbSPaolo Bonzini source->drv->format_name, 3614f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3615d9b902dbSPaolo Bonzini break; 3616d9b902dbSPaolo Bonzini default: 3617d9b902dbSPaolo Bonzini abort(); 3618d9b902dbSPaolo Bonzini } 3619d9b902dbSPaolo Bonzini } 3620d9b902dbSPaolo Bonzini 362184d18f06SMarkus Armbruster if (local_err) { 3622cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 36235a7e7a0bSStefan Hajnoczi goto out; 3624d9b902dbSPaolo Bonzini } 3625d9b902dbSPaolo Bonzini 36264c828dc6SBenoît Canet options = qdict_new(); 3627e6641719SMax Reitz if (has_node_name) { 36284c828dc6SBenoît Canet qdict_put(options, "node-name", qstring_from_str(node_name)); 36294c828dc6SBenoît Canet } 3630e6641719SMax Reitz if (format) { 3631e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3632e6641719SMax Reitz } 36334c828dc6SBenoît Canet 3634b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3635b812f671SPaolo Bonzini * file. 3636b812f671SPaolo Bonzini */ 36375b363937SMax Reitz target_bs = bdrv_open(target, NULL, options, flags | BDRV_O_NO_BACKING, 36385b363937SMax Reitz errp); 36395b363937SMax Reitz if (!target_bs) { 36405a7e7a0bSStefan Hajnoczi goto out; 3641d9b902dbSPaolo Bonzini } 3642d9b902dbSPaolo Bonzini 36435a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 36445a7e7a0bSStefan Hajnoczi 364571aa9867SAlberto Garcia blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 3646274fcceeSMax Reitz has_replaces, replaces, sync, backing_mode, 36474193cdd7SFam Zheng has_speed, speed, 36484193cdd7SFam Zheng has_granularity, granularity, 36494193cdd7SFam Zheng has_buf_size, buf_size, 36504193cdd7SFam Zheng has_on_source_error, on_source_error, 36514193cdd7SFam Zheng has_on_target_error, on_target_error, 36524193cdd7SFam Zheng has_unmap, unmap, 36534193cdd7SFam Zheng &local_err); 3654e253f4b8SKevin Wolf bdrv_unref(target_bs); 3655d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 36565a7e7a0bSStefan Hajnoczi out: 36575a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3658d9b902dbSPaolo Bonzini } 3659d9b902dbSPaolo Bonzini 366071aa9867SAlberto Garcia void qmp_blockdev_mirror(bool has_job_id, const char *job_id, 366171aa9867SAlberto Garcia const char *device, const char *target, 3662df92562eSFam Zheng bool has_replaces, const char *replaces, 3663df92562eSFam Zheng MirrorSyncMode sync, 3664df92562eSFam Zheng bool has_speed, int64_t speed, 3665df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3666df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3667df92562eSFam Zheng bool has_on_source_error, 3668df92562eSFam Zheng BlockdevOnError on_source_error, 3669df92562eSFam Zheng bool has_on_target_error, 3670df92562eSFam Zheng BlockdevOnError on_target_error, 3671df92562eSFam Zheng Error **errp) 3672df92562eSFam Zheng { 3673df92562eSFam Zheng BlockDriverState *bs; 3674df92562eSFam Zheng BlockBackend *blk; 3675df92562eSFam Zheng BlockDriverState *target_bs; 3676df92562eSFam Zheng AioContext *aio_context; 3677274fcceeSMax Reitz BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; 3678df92562eSFam Zheng Error *local_err = NULL; 3679df92562eSFam Zheng 3680df92562eSFam Zheng blk = blk_by_name(device); 3681df92562eSFam Zheng if (!blk) { 3682df92562eSFam Zheng error_setg(errp, "Device '%s' not found", device); 3683df92562eSFam Zheng return; 3684df92562eSFam Zheng } 3685df92562eSFam Zheng bs = blk_bs(blk); 3686df92562eSFam Zheng 3687df92562eSFam Zheng if (!bs) { 3688df92562eSFam Zheng error_setg(errp, "Device '%s' has no media", device); 3689df92562eSFam Zheng return; 3690df92562eSFam Zheng } 3691df92562eSFam Zheng 3692df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3693df92562eSFam Zheng if (!target_bs) { 3694df92562eSFam Zheng return; 3695df92562eSFam Zheng } 3696df92562eSFam Zheng 3697df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3698df92562eSFam Zheng aio_context_acquire(aio_context); 3699df92562eSFam Zheng 3700df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3701df92562eSFam Zheng 370271aa9867SAlberto Garcia blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 3703274fcceeSMax Reitz has_replaces, replaces, sync, backing_mode, 3704df92562eSFam Zheng has_speed, speed, 3705df92562eSFam Zheng has_granularity, granularity, 3706df92562eSFam Zheng has_buf_size, buf_size, 3707df92562eSFam Zheng has_on_source_error, on_source_error, 3708df92562eSFam Zheng has_on_target_error, on_target_error, 3709df92562eSFam Zheng true, true, 3710df92562eSFam Zheng &local_err); 3711df92562eSFam Zheng error_propagate(errp, local_err); 3712df92562eSFam Zheng 3713df92562eSFam Zheng aio_context_release(aio_context); 3714df92562eSFam Zheng } 3715df92562eSFam Zheng 37163ddf3efeSAlberto Garcia /* Get a block job using its ID and acquire its AioContext */ 37173ddf3efeSAlberto Garcia static BlockJob *find_block_job(const char *id, AioContext **aio_context, 371824d6bffeSMarkus Armbruster Error **errp) 37192d47c6e9SStefan Hajnoczi { 37203ddf3efeSAlberto Garcia BlockJob *job; 37213ddf3efeSAlberto Garcia 37223ddf3efeSAlberto Garcia assert(id != NULL); 37232d47c6e9SStefan Hajnoczi 37245433c24fSMax Reitz *aio_context = NULL; 37255433c24fSMax Reitz 37263ddf3efeSAlberto Garcia job = block_job_get(id); 37273ddf3efeSAlberto Garcia 37283ddf3efeSAlberto Garcia if (!job) { 37293ddf3efeSAlberto Garcia error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 37303ddf3efeSAlberto Garcia "Block job '%s' not found", id); 37313ddf3efeSAlberto Garcia return NULL; 37322d47c6e9SStefan Hajnoczi } 37333d948cdfSStefan Hajnoczi 37343ddf3efeSAlberto Garcia *aio_context = blk_get_aio_context(job->blk); 37353d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 37363d948cdfSStefan Hajnoczi 37373ddf3efeSAlberto Garcia return job; 37382d47c6e9SStefan Hajnoczi } 37392d47c6e9SStefan Hajnoczi 3740882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 37412d47c6e9SStefan Hajnoczi { 37423d948cdfSStefan Hajnoczi AioContext *aio_context; 374324d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37442d47c6e9SStefan Hajnoczi 37452d47c6e9SStefan Hajnoczi if (!job) { 37462d47c6e9SStefan Hajnoczi return; 37472d47c6e9SStefan Hajnoczi } 37482d47c6e9SStefan Hajnoczi 3749882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 37503d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37512d47c6e9SStefan Hajnoczi } 3752370521a1SStefan Hajnoczi 37536e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 37546e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 37556e37fb81SPaolo Bonzini { 37563d948cdfSStefan Hajnoczi AioContext *aio_context; 375724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37586e37fb81SPaolo Bonzini 37596e37fb81SPaolo Bonzini if (!job) { 37606e37fb81SPaolo Bonzini return; 37616e37fb81SPaolo Bonzini } 37623d948cdfSStefan Hajnoczi 37633d948cdfSStefan Hajnoczi if (!has_force) { 37643d948cdfSStefan Hajnoczi force = false; 37653d948cdfSStefan Hajnoczi } 37663d948cdfSStefan Hajnoczi 3767751ebd76SFam Zheng if (job->user_paused && !force) { 3768f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3769f231b88dSCole Robinson device); 37703d948cdfSStefan Hajnoczi goto out; 37716e37fb81SPaolo Bonzini } 37726e37fb81SPaolo Bonzini 37736e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 37746e37fb81SPaolo Bonzini block_job_cancel(job); 37753d948cdfSStefan Hajnoczi out: 37763d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37776e37fb81SPaolo Bonzini } 37786e37fb81SPaolo Bonzini 37796e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3780370521a1SStefan Hajnoczi { 37813d948cdfSStefan Hajnoczi AioContext *aio_context; 378224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3783370521a1SStefan Hajnoczi 3784751ebd76SFam Zheng if (!job || job->user_paused) { 3785370521a1SStefan Hajnoczi return; 3786370521a1SStefan Hajnoczi } 37876e37fb81SPaolo Bonzini 3788751ebd76SFam Zheng job->user_paused = true; 37896e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 37906e37fb81SPaolo Bonzini block_job_pause(job); 37913d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37926e37fb81SPaolo Bonzini } 37936e37fb81SPaolo Bonzini 37946e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 37956e37fb81SPaolo Bonzini { 37963d948cdfSStefan Hajnoczi AioContext *aio_context; 379724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37986e37fb81SPaolo Bonzini 3799751ebd76SFam Zheng if (!job || !job->user_paused) { 38008acc72a4SPaolo Bonzini return; 38018acc72a4SPaolo Bonzini } 3802370521a1SStefan Hajnoczi 3803751ebd76SFam Zheng job->user_paused = false; 38046e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 380517bd51f9SStefan Hajnoczi block_job_iostatus_reset(job); 38066e37fb81SPaolo Bonzini block_job_resume(job); 38073d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3808370521a1SStefan Hajnoczi } 3809fb5458cdSStefan Hajnoczi 3810aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3811aeae883bSPaolo Bonzini { 38123d948cdfSStefan Hajnoczi AioContext *aio_context; 381324d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3814aeae883bSPaolo Bonzini 3815aeae883bSPaolo Bonzini if (!job) { 3816aeae883bSPaolo Bonzini return; 3817aeae883bSPaolo Bonzini } 3818aeae883bSPaolo Bonzini 3819aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3820aeae883bSPaolo Bonzini block_job_complete(job, errp); 38213d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3822aeae883bSPaolo Bonzini } 3823aeae883bSPaolo Bonzini 3824fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3825fa40e656SJeff Cody const char *image_node_name, 3826fa40e656SJeff Cody const char *backing_file, 3827fa40e656SJeff Cody Error **errp) 3828fa40e656SJeff Cody { 3829a0e8544cSFam Zheng BlockBackend *blk; 3830fa40e656SJeff Cody BlockDriverState *bs = NULL; 3831729962f6SStefan Hajnoczi AioContext *aio_context; 3832fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3833fa40e656SJeff Cody Error *local_err = NULL; 3834fa40e656SJeff Cody bool ro; 3835fa40e656SJeff Cody int open_flags; 3836fa40e656SJeff Cody int ret; 3837fa40e656SJeff Cody 3838a0e8544cSFam Zheng blk = blk_by_name(device); 3839a0e8544cSFam Zheng if (!blk) { 384075158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 384175158ebbSMarkus Armbruster "Device '%s' not found", device); 3842fa40e656SJeff Cody return; 3843fa40e656SJeff Cody } 3844fa40e656SJeff Cody 38455433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3846729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3847729962f6SStefan Hajnoczi 38485433c24fSMax Reitz if (!blk_is_available(blk)) { 38495433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 38505433c24fSMax Reitz goto out; 38515433c24fSMax Reitz } 38525433c24fSMax Reitz bs = blk_bs(blk); 38535433c24fSMax Reitz 3854fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3855fa40e656SJeff Cody if (local_err) { 3856fa40e656SJeff Cody error_propagate(errp, local_err); 3857729962f6SStefan Hajnoczi goto out; 3858fa40e656SJeff Cody } 3859fa40e656SJeff Cody 3860fa40e656SJeff Cody if (!image_bs) { 3861fa40e656SJeff Cody error_setg(errp, "image file not found"); 3862729962f6SStefan Hajnoczi goto out; 3863fa40e656SJeff Cody } 3864fa40e656SJeff Cody 3865fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3866fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3867fa40e656SJeff Cody "without a backing file"); 3868729962f6SStefan Hajnoczi goto out; 3869fa40e656SJeff Cody } 3870fa40e656SJeff Cody 3871fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3872fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3873fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3874729962f6SStefan Hajnoczi goto out; 3875fa40e656SJeff Cody } 3876fa40e656SJeff Cody 3877fa40e656SJeff Cody /* final sanity check */ 3878fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3879fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3880fa40e656SJeff Cody device); 3881729962f6SStefan Hajnoczi goto out; 3882fa40e656SJeff Cody } 3883fa40e656SJeff Cody 3884fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3885fa40e656SJeff Cody open_flags = image_bs->open_flags; 3886fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3887fa40e656SJeff Cody 3888fa40e656SJeff Cody if (ro) { 3889fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3890fa40e656SJeff Cody if (local_err) { 3891fa40e656SJeff Cody error_propagate(errp, local_err); 3892729962f6SStefan Hajnoczi goto out; 3893fa40e656SJeff Cody } 3894fa40e656SJeff Cody } 3895fa40e656SJeff Cody 3896fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3897fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3898fa40e656SJeff Cody 3899fa40e656SJeff Cody if (ret < 0) { 3900fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3901fa40e656SJeff Cody backing_file); 3902fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3903fa40e656SJeff Cody * appropriate */ 3904fa40e656SJeff Cody } 3905fa40e656SJeff Cody 3906fa40e656SJeff Cody if (ro) { 3907fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3908621ff94dSEduardo Habkost error_propagate(errp, local_err); 3909fa40e656SJeff Cody } 3910729962f6SStefan Hajnoczi 3911729962f6SStefan Hajnoczi out: 3912729962f6SStefan Hajnoczi aio_context_release(aio_context); 3913fa40e656SJeff Cody } 3914fa40e656SJeff Cody 3915abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 3916abb21ac3SKevin Wolf { 3917abb21ac3SKevin Wolf QemuOpts *opts; 3918abb21ac3SKevin Wolf QDict *qdict; 3919abb21ac3SKevin Wolf Error *local_err = NULL; 3920abb21ac3SKevin Wolf 3921abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 3922abb21ac3SKevin Wolf if (!opts) { 3923abb21ac3SKevin Wolf return; 3924abb21ac3SKevin Wolf } 3925abb21ac3SKevin Wolf 3926abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 3927abb21ac3SKevin Wolf 3928abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 3929f8746fb8SKevin Wolf QDECREF(qdict); 3930abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 3931abb21ac3SKevin Wolf goto out; 3932abb21ac3SKevin Wolf } 3933abb21ac3SKevin Wolf 3934abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 3935abb21ac3SKevin Wolf if (!bs) { 3936abb21ac3SKevin Wolf error_report_err(local_err); 3937abb21ac3SKevin Wolf goto out; 3938abb21ac3SKevin Wolf } 3939abb21ac3SKevin Wolf 3940abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3941abb21ac3SKevin Wolf 3942abb21ac3SKevin Wolf out: 3943abb21ac3SKevin Wolf qemu_opts_del(opts); 3944abb21ac3SKevin Wolf } 3945abb21ac3SKevin Wolf 3946d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3947d26c9a15SKevin Wolf { 3948be4b67bcSMax Reitz BlockDriverState *bs; 3949be4b67bcSMax Reitz BlockBackend *blk = NULL; 3950d26c9a15SKevin Wolf QObject *obj; 39513b098d56SEric Blake Visitor *v = qmp_output_visitor_new(&obj); 3952d26c9a15SKevin Wolf QDict *qdict; 3953d26c9a15SKevin Wolf Error *local_err = NULL; 3954d26c9a15SKevin Wolf 395560e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3956d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 395760e19e06SMarkus Armbruster * when called from drive_new(). 3958d26c9a15SKevin Wolf * 3959d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3960d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3961c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3962c6e0bd9bSKevin Wolf options->cache->has_direct && 3963c6e0bd9bSKevin Wolf options->cache->direct; 3964c6e0bd9bSKevin Wolf if (!direct) { 3965d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3966d26c9a15SKevin Wolf goto fail; 3967d26c9a15SKevin Wolf } 3968d26c9a15SKevin Wolf } 3969d26c9a15SKevin Wolf 39703b098d56SEric Blake visit_type_BlockdevOptions(v, NULL, &options, &local_err); 397184d18f06SMarkus Armbruster if (local_err) { 3972d26c9a15SKevin Wolf error_propagate(errp, local_err); 3973d26c9a15SKevin Wolf goto fail; 3974d26c9a15SKevin Wolf } 3975d26c9a15SKevin Wolf 39763b098d56SEric Blake visit_complete(v, &obj); 3977d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3978d26c9a15SKevin Wolf 3979d26c9a15SKevin Wolf qdict_flatten(qdict); 3980d26c9a15SKevin Wolf 3981be4b67bcSMax Reitz if (options->has_id) { 398218e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 398384d18f06SMarkus Armbruster if (local_err) { 3984b681072dSKevin Wolf error_propagate(errp, local_err); 3985d26c9a15SKevin Wolf goto fail; 3986d26c9a15SKevin Wolf } 3987d26c9a15SKevin Wolf 3988be4b67bcSMax Reitz bs = blk_bs(blk); 3989be4b67bcSMax Reitz } else { 3990be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3991be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3992be4b67bcSMax Reitz "the root node"); 3993be4b67bcSMax Reitz goto fail; 3994be4b67bcSMax Reitz } 3995be4b67bcSMax Reitz 3996bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3997bd745e23SMax Reitz if (!bs) { 3998be4b67bcSMax Reitz goto fail; 3999be4b67bcSMax Reitz } 40009c4218e9SMax Reitz 40019c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4002be4b67bcSMax Reitz } 4003be4b67bcSMax Reitz 4004be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 4005be4b67bcSMax Reitz if (blk) { 4006efaa7c4eSMax Reitz monitor_remove_blk(blk); 400718e46a03SMarkus Armbruster blk_unref(blk); 4008be4b67bcSMax Reitz } else { 40099c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 4010be4b67bcSMax Reitz bdrv_unref(bs); 4011be4b67bcSMax Reitz } 40128ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 40138ae8e904SKevin Wolf goto fail; 40148ae8e904SKevin Wolf } 40158ae8e904SKevin Wolf 4016d26c9a15SKevin Wolf fail: 40173b098d56SEric Blake visit_free(v); 4018d26c9a15SKevin Wolf } 4019d26c9a15SKevin Wolf 402081b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id, 402181b936aeSAlberto Garcia bool has_node_name, const char *node_name, Error **errp) 402281b936aeSAlberto Garcia { 402381b936aeSAlberto Garcia AioContext *aio_context; 402481b936aeSAlberto Garcia BlockBackend *blk; 402581b936aeSAlberto Garcia BlockDriverState *bs; 402681b936aeSAlberto Garcia 402781b936aeSAlberto Garcia if (has_id && has_node_name) { 402881b936aeSAlberto Garcia error_setg(errp, "Only one of id and node-name must be specified"); 402981b936aeSAlberto Garcia return; 403081b936aeSAlberto Garcia } else if (!has_id && !has_node_name) { 403181b936aeSAlberto Garcia error_setg(errp, "No block device specified"); 403281b936aeSAlberto Garcia return; 403381b936aeSAlberto Garcia } 403481b936aeSAlberto Garcia 403581b936aeSAlberto Garcia if (has_id) { 4036efaa7c4eSMax Reitz /* blk_by_name() never returns a BB that is not owned by the monitor */ 403781b936aeSAlberto Garcia blk = blk_by_name(id); 403881b936aeSAlberto Garcia if (!blk) { 403981b936aeSAlberto Garcia error_setg(errp, "Cannot find block backend %s", id); 404081b936aeSAlberto Garcia return; 404181b936aeSAlberto Garcia } 40425cf87fd6SPaolo Bonzini if (blk_legacy_dinfo(blk)) { 40435cf87fd6SPaolo Bonzini error_setg(errp, "Deleting block backend added with drive-add" 40445cf87fd6SPaolo Bonzini " is not supported"); 40455cf87fd6SPaolo Bonzini return; 40465cf87fd6SPaolo Bonzini } 404781b936aeSAlberto Garcia if (blk_get_refcnt(blk) > 1) { 404881b936aeSAlberto Garcia error_setg(errp, "Block backend %s is in use", id); 404981b936aeSAlberto Garcia return; 405081b936aeSAlberto Garcia } 405181b936aeSAlberto Garcia bs = blk_bs(blk); 405281b936aeSAlberto Garcia aio_context = blk_get_aio_context(blk); 405381b936aeSAlberto Garcia } else { 40541f0c461bSKevin Wolf blk = NULL; 405581b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 405681b936aeSAlberto Garcia if (!bs) { 405781b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 405881b936aeSAlberto Garcia return; 405981b936aeSAlberto Garcia } 40601f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 406181b936aeSAlberto Garcia error_setg(errp, "Node %s is in use by %s", 40621f0c461bSKevin Wolf node_name, bdrv_get_parent_name(bs)); 406381b936aeSAlberto Garcia return; 406481b936aeSAlberto Garcia } 406581b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 406681b936aeSAlberto Garcia } 406781b936aeSAlberto Garcia 406881b936aeSAlberto Garcia aio_context_acquire(aio_context); 406981b936aeSAlberto Garcia 407081b936aeSAlberto Garcia if (bs) { 407181b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 407281b936aeSAlberto Garcia goto out; 407381b936aeSAlberto Garcia } 407481b936aeSAlberto Garcia 40759c4218e9SMax Reitz if (!blk && !bs->monitor_list.tqe_prev) { 40769c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 40779c4218e9SMax Reitz bs->node_name); 40789c4218e9SMax Reitz goto out; 40799c4218e9SMax Reitz } 40809c4218e9SMax Reitz 40819c4218e9SMax Reitz if (bs->refcnt > 1) { 408281b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 408381b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 408481b936aeSAlberto Garcia goto out; 408581b936aeSAlberto Garcia } 408681b936aeSAlberto Garcia } 408781b936aeSAlberto Garcia 408881b936aeSAlberto Garcia if (blk) { 4089efaa7c4eSMax Reitz monitor_remove_blk(blk); 409081b936aeSAlberto Garcia blk_unref(blk); 409181b936aeSAlberto Garcia } else { 40929c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 409381b936aeSAlberto Garcia bdrv_unref(bs); 409481b936aeSAlberto Garcia } 409581b936aeSAlberto Garcia 409681b936aeSAlberto Garcia out: 409781b936aeSAlberto Garcia aio_context_release(aio_context); 409881b936aeSAlberto Garcia } 409981b936aeSAlberto Garcia 41007f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 41017f821597SWen Congyang const char *child_name) 41027f821597SWen Congyang { 41037f821597SWen Congyang BdrvChild *child; 41047f821597SWen Congyang 41057f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 41067f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 41077f821597SWen Congyang return child; 41087f821597SWen Congyang } 41097f821597SWen Congyang } 41107f821597SWen Congyang 41117f821597SWen Congyang return NULL; 41127f821597SWen Congyang } 41137f821597SWen Congyang 41147f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 41157f821597SWen Congyang const char *child, bool has_node, 41167f821597SWen Congyang const char *node, Error **errp) 41177f821597SWen Congyang { 41187f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 41197f821597SWen Congyang BdrvChild *p_child; 41207f821597SWen Congyang 41217f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 41227f821597SWen Congyang if (!parent_bs) { 41237f821597SWen Congyang return; 41247f821597SWen Congyang } 41257f821597SWen Congyang 41267f821597SWen Congyang if (has_child == has_node) { 41277f821597SWen Congyang if (has_child) { 41287f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 41297f821597SWen Congyang } else { 41307f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 41317f821597SWen Congyang } 41327f821597SWen Congyang return; 41337f821597SWen Congyang } 41347f821597SWen Congyang 41357f821597SWen Congyang if (has_child) { 41367f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 41377f821597SWen Congyang if (!p_child) { 41387f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 41397f821597SWen Congyang parent, child); 41407f821597SWen Congyang return; 41417f821597SWen Congyang } 41427f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 41437f821597SWen Congyang } 41447f821597SWen Congyang 41457f821597SWen Congyang if (has_node) { 41467f821597SWen Congyang new_bs = bdrv_find_node(node); 41477f821597SWen Congyang if (!new_bs) { 41487f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 41497f821597SWen Congyang return; 41507f821597SWen Congyang } 41517f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 41527f821597SWen Congyang } 41537f821597SWen Congyang } 41547f821597SWen Congyang 4155fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4156fb5458cdSStefan Hajnoczi { 4157fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4158f0f55dedSAlberto Garcia BlockJob *job; 4159fea68bb6SMarkus Armbruster 4160f0f55dedSAlberto Garcia for (job = block_job_next(NULL); job; job = block_job_next(job)) { 4161f0f55dedSAlberto Garcia BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 4162f0f55dedSAlberto Garcia AioContext *aio_context = blk_get_aio_context(job->blk); 416369691e72SStefan Hajnoczi 416469691e72SStefan Hajnoczi aio_context_acquire(aio_context); 4165f0f55dedSAlberto Garcia elem->value = block_job_query(job); 4166f0f55dedSAlberto Garcia aio_context_release(aio_context); 416769691e72SStefan Hajnoczi 4168fea68bb6SMarkus Armbruster *p_next = elem; 4169fea68bb6SMarkus Armbruster p_next = &elem->next; 4170fea68bb6SMarkus Armbruster } 417169691e72SStefan Hajnoczi 4172fea68bb6SMarkus Armbruster return head; 4173fb5458cdSStefan Hajnoczi } 41744d454574SPaolo Bonzini 41750006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 41764d454574SPaolo Bonzini .name = "drive", 41770006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 41784d454574SPaolo Bonzini .desc = { 41794d454574SPaolo Bonzini { 41804d454574SPaolo Bonzini .name = "snapshot", 41814d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 41824d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 41834d454574SPaolo Bonzini },{ 4184a9384affSPaolo Bonzini .name = "discard", 4185a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 4186a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 4187a9384affSPaolo Bonzini },{ 41884d454574SPaolo Bonzini .name = "aio", 41894d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41904d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 41914d454574SPaolo Bonzini },{ 4192e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4193e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4194e4b24b49SKevin Wolf .help = "Enable writeback mode", 4195e4b24b49SKevin Wolf },{ 41964d454574SPaolo Bonzini .name = "format", 41974d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41984d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 41994d454574SPaolo Bonzini },{ 42004d454574SPaolo Bonzini .name = "rerror", 42014d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 42024d454574SPaolo Bonzini .help = "read error action", 42034d454574SPaolo Bonzini },{ 42044d454574SPaolo Bonzini .name = "werror", 42054d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 42064d454574SPaolo Bonzini .help = "write error action", 42074d454574SPaolo Bonzini },{ 42080f227a94SKevin Wolf .name = "read-only", 42094d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 42104d454574SPaolo Bonzini .help = "open drive file as read-only", 42114d454574SPaolo Bonzini },{ 421257975222SKevin Wolf .name = "throttling.iops-total", 42134d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42144d454574SPaolo Bonzini .help = "limit total I/O operations per second", 42154d454574SPaolo Bonzini },{ 421657975222SKevin Wolf .name = "throttling.iops-read", 42174d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42184d454574SPaolo Bonzini .help = "limit read operations per second", 42194d454574SPaolo Bonzini },{ 422057975222SKevin Wolf .name = "throttling.iops-write", 42214d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42224d454574SPaolo Bonzini .help = "limit write operations per second", 42234d454574SPaolo Bonzini },{ 422457975222SKevin Wolf .name = "throttling.bps-total", 42254d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42264d454574SPaolo Bonzini .help = "limit total bytes per second", 42274d454574SPaolo Bonzini },{ 422857975222SKevin Wolf .name = "throttling.bps-read", 42294d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42304d454574SPaolo Bonzini .help = "limit read bytes per second", 42314d454574SPaolo Bonzini },{ 423257975222SKevin Wolf .name = "throttling.bps-write", 42334d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42344d454574SPaolo Bonzini .help = "limit write bytes per second", 42354d454574SPaolo Bonzini },{ 42363e9fab69SBenoît Canet .name = "throttling.iops-total-max", 42373e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42383e9fab69SBenoît Canet .help = "I/O operations burst", 42393e9fab69SBenoît Canet },{ 42403e9fab69SBenoît Canet .name = "throttling.iops-read-max", 42413e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42423e9fab69SBenoît Canet .help = "I/O operations read burst", 42433e9fab69SBenoît Canet },{ 42443e9fab69SBenoît Canet .name = "throttling.iops-write-max", 42453e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42463e9fab69SBenoît Canet .help = "I/O operations write burst", 42473e9fab69SBenoît Canet },{ 42483e9fab69SBenoît Canet .name = "throttling.bps-total-max", 42493e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42503e9fab69SBenoît Canet .help = "total bytes burst", 42513e9fab69SBenoît Canet },{ 42523e9fab69SBenoît Canet .name = "throttling.bps-read-max", 42533e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42543e9fab69SBenoît Canet .help = "total bytes read burst", 42553e9fab69SBenoît Canet },{ 42563e9fab69SBenoît Canet .name = "throttling.bps-write-max", 42573e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42583e9fab69SBenoît Canet .help = "total bytes write burst", 42593e9fab69SBenoît Canet },{ 42608a0fc18dSAlberto Garcia .name = "throttling.iops-total-max-length", 42618a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42628a0fc18dSAlberto Garcia .help = "length of the iops-total-max burst period, in seconds", 42638a0fc18dSAlberto Garcia },{ 42648a0fc18dSAlberto Garcia .name = "throttling.iops-read-max-length", 42658a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42668a0fc18dSAlberto Garcia .help = "length of the iops-read-max burst period, in seconds", 42678a0fc18dSAlberto Garcia },{ 42688a0fc18dSAlberto Garcia .name = "throttling.iops-write-max-length", 42698a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42708a0fc18dSAlberto Garcia .help = "length of the iops-write-max burst period, in seconds", 42718a0fc18dSAlberto Garcia },{ 42728a0fc18dSAlberto Garcia .name = "throttling.bps-total-max-length", 42738a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42748a0fc18dSAlberto Garcia .help = "length of the bps-total-max burst period, in seconds", 42758a0fc18dSAlberto Garcia },{ 42768a0fc18dSAlberto Garcia .name = "throttling.bps-read-max-length", 42778a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42788a0fc18dSAlberto Garcia .help = "length of the bps-read-max burst period, in seconds", 42798a0fc18dSAlberto Garcia },{ 42808a0fc18dSAlberto Garcia .name = "throttling.bps-write-max-length", 42818a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42828a0fc18dSAlberto Garcia .help = "length of the bps-write-max burst period, in seconds", 42838a0fc18dSAlberto Garcia },{ 42842024c1dfSBenoît Canet .name = "throttling.iops-size", 42852024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 42862024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 42872024c1dfSBenoît Canet },{ 428876f4afb4SAlberto Garcia .name = "throttling.group", 428976f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 429076f4afb4SAlberto Garcia .help = "name of the block throttling group", 429176f4afb4SAlberto Garcia },{ 42924d454574SPaolo Bonzini .name = "copy-on-read", 42934d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 42944d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4295465bee1dSPeter Lieven },{ 4296465bee1dSPeter Lieven .name = "detect-zeroes", 4297465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4298465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4299362e9299SAlberto Garcia },{ 4300362e9299SAlberto Garcia .name = "stats-account-invalid", 4301362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4302362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4303362e9299SAlberto Garcia "in the statistics", 4304362e9299SAlberto Garcia },{ 4305362e9299SAlberto Garcia .name = "stats-account-failed", 4306362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4307362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4308362e9299SAlberto Garcia "in the statistics", 43094d454574SPaolo Bonzini }, 43104d454574SPaolo Bonzini { /* end of list */ } 43114d454574SPaolo Bonzini }, 43124d454574SPaolo Bonzini }; 43130006383eSKevin Wolf 4314bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 4315bd745e23SMax Reitz .name = "root-bds", 431623f7fcb2SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head), 4317bd745e23SMax Reitz .desc = { 4318bd745e23SMax Reitz { 4319bd745e23SMax Reitz .name = "discard", 4320bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4321bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 4322bd745e23SMax Reitz },{ 4323bd745e23SMax Reitz .name = "aio", 4324bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4325bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 4326bd745e23SMax Reitz },{ 4327bd745e23SMax Reitz .name = "read-only", 4328bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4329bd745e23SMax Reitz .help = "open drive file as read-only", 4330bd745e23SMax Reitz },{ 4331bd745e23SMax Reitz .name = "copy-on-read", 4332bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4333bd745e23SMax Reitz .help = "copy read data from backing file into image file", 4334bd745e23SMax Reitz },{ 4335bd745e23SMax Reitz .name = "detect-zeroes", 4336bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4337bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 4338bd745e23SMax Reitz }, 4339bd745e23SMax Reitz { /* end of list */ } 4340bd745e23SMax Reitz }, 4341bd745e23SMax Reitz }; 4342bd745e23SMax Reitz 43430006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 43440006383eSKevin Wolf .name = "drive", 43450006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 43460006383eSKevin Wolf .desc = { 4347492fdc6fSKevin Wolf /* 4348492fdc6fSKevin Wolf * no elements => accept any params 4349492fdc6fSKevin Wolf * validation will happen later 4350492fdc6fSKevin Wolf */ 43510006383eSKevin Wolf { /* end of list */ } 43520006383eSKevin Wolf }, 43530006383eSKevin Wolf }; 4354