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 515ff356ee4SAlberto Garcia id = qemu_opts_id(opts); 516ff356ee4SAlberto Garcia 51740119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51840119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51940119effSAlberto Garcia 52040119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 52140119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 52240119effSAlberto Garcia qdict_first(interval_dict)->key); 52340119effSAlberto Garcia goto early_err; 52440119effSAlberto Garcia } 5252be5506fSAlberto Garcia 526fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 527fbf8175eSMax Reitz &detect_zeroes, &error); 528fbf8175eSMax Reitz if (error) { 529fbf8175eSMax Reitz error_propagate(errp, error); 530ec9c10d2SStefan Hajnoczi goto early_err; 531a9384affSPaolo Bonzini } 532666daa68SMarkus Armbruster 533666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 534c8057f95SPeter Maydell if (is_help_option(buf)) { 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) { 621ff356ee4SAlberto 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 630ff356ee4SAlberto 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 1177b6c1bae5SKevin Wolf static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) 1178b6c1bae5SKevin Wolf { 1179b6c1bae5SKevin Wolf BlockDriverState *bs; 1180b6c1bae5SKevin Wolf 1181b6c1bae5SKevin Wolf bs = bdrv_lookup_bs(name, name, errp); 1182b6c1bae5SKevin Wolf if (bs == NULL) { 1183b6c1bae5SKevin Wolf return NULL; 1184b6c1bae5SKevin Wolf } 1185b6c1bae5SKevin Wolf 1186b6c1bae5SKevin Wolf if (!bdrv_is_root_node(bs)) { 1187b6c1bae5SKevin Wolf error_setg(errp, "Need a root block node"); 1188b6c1bae5SKevin Wolf return NULL; 1189b6c1bae5SKevin Wolf } 1190b6c1bae5SKevin Wolf 1191b6c1bae5SKevin Wolf if (!bdrv_is_inserted(bs)) { 1192b6c1bae5SKevin Wolf error_setg(errp, "Device has no medium"); 1193b6c1bae5SKevin Wolf return NULL; 1194b6c1bae5SKevin Wolf } 1195b6c1bae5SKevin Wolf 1196b6c1bae5SKevin Wolf return bs; 1197b6c1bae5SKevin Wolf } 1198b6c1bae5SKevin Wolf 11993e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1200666daa68SMarkus Armbruster { 1201666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1202a0e8544cSFam Zheng BlockBackend *blk; 12032d3735d3SStefan Hajnoczi int ret; 12042d3735d3SStefan Hajnoczi 1205e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1206da31d594SMax Reitz ret = blk_commit_all(); 1207e8877497SStefan Hajnoczi } else { 120884aa0140SStefan Hajnoczi BlockDriverState *bs; 120984aa0140SStefan Hajnoczi AioContext *aio_context; 121084aa0140SStefan Hajnoczi 1211a0e8544cSFam Zheng blk = blk_by_name(device); 1212a0e8544cSFam Zheng if (!blk) { 121358513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1214ac59eb95SMarkus Armbruster return; 12156ab4b5abSMarkus Armbruster } 12165433c24fSMax Reitz if (!blk_is_available(blk)) { 12175433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 12185433c24fSMax Reitz return; 12195433c24fSMax Reitz } 122084aa0140SStefan Hajnoczi 122184aa0140SStefan Hajnoczi bs = blk_bs(blk); 122284aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 122384aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 122484aa0140SStefan Hajnoczi 122584aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 122684aa0140SStefan Hajnoczi 122784aa0140SStefan Hajnoczi aio_context_release(aio_context); 12282d3735d3SStefan Hajnoczi } 122958513bdeSJeff Cody if (ret < 0) { 123058513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 123158513bdeSJeff Cody strerror(-ret)); 1232666daa68SMarkus Armbruster } 1233666daa68SMarkus Armbruster } 1234666daa68SMarkus Armbruster 123510f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 12366cc2a415SPaolo Bonzini { 1237c8a83e85SKevin Wolf TransactionActionList list; 12386cc2a415SPaolo Bonzini 123910f75907SEric Blake list.value = action; 12406cc2a415SPaolo Bonzini list.next = NULL; 124194d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 12426cc2a415SPaolo Bonzini } 12436cc2a415SPaolo Bonzini 12440901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 12450901f67eSBenoît Canet bool has_node_name, const char *node_name, 12460901f67eSBenoît Canet const char *snapshot_file, 12470901f67eSBenoît Canet bool has_snapshot_node_name, 12480901f67eSBenoît Canet const char *snapshot_node_name, 12496106e249SLuiz Capitulino bool has_format, const char *format, 12500901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1251f8882568SJes Sorensen { 1252a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 12530901f67eSBenoît Canet .has_device = has_device, 12546cc2a415SPaolo Bonzini .device = (char *) device, 12550901f67eSBenoît Canet .has_node_name = has_node_name, 12560901f67eSBenoît Canet .node_name = (char *) node_name, 12576cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 12580901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 12590901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 12606cc2a415SPaolo Bonzini .has_format = has_format, 12616cc2a415SPaolo Bonzini .format = (char *) format, 12626cc2a415SPaolo Bonzini .has_mode = has_mode, 12636cc2a415SPaolo Bonzini .mode = mode, 12646cc2a415SPaolo Bonzini }; 126510f75907SEric Blake TransactionAction action = { 126610f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 126732bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 126810f75907SEric Blake }; 126910f75907SEric Blake blockdev_do_action(&action, errp); 1270f8882568SJes Sorensen } 1271f8882568SJes Sorensen 127243de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 127343de7e2dSAlberto Garcia Error **errp) 127443de7e2dSAlberto Garcia { 127543de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 127643de7e2dSAlberto Garcia .node = (char *) node, 127743de7e2dSAlberto Garcia .overlay = (char *) overlay 127843de7e2dSAlberto Garcia }; 127910f75907SEric Blake TransactionAction action = { 128010f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 128132bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 128210f75907SEric Blake }; 128310f75907SEric Blake blockdev_do_action(&action, errp); 128443de7e2dSAlberto Garcia } 128543de7e2dSAlberto Garcia 1286f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1287f323bc9eSWenchao Xia const char *name, 1288f323bc9eSWenchao Xia Error **errp) 1289f323bc9eSWenchao Xia { 1290f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1291f323bc9eSWenchao Xia .device = (char *) device, 1292f323bc9eSWenchao Xia .name = (char *) name 1293f323bc9eSWenchao Xia }; 129410f75907SEric Blake TransactionAction action = { 129510f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 129632bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 129710f75907SEric Blake }; 129810f75907SEric Blake blockdev_do_action(&action, errp); 1299f323bc9eSWenchao Xia } 1300f323bc9eSWenchao Xia 130144e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 130244e3e053SWenchao Xia bool has_id, 130344e3e053SWenchao Xia const char *id, 130444e3e053SWenchao Xia bool has_name, 130544e3e053SWenchao Xia const char *name, 130644e3e053SWenchao Xia Error **errp) 130744e3e053SWenchao Xia { 1308a0e8544cSFam Zheng BlockDriverState *bs; 13094ef3982aSStefan Hajnoczi AioContext *aio_context; 131044e3e053SWenchao Xia QEMUSnapshotInfo sn; 131144e3e053SWenchao Xia Error *local_err = NULL; 131244e3e053SWenchao Xia SnapshotInfo *info = NULL; 131344e3e053SWenchao Xia int ret; 131444e3e053SWenchao Xia 13152dfb4c03SKevin Wolf bs = qmp_get_root_bs(device, errp); 13162dfb4c03SKevin Wolf if (!bs) { 131744e3e053SWenchao Xia return NULL; 131844e3e053SWenchao Xia } 13192dfb4c03SKevin Wolf aio_context = bdrv_get_aio_context(bs); 13205433c24fSMax Reitz aio_context_acquire(aio_context); 132144e3e053SWenchao Xia 132244e3e053SWenchao Xia if (!has_id) { 132344e3e053SWenchao Xia id = NULL; 132444e3e053SWenchao Xia } 132544e3e053SWenchao Xia 132644e3e053SWenchao Xia if (!has_name) { 132744e3e053SWenchao Xia name = NULL; 132844e3e053SWenchao Xia } 132944e3e053SWenchao Xia 133044e3e053SWenchao Xia if (!id && !name) { 133144e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 13325433c24fSMax Reitz goto out_aio_context; 133344e3e053SWenchao Xia } 133444e3e053SWenchao Xia 13350b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 13360b928854SStefan Hajnoczi goto out_aio_context; 13370b928854SStefan Hajnoczi } 13380b928854SStefan Hajnoczi 133944e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 134084d18f06SMarkus Armbruster if (local_err) { 134144e3e053SWenchao Xia error_propagate(errp, local_err); 13424ef3982aSStefan Hajnoczi goto out_aio_context; 134344e3e053SWenchao Xia } 134444e3e053SWenchao Xia if (!ret) { 134544e3e053SWenchao Xia error_setg(errp, 134644e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 134744e3e053SWenchao Xia "device '%s'", 134844e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 13494ef3982aSStefan Hajnoczi goto out_aio_context; 135044e3e053SWenchao Xia } 135144e3e053SWenchao Xia 135244e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 135384d18f06SMarkus Armbruster if (local_err) { 135444e3e053SWenchao Xia error_propagate(errp, local_err); 13554ef3982aSStefan Hajnoczi goto out_aio_context; 135644e3e053SWenchao Xia } 135744e3e053SWenchao Xia 13584ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13594ef3982aSStefan Hajnoczi 13605839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 136144e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 136244e3e053SWenchao Xia info->name = g_strdup(sn.name); 136344e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 136444e3e053SWenchao Xia info->date_sec = sn.date_sec; 136544e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 136644e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 136744e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 136844e3e053SWenchao Xia 136944e3e053SWenchao Xia return info; 13704ef3982aSStefan Hajnoczi 13714ef3982aSStefan Hajnoczi out_aio_context: 13724ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13734ef3982aSStefan Hajnoczi return NULL; 137444e3e053SWenchao Xia } 13758802d1fdSJeff Cody 1376341ebc2fSJohn Snow /** 1377341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1378341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1379341ebc2fSJohn Snow * the node reference and bitmap names. 1380341ebc2fSJohn Snow * 1381341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1382341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1383341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1384341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1385341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1386341ebc2fSJohn Snow * 1387341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1388341ebc2fSJohn Snow */ 1389341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1390341ebc2fSJohn Snow const char *name, 1391341ebc2fSJohn Snow BlockDriverState **pbs, 1392341ebc2fSJohn Snow AioContext **paio, 1393341ebc2fSJohn Snow Error **errp) 1394341ebc2fSJohn Snow { 1395341ebc2fSJohn Snow BlockDriverState *bs; 1396341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1397341ebc2fSJohn Snow AioContext *aio_context; 1398341ebc2fSJohn Snow 1399341ebc2fSJohn Snow if (!node) { 1400341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1401341ebc2fSJohn Snow return NULL; 1402341ebc2fSJohn Snow } 1403341ebc2fSJohn Snow if (!name) { 1404341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1405341ebc2fSJohn Snow return NULL; 1406341ebc2fSJohn Snow } 1407341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1408341ebc2fSJohn Snow if (!bs) { 1409341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1410341ebc2fSJohn Snow return NULL; 1411341ebc2fSJohn Snow } 1412341ebc2fSJohn Snow 1413341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 1414341ebc2fSJohn Snow aio_context_acquire(aio_context); 1415341ebc2fSJohn Snow 1416341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1417341ebc2fSJohn Snow if (!bitmap) { 1418341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 1419341ebc2fSJohn Snow goto fail; 1420341ebc2fSJohn Snow } 1421341ebc2fSJohn Snow 1422341ebc2fSJohn Snow if (pbs) { 1423341ebc2fSJohn Snow *pbs = bs; 1424341ebc2fSJohn Snow } 1425341ebc2fSJohn Snow if (paio) { 1426341ebc2fSJohn Snow *paio = aio_context; 1427341ebc2fSJohn Snow } else { 1428341ebc2fSJohn Snow aio_context_release(aio_context); 1429341ebc2fSJohn Snow } 1430341ebc2fSJohn Snow 1431341ebc2fSJohn Snow return bitmap; 1432341ebc2fSJohn Snow 1433341ebc2fSJohn Snow fail: 1434341ebc2fSJohn Snow aio_context_release(aio_context); 1435341ebc2fSJohn Snow return NULL; 1436341ebc2fSJohn Snow } 1437341ebc2fSJohn Snow 1438b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1439ba0c86a3SWenchao Xia 144050f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1441ba0c86a3SWenchao Xia 144250f43f0fSJohn Snow /** 144350f43f0fSJohn Snow * BlkActionOps: 144450f43f0fSJohn Snow * Table of operations that define an Action. 144550f43f0fSJohn Snow * 144650f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 144750f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 144850f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 144950f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 145050f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 145150f43f0fSJohn Snow * commit() or abort(). Can be NULL. 145250f43f0fSJohn Snow * 145350f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 145450f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1455ba0c86a3SWenchao Xia */ 145650f43f0fSJohn Snow typedef struct BlkActionOps { 145750f43f0fSJohn Snow size_t instance_size; 145850f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 145950f43f0fSJohn Snow void (*commit)(BlkActionState *common); 146050f43f0fSJohn Snow void (*abort)(BlkActionState *common); 146150f43f0fSJohn Snow void (*clean)(BlkActionState *common); 146250f43f0fSJohn Snow } BlkActionOps; 146350f43f0fSJohn Snow 146450f43f0fSJohn Snow /** 146550f43f0fSJohn Snow * BlkActionState: 146650f43f0fSJohn Snow * Describes one Action's state within a Transaction. 146750f43f0fSJohn Snow * 146850f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 146950f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 147094d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 147150f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 147250f43f0fSJohn Snow * 147350f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 147450f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 147550f43f0fSJohn Snow * base class. 147650f43f0fSJohn Snow */ 147750f43f0fSJohn Snow struct BlkActionState { 1478c8a83e85SKevin Wolf TransactionAction *action; 147950f43f0fSJohn Snow const BlkActionOps *ops; 148094d16a64SJohn Snow BlockJobTxn *block_job_txn; 148194d16a64SJohn Snow TransactionProperties *txn_props; 148250f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1483ba0c86a3SWenchao Xia }; 1484ba0c86a3SWenchao Xia 1485bbe86010SWenchao Xia /* internal snapshot private data */ 1486bbe86010SWenchao Xia typedef struct InternalSnapshotState { 148750f43f0fSJohn Snow BlkActionState common; 1488bbe86010SWenchao Xia BlockDriverState *bs; 14895d6e96efSStefan Hajnoczi AioContext *aio_context; 1490bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1491507306ccSFam Zheng bool created; 1492bbe86010SWenchao Xia } InternalSnapshotState; 1493bbe86010SWenchao Xia 149494d16a64SJohn Snow 149594d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 149694d16a64SJohn Snow { 149794d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 149894d16a64SJohn Snow error_setg(errp, 149994d16a64SJohn Snow "Action '%s' does not support Transaction property " 150094d16a64SJohn Snow "completion-mode = %s", 150194d16a64SJohn Snow TransactionActionKind_lookup[s->action->type], 150294d16a64SJohn Snow ActionCompletionMode_lookup[s->txn_props->completion_mode]); 150394d16a64SJohn Snow return -1; 150494d16a64SJohn Snow } 150594d16a64SJohn Snow return 0; 150694d16a64SJohn Snow } 150794d16a64SJohn Snow 150850f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1509bbe86010SWenchao Xia Error **errp) 1510bbe86010SWenchao Xia { 1511f70edf99SMarkus Armbruster Error *local_err = NULL; 1512bbe86010SWenchao Xia const char *device; 1513bbe86010SWenchao Xia const char *name; 1514bbe86010SWenchao Xia BlockDriverState *bs; 1515bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1516bbe86010SWenchao Xia bool ret; 1517bbe86010SWenchao Xia qemu_timeval tv; 1518bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1519bbe86010SWenchao Xia InternalSnapshotState *state; 1520bbe86010SWenchao Xia int ret1; 1521bbe86010SWenchao Xia 15226a8f9661SEric Blake g_assert(common->action->type == 1523bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 152432bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1525bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1526bbe86010SWenchao Xia 1527bbe86010SWenchao Xia /* 1. parse input */ 1528bbe86010SWenchao Xia device = internal->device; 1529bbe86010SWenchao Xia name = internal->name; 1530bbe86010SWenchao Xia 1531bbe86010SWenchao Xia /* 2. check for validation */ 153294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 153394d16a64SJohn Snow return; 153494d16a64SJohn Snow } 153594d16a64SJohn Snow 1536*75dfd402SKevin Wolf bs = qmp_get_root_bs(device, errp); 1537*75dfd402SKevin Wolf if (!bs) { 1538bbe86010SWenchao Xia return; 1539bbe86010SWenchao Xia } 1540bbe86010SWenchao Xia 15415d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 1542*75dfd402SKevin Wolf state->aio_context = bdrv_get_aio_context(bs); 15435d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 15445d6e96efSStefan Hajnoczi 1545507306ccSFam Zheng state->bs = bs; 1546507306ccSFam Zheng bdrv_drained_begin(bs); 1547507306ccSFam Zheng 15483dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 15493dc7ca3cSStefan Hajnoczi return; 15503dc7ca3cSStefan Hajnoczi } 15513dc7ca3cSStefan Hajnoczi 1552bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 155381e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1554bbe86010SWenchao Xia return; 1555bbe86010SWenchao Xia } 1556bbe86010SWenchao Xia 1557bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 155881e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 155981e5f78aSAlberto Garcia "does not support internal snapshots", 156081e5f78aSAlberto Garcia bs->drv->format_name, device); 1561bbe86010SWenchao Xia return; 1562bbe86010SWenchao Xia } 1563bbe86010SWenchao Xia 1564bbe86010SWenchao Xia if (!strlen(name)) { 1565bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1566bbe86010SWenchao Xia return; 1567bbe86010SWenchao Xia } 1568bbe86010SWenchao Xia 1569bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1570f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1571f70edf99SMarkus Armbruster &local_err); 1572f70edf99SMarkus Armbruster if (local_err) { 1573f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1574bbe86010SWenchao Xia return; 1575bbe86010SWenchao Xia } else if (ret) { 1576bbe86010SWenchao Xia error_setg(errp, 1577bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1578bbe86010SWenchao Xia name, device); 1579bbe86010SWenchao Xia return; 1580bbe86010SWenchao Xia } 1581bbe86010SWenchao Xia 1582bbe86010SWenchao Xia /* 3. take the snapshot */ 1583bbe86010SWenchao Xia sn = &state->sn; 1584bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1585bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1586bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1587bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1588bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1589bbe86010SWenchao Xia 1590bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1591bbe86010SWenchao Xia if (ret1 < 0) { 1592bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1593bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1594bbe86010SWenchao Xia name, device); 1595bbe86010SWenchao Xia return; 1596bbe86010SWenchao Xia } 1597bbe86010SWenchao Xia 1598bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1599507306ccSFam Zheng state->created = true; 1600bbe86010SWenchao Xia } 1601bbe86010SWenchao Xia 160250f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1603bbe86010SWenchao Xia { 1604bbe86010SWenchao Xia InternalSnapshotState *state = 1605bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1606bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1607bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1608bbe86010SWenchao Xia Error *local_error = NULL; 1609bbe86010SWenchao Xia 1610507306ccSFam Zheng if (!state->created) { 1611bbe86010SWenchao Xia return; 1612bbe86010SWenchao Xia } 1613bbe86010SWenchao Xia 1614bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1615c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1616c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1617c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1618c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1619c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1620bbe86010SWenchao Xia } 1621bbe86010SWenchao Xia } 1622bbe86010SWenchao Xia 162350f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 16245d6e96efSStefan Hajnoczi { 16255d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 16265d6e96efSStefan Hajnoczi common, common); 16275d6e96efSStefan Hajnoczi 16285d6e96efSStefan Hajnoczi if (state->aio_context) { 1629507306ccSFam Zheng if (state->bs) { 1630507306ccSFam Zheng bdrv_drained_end(state->bs); 1631507306ccSFam Zheng } 16325d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16335d6e96efSStefan Hajnoczi } 16345d6e96efSStefan Hajnoczi } 16355d6e96efSStefan Hajnoczi 1636ba0c86a3SWenchao Xia /* external snapshot private data */ 1637ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 163850f43f0fSJohn Snow BlkActionState common; 16398802d1fdSJeff Cody BlockDriverState *old_bs; 16408802d1fdSJeff Cody BlockDriverState *new_bs; 16415d6e96efSStefan Hajnoczi AioContext *aio_context; 1642ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16438802d1fdSJeff Cody 164450f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16459b9877eeSWenchao Xia Error **errp) 16469b9877eeSWenchao Xia { 16475b363937SMax Reitz int flags = 0; 164843de7e2dSAlberto Garcia QDict *options = NULL; 16499b9877eeSWenchao Xia Error *local_err = NULL; 165043de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1651e2a31e87SWenchao Xia const char *device; 16520901f67eSBenoît Canet const char *node_name; 165343de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 165443de7e2dSAlberto Garcia const char *snapshot_ref; 165543de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1656e2a31e87SWenchao Xia const char *new_image_file; 1657ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1658ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1659c8a83e85SKevin Wolf TransactionAction *action = common->action; 16609b9877eeSWenchao Xia 166143de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 166243de7e2dSAlberto Garcia * purpose but a different set of parameters */ 166343de7e2dSAlberto Garcia switch (action->type) { 166443de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 166543de7e2dSAlberto Garcia { 166632bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 166743de7e2dSAlberto Garcia device = s->node; 166843de7e2dSAlberto Garcia node_name = s->node; 166943de7e2dSAlberto Garcia new_image_file = NULL; 167043de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1671e2a31e87SWenchao Xia } 167243de7e2dSAlberto Garcia break; 167343de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 167443de7e2dSAlberto Garcia { 167532bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 167643de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 167743de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 167843de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 167943de7e2dSAlberto Garcia snapshot_ref = NULL; 168043de7e2dSAlberto Garcia } 168143de7e2dSAlberto Garcia break; 168243de7e2dSAlberto Garcia default: 168343de7e2dSAlberto Garcia g_assert_not_reached(); 1684e2a31e87SWenchao Xia } 1685e2a31e87SWenchao Xia 1686e2a31e87SWenchao Xia /* start processing */ 168794d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 168894d16a64SJohn Snow return; 168994d16a64SJohn Snow } 169094d16a64SJohn Snow 169143de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 169243de7e2dSAlberto Garcia if (!state->old_bs) { 16939b9877eeSWenchao Xia return; 16949b9877eeSWenchao Xia } 16959b9877eeSWenchao Xia 16965d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 16975d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 16985d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1699da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 17005d6e96efSStefan Hajnoczi 1701ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1702c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 17039b9877eeSWenchao Xia return; 17049b9877eeSWenchao Xia } 17059b9877eeSWenchao Xia 17063718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 17073718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 17089b9877eeSWenchao Xia return; 17099b9877eeSWenchao Xia } 17109b9877eeSWenchao Xia 1711ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1712ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1713c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 17149b9877eeSWenchao Xia return; 17159b9877eeSWenchao Xia } 17169b9877eeSWenchao Xia } 17179b9877eeSWenchao Xia 1718212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1719c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1720f6186f49SBenoît Canet return; 1721f6186f49SBenoît Canet } 1722f6186f49SBenoît Canet 172343de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 172432bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 172543de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 172643de7e2dSAlberto Garcia enum NewImageMode mode; 172743de7e2dSAlberto Garcia const char *snapshot_node_name = 172843de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 172943de7e2dSAlberto Garcia 173043de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 173143de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 173243de7e2dSAlberto Garcia return; 173343de7e2dSAlberto Garcia } 173443de7e2dSAlberto Garcia 173543de7e2dSAlberto Garcia if (snapshot_node_name && 173643de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 173743de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 173843de7e2dSAlberto Garcia return; 173943de7e2dSAlberto Garcia } 174043de7e2dSAlberto Garcia 1741ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17424c844983SKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); 17439b9877eeSWenchao Xia 17449b9877eeSWenchao Xia /* create new image w/backing file */ 174543de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17469b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1747f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1748f86b8b58SKevin Wolf if (size < 0) { 1749f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 1750f86b8b58SKevin Wolf return; 1751f86b8b58SKevin Wolf } 17529b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1753ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1754ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 1755f86b8b58SKevin Wolf NULL, size, flags, &local_err, false); 175684d18f06SMarkus Armbruster if (local_err) { 17579b9877eeSWenchao Xia error_propagate(errp, local_err); 17589b9877eeSWenchao Xia return; 17599b9877eeSWenchao Xia } 17609b9877eeSWenchao Xia } 17619b9877eeSWenchao Xia 17620901f67eSBenoît Canet options = qdict_new(); 176343de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 17640901f67eSBenoît Canet qdict_put(options, "node-name", 17650901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 17660901f67eSBenoît Canet } 1767e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 17680901f67eSBenoît Canet 176943de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 177043de7e2dSAlberto Garcia } 177143de7e2dSAlberto Garcia 17725b363937SMax Reitz state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, 17735b363937SMax Reitz errp); 1774f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17755b363937SMax Reitz if (!state->new_bs) { 177643de7e2dSAlberto Garcia return; 177743de7e2dSAlberto Garcia } 177843de7e2dSAlberto Garcia 17791f0c461bSKevin Wolf if (bdrv_has_blk(state->new_bs)) { 178043de7e2dSAlberto Garcia error_setg(errp, "The snapshot is already in use by %s", 17811f0c461bSKevin Wolf bdrv_get_parent_name(state->new_bs)); 178243de7e2dSAlberto Garcia return; 178343de7e2dSAlberto Garcia } 178443de7e2dSAlberto Garcia 178543de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 178643de7e2dSAlberto Garcia errp)) { 178743de7e2dSAlberto Garcia return; 178843de7e2dSAlberto Garcia } 178943de7e2dSAlberto Garcia 179043de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 179143de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 179208b24cfeSAlberto Garcia return; 179308b24cfeSAlberto Garcia } 179408b24cfeSAlberto Garcia 179508b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 179608b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 17979b9877eeSWenchao Xia } 17989b9877eeSWenchao Xia } 17999b9877eeSWenchao Xia 180050f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 18013b0047e8SWenchao Xia { 1802ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1803ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1804ba0c86a3SWenchao Xia 18055d6e96efSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, state->aio_context); 18065d6e96efSStefan Hajnoczi 1807ba5d6ab6SStefan Hajnoczi /* This removes our old bs and adds the new bs */ 1808ba5d6ab6SStefan Hajnoczi bdrv_append(state->new_bs, state->old_bs); 18093b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 18103b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 18113b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 18124c844983SKevin Wolf if (!state->old_bs->copy_on_read) { 1813dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 18143b0047e8SWenchao Xia NULL); 18153b0047e8SWenchao Xia } 18164c844983SKevin Wolf } 18173b0047e8SWenchao Xia 181850f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 181996b86bf7SWenchao Xia { 1820ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1821ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1822ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 18234f6fd349SFam Zheng bdrv_unref(state->new_bs); 182496b86bf7SWenchao Xia } 1825da763e83SFam Zheng } 1826da763e83SFam Zheng 182750f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1828da763e83SFam Zheng { 1829da763e83SFam Zheng ExternalSnapshotState *state = 1830da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 18315d6e96efSStefan Hajnoczi if (state->aio_context) { 1832da763e83SFam Zheng bdrv_drained_end(state->old_bs); 18335d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18345d6e96efSStefan Hajnoczi } 183596b86bf7SWenchao Xia } 183696b86bf7SWenchao Xia 18373037f364SStefan Hajnoczi typedef struct DriveBackupState { 183850f43f0fSJohn Snow BlkActionState common; 18393037f364SStefan Hajnoczi BlockDriverState *bs; 18405d6e96efSStefan Hajnoczi AioContext *aio_context; 18413037f364SStefan Hajnoczi BlockJob *job; 18423037f364SStefan Hajnoczi } DriveBackupState; 18433037f364SStefan Hajnoczi 184470559d49SAlberto Garcia static void do_drive_backup(const char *job_id, const char *device, 184570559d49SAlberto Garcia const char *target, bool has_format, 184670559d49SAlberto Garcia const char *format, enum MirrorSyncMode sync, 184778f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 184878f51fdeSJohn Snow bool has_speed, int64_t speed, 184978f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 185078f51fdeSJohn Snow bool has_on_source_error, 185178f51fdeSJohn Snow BlockdevOnError on_source_error, 185278f51fdeSJohn Snow bool has_on_target_error, 185378f51fdeSJohn Snow BlockdevOnError on_target_error, 185478f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 185578f51fdeSJohn Snow 185650f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18573037f364SStefan Hajnoczi { 18583037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1859a0e8544cSFam Zheng BlockBackend *blk; 18603037f364SStefan Hajnoczi DriveBackup *backup; 18613037f364SStefan Hajnoczi Error *local_err = NULL; 18623037f364SStefan Hajnoczi 18636a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 186432bafa8fSEric Blake backup = common->action->u.drive_backup.data; 18653037f364SStefan Hajnoczi 1866a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1867a0e8544cSFam Zheng if (!blk) { 186875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 186975158ebbSMarkus Armbruster "Device '%s' not found", backup->device); 18705d6e96efSStefan Hajnoczi return; 18715d6e96efSStefan Hajnoczi } 18725d6e96efSStefan Hajnoczi 18731fdd4b7bSFam Zheng if (!blk_is_available(blk)) { 18741fdd4b7bSFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 18751fdd4b7bSFam Zheng return; 18761fdd4b7bSFam Zheng } 18771fdd4b7bSFam Zheng 18785d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 18795433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 18805d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 18811fdd4b7bSFam Zheng bdrv_drained_begin(blk_bs(blk)); 18821fdd4b7bSFam Zheng state->bs = blk_bs(blk); 18835d6e96efSStefan Hajnoczi 188470559d49SAlberto Garcia do_drive_backup(backup->has_job_id ? backup->job_id : NULL, 188570559d49SAlberto Garcia backup->device, backup->target, 18863037f364SStefan Hajnoczi backup->has_format, backup->format, 1887b53169eaSStefan Hajnoczi backup->sync, 18883037f364SStefan Hajnoczi backup->has_mode, backup->mode, 18893037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1890d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 18913037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 18923037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 189394d16a64SJohn Snow common->block_job_txn, &local_err); 189484d18f06SMarkus Armbruster if (local_err) { 18953037f364SStefan Hajnoczi error_propagate(errp, local_err); 18963037f364SStefan Hajnoczi return; 18973037f364SStefan Hajnoczi } 18983037f364SStefan Hajnoczi 18993037f364SStefan Hajnoczi state->job = state->bs->job; 19003037f364SStefan Hajnoczi } 19013037f364SStefan Hajnoczi 190250f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 19033037f364SStefan Hajnoczi { 19043037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19053037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 19063037f364SStefan Hajnoczi 19073037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 19083037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 19093037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 19103037f364SStefan Hajnoczi } 19113037f364SStefan Hajnoczi } 19123037f364SStefan Hajnoczi 191350f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 19145d6e96efSStefan Hajnoczi { 19155d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19165d6e96efSStefan Hajnoczi 19175d6e96efSStefan Hajnoczi if (state->aio_context) { 19181fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 19195d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 19205d6e96efSStefan Hajnoczi } 19215d6e96efSStefan Hajnoczi } 19225d6e96efSStefan Hajnoczi 1923bd8baecdSFam Zheng typedef struct BlockdevBackupState { 192450f43f0fSJohn Snow BlkActionState common; 1925bd8baecdSFam Zheng BlockDriverState *bs; 1926bd8baecdSFam Zheng BlockJob *job; 1927bd8baecdSFam Zheng AioContext *aio_context; 1928bd8baecdSFam Zheng } BlockdevBackupState; 1929bd8baecdSFam Zheng 193070559d49SAlberto Garcia static void do_blockdev_backup(const char *job_id, const char *device, 193170559d49SAlberto Garcia const char *target, enum MirrorSyncMode sync, 193278f51fdeSJohn Snow bool has_speed, int64_t speed, 193378f51fdeSJohn Snow bool has_on_source_error, 193478f51fdeSJohn Snow BlockdevOnError on_source_error, 193578f51fdeSJohn Snow bool has_on_target_error, 193678f51fdeSJohn Snow BlockdevOnError on_target_error, 193778f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 193878f51fdeSJohn Snow 193950f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1940bd8baecdSFam Zheng { 1941bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1942bd8baecdSFam Zheng BlockdevBackup *backup; 1943cef34eebSKevin Wolf BlockDriverState *bs, *target; 1944bd8baecdSFam Zheng Error *local_err = NULL; 1945bd8baecdSFam Zheng 19466a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 194732bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1948bd8baecdSFam Zheng 1949cef34eebSKevin Wolf bs = qmp_get_root_bs(backup->device, errp); 1950cef34eebSKevin Wolf if (!bs) { 1951ff52bf36SFam Zheng return; 1952ff52bf36SFam Zheng } 1953ff52bf36SFam Zheng 195439d990acSKevin Wolf target = bdrv_lookup_bs(backup->target, backup->target, errp); 19555433c24fSMax Reitz if (!target) { 1956bd8baecdSFam Zheng return; 1957bd8baecdSFam Zheng } 1958bd8baecdSFam Zheng 1959bd8baecdSFam Zheng /* AioContext is released in .clean() */ 1960cef34eebSKevin Wolf state->aio_context = bdrv_get_aio_context(bs); 196139d990acSKevin Wolf if (state->aio_context != bdrv_get_aio_context(target)) { 1962bd8baecdSFam Zheng state->aio_context = NULL; 1963bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1964bd8baecdSFam Zheng return; 1965bd8baecdSFam Zheng } 1966bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1967cef34eebSKevin Wolf state->bs = bs; 1968ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1969bd8baecdSFam Zheng 197070559d49SAlberto Garcia do_blockdev_backup(backup->has_job_id ? backup->job_id : NULL, 197170559d49SAlberto Garcia backup->device, backup->target, backup->sync, 1972bd8baecdSFam Zheng backup->has_speed, backup->speed, 1973bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1974bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 197594d16a64SJohn Snow common->block_job_txn, &local_err); 1976bd8baecdSFam Zheng if (local_err) { 1977bd8baecdSFam Zheng error_propagate(errp, local_err); 1978bd8baecdSFam Zheng return; 1979bd8baecdSFam Zheng } 1980bd8baecdSFam Zheng 1981bd8baecdSFam Zheng state->job = state->bs->job; 1982bd8baecdSFam Zheng } 1983bd8baecdSFam Zheng 198450f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1985bd8baecdSFam Zheng { 1986bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1987bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1988bd8baecdSFam Zheng 1989bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 1990bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 1991bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 1992bd8baecdSFam Zheng } 1993bd8baecdSFam Zheng } 1994bd8baecdSFam Zheng 199550f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 1996bd8baecdSFam Zheng { 1997bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1998bd8baecdSFam Zheng 1999bd8baecdSFam Zheng if (state->aio_context) { 2000ff52bf36SFam Zheng bdrv_drained_end(state->bs); 2001bd8baecdSFam Zheng aio_context_release(state->aio_context); 2002bd8baecdSFam Zheng } 2003bd8baecdSFam Zheng } 2004bd8baecdSFam Zheng 2005df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 200650f43f0fSJohn Snow BlkActionState common; 2007df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 2008df9a681dSFam Zheng BlockDriverState *bs; 2009df9a681dSFam Zheng AioContext *aio_context; 2010df9a681dSFam Zheng HBitmap *backup; 2011df9a681dSFam Zheng bool prepared; 2012df9a681dSFam Zheng } BlockDirtyBitmapState; 2013df9a681dSFam Zheng 201450f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 2015df9a681dSFam Zheng Error **errp) 2016df9a681dSFam Zheng { 2017df9a681dSFam Zheng Error *local_err = NULL; 2018df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2019df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2020df9a681dSFam Zheng common, common); 2021df9a681dSFam Zheng 202294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 202394d16a64SJohn Snow return; 202494d16a64SJohn Snow } 202594d16a64SJohn Snow 202632bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2027df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2028df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2029df9a681dSFam Zheng action->has_granularity, action->granularity, 2030df9a681dSFam Zheng &local_err); 2031df9a681dSFam Zheng 2032df9a681dSFam Zheng if (!local_err) { 2033df9a681dSFam Zheng state->prepared = true; 2034df9a681dSFam Zheng } else { 2035df9a681dSFam Zheng error_propagate(errp, local_err); 2036df9a681dSFam Zheng } 2037df9a681dSFam Zheng } 2038df9a681dSFam Zheng 203950f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2040df9a681dSFam Zheng { 2041df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2042df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2043df9a681dSFam Zheng common, common); 2044df9a681dSFam Zheng 204532bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2046df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2047df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2048df9a681dSFam Zheng */ 2049df9a681dSFam Zheng if (state->prepared) { 2050df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2051df9a681dSFam Zheng } 2052df9a681dSFam Zheng } 2053df9a681dSFam Zheng 205450f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2055df9a681dSFam Zheng Error **errp) 2056df9a681dSFam Zheng { 2057df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2058df9a681dSFam Zheng common, common); 2059df9a681dSFam Zheng BlockDirtyBitmap *action; 2060df9a681dSFam Zheng 206194d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 206294d16a64SJohn Snow return; 206394d16a64SJohn Snow } 206494d16a64SJohn Snow 206532bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2066df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2067df9a681dSFam Zheng action->name, 2068df9a681dSFam Zheng &state->bs, 2069df9a681dSFam Zheng &state->aio_context, 2070df9a681dSFam Zheng errp); 2071df9a681dSFam Zheng if (!state->bitmap) { 2072df9a681dSFam Zheng return; 2073df9a681dSFam Zheng } 2074df9a681dSFam Zheng 2075df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2076df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2077df9a681dSFam Zheng return; 2078df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2079df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2080df9a681dSFam Zheng return; 2081df9a681dSFam Zheng } 2082df9a681dSFam Zheng 2083df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2084df9a681dSFam Zheng /* AioContext is released in .clean() */ 2085df9a681dSFam Zheng } 2086df9a681dSFam Zheng 208750f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2088df9a681dSFam Zheng { 2089df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2090df9a681dSFam Zheng common, common); 2091df9a681dSFam Zheng 2092df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2093df9a681dSFam Zheng } 2094df9a681dSFam Zheng 209550f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2096df9a681dSFam Zheng { 2097df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2098df9a681dSFam Zheng common, common); 2099df9a681dSFam Zheng 2100df9a681dSFam Zheng hbitmap_free(state->backup); 2101df9a681dSFam Zheng } 2102df9a681dSFam Zheng 210350f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2104df9a681dSFam Zheng { 2105df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2106df9a681dSFam Zheng common, common); 2107df9a681dSFam Zheng 2108df9a681dSFam Zheng if (state->aio_context) { 2109df9a681dSFam Zheng aio_context_release(state->aio_context); 2110df9a681dSFam Zheng } 2111df9a681dSFam Zheng } 2112df9a681dSFam Zheng 211350f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 211478b18b78SStefan Hajnoczi { 211578b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 211678b18b78SStefan Hajnoczi } 211778b18b78SStefan Hajnoczi 211850f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 211978b18b78SStefan Hajnoczi { 2120dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 212178b18b78SStefan Hajnoczi } 212278b18b78SStefan Hajnoczi 212350f43f0fSJohn Snow static const BlkActionOps actions[] = { 212443de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 212543de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 212643de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 212743de7e2dSAlberto Garcia .commit = external_snapshot_commit, 212843de7e2dSAlberto Garcia .abort = external_snapshot_abort, 21294ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 213043de7e2dSAlberto Garcia }, 2131c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2132ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2133ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2134ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2135ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2136da763e83SFam Zheng .clean = external_snapshot_clean, 2137ba0c86a3SWenchao Xia }, 21383037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21393037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21403037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 21413037f364SStefan Hajnoczi .abort = drive_backup_abort, 21425d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21433037f364SStefan Hajnoczi }, 2144bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2145bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2146bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2147bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2148bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2149bd8baecdSFam Zheng }, 215078b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 215150f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 215278b18b78SStefan Hajnoczi .prepare = abort_prepare, 215378b18b78SStefan Hajnoczi .commit = abort_commit, 215478b18b78SStefan Hajnoczi }, 2155bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2156bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2157bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2158bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21595d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2160bbe86010SWenchao Xia }, 2161df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2162df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2163df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2164df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2165df9a681dSFam Zheng }, 2166df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2167df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2168df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2169df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2170df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2171df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2172df9a681dSFam Zheng } 2173ba0c86a3SWenchao Xia }; 2174ba0c86a3SWenchao Xia 217594d16a64SJohn Snow /** 217694d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 217794d16a64SJohn Snow * that structure with desired defaults if they are unset. 217894d16a64SJohn Snow */ 217994d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 218094d16a64SJohn Snow TransactionProperties *props) 218194d16a64SJohn Snow { 218294d16a64SJohn Snow if (!props) { 218394d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 218494d16a64SJohn Snow } 218594d16a64SJohn Snow 218694d16a64SJohn Snow if (!props->has_completion_mode) { 218794d16a64SJohn Snow props->has_completion_mode = true; 218894d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 218994d16a64SJohn Snow } 219094d16a64SJohn Snow 219194d16a64SJohn Snow return props; 219294d16a64SJohn Snow } 219394d16a64SJohn Snow 21948802d1fdSJeff Cody /* 2195b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2196b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 21978802d1fdSJeff Cody */ 219894d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 219994d16a64SJohn Snow bool has_props, 220094d16a64SJohn Snow struct TransactionProperties *props, 220194d16a64SJohn Snow Error **errp) 22028802d1fdSJeff Cody { 2203c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 220494d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 220550f43f0fSJohn Snow BlkActionState *state, *next; 220643e17041SLuiz Capitulino Error *local_err = NULL; 22078802d1fdSJeff Cody 220850f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 22098802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 22108802d1fdSJeff Cody 221194d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 221294d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 221394d16a64SJohn Snow */ 221494d16a64SJohn Snow props = get_transaction_properties(props); 221594d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 221694d16a64SJohn Snow block_job_txn = block_job_txn_new(); 221794d16a64SJohn Snow } 221894d16a64SJohn Snow 2219b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 22208802d1fdSJeff Cody bdrv_drain_all(); 22218802d1fdSJeff Cody 2222b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 22238802d1fdSJeff Cody while (NULL != dev_entry) { 2224c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 222550f43f0fSJohn Snow const BlkActionOps *ops; 222652e7c241SPaolo Bonzini 22278802d1fdSJeff Cody dev_info = dev_entry->value; 22288802d1fdSJeff Cody dev_entry = dev_entry->next; 22298802d1fdSJeff Cody 22306a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2231ba0c86a3SWenchao Xia 22326a8f9661SEric Blake ops = &actions[dev_info->type]; 2233aa3fe714SMax Reitz assert(ops->instance_size > 0); 2234aa3fe714SMax Reitz 2235ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2236ba5d6ab6SStefan Hajnoczi state->ops = ops; 2237ba5d6ab6SStefan Hajnoczi state->action = dev_info; 223894d16a64SJohn Snow state->block_job_txn = block_job_txn; 223994d16a64SJohn Snow state->txn_props = props; 2240ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22418802d1fdSJeff Cody 2242ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 224384d18f06SMarkus Armbruster if (local_err) { 22449b9877eeSWenchao Xia error_propagate(errp, local_err); 22459b9877eeSWenchao Xia goto delete_and_fail; 22469b9877eeSWenchao Xia } 224752e7c241SPaolo Bonzini } 22488802d1fdSJeff Cody 2249ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2250f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2251ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22528802d1fdSJeff Cody } 2253f9ea81e8SStefan Hajnoczi } 22548802d1fdSJeff Cody 22558802d1fdSJeff Cody /* success */ 22568802d1fdSJeff Cody goto exit; 22578802d1fdSJeff Cody 22588802d1fdSJeff Cody delete_and_fail: 2259b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2260ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2261ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2262ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2263ba0c86a3SWenchao Xia } 22648802d1fdSJeff Cody } 22658802d1fdSJeff Cody exit: 2266ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2267ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2268ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2269ba0c86a3SWenchao Xia } 2270ba5d6ab6SStefan Hajnoczi g_free(state); 22718802d1fdSJeff Cody } 227294d16a64SJohn Snow if (!has_props) { 227394d16a64SJohn Snow qapi_free_TransactionProperties(props); 227494d16a64SJohn Snow } 227594d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22768802d1fdSJeff Cody } 22778802d1fdSJeff Cody 2278c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 2279666daa68SMarkus Armbruster { 228038f54bd1SMax Reitz Error *local_err = NULL; 22813a3086b7SJohn Snow int rc; 2282666daa68SMarkus Armbruster 22833a3086b7SJohn Snow if (!has_force) { 22843a3086b7SJohn Snow force = false; 22853a3086b7SJohn Snow } 22863a3086b7SJohn Snow 22873a3086b7SJohn Snow rc = do_open_tray(device, force, &local_err); 2288bf18bee5SColin Lord if (rc && rc != -ENOSYS) { 228938f54bd1SMax Reitz error_propagate(errp, local_err); 2290c245b6a3SLuiz Capitulino return; 2291666daa68SMarkus Armbruster } 2292bf18bee5SColin Lord error_free(local_err); 22933a3086b7SJohn Snow 22946e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, errp); 2295666daa68SMarkus Armbruster } 2296666daa68SMarkus Armbruster 229712d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 229812d3ba82SBenoît Canet bool has_node_name, const char *node_name, 229912d3ba82SBenoît Canet const char *password, Error **errp) 2300666daa68SMarkus Armbruster { 230112d3ba82SBenoît Canet Error *local_err = NULL; 2302666daa68SMarkus Armbruster BlockDriverState *bs; 2303e3442099SStefan Hajnoczi AioContext *aio_context; 2304666daa68SMarkus Armbruster 230512d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 230612d3ba82SBenoît Canet has_node_name ? node_name : NULL, 230712d3ba82SBenoît Canet &local_err); 230884d18f06SMarkus Armbruster if (local_err) { 230912d3ba82SBenoît Canet error_propagate(errp, local_err); 2310a4dea8a9SLuiz Capitulino return; 2311666daa68SMarkus Armbruster } 2312666daa68SMarkus Armbruster 2313e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2314e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2315e3442099SStefan Hajnoczi 23164d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2317666daa68SMarkus Armbruster 2318e3442099SStefan Hajnoczi aio_context_release(aio_context); 2319e3442099SStefan Hajnoczi } 2320e3442099SStefan Hajnoczi 2321bf18bee5SColin Lord /* 2322bf18bee5SColin Lord * Attempt to open the tray of @device. 2323bf18bee5SColin Lord * If @force, ignore its tray lock. 2324bf18bee5SColin Lord * Else, if the tray is locked, don't open it, but ask the guest to open it. 2325bf18bee5SColin Lord * On error, store an error through @errp and return -errno. 2326bf18bee5SColin Lord * If @device does not exist, return -ENODEV. 2327bf18bee5SColin Lord * If it has no removable media, return -ENOTSUP. 2328bf18bee5SColin Lord * If it has no tray, return -ENOSYS. 2329bf18bee5SColin Lord * If the guest was asked to open the tray, return -EINPROGRESS. 2330bf18bee5SColin Lord * Else, return 0. 23313a3086b7SJohn Snow */ 23323a3086b7SJohn Snow static int do_open_tray(const char *device, bool force, Error **errp) 23337d8a9f71SMax Reitz { 23347d8a9f71SMax Reitz BlockBackend *blk; 23357d8a9f71SMax Reitz bool locked; 23367d8a9f71SMax Reitz 23377d8a9f71SMax Reitz blk = blk_by_name(device); 23387d8a9f71SMax Reitz if (!blk) { 23397d8a9f71SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 23407d8a9f71SMax Reitz "Device '%s' not found", device); 23413a3086b7SJohn Snow return -ENODEV; 23427d8a9f71SMax Reitz } 23437d8a9f71SMax Reitz 23447d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23457d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23463a3086b7SJohn Snow return -ENOTSUP; 23477d8a9f71SMax Reitz } 23487d8a9f71SMax Reitz 234912c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 2350bf18bee5SColin Lord error_setg(errp, "Device '%s' does not have a tray", device); 2351bf18bee5SColin Lord return -ENOSYS; 235212c7ec87SMax Reitz } 235312c7ec87SMax Reitz 23547d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23553a3086b7SJohn Snow return 0; 23567d8a9f71SMax Reitz } 23577d8a9f71SMax Reitz 23587d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23597d8a9f71SMax Reitz if (locked) { 23607d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23617d8a9f71SMax Reitz } 23627d8a9f71SMax Reitz 23637d8a9f71SMax Reitz if (!locked || force) { 23647d8a9f71SMax Reitz blk_dev_change_media_cb(blk, false); 23657d8a9f71SMax Reitz } 23663a3086b7SJohn Snow 23673a3086b7SJohn Snow if (locked && !force) { 2368bf18bee5SColin Lord error_setg(errp, "Device '%s' is locked and force was not specified, " 2369bf18bee5SColin Lord "wait for tray to open and try again", device); 2370bf18bee5SColin Lord return -EINPROGRESS; 23713a3086b7SJohn Snow } 23723a3086b7SJohn Snow 23733a3086b7SJohn Snow return 0; 23743a3086b7SJohn Snow } 23753a3086b7SJohn Snow 23763a3086b7SJohn Snow void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 23773a3086b7SJohn Snow Error **errp) 23783a3086b7SJohn Snow { 2379bf18bee5SColin Lord Error *local_err = NULL; 2380bf18bee5SColin Lord int rc; 2381bf18bee5SColin Lord 23823a3086b7SJohn Snow if (!has_force) { 23833a3086b7SJohn Snow force = false; 23843a3086b7SJohn Snow } 2385bf18bee5SColin Lord rc = do_open_tray(device, force, &local_err); 2386bf18bee5SColin Lord if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { 2387bf18bee5SColin Lord error_propagate(errp, local_err); 2388bf18bee5SColin Lord return; 2389bf18bee5SColin Lord } 2390bf18bee5SColin Lord error_free(local_err); 23917d8a9f71SMax Reitz } 23927d8a9f71SMax Reitz 2393abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2394abaaf59dSMax Reitz { 2395abaaf59dSMax Reitz BlockBackend *blk; 2396abaaf59dSMax Reitz 2397abaaf59dSMax Reitz blk = blk_by_name(device); 2398abaaf59dSMax Reitz if (!blk) { 2399abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2400abaaf59dSMax Reitz "Device '%s' not found", device); 2401abaaf59dSMax Reitz return; 2402abaaf59dSMax Reitz } 2403abaaf59dSMax Reitz 2404abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2405abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2406abaaf59dSMax Reitz return; 2407abaaf59dSMax Reitz } 2408abaaf59dSMax Reitz 240912c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 241012c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 241112c7ec87SMax Reitz return; 241212c7ec87SMax Reitz } 241312c7ec87SMax Reitz 2414abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2415abaaf59dSMax Reitz return; 2416abaaf59dSMax Reitz } 2417abaaf59dSMax Reitz 2418abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2419abaaf59dSMax Reitz } 2420abaaf59dSMax Reitz 24216e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 24222814f672SMax Reitz { 24232814f672SMax Reitz BlockBackend *blk; 24242814f672SMax Reitz BlockDriverState *bs; 24252814f672SMax Reitz AioContext *aio_context; 24262814f672SMax Reitz bool has_device; 24272814f672SMax Reitz 24282814f672SMax Reitz blk = blk_by_name(device); 24292814f672SMax Reitz if (!blk) { 24302814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 24312814f672SMax Reitz "Device '%s' not found", device); 24322814f672SMax Reitz return; 24332814f672SMax Reitz } 24342814f672SMax Reitz 24352814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 24362814f672SMax Reitz has_device = blk_get_attached_dev(blk); 24372814f672SMax Reitz 24382814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 24392814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 24402814f672SMax Reitz return; 24412814f672SMax Reitz } 24422814f672SMax Reitz 244312c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 24442814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 24452814f672SMax Reitz return; 24462814f672SMax Reitz } 24472814f672SMax Reitz 24482814f672SMax Reitz bs = blk_bs(blk); 24492814f672SMax Reitz if (!bs) { 24502814f672SMax Reitz return; 24512814f672SMax Reitz } 24522814f672SMax Reitz 24532814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 24542814f672SMax Reitz aio_context_acquire(aio_context); 24552814f672SMax Reitz 24562814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 24572814f672SMax Reitz goto out; 24582814f672SMax Reitz } 24592814f672SMax Reitz 24602814f672SMax Reitz blk_remove_bs(blk); 24612814f672SMax Reitz 246212c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 246312c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 246412c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 246512c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 246612c7ec87SMax Reitz * value passed here (i.e. false). */ 246712c7ec87SMax Reitz blk_dev_change_media_cb(blk, false); 246812c7ec87SMax Reitz } 246912c7ec87SMax Reitz 24702814f672SMax Reitz out: 24712814f672SMax Reitz aio_context_release(aio_context); 24722814f672SMax Reitz } 24732814f672SMax Reitz 2474d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device, 2475d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2476d1299882SMax Reitz { 2477d1299882SMax Reitz BlockBackend *blk; 2478d1299882SMax Reitz bool has_device; 2479d1299882SMax Reitz 2480d1299882SMax Reitz blk = blk_by_name(device); 2481d1299882SMax Reitz if (!blk) { 2482d1299882SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2483d1299882SMax Reitz "Device '%s' not found", device); 2484d1299882SMax Reitz return; 2485d1299882SMax Reitz } 2486d1299882SMax Reitz 2487d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2488d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2489d1299882SMax Reitz 2490d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2491d1299882SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2492d1299882SMax Reitz return; 2493d1299882SMax Reitz } 2494d1299882SMax Reitz 249512c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2496d1299882SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2497d1299882SMax Reitz return; 2498d1299882SMax Reitz } 2499d1299882SMax Reitz 2500d1299882SMax Reitz if (blk_bs(blk)) { 2501d1299882SMax Reitz error_setg(errp, "There already is a medium in device '%s'", device); 2502d1299882SMax Reitz return; 2503d1299882SMax Reitz } 2504d1299882SMax Reitz 2505d1299882SMax Reitz blk_insert_bs(blk, bs); 2506d1299882SMax Reitz 250712c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 250812c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 250912c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 251012c7ec87SMax Reitz * slot here. 251112c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 251212c7ec87SMax Reitz * value passed here (i.e. true). */ 251312c7ec87SMax Reitz blk_dev_change_media_cb(blk, true); 251412c7ec87SMax Reitz } 2515d1299882SMax Reitz } 2516d1299882SMax Reitz 25176e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2518d1299882SMax Reitz Error **errp) 2519d1299882SMax Reitz { 2520d1299882SMax Reitz BlockDriverState *bs; 2521d1299882SMax Reitz 2522d1299882SMax Reitz bs = bdrv_find_node(node_name); 2523d1299882SMax Reitz if (!bs) { 2524d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2525d1299882SMax Reitz return; 2526d1299882SMax Reitz } 2527d1299882SMax Reitz 25281f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2529d1299882SMax Reitz error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 25301f0c461bSKevin Wolf bdrv_get_parent_name(bs)); 2531d1299882SMax Reitz return; 2532d1299882SMax Reitz } 2533d1299882SMax Reitz 2534d1299882SMax Reitz qmp_blockdev_insert_anon_medium(device, bs, errp); 2535d1299882SMax Reitz } 2536d1299882SMax Reitz 253724fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename, 253824fb4133SMax Reitz bool has_format, const char *format, 253939ff43d9SMax Reitz bool has_read_only, 254039ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 254124fb4133SMax Reitz Error **errp) 2542de2c6c05SMax Reitz { 2543de2c6c05SMax Reitz BlockBackend *blk; 2544de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 25455b363937SMax Reitz int bdrv_flags; 254638a53d50SColin Lord int rc; 2547de2c6c05SMax Reitz QDict *options = NULL; 2548de2c6c05SMax Reitz Error *err = NULL; 2549de2c6c05SMax Reitz 2550de2c6c05SMax Reitz blk = blk_by_name(device); 2551de2c6c05SMax Reitz if (!blk) { 2552de2c6c05SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2553de2c6c05SMax Reitz "Device '%s' not found", device); 2554de2c6c05SMax Reitz goto fail; 2555de2c6c05SMax Reitz } 2556de2c6c05SMax Reitz 2557de2c6c05SMax Reitz if (blk_bs(blk)) { 2558de2c6c05SMax Reitz blk_update_root_state(blk); 2559de2c6c05SMax Reitz } 2560de2c6c05SMax Reitz 2561de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2562156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2563156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2564de2c6c05SMax Reitz 256539ff43d9SMax Reitz if (!has_read_only) { 256639ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 256739ff43d9SMax Reitz } 256839ff43d9SMax Reitz 256939ff43d9SMax Reitz switch (read_only) { 257039ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 257139ff43d9SMax Reitz break; 257239ff43d9SMax Reitz 257339ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 257439ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 257539ff43d9SMax Reitz break; 257639ff43d9SMax Reitz 257739ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 257839ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 257939ff43d9SMax Reitz break; 258039ff43d9SMax Reitz 258139ff43d9SMax Reitz default: 258239ff43d9SMax Reitz abort(); 258339ff43d9SMax Reitz } 258439ff43d9SMax Reitz 258524fb4133SMax Reitz if (has_format) { 2586de2c6c05SMax Reitz options = qdict_new(); 2587de2c6c05SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2588de2c6c05SMax Reitz } 2589de2c6c05SMax Reitz 25905b363937SMax Reitz medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 25915b363937SMax Reitz if (!medium_bs) { 2592de2c6c05SMax Reitz goto fail; 2593de2c6c05SMax Reitz } 2594de2c6c05SMax Reitz 2595de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2596de2c6c05SMax Reitz if (err) { 2597de2c6c05SMax Reitz error_propagate(errp, err); 2598de2c6c05SMax Reitz goto fail; 2599de2c6c05SMax Reitz } 2600de2c6c05SMax Reitz 260138a53d50SColin Lord rc = do_open_tray(device, false, &err); 260238a53d50SColin Lord if (rc && rc != -ENOSYS) { 2603de2c6c05SMax Reitz error_propagate(errp, err); 2604de2c6c05SMax Reitz goto fail; 2605de2c6c05SMax Reitz } 260638a53d50SColin Lord error_free(err); 260738a53d50SColin Lord err = NULL; 2608de2c6c05SMax Reitz 26096e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, &err); 2610de2c6c05SMax Reitz if (err) { 2611de2c6c05SMax Reitz error_propagate(errp, err); 2612de2c6c05SMax Reitz goto fail; 2613de2c6c05SMax Reitz } 2614de2c6c05SMax Reitz 2615de2c6c05SMax Reitz qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2616de2c6c05SMax Reitz if (err) { 2617de2c6c05SMax Reitz error_propagate(errp, err); 2618de2c6c05SMax Reitz goto fail; 2619de2c6c05SMax Reitz } 2620de2c6c05SMax Reitz 2621a5614993SKevin Wolf blk_apply_root_state(blk, medium_bs); 2622a5614993SKevin Wolf 2623de2c6c05SMax Reitz qmp_blockdev_close_tray(device, errp); 2624de2c6c05SMax Reitz 2625de2c6c05SMax Reitz fail: 2626de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2627de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2628de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2629de2c6c05SMax Reitz bdrv_unref(medium_bs); 2630de2c6c05SMax Reitz } 2631de2c6c05SMax Reitz 2632727f005eSZhi Yong Wu /* throttling disk I/O limits */ 26334dc9397bSEric Blake void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) 2634727f005eSZhi Yong Wu { 2635cc0681c4SBenoît Canet ThrottleConfig cfg; 2636727f005eSZhi Yong Wu BlockDriverState *bs; 2637a0e8544cSFam Zheng BlockBackend *blk; 2638b15446fdSStefan Hajnoczi AioContext *aio_context; 2639727f005eSZhi Yong Wu 26404dc9397bSEric Blake blk = blk_by_name(arg->device); 2641a0e8544cSFam Zheng if (!blk) { 264275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 26434dc9397bSEric Blake "Device '%s' not found", arg->device); 264480047da5SLuiz Capitulino return; 2645727f005eSZhi Yong Wu } 26465433c24fSMax Reitz 26475433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 26485433c24fSMax Reitz aio_context_acquire(aio_context); 26495433c24fSMax Reitz 2650a0e8544cSFam Zheng bs = blk_bs(blk); 26515433c24fSMax Reitz if (!bs) { 26524dc9397bSEric Blake error_setg(errp, "Device '%s' has no medium", arg->device); 26535433c24fSMax Reitz goto out; 26545433c24fSMax Reitz } 2655727f005eSZhi Yong Wu 26561588ab5dSAlberto Garcia throttle_config_init(&cfg); 26574dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; 26584dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; 26594dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; 2660727f005eSZhi Yong Wu 26614dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; 26624dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; 26634dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; 2664cc0681c4SBenoît Canet 26654dc9397bSEric Blake if (arg->has_bps_max) { 26664dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; 26673e9fab69SBenoît Canet } 26684dc9397bSEric Blake if (arg->has_bps_rd_max) { 26694dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; 26703e9fab69SBenoît Canet } 26714dc9397bSEric Blake if (arg->has_bps_wr_max) { 26724dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; 26733e9fab69SBenoît Canet } 26744dc9397bSEric Blake if (arg->has_iops_max) { 26754dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; 26763e9fab69SBenoît Canet } 26774dc9397bSEric Blake if (arg->has_iops_rd_max) { 26784dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; 26793e9fab69SBenoît Canet } 26804dc9397bSEric Blake if (arg->has_iops_wr_max) { 26814dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; 26823e9fab69SBenoît Canet } 2683cc0681c4SBenoît Canet 26844dc9397bSEric Blake if (arg->has_bps_max_length) { 26854dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; 2686dce13204SAlberto Garcia } 26874dc9397bSEric Blake if (arg->has_bps_rd_max_length) { 26884dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; 2689dce13204SAlberto Garcia } 26904dc9397bSEric Blake if (arg->has_bps_wr_max_length) { 26914dc9397bSEric Blake cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; 2692dce13204SAlberto Garcia } 26934dc9397bSEric Blake if (arg->has_iops_max_length) { 26944dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; 2695dce13204SAlberto Garcia } 26964dc9397bSEric Blake if (arg->has_iops_rd_max_length) { 26974dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; 2698dce13204SAlberto Garcia } 26994dc9397bSEric Blake if (arg->has_iops_wr_max_length) { 27004dc9397bSEric Blake cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; 2701dce13204SAlberto Garcia } 2702dce13204SAlberto Garcia 27034dc9397bSEric Blake if (arg->has_iops_size) { 27044dc9397bSEric Blake cfg.op_size = arg->iops_size; 27052024c1dfSBenoît Canet } 2706cc0681c4SBenoît Canet 2707d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 27085433c24fSMax Reitz goto out; 2709727f005eSZhi Yong Wu } 2710727f005eSZhi Yong Wu 271176f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 271276f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 271376f4afb4SAlberto Garcia * just update the throttling group. */ 271497148076SKevin Wolf if (!blk_get_public(blk)->throttle_state) { 27154dc9397bSEric Blake blk_io_limits_enable(blk, 27164dc9397bSEric Blake arg->has_group ? arg->group : arg->device); 27174dc9397bSEric Blake } else if (arg->has_group) { 27184dc9397bSEric Blake blk_io_limits_update_group(blk, arg->group); 2719727f005eSZhi Yong Wu } 272076f4afb4SAlberto Garcia /* Set the new throttling configuration */ 272197148076SKevin Wolf blk_set_io_limits(blk, &cfg); 272297148076SKevin Wolf } else if (blk_get_public(blk)->throttle_state) { 272376f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 272497148076SKevin Wolf blk_io_limits_disable(blk); 2725727f005eSZhi Yong Wu } 2726b15446fdSStefan Hajnoczi 27275433c24fSMax Reitz out: 2728b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2729727f005eSZhi Yong Wu } 2730727f005eSZhi Yong Wu 2731341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2732341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2733341ebc2fSJohn Snow Error **errp) 2734341ebc2fSJohn Snow { 2735341ebc2fSJohn Snow AioContext *aio_context; 2736341ebc2fSJohn Snow BlockDriverState *bs; 2737341ebc2fSJohn Snow 2738341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2739341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2740341ebc2fSJohn Snow return; 2741341ebc2fSJohn Snow } 2742341ebc2fSJohn Snow 2743341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2744341ebc2fSJohn Snow if (!bs) { 2745341ebc2fSJohn Snow return; 2746341ebc2fSJohn Snow } 2747341ebc2fSJohn Snow 2748341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2749341ebc2fSJohn Snow aio_context_acquire(aio_context); 2750341ebc2fSJohn Snow 2751341ebc2fSJohn Snow if (has_granularity) { 2752341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2753341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2754341ebc2fSJohn Snow "and at least 512"); 2755341ebc2fSJohn Snow goto out; 2756341ebc2fSJohn Snow } 2757341ebc2fSJohn Snow } else { 2758341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2759341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2760341ebc2fSJohn Snow } 2761341ebc2fSJohn Snow 2762341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2763341ebc2fSJohn Snow 2764341ebc2fSJohn Snow out: 2765341ebc2fSJohn Snow aio_context_release(aio_context); 2766341ebc2fSJohn Snow } 2767341ebc2fSJohn Snow 2768341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2769341ebc2fSJohn Snow Error **errp) 2770341ebc2fSJohn Snow { 2771341ebc2fSJohn Snow AioContext *aio_context; 2772341ebc2fSJohn Snow BlockDriverState *bs; 2773341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2774341ebc2fSJohn Snow 2775341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2776341ebc2fSJohn Snow if (!bitmap || !bs) { 2777341ebc2fSJohn Snow return; 2778341ebc2fSJohn Snow } 2779341ebc2fSJohn Snow 27809bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 27819bd2b08fSJohn Snow error_setg(errp, 27829bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 27839bd2b08fSJohn Snow name); 27849bd2b08fSJohn Snow goto out; 27859bd2b08fSJohn Snow } 278620dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2787341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2788341ebc2fSJohn Snow 27899bd2b08fSJohn Snow out: 2790341ebc2fSJohn Snow aio_context_release(aio_context); 2791341ebc2fSJohn Snow } 2792341ebc2fSJohn Snow 2793e74e6b78SJohn Snow /** 2794e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2795e74e6b78SJohn Snow * immediately after a full backup operation. 2796e74e6b78SJohn Snow */ 2797e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2798e74e6b78SJohn Snow Error **errp) 2799e74e6b78SJohn Snow { 2800e74e6b78SJohn Snow AioContext *aio_context; 2801e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2802e74e6b78SJohn Snow BlockDriverState *bs; 2803e74e6b78SJohn Snow 2804e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2805e74e6b78SJohn Snow if (!bitmap || !bs) { 2806e74e6b78SJohn Snow return; 2807e74e6b78SJohn Snow } 2808e74e6b78SJohn Snow 2809e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2810e74e6b78SJohn Snow error_setg(errp, 2811e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2812e74e6b78SJohn Snow name); 2813e74e6b78SJohn Snow goto out; 2814e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2815e74e6b78SJohn Snow error_setg(errp, 2816e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2817e74e6b78SJohn Snow name); 2818e74e6b78SJohn Snow goto out; 2819e74e6b78SJohn Snow } 2820e74e6b78SJohn Snow 2821df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2822e74e6b78SJohn Snow 2823e74e6b78SJohn Snow out: 2824e74e6b78SJohn Snow aio_context_release(aio_context); 2825e74e6b78SJohn Snow } 2826e74e6b78SJohn Snow 2827072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 28289063f814SRyan Harper { 28299063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 28307e7d56d9SMarkus Armbruster BlockBackend *blk; 28319063f814SRyan Harper BlockDriverState *bs; 28328ad4202bSStefan Hajnoczi AioContext *aio_context; 28333718d8abSFam Zheng Error *local_err = NULL; 28349063f814SRyan Harper 28352073d410SKevin Wolf bs = bdrv_find_node(id); 28362073d410SKevin Wolf if (bs) { 28372073d410SKevin Wolf qmp_x_blockdev_del(false, NULL, true, id, &local_err); 28382073d410SKevin Wolf if (local_err) { 28392073d410SKevin Wolf error_report_err(local_err); 28402073d410SKevin Wolf } 28412073d410SKevin Wolf return; 28422073d410SKevin Wolf } 28432073d410SKevin Wolf 28447e7d56d9SMarkus Armbruster blk = blk_by_name(id); 28457e7d56d9SMarkus Armbruster if (!blk) { 2846b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2847072ebe6bSMarkus Armbruster return; 28489063f814SRyan Harper } 28498ad4202bSStefan Hajnoczi 285026f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 285148f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 285248f364ddSMarkus Armbruster " is not supported"); 2853072ebe6bSMarkus Armbruster return; 285448f364ddSMarkus Armbruster } 285548f364ddSMarkus Armbruster 28565433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 28578ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 28588ad4202bSStefan Hajnoczi 28595433c24fSMax Reitz bs = blk_bs(blk); 28605433c24fSMax Reitz if (bs) { 28613718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2862565f65d2SMarkus Armbruster error_report_err(local_err); 28638ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2864072ebe6bSMarkus Armbruster return; 28658591675fSMarcelo Tosatti } 28669063f814SRyan Harper 2867938abd43SMax Reitz blk_remove_bs(blk); 28685433c24fSMax Reitz } 28699063f814SRyan Harper 28707c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 2871efaa7c4eSMax Reitz monitor_remove_blk(blk); 2872efaa7c4eSMax Reitz 28737c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 28747c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 2875d22b2f41SRyan Harper */ 2876a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 2877293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2878373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2879293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2880d22b2f41SRyan Harper } else { 2881b9fe8a7aSMarkus Armbruster blk_unref(blk); 2882d22b2f41SRyan Harper } 28839063f814SRyan Harper 28848ad4202bSStefan Hajnoczi aio_context_release(aio_context); 28859063f814SRyan Harper } 28866d4a2b3aSChristoph Hellwig 28873b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 28883b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 28893b1dbd11SBenoît Canet int64_t size, Error **errp) 28906d4a2b3aSChristoph Hellwig { 28913b1dbd11SBenoît Canet Error *local_err = NULL; 28926d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2893927e0e76SStefan Hajnoczi AioContext *aio_context; 28948732901eSKevin Wolf int ret; 28956d4a2b3aSChristoph Hellwig 28963b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 28973b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 28983b1dbd11SBenoît Canet &local_err); 289984d18f06SMarkus Armbruster if (local_err) { 29003b1dbd11SBenoît Canet error_propagate(errp, local_err); 29013b1dbd11SBenoît Canet return; 29023b1dbd11SBenoît Canet } 29033b1dbd11SBenoît Canet 2904927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2905927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2906927e0e76SStefan Hajnoczi 29073b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2908c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2909927e0e76SStefan Hajnoczi goto out; 29106d4a2b3aSChristoph Hellwig } 29116d4a2b3aSChristoph Hellwig 29126d4a2b3aSChristoph Hellwig if (size < 0) { 2913c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2914927e0e76SStefan Hajnoczi goto out; 29156d4a2b3aSChristoph Hellwig } 29166d4a2b3aSChristoph Hellwig 29179c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2918c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2919927e0e76SStefan Hajnoczi goto out; 29209c75e168SJeff Cody } 29219c75e168SJeff Cody 292292b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 292392b7a08dSPeter Lieven bdrv_drain_all(); 292492b7a08dSPeter Lieven 29258732901eSKevin Wolf ret = bdrv_truncate(bs, size); 29268732901eSKevin Wolf switch (ret) { 2927939a1cc3SStefan Hajnoczi case 0: 2928939a1cc3SStefan Hajnoczi break; 2929939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2930c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2931939a1cc3SStefan Hajnoczi break; 2932939a1cc3SStefan Hajnoczi case -ENOTSUP: 2933c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2934939a1cc3SStefan Hajnoczi break; 2935939a1cc3SStefan Hajnoczi case -EACCES: 293681e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2937939a1cc3SStefan Hajnoczi break; 2938939a1cc3SStefan Hajnoczi case -EBUSY: 2939c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2940939a1cc3SStefan Hajnoczi break; 2941939a1cc3SStefan Hajnoczi default: 29428732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2943939a1cc3SStefan Hajnoczi break; 29446d4a2b3aSChristoph Hellwig } 2945927e0e76SStefan Hajnoczi 2946927e0e76SStefan Hajnoczi out: 2947927e0e76SStefan Hajnoczi aio_context_release(aio_context); 29486d4a2b3aSChristoph Hellwig } 294912bd451fSStefan Hajnoczi 29509abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 295112bd451fSStefan Hajnoczi { 2952723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2953723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2954723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2955723c5d93SStefan Hajnoczi */ 2956723c5d93SStefan Hajnoczi 295712bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2958bcada37bSWenchao Xia const char *msg = NULL; 295912bd451fSStefan Hajnoczi 29609abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 296112bd451fSStefan Hajnoczi 296212bd451fSStefan Hajnoczi assert(bs->job); 2963bcada37bSWenchao Xia 296412bd451fSStefan Hajnoczi if (ret < 0) { 2965bcada37bSWenchao Xia msg = strerror(-ret); 296612bd451fSStefan Hajnoczi } 296712bd451fSStefan Hajnoczi 2968370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 2969bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 2970370521a1SStefan Hajnoczi } else { 2971bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 2972370521a1SStefan Hajnoczi } 297312bd451fSStefan Hajnoczi } 297412bd451fSStefan Hajnoczi 29752323322eSAlberto Garcia void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, 297613d8cc51SJeff Cody bool has_base, const char *base, 297713d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 297813d8cc51SJeff Cody bool has_speed, int64_t speed, 29791d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 29801d809098SPaolo Bonzini Error **errp) 298112bd451fSStefan Hajnoczi { 298212bd451fSStefan Hajnoczi BlockDriverState *bs; 2983c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 2984f3e69bebSStefan Hajnoczi AioContext *aio_context; 2985fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 298613d8cc51SJeff Cody const char *base_name = NULL; 298712bd451fSStefan Hajnoczi 29881d809098SPaolo Bonzini if (!has_on_error) { 29891d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 29901d809098SPaolo Bonzini } 29911d809098SPaolo Bonzini 2992b6c1bae5SKevin Wolf bs = qmp_get_root_bs(device, errp); 2993b6c1bae5SKevin Wolf if (!bs) { 299412bd451fSStefan Hajnoczi return; 299512bd451fSStefan Hajnoczi } 299612bd451fSStefan Hajnoczi 2997b6c1bae5SKevin Wolf aio_context = bdrv_get_aio_context(bs); 2998f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 2999f3e69bebSStefan Hajnoczi 3000628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 3001f3e69bebSStefan Hajnoczi goto out; 3002628ff683SFam Zheng } 3003628ff683SFam Zheng 300413d8cc51SJeff Cody if (has_base) { 3005c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3006c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3007c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3008f3e69bebSStefan Hajnoczi goto out; 300912bd451fSStefan Hajnoczi } 3010f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 301113d8cc51SJeff Cody base_name = base; 3012c8c3080fSMarcelo Tosatti } 301312bd451fSStefan Hajnoczi 301413d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 301513d8cc51SJeff Cody * file, and specifying one is therefore an error */ 301613d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 301713d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 301813d8cc51SJeff Cody "entire chain"); 3019f3e69bebSStefan Hajnoczi goto out; 302013d8cc51SJeff Cody } 302113d8cc51SJeff Cody 302213d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 302313d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 302413d8cc51SJeff Cody 30252323322eSAlberto Garcia stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, 30262323322eSAlberto Garcia has_speed ? speed : 0, on_error, block_job_cb, bs, &local_err); 302784d18f06SMarkus Armbruster if (local_err) { 3028fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3029f3e69bebSStefan Hajnoczi goto out; 303012bd451fSStefan Hajnoczi } 303112bd451fSStefan Hajnoczi 303212bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3033f3e69bebSStefan Hajnoczi 3034f3e69bebSStefan Hajnoczi out: 3035f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 303612bd451fSStefan Hajnoczi } 30372d47c6e9SStefan Hajnoczi 3038fd62c609SAlberto Garcia void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, 30397676e2c5SJeff Cody bool has_base, const char *base, 30407676e2c5SJeff Cody bool has_top, const char *top, 304154e26900SJeff Cody bool has_backing_file, const char *backing_file, 3042ed61fc10SJeff Cody bool has_speed, int64_t speed, 3043ed61fc10SJeff Cody Error **errp) 3044ed61fc10SJeff Cody { 3045ed61fc10SJeff Cody BlockDriverState *bs; 3046ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 30479e85cd5cSStefan Hajnoczi AioContext *aio_context; 3048ed61fc10SJeff Cody Error *local_err = NULL; 3049ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3050ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3051ed61fc10SJeff Cody */ 305292aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3053ed61fc10SJeff Cody 305454504663SMax Reitz if (!has_speed) { 305554504663SMax Reitz speed = 0; 305654504663SMax Reitz } 305754504663SMax Reitz 30587676e2c5SJeff Cody /* Important Note: 30597676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 30607676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 30617676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 30627676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 30631d13b167SKevin Wolf bs = qmp_get_root_bs(device, &local_err); 30641d13b167SKevin Wolf if (!bs) { 30651d13b167SKevin Wolf bs = bdrv_lookup_bs(device, device, NULL); 30661d13b167SKevin Wolf if (!bs) { 30671d13b167SKevin Wolf error_free(local_err); 306875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 306975158ebbSMarkus Armbruster "Device '%s' not found", device); 30701d13b167SKevin Wolf } else { 30711d13b167SKevin Wolf error_propagate(errp, local_err); 30721d13b167SKevin Wolf } 3073ed61fc10SJeff Cody return; 3074ed61fc10SJeff Cody } 3075ed61fc10SJeff Cody 30761d13b167SKevin Wolf aio_context = bdrv_get_aio_context(bs); 30779e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 30789e85cd5cSStefan Hajnoczi 3079bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 30809e85cd5cSStefan Hajnoczi goto out; 3081628ff683SFam Zheng } 3082628ff683SFam Zheng 3083ed61fc10SJeff Cody /* default top_bs is the active layer */ 3084ed61fc10SJeff Cody top_bs = bs; 3085ed61fc10SJeff Cody 30867676e2c5SJeff Cody if (has_top && top) { 3087ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3088ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3089ed61fc10SJeff Cody } 3090ed61fc10SJeff Cody } 3091ed61fc10SJeff Cody 3092ed61fc10SJeff Cody if (top_bs == NULL) { 3093ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 30949e85cd5cSStefan Hajnoczi goto out; 3095ed61fc10SJeff Cody } 3096ed61fc10SJeff Cody 30979e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 30989e85cd5cSStefan Hajnoczi 3099d5208c45SJeff Cody if (has_base && base) { 3100d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3101d5208c45SJeff Cody } else { 3102d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3103d5208c45SJeff Cody } 3104d5208c45SJeff Cody 3105d5208c45SJeff Cody if (base_bs == NULL) { 3106c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 31079e85cd5cSStefan Hajnoczi goto out; 3108d5208c45SJeff Cody } 3109d5208c45SJeff Cody 31109e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 31119e85cd5cSStefan Hajnoczi 3112bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3113bb00021dSFam Zheng goto out; 3114bb00021dSFam Zheng } 3115bb00021dSFam Zheng 31167676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 31177676e2c5SJeff Cody if (top_bs == base_bs) { 31187676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 31199e85cd5cSStefan Hajnoczi goto out; 31207676e2c5SJeff Cody } 31217676e2c5SJeff Cody 312220a63d2cSFam Zheng if (top_bs == bs) { 312354e26900SJeff Cody if (has_backing_file) { 312454e26900SJeff Cody error_setg(errp, "'backing-file' specified," 312554e26900SJeff Cody " but 'top' is the active layer"); 31269e85cd5cSStefan Hajnoczi goto out; 312754e26900SJeff Cody } 3128fd62c609SAlberto Garcia commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, speed, 3129fd62c609SAlberto Garcia on_error, block_job_cb, bs, &local_err); 313020a63d2cSFam Zheng } else { 3131fd62c609SAlberto Garcia commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed, 3132fd62c609SAlberto Garcia on_error, block_job_cb, bs, 313354e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 313420a63d2cSFam Zheng } 3135ed61fc10SJeff Cody if (local_err != NULL) { 3136ed61fc10SJeff Cody error_propagate(errp, local_err); 31379e85cd5cSStefan Hajnoczi goto out; 3138ed61fc10SJeff Cody } 31399e85cd5cSStefan Hajnoczi 31409e85cd5cSStefan Hajnoczi out: 31419e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3142ed61fc10SJeff Cody } 3143ed61fc10SJeff Cody 314470559d49SAlberto Garcia static void do_drive_backup(const char *job_id, const char *device, 314570559d49SAlberto Garcia const char *target, bool has_format, 314670559d49SAlberto Garcia const char *format, enum MirrorSyncMode sync, 314799a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 314899a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 3149d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 315078f51fdeSJohn Snow bool has_on_source_error, 315178f51fdeSJohn Snow BlockdevOnError on_source_error, 315278f51fdeSJohn Snow bool has_on_target_error, 315378f51fdeSJohn Snow BlockdevOnError on_target_error, 315478f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 315599a9addfSStefan Hajnoczi { 3156a0e8544cSFam Zheng BlockBackend *blk; 315799a9addfSStefan Hajnoczi BlockDriverState *bs; 315899a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3159fc5d3f84SIan Main BlockDriverState *source = NULL; 3160d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3161761731b1SStefan Hajnoczi AioContext *aio_context; 3162e6641719SMax Reitz QDict *options = NULL; 316399a9addfSStefan Hajnoczi Error *local_err = NULL; 316499a9addfSStefan Hajnoczi int flags; 316599a9addfSStefan Hajnoczi int64_t size; 316699a9addfSStefan Hajnoczi 316799a9addfSStefan Hajnoczi if (!has_speed) { 316899a9addfSStefan Hajnoczi speed = 0; 316999a9addfSStefan Hajnoczi } 317099a9addfSStefan Hajnoczi if (!has_on_source_error) { 317199a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 317299a9addfSStefan Hajnoczi } 317399a9addfSStefan Hajnoczi if (!has_on_target_error) { 317499a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 317599a9addfSStefan Hajnoczi } 317699a9addfSStefan Hajnoczi if (!has_mode) { 317799a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 317899a9addfSStefan Hajnoczi } 317999a9addfSStefan Hajnoczi 3180a0e8544cSFam Zheng blk = blk_by_name(device); 3181a0e8544cSFam Zheng if (!blk) { 318275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 318375158ebbSMarkus Armbruster "Device '%s' not found", device); 318499a9addfSStefan Hajnoczi return; 318599a9addfSStefan Hajnoczi } 318699a9addfSStefan Hajnoczi 31875433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3188761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3189761731b1SStefan Hajnoczi 3190c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 3191c29c1dd3SFam Zheng * do an early check redundantly. */ 31925433c24fSMax Reitz if (!blk_is_available(blk)) { 3193c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3194761731b1SStefan Hajnoczi goto out; 319599a9addfSStefan Hajnoczi } 31965433c24fSMax Reitz bs = blk_bs(blk); 319799a9addfSStefan Hajnoczi 319899a9addfSStefan Hajnoczi if (!has_format) { 319999a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 320099a9addfSStefan Hajnoczi } 320199a9addfSStefan Hajnoczi 3202c29c1dd3SFam Zheng /* Early check to avoid creating target */ 32033718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3204761731b1SStefan Hajnoczi goto out; 320599a9addfSStefan Hajnoczi } 320699a9addfSStefan Hajnoczi 320761de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 320899a9addfSStefan Hajnoczi 3209fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3210fc5d3f84SIan Main * on top of. */ 3211fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 3212760e0063SKevin Wolf source = backing_bs(bs); 3213fc5d3f84SIan Main if (!source) { 3214fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 3215fc5d3f84SIan Main } 3216fc5d3f84SIan Main } 3217fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 3218fc5d3f84SIan Main source = bs; 3219fc5d3f84SIan Main } 3220fc5d3f84SIan Main 322199a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 322299a9addfSStefan Hajnoczi if (size < 0) { 322399a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3224761731b1SStefan Hajnoczi goto out; 322599a9addfSStefan Hajnoczi } 322699a9addfSStefan Hajnoczi 322799a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 3228e6641719SMax Reitz assert(format); 3229fc5d3f84SIan Main if (source) { 3230fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 3231fc5d3f84SIan Main source->drv->format_name, NULL, 3232fc5d3f84SIan Main size, flags, &local_err, false); 3233fc5d3f84SIan Main } else { 3234fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 3235fc5d3f84SIan Main size, flags, &local_err, false); 3236fc5d3f84SIan Main } 323799a9addfSStefan Hajnoczi } 323899a9addfSStefan Hajnoczi 323984d18f06SMarkus Armbruster if (local_err) { 324099a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3241761731b1SStefan Hajnoczi goto out; 324299a9addfSStefan Hajnoczi } 324399a9addfSStefan Hajnoczi 3244e6641719SMax Reitz if (format) { 3245e6641719SMax Reitz options = qdict_new(); 3246e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3247e6641719SMax Reitz } 3248e6641719SMax Reitz 32495b363937SMax Reitz target_bs = bdrv_open(target, NULL, options, flags, errp); 32505b363937SMax Reitz if (!target_bs) { 3251761731b1SStefan Hajnoczi goto out; 325299a9addfSStefan Hajnoczi } 325399a9addfSStefan Hajnoczi 3254761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3255761731b1SStefan Hajnoczi 3256d58d8453SJohn Snow if (has_bitmap) { 3257d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3258d58d8453SJohn Snow if (!bmap) { 3259d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 32600702d3d8SMax Reitz bdrv_unref(target_bs); 3261d58d8453SJohn Snow goto out; 3262d58d8453SJohn Snow } 3263d58d8453SJohn Snow } 3264d58d8453SJohn Snow 326570559d49SAlberto Garcia backup_start(job_id, bs, target_bs, speed, sync, bmap, 3266d58d8453SJohn Snow on_source_error, on_target_error, 326778f51fdeSJohn Snow block_job_cb, bs, txn, &local_err); 32684f6fd349SFam Zheng bdrv_unref(target_bs); 32695c438bc6SKevin Wolf if (local_err != NULL) { 327099a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3271761731b1SStefan Hajnoczi goto out; 327299a9addfSStefan Hajnoczi } 3273761731b1SStefan Hajnoczi 3274761731b1SStefan Hajnoczi out: 3275761731b1SStefan Hajnoczi aio_context_release(aio_context); 327699a9addfSStefan Hajnoczi } 327799a9addfSStefan Hajnoczi 327870559d49SAlberto Garcia void qmp_drive_backup(bool has_job_id, const char *job_id, 327970559d49SAlberto Garcia const char *device, const char *target, 328078f51fdeSJohn Snow bool has_format, const char *format, 328178f51fdeSJohn Snow enum MirrorSyncMode sync, 328278f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 328378f51fdeSJohn Snow bool has_speed, int64_t speed, 328478f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 328578f51fdeSJohn Snow bool has_on_source_error, BlockdevOnError on_source_error, 328678f51fdeSJohn Snow bool has_on_target_error, BlockdevOnError on_target_error, 328778f51fdeSJohn Snow Error **errp) 328878f51fdeSJohn Snow { 328970559d49SAlberto Garcia return do_drive_backup(has_job_id ? job_id : NULL, device, target, 329070559d49SAlberto Garcia has_format, format, sync, 329178f51fdeSJohn Snow has_mode, mode, has_speed, speed, 329278f51fdeSJohn Snow has_bitmap, bitmap, 329378f51fdeSJohn Snow has_on_source_error, on_source_error, 329478f51fdeSJohn Snow has_on_target_error, on_target_error, 329578f51fdeSJohn Snow NULL, errp); 329678f51fdeSJohn Snow } 329778f51fdeSJohn Snow 3298c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3299c13163fbSBenoît Canet { 3300d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3301c13163fbSBenoît Canet } 3302c13163fbSBenoît Canet 330370559d49SAlberto Garcia void do_blockdev_backup(const char *job_id, const char *device, 330470559d49SAlberto Garcia const char *target, enum MirrorSyncMode sync, 3305c29c1dd3SFam Zheng bool has_speed, int64_t speed, 3306c29c1dd3SFam Zheng bool has_on_source_error, 3307c29c1dd3SFam Zheng BlockdevOnError on_source_error, 3308c29c1dd3SFam Zheng bool has_on_target_error, 3309c29c1dd3SFam Zheng BlockdevOnError on_target_error, 331078f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 3311c29c1dd3SFam Zheng { 3312c29c1dd3SFam Zheng BlockDriverState *bs; 3313c29c1dd3SFam Zheng BlockDriverState *target_bs; 3314c29c1dd3SFam Zheng Error *local_err = NULL; 3315c29c1dd3SFam Zheng AioContext *aio_context; 3316c29c1dd3SFam Zheng 3317c29c1dd3SFam Zheng if (!has_speed) { 3318c29c1dd3SFam Zheng speed = 0; 3319c29c1dd3SFam Zheng } 3320c29c1dd3SFam Zheng if (!has_on_source_error) { 3321c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3322c29c1dd3SFam Zheng } 3323c29c1dd3SFam Zheng if (!has_on_target_error) { 3324c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3325c29c1dd3SFam Zheng } 3326c29c1dd3SFam Zheng 3327cef34eebSKevin Wolf bs = qmp_get_root_bs(device, errp); 3328cef34eebSKevin Wolf if (!bs) { 3329c29c1dd3SFam Zheng return; 3330c29c1dd3SFam Zheng } 3331c29c1dd3SFam Zheng 3332cef34eebSKevin Wolf aio_context = bdrv_get_aio_context(bs); 3333c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3334c29c1dd3SFam Zheng 33350d978913SFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 33360d978913SFam Zheng if (!target_bs) { 3337c29c1dd3SFam Zheng goto out; 3338c29c1dd3SFam Zheng } 33395433c24fSMax Reitz 3340efd75567SFam Zheng if (bdrv_get_aio_context(target_bs) != aio_context) { 3341efd75567SFam Zheng if (!bdrv_has_blk(target_bs)) { 3342efd75567SFam Zheng /* The target BDS is not attached, we can safely move it to another 3343efd75567SFam Zheng * AioContext. */ 3344c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3345efd75567SFam Zheng } else { 3346efd75567SFam Zheng error_setg(errp, "Target is attached to a different thread from " 3347efd75567SFam Zheng "source."); 3348efd75567SFam Zheng goto out; 3349efd75567SFam Zheng } 3350efd75567SFam Zheng } 335170559d49SAlberto Garcia backup_start(job_id, bs, target_bs, speed, sync, NULL, on_source_error, 335278f51fdeSJohn Snow on_target_error, block_job_cb, bs, txn, &local_err); 3353c29c1dd3SFam Zheng if (local_err != NULL) { 3354c29c1dd3SFam Zheng error_propagate(errp, local_err); 3355c29c1dd3SFam Zheng } 3356c29c1dd3SFam Zheng out: 3357c29c1dd3SFam Zheng aio_context_release(aio_context); 3358c29c1dd3SFam Zheng } 3359c29c1dd3SFam Zheng 336070559d49SAlberto Garcia void qmp_blockdev_backup(bool has_job_id, const char *job_id, 336170559d49SAlberto Garcia const char *device, const char *target, 336278f51fdeSJohn Snow enum MirrorSyncMode sync, 336378f51fdeSJohn Snow bool has_speed, int64_t speed, 336478f51fdeSJohn Snow bool has_on_source_error, 336578f51fdeSJohn Snow BlockdevOnError on_source_error, 336678f51fdeSJohn Snow bool has_on_target_error, 336778f51fdeSJohn Snow BlockdevOnError on_target_error, 336878f51fdeSJohn Snow Error **errp) 336978f51fdeSJohn Snow { 337070559d49SAlberto Garcia do_blockdev_backup(has_job_id ? job_id : NULL, device, target, 337170559d49SAlberto Garcia sync, has_speed, speed, 337278f51fdeSJohn Snow has_on_source_error, on_source_error, 337378f51fdeSJohn Snow has_on_target_error, on_target_error, 337478f51fdeSJohn Snow NULL, errp); 337578f51fdeSJohn Snow } 337678f51fdeSJohn Snow 33774193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 33784193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 33794193cdd7SFam Zheng **/ 338071aa9867SAlberto Garcia static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, 33814193cdd7SFam Zheng BlockDriverState *target, 338209158f00SBenoît Canet bool has_replaces, const char *replaces, 3383d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3384274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 3385b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3386eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 338708e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 33884193cdd7SFam Zheng bool has_on_source_error, 33894193cdd7SFam Zheng BlockdevOnError on_source_error, 33904193cdd7SFam Zheng bool has_on_target_error, 33914193cdd7SFam Zheng BlockdevOnError on_target_error, 33920fc9f8eaSFam Zheng bool has_unmap, bool unmap, 3393b952b558SPaolo Bonzini Error **errp) 3394d9b902dbSPaolo Bonzini { 3395d9b902dbSPaolo Bonzini 3396d9b902dbSPaolo Bonzini if (!has_speed) { 3397d9b902dbSPaolo Bonzini speed = 0; 3398d9b902dbSPaolo Bonzini } 3399b952b558SPaolo Bonzini if (!has_on_source_error) { 3400b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3401b952b558SPaolo Bonzini } 3402b952b558SPaolo Bonzini if (!has_on_target_error) { 3403b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3404b952b558SPaolo Bonzini } 3405eee13dfeSPaolo Bonzini if (!has_granularity) { 3406eee13dfeSPaolo Bonzini granularity = 0; 3407eee13dfeSPaolo Bonzini } 340808e4ed6cSPaolo Bonzini if (!has_buf_size) { 340948ac0a4dSWen Congyang buf_size = 0; 341008e4ed6cSPaolo Bonzini } 34110fc9f8eaSFam Zheng if (!has_unmap) { 34120fc9f8eaSFam Zheng unmap = true; 34130fc9f8eaSFam Zheng } 341408e4ed6cSPaolo Bonzini 3415eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3416c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 34173cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3418eee13dfeSPaolo Bonzini return; 3419eee13dfeSPaolo Bonzini } 3420eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3421c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3422c6bd8c70SMarkus Armbruster "power of 2"); 3423eee13dfeSPaolo Bonzini return; 3424eee13dfeSPaolo Bonzini } 3425d9b902dbSPaolo Bonzini 34264193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 34274193cdd7SFam Zheng return; 34284193cdd7SFam Zheng } 3429e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3430e40e5027SFam Zheng return; 3431e40e5027SFam Zheng } 34324193cdd7SFam Zheng 34334193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 34344193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 34354193cdd7SFam Zheng } 34364193cdd7SFam Zheng 34374193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 34384193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 34394193cdd7SFam Zheng */ 344071aa9867SAlberto Garcia mirror_start(job_id, bs, target, 34414193cdd7SFam Zheng has_replaces ? replaces : NULL, 3442274fcceeSMax Reitz speed, granularity, buf_size, sync, backing_mode, 34434193cdd7SFam Zheng on_source_error, on_target_error, unmap, 34444193cdd7SFam Zheng block_job_cb, bs, errp); 34454193cdd7SFam Zheng } 34464193cdd7SFam Zheng 3447faecd40aSEric Blake void qmp_drive_mirror(DriveMirror *arg, Error **errp) 34484193cdd7SFam Zheng { 34494193cdd7SFam Zheng BlockDriverState *bs; 34504193cdd7SFam Zheng BlockBackend *blk; 34514193cdd7SFam Zheng BlockDriverState *source, *target_bs; 34524193cdd7SFam Zheng AioContext *aio_context; 3453274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 34544193cdd7SFam Zheng Error *local_err = NULL; 34554193cdd7SFam Zheng QDict *options = NULL; 34564193cdd7SFam Zheng int flags; 34574193cdd7SFam Zheng int64_t size; 3458faecd40aSEric Blake const char *format = arg->format; 34594193cdd7SFam Zheng 3460faecd40aSEric Blake blk = blk_by_name(arg->device); 3461a0e8544cSFam Zheng if (!blk) { 346275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 3463faecd40aSEric Blake "Device '%s' not found", arg->device); 3464d9b902dbSPaolo Bonzini return; 3465d9b902dbSPaolo Bonzini } 3466d9b902dbSPaolo Bonzini 34675433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 34685a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 34695a7e7a0bSStefan Hajnoczi 34705433c24fSMax Reitz if (!blk_is_available(blk)) { 3471faecd40aSEric Blake error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, arg->device); 34725a7e7a0bSStefan Hajnoczi goto out; 3473d9b902dbSPaolo Bonzini } 34745433c24fSMax Reitz bs = blk_bs(blk); 3475faecd40aSEric Blake if (!arg->has_mode) { 3476faecd40aSEric Blake arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 34774193cdd7SFam Zheng } 3478d9b902dbSPaolo Bonzini 3479faecd40aSEric Blake if (!arg->has_format) { 3480faecd40aSEric Blake format = (arg->mode == NEW_IMAGE_MODE_EXISTING 3481faecd40aSEric Blake ? NULL : bs->drv->format_name); 3482d9b902dbSPaolo Bonzini } 3483d9b902dbSPaolo Bonzini 348461de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3485760e0063SKevin Wolf source = backing_bs(bs); 3486faecd40aSEric Blake if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { 3487faecd40aSEric Blake arg->sync = MIRROR_SYNC_MODE_FULL; 3488d9b902dbSPaolo Bonzini } 3489faecd40aSEric Blake if (arg->sync == MIRROR_SYNC_MODE_NONE) { 3490117e0c82SMax Reitz source = bs; 3491117e0c82SMax Reitz } 3492d9b902dbSPaolo Bonzini 3493ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3494ac3c5d83SStefan Hajnoczi if (size < 0) { 3495ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 34965a7e7a0bSStefan Hajnoczi goto out; 3497ac3c5d83SStefan Hajnoczi } 3498ac3c5d83SStefan Hajnoczi 3499faecd40aSEric Blake if (arg->has_replaces) { 350009158f00SBenoît Canet BlockDriverState *to_replace_bs; 35015a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 35025a7e7a0bSStefan Hajnoczi int64_t replace_size; 350309158f00SBenoît Canet 3504faecd40aSEric Blake if (!arg->has_node_name) { 350509158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 350609158f00SBenoît Canet " named node of the graph"); 35075a7e7a0bSStefan Hajnoczi goto out; 350809158f00SBenoît Canet } 350909158f00SBenoît Canet 3510faecd40aSEric Blake to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err); 351109158f00SBenoît Canet 351209158f00SBenoît Canet if (!to_replace_bs) { 351309158f00SBenoît Canet error_propagate(errp, local_err); 35145a7e7a0bSStefan Hajnoczi goto out; 351509158f00SBenoît Canet } 351609158f00SBenoît Canet 35175a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 35185a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 35195a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 35205a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 35215a7e7a0bSStefan Hajnoczi 35225a7e7a0bSStefan Hajnoczi if (size != replace_size) { 352309158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 352409158f00SBenoît Canet "different size"); 35255a7e7a0bSStefan Hajnoczi goto out; 352609158f00SBenoît Canet } 352709158f00SBenoît Canet } 352809158f00SBenoît Canet 3529faecd40aSEric Blake if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { 3530274fcceeSMax Reitz backing_mode = MIRROR_SOURCE_BACKING_CHAIN; 3531274fcceeSMax Reitz } else { 3532274fcceeSMax Reitz backing_mode = MIRROR_OPEN_BACKING_CHAIN; 3533274fcceeSMax Reitz } 3534274fcceeSMax Reitz 3535faecd40aSEric Blake if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) 3536faecd40aSEric Blake && arg->mode != NEW_IMAGE_MODE_EXISTING) 353714526864SMax Reitz { 3538d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3539e6641719SMax Reitz assert(format); 3540faecd40aSEric Blake bdrv_img_create(arg->target, format, 3541f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3542d9b902dbSPaolo Bonzini } else { 3543faecd40aSEric Blake switch (arg->mode) { 3544d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3545d9b902dbSPaolo Bonzini break; 3546d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3547d9b902dbSPaolo Bonzini /* create new image with backing file */ 3548faecd40aSEric Blake bdrv_img_create(arg->target, format, 3549d9b902dbSPaolo Bonzini source->filename, 3550d9b902dbSPaolo Bonzini source->drv->format_name, 3551f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3552d9b902dbSPaolo Bonzini break; 3553d9b902dbSPaolo Bonzini default: 3554d9b902dbSPaolo Bonzini abort(); 3555d9b902dbSPaolo Bonzini } 3556d9b902dbSPaolo Bonzini } 3557d9b902dbSPaolo Bonzini 355884d18f06SMarkus Armbruster if (local_err) { 3559cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 35605a7e7a0bSStefan Hajnoczi goto out; 3561d9b902dbSPaolo Bonzini } 3562d9b902dbSPaolo Bonzini 35634c828dc6SBenoît Canet options = qdict_new(); 3564faecd40aSEric Blake if (arg->has_node_name) { 3565faecd40aSEric Blake qdict_put(options, "node-name", qstring_from_str(arg->node_name)); 35664c828dc6SBenoît Canet } 3567e6641719SMax Reitz if (format) { 3568e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3569e6641719SMax Reitz } 35704c828dc6SBenoît Canet 3571b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3572b812f671SPaolo Bonzini * file. 3573b812f671SPaolo Bonzini */ 3574faecd40aSEric Blake target_bs = bdrv_open(arg->target, NULL, options, 3575faecd40aSEric Blake flags | BDRV_O_NO_BACKING, errp); 35765b363937SMax Reitz if (!target_bs) { 35775a7e7a0bSStefan Hajnoczi goto out; 3578d9b902dbSPaolo Bonzini } 3579d9b902dbSPaolo Bonzini 35805a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 35815a7e7a0bSStefan Hajnoczi 3582faecd40aSEric Blake blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, 3583faecd40aSEric Blake arg->has_replaces, arg->replaces, arg->sync, 3584faecd40aSEric Blake backing_mode, arg->has_speed, arg->speed, 3585faecd40aSEric Blake arg->has_granularity, arg->granularity, 3586faecd40aSEric Blake arg->has_buf_size, arg->buf_size, 3587faecd40aSEric Blake arg->has_on_source_error, arg->on_source_error, 3588faecd40aSEric Blake arg->has_on_target_error, arg->on_target_error, 3589faecd40aSEric Blake arg->has_unmap, arg->unmap, 35904193cdd7SFam Zheng &local_err); 3591e253f4b8SKevin Wolf bdrv_unref(target_bs); 3592d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 35935a7e7a0bSStefan Hajnoczi out: 35945a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3595d9b902dbSPaolo Bonzini } 3596d9b902dbSPaolo Bonzini 359771aa9867SAlberto Garcia void qmp_blockdev_mirror(bool has_job_id, const char *job_id, 359871aa9867SAlberto Garcia const char *device, const char *target, 3599df92562eSFam Zheng bool has_replaces, const char *replaces, 3600df92562eSFam Zheng MirrorSyncMode sync, 3601df92562eSFam Zheng bool has_speed, int64_t speed, 3602df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3603df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3604df92562eSFam Zheng bool has_on_source_error, 3605df92562eSFam Zheng BlockdevOnError on_source_error, 3606df92562eSFam Zheng bool has_on_target_error, 3607df92562eSFam Zheng BlockdevOnError on_target_error, 3608df92562eSFam Zheng Error **errp) 3609df92562eSFam Zheng { 3610df92562eSFam Zheng BlockDriverState *bs; 3611df92562eSFam Zheng BlockDriverState *target_bs; 3612df92562eSFam Zheng AioContext *aio_context; 3613274fcceeSMax Reitz BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; 3614df92562eSFam Zheng Error *local_err = NULL; 3615df92562eSFam Zheng 361607eec652SKevin Wolf bs = qmp_get_root_bs(device, errp); 3617df92562eSFam Zheng if (!bs) { 3618df92562eSFam Zheng return; 3619df92562eSFam Zheng } 3620df92562eSFam Zheng 3621df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3622df92562eSFam Zheng if (!target_bs) { 3623df92562eSFam Zheng return; 3624df92562eSFam Zheng } 3625df92562eSFam Zheng 3626df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3627df92562eSFam Zheng aio_context_acquire(aio_context); 3628df92562eSFam Zheng 3629df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3630df92562eSFam Zheng 363171aa9867SAlberto Garcia blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 3632274fcceeSMax Reitz has_replaces, replaces, sync, backing_mode, 3633df92562eSFam Zheng has_speed, speed, 3634df92562eSFam Zheng has_granularity, granularity, 3635df92562eSFam Zheng has_buf_size, buf_size, 3636df92562eSFam Zheng has_on_source_error, on_source_error, 3637df92562eSFam Zheng has_on_target_error, on_target_error, 3638df92562eSFam Zheng true, true, 3639df92562eSFam Zheng &local_err); 3640df92562eSFam Zheng error_propagate(errp, local_err); 3641df92562eSFam Zheng 3642df92562eSFam Zheng aio_context_release(aio_context); 3643df92562eSFam Zheng } 3644df92562eSFam Zheng 36453ddf3efeSAlberto Garcia /* Get a block job using its ID and acquire its AioContext */ 36463ddf3efeSAlberto Garcia static BlockJob *find_block_job(const char *id, AioContext **aio_context, 364724d6bffeSMarkus Armbruster Error **errp) 36482d47c6e9SStefan Hajnoczi { 36493ddf3efeSAlberto Garcia BlockJob *job; 36503ddf3efeSAlberto Garcia 36513ddf3efeSAlberto Garcia assert(id != NULL); 36522d47c6e9SStefan Hajnoczi 36535433c24fSMax Reitz *aio_context = NULL; 36545433c24fSMax Reitz 36553ddf3efeSAlberto Garcia job = block_job_get(id); 36563ddf3efeSAlberto Garcia 36573ddf3efeSAlberto Garcia if (!job) { 36583ddf3efeSAlberto Garcia error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 36593ddf3efeSAlberto Garcia "Block job '%s' not found", id); 36603ddf3efeSAlberto Garcia return NULL; 36612d47c6e9SStefan Hajnoczi } 36623d948cdfSStefan Hajnoczi 36633ddf3efeSAlberto Garcia *aio_context = blk_get_aio_context(job->blk); 36643d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 36653d948cdfSStefan Hajnoczi 36663ddf3efeSAlberto Garcia return job; 36672d47c6e9SStefan Hajnoczi } 36682d47c6e9SStefan Hajnoczi 3669882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 36702d47c6e9SStefan Hajnoczi { 36713d948cdfSStefan Hajnoczi AioContext *aio_context; 367224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36732d47c6e9SStefan Hajnoczi 36742d47c6e9SStefan Hajnoczi if (!job) { 36752d47c6e9SStefan Hajnoczi return; 36762d47c6e9SStefan Hajnoczi } 36772d47c6e9SStefan Hajnoczi 3678882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 36793d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36802d47c6e9SStefan Hajnoczi } 3681370521a1SStefan Hajnoczi 36826e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 36836e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 36846e37fb81SPaolo Bonzini { 36853d948cdfSStefan Hajnoczi AioContext *aio_context; 368624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36876e37fb81SPaolo Bonzini 36886e37fb81SPaolo Bonzini if (!job) { 36896e37fb81SPaolo Bonzini return; 36906e37fb81SPaolo Bonzini } 36913d948cdfSStefan Hajnoczi 36923d948cdfSStefan Hajnoczi if (!has_force) { 36933d948cdfSStefan Hajnoczi force = false; 36943d948cdfSStefan Hajnoczi } 36953d948cdfSStefan Hajnoczi 3696751ebd76SFam Zheng if (job->user_paused && !force) { 3697f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3698f231b88dSCole Robinson device); 36993d948cdfSStefan Hajnoczi goto out; 37006e37fb81SPaolo Bonzini } 37016e37fb81SPaolo Bonzini 37026e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 37036e37fb81SPaolo Bonzini block_job_cancel(job); 37043d948cdfSStefan Hajnoczi out: 37053d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37066e37fb81SPaolo Bonzini } 37076e37fb81SPaolo Bonzini 37086e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3709370521a1SStefan Hajnoczi { 37103d948cdfSStefan Hajnoczi AioContext *aio_context; 371124d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3712370521a1SStefan Hajnoczi 3713751ebd76SFam Zheng if (!job || job->user_paused) { 3714370521a1SStefan Hajnoczi return; 3715370521a1SStefan Hajnoczi } 37166e37fb81SPaolo Bonzini 3717751ebd76SFam Zheng job->user_paused = true; 37186e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 37196e37fb81SPaolo Bonzini block_job_pause(job); 37203d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37216e37fb81SPaolo Bonzini } 37226e37fb81SPaolo Bonzini 37236e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 37246e37fb81SPaolo Bonzini { 37253d948cdfSStefan Hajnoczi AioContext *aio_context; 372624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37276e37fb81SPaolo Bonzini 3728751ebd76SFam Zheng if (!job || !job->user_paused) { 37298acc72a4SPaolo Bonzini return; 37308acc72a4SPaolo Bonzini } 3731370521a1SStefan Hajnoczi 3732751ebd76SFam Zheng job->user_paused = false; 37336e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 373417bd51f9SStefan Hajnoczi block_job_iostatus_reset(job); 37356e37fb81SPaolo Bonzini block_job_resume(job); 37363d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3737370521a1SStefan Hajnoczi } 3738fb5458cdSStefan Hajnoczi 3739aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3740aeae883bSPaolo Bonzini { 37413d948cdfSStefan Hajnoczi AioContext *aio_context; 374224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3743aeae883bSPaolo Bonzini 3744aeae883bSPaolo Bonzini if (!job) { 3745aeae883bSPaolo Bonzini return; 3746aeae883bSPaolo Bonzini } 3747aeae883bSPaolo Bonzini 3748aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3749aeae883bSPaolo Bonzini block_job_complete(job, errp); 37503d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3751aeae883bSPaolo Bonzini } 3752aeae883bSPaolo Bonzini 3753fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3754fa40e656SJeff Cody const char *image_node_name, 3755fa40e656SJeff Cody const char *backing_file, 3756fa40e656SJeff Cody Error **errp) 3757fa40e656SJeff Cody { 3758a0e8544cSFam Zheng BlockBackend *blk; 3759fa40e656SJeff Cody BlockDriverState *bs = NULL; 3760729962f6SStefan Hajnoczi AioContext *aio_context; 3761fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3762fa40e656SJeff Cody Error *local_err = NULL; 3763fa40e656SJeff Cody bool ro; 3764fa40e656SJeff Cody int open_flags; 3765fa40e656SJeff Cody int ret; 3766fa40e656SJeff Cody 3767a0e8544cSFam Zheng blk = blk_by_name(device); 3768a0e8544cSFam Zheng if (!blk) { 376975158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 377075158ebbSMarkus Armbruster "Device '%s' not found", device); 3771fa40e656SJeff Cody return; 3772fa40e656SJeff Cody } 3773fa40e656SJeff Cody 37745433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3775729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3776729962f6SStefan Hajnoczi 37775433c24fSMax Reitz if (!blk_is_available(blk)) { 37785433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 37795433c24fSMax Reitz goto out; 37805433c24fSMax Reitz } 37815433c24fSMax Reitz bs = blk_bs(blk); 37825433c24fSMax Reitz 3783fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3784fa40e656SJeff Cody if (local_err) { 3785fa40e656SJeff Cody error_propagate(errp, local_err); 3786729962f6SStefan Hajnoczi goto out; 3787fa40e656SJeff Cody } 3788fa40e656SJeff Cody 3789fa40e656SJeff Cody if (!image_bs) { 3790fa40e656SJeff Cody error_setg(errp, "image file not found"); 3791729962f6SStefan Hajnoczi goto out; 3792fa40e656SJeff Cody } 3793fa40e656SJeff Cody 3794fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3795fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3796fa40e656SJeff Cody "without a backing file"); 3797729962f6SStefan Hajnoczi goto out; 3798fa40e656SJeff Cody } 3799fa40e656SJeff Cody 3800fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3801fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3802fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3803729962f6SStefan Hajnoczi goto out; 3804fa40e656SJeff Cody } 3805fa40e656SJeff Cody 3806fa40e656SJeff Cody /* final sanity check */ 3807fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3808fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3809fa40e656SJeff Cody device); 3810729962f6SStefan Hajnoczi goto out; 3811fa40e656SJeff Cody } 3812fa40e656SJeff Cody 3813fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3814fa40e656SJeff Cody open_flags = image_bs->open_flags; 3815fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3816fa40e656SJeff Cody 3817fa40e656SJeff Cody if (ro) { 3818fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3819fa40e656SJeff Cody if (local_err) { 3820fa40e656SJeff Cody error_propagate(errp, local_err); 3821729962f6SStefan Hajnoczi goto out; 3822fa40e656SJeff Cody } 3823fa40e656SJeff Cody } 3824fa40e656SJeff Cody 3825fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3826fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3827fa40e656SJeff Cody 3828fa40e656SJeff Cody if (ret < 0) { 3829fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3830fa40e656SJeff Cody backing_file); 3831fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3832fa40e656SJeff Cody * appropriate */ 3833fa40e656SJeff Cody } 3834fa40e656SJeff Cody 3835fa40e656SJeff Cody if (ro) { 3836fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3837621ff94dSEduardo Habkost error_propagate(errp, local_err); 3838fa40e656SJeff Cody } 3839729962f6SStefan Hajnoczi 3840729962f6SStefan Hajnoczi out: 3841729962f6SStefan Hajnoczi aio_context_release(aio_context); 3842fa40e656SJeff Cody } 3843fa40e656SJeff Cody 3844abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 3845abb21ac3SKevin Wolf { 3846abb21ac3SKevin Wolf QemuOpts *opts; 3847abb21ac3SKevin Wolf QDict *qdict; 3848abb21ac3SKevin Wolf Error *local_err = NULL; 3849abb21ac3SKevin Wolf 3850abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 3851abb21ac3SKevin Wolf if (!opts) { 3852abb21ac3SKevin Wolf return; 3853abb21ac3SKevin Wolf } 3854abb21ac3SKevin Wolf 3855abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 3856abb21ac3SKevin Wolf 3857abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 3858f8746fb8SKevin Wolf QDECREF(qdict); 3859abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 3860abb21ac3SKevin Wolf goto out; 3861abb21ac3SKevin Wolf } 3862abb21ac3SKevin Wolf 3863abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 3864abb21ac3SKevin Wolf if (!bs) { 3865abb21ac3SKevin Wolf error_report_err(local_err); 3866abb21ac3SKevin Wolf goto out; 3867abb21ac3SKevin Wolf } 3868abb21ac3SKevin Wolf 3869abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3870abb21ac3SKevin Wolf 3871abb21ac3SKevin Wolf out: 3872abb21ac3SKevin Wolf qemu_opts_del(opts); 3873abb21ac3SKevin Wolf } 3874abb21ac3SKevin Wolf 3875d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3876d26c9a15SKevin Wolf { 3877be4b67bcSMax Reitz BlockDriverState *bs; 3878be4b67bcSMax Reitz BlockBackend *blk = NULL; 3879d26c9a15SKevin Wolf QObject *obj; 38803b098d56SEric Blake Visitor *v = qmp_output_visitor_new(&obj); 3881d26c9a15SKevin Wolf QDict *qdict; 3882d26c9a15SKevin Wolf Error *local_err = NULL; 3883d26c9a15SKevin Wolf 388460e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3885d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 388660e19e06SMarkus Armbruster * when called from drive_new(). 3887d26c9a15SKevin Wolf * 3888d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3889d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3890c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3891c6e0bd9bSKevin Wolf options->cache->has_direct && 3892c6e0bd9bSKevin Wolf options->cache->direct; 3893c6e0bd9bSKevin Wolf if (!direct) { 3894d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3895d26c9a15SKevin Wolf goto fail; 3896d26c9a15SKevin Wolf } 3897d26c9a15SKevin Wolf } 3898d26c9a15SKevin Wolf 38993b098d56SEric Blake visit_type_BlockdevOptions(v, NULL, &options, &local_err); 390084d18f06SMarkus Armbruster if (local_err) { 3901d26c9a15SKevin Wolf error_propagate(errp, local_err); 3902d26c9a15SKevin Wolf goto fail; 3903d26c9a15SKevin Wolf } 3904d26c9a15SKevin Wolf 39053b098d56SEric Blake visit_complete(v, &obj); 3906d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3907d26c9a15SKevin Wolf 3908d26c9a15SKevin Wolf qdict_flatten(qdict); 3909d26c9a15SKevin Wolf 3910be4b67bcSMax Reitz if (options->has_id) { 391118e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 391284d18f06SMarkus Armbruster if (local_err) { 3913b681072dSKevin Wolf error_propagate(errp, local_err); 3914d26c9a15SKevin Wolf goto fail; 3915d26c9a15SKevin Wolf } 3916d26c9a15SKevin Wolf 3917be4b67bcSMax Reitz bs = blk_bs(blk); 3918be4b67bcSMax Reitz } else { 3919be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3920be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3921be4b67bcSMax Reitz "the root node"); 3922be4b67bcSMax Reitz goto fail; 3923be4b67bcSMax Reitz } 3924be4b67bcSMax Reitz 3925bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3926bd745e23SMax Reitz if (!bs) { 3927be4b67bcSMax Reitz goto fail; 3928be4b67bcSMax Reitz } 39299c4218e9SMax Reitz 39309c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3931be4b67bcSMax Reitz } 3932be4b67bcSMax Reitz 3933be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 3934be4b67bcSMax Reitz if (blk) { 3935efaa7c4eSMax Reitz monitor_remove_blk(blk); 393618e46a03SMarkus Armbruster blk_unref(blk); 3937be4b67bcSMax Reitz } else { 39389c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 3939be4b67bcSMax Reitz bdrv_unref(bs); 3940be4b67bcSMax Reitz } 39418ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 39428ae8e904SKevin Wolf goto fail; 39438ae8e904SKevin Wolf } 39448ae8e904SKevin Wolf 3945d26c9a15SKevin Wolf fail: 39463b098d56SEric Blake visit_free(v); 3947d26c9a15SKevin Wolf } 3948d26c9a15SKevin Wolf 394981b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id, 395081b936aeSAlberto Garcia bool has_node_name, const char *node_name, Error **errp) 395181b936aeSAlberto Garcia { 395281b936aeSAlberto Garcia AioContext *aio_context; 395381b936aeSAlberto Garcia BlockBackend *blk; 395481b936aeSAlberto Garcia BlockDriverState *bs; 395581b936aeSAlberto Garcia 395681b936aeSAlberto Garcia if (has_id && has_node_name) { 395781b936aeSAlberto Garcia error_setg(errp, "Only one of id and node-name must be specified"); 395881b936aeSAlberto Garcia return; 395981b936aeSAlberto Garcia } else if (!has_id && !has_node_name) { 396081b936aeSAlberto Garcia error_setg(errp, "No block device specified"); 396181b936aeSAlberto Garcia return; 396281b936aeSAlberto Garcia } 396381b936aeSAlberto Garcia 396481b936aeSAlberto Garcia if (has_id) { 3965efaa7c4eSMax Reitz /* blk_by_name() never returns a BB that is not owned by the monitor */ 396681b936aeSAlberto Garcia blk = blk_by_name(id); 396781b936aeSAlberto Garcia if (!blk) { 396881b936aeSAlberto Garcia error_setg(errp, "Cannot find block backend %s", id); 396981b936aeSAlberto Garcia return; 397081b936aeSAlberto Garcia } 39715cf87fd6SPaolo Bonzini if (blk_legacy_dinfo(blk)) { 39725cf87fd6SPaolo Bonzini error_setg(errp, "Deleting block backend added with drive-add" 39735cf87fd6SPaolo Bonzini " is not supported"); 39745cf87fd6SPaolo Bonzini return; 39755cf87fd6SPaolo Bonzini } 397681b936aeSAlberto Garcia if (blk_get_refcnt(blk) > 1) { 397781b936aeSAlberto Garcia error_setg(errp, "Block backend %s is in use", id); 397881b936aeSAlberto Garcia return; 397981b936aeSAlberto Garcia } 398081b936aeSAlberto Garcia bs = blk_bs(blk); 398181b936aeSAlberto Garcia aio_context = blk_get_aio_context(blk); 398281b936aeSAlberto Garcia } else { 39831f0c461bSKevin Wolf blk = NULL; 398481b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 398581b936aeSAlberto Garcia if (!bs) { 398681b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 398781b936aeSAlberto Garcia return; 398881b936aeSAlberto Garcia } 39891f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 399081b936aeSAlberto Garcia error_setg(errp, "Node %s is in use by %s", 39911f0c461bSKevin Wolf node_name, bdrv_get_parent_name(bs)); 399281b936aeSAlberto Garcia return; 399381b936aeSAlberto Garcia } 399481b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 399581b936aeSAlberto Garcia } 399681b936aeSAlberto Garcia 399781b936aeSAlberto Garcia aio_context_acquire(aio_context); 399881b936aeSAlberto Garcia 399981b936aeSAlberto Garcia if (bs) { 400081b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 400181b936aeSAlberto Garcia goto out; 400281b936aeSAlberto Garcia } 400381b936aeSAlberto Garcia 40049c4218e9SMax Reitz if (!blk && !bs->monitor_list.tqe_prev) { 40059c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 40069c4218e9SMax Reitz bs->node_name); 40079c4218e9SMax Reitz goto out; 40089c4218e9SMax Reitz } 40099c4218e9SMax Reitz 40109c4218e9SMax Reitz if (bs->refcnt > 1) { 401181b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 401281b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 401381b936aeSAlberto Garcia goto out; 401481b936aeSAlberto Garcia } 401581b936aeSAlberto Garcia } 401681b936aeSAlberto Garcia 401781b936aeSAlberto Garcia if (blk) { 4018efaa7c4eSMax Reitz monitor_remove_blk(blk); 401981b936aeSAlberto Garcia blk_unref(blk); 402081b936aeSAlberto Garcia } else { 40219c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 402281b936aeSAlberto Garcia bdrv_unref(bs); 402381b936aeSAlberto Garcia } 402481b936aeSAlberto Garcia 402581b936aeSAlberto Garcia out: 402681b936aeSAlberto Garcia aio_context_release(aio_context); 402781b936aeSAlberto Garcia } 402881b936aeSAlberto Garcia 40297f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 40307f821597SWen Congyang const char *child_name) 40317f821597SWen Congyang { 40327f821597SWen Congyang BdrvChild *child; 40337f821597SWen Congyang 40347f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 40357f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 40367f821597SWen Congyang return child; 40377f821597SWen Congyang } 40387f821597SWen Congyang } 40397f821597SWen Congyang 40407f821597SWen Congyang return NULL; 40417f821597SWen Congyang } 40427f821597SWen Congyang 40437f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 40447f821597SWen Congyang const char *child, bool has_node, 40457f821597SWen Congyang const char *node, Error **errp) 40467f821597SWen Congyang { 40477f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 40487f821597SWen Congyang BdrvChild *p_child; 40497f821597SWen Congyang 40507f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 40517f821597SWen Congyang if (!parent_bs) { 40527f821597SWen Congyang return; 40537f821597SWen Congyang } 40547f821597SWen Congyang 40557f821597SWen Congyang if (has_child == has_node) { 40567f821597SWen Congyang if (has_child) { 40577f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 40587f821597SWen Congyang } else { 40597f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 40607f821597SWen Congyang } 40617f821597SWen Congyang return; 40627f821597SWen Congyang } 40637f821597SWen Congyang 40647f821597SWen Congyang if (has_child) { 40657f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 40667f821597SWen Congyang if (!p_child) { 40677f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 40687f821597SWen Congyang parent, child); 40697f821597SWen Congyang return; 40707f821597SWen Congyang } 40717f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 40727f821597SWen Congyang } 40737f821597SWen Congyang 40747f821597SWen Congyang if (has_node) { 40757f821597SWen Congyang new_bs = bdrv_find_node(node); 40767f821597SWen Congyang if (!new_bs) { 40777f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 40787f821597SWen Congyang return; 40797f821597SWen Congyang } 40807f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 40817f821597SWen Congyang } 40827f821597SWen Congyang } 40837f821597SWen Congyang 4084fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4085fb5458cdSStefan Hajnoczi { 4086fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4087f0f55dedSAlberto Garcia BlockJob *job; 4088fea68bb6SMarkus Armbruster 4089f0f55dedSAlberto Garcia for (job = block_job_next(NULL); job; job = block_job_next(job)) { 4090f0f55dedSAlberto Garcia BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 4091f0f55dedSAlberto Garcia AioContext *aio_context = blk_get_aio_context(job->blk); 409269691e72SStefan Hajnoczi 409369691e72SStefan Hajnoczi aio_context_acquire(aio_context); 4094f0f55dedSAlberto Garcia elem->value = block_job_query(job); 4095f0f55dedSAlberto Garcia aio_context_release(aio_context); 409669691e72SStefan Hajnoczi 4097fea68bb6SMarkus Armbruster *p_next = elem; 4098fea68bb6SMarkus Armbruster p_next = &elem->next; 4099fea68bb6SMarkus Armbruster } 410069691e72SStefan Hajnoczi 4101fea68bb6SMarkus Armbruster return head; 4102fb5458cdSStefan Hajnoczi } 41034d454574SPaolo Bonzini 41040006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 41054d454574SPaolo Bonzini .name = "drive", 41060006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 41074d454574SPaolo Bonzini .desc = { 41084d454574SPaolo Bonzini { 41094d454574SPaolo Bonzini .name = "snapshot", 41104d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 41114d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 41124d454574SPaolo Bonzini },{ 4113a9384affSPaolo Bonzini .name = "discard", 4114a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 4115a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 4116a9384affSPaolo Bonzini },{ 41174d454574SPaolo Bonzini .name = "aio", 41184d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41194d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 41204d454574SPaolo Bonzini },{ 4121e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4122e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4123e4b24b49SKevin Wolf .help = "Enable writeback mode", 4124e4b24b49SKevin Wolf },{ 41254d454574SPaolo Bonzini .name = "format", 41264d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41274d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 41284d454574SPaolo Bonzini },{ 41294d454574SPaolo Bonzini .name = "rerror", 41304d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41314d454574SPaolo Bonzini .help = "read error action", 41324d454574SPaolo Bonzini },{ 41334d454574SPaolo Bonzini .name = "werror", 41344d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41354d454574SPaolo Bonzini .help = "write error action", 41364d454574SPaolo Bonzini },{ 41370f227a94SKevin Wolf .name = "read-only", 41384d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 41394d454574SPaolo Bonzini .help = "open drive file as read-only", 41404d454574SPaolo Bonzini },{ 414157975222SKevin Wolf .name = "throttling.iops-total", 41424d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41434d454574SPaolo Bonzini .help = "limit total I/O operations per second", 41444d454574SPaolo Bonzini },{ 414557975222SKevin Wolf .name = "throttling.iops-read", 41464d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41474d454574SPaolo Bonzini .help = "limit read operations per second", 41484d454574SPaolo Bonzini },{ 414957975222SKevin Wolf .name = "throttling.iops-write", 41504d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41514d454574SPaolo Bonzini .help = "limit write operations per second", 41524d454574SPaolo Bonzini },{ 415357975222SKevin Wolf .name = "throttling.bps-total", 41544d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41554d454574SPaolo Bonzini .help = "limit total bytes per second", 41564d454574SPaolo Bonzini },{ 415757975222SKevin Wolf .name = "throttling.bps-read", 41584d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41594d454574SPaolo Bonzini .help = "limit read bytes per second", 41604d454574SPaolo Bonzini },{ 416157975222SKevin Wolf .name = "throttling.bps-write", 41624d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41634d454574SPaolo Bonzini .help = "limit write bytes per second", 41644d454574SPaolo Bonzini },{ 41653e9fab69SBenoît Canet .name = "throttling.iops-total-max", 41663e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41673e9fab69SBenoît Canet .help = "I/O operations burst", 41683e9fab69SBenoît Canet },{ 41693e9fab69SBenoît Canet .name = "throttling.iops-read-max", 41703e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41713e9fab69SBenoît Canet .help = "I/O operations read burst", 41723e9fab69SBenoît Canet },{ 41733e9fab69SBenoît Canet .name = "throttling.iops-write-max", 41743e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41753e9fab69SBenoît Canet .help = "I/O operations write burst", 41763e9fab69SBenoît Canet },{ 41773e9fab69SBenoît Canet .name = "throttling.bps-total-max", 41783e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41793e9fab69SBenoît Canet .help = "total bytes burst", 41803e9fab69SBenoît Canet },{ 41813e9fab69SBenoît Canet .name = "throttling.bps-read-max", 41823e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41833e9fab69SBenoît Canet .help = "total bytes read burst", 41843e9fab69SBenoît Canet },{ 41853e9fab69SBenoît Canet .name = "throttling.bps-write-max", 41863e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41873e9fab69SBenoît Canet .help = "total bytes write burst", 41883e9fab69SBenoît Canet },{ 41898a0fc18dSAlberto Garcia .name = "throttling.iops-total-max-length", 41908a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 41918a0fc18dSAlberto Garcia .help = "length of the iops-total-max burst period, in seconds", 41928a0fc18dSAlberto Garcia },{ 41938a0fc18dSAlberto Garcia .name = "throttling.iops-read-max-length", 41948a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 41958a0fc18dSAlberto Garcia .help = "length of the iops-read-max burst period, in seconds", 41968a0fc18dSAlberto Garcia },{ 41978a0fc18dSAlberto Garcia .name = "throttling.iops-write-max-length", 41988a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 41998a0fc18dSAlberto Garcia .help = "length of the iops-write-max burst period, in seconds", 42008a0fc18dSAlberto Garcia },{ 42018a0fc18dSAlberto Garcia .name = "throttling.bps-total-max-length", 42028a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42038a0fc18dSAlberto Garcia .help = "length of the bps-total-max burst period, in seconds", 42048a0fc18dSAlberto Garcia },{ 42058a0fc18dSAlberto Garcia .name = "throttling.bps-read-max-length", 42068a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42078a0fc18dSAlberto Garcia .help = "length of the bps-read-max burst period, in seconds", 42088a0fc18dSAlberto Garcia },{ 42098a0fc18dSAlberto Garcia .name = "throttling.bps-write-max-length", 42108a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42118a0fc18dSAlberto Garcia .help = "length of the bps-write-max burst period, in seconds", 42128a0fc18dSAlberto Garcia },{ 42132024c1dfSBenoît Canet .name = "throttling.iops-size", 42142024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 42152024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 42162024c1dfSBenoît Canet },{ 421776f4afb4SAlberto Garcia .name = "throttling.group", 421876f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 421976f4afb4SAlberto Garcia .help = "name of the block throttling group", 422076f4afb4SAlberto Garcia },{ 42214d454574SPaolo Bonzini .name = "copy-on-read", 42224d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 42234d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4224465bee1dSPeter Lieven },{ 4225465bee1dSPeter Lieven .name = "detect-zeroes", 4226465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4227465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4228362e9299SAlberto Garcia },{ 4229362e9299SAlberto Garcia .name = "stats-account-invalid", 4230362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4231362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4232362e9299SAlberto Garcia "in the statistics", 4233362e9299SAlberto Garcia },{ 4234362e9299SAlberto Garcia .name = "stats-account-failed", 4235362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4236362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4237362e9299SAlberto Garcia "in the statistics", 42384d454574SPaolo Bonzini }, 42394d454574SPaolo Bonzini { /* end of list */ } 42404d454574SPaolo Bonzini }, 42414d454574SPaolo Bonzini }; 42420006383eSKevin Wolf 4243bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 4244bd745e23SMax Reitz .name = "root-bds", 424523f7fcb2SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head), 4246bd745e23SMax Reitz .desc = { 4247bd745e23SMax Reitz { 4248bd745e23SMax Reitz .name = "discard", 4249bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4250bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 4251bd745e23SMax Reitz },{ 4252bd745e23SMax Reitz .name = "aio", 4253bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4254bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 4255bd745e23SMax Reitz },{ 4256bd745e23SMax Reitz .name = "read-only", 4257bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4258bd745e23SMax Reitz .help = "open drive file as read-only", 4259bd745e23SMax Reitz },{ 4260bd745e23SMax Reitz .name = "copy-on-read", 4261bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4262bd745e23SMax Reitz .help = "copy read data from backing file into image file", 4263bd745e23SMax Reitz },{ 4264bd745e23SMax Reitz .name = "detect-zeroes", 4265bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4266bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 4267bd745e23SMax Reitz }, 4268bd745e23SMax Reitz { /* end of list */ } 4269bd745e23SMax Reitz }, 4270bd745e23SMax Reitz }; 4271bd745e23SMax Reitz 42720006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 42730006383eSKevin Wolf .name = "drive", 42740006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 42750006383eSKevin Wolf .desc = { 4276492fdc6fSKevin Wolf /* 4277492fdc6fSKevin Wolf * no elements => accept any params 4278492fdc6fSKevin Wolf * validation will happen later 4279492fdc6fSKevin Wolf */ 42800006383eSKevin Wolf { /* end of list */ } 42810006383eSKevin Wolf }, 42820006383eSKevin Wolf }; 4283