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 591960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 601960966dSMarkus Armbruster [IF_NONE] = "none", 611960966dSMarkus Armbruster [IF_IDE] = "ide", 621960966dSMarkus Armbruster [IF_SCSI] = "scsi", 631960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 641960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 651960966dSMarkus Armbruster [IF_MTD] = "mtd", 661960966dSMarkus Armbruster [IF_SD] = "sd", 671960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 681960966dSMarkus Armbruster [IF_XEN] = "xen", 691960966dSMarkus Armbruster }; 701960966dSMarkus Armbruster 7121dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = { 7227d6bf40SMarkus Armbruster /* 7327d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 7427d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 7527d6bf40SMarkus Armbruster * 76547cb157SWei Jiangang * All controllers used to implement if=T drives need to support 7727d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 7827d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 7927d6bf40SMarkus Armbruster * values. 8027d6bf40SMarkus Armbruster * 8127d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 8227d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 8327d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 8427d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 8527d6bf40SMarkus Armbruster */ 8627d6bf40SMarkus Armbruster [IF_IDE] = 2, 8727d6bf40SMarkus Armbruster [IF_SCSI] = 7, 881960966dSMarkus Armbruster }; 891960966dSMarkus Armbruster 9021dff8cfSJohn Snow /** 9121dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides 9221dff8cfSJohn Snow * of the default, global values. 9321dff8cfSJohn Snow */ 9421dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs) 9521dff8cfSJohn Snow { 9618e46a03SMarkus Armbruster BlockBackend *blk; 9721dff8cfSJohn Snow DriveInfo *dinfo; 9821dff8cfSJohn Snow 9921dff8cfSJohn Snow if (max_devs <= 0) { 10021dff8cfSJohn Snow return; 10121dff8cfSJohn Snow } 10221dff8cfSJohn Snow 10318e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 10418e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 10521dff8cfSJohn Snow if (dinfo->type == type) { 10621dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of" 10721dff8cfSJohn Snow " the %s interface, because a drive of that type has" 10821dff8cfSJohn Snow " already been added.\n", if_name[type]); 10921dff8cfSJohn Snow g_assert_not_reached(); 11021dff8cfSJohn Snow } 11121dff8cfSJohn Snow } 11221dff8cfSJohn Snow 11321dff8cfSJohn Snow if_max_devs[type] = max_devs; 11421dff8cfSJohn Snow } 11521dff8cfSJohn Snow 11614bafc54SMarkus Armbruster /* 11714bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 11814bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 11914bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 12014bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 12114bafc54SMarkus Armbruster * when deletion is actually safe. 12214bafc54SMarkus Armbruster */ 1234be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk) 12414bafc54SMarkus Armbruster { 12518e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 1264be74634SMarkus Armbruster BlockDriverState *bs = blk_bs(blk); 12791fddb0dSStefan Hajnoczi AioContext *aio_context; 12814bafc54SMarkus Armbruster 12926f8b3a8SMarkus Armbruster if (!dinfo) { 1302d246f01SKevin Wolf return; 1312d246f01SKevin Wolf } 1322d246f01SKevin Wolf 1335433c24fSMax Reitz if (bs) { 13491fddb0dSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 13591fddb0dSStefan Hajnoczi aio_context_acquire(aio_context); 13691fddb0dSStefan Hajnoczi 13712bde0eeSPaolo Bonzini if (bs->job) { 13812bde0eeSPaolo Bonzini block_job_cancel(bs->job); 13912bde0eeSPaolo Bonzini } 14091fddb0dSStefan Hajnoczi 14191fddb0dSStefan Hajnoczi aio_context_release(aio_context); 1425433c24fSMax Reitz } 14391fddb0dSStefan Hajnoczi 14414bafc54SMarkus Armbruster dinfo->auto_del = 1; 14514bafc54SMarkus Armbruster } 14614bafc54SMarkus Armbruster 1474be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk) 14814bafc54SMarkus Armbruster { 14918e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 15014bafc54SMarkus Armbruster 1510fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 152efaa7c4eSMax Reitz monitor_remove_blk(blk); 153b9fe8a7aSMarkus Armbruster blk_unref(blk); 15414bafc54SMarkus Armbruster } 15514bafc54SMarkus Armbruster } 15614bafc54SMarkus Armbruster 157d8f94e1bSJohn Snow /** 158d8f94e1bSJohn Snow * Returns the current mapping of how many units per bus 159d8f94e1bSJohn Snow * a particular interface can support. 160d8f94e1bSJohn Snow * 161d8f94e1bSJohn Snow * A positive integer indicates n units per bus. 162d8f94e1bSJohn Snow * 0 implies the mapping has not been established. 163d8f94e1bSJohn Snow * -1 indicates an invalid BlockInterfaceType was given. 164d8f94e1bSJohn Snow */ 165d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type) 166d8f94e1bSJohn Snow { 167d8f94e1bSJohn Snow if (type >= IF_IDE && type < IF_COUNT) { 168d8f94e1bSJohn Snow return if_max_devs[type]; 169d8f94e1bSJohn Snow } 170d8f94e1bSJohn Snow 171d8f94e1bSJohn Snow return -1; 172d8f94e1bSJohn Snow } 173d8f94e1bSJohn Snow 174505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 175505a7fb1SMarkus Armbruster { 176505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 177505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 178505a7fb1SMarkus Armbruster } 179505a7fb1SMarkus Armbruster 180505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 181505a7fb1SMarkus Armbruster { 182505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 183505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 184505a7fb1SMarkus Armbruster } 185505a7fb1SMarkus Armbruster 1862292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 1872292ddaeSMarkus Armbruster { 18870b94331SMarkus Armbruster return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 1892292ddaeSMarkus Armbruster } 1902292ddaeSMarkus Armbruster 1912292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 1925645b0f4SMarkus Armbruster const char *optstr) 193666daa68SMarkus Armbruster { 194666daa68SMarkus Armbruster QemuOpts *opts; 195666daa68SMarkus Armbruster 1962292ddaeSMarkus Armbruster opts = drive_def(optstr); 197666daa68SMarkus Armbruster if (!opts) { 198666daa68SMarkus Armbruster return NULL; 199666daa68SMarkus Armbruster } 2002292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 201f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort); 2022292ddaeSMarkus Armbruster } 2032292ddaeSMarkus Armbruster if (index >= 0) { 204a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort); 2052292ddaeSMarkus Armbruster } 206666daa68SMarkus Armbruster if (file) 207f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort); 208666daa68SMarkus Armbruster return opts; 209666daa68SMarkus Armbruster } 210666daa68SMarkus Armbruster 211666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 212666daa68SMarkus Armbruster { 21318e46a03SMarkus Armbruster BlockBackend *blk; 214666daa68SMarkus Armbruster DriveInfo *dinfo; 215666daa68SMarkus Armbruster 21618e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 21718e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 21818e46a03SMarkus Armbruster if (dinfo && dinfo->type == type 21918e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) { 220666daa68SMarkus Armbruster return dinfo; 221666daa68SMarkus Armbruster } 22218e46a03SMarkus Armbruster } 223666daa68SMarkus Armbruster 224666daa68SMarkus Armbruster return NULL; 225666daa68SMarkus Armbruster } 226666daa68SMarkus Armbruster 227a66c9dc7SJohn Snow bool drive_check_orphaned(void) 228a66c9dc7SJohn Snow { 22918e46a03SMarkus Armbruster BlockBackend *blk; 230a66c9dc7SJohn Snow DriveInfo *dinfo; 231a66c9dc7SJohn Snow bool rs = false; 232a66c9dc7SJohn Snow 23318e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 23418e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 235a66c9dc7SJohn Snow /* If dinfo->bdrv->dev is NULL, it has no device attached. */ 236a66c9dc7SJohn Snow /* Unless this is a default drive, this may be an oversight. */ 237a7f53e26SMarkus Armbruster if (!blk_get_attached_dev(blk) && !dinfo->is_default && 238a66c9dc7SJohn Snow dinfo->type != IF_NONE) { 239a66c9dc7SJohn Snow fprintf(stderr, "Warning: Orphaned drive without device: " 240a66c9dc7SJohn Snow "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", 2415433c24fSMax Reitz blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "", 2425433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit); 243a66c9dc7SJohn Snow rs = true; 244a66c9dc7SJohn Snow } 245a66c9dc7SJohn Snow } 246a66c9dc7SJohn Snow 247a66c9dc7SJohn Snow return rs; 248a66c9dc7SJohn Snow } 249a66c9dc7SJohn Snow 250f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 251f1bd51acSMarkus Armbruster { 252f1bd51acSMarkus Armbruster return drive_get(type, 253f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 254f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 255f1bd51acSMarkus Armbruster } 256f1bd51acSMarkus Armbruster 257666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 258666daa68SMarkus Armbruster { 259666daa68SMarkus Armbruster int max_bus; 26018e46a03SMarkus Armbruster BlockBackend *blk; 261666daa68SMarkus Armbruster DriveInfo *dinfo; 262666daa68SMarkus Armbruster 263666daa68SMarkus Armbruster max_bus = -1; 26418e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 26518e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 26618e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 267666daa68SMarkus Armbruster max_bus = dinfo->bus; 268666daa68SMarkus Armbruster } 26918e46a03SMarkus Armbruster } 270666daa68SMarkus Armbruster return max_bus; 271666daa68SMarkus Armbruster } 272666daa68SMarkus Armbruster 27313839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 27413839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 27513839974SMarkus Armbruster appropriate bus. */ 27613839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 27713839974SMarkus Armbruster { 27813839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 27913839974SMarkus Armbruster 28013839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 28113839974SMarkus Armbruster } 28213839974SMarkus Armbruster 283666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 284666daa68SMarkus Armbruster { 285807105a7SMarkus Armbruster error_printf(" %s", name); 286666daa68SMarkus Armbruster } 287666daa68SMarkus Armbruster 288aa398a5cSStefan Hajnoczi typedef struct { 289aa398a5cSStefan Hajnoczi QEMUBH *bh; 290fa510ebfSFam Zheng BlockDriverState *bs; 291fa510ebfSFam Zheng } BDRVPutRefBH; 292aa398a5cSStefan Hajnoczi 293b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 294666daa68SMarkus Armbruster { 295666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 29692aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 297666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 29892aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 299666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 30092aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 301666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 30292aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 303666daa68SMarkus Armbruster } else { 304b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 305666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 306666daa68SMarkus Armbruster return -1; 307666daa68SMarkus Armbruster } 308666daa68SMarkus Armbruster } 309666daa68SMarkus Armbruster 31040119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 31140119effSAlberto Garcia Error **errp) 31240119effSAlberto Garcia { 31340119effSAlberto Garcia const QListEntry *entry; 31440119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 31540119effSAlberto Garcia switch (qobject_type(entry->value)) { 31640119effSAlberto Garcia 31740119effSAlberto Garcia case QTYPE_QSTRING: { 31840119effSAlberto Garcia unsigned long long length; 31940119effSAlberto Garcia const char *str = qstring_get_str(qobject_to_qstring(entry->value)); 32040119effSAlberto Garcia if (parse_uint_full(str, &length, 10) == 0 && 32140119effSAlberto Garcia length > 0 && length <= UINT_MAX) { 32240119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 32340119effSAlberto Garcia } else { 32440119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str); 32540119effSAlberto Garcia return false; 32640119effSAlberto Garcia } 32740119effSAlberto Garcia break; 32840119effSAlberto Garcia } 32940119effSAlberto Garcia 33040119effSAlberto Garcia case QTYPE_QINT: { 33140119effSAlberto Garcia int64_t length = qint_get_int(qobject_to_qint(entry->value)); 33240119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) { 33340119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 33440119effSAlberto Garcia } else { 33540119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length); 33640119effSAlberto Garcia return false; 33740119effSAlberto Garcia } 33840119effSAlberto Garcia break; 33940119effSAlberto Garcia } 34040119effSAlberto Garcia 34140119effSAlberto Garcia default: 34240119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid"); 34340119effSAlberto Garcia return false; 34440119effSAlberto Garcia } 34540119effSAlberto Garcia } 34640119effSAlberto Garcia return true; 34740119effSAlberto Garcia } 34840119effSAlberto Garcia 34933cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 35033cb7dc8SKevin Wolf 351fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 352fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 353fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 354fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 355fbf8175eSMax Reitz { 356fbf8175eSMax Reitz const char *discard; 357fbf8175eSMax Reitz Error *local_error = NULL; 358fbf8175eSMax Reitz const char *aio; 359fbf8175eSMax Reitz 360fbf8175eSMax Reitz if (bdrv_flags) { 361fbf8175eSMax Reitz if (!qemu_opt_get_bool(opts, "read-only", false)) { 362fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_RDWR; 363fbf8175eSMax Reitz } 364fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 365fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 366fbf8175eSMax Reitz } 367fbf8175eSMax Reitz 368fbf8175eSMax Reitz if ((discard = qemu_opt_get(opts, "discard")) != NULL) { 369fbf8175eSMax Reitz if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { 370fbf8175eSMax Reitz error_setg(errp, "Invalid discard option"); 371fbf8175eSMax Reitz return; 372fbf8175eSMax Reitz } 373fbf8175eSMax Reitz } 374fbf8175eSMax Reitz 375fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 376fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 377fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 378fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 379fbf8175eSMax Reitz /* this is the default */ 380fbf8175eSMax Reitz } else { 381fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 382fbf8175eSMax Reitz return; 383fbf8175eSMax Reitz } 384fbf8175eSMax Reitz } 385fbf8175eSMax Reitz } 386fbf8175eSMax Reitz 387fbf8175eSMax Reitz /* disk I/O throttling */ 388fbf8175eSMax Reitz if (throttling_group) { 389fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 390fbf8175eSMax Reitz } 391fbf8175eSMax Reitz 392fbf8175eSMax Reitz if (throttle_cfg) { 3931588ab5dSAlberto Garcia throttle_config_init(throttle_cfg); 394fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 395fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 396fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 397fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 398fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 399fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 400fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 401fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 402fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 403fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 404fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 405fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 406fbf8175eSMax Reitz 407fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 408fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 409fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 410fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 411fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 412fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 413fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 414fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 415fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 416fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 417fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 418fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 419fbf8175eSMax Reitz 4208a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = 4218a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); 4228a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = 4238a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); 4248a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = 4258a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); 4268a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = 4278a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); 4288a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = 4298a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); 4308a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = 4318a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); 4328a0fc18dSAlberto Garcia 433fbf8175eSMax Reitz throttle_cfg->op_size = 434fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 435fbf8175eSMax Reitz 436d5851089SAlberto Garcia if (!throttle_is_valid(throttle_cfg, errp)) { 437fbf8175eSMax Reitz return; 438fbf8175eSMax Reitz } 439fbf8175eSMax Reitz } 440fbf8175eSMax Reitz 441fbf8175eSMax Reitz if (detect_zeroes) { 442fbf8175eSMax Reitz *detect_zeroes = 443fbf8175eSMax Reitz qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, 444fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 4457fb1cf16SEric Blake BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX, 446fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 447fbf8175eSMax Reitz &local_error); 448fbf8175eSMax Reitz if (local_error) { 449fbf8175eSMax Reitz error_propagate(errp, local_error); 450fbf8175eSMax Reitz return; 451fbf8175eSMax Reitz } 452fbf8175eSMax Reitz 453fbf8175eSMax Reitz if (bdrv_flags && 454fbf8175eSMax Reitz *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 455fbf8175eSMax Reitz !(*bdrv_flags & BDRV_O_UNMAP)) 456fbf8175eSMax Reitz { 457fbf8175eSMax Reitz error_setg(errp, "setting detect-zeroes to unmap is not allowed " 458fbf8175eSMax Reitz "without setting discard operation to unmap"); 459fbf8175eSMax Reitz return; 460fbf8175eSMax Reitz } 461fbf8175eSMax Reitz } 462fbf8175eSMax Reitz } 463fbf8175eSMax Reitz 464f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 46518e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 466b681072dSKevin Wolf Error **errp) 467666daa68SMarkus Armbruster { 468666daa68SMarkus Armbruster const char *buf; 469666daa68SMarkus Armbruster int bdrv_flags = 0; 470666daa68SMarkus Armbruster int on_read_error, on_write_error; 471362e9299SAlberto Garcia bool account_invalid, account_failed; 472e4b24b49SKevin Wolf bool writethrough; 47326f54e9aSMarkus Armbruster BlockBackend *blk; 474a0f1eab1SMarkus Armbruster BlockDriverState *bs; 475cc0681c4SBenoît Canet ThrottleConfig cfg; 476666daa68SMarkus Armbruster int snapshot = 0; 477c546194fSStefan Hajnoczi Error *error = NULL; 4780006383eSKevin Wolf QemuOpts *opts; 47940119effSAlberto Garcia QDict *interval_dict = NULL; 48040119effSAlberto Garcia QList *interval_list = NULL; 4810006383eSKevin Wolf const char *id; 482fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 483fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 484fbf8175eSMax Reitz const char *throttling_group = NULL; 485666daa68SMarkus Armbruster 486f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 487f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 488f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4890006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 49084d18f06SMarkus Armbruster if (error) { 491b681072dSKevin Wolf error_propagate(errp, error); 4926376f952SMarkus Armbruster goto err_no_opts; 4930006383eSKevin Wolf } 4940006383eSKevin Wolf 4950006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49684d18f06SMarkus Armbruster if (error) { 497b681072dSKevin Wolf error_propagate(errp, error); 498ec9c10d2SStefan Hajnoczi goto early_err; 4990006383eSKevin Wolf } 5000006383eSKevin Wolf 5010006383eSKevin Wolf if (id) { 5020006383eSKevin Wolf qdict_del(bs_opts, "id"); 5030006383eSKevin Wolf } 5040006383eSKevin Wolf 505666daa68SMarkus Armbruster /* extract parameters */ 506666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 507666daa68SMarkus Armbruster 508362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 509362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 510362e9299SAlberto Garcia 511e4b24b49SKevin Wolf writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); 512e4b24b49SKevin Wolf 51340119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51440119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51540119effSAlberto Garcia 51640119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 51740119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 51840119effSAlberto Garcia qdict_first(interval_dict)->key); 51940119effSAlberto Garcia goto early_err; 52040119effSAlberto Garcia } 5212be5506fSAlberto Garcia 522fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 523fbf8175eSMax Reitz &detect_zeroes, &error); 524fbf8175eSMax Reitz if (error) { 525fbf8175eSMax Reitz error_propagate(errp, error); 526ec9c10d2SStefan Hajnoczi goto early_err; 527a9384affSPaolo Bonzini } 528666daa68SMarkus Armbruster 529666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 530c8057f95SPeter Maydell if (is_help_option(buf)) { 531807105a7SMarkus Armbruster error_printf("Supported formats:"); 532666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 533807105a7SMarkus Armbruster error_printf("\n"); 534ec9c10d2SStefan Hajnoczi goto early_err; 535666daa68SMarkus Armbruster } 53674fe54f2SKevin Wolf 537e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 538e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 539ec9c10d2SStefan Hajnoczi goto early_err; 5406db5f5d6SMike Qiu } 541e4342ce5SMax Reitz qdict_put(bs_opts, "driver", qstring_from_str(buf)); 542666daa68SMarkus Armbruster } 543666daa68SMarkus Armbruster 54492aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 545666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 546b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 54784d18f06SMarkus Armbruster if (error) { 548b681072dSKevin Wolf error_propagate(errp, error); 549ec9c10d2SStefan Hajnoczi goto early_err; 550666daa68SMarkus Armbruster } 551666daa68SMarkus Armbruster } 552666daa68SMarkus Armbruster 55392aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 554666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 555b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 55684d18f06SMarkus Armbruster if (error) { 557b681072dSKevin Wolf error_propagate(errp, error); 558ec9c10d2SStefan Hajnoczi goto early_err; 559666daa68SMarkus Armbruster } 560666daa68SMarkus Armbruster } 561666daa68SMarkus Armbruster 562666daa68SMarkus Armbruster if (snapshot) { 56391a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 564666daa68SMarkus Armbruster } 565666daa68SMarkus Armbruster 5665ec18f8cSMax Reitz /* init */ 56739c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5685ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5695ec18f8cSMax Reitz 570efaa7c4eSMax Reitz blk = blk_new(errp); 5715ec18f8cSMax Reitz if (!blk) { 5725ec18f8cSMax Reitz goto early_err; 5735ec18f8cSMax Reitz } 5745ec18f8cSMax Reitz 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) { 6217ca7f0f6SKevin Wolf throttling_group = blk_name(blk); 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 630efaa7c4eSMax Reitz if (!monitor_add_blk(blk, qemu_opts_id(opts), 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 ret; 661bd745e23SMax Reitz int bdrv_flags = 0; 662bd745e23SMax Reitz 663bd745e23SMax Reitz opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); 664bd745e23SMax Reitz if (!opts) { 665bd745e23SMax Reitz goto fail; 666bd745e23SMax Reitz } 667bd745e23SMax Reitz 668bd745e23SMax Reitz qemu_opts_absorb_qdict(opts, bs_opts, &local_error); 669bd745e23SMax Reitz if (local_error) { 670bd745e23SMax Reitz error_propagate(errp, local_error); 671bd745e23SMax Reitz goto fail; 672bd745e23SMax Reitz } 673bd745e23SMax Reitz 674bd745e23SMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, 675bd745e23SMax Reitz &detect_zeroes, &local_error); 676bd745e23SMax Reitz if (local_error) { 677bd745e23SMax Reitz error_propagate(errp, local_error); 678bd745e23SMax Reitz goto fail; 679bd745e23SMax Reitz } 680bd745e23SMax Reitz 681a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 682a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults. 683a81d6164SKevin Wolf * Apply the defaults here instead. */ 684a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 685a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 686a81d6164SKevin Wolf 68712d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 68812d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 68912d5ee3aSKevin Wolf } 69012d5ee3aSKevin Wolf 691bd745e23SMax Reitz bs = NULL; 692bd745e23SMax Reitz ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp); 693bd745e23SMax Reitz if (ret < 0) { 694bd745e23SMax Reitz goto fail_no_bs_opts; 695bd745e23SMax Reitz } 696bd745e23SMax Reitz 697bd745e23SMax Reitz bs->detect_zeroes = detect_zeroes; 698bd745e23SMax Reitz 699bd745e23SMax Reitz fail_no_bs_opts: 700bd745e23SMax Reitz qemu_opts_del(opts); 701bd745e23SMax Reitz return bs; 702bd745e23SMax Reitz 703bd745e23SMax Reitz fail: 704bd745e23SMax Reitz qemu_opts_del(opts); 705bd745e23SMax Reitz QDECREF(bs_opts); 706bd745e23SMax Reitz return NULL; 707bd745e23SMax Reitz } 708bd745e23SMax Reitz 7099c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void) 7109c4218e9SMax Reitz { 7119c4218e9SMax Reitz BlockDriverState *bs, *next_bs; 7129c4218e9SMax Reitz 7139c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { 7149c4218e9SMax Reitz AioContext *ctx = bdrv_get_aio_context(bs); 7159c4218e9SMax Reitz 7169c4218e9SMax Reitz aio_context_acquire(ctx); 7179c4218e9SMax Reitz bdrv_unref(bs); 7189c4218e9SMax Reitz aio_context_release(ctx); 7199c4218e9SMax Reitz } 7209c4218e9SMax Reitz } 7219c4218e9SMax Reitz 722262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */ 723262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) 724262b4e8fSMax Reitz { 725262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list) 726262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states); 727262b4e8fSMax Reitz } 728262b4e8fSMax Reitz 7295abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 7305abbf0eeSKevin Wolf Error **errp) 73157975222SKevin Wolf { 73257975222SKevin Wolf const char *value; 73357975222SKevin Wolf 73457975222SKevin Wolf value = qemu_opt_get(opts, from); 73557975222SKevin Wolf if (value) { 7365abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 7375abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 7385abbf0eeSKevin Wolf "same time", to, from); 7395abbf0eeSKevin Wolf return; 7405abbf0eeSKevin Wolf } 74120d6cd47SJun Li } 74220d6cd47SJun Li 74320d6cd47SJun Li /* rename all items in opts */ 74420d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 745f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 74657975222SKevin Wolf qemu_opt_unset(opts, from); 74757975222SKevin Wolf } 74857975222SKevin Wolf } 74957975222SKevin Wolf 75033cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 75133cb7dc8SKevin Wolf .name = "drive", 75233cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 75333cb7dc8SKevin Wolf .desc = { 75433cb7dc8SKevin Wolf { 75587a899c5SKevin Wolf .name = "bus", 75687a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 75787a899c5SKevin Wolf .help = "bus number", 75887a899c5SKevin Wolf },{ 75987a899c5SKevin Wolf .name = "unit", 76087a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 76187a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 76287a899c5SKevin Wolf },{ 76387a899c5SKevin Wolf .name = "index", 76487a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 76587a899c5SKevin Wolf .help = "index number", 76687a899c5SKevin Wolf },{ 76733cb7dc8SKevin Wolf .name = "media", 76833cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 76933cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 770593d464bSKevin Wolf },{ 771593d464bSKevin Wolf .name = "if", 772593d464bSKevin Wolf .type = QEMU_OPT_STRING, 773593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 774b41a7338SKevin Wolf },{ 775b41a7338SKevin Wolf .name = "cyls", 776b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 777b41a7338SKevin Wolf .help = "number of cylinders (ide disk geometry)", 778b41a7338SKevin Wolf },{ 779b41a7338SKevin Wolf .name = "heads", 780b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 781b41a7338SKevin Wolf .help = "number of heads (ide disk geometry)", 782b41a7338SKevin Wolf },{ 783b41a7338SKevin Wolf .name = "secs", 784b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 785b41a7338SKevin Wolf .help = "number of sectors (ide disk geometry)", 786b41a7338SKevin Wolf },{ 787b41a7338SKevin Wolf .name = "trans", 788b41a7338SKevin Wolf .type = QEMU_OPT_STRING, 789b41a7338SKevin Wolf .help = "chs translation (auto, lba, none)", 79026929298SKevin Wolf },{ 79126929298SKevin Wolf .name = "boot", 79226929298SKevin Wolf .type = QEMU_OPT_BOOL, 79326929298SKevin Wolf .help = "(deprecated, ignored)", 794394c7d4dSKevin Wolf },{ 795394c7d4dSKevin Wolf .name = "addr", 796394c7d4dSKevin Wolf .type = QEMU_OPT_STRING, 797394c7d4dSKevin Wolf .help = "pci address (virtio only)", 798d095b465SMax Reitz },{ 799bcf83158SKevin Wolf .name = "serial", 800bcf83158SKevin Wolf .type = QEMU_OPT_STRING, 801bcf83158SKevin Wolf .help = "disk serial number", 802bcf83158SKevin Wolf },{ 803d095b465SMax Reitz .name = "file", 804d095b465SMax Reitz .type = QEMU_OPT_STRING, 805d095b465SMax Reitz .help = "file name", 80633cb7dc8SKevin Wolf }, 8070ebd24e0SKevin Wolf 8080ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 8090ebd24e0SKevin Wolf { 8100ebd24e0SKevin Wolf .name = "read-only", 8110ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8120ebd24e0SKevin Wolf .help = "open drive file as read-only", 8130ebd24e0SKevin Wolf },{ 814ee13ed1cSKevin Wolf .name = "rerror", 815ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 816ee13ed1cSKevin Wolf .help = "read error action", 817ee13ed1cSKevin Wolf },{ 818ee13ed1cSKevin Wolf .name = "werror", 819ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 820ee13ed1cSKevin Wolf .help = "write error action", 821ee13ed1cSKevin Wolf },{ 8220ebd24e0SKevin Wolf .name = "copy-on-read", 8230ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8240ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 8250ebd24e0SKevin Wolf }, 8260ebd24e0SKevin Wolf 82733cb7dc8SKevin Wolf { /* end of list */ } 82833cb7dc8SKevin Wolf }, 82933cb7dc8SKevin Wolf }; 83033cb7dc8SKevin Wolf 83160e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 83257975222SKevin Wolf { 83329c4e2b5SKevin Wolf const char *value; 83418e46a03SMarkus Armbruster BlockBackend *blk; 83533cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 836f298d071SKevin Wolf QDict *bs_opts; 83733cb7dc8SKevin Wolf QemuOpts *legacy_opts; 83833cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 839593d464bSKevin Wolf BlockInterfaceType type; 840b41a7338SKevin Wolf int cyls, heads, secs, translation; 84187a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 842394c7d4dSKevin Wolf const char *devaddr; 843ee13ed1cSKevin Wolf const char *werror, *rerror; 844a7fdbcf0SFam Zheng bool read_only = false; 845a7fdbcf0SFam Zheng bool copy_on_read; 846bcf83158SKevin Wolf const char *serial; 847d095b465SMax Reitz const char *filename; 84833cb7dc8SKevin Wolf Error *local_err = NULL; 849247147fbSKevin Wolf int i; 85029c4e2b5SKevin Wolf 85157975222SKevin Wolf /* Change legacy command line options into QMP ones */ 852247147fbSKevin Wolf static const struct { 853247147fbSKevin Wolf const char *from; 854247147fbSKevin Wolf const char *to; 855247147fbSKevin Wolf } opt_renames[] = { 856247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 857247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 858247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 85957975222SKevin Wolf 860247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 861247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 862247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 86357975222SKevin Wolf 864247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 865247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 866247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8673e9fab69SBenoît Canet 868247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 869247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 870247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8713e9fab69SBenoît Canet 872247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8732024c1dfSBenoît Canet 87476f4afb4SAlberto Garcia { "group", "throttling.group" }, 87576f4afb4SAlberto Garcia 876247147fbSKevin Wolf { "readonly", "read-only" }, 877247147fbSKevin Wolf }; 878247147fbSKevin Wolf 879247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8805abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8815abbf0eeSKevin Wolf &local_err); 8825abbf0eeSKevin Wolf if (local_err) { 883565f65d2SMarkus Armbruster error_report_err(local_err); 8845abbf0eeSKevin Wolf return NULL; 8855abbf0eeSKevin Wolf } 886247147fbSKevin Wolf } 8870f227a94SKevin Wolf 88829c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 88929c4e2b5SKevin Wolf if (value) { 89029c4e2b5SKevin Wolf int flags = 0; 89104feb4a5SKevin Wolf bool writethrough; 89229c4e2b5SKevin Wolf 89304feb4a5SKevin Wolf if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { 89429c4e2b5SKevin Wolf error_report("invalid cache option"); 89529c4e2b5SKevin Wolf return NULL; 89629c4e2b5SKevin Wolf } 89729c4e2b5SKevin Wolf 89829c4e2b5SKevin Wolf /* Specific options take precedence */ 89954861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 90054861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 90104feb4a5SKevin Wolf !writethrough, &error_abort); 90229c4e2b5SKevin Wolf } 90354861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 90454861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 905cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 90629c4e2b5SKevin Wolf } 90754861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 90854861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 909cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 91029c4e2b5SKevin Wolf } 91129c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 91229c4e2b5SKevin Wolf } 91329c4e2b5SKevin Wolf 914f298d071SKevin Wolf /* Get a QDict for processing the options */ 915f298d071SKevin Wolf bs_opts = qdict_new(); 916f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 917f298d071SKevin Wolf 91887ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 91987ea75d5SPeter Crosthwaite &error_abort); 92033cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 92184d18f06SMarkus Armbruster if (local_err) { 922565f65d2SMarkus Armbruster error_report_err(local_err); 92333cb7dc8SKevin Wolf goto fail; 92433cb7dc8SKevin Wolf } 92533cb7dc8SKevin Wolf 92626929298SKevin Wolf /* Deprecated option boot=[on|off] */ 92726929298SKevin Wolf if (qemu_opt_get(legacy_opts, "boot") != NULL) { 92826929298SKevin Wolf fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 92926929298SKevin Wolf "ignored. Future versions will reject this parameter. Please " 93026929298SKevin Wolf "update your scripts.\n"); 93126929298SKevin Wolf } 93226929298SKevin Wolf 93333cb7dc8SKevin Wolf /* Media type */ 93433cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 93533cb7dc8SKevin Wolf if (value) { 93633cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 93733cb7dc8SKevin Wolf media = MEDIA_DISK; 93833cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 93933cb7dc8SKevin Wolf media = MEDIA_CDROM; 940a7fdbcf0SFam Zheng read_only = true; 94133cb7dc8SKevin Wolf } else { 94233cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 94333cb7dc8SKevin Wolf goto fail; 94433cb7dc8SKevin Wolf } 94533cb7dc8SKevin Wolf } 94633cb7dc8SKevin Wolf 9470ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 948a7fdbcf0SFam Zheng read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); 9490ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 9500ebd24e0SKevin Wolf 9510ebd24e0SKevin Wolf if (read_only && copy_on_read) { 9520ebd24e0SKevin Wolf error_report("warning: disabling copy-on-read on read-only drive"); 9530ebd24e0SKevin Wolf copy_on_read = false; 9540ebd24e0SKevin Wolf } 9550ebd24e0SKevin Wolf 9560ebd24e0SKevin Wolf qdict_put(bs_opts, "read-only", 9570ebd24e0SKevin Wolf qstring_from_str(read_only ? "on" : "off")); 9580ebd24e0SKevin Wolf qdict_put(bs_opts, "copy-on-read", 9590ebd24e0SKevin Wolf qstring_from_str(copy_on_read ? "on" :"off")); 9600ebd24e0SKevin Wolf 961593d464bSKevin Wolf /* Controller type */ 962593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 963593d464bSKevin Wolf if (value) { 964593d464bSKevin Wolf for (type = 0; 965593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 966593d464bSKevin Wolf type++) { 967593d464bSKevin Wolf } 968593d464bSKevin Wolf if (type == IF_COUNT) { 969593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 970593d464bSKevin Wolf goto fail; 971593d464bSKevin Wolf } 972593d464bSKevin Wolf } else { 973593d464bSKevin Wolf type = block_default_type; 974593d464bSKevin Wolf } 975593d464bSKevin Wolf 976b41a7338SKevin Wolf /* Geometry */ 977b41a7338SKevin Wolf cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 978b41a7338SKevin Wolf heads = qemu_opt_get_number(legacy_opts, "heads", 0); 979b41a7338SKevin Wolf secs = qemu_opt_get_number(legacy_opts, "secs", 0); 980b41a7338SKevin Wolf 981b41a7338SKevin Wolf if (cyls || heads || secs) { 982b41a7338SKevin Wolf if (cyls < 1) { 983b41a7338SKevin Wolf error_report("invalid physical cyls number"); 984b41a7338SKevin Wolf goto fail; 985b41a7338SKevin Wolf } 986b41a7338SKevin Wolf if (heads < 1) { 987b41a7338SKevin Wolf error_report("invalid physical heads number"); 988b41a7338SKevin Wolf goto fail; 989b41a7338SKevin Wolf } 990b41a7338SKevin Wolf if (secs < 1) { 991b41a7338SKevin Wolf error_report("invalid physical secs number"); 992b41a7338SKevin Wolf goto fail; 993b41a7338SKevin Wolf } 994b41a7338SKevin Wolf } 995b41a7338SKevin Wolf 996b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 997b41a7338SKevin Wolf value = qemu_opt_get(legacy_opts, "trans"); 998b41a7338SKevin Wolf if (value != NULL) { 999b41a7338SKevin Wolf if (!cyls) { 1000b41a7338SKevin Wolf error_report("'%s' trans must be used with cyls, heads and secs", 1001b41a7338SKevin Wolf value); 1002b41a7338SKevin Wolf goto fail; 1003b41a7338SKevin Wolf } 1004b41a7338SKevin Wolf if (!strcmp(value, "none")) { 1005b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_NONE; 1006b41a7338SKevin Wolf } else if (!strcmp(value, "lba")) { 1007b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_LBA; 1008f31c41ffSPaolo Bonzini } else if (!strcmp(value, "large")) { 1009f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_LARGE; 1010f31c41ffSPaolo Bonzini } else if (!strcmp(value, "rechs")) { 1011f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_RECHS; 1012b41a7338SKevin Wolf } else if (!strcmp(value, "auto")) { 1013b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 1014b41a7338SKevin Wolf } else { 1015b41a7338SKevin Wolf error_report("'%s' invalid translation type", value); 1016b41a7338SKevin Wolf goto fail; 1017b41a7338SKevin Wolf } 1018b41a7338SKevin Wolf } 1019b41a7338SKevin Wolf 1020b41a7338SKevin Wolf if (media == MEDIA_CDROM) { 1021b41a7338SKevin Wolf if (cyls || secs || heads) { 1022b41a7338SKevin Wolf error_report("CHS can't be set with media=cdrom"); 1023b41a7338SKevin Wolf goto fail; 1024b41a7338SKevin Wolf } 1025b41a7338SKevin Wolf } 1026b41a7338SKevin Wolf 102787a899c5SKevin Wolf /* Device address specified by bus/unit or index. 102887a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 102987a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 103087a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 103187a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 103287a899c5SKevin Wolf 103387a899c5SKevin Wolf max_devs = if_max_devs[type]; 103487a899c5SKevin Wolf 103587a899c5SKevin Wolf if (index != -1) { 103687a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 103787a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 103887a899c5SKevin Wolf goto fail; 103987a899c5SKevin Wolf } 104087a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 104187a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 104287a899c5SKevin Wolf } 104387a899c5SKevin Wolf 104487a899c5SKevin Wolf if (unit_id == -1) { 104587a899c5SKevin Wolf unit_id = 0; 104687a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 104787a899c5SKevin Wolf unit_id++; 104887a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 104987a899c5SKevin Wolf unit_id -= max_devs; 105087a899c5SKevin Wolf bus_id++; 105187a899c5SKevin Wolf } 105287a899c5SKevin Wolf } 105387a899c5SKevin Wolf } 105487a899c5SKevin Wolf 105587a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 105687a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 105787a899c5SKevin Wolf goto fail; 105887a899c5SKevin Wolf } 105987a899c5SKevin Wolf 106087a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 106187a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 106287a899c5SKevin Wolf bus_id, unit_id, index); 106387a899c5SKevin Wolf goto fail; 106487a899c5SKevin Wolf } 106587a899c5SKevin Wolf 1066bcf83158SKevin Wolf /* Serial number */ 1067bcf83158SKevin Wolf serial = qemu_opt_get(legacy_opts, "serial"); 1068bcf83158SKevin Wolf 106987a899c5SKevin Wolf /* no id supplied -> create one */ 107087a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 107187a899c5SKevin Wolf char *new_id; 107287a899c5SKevin Wolf const char *mediastr = ""; 107387a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 107487a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 107587a899c5SKevin Wolf } 107687a899c5SKevin Wolf if (max_devs) { 107787a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 107887a899c5SKevin Wolf mediastr, unit_id); 107987a899c5SKevin Wolf } else { 108087a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 108187a899c5SKevin Wolf mediastr, unit_id); 108287a899c5SKevin Wolf } 108387a899c5SKevin Wolf qdict_put(bs_opts, "id", qstring_from_str(new_id)); 108487a899c5SKevin Wolf g_free(new_id); 108587a899c5SKevin Wolf } 108687a899c5SKevin Wolf 1087394c7d4dSKevin Wolf /* Add virtio block device */ 1088394c7d4dSKevin Wolf devaddr = qemu_opt_get(legacy_opts, "addr"); 1089394c7d4dSKevin Wolf if (devaddr && type != IF_VIRTIO) { 1090394c7d4dSKevin Wolf error_report("addr is not supported by this bus type"); 1091394c7d4dSKevin Wolf goto fail; 1092394c7d4dSKevin Wolf } 1093394c7d4dSKevin Wolf 1094394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 1095394c7d4dSKevin Wolf QemuOpts *devopts; 109687ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 109787ea75d5SPeter Crosthwaite &error_abort); 1098394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 10991f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1100394c7d4dSKevin Wolf } else { 1101f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1102394c7d4dSKevin Wolf } 1103f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1104f43e47dbSMarkus Armbruster &error_abort); 1105394c7d4dSKevin Wolf if (devaddr) { 1106f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1107394c7d4dSKevin Wolf } 1108394c7d4dSKevin Wolf } 1109394c7d4dSKevin Wolf 1110d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 1111d095b465SMax Reitz 1112ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 1113ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 1114ee13ed1cSKevin Wolf if (werror != NULL) { 1115ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1116ee13ed1cSKevin Wolf type != IF_NONE) { 1117ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 1118ee13ed1cSKevin Wolf goto fail; 1119ee13ed1cSKevin Wolf } 1120ee13ed1cSKevin Wolf qdict_put(bs_opts, "werror", qstring_from_str(werror)); 1121ee13ed1cSKevin Wolf } 1122ee13ed1cSKevin Wolf 1123ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 1124ee13ed1cSKevin Wolf if (rerror != NULL) { 1125ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1126ee13ed1cSKevin Wolf type != IF_NONE) { 1127ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 1128ee13ed1cSKevin Wolf goto fail; 1129ee13ed1cSKevin Wolf } 1130ee13ed1cSKevin Wolf qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); 1131ee13ed1cSKevin Wolf } 1132ee13ed1cSKevin Wolf 11332d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 113418e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 11353cb0e25cSMarkus Armbruster bs_opts = NULL; 113618e46a03SMarkus Armbruster if (!blk) { 113784d18f06SMarkus Armbruster if (local_err) { 1138565f65d2SMarkus Armbruster error_report_err(local_err); 1139b681072dSKevin Wolf } 11402d246f01SKevin Wolf goto fail; 1141b681072dSKevin Wolf } else { 114284d18f06SMarkus Armbruster assert(!local_err); 11432d246f01SKevin Wolf } 11442d246f01SKevin Wolf 114526f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 114626f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1147f298d071SKevin Wolf dinfo->opts = all_opts; 11482d246f01SKevin Wolf 1149b41a7338SKevin Wolf dinfo->cyls = cyls; 1150b41a7338SKevin Wolf dinfo->heads = heads; 1151b41a7338SKevin Wolf dinfo->secs = secs; 1152b41a7338SKevin Wolf dinfo->trans = translation; 1153b41a7338SKevin Wolf 1154ee13ed1cSKevin Wolf dinfo->type = type; 115587a899c5SKevin Wolf dinfo->bus = bus_id; 115687a899c5SKevin Wolf dinfo->unit = unit_id; 1157394c7d4dSKevin Wolf dinfo->devaddr = devaddr; 1158bcf83158SKevin Wolf dinfo->serial = g_strdup(serial); 1159bcf83158SKevin Wolf 116026f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 116126f8b3a8SMarkus Armbruster 1162e34ef046SKevin Wolf switch(type) { 1163e34ef046SKevin Wolf case IF_IDE: 1164e34ef046SKevin Wolf case IF_SCSI: 1165e34ef046SKevin Wolf case IF_XEN: 1166e34ef046SKevin Wolf case IF_NONE: 1167e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1168e34ef046SKevin Wolf break; 1169e34ef046SKevin Wolf default: 1170e34ef046SKevin Wolf break; 1171e34ef046SKevin Wolf } 1172e34ef046SKevin Wolf 11732d246f01SKevin Wolf fail: 117433cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 11753cb0e25cSMarkus Armbruster QDECREF(bs_opts); 11762d246f01SKevin Wolf return dinfo; 117757975222SKevin Wolf } 117857975222SKevin Wolf 11793e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1180666daa68SMarkus Armbruster { 1181666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1182a0e8544cSFam Zheng BlockBackend *blk; 11832d3735d3SStefan Hajnoczi int ret; 11842d3735d3SStefan Hajnoczi 1185e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1186da31d594SMax Reitz ret = blk_commit_all(); 1187e8877497SStefan Hajnoczi } else { 118884aa0140SStefan Hajnoczi BlockDriverState *bs; 118984aa0140SStefan Hajnoczi AioContext *aio_context; 119084aa0140SStefan Hajnoczi 1191a0e8544cSFam Zheng blk = blk_by_name(device); 1192a0e8544cSFam Zheng if (!blk) { 119358513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1194ac59eb95SMarkus Armbruster return; 11956ab4b5abSMarkus Armbruster } 11965433c24fSMax Reitz if (!blk_is_available(blk)) { 11975433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 11985433c24fSMax Reitz return; 11995433c24fSMax Reitz } 120084aa0140SStefan Hajnoczi 120184aa0140SStefan Hajnoczi bs = blk_bs(blk); 120284aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 120384aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 120484aa0140SStefan Hajnoczi 120584aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 120684aa0140SStefan Hajnoczi 120784aa0140SStefan Hajnoczi aio_context_release(aio_context); 12082d3735d3SStefan Hajnoczi } 120958513bdeSJeff Cody if (ret < 0) { 121058513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 121158513bdeSJeff Cody strerror(-ret)); 1212666daa68SMarkus Armbruster } 1213666daa68SMarkus Armbruster } 1214666daa68SMarkus Armbruster 121510f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 12166cc2a415SPaolo Bonzini { 1217c8a83e85SKevin Wolf TransactionActionList list; 12186cc2a415SPaolo Bonzini 121910f75907SEric Blake list.value = action; 12206cc2a415SPaolo Bonzini list.next = NULL; 122194d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 12226cc2a415SPaolo Bonzini } 12236cc2a415SPaolo Bonzini 12240901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 12250901f67eSBenoît Canet bool has_node_name, const char *node_name, 12260901f67eSBenoît Canet const char *snapshot_file, 12270901f67eSBenoît Canet bool has_snapshot_node_name, 12280901f67eSBenoît Canet const char *snapshot_node_name, 12296106e249SLuiz Capitulino bool has_format, const char *format, 12300901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1231f8882568SJes Sorensen { 1232a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 12330901f67eSBenoît Canet .has_device = has_device, 12346cc2a415SPaolo Bonzini .device = (char *) device, 12350901f67eSBenoît Canet .has_node_name = has_node_name, 12360901f67eSBenoît Canet .node_name = (char *) node_name, 12376cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 12380901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 12390901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 12406cc2a415SPaolo Bonzini .has_format = has_format, 12416cc2a415SPaolo Bonzini .format = (char *) format, 12426cc2a415SPaolo Bonzini .has_mode = has_mode, 12436cc2a415SPaolo Bonzini .mode = mode, 12446cc2a415SPaolo Bonzini }; 124510f75907SEric Blake TransactionAction action = { 124610f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 124732bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 124810f75907SEric Blake }; 124910f75907SEric Blake blockdev_do_action(&action, errp); 1250f8882568SJes Sorensen } 1251f8882568SJes Sorensen 125243de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 125343de7e2dSAlberto Garcia Error **errp) 125443de7e2dSAlberto Garcia { 125543de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 125643de7e2dSAlberto Garcia .node = (char *) node, 125743de7e2dSAlberto Garcia .overlay = (char *) overlay 125843de7e2dSAlberto Garcia }; 125910f75907SEric Blake TransactionAction action = { 126010f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 126132bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 126210f75907SEric Blake }; 126310f75907SEric Blake blockdev_do_action(&action, errp); 126443de7e2dSAlberto Garcia } 126543de7e2dSAlberto Garcia 1266f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1267f323bc9eSWenchao Xia const char *name, 1268f323bc9eSWenchao Xia Error **errp) 1269f323bc9eSWenchao Xia { 1270f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1271f323bc9eSWenchao Xia .device = (char *) device, 1272f323bc9eSWenchao Xia .name = (char *) name 1273f323bc9eSWenchao Xia }; 127410f75907SEric Blake TransactionAction action = { 127510f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 127632bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 127710f75907SEric Blake }; 127810f75907SEric Blake blockdev_do_action(&action, errp); 1279f323bc9eSWenchao Xia } 1280f323bc9eSWenchao Xia 128144e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 128244e3e053SWenchao Xia bool has_id, 128344e3e053SWenchao Xia const char *id, 128444e3e053SWenchao Xia bool has_name, 128544e3e053SWenchao Xia const char *name, 128644e3e053SWenchao Xia Error **errp) 128744e3e053SWenchao Xia { 1288a0e8544cSFam Zheng BlockDriverState *bs; 1289a0e8544cSFam Zheng BlockBackend *blk; 12904ef3982aSStefan Hajnoczi AioContext *aio_context; 129144e3e053SWenchao Xia QEMUSnapshotInfo sn; 129244e3e053SWenchao Xia Error *local_err = NULL; 129344e3e053SWenchao Xia SnapshotInfo *info = NULL; 129444e3e053SWenchao Xia int ret; 129544e3e053SWenchao Xia 1296a0e8544cSFam Zheng blk = blk_by_name(device); 1297a0e8544cSFam Zheng if (!blk) { 129875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 129975158ebbSMarkus Armbruster "Device '%s' not found", device); 130044e3e053SWenchao Xia return NULL; 130144e3e053SWenchao Xia } 13025433c24fSMax Reitz 13035433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 13045433c24fSMax Reitz aio_context_acquire(aio_context); 130544e3e053SWenchao Xia 130644e3e053SWenchao Xia if (!has_id) { 130744e3e053SWenchao Xia id = NULL; 130844e3e053SWenchao Xia } 130944e3e053SWenchao Xia 131044e3e053SWenchao Xia if (!has_name) { 131144e3e053SWenchao Xia name = NULL; 131244e3e053SWenchao Xia } 131344e3e053SWenchao Xia 131444e3e053SWenchao Xia if (!id && !name) { 131544e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 13165433c24fSMax Reitz goto out_aio_context; 131744e3e053SWenchao Xia } 131844e3e053SWenchao Xia 13195433c24fSMax Reitz if (!blk_is_available(blk)) { 13205433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 13215433c24fSMax Reitz goto out_aio_context; 13225433c24fSMax Reitz } 13235433c24fSMax Reitz bs = blk_bs(blk); 13244ef3982aSStefan Hajnoczi 13250b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 13260b928854SStefan Hajnoczi goto out_aio_context; 13270b928854SStefan Hajnoczi } 13280b928854SStefan Hajnoczi 132944e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 133084d18f06SMarkus Armbruster if (local_err) { 133144e3e053SWenchao Xia error_propagate(errp, local_err); 13324ef3982aSStefan Hajnoczi goto out_aio_context; 133344e3e053SWenchao Xia } 133444e3e053SWenchao Xia if (!ret) { 133544e3e053SWenchao Xia error_setg(errp, 133644e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 133744e3e053SWenchao Xia "device '%s'", 133844e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 13394ef3982aSStefan Hajnoczi goto out_aio_context; 134044e3e053SWenchao Xia } 134144e3e053SWenchao Xia 134244e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 134384d18f06SMarkus Armbruster if (local_err) { 134444e3e053SWenchao Xia error_propagate(errp, local_err); 13454ef3982aSStefan Hajnoczi goto out_aio_context; 134644e3e053SWenchao Xia } 134744e3e053SWenchao Xia 13484ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13494ef3982aSStefan Hajnoczi 13505839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 135144e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 135244e3e053SWenchao Xia info->name = g_strdup(sn.name); 135344e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 135444e3e053SWenchao Xia info->date_sec = sn.date_sec; 135544e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 135644e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 135744e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 135844e3e053SWenchao Xia 135944e3e053SWenchao Xia return info; 13604ef3982aSStefan Hajnoczi 13614ef3982aSStefan Hajnoczi out_aio_context: 13624ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13634ef3982aSStefan Hajnoczi return NULL; 136444e3e053SWenchao Xia } 13658802d1fdSJeff Cody 1366341ebc2fSJohn Snow /** 1367341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1368341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1369341ebc2fSJohn Snow * the node reference and bitmap names. 1370341ebc2fSJohn Snow * 1371341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1372341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1373341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1374341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1375341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1376341ebc2fSJohn Snow * 1377341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1378341ebc2fSJohn Snow */ 1379341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1380341ebc2fSJohn Snow const char *name, 1381341ebc2fSJohn Snow BlockDriverState **pbs, 1382341ebc2fSJohn Snow AioContext **paio, 1383341ebc2fSJohn Snow Error **errp) 1384341ebc2fSJohn Snow { 1385341ebc2fSJohn Snow BlockDriverState *bs; 1386341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1387341ebc2fSJohn Snow AioContext *aio_context; 1388341ebc2fSJohn Snow 1389341ebc2fSJohn Snow if (!node) { 1390341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1391341ebc2fSJohn Snow return NULL; 1392341ebc2fSJohn Snow } 1393341ebc2fSJohn Snow if (!name) { 1394341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1395341ebc2fSJohn Snow return NULL; 1396341ebc2fSJohn Snow } 1397341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1398341ebc2fSJohn Snow if (!bs) { 1399341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1400341ebc2fSJohn Snow return NULL; 1401341ebc2fSJohn Snow } 1402341ebc2fSJohn Snow 1403341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 1404341ebc2fSJohn Snow aio_context_acquire(aio_context); 1405341ebc2fSJohn Snow 1406341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1407341ebc2fSJohn Snow if (!bitmap) { 1408341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 1409341ebc2fSJohn Snow goto fail; 1410341ebc2fSJohn Snow } 1411341ebc2fSJohn Snow 1412341ebc2fSJohn Snow if (pbs) { 1413341ebc2fSJohn Snow *pbs = bs; 1414341ebc2fSJohn Snow } 1415341ebc2fSJohn Snow if (paio) { 1416341ebc2fSJohn Snow *paio = aio_context; 1417341ebc2fSJohn Snow } else { 1418341ebc2fSJohn Snow aio_context_release(aio_context); 1419341ebc2fSJohn Snow } 1420341ebc2fSJohn Snow 1421341ebc2fSJohn Snow return bitmap; 1422341ebc2fSJohn Snow 1423341ebc2fSJohn Snow fail: 1424341ebc2fSJohn Snow aio_context_release(aio_context); 1425341ebc2fSJohn Snow return NULL; 1426341ebc2fSJohn Snow } 1427341ebc2fSJohn Snow 1428b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1429ba0c86a3SWenchao Xia 143050f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1431ba0c86a3SWenchao Xia 143250f43f0fSJohn Snow /** 143350f43f0fSJohn Snow * BlkActionOps: 143450f43f0fSJohn Snow * Table of operations that define an Action. 143550f43f0fSJohn Snow * 143650f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 143750f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 143850f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 143950f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 144050f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 144150f43f0fSJohn Snow * commit() or abort(). Can be NULL. 144250f43f0fSJohn Snow * 144350f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 144450f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1445ba0c86a3SWenchao Xia */ 144650f43f0fSJohn Snow typedef struct BlkActionOps { 144750f43f0fSJohn Snow size_t instance_size; 144850f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 144950f43f0fSJohn Snow void (*commit)(BlkActionState *common); 145050f43f0fSJohn Snow void (*abort)(BlkActionState *common); 145150f43f0fSJohn Snow void (*clean)(BlkActionState *common); 145250f43f0fSJohn Snow } BlkActionOps; 145350f43f0fSJohn Snow 145450f43f0fSJohn Snow /** 145550f43f0fSJohn Snow * BlkActionState: 145650f43f0fSJohn Snow * Describes one Action's state within a Transaction. 145750f43f0fSJohn Snow * 145850f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 145950f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 146094d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 146150f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 146250f43f0fSJohn Snow * 146350f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 146450f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 146550f43f0fSJohn Snow * base class. 146650f43f0fSJohn Snow */ 146750f43f0fSJohn Snow struct BlkActionState { 1468c8a83e85SKevin Wolf TransactionAction *action; 146950f43f0fSJohn Snow const BlkActionOps *ops; 147094d16a64SJohn Snow BlockJobTxn *block_job_txn; 147194d16a64SJohn Snow TransactionProperties *txn_props; 147250f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1473ba0c86a3SWenchao Xia }; 1474ba0c86a3SWenchao Xia 1475bbe86010SWenchao Xia /* internal snapshot private data */ 1476bbe86010SWenchao Xia typedef struct InternalSnapshotState { 147750f43f0fSJohn Snow BlkActionState common; 1478bbe86010SWenchao Xia BlockDriverState *bs; 14795d6e96efSStefan Hajnoczi AioContext *aio_context; 1480bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1481507306ccSFam Zheng bool created; 1482bbe86010SWenchao Xia } InternalSnapshotState; 1483bbe86010SWenchao Xia 148494d16a64SJohn Snow 148594d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 148694d16a64SJohn Snow { 148794d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 148894d16a64SJohn Snow error_setg(errp, 148994d16a64SJohn Snow "Action '%s' does not support Transaction property " 149094d16a64SJohn Snow "completion-mode = %s", 149194d16a64SJohn Snow TransactionActionKind_lookup[s->action->type], 149294d16a64SJohn Snow ActionCompletionMode_lookup[s->txn_props->completion_mode]); 149394d16a64SJohn Snow return -1; 149494d16a64SJohn Snow } 149594d16a64SJohn Snow return 0; 149694d16a64SJohn Snow } 149794d16a64SJohn Snow 149850f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1499bbe86010SWenchao Xia Error **errp) 1500bbe86010SWenchao Xia { 1501f70edf99SMarkus Armbruster Error *local_err = NULL; 1502bbe86010SWenchao Xia const char *device; 1503bbe86010SWenchao Xia const char *name; 1504a0e8544cSFam Zheng BlockBackend *blk; 1505bbe86010SWenchao Xia BlockDriverState *bs; 1506bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1507bbe86010SWenchao Xia bool ret; 1508bbe86010SWenchao Xia qemu_timeval tv; 1509bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1510bbe86010SWenchao Xia InternalSnapshotState *state; 1511bbe86010SWenchao Xia int ret1; 1512bbe86010SWenchao Xia 15136a8f9661SEric Blake g_assert(common->action->type == 1514bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 151532bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1516bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1517bbe86010SWenchao Xia 1518bbe86010SWenchao Xia /* 1. parse input */ 1519bbe86010SWenchao Xia device = internal->device; 1520bbe86010SWenchao Xia name = internal->name; 1521bbe86010SWenchao Xia 1522bbe86010SWenchao Xia /* 2. check for validation */ 152394d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 152494d16a64SJohn Snow return; 152594d16a64SJohn Snow } 152694d16a64SJohn Snow 1527a0e8544cSFam Zheng blk = blk_by_name(device); 1528a0e8544cSFam Zheng if (!blk) { 152975158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 153075158ebbSMarkus Armbruster "Device '%s' not found", device); 1531bbe86010SWenchao Xia return; 1532bbe86010SWenchao Xia } 1533bbe86010SWenchao Xia 15345d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 15355433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 15365d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 15375d6e96efSStefan Hajnoczi 15385433c24fSMax Reitz if (!blk_is_available(blk)) { 1539c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1540bbe86010SWenchao Xia return; 1541bbe86010SWenchao Xia } 15425433c24fSMax Reitz bs = blk_bs(blk); 1543bbe86010SWenchao Xia 1544507306ccSFam Zheng state->bs = bs; 1545507306ccSFam Zheng bdrv_drained_begin(bs); 1546507306ccSFam Zheng 15473dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 15483dc7ca3cSStefan Hajnoczi return; 15493dc7ca3cSStefan Hajnoczi } 15503dc7ca3cSStefan Hajnoczi 1551bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 155281e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1553bbe86010SWenchao Xia return; 1554bbe86010SWenchao Xia } 1555bbe86010SWenchao Xia 1556bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 155781e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 155881e5f78aSAlberto Garcia "does not support internal snapshots", 155981e5f78aSAlberto Garcia bs->drv->format_name, device); 1560bbe86010SWenchao Xia return; 1561bbe86010SWenchao Xia } 1562bbe86010SWenchao Xia 1563bbe86010SWenchao Xia if (!strlen(name)) { 1564bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1565bbe86010SWenchao Xia return; 1566bbe86010SWenchao Xia } 1567bbe86010SWenchao Xia 1568bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1569f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1570f70edf99SMarkus Armbruster &local_err); 1571f70edf99SMarkus Armbruster if (local_err) { 1572f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1573bbe86010SWenchao Xia return; 1574bbe86010SWenchao Xia } else if (ret) { 1575bbe86010SWenchao Xia error_setg(errp, 1576bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1577bbe86010SWenchao Xia name, device); 1578bbe86010SWenchao Xia return; 1579bbe86010SWenchao Xia } 1580bbe86010SWenchao Xia 1581bbe86010SWenchao Xia /* 3. take the snapshot */ 1582bbe86010SWenchao Xia sn = &state->sn; 1583bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1584bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1585bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1586bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1587bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1588bbe86010SWenchao Xia 1589bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1590bbe86010SWenchao Xia if (ret1 < 0) { 1591bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1592bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1593bbe86010SWenchao Xia name, device); 1594bbe86010SWenchao Xia return; 1595bbe86010SWenchao Xia } 1596bbe86010SWenchao Xia 1597bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1598507306ccSFam Zheng state->created = true; 1599bbe86010SWenchao Xia } 1600bbe86010SWenchao Xia 160150f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1602bbe86010SWenchao Xia { 1603bbe86010SWenchao Xia InternalSnapshotState *state = 1604bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1605bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1606bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1607bbe86010SWenchao Xia Error *local_error = NULL; 1608bbe86010SWenchao Xia 1609507306ccSFam Zheng if (!state->created) { 1610bbe86010SWenchao Xia return; 1611bbe86010SWenchao Xia } 1612bbe86010SWenchao Xia 1613bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1614c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1615c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1616c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1617c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1618c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1619bbe86010SWenchao Xia } 1620bbe86010SWenchao Xia } 1621bbe86010SWenchao Xia 162250f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 16235d6e96efSStefan Hajnoczi { 16245d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 16255d6e96efSStefan Hajnoczi common, common); 16265d6e96efSStefan Hajnoczi 16275d6e96efSStefan Hajnoczi if (state->aio_context) { 1628507306ccSFam Zheng if (state->bs) { 1629507306ccSFam Zheng bdrv_drained_end(state->bs); 1630507306ccSFam Zheng } 16315d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16325d6e96efSStefan Hajnoczi } 16335d6e96efSStefan Hajnoczi } 16345d6e96efSStefan Hajnoczi 1635ba0c86a3SWenchao Xia /* external snapshot private data */ 1636ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 163750f43f0fSJohn Snow BlkActionState common; 16388802d1fdSJeff Cody BlockDriverState *old_bs; 16398802d1fdSJeff Cody BlockDriverState *new_bs; 16405d6e96efSStefan Hajnoczi AioContext *aio_context; 1641ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16428802d1fdSJeff Cody 164350f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16449b9877eeSWenchao Xia Error **errp) 16459b9877eeSWenchao Xia { 164643de7e2dSAlberto Garcia int flags = 0, ret; 164743de7e2dSAlberto Garcia QDict *options = NULL; 16489b9877eeSWenchao Xia Error *local_err = NULL; 164943de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1650e2a31e87SWenchao Xia const char *device; 16510901f67eSBenoît Canet const char *node_name; 165243de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 165343de7e2dSAlberto Garcia const char *snapshot_ref; 165443de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1655e2a31e87SWenchao Xia const char *new_image_file; 1656ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1657ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1658c8a83e85SKevin Wolf TransactionAction *action = common->action; 16599b9877eeSWenchao Xia 166043de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 166143de7e2dSAlberto Garcia * purpose but a different set of parameters */ 166243de7e2dSAlberto Garcia switch (action->type) { 166343de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 166443de7e2dSAlberto Garcia { 166532bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 166643de7e2dSAlberto Garcia device = s->node; 166743de7e2dSAlberto Garcia node_name = s->node; 166843de7e2dSAlberto Garcia new_image_file = NULL; 166943de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1670e2a31e87SWenchao Xia } 167143de7e2dSAlberto Garcia break; 167243de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 167343de7e2dSAlberto Garcia { 167432bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 167543de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 167643de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 167743de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 167843de7e2dSAlberto Garcia snapshot_ref = NULL; 167943de7e2dSAlberto Garcia } 168043de7e2dSAlberto Garcia break; 168143de7e2dSAlberto Garcia default: 168243de7e2dSAlberto Garcia g_assert_not_reached(); 1683e2a31e87SWenchao Xia } 1684e2a31e87SWenchao Xia 1685e2a31e87SWenchao Xia /* start processing */ 168694d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 168794d16a64SJohn Snow return; 168894d16a64SJohn Snow } 168994d16a64SJohn Snow 169043de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 169143de7e2dSAlberto Garcia if (!state->old_bs) { 16929b9877eeSWenchao Xia return; 16939b9877eeSWenchao Xia } 16949b9877eeSWenchao Xia 16955d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 16965d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 16975d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1698da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 16995d6e96efSStefan Hajnoczi 1700ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1701c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 17029b9877eeSWenchao Xia return; 17039b9877eeSWenchao Xia } 17049b9877eeSWenchao Xia 17053718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 17063718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 17079b9877eeSWenchao Xia return; 17089b9877eeSWenchao Xia } 17099b9877eeSWenchao Xia 1710ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1711ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1712c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 17139b9877eeSWenchao Xia return; 17149b9877eeSWenchao Xia } 17159b9877eeSWenchao Xia } 17169b9877eeSWenchao Xia 1717212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1718c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1719f6186f49SBenoît Canet return; 1720f6186f49SBenoît Canet } 1721f6186f49SBenoît Canet 172243de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 172332bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 172443de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 172543de7e2dSAlberto Garcia enum NewImageMode mode; 172643de7e2dSAlberto Garcia const char *snapshot_node_name = 172743de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 172843de7e2dSAlberto Garcia 172943de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 173043de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 173143de7e2dSAlberto Garcia return; 173243de7e2dSAlberto Garcia } 173343de7e2dSAlberto Garcia 173443de7e2dSAlberto Garcia if (snapshot_node_name && 173543de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 173643de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 173743de7e2dSAlberto Garcia return; 173843de7e2dSAlberto Garcia } 173943de7e2dSAlberto Garcia 1740ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17414c844983SKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); 17429b9877eeSWenchao Xia 17439b9877eeSWenchao Xia /* create new image w/backing file */ 174443de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17459b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1746f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1747f86b8b58SKevin Wolf if (size < 0) { 1748f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 1749f86b8b58SKevin Wolf return; 1750f86b8b58SKevin Wolf } 17519b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1752ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1753ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 1754f86b8b58SKevin Wolf NULL, size, flags, &local_err, false); 175584d18f06SMarkus Armbruster if (local_err) { 17569b9877eeSWenchao Xia error_propagate(errp, local_err); 17579b9877eeSWenchao Xia return; 17589b9877eeSWenchao Xia } 17599b9877eeSWenchao Xia } 17609b9877eeSWenchao Xia 17610901f67eSBenoît Canet options = qdict_new(); 176243de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 17630901f67eSBenoît Canet qdict_put(options, "node-name", 17640901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 17650901f67eSBenoît Canet } 1766e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 17670901f67eSBenoît Canet 176843de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 176943de7e2dSAlberto Garcia } 177043de7e2dSAlberto Garcia 1771f67503e5SMax Reitz assert(state->new_bs == NULL); 177243de7e2dSAlberto Garcia ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options, 177343de7e2dSAlberto Garcia flags, errp); 1774f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17759b9877eeSWenchao Xia if (ret != 0) { 177643de7e2dSAlberto Garcia return; 177743de7e2dSAlberto Garcia } 177843de7e2dSAlberto Garcia 1779*1f0c461bSKevin Wolf if (bdrv_has_blk(state->new_bs)) { 178043de7e2dSAlberto Garcia error_setg(errp, "The snapshot is already in use by %s", 1781*1f0c461bSKevin 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 184478f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 184578f51fdeSJohn Snow bool has_format, const char *format, 184678f51fdeSJohn Snow 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 188478f51fdeSJohn Snow do_drive_backup(backup->device, backup->target, 18853037f364SStefan Hajnoczi backup->has_format, backup->format, 1886b53169eaSStefan Hajnoczi backup->sync, 18873037f364SStefan Hajnoczi backup->has_mode, backup->mode, 18883037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1889d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 18903037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 18913037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 189294d16a64SJohn Snow common->block_job_txn, &local_err); 189384d18f06SMarkus Armbruster if (local_err) { 18943037f364SStefan Hajnoczi error_propagate(errp, local_err); 18953037f364SStefan Hajnoczi return; 18963037f364SStefan Hajnoczi } 18973037f364SStefan Hajnoczi 18983037f364SStefan Hajnoczi state->job = state->bs->job; 18993037f364SStefan Hajnoczi } 19003037f364SStefan Hajnoczi 190150f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 19023037f364SStefan Hajnoczi { 19033037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19043037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 19053037f364SStefan Hajnoczi 19063037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 19073037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 19083037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 19093037f364SStefan Hajnoczi } 19103037f364SStefan Hajnoczi } 19113037f364SStefan Hajnoczi 191250f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 19135d6e96efSStefan Hajnoczi { 19145d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19155d6e96efSStefan Hajnoczi 19165d6e96efSStefan Hajnoczi if (state->aio_context) { 19171fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 19185d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 19195d6e96efSStefan Hajnoczi } 19205d6e96efSStefan Hajnoczi } 19215d6e96efSStefan Hajnoczi 1922bd8baecdSFam Zheng typedef struct BlockdevBackupState { 192350f43f0fSJohn Snow BlkActionState common; 1924bd8baecdSFam Zheng BlockDriverState *bs; 1925bd8baecdSFam Zheng BlockJob *job; 1926bd8baecdSFam Zheng AioContext *aio_context; 1927bd8baecdSFam Zheng } BlockdevBackupState; 1928bd8baecdSFam Zheng 192978f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target, 193078f51fdeSJohn Snow enum MirrorSyncMode sync, 193178f51fdeSJohn Snow bool has_speed, int64_t speed, 193278f51fdeSJohn Snow bool has_on_source_error, 193378f51fdeSJohn Snow BlockdevOnError on_source_error, 193478f51fdeSJohn Snow bool has_on_target_error, 193578f51fdeSJohn Snow BlockdevOnError on_target_error, 193678f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 193778f51fdeSJohn Snow 193850f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1939bd8baecdSFam Zheng { 1940bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1941bd8baecdSFam Zheng BlockdevBackup *backup; 19425433c24fSMax Reitz BlockBackend *blk, *target; 1943bd8baecdSFam Zheng Error *local_err = NULL; 1944bd8baecdSFam Zheng 19456a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 194632bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1947bd8baecdSFam Zheng 1948a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1949a0e8544cSFam Zheng if (!blk) { 19505b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->device); 1951bd8baecdSFam Zheng return; 1952bd8baecdSFam Zheng } 1953bd8baecdSFam Zheng 1954ff52bf36SFam Zheng if (!blk_is_available(blk)) { 1955ff52bf36SFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1956ff52bf36SFam Zheng return; 1957ff52bf36SFam Zheng } 1958ff52bf36SFam Zheng 19595433c24fSMax Reitz target = blk_by_name(backup->target); 19605433c24fSMax Reitz if (!target) { 19615b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->target); 1962bd8baecdSFam Zheng return; 1963bd8baecdSFam Zheng } 1964bd8baecdSFam Zheng 1965bd8baecdSFam Zheng /* AioContext is released in .clean() */ 19665433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 19675433c24fSMax Reitz if (state->aio_context != blk_get_aio_context(target)) { 1968bd8baecdSFam Zheng state->aio_context = NULL; 1969bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1970bd8baecdSFam Zheng return; 1971bd8baecdSFam Zheng } 1972bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1973ff52bf36SFam Zheng state->bs = blk_bs(blk); 1974ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1975bd8baecdSFam Zheng 197678f51fdeSJohn Snow do_blockdev_backup(backup->device, backup->target, 1977bd8baecdSFam Zheng backup->sync, 1978bd8baecdSFam Zheng backup->has_speed, backup->speed, 1979bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1980bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 198194d16a64SJohn Snow common->block_job_txn, &local_err); 1982bd8baecdSFam Zheng if (local_err) { 1983bd8baecdSFam Zheng error_propagate(errp, local_err); 1984bd8baecdSFam Zheng return; 1985bd8baecdSFam Zheng } 1986bd8baecdSFam Zheng 1987bd8baecdSFam Zheng state->job = state->bs->job; 1988bd8baecdSFam Zheng } 1989bd8baecdSFam Zheng 199050f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1991bd8baecdSFam Zheng { 1992bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1993bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1994bd8baecdSFam Zheng 1995bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 1996bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 1997bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 1998bd8baecdSFam Zheng } 1999bd8baecdSFam Zheng } 2000bd8baecdSFam Zheng 200150f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 2002bd8baecdSFam Zheng { 2003bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 2004bd8baecdSFam Zheng 2005bd8baecdSFam Zheng if (state->aio_context) { 2006ff52bf36SFam Zheng bdrv_drained_end(state->bs); 2007bd8baecdSFam Zheng aio_context_release(state->aio_context); 2008bd8baecdSFam Zheng } 2009bd8baecdSFam Zheng } 2010bd8baecdSFam Zheng 2011df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 201250f43f0fSJohn Snow BlkActionState common; 2013df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 2014df9a681dSFam Zheng BlockDriverState *bs; 2015df9a681dSFam Zheng AioContext *aio_context; 2016df9a681dSFam Zheng HBitmap *backup; 2017df9a681dSFam Zheng bool prepared; 2018df9a681dSFam Zheng } BlockDirtyBitmapState; 2019df9a681dSFam Zheng 202050f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 2021df9a681dSFam Zheng Error **errp) 2022df9a681dSFam Zheng { 2023df9a681dSFam Zheng Error *local_err = NULL; 2024df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2025df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2026df9a681dSFam Zheng common, common); 2027df9a681dSFam Zheng 202894d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 202994d16a64SJohn Snow return; 203094d16a64SJohn Snow } 203194d16a64SJohn Snow 203232bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2033df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2034df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2035df9a681dSFam Zheng action->has_granularity, action->granularity, 2036df9a681dSFam Zheng &local_err); 2037df9a681dSFam Zheng 2038df9a681dSFam Zheng if (!local_err) { 2039df9a681dSFam Zheng state->prepared = true; 2040df9a681dSFam Zheng } else { 2041df9a681dSFam Zheng error_propagate(errp, local_err); 2042df9a681dSFam Zheng } 2043df9a681dSFam Zheng } 2044df9a681dSFam Zheng 204550f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2046df9a681dSFam Zheng { 2047df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2048df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2049df9a681dSFam Zheng common, common); 2050df9a681dSFam Zheng 205132bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2052df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2053df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2054df9a681dSFam Zheng */ 2055df9a681dSFam Zheng if (state->prepared) { 2056df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2057df9a681dSFam Zheng } 2058df9a681dSFam Zheng } 2059df9a681dSFam Zheng 206050f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2061df9a681dSFam Zheng Error **errp) 2062df9a681dSFam Zheng { 2063df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2064df9a681dSFam Zheng common, common); 2065df9a681dSFam Zheng BlockDirtyBitmap *action; 2066df9a681dSFam Zheng 206794d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 206894d16a64SJohn Snow return; 206994d16a64SJohn Snow } 207094d16a64SJohn Snow 207132bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2072df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2073df9a681dSFam Zheng action->name, 2074df9a681dSFam Zheng &state->bs, 2075df9a681dSFam Zheng &state->aio_context, 2076df9a681dSFam Zheng errp); 2077df9a681dSFam Zheng if (!state->bitmap) { 2078df9a681dSFam Zheng return; 2079df9a681dSFam Zheng } 2080df9a681dSFam Zheng 2081df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2082df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2083df9a681dSFam Zheng return; 2084df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2085df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2086df9a681dSFam Zheng return; 2087df9a681dSFam Zheng } 2088df9a681dSFam Zheng 2089df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2090df9a681dSFam Zheng /* AioContext is released in .clean() */ 2091df9a681dSFam Zheng } 2092df9a681dSFam Zheng 209350f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2094df9a681dSFam Zheng { 2095df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2096df9a681dSFam Zheng common, common); 2097df9a681dSFam Zheng 2098df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2099df9a681dSFam Zheng } 2100df9a681dSFam Zheng 210150f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2102df9a681dSFam Zheng { 2103df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2104df9a681dSFam Zheng common, common); 2105df9a681dSFam Zheng 2106df9a681dSFam Zheng hbitmap_free(state->backup); 2107df9a681dSFam Zheng } 2108df9a681dSFam Zheng 210950f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2110df9a681dSFam Zheng { 2111df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2112df9a681dSFam Zheng common, common); 2113df9a681dSFam Zheng 2114df9a681dSFam Zheng if (state->aio_context) { 2115df9a681dSFam Zheng aio_context_release(state->aio_context); 2116df9a681dSFam Zheng } 2117df9a681dSFam Zheng } 2118df9a681dSFam Zheng 211950f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 212078b18b78SStefan Hajnoczi { 212178b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 212278b18b78SStefan Hajnoczi } 212378b18b78SStefan Hajnoczi 212450f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 212578b18b78SStefan Hajnoczi { 2126dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 212778b18b78SStefan Hajnoczi } 212878b18b78SStefan Hajnoczi 212950f43f0fSJohn Snow static const BlkActionOps actions[] = { 213043de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 213143de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 213243de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 213343de7e2dSAlberto Garcia .commit = external_snapshot_commit, 213443de7e2dSAlberto Garcia .abort = external_snapshot_abort, 21354ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 213643de7e2dSAlberto Garcia }, 2137c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2138ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2139ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2140ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2141ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2142da763e83SFam Zheng .clean = external_snapshot_clean, 2143ba0c86a3SWenchao Xia }, 21443037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21453037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21463037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 21473037f364SStefan Hajnoczi .abort = drive_backup_abort, 21485d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21493037f364SStefan Hajnoczi }, 2150bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2151bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2152bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2153bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2154bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2155bd8baecdSFam Zheng }, 215678b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 215750f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 215878b18b78SStefan Hajnoczi .prepare = abort_prepare, 215978b18b78SStefan Hajnoczi .commit = abort_commit, 216078b18b78SStefan Hajnoczi }, 2161bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2162bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2163bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2164bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21655d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2166bbe86010SWenchao Xia }, 2167df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2168df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2169df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2170df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2171df9a681dSFam Zheng }, 2172df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2173df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2174df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2175df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2176df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2177df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2178df9a681dSFam Zheng } 2179ba0c86a3SWenchao Xia }; 2180ba0c86a3SWenchao Xia 218194d16a64SJohn Snow /** 218294d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 218394d16a64SJohn Snow * that structure with desired defaults if they are unset. 218494d16a64SJohn Snow */ 218594d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 218694d16a64SJohn Snow TransactionProperties *props) 218794d16a64SJohn Snow { 218894d16a64SJohn Snow if (!props) { 218994d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 219094d16a64SJohn Snow } 219194d16a64SJohn Snow 219294d16a64SJohn Snow if (!props->has_completion_mode) { 219394d16a64SJohn Snow props->has_completion_mode = true; 219494d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 219594d16a64SJohn Snow } 219694d16a64SJohn Snow 219794d16a64SJohn Snow return props; 219894d16a64SJohn Snow } 219994d16a64SJohn Snow 22008802d1fdSJeff Cody /* 2201b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2202b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 22038802d1fdSJeff Cody */ 220494d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 220594d16a64SJohn Snow bool has_props, 220694d16a64SJohn Snow struct TransactionProperties *props, 220794d16a64SJohn Snow Error **errp) 22088802d1fdSJeff Cody { 2209c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 221094d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 221150f43f0fSJohn Snow BlkActionState *state, *next; 221243e17041SLuiz Capitulino Error *local_err = NULL; 22138802d1fdSJeff Cody 221450f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 22158802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 22168802d1fdSJeff Cody 221794d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 221894d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 221994d16a64SJohn Snow */ 222094d16a64SJohn Snow props = get_transaction_properties(props); 222194d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 222294d16a64SJohn Snow block_job_txn = block_job_txn_new(); 222394d16a64SJohn Snow } 222494d16a64SJohn Snow 2225b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 22268802d1fdSJeff Cody bdrv_drain_all(); 22278802d1fdSJeff Cody 2228b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 22298802d1fdSJeff Cody while (NULL != dev_entry) { 2230c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 223150f43f0fSJohn Snow const BlkActionOps *ops; 223252e7c241SPaolo Bonzini 22338802d1fdSJeff Cody dev_info = dev_entry->value; 22348802d1fdSJeff Cody dev_entry = dev_entry->next; 22358802d1fdSJeff Cody 22366a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2237ba0c86a3SWenchao Xia 22386a8f9661SEric Blake ops = &actions[dev_info->type]; 2239aa3fe714SMax Reitz assert(ops->instance_size > 0); 2240aa3fe714SMax Reitz 2241ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2242ba5d6ab6SStefan Hajnoczi state->ops = ops; 2243ba5d6ab6SStefan Hajnoczi state->action = dev_info; 224494d16a64SJohn Snow state->block_job_txn = block_job_txn; 224594d16a64SJohn Snow state->txn_props = props; 2246ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22478802d1fdSJeff Cody 2248ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 224984d18f06SMarkus Armbruster if (local_err) { 22509b9877eeSWenchao Xia error_propagate(errp, local_err); 22519b9877eeSWenchao Xia goto delete_and_fail; 22529b9877eeSWenchao Xia } 225352e7c241SPaolo Bonzini } 22548802d1fdSJeff Cody 2255ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2256f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2257ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22588802d1fdSJeff Cody } 2259f9ea81e8SStefan Hajnoczi } 22608802d1fdSJeff Cody 22618802d1fdSJeff Cody /* success */ 22628802d1fdSJeff Cody goto exit; 22638802d1fdSJeff Cody 22648802d1fdSJeff Cody delete_and_fail: 2265b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2266ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2267ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2268ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2269ba0c86a3SWenchao Xia } 22708802d1fdSJeff Cody } 22718802d1fdSJeff Cody exit: 2272ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2273ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2274ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2275ba0c86a3SWenchao Xia } 2276ba5d6ab6SStefan Hajnoczi g_free(state); 22778802d1fdSJeff Cody } 227894d16a64SJohn Snow if (!has_props) { 227994d16a64SJohn Snow qapi_free_TransactionProperties(props); 228094d16a64SJohn Snow } 228194d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22828802d1fdSJeff Cody } 22838802d1fdSJeff Cody 2284c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 2285666daa68SMarkus Armbruster { 228638f54bd1SMax Reitz Error *local_err = NULL; 2287666daa68SMarkus Armbruster 228838f54bd1SMax Reitz qmp_blockdev_open_tray(device, has_force, force, &local_err); 228938f54bd1SMax Reitz if (local_err) { 229038f54bd1SMax Reitz error_propagate(errp, local_err); 2291c245b6a3SLuiz Capitulino return; 2292666daa68SMarkus Armbruster } 229392d48558SLuiz Capitulino 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 23217d8a9f71SMax Reitz void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 23227d8a9f71SMax Reitz Error **errp) 23237d8a9f71SMax Reitz { 23247d8a9f71SMax Reitz BlockBackend *blk; 23257d8a9f71SMax Reitz bool locked; 23267d8a9f71SMax Reitz 23277d8a9f71SMax Reitz if (!has_force) { 23287d8a9f71SMax Reitz force = false; 23297d8a9f71SMax Reitz } 23307d8a9f71SMax Reitz 23317d8a9f71SMax Reitz blk = blk_by_name(device); 23327d8a9f71SMax Reitz if (!blk) { 23337d8a9f71SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 23347d8a9f71SMax Reitz "Device '%s' not found", device); 23357d8a9f71SMax Reitz return; 23367d8a9f71SMax Reitz } 23377d8a9f71SMax Reitz 23387d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23397d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23407d8a9f71SMax Reitz return; 23417d8a9f71SMax Reitz } 23427d8a9f71SMax Reitz 234312c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 234412c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 234512c7ec87SMax Reitz return; 234612c7ec87SMax Reitz } 234712c7ec87SMax Reitz 23487d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23497d8a9f71SMax Reitz return; 23507d8a9f71SMax Reitz } 23517d8a9f71SMax Reitz 23527d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23537d8a9f71SMax Reitz if (locked) { 23547d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23557d8a9f71SMax Reitz } 23567d8a9f71SMax Reitz 23577d8a9f71SMax Reitz if (!locked || force) { 23587d8a9f71SMax Reitz blk_dev_change_media_cb(blk, false); 23597d8a9f71SMax Reitz } 23607d8a9f71SMax Reitz } 23617d8a9f71SMax Reitz 2362abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2363abaaf59dSMax Reitz { 2364abaaf59dSMax Reitz BlockBackend *blk; 2365abaaf59dSMax Reitz 2366abaaf59dSMax Reitz blk = blk_by_name(device); 2367abaaf59dSMax Reitz if (!blk) { 2368abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2369abaaf59dSMax Reitz "Device '%s' not found", device); 2370abaaf59dSMax Reitz return; 2371abaaf59dSMax Reitz } 2372abaaf59dSMax Reitz 2373abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2374abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2375abaaf59dSMax Reitz return; 2376abaaf59dSMax Reitz } 2377abaaf59dSMax Reitz 237812c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 237912c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 238012c7ec87SMax Reitz return; 238112c7ec87SMax Reitz } 238212c7ec87SMax Reitz 2383abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2384abaaf59dSMax Reitz return; 2385abaaf59dSMax Reitz } 2386abaaf59dSMax Reitz 2387abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2388abaaf59dSMax Reitz } 2389abaaf59dSMax Reitz 23906e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 23912814f672SMax Reitz { 23922814f672SMax Reitz BlockBackend *blk; 23932814f672SMax Reitz BlockDriverState *bs; 23942814f672SMax Reitz AioContext *aio_context; 23952814f672SMax Reitz bool has_device; 23962814f672SMax Reitz 23972814f672SMax Reitz blk = blk_by_name(device); 23982814f672SMax Reitz if (!blk) { 23992814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 24002814f672SMax Reitz "Device '%s' not found", device); 24012814f672SMax Reitz return; 24022814f672SMax Reitz } 24032814f672SMax Reitz 24042814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 24052814f672SMax Reitz has_device = blk_get_attached_dev(blk); 24062814f672SMax Reitz 24072814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 24082814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 24092814f672SMax Reitz return; 24102814f672SMax Reitz } 24112814f672SMax Reitz 241212c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 24132814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 24142814f672SMax Reitz return; 24152814f672SMax Reitz } 24162814f672SMax Reitz 24172814f672SMax Reitz bs = blk_bs(blk); 24182814f672SMax Reitz if (!bs) { 24192814f672SMax Reitz return; 24202814f672SMax Reitz } 24212814f672SMax Reitz 24222814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 24232814f672SMax Reitz aio_context_acquire(aio_context); 24242814f672SMax Reitz 24252814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 24262814f672SMax Reitz goto out; 24272814f672SMax Reitz } 24282814f672SMax Reitz 24292814f672SMax Reitz blk_remove_bs(blk); 24302814f672SMax Reitz 243112c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 243212c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 243312c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 243412c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 243512c7ec87SMax Reitz * value passed here (i.e. false). */ 243612c7ec87SMax Reitz blk_dev_change_media_cb(blk, false); 243712c7ec87SMax Reitz } 243812c7ec87SMax Reitz 24392814f672SMax Reitz out: 24402814f672SMax Reitz aio_context_release(aio_context); 24412814f672SMax Reitz } 24422814f672SMax Reitz 2443d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device, 2444d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2445d1299882SMax Reitz { 2446d1299882SMax Reitz BlockBackend *blk; 2447d1299882SMax Reitz bool has_device; 2448d1299882SMax Reitz 2449d1299882SMax Reitz blk = blk_by_name(device); 2450d1299882SMax Reitz if (!blk) { 2451d1299882SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2452d1299882SMax Reitz "Device '%s' not found", device); 2453d1299882SMax Reitz return; 2454d1299882SMax Reitz } 2455d1299882SMax Reitz 2456d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2457d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2458d1299882SMax Reitz 2459d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2460d1299882SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2461d1299882SMax Reitz return; 2462d1299882SMax Reitz } 2463d1299882SMax Reitz 246412c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2465d1299882SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2466d1299882SMax Reitz return; 2467d1299882SMax Reitz } 2468d1299882SMax Reitz 2469d1299882SMax Reitz if (blk_bs(blk)) { 2470d1299882SMax Reitz error_setg(errp, "There already is a medium in device '%s'", device); 2471d1299882SMax Reitz return; 2472d1299882SMax Reitz } 2473d1299882SMax Reitz 2474d1299882SMax Reitz blk_insert_bs(blk, bs); 2475d1299882SMax Reitz 247612c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 247712c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 247812c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 247912c7ec87SMax Reitz * slot here. 248012c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 248112c7ec87SMax Reitz * value passed here (i.e. true). */ 248212c7ec87SMax Reitz blk_dev_change_media_cb(blk, true); 248312c7ec87SMax Reitz } 2484d1299882SMax Reitz } 2485d1299882SMax Reitz 24866e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2487d1299882SMax Reitz Error **errp) 2488d1299882SMax Reitz { 2489d1299882SMax Reitz BlockDriverState *bs; 2490d1299882SMax Reitz 2491d1299882SMax Reitz bs = bdrv_find_node(node_name); 2492d1299882SMax Reitz if (!bs) { 2493d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2494d1299882SMax Reitz return; 2495d1299882SMax Reitz } 2496d1299882SMax Reitz 2497*1f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2498d1299882SMax Reitz error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 2499*1f0c461bSKevin Wolf bdrv_get_parent_name(bs)); 2500d1299882SMax Reitz return; 2501d1299882SMax Reitz } 2502d1299882SMax Reitz 2503d1299882SMax Reitz qmp_blockdev_insert_anon_medium(device, bs, errp); 2504d1299882SMax Reitz } 2505d1299882SMax Reitz 250624fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename, 250724fb4133SMax Reitz bool has_format, const char *format, 250839ff43d9SMax Reitz bool has_read_only, 250939ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 251024fb4133SMax Reitz Error **errp) 2511de2c6c05SMax Reitz { 2512de2c6c05SMax Reitz BlockBackend *blk; 2513de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 2514de2c6c05SMax Reitz int bdrv_flags, ret; 2515de2c6c05SMax Reitz QDict *options = NULL; 2516de2c6c05SMax Reitz Error *err = NULL; 2517de2c6c05SMax Reitz 2518de2c6c05SMax Reitz blk = blk_by_name(device); 2519de2c6c05SMax Reitz if (!blk) { 2520de2c6c05SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2521de2c6c05SMax Reitz "Device '%s' not found", device); 2522de2c6c05SMax Reitz goto fail; 2523de2c6c05SMax Reitz } 2524de2c6c05SMax Reitz 2525de2c6c05SMax Reitz if (blk_bs(blk)) { 2526de2c6c05SMax Reitz blk_update_root_state(blk); 2527de2c6c05SMax Reitz } 2528de2c6c05SMax Reitz 2529de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2530156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2531156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2532de2c6c05SMax Reitz 253339ff43d9SMax Reitz if (!has_read_only) { 253439ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 253539ff43d9SMax Reitz } 253639ff43d9SMax Reitz 253739ff43d9SMax Reitz switch (read_only) { 253839ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 253939ff43d9SMax Reitz break; 254039ff43d9SMax Reitz 254139ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 254239ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 254339ff43d9SMax Reitz break; 254439ff43d9SMax Reitz 254539ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 254639ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 254739ff43d9SMax Reitz break; 254839ff43d9SMax Reitz 254939ff43d9SMax Reitz default: 255039ff43d9SMax Reitz abort(); 255139ff43d9SMax Reitz } 255239ff43d9SMax Reitz 255324fb4133SMax Reitz if (has_format) { 2554de2c6c05SMax Reitz options = qdict_new(); 2555de2c6c05SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2556de2c6c05SMax Reitz } 2557de2c6c05SMax Reitz 2558de2c6c05SMax Reitz assert(!medium_bs); 2559de2c6c05SMax Reitz ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp); 2560de2c6c05SMax Reitz if (ret < 0) { 2561de2c6c05SMax Reitz goto fail; 2562de2c6c05SMax Reitz } 2563de2c6c05SMax Reitz 2564de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2565de2c6c05SMax Reitz if (err) { 2566de2c6c05SMax Reitz error_propagate(errp, err); 2567de2c6c05SMax Reitz goto fail; 2568de2c6c05SMax Reitz } 2569de2c6c05SMax Reitz 2570de2c6c05SMax Reitz qmp_blockdev_open_tray(device, false, false, &err); 2571de2c6c05SMax Reitz if (err) { 2572de2c6c05SMax Reitz error_propagate(errp, err); 2573de2c6c05SMax Reitz goto fail; 2574de2c6c05SMax Reitz } 2575de2c6c05SMax Reitz 25766e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, &err); 2577de2c6c05SMax Reitz if (err) { 2578de2c6c05SMax Reitz error_propagate(errp, err); 2579de2c6c05SMax Reitz goto fail; 2580de2c6c05SMax Reitz } 2581de2c6c05SMax Reitz 2582de2c6c05SMax Reitz qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2583de2c6c05SMax Reitz if (err) { 2584de2c6c05SMax Reitz error_propagate(errp, err); 2585de2c6c05SMax Reitz goto fail; 2586de2c6c05SMax Reitz } 2587de2c6c05SMax Reitz 2588a5614993SKevin Wolf blk_apply_root_state(blk, medium_bs); 2589a5614993SKevin Wolf 2590de2c6c05SMax Reitz qmp_blockdev_close_tray(device, errp); 2591de2c6c05SMax Reitz 2592de2c6c05SMax Reitz fail: 2593de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2594de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2595de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2596de2c6c05SMax Reitz bdrv_unref(medium_bs); 2597de2c6c05SMax Reitz } 2598de2c6c05SMax Reitz 2599727f005eSZhi Yong Wu /* throttling disk I/O limits */ 260080047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 26013e9fab69SBenoît Canet int64_t bps_wr, 26023e9fab69SBenoît Canet int64_t iops, 26033e9fab69SBenoît Canet int64_t iops_rd, 26043e9fab69SBenoît Canet int64_t iops_wr, 26053e9fab69SBenoît Canet bool has_bps_max, 26063e9fab69SBenoît Canet int64_t bps_max, 26073e9fab69SBenoît Canet bool has_bps_rd_max, 26083e9fab69SBenoît Canet int64_t bps_rd_max, 26093e9fab69SBenoît Canet bool has_bps_wr_max, 26103e9fab69SBenoît Canet int64_t bps_wr_max, 26113e9fab69SBenoît Canet bool has_iops_max, 26123e9fab69SBenoît Canet int64_t iops_max, 26133e9fab69SBenoît Canet bool has_iops_rd_max, 26143e9fab69SBenoît Canet int64_t iops_rd_max, 26153e9fab69SBenoît Canet bool has_iops_wr_max, 26162024c1dfSBenoît Canet int64_t iops_wr_max, 2617dce13204SAlberto Garcia bool has_bps_max_length, 2618dce13204SAlberto Garcia int64_t bps_max_length, 2619dce13204SAlberto Garcia bool has_bps_rd_max_length, 2620dce13204SAlberto Garcia int64_t bps_rd_max_length, 2621dce13204SAlberto Garcia bool has_bps_wr_max_length, 2622dce13204SAlberto Garcia int64_t bps_wr_max_length, 2623dce13204SAlberto Garcia bool has_iops_max_length, 2624dce13204SAlberto Garcia int64_t iops_max_length, 2625dce13204SAlberto Garcia bool has_iops_rd_max_length, 2626dce13204SAlberto Garcia int64_t iops_rd_max_length, 2627dce13204SAlberto Garcia bool has_iops_wr_max_length, 2628dce13204SAlberto Garcia int64_t iops_wr_max_length, 26292024c1dfSBenoît Canet bool has_iops_size, 263076f4afb4SAlberto Garcia int64_t iops_size, 263176f4afb4SAlberto Garcia bool has_group, 263276f4afb4SAlberto Garcia const char *group, Error **errp) 2633727f005eSZhi Yong Wu { 2634cc0681c4SBenoît Canet ThrottleConfig cfg; 2635727f005eSZhi Yong Wu BlockDriverState *bs; 2636a0e8544cSFam Zheng BlockBackend *blk; 2637b15446fdSStefan Hajnoczi AioContext *aio_context; 2638727f005eSZhi Yong Wu 2639a0e8544cSFam Zheng blk = blk_by_name(device); 2640a0e8544cSFam Zheng if (!blk) { 264175158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 264275158ebbSMarkus Armbruster "Device '%s' not found", device); 264380047da5SLuiz Capitulino return; 2644727f005eSZhi Yong Wu } 26455433c24fSMax Reitz 26465433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 26475433c24fSMax Reitz aio_context_acquire(aio_context); 26485433c24fSMax Reitz 2649a0e8544cSFam Zheng bs = blk_bs(blk); 26505433c24fSMax Reitz if (!bs) { 26515433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 26525433c24fSMax Reitz goto out; 26535433c24fSMax Reitz } 2654727f005eSZhi Yong Wu 26551588ab5dSAlberto Garcia throttle_config_init(&cfg); 2656cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2657cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2658cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2659727f005eSZhi Yong Wu 2660cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2661cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2662cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2663cc0681c4SBenoît Canet 26643e9fab69SBenoît Canet if (has_bps_max) { 26653e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 26663e9fab69SBenoît Canet } 26673e9fab69SBenoît Canet if (has_bps_rd_max) { 26683e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 26693e9fab69SBenoît Canet } 26703e9fab69SBenoît Canet if (has_bps_wr_max) { 26713e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 26723e9fab69SBenoît Canet } 26733e9fab69SBenoît Canet if (has_iops_max) { 26743e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 26753e9fab69SBenoît Canet } 26763e9fab69SBenoît Canet if (has_iops_rd_max) { 26773e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 26783e9fab69SBenoît Canet } 26793e9fab69SBenoît Canet if (has_iops_wr_max) { 26803e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 26813e9fab69SBenoît Canet } 2682cc0681c4SBenoît Canet 2683dce13204SAlberto Garcia if (has_bps_max_length) { 2684dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = bps_max_length; 2685dce13204SAlberto Garcia } 2686dce13204SAlberto Garcia if (has_bps_rd_max_length) { 2687dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_READ].burst_length = bps_rd_max_length; 2688dce13204SAlberto Garcia } 2689dce13204SAlberto Garcia if (has_bps_wr_max_length) { 2690dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_WRITE].burst_length = bps_wr_max_length; 2691dce13204SAlberto Garcia } 2692dce13204SAlberto Garcia if (has_iops_max_length) { 2693dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = iops_max_length; 2694dce13204SAlberto Garcia } 2695dce13204SAlberto Garcia if (has_iops_rd_max_length) { 2696dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_READ].burst_length = iops_rd_max_length; 2697dce13204SAlberto Garcia } 2698dce13204SAlberto Garcia if (has_iops_wr_max_length) { 2699dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_WRITE].burst_length = iops_wr_max_length; 2700dce13204SAlberto Garcia } 2701dce13204SAlberto Garcia 27022024c1dfSBenoît Canet if (has_iops_size) { 27032024c1dfSBenoît Canet cfg.op_size = iops_size; 27042024c1dfSBenoît Canet } 2705cc0681c4SBenoît Canet 2706d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 27075433c24fSMax Reitz goto out; 2708727f005eSZhi Yong Wu } 2709727f005eSZhi Yong Wu 271076f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 271176f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 271276f4afb4SAlberto Garcia * just update the throttling group. */ 271397148076SKevin Wolf if (!blk_get_public(blk)->throttle_state) { 271497148076SKevin Wolf blk_io_limits_enable(blk, has_group ? group : device); 271576f4afb4SAlberto Garcia } else if (has_group) { 271697148076SKevin Wolf blk_io_limits_update_group(blk, group); 2717727f005eSZhi Yong Wu } 271876f4afb4SAlberto Garcia /* Set the new throttling configuration */ 271997148076SKevin Wolf blk_set_io_limits(blk, &cfg); 272097148076SKevin Wolf } else if (blk_get_public(blk)->throttle_state) { 272176f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 272297148076SKevin Wolf blk_io_limits_disable(blk); 2723727f005eSZhi Yong Wu } 2724b15446fdSStefan Hajnoczi 27255433c24fSMax Reitz out: 2726b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2727727f005eSZhi Yong Wu } 2728727f005eSZhi Yong Wu 2729341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2730341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2731341ebc2fSJohn Snow Error **errp) 2732341ebc2fSJohn Snow { 2733341ebc2fSJohn Snow AioContext *aio_context; 2734341ebc2fSJohn Snow BlockDriverState *bs; 2735341ebc2fSJohn Snow 2736341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2737341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2738341ebc2fSJohn Snow return; 2739341ebc2fSJohn Snow } 2740341ebc2fSJohn Snow 2741341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2742341ebc2fSJohn Snow if (!bs) { 2743341ebc2fSJohn Snow return; 2744341ebc2fSJohn Snow } 2745341ebc2fSJohn Snow 2746341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2747341ebc2fSJohn Snow aio_context_acquire(aio_context); 2748341ebc2fSJohn Snow 2749341ebc2fSJohn Snow if (has_granularity) { 2750341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2751341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2752341ebc2fSJohn Snow "and at least 512"); 2753341ebc2fSJohn Snow goto out; 2754341ebc2fSJohn Snow } 2755341ebc2fSJohn Snow } else { 2756341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2757341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2758341ebc2fSJohn Snow } 2759341ebc2fSJohn Snow 2760341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2761341ebc2fSJohn Snow 2762341ebc2fSJohn Snow out: 2763341ebc2fSJohn Snow aio_context_release(aio_context); 2764341ebc2fSJohn Snow } 2765341ebc2fSJohn Snow 2766341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2767341ebc2fSJohn Snow Error **errp) 2768341ebc2fSJohn Snow { 2769341ebc2fSJohn Snow AioContext *aio_context; 2770341ebc2fSJohn Snow BlockDriverState *bs; 2771341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2772341ebc2fSJohn Snow 2773341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2774341ebc2fSJohn Snow if (!bitmap || !bs) { 2775341ebc2fSJohn Snow return; 2776341ebc2fSJohn Snow } 2777341ebc2fSJohn Snow 27789bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 27799bd2b08fSJohn Snow error_setg(errp, 27809bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 27819bd2b08fSJohn Snow name); 27829bd2b08fSJohn Snow goto out; 27839bd2b08fSJohn Snow } 278420dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2785341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2786341ebc2fSJohn Snow 27879bd2b08fSJohn Snow out: 2788341ebc2fSJohn Snow aio_context_release(aio_context); 2789341ebc2fSJohn Snow } 2790341ebc2fSJohn Snow 2791e74e6b78SJohn Snow /** 2792e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2793e74e6b78SJohn Snow * immediately after a full backup operation. 2794e74e6b78SJohn Snow */ 2795e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2796e74e6b78SJohn Snow Error **errp) 2797e74e6b78SJohn Snow { 2798e74e6b78SJohn Snow AioContext *aio_context; 2799e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2800e74e6b78SJohn Snow BlockDriverState *bs; 2801e74e6b78SJohn Snow 2802e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2803e74e6b78SJohn Snow if (!bitmap || !bs) { 2804e74e6b78SJohn Snow return; 2805e74e6b78SJohn Snow } 2806e74e6b78SJohn Snow 2807e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2808e74e6b78SJohn Snow error_setg(errp, 2809e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2810e74e6b78SJohn Snow name); 2811e74e6b78SJohn Snow goto out; 2812e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2813e74e6b78SJohn Snow error_setg(errp, 2814e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2815e74e6b78SJohn Snow name); 2816e74e6b78SJohn Snow goto out; 2817e74e6b78SJohn Snow } 2818e74e6b78SJohn Snow 2819df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2820e74e6b78SJohn Snow 2821e74e6b78SJohn Snow out: 2822e74e6b78SJohn Snow aio_context_release(aio_context); 2823e74e6b78SJohn Snow } 2824e74e6b78SJohn Snow 2825072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 28269063f814SRyan Harper { 28279063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 28287e7d56d9SMarkus Armbruster BlockBackend *blk; 28299063f814SRyan Harper BlockDriverState *bs; 28308ad4202bSStefan Hajnoczi AioContext *aio_context; 28313718d8abSFam Zheng Error *local_err = NULL; 28329063f814SRyan Harper 28332073d410SKevin Wolf bs = bdrv_find_node(id); 28342073d410SKevin Wolf if (bs) { 28352073d410SKevin Wolf qmp_x_blockdev_del(false, NULL, true, id, &local_err); 28362073d410SKevin Wolf if (local_err) { 28372073d410SKevin Wolf error_report_err(local_err); 28382073d410SKevin Wolf } 28392073d410SKevin Wolf return; 28402073d410SKevin Wolf } 28412073d410SKevin Wolf 28427e7d56d9SMarkus Armbruster blk = blk_by_name(id); 28437e7d56d9SMarkus Armbruster if (!blk) { 2844b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2845072ebe6bSMarkus Armbruster return; 28469063f814SRyan Harper } 28478ad4202bSStefan Hajnoczi 284826f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 284948f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 285048f364ddSMarkus Armbruster " is not supported"); 2851072ebe6bSMarkus Armbruster return; 285248f364ddSMarkus Armbruster } 285348f364ddSMarkus Armbruster 28545433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 28558ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 28568ad4202bSStefan Hajnoczi 28575433c24fSMax Reitz bs = blk_bs(blk); 28585433c24fSMax Reitz if (bs) { 28593718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2860565f65d2SMarkus Armbruster error_report_err(local_err); 28618ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2862072ebe6bSMarkus Armbruster return; 28638591675fSMarcelo Tosatti } 28649063f814SRyan Harper 2865938abd43SMax Reitz blk_remove_bs(blk); 28665433c24fSMax Reitz } 28679063f814SRyan Harper 28687c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 2869efaa7c4eSMax Reitz monitor_remove_blk(blk); 2870efaa7c4eSMax Reitz 28717c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 28727c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 2873d22b2f41SRyan Harper */ 2874a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 2875293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2876373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2877293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2878d22b2f41SRyan Harper } else { 2879b9fe8a7aSMarkus Armbruster blk_unref(blk); 2880d22b2f41SRyan Harper } 28819063f814SRyan Harper 28828ad4202bSStefan Hajnoczi aio_context_release(aio_context); 28839063f814SRyan Harper } 28846d4a2b3aSChristoph Hellwig 28853b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 28863b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 28873b1dbd11SBenoît Canet int64_t size, Error **errp) 28886d4a2b3aSChristoph Hellwig { 28893b1dbd11SBenoît Canet Error *local_err = NULL; 28906d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2891927e0e76SStefan Hajnoczi AioContext *aio_context; 28928732901eSKevin Wolf int ret; 28936d4a2b3aSChristoph Hellwig 28943b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 28953b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 28963b1dbd11SBenoît Canet &local_err); 289784d18f06SMarkus Armbruster if (local_err) { 28983b1dbd11SBenoît Canet error_propagate(errp, local_err); 28993b1dbd11SBenoît Canet return; 29003b1dbd11SBenoît Canet } 29013b1dbd11SBenoît Canet 2902927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2903927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2904927e0e76SStefan Hajnoczi 29053b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2906c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2907927e0e76SStefan Hajnoczi goto out; 29086d4a2b3aSChristoph Hellwig } 29096d4a2b3aSChristoph Hellwig 29106d4a2b3aSChristoph Hellwig if (size < 0) { 2911c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2912927e0e76SStefan Hajnoczi goto out; 29136d4a2b3aSChristoph Hellwig } 29146d4a2b3aSChristoph Hellwig 29159c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2916c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2917927e0e76SStefan Hajnoczi goto out; 29189c75e168SJeff Cody } 29199c75e168SJeff Cody 292092b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 292192b7a08dSPeter Lieven bdrv_drain_all(); 292292b7a08dSPeter Lieven 29238732901eSKevin Wolf ret = bdrv_truncate(bs, size); 29248732901eSKevin Wolf switch (ret) { 2925939a1cc3SStefan Hajnoczi case 0: 2926939a1cc3SStefan Hajnoczi break; 2927939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2928c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2929939a1cc3SStefan Hajnoczi break; 2930939a1cc3SStefan Hajnoczi case -ENOTSUP: 2931c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2932939a1cc3SStefan Hajnoczi break; 2933939a1cc3SStefan Hajnoczi case -EACCES: 293481e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2935939a1cc3SStefan Hajnoczi break; 2936939a1cc3SStefan Hajnoczi case -EBUSY: 2937c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2938939a1cc3SStefan Hajnoczi break; 2939939a1cc3SStefan Hajnoczi default: 29408732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2941939a1cc3SStefan Hajnoczi break; 29426d4a2b3aSChristoph Hellwig } 2943927e0e76SStefan Hajnoczi 2944927e0e76SStefan Hajnoczi out: 2945927e0e76SStefan Hajnoczi aio_context_release(aio_context); 29466d4a2b3aSChristoph Hellwig } 294712bd451fSStefan Hajnoczi 29489abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 294912bd451fSStefan Hajnoczi { 2950723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2951723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2952723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2953723c5d93SStefan Hajnoczi */ 2954723c5d93SStefan Hajnoczi 295512bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2956bcada37bSWenchao Xia const char *msg = NULL; 295712bd451fSStefan Hajnoczi 29589abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 295912bd451fSStefan Hajnoczi 296012bd451fSStefan Hajnoczi assert(bs->job); 2961bcada37bSWenchao Xia 296212bd451fSStefan Hajnoczi if (ret < 0) { 2963bcada37bSWenchao Xia msg = strerror(-ret); 296412bd451fSStefan Hajnoczi } 296512bd451fSStefan Hajnoczi 2966370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 2967bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 2968370521a1SStefan Hajnoczi } else { 2969bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 2970370521a1SStefan Hajnoczi } 297112bd451fSStefan Hajnoczi } 297212bd451fSStefan Hajnoczi 297313d8cc51SJeff Cody void qmp_block_stream(const char *device, 297413d8cc51SJeff Cody bool has_base, const char *base, 297513d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 297613d8cc51SJeff Cody bool has_speed, int64_t speed, 29771d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 29781d809098SPaolo Bonzini Error **errp) 297912bd451fSStefan Hajnoczi { 2980a0e8544cSFam Zheng BlockBackend *blk; 298112bd451fSStefan Hajnoczi BlockDriverState *bs; 2982c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 2983f3e69bebSStefan Hajnoczi AioContext *aio_context; 2984fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 298513d8cc51SJeff Cody const char *base_name = NULL; 298612bd451fSStefan Hajnoczi 29871d809098SPaolo Bonzini if (!has_on_error) { 29881d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 29891d809098SPaolo Bonzini } 29901d809098SPaolo Bonzini 2991a0e8544cSFam Zheng blk = blk_by_name(device); 2992a0e8544cSFam Zheng if (!blk) { 299375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 299475158ebbSMarkus Armbruster "Device '%s' not found", device); 299512bd451fSStefan Hajnoczi return; 299612bd451fSStefan Hajnoczi } 299712bd451fSStefan Hajnoczi 29985433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 2999f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 3000f3e69bebSStefan Hajnoczi 30015433c24fSMax Reitz if (!blk_is_available(blk)) { 30025433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 30035433c24fSMax Reitz goto out; 30045433c24fSMax Reitz } 30055433c24fSMax Reitz bs = blk_bs(blk); 30065433c24fSMax Reitz 3007628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 3008f3e69bebSStefan Hajnoczi goto out; 3009628ff683SFam Zheng } 3010628ff683SFam Zheng 301113d8cc51SJeff Cody if (has_base) { 3012c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3013c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3014c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3015f3e69bebSStefan Hajnoczi goto out; 301612bd451fSStefan Hajnoczi } 3017f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 301813d8cc51SJeff Cody base_name = base; 3019c8c3080fSMarcelo Tosatti } 302012bd451fSStefan Hajnoczi 302113d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 302213d8cc51SJeff Cody * file, and specifying one is therefore an error */ 302313d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 302413d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 302513d8cc51SJeff Cody "entire chain"); 3026f3e69bebSStefan Hajnoczi goto out; 302713d8cc51SJeff Cody } 302813d8cc51SJeff Cody 302913d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 303013d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 303113d8cc51SJeff Cody 303213d8cc51SJeff Cody stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 30331d809098SPaolo Bonzini on_error, block_job_cb, bs, &local_err); 303484d18f06SMarkus Armbruster if (local_err) { 3035fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3036f3e69bebSStefan Hajnoczi goto out; 303712bd451fSStefan Hajnoczi } 303812bd451fSStefan Hajnoczi 303912bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3040f3e69bebSStefan Hajnoczi 3041f3e69bebSStefan Hajnoczi out: 3042f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 304312bd451fSStefan Hajnoczi } 30442d47c6e9SStefan Hajnoczi 3045ed61fc10SJeff Cody void qmp_block_commit(const char *device, 30467676e2c5SJeff Cody bool has_base, const char *base, 30477676e2c5SJeff Cody bool has_top, const char *top, 304854e26900SJeff Cody bool has_backing_file, const char *backing_file, 3049ed61fc10SJeff Cody bool has_speed, int64_t speed, 3050ed61fc10SJeff Cody Error **errp) 3051ed61fc10SJeff Cody { 3052a0e8544cSFam Zheng BlockBackend *blk; 3053ed61fc10SJeff Cody BlockDriverState *bs; 3054ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 30559e85cd5cSStefan Hajnoczi AioContext *aio_context; 3056ed61fc10SJeff Cody Error *local_err = NULL; 3057ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3058ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3059ed61fc10SJeff Cody */ 306092aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3061ed61fc10SJeff Cody 306254504663SMax Reitz if (!has_speed) { 306354504663SMax Reitz speed = 0; 306454504663SMax Reitz } 306554504663SMax Reitz 30667676e2c5SJeff Cody /* Important Note: 30677676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 30687676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 30697676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 30707676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 3071a0e8544cSFam Zheng blk = blk_by_name(device); 3072a0e8544cSFam Zheng if (!blk) { 307375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 307475158ebbSMarkus Armbruster "Device '%s' not found", device); 3075ed61fc10SJeff Cody return; 3076ed61fc10SJeff Cody } 3077ed61fc10SJeff Cody 30785433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 30799e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 30809e85cd5cSStefan Hajnoczi 30815433c24fSMax Reitz if (!blk_is_available(blk)) { 30825433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 30835433c24fSMax Reitz goto out; 30845433c24fSMax Reitz } 30855433c24fSMax Reitz bs = blk_bs(blk); 30865433c24fSMax Reitz 3087bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 30889e85cd5cSStefan Hajnoczi goto out; 3089628ff683SFam Zheng } 3090628ff683SFam Zheng 3091ed61fc10SJeff Cody /* default top_bs is the active layer */ 3092ed61fc10SJeff Cody top_bs = bs; 3093ed61fc10SJeff Cody 30947676e2c5SJeff Cody if (has_top && top) { 3095ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3096ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3097ed61fc10SJeff Cody } 3098ed61fc10SJeff Cody } 3099ed61fc10SJeff Cody 3100ed61fc10SJeff Cody if (top_bs == NULL) { 3101ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 31029e85cd5cSStefan Hajnoczi goto out; 3103ed61fc10SJeff Cody } 3104ed61fc10SJeff Cody 31059e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 31069e85cd5cSStefan Hajnoczi 3107d5208c45SJeff Cody if (has_base && base) { 3108d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3109d5208c45SJeff Cody } else { 3110d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3111d5208c45SJeff Cody } 3112d5208c45SJeff Cody 3113d5208c45SJeff Cody if (base_bs == NULL) { 3114c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 31159e85cd5cSStefan Hajnoczi goto out; 3116d5208c45SJeff Cody } 3117d5208c45SJeff Cody 31189e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 31199e85cd5cSStefan Hajnoczi 3120bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3121bb00021dSFam Zheng goto out; 3122bb00021dSFam Zheng } 3123bb00021dSFam Zheng 31247676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 31257676e2c5SJeff Cody if (top_bs == base_bs) { 31267676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 31279e85cd5cSStefan Hajnoczi goto out; 31287676e2c5SJeff Cody } 31297676e2c5SJeff Cody 313020a63d2cSFam Zheng if (top_bs == bs) { 313154e26900SJeff Cody if (has_backing_file) { 313254e26900SJeff Cody error_setg(errp, "'backing-file' specified," 313354e26900SJeff Cody " but 'top' is the active layer"); 31349e85cd5cSStefan Hajnoczi goto out; 313554e26900SJeff Cody } 313620a63d2cSFam Zheng commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 313720a63d2cSFam Zheng bs, &local_err); 313820a63d2cSFam Zheng } else { 3139ed61fc10SJeff Cody commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 314054e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 314120a63d2cSFam Zheng } 3142ed61fc10SJeff Cody if (local_err != NULL) { 3143ed61fc10SJeff Cody error_propagate(errp, local_err); 31449e85cd5cSStefan Hajnoczi goto out; 3145ed61fc10SJeff Cody } 31469e85cd5cSStefan Hajnoczi 31479e85cd5cSStefan Hajnoczi out: 31489e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3149ed61fc10SJeff Cody } 3150ed61fc10SJeff Cody 315178f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 315299a9addfSStefan Hajnoczi bool has_format, const char *format, 3153b53169eaSStefan Hajnoczi enum MirrorSyncMode sync, 315499a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 315599a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 3156d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 315778f51fdeSJohn Snow bool has_on_source_error, 315878f51fdeSJohn Snow BlockdevOnError on_source_error, 315978f51fdeSJohn Snow bool has_on_target_error, 316078f51fdeSJohn Snow BlockdevOnError on_target_error, 316178f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 316299a9addfSStefan Hajnoczi { 3163a0e8544cSFam Zheng BlockBackend *blk; 316499a9addfSStefan Hajnoczi BlockDriverState *bs; 316599a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3166fc5d3f84SIan Main BlockDriverState *source = NULL; 3167d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3168761731b1SStefan Hajnoczi AioContext *aio_context; 3169e6641719SMax Reitz QDict *options = NULL; 317099a9addfSStefan Hajnoczi Error *local_err = NULL; 317199a9addfSStefan Hajnoczi int flags; 317299a9addfSStefan Hajnoczi int64_t size; 317399a9addfSStefan Hajnoczi int ret; 317499a9addfSStefan Hajnoczi 317599a9addfSStefan Hajnoczi if (!has_speed) { 317699a9addfSStefan Hajnoczi speed = 0; 317799a9addfSStefan Hajnoczi } 317899a9addfSStefan Hajnoczi if (!has_on_source_error) { 317999a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 318099a9addfSStefan Hajnoczi } 318199a9addfSStefan Hajnoczi if (!has_on_target_error) { 318299a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 318399a9addfSStefan Hajnoczi } 318499a9addfSStefan Hajnoczi if (!has_mode) { 318599a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 318699a9addfSStefan Hajnoczi } 318799a9addfSStefan Hajnoczi 3188a0e8544cSFam Zheng blk = blk_by_name(device); 3189a0e8544cSFam Zheng if (!blk) { 319075158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 319175158ebbSMarkus Armbruster "Device '%s' not found", device); 319299a9addfSStefan Hajnoczi return; 319399a9addfSStefan Hajnoczi } 319499a9addfSStefan Hajnoczi 31955433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3196761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3197761731b1SStefan Hajnoczi 3198c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 3199c29c1dd3SFam Zheng * do an early check redundantly. */ 32005433c24fSMax Reitz if (!blk_is_available(blk)) { 3201c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3202761731b1SStefan Hajnoczi goto out; 320399a9addfSStefan Hajnoczi } 32045433c24fSMax Reitz bs = blk_bs(blk); 320599a9addfSStefan Hajnoczi 320699a9addfSStefan Hajnoczi if (!has_format) { 320799a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 320899a9addfSStefan Hajnoczi } 320999a9addfSStefan Hajnoczi 3210c29c1dd3SFam Zheng /* Early check to avoid creating target */ 32113718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3212761731b1SStefan Hajnoczi goto out; 321399a9addfSStefan Hajnoczi } 321499a9addfSStefan Hajnoczi 321561de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 321699a9addfSStefan Hajnoczi 3217fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3218fc5d3f84SIan Main * on top of. */ 3219fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 3220760e0063SKevin Wolf source = backing_bs(bs); 3221fc5d3f84SIan Main if (!source) { 3222fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 3223fc5d3f84SIan Main } 3224fc5d3f84SIan Main } 3225fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 3226fc5d3f84SIan Main source = bs; 3227fc5d3f84SIan Main } 3228fc5d3f84SIan Main 322999a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 323099a9addfSStefan Hajnoczi if (size < 0) { 323199a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3232761731b1SStefan Hajnoczi goto out; 323399a9addfSStefan Hajnoczi } 323499a9addfSStefan Hajnoczi 323599a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 3236e6641719SMax Reitz assert(format); 3237fc5d3f84SIan Main if (source) { 3238fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 3239fc5d3f84SIan Main source->drv->format_name, NULL, 3240fc5d3f84SIan Main size, flags, &local_err, false); 3241fc5d3f84SIan Main } else { 3242fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 3243fc5d3f84SIan Main size, flags, &local_err, false); 3244fc5d3f84SIan Main } 324599a9addfSStefan Hajnoczi } 324699a9addfSStefan Hajnoczi 324784d18f06SMarkus Armbruster if (local_err) { 324899a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3249761731b1SStefan Hajnoczi goto out; 325099a9addfSStefan Hajnoczi } 325199a9addfSStefan Hajnoczi 3252e6641719SMax Reitz if (format) { 3253e6641719SMax Reitz options = qdict_new(); 3254e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3255e6641719SMax Reitz } 3256e6641719SMax Reitz 3257f67503e5SMax Reitz target_bs = NULL; 32586ebf9aa2SMax Reitz ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err); 325999a9addfSStefan Hajnoczi if (ret < 0) { 326034b5d2c6SMax Reitz error_propagate(errp, local_err); 3261761731b1SStefan Hajnoczi goto out; 326299a9addfSStefan Hajnoczi } 326399a9addfSStefan Hajnoczi 3264761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3265761731b1SStefan Hajnoczi 3266d58d8453SJohn Snow if (has_bitmap) { 3267d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3268d58d8453SJohn Snow if (!bmap) { 3269d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 32700702d3d8SMax Reitz bdrv_unref(target_bs); 3271d58d8453SJohn Snow goto out; 3272d58d8453SJohn Snow } 3273d58d8453SJohn Snow } 3274d58d8453SJohn Snow 3275d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, bmap, 3276d58d8453SJohn Snow on_source_error, on_target_error, 327778f51fdeSJohn Snow block_job_cb, bs, txn, &local_err); 327899a9addfSStefan Hajnoczi if (local_err != NULL) { 32794f6fd349SFam Zheng bdrv_unref(target_bs); 328099a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3281761731b1SStefan Hajnoczi goto out; 328299a9addfSStefan Hajnoczi } 3283761731b1SStefan Hajnoczi 3284761731b1SStefan Hajnoczi out: 3285761731b1SStefan Hajnoczi aio_context_release(aio_context); 328699a9addfSStefan Hajnoczi } 328799a9addfSStefan Hajnoczi 328878f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target, 328978f51fdeSJohn Snow bool has_format, const char *format, 329078f51fdeSJohn Snow enum MirrorSyncMode sync, 329178f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 329278f51fdeSJohn Snow bool has_speed, int64_t speed, 329378f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 329478f51fdeSJohn Snow bool has_on_source_error, BlockdevOnError on_source_error, 329578f51fdeSJohn Snow bool has_on_target_error, BlockdevOnError on_target_error, 329678f51fdeSJohn Snow Error **errp) 329778f51fdeSJohn Snow { 329878f51fdeSJohn Snow return do_drive_backup(device, target, has_format, format, sync, 329978f51fdeSJohn Snow has_mode, mode, has_speed, speed, 330078f51fdeSJohn Snow has_bitmap, bitmap, 330178f51fdeSJohn Snow has_on_source_error, on_source_error, 330278f51fdeSJohn Snow has_on_target_error, on_target_error, 330378f51fdeSJohn Snow NULL, errp); 330478f51fdeSJohn Snow } 330578f51fdeSJohn Snow 3306c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3307c13163fbSBenoît Canet { 3308d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3309c13163fbSBenoît Canet } 3310c13163fbSBenoît Canet 331178f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target, 3312c29c1dd3SFam Zheng enum MirrorSyncMode sync, 3313c29c1dd3SFam Zheng bool has_speed, int64_t speed, 3314c29c1dd3SFam Zheng bool has_on_source_error, 3315c29c1dd3SFam Zheng BlockdevOnError on_source_error, 3316c29c1dd3SFam Zheng bool has_on_target_error, 3317c29c1dd3SFam Zheng BlockdevOnError on_target_error, 331878f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 3319c29c1dd3SFam Zheng { 33205433c24fSMax Reitz BlockBackend *blk, *target_blk; 3321c29c1dd3SFam Zheng BlockDriverState *bs; 3322c29c1dd3SFam Zheng BlockDriverState *target_bs; 3323c29c1dd3SFam Zheng Error *local_err = NULL; 3324c29c1dd3SFam Zheng AioContext *aio_context; 3325c29c1dd3SFam Zheng 3326c29c1dd3SFam Zheng if (!has_speed) { 3327c29c1dd3SFam Zheng speed = 0; 3328c29c1dd3SFam Zheng } 3329c29c1dd3SFam Zheng if (!has_on_source_error) { 3330c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3331c29c1dd3SFam Zheng } 3332c29c1dd3SFam Zheng if (!has_on_target_error) { 3333c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3334c29c1dd3SFam Zheng } 3335c29c1dd3SFam Zheng 3336a0e8544cSFam Zheng blk = blk_by_name(device); 3337a0e8544cSFam Zheng if (!blk) { 33385b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", device); 3339c29c1dd3SFam Zheng return; 3340c29c1dd3SFam Zheng } 3341c29c1dd3SFam Zheng 33425433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3343c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3344c29c1dd3SFam Zheng 33455433c24fSMax Reitz if (!blk_is_available(blk)) { 33465433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 33475433c24fSMax Reitz goto out; 33485433c24fSMax Reitz } 33495433c24fSMax Reitz bs = blk_bs(blk); 33505433c24fSMax Reitz 33515433c24fSMax Reitz target_blk = blk_by_name(target); 33525433c24fSMax Reitz if (!target_blk) { 33535b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", target); 3354c29c1dd3SFam Zheng goto out; 3355c29c1dd3SFam Zheng } 33565433c24fSMax Reitz 33575433c24fSMax Reitz if (!blk_is_available(target_blk)) { 33585433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", target); 33595433c24fSMax Reitz goto out; 33605433c24fSMax Reitz } 33615433c24fSMax Reitz target_bs = blk_bs(target_blk); 3362c29c1dd3SFam Zheng 3363c29c1dd3SFam Zheng bdrv_ref(target_bs); 3364c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3365d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 336678f51fdeSJohn Snow on_target_error, block_job_cb, bs, txn, &local_err); 3367c29c1dd3SFam Zheng if (local_err != NULL) { 3368c29c1dd3SFam Zheng bdrv_unref(target_bs); 3369c29c1dd3SFam Zheng error_propagate(errp, local_err); 3370c29c1dd3SFam Zheng } 3371c29c1dd3SFam Zheng out: 3372c29c1dd3SFam Zheng aio_context_release(aio_context); 3373c29c1dd3SFam Zheng } 3374c29c1dd3SFam Zheng 337578f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target, 337678f51fdeSJohn Snow enum MirrorSyncMode sync, 337778f51fdeSJohn Snow bool has_speed, int64_t speed, 337878f51fdeSJohn Snow bool has_on_source_error, 337978f51fdeSJohn Snow BlockdevOnError on_source_error, 338078f51fdeSJohn Snow bool has_on_target_error, 338178f51fdeSJohn Snow BlockdevOnError on_target_error, 338278f51fdeSJohn Snow Error **errp) 338378f51fdeSJohn Snow { 338478f51fdeSJohn Snow do_blockdev_backup(device, target, sync, has_speed, speed, 338578f51fdeSJohn Snow has_on_source_error, on_source_error, 338678f51fdeSJohn Snow has_on_target_error, on_target_error, 338778f51fdeSJohn Snow NULL, errp); 338878f51fdeSJohn Snow } 338978f51fdeSJohn Snow 33904193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 33914193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 33924193cdd7SFam Zheng **/ 33934193cdd7SFam Zheng static void blockdev_mirror_common(BlockDriverState *bs, 33944193cdd7SFam Zheng BlockDriverState *target, 339509158f00SBenoît Canet bool has_replaces, const char *replaces, 3396d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3397b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3398eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 339908e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 34004193cdd7SFam Zheng bool has_on_source_error, 34014193cdd7SFam Zheng BlockdevOnError on_source_error, 34024193cdd7SFam Zheng bool has_on_target_error, 34034193cdd7SFam Zheng BlockdevOnError on_target_error, 34040fc9f8eaSFam Zheng bool has_unmap, bool unmap, 3405b952b558SPaolo Bonzini Error **errp) 3406d9b902dbSPaolo Bonzini { 3407d9b902dbSPaolo Bonzini 3408d9b902dbSPaolo Bonzini if (!has_speed) { 3409d9b902dbSPaolo Bonzini speed = 0; 3410d9b902dbSPaolo Bonzini } 3411b952b558SPaolo Bonzini if (!has_on_source_error) { 3412b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3413b952b558SPaolo Bonzini } 3414b952b558SPaolo Bonzini if (!has_on_target_error) { 3415b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3416b952b558SPaolo Bonzini } 3417eee13dfeSPaolo Bonzini if (!has_granularity) { 3418eee13dfeSPaolo Bonzini granularity = 0; 3419eee13dfeSPaolo Bonzini } 342008e4ed6cSPaolo Bonzini if (!has_buf_size) { 342148ac0a4dSWen Congyang buf_size = 0; 342208e4ed6cSPaolo Bonzini } 34230fc9f8eaSFam Zheng if (!has_unmap) { 34240fc9f8eaSFam Zheng unmap = true; 34250fc9f8eaSFam Zheng } 342608e4ed6cSPaolo Bonzini 3427eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3428c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 34293cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3430eee13dfeSPaolo Bonzini return; 3431eee13dfeSPaolo Bonzini } 3432eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3433c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3434c6bd8c70SMarkus Armbruster "power of 2"); 3435eee13dfeSPaolo Bonzini return; 3436eee13dfeSPaolo Bonzini } 3437d9b902dbSPaolo Bonzini 34384193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 34394193cdd7SFam Zheng return; 34404193cdd7SFam Zheng } 3441e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3442e40e5027SFam Zheng return; 3443e40e5027SFam Zheng } 3444*1f0c461bSKevin Wolf if (bdrv_has_blk(target)) { 3445df92562eSFam Zheng error_setg(errp, "Cannot mirror to an attached block device"); 3446df92562eSFam Zheng return; 3447df92562eSFam Zheng } 34484193cdd7SFam Zheng 34494193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 34504193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 34514193cdd7SFam Zheng } 34524193cdd7SFam Zheng 34534193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 34544193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 34554193cdd7SFam Zheng */ 34564193cdd7SFam Zheng mirror_start(bs, target, 34574193cdd7SFam Zheng has_replaces ? replaces : NULL, 34584193cdd7SFam Zheng speed, granularity, buf_size, sync, 34594193cdd7SFam Zheng on_source_error, on_target_error, unmap, 34604193cdd7SFam Zheng block_job_cb, bs, errp); 34614193cdd7SFam Zheng } 34624193cdd7SFam Zheng 34634193cdd7SFam Zheng void qmp_drive_mirror(const char *device, const char *target, 34644193cdd7SFam Zheng bool has_format, const char *format, 34654193cdd7SFam Zheng bool has_node_name, const char *node_name, 34664193cdd7SFam Zheng bool has_replaces, const char *replaces, 34674193cdd7SFam Zheng enum MirrorSyncMode sync, 34684193cdd7SFam Zheng bool has_mode, enum NewImageMode mode, 34694193cdd7SFam Zheng bool has_speed, int64_t speed, 34704193cdd7SFam Zheng bool has_granularity, uint32_t granularity, 34714193cdd7SFam Zheng bool has_buf_size, int64_t buf_size, 34724193cdd7SFam Zheng bool has_on_source_error, BlockdevOnError on_source_error, 34734193cdd7SFam Zheng bool has_on_target_error, BlockdevOnError on_target_error, 34744193cdd7SFam Zheng bool has_unmap, bool unmap, 34754193cdd7SFam Zheng Error **errp) 34764193cdd7SFam Zheng { 34774193cdd7SFam Zheng BlockDriverState *bs; 34784193cdd7SFam Zheng BlockBackend *blk; 34794193cdd7SFam Zheng BlockDriverState *source, *target_bs; 34804193cdd7SFam Zheng AioContext *aio_context; 34814193cdd7SFam Zheng Error *local_err = NULL; 34824193cdd7SFam Zheng QDict *options = NULL; 34834193cdd7SFam Zheng int flags; 34844193cdd7SFam Zheng int64_t size; 34854193cdd7SFam Zheng int ret; 34864193cdd7SFam Zheng 3487a0e8544cSFam Zheng blk = blk_by_name(device); 3488a0e8544cSFam Zheng if (!blk) { 348975158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 349075158ebbSMarkus Armbruster "Device '%s' not found", device); 3491d9b902dbSPaolo Bonzini return; 3492d9b902dbSPaolo Bonzini } 3493d9b902dbSPaolo Bonzini 34945433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 34955a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 34965a7e7a0bSStefan Hajnoczi 34975433c24fSMax Reitz if (!blk_is_available(blk)) { 3498c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 34995a7e7a0bSStefan Hajnoczi goto out; 3500d9b902dbSPaolo Bonzini } 35015433c24fSMax Reitz bs = blk_bs(blk); 35024193cdd7SFam Zheng if (!has_mode) { 35034193cdd7SFam Zheng mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 35044193cdd7SFam Zheng } 3505d9b902dbSPaolo Bonzini 3506d9b902dbSPaolo Bonzini if (!has_format) { 3507d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3508d9b902dbSPaolo Bonzini } 3509d9b902dbSPaolo Bonzini 351061de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3511760e0063SKevin Wolf source = backing_bs(bs); 3512d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3513d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 3514d9b902dbSPaolo Bonzini } 3515117e0c82SMax Reitz if (sync == MIRROR_SYNC_MODE_NONE) { 3516117e0c82SMax Reitz source = bs; 3517117e0c82SMax Reitz } 3518d9b902dbSPaolo Bonzini 3519ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3520ac3c5d83SStefan Hajnoczi if (size < 0) { 3521ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 35225a7e7a0bSStefan Hajnoczi goto out; 3523ac3c5d83SStefan Hajnoczi } 3524ac3c5d83SStefan Hajnoczi 352509158f00SBenoît Canet if (has_replaces) { 352609158f00SBenoît Canet BlockDriverState *to_replace_bs; 35275a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 35285a7e7a0bSStefan Hajnoczi int64_t replace_size; 352909158f00SBenoît Canet 353009158f00SBenoît Canet if (!has_node_name) { 353109158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 353209158f00SBenoît Canet " named node of the graph"); 35335a7e7a0bSStefan Hajnoczi goto out; 353409158f00SBenoît Canet } 353509158f00SBenoît Canet 3536e12f3784SWen Congyang to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 353709158f00SBenoît Canet 353809158f00SBenoît Canet if (!to_replace_bs) { 353909158f00SBenoît Canet error_propagate(errp, local_err); 35405a7e7a0bSStefan Hajnoczi goto out; 354109158f00SBenoît Canet } 354209158f00SBenoît Canet 35435a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 35445a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 35455a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 35465a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 35475a7e7a0bSStefan Hajnoczi 35485a7e7a0bSStefan Hajnoczi if (size != replace_size) { 354909158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 355009158f00SBenoît Canet "different size"); 35515a7e7a0bSStefan Hajnoczi goto out; 355209158f00SBenoît Canet } 355309158f00SBenoît Canet } 355409158f00SBenoît Canet 355514526864SMax Reitz if ((sync == MIRROR_SYNC_MODE_FULL || !source) 355614526864SMax Reitz && mode != NEW_IMAGE_MODE_EXISTING) 355714526864SMax Reitz { 3558d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3559e6641719SMax Reitz assert(format); 3560cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3561f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3562d9b902dbSPaolo Bonzini } else { 3563d9b902dbSPaolo Bonzini switch (mode) { 3564d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3565d9b902dbSPaolo Bonzini break; 3566d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3567d9b902dbSPaolo Bonzini /* create new image with backing file */ 3568cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3569d9b902dbSPaolo Bonzini source->filename, 3570d9b902dbSPaolo Bonzini source->drv->format_name, 3571f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3572d9b902dbSPaolo Bonzini break; 3573d9b902dbSPaolo Bonzini default: 3574d9b902dbSPaolo Bonzini abort(); 3575d9b902dbSPaolo Bonzini } 3576d9b902dbSPaolo Bonzini } 3577d9b902dbSPaolo Bonzini 357884d18f06SMarkus Armbruster if (local_err) { 3579cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 35805a7e7a0bSStefan Hajnoczi goto out; 3581d9b902dbSPaolo Bonzini } 3582d9b902dbSPaolo Bonzini 35834c828dc6SBenoît Canet options = qdict_new(); 3584e6641719SMax Reitz if (has_node_name) { 35854c828dc6SBenoît Canet qdict_put(options, "node-name", qstring_from_str(node_name)); 35864c828dc6SBenoît Canet } 3587e6641719SMax Reitz if (format) { 3588e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3589e6641719SMax Reitz } 35904c828dc6SBenoît Canet 3591b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3592b812f671SPaolo Bonzini * file. 3593b812f671SPaolo Bonzini */ 3594f67503e5SMax Reitz target_bs = NULL; 35954c828dc6SBenoît Canet ret = bdrv_open(&target_bs, target, NULL, options, 35966ebf9aa2SMax Reitz flags | BDRV_O_NO_BACKING, &local_err); 3597d9b902dbSPaolo Bonzini if (ret < 0) { 359834b5d2c6SMax Reitz error_propagate(errp, local_err); 35995a7e7a0bSStefan Hajnoczi goto out; 3600d9b902dbSPaolo Bonzini } 3601d9b902dbSPaolo Bonzini 36025a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 36035a7e7a0bSStefan Hajnoczi 36044193cdd7SFam Zheng blockdev_mirror_common(bs, target_bs, 36054193cdd7SFam Zheng has_replaces, replaces, sync, 36064193cdd7SFam Zheng has_speed, speed, 36074193cdd7SFam Zheng has_granularity, granularity, 36084193cdd7SFam Zheng has_buf_size, buf_size, 36094193cdd7SFam Zheng has_on_source_error, on_source_error, 36104193cdd7SFam Zheng has_on_target_error, on_target_error, 36114193cdd7SFam Zheng has_unmap, unmap, 36124193cdd7SFam Zheng &local_err); 36134193cdd7SFam Zheng if (local_err) { 3614d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 36154193cdd7SFam Zheng bdrv_unref(target_bs); 3616d9b902dbSPaolo Bonzini } 36175a7e7a0bSStefan Hajnoczi out: 36185a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3619d9b902dbSPaolo Bonzini } 3620d9b902dbSPaolo Bonzini 3621df92562eSFam Zheng void qmp_blockdev_mirror(const char *device, const char *target, 3622df92562eSFam Zheng bool has_replaces, const char *replaces, 3623df92562eSFam Zheng MirrorSyncMode sync, 3624df92562eSFam Zheng bool has_speed, int64_t speed, 3625df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3626df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3627df92562eSFam Zheng bool has_on_source_error, 3628df92562eSFam Zheng BlockdevOnError on_source_error, 3629df92562eSFam Zheng bool has_on_target_error, 3630df92562eSFam Zheng BlockdevOnError on_target_error, 3631df92562eSFam Zheng Error **errp) 3632df92562eSFam Zheng { 3633df92562eSFam Zheng BlockDriverState *bs; 3634df92562eSFam Zheng BlockBackend *blk; 3635df92562eSFam Zheng BlockDriverState *target_bs; 3636df92562eSFam Zheng AioContext *aio_context; 3637df92562eSFam Zheng Error *local_err = NULL; 3638df92562eSFam Zheng 3639df92562eSFam Zheng blk = blk_by_name(device); 3640df92562eSFam Zheng if (!blk) { 3641df92562eSFam Zheng error_setg(errp, "Device '%s' not found", device); 3642df92562eSFam Zheng return; 3643df92562eSFam Zheng } 3644df92562eSFam Zheng bs = blk_bs(blk); 3645df92562eSFam Zheng 3646df92562eSFam Zheng if (!bs) { 3647df92562eSFam Zheng error_setg(errp, "Device '%s' has no media", device); 3648df92562eSFam Zheng return; 3649df92562eSFam Zheng } 3650df92562eSFam Zheng 3651df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3652df92562eSFam Zheng if (!target_bs) { 3653df92562eSFam Zheng return; 3654df92562eSFam Zheng } 3655df92562eSFam Zheng 3656df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3657df92562eSFam Zheng aio_context_acquire(aio_context); 3658df92562eSFam Zheng 3659df92562eSFam Zheng bdrv_ref(target_bs); 3660df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3661df92562eSFam Zheng 3662df92562eSFam Zheng blockdev_mirror_common(bs, target_bs, 3663df92562eSFam Zheng has_replaces, replaces, sync, 3664df92562eSFam Zheng has_speed, speed, 3665df92562eSFam Zheng has_granularity, granularity, 3666df92562eSFam Zheng has_buf_size, buf_size, 3667df92562eSFam Zheng has_on_source_error, on_source_error, 3668df92562eSFam Zheng has_on_target_error, on_target_error, 3669df92562eSFam Zheng true, true, 3670df92562eSFam Zheng &local_err); 3671df92562eSFam Zheng if (local_err) { 3672df92562eSFam Zheng error_propagate(errp, local_err); 3673df92562eSFam Zheng bdrv_unref(target_bs); 3674df92562eSFam Zheng } 3675df92562eSFam Zheng 3676df92562eSFam Zheng aio_context_release(aio_context); 3677df92562eSFam Zheng } 3678df92562eSFam Zheng 36793d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */ 368024d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context, 368124d6bffeSMarkus Armbruster Error **errp) 36822d47c6e9SStefan Hajnoczi { 3683a0e8544cSFam Zheng BlockBackend *blk; 36842d47c6e9SStefan Hajnoczi BlockDriverState *bs; 36852d47c6e9SStefan Hajnoczi 36865433c24fSMax Reitz *aio_context = NULL; 36875433c24fSMax Reitz 3688a0e8544cSFam Zheng blk = blk_by_name(device); 3689a0e8544cSFam Zheng if (!blk) { 36903d948cdfSStefan Hajnoczi goto notfound; 36912d47c6e9SStefan Hajnoczi } 36923d948cdfSStefan Hajnoczi 36935433c24fSMax Reitz *aio_context = blk_get_aio_context(blk); 36943d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 36953d948cdfSStefan Hajnoczi 36965433c24fSMax Reitz if (!blk_is_available(blk)) { 36975433c24fSMax Reitz goto notfound; 36985433c24fSMax Reitz } 36995433c24fSMax Reitz bs = blk_bs(blk); 37005433c24fSMax Reitz 37013d948cdfSStefan Hajnoczi if (!bs->job) { 37023d948cdfSStefan Hajnoczi goto notfound; 37033d948cdfSStefan Hajnoczi } 37043d948cdfSStefan Hajnoczi 37052d47c6e9SStefan Hajnoczi return bs->job; 37063d948cdfSStefan Hajnoczi 37073d948cdfSStefan Hajnoczi notfound: 37082e3a0266SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 37092e3a0266SMarkus Armbruster "No active block job on device '%s'", device); 37105433c24fSMax Reitz if (*aio_context) { 37115433c24fSMax Reitz aio_context_release(*aio_context); 37123d948cdfSStefan Hajnoczi *aio_context = NULL; 37135433c24fSMax Reitz } 37143d948cdfSStefan Hajnoczi return NULL; 37152d47c6e9SStefan Hajnoczi } 37162d47c6e9SStefan Hajnoczi 3717882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 37182d47c6e9SStefan Hajnoczi { 37193d948cdfSStefan Hajnoczi AioContext *aio_context; 372024d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37212d47c6e9SStefan Hajnoczi 37222d47c6e9SStefan Hajnoczi if (!job) { 37232d47c6e9SStefan Hajnoczi return; 37242d47c6e9SStefan Hajnoczi } 37252d47c6e9SStefan Hajnoczi 3726882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 37273d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37282d47c6e9SStefan Hajnoczi } 3729370521a1SStefan Hajnoczi 37306e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 37316e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 37326e37fb81SPaolo Bonzini { 37333d948cdfSStefan Hajnoczi AioContext *aio_context; 373424d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37356e37fb81SPaolo Bonzini 37366e37fb81SPaolo Bonzini if (!job) { 37376e37fb81SPaolo Bonzini return; 37386e37fb81SPaolo Bonzini } 37393d948cdfSStefan Hajnoczi 37403d948cdfSStefan Hajnoczi if (!has_force) { 37413d948cdfSStefan Hajnoczi force = false; 37423d948cdfSStefan Hajnoczi } 37433d948cdfSStefan Hajnoczi 3744751ebd76SFam Zheng if (job->user_paused && !force) { 3745f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3746f231b88dSCole Robinson device); 37473d948cdfSStefan Hajnoczi goto out; 37486e37fb81SPaolo Bonzini } 37496e37fb81SPaolo Bonzini 37506e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 37516e37fb81SPaolo Bonzini block_job_cancel(job); 37523d948cdfSStefan Hajnoczi out: 37533d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37546e37fb81SPaolo Bonzini } 37556e37fb81SPaolo Bonzini 37566e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3757370521a1SStefan Hajnoczi { 37583d948cdfSStefan Hajnoczi AioContext *aio_context; 375924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3760370521a1SStefan Hajnoczi 3761751ebd76SFam Zheng if (!job || job->user_paused) { 3762370521a1SStefan Hajnoczi return; 3763370521a1SStefan Hajnoczi } 37646e37fb81SPaolo Bonzini 3765751ebd76SFam Zheng job->user_paused = true; 37666e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 37676e37fb81SPaolo Bonzini block_job_pause(job); 37683d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37696e37fb81SPaolo Bonzini } 37706e37fb81SPaolo Bonzini 37716e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 37726e37fb81SPaolo Bonzini { 37733d948cdfSStefan Hajnoczi AioContext *aio_context; 377424d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37756e37fb81SPaolo Bonzini 3776751ebd76SFam Zheng if (!job || !job->user_paused) { 37778acc72a4SPaolo Bonzini return; 37788acc72a4SPaolo Bonzini } 3779370521a1SStefan Hajnoczi 3780751ebd76SFam Zheng job->user_paused = false; 37816e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 37826e37fb81SPaolo Bonzini block_job_resume(job); 37833d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3784370521a1SStefan Hajnoczi } 3785fb5458cdSStefan Hajnoczi 3786aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3787aeae883bSPaolo Bonzini { 37883d948cdfSStefan Hajnoczi AioContext *aio_context; 378924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3790aeae883bSPaolo Bonzini 3791aeae883bSPaolo Bonzini if (!job) { 3792aeae883bSPaolo Bonzini return; 3793aeae883bSPaolo Bonzini } 3794aeae883bSPaolo Bonzini 3795aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3796aeae883bSPaolo Bonzini block_job_complete(job, errp); 37973d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3798aeae883bSPaolo Bonzini } 3799aeae883bSPaolo Bonzini 3800fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3801fa40e656SJeff Cody const char *image_node_name, 3802fa40e656SJeff Cody const char *backing_file, 3803fa40e656SJeff Cody Error **errp) 3804fa40e656SJeff Cody { 3805a0e8544cSFam Zheng BlockBackend *blk; 3806fa40e656SJeff Cody BlockDriverState *bs = NULL; 3807729962f6SStefan Hajnoczi AioContext *aio_context; 3808fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3809fa40e656SJeff Cody Error *local_err = NULL; 3810fa40e656SJeff Cody bool ro; 3811fa40e656SJeff Cody int open_flags; 3812fa40e656SJeff Cody int ret; 3813fa40e656SJeff Cody 3814a0e8544cSFam Zheng blk = blk_by_name(device); 3815a0e8544cSFam Zheng if (!blk) { 381675158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 381775158ebbSMarkus Armbruster "Device '%s' not found", device); 3818fa40e656SJeff Cody return; 3819fa40e656SJeff Cody } 3820fa40e656SJeff Cody 38215433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3822729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3823729962f6SStefan Hajnoczi 38245433c24fSMax Reitz if (!blk_is_available(blk)) { 38255433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 38265433c24fSMax Reitz goto out; 38275433c24fSMax Reitz } 38285433c24fSMax Reitz bs = blk_bs(blk); 38295433c24fSMax Reitz 3830fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3831fa40e656SJeff Cody if (local_err) { 3832fa40e656SJeff Cody error_propagate(errp, local_err); 3833729962f6SStefan Hajnoczi goto out; 3834fa40e656SJeff Cody } 3835fa40e656SJeff Cody 3836fa40e656SJeff Cody if (!image_bs) { 3837fa40e656SJeff Cody error_setg(errp, "image file not found"); 3838729962f6SStefan Hajnoczi goto out; 3839fa40e656SJeff Cody } 3840fa40e656SJeff Cody 3841fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3842fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3843fa40e656SJeff Cody "without a backing file"); 3844729962f6SStefan Hajnoczi goto out; 3845fa40e656SJeff Cody } 3846fa40e656SJeff Cody 3847fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3848fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3849fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3850729962f6SStefan Hajnoczi goto out; 3851fa40e656SJeff Cody } 3852fa40e656SJeff Cody 3853fa40e656SJeff Cody /* final sanity check */ 3854fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3855fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3856fa40e656SJeff Cody device); 3857729962f6SStefan Hajnoczi goto out; 3858fa40e656SJeff Cody } 3859fa40e656SJeff Cody 3860fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3861fa40e656SJeff Cody open_flags = image_bs->open_flags; 3862fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3863fa40e656SJeff Cody 3864fa40e656SJeff Cody if (ro) { 3865fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3866fa40e656SJeff Cody if (local_err) { 3867fa40e656SJeff Cody error_propagate(errp, local_err); 3868729962f6SStefan Hajnoczi goto out; 3869fa40e656SJeff Cody } 3870fa40e656SJeff Cody } 3871fa40e656SJeff Cody 3872fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3873fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3874fa40e656SJeff Cody 3875fa40e656SJeff Cody if (ret < 0) { 3876fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3877fa40e656SJeff Cody backing_file); 3878fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3879fa40e656SJeff Cody * appropriate */ 3880fa40e656SJeff Cody } 3881fa40e656SJeff Cody 3882fa40e656SJeff Cody if (ro) { 3883fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3884fa40e656SJeff Cody if (local_err) { 3885fa40e656SJeff Cody error_propagate(errp, local_err); /* will preserve prior errp */ 3886fa40e656SJeff Cody } 3887fa40e656SJeff Cody } 3888729962f6SStefan Hajnoczi 3889729962f6SStefan Hajnoczi out: 3890729962f6SStefan Hajnoczi aio_context_release(aio_context); 3891fa40e656SJeff Cody } 3892fa40e656SJeff Cody 3893abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 3894abb21ac3SKevin Wolf { 3895abb21ac3SKevin Wolf QemuOpts *opts; 3896abb21ac3SKevin Wolf QDict *qdict; 3897abb21ac3SKevin Wolf Error *local_err = NULL; 3898abb21ac3SKevin Wolf 3899abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 3900abb21ac3SKevin Wolf if (!opts) { 3901abb21ac3SKevin Wolf return; 3902abb21ac3SKevin Wolf } 3903abb21ac3SKevin Wolf 3904abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 3905abb21ac3SKevin Wolf 3906abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 3907f8746fb8SKevin Wolf QDECREF(qdict); 3908abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 3909abb21ac3SKevin Wolf goto out; 3910abb21ac3SKevin Wolf } 3911abb21ac3SKevin Wolf 3912abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 3913abb21ac3SKevin Wolf if (!bs) { 3914abb21ac3SKevin Wolf error_report_err(local_err); 3915abb21ac3SKevin Wolf goto out; 3916abb21ac3SKevin Wolf } 3917abb21ac3SKevin Wolf 3918abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3919abb21ac3SKevin Wolf 3920abb21ac3SKevin Wolf out: 3921abb21ac3SKevin Wolf qemu_opts_del(opts); 3922abb21ac3SKevin Wolf } 3923abb21ac3SKevin Wolf 3924d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3925d26c9a15SKevin Wolf { 3926d26c9a15SKevin Wolf QmpOutputVisitor *ov = qmp_output_visitor_new(); 3927be4b67bcSMax Reitz BlockDriverState *bs; 3928be4b67bcSMax Reitz BlockBackend *blk = NULL; 3929d26c9a15SKevin Wolf QObject *obj; 3930d26c9a15SKevin Wolf QDict *qdict; 3931d26c9a15SKevin Wolf Error *local_err = NULL; 3932d26c9a15SKevin Wolf 393360e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3934d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 393560e19e06SMarkus Armbruster * when called from drive_new(). 3936d26c9a15SKevin Wolf * 3937d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3938d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3939c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3940c6e0bd9bSKevin Wolf options->cache->has_direct && 3941c6e0bd9bSKevin Wolf options->cache->direct; 3942c6e0bd9bSKevin Wolf if (!direct) { 3943d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3944d26c9a15SKevin Wolf goto fail; 3945d26c9a15SKevin Wolf } 3946d26c9a15SKevin Wolf } 3947d26c9a15SKevin Wolf 394851e72bc1SEric Blake visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options, 394951e72bc1SEric Blake &local_err); 395084d18f06SMarkus Armbruster if (local_err) { 3951d26c9a15SKevin Wolf error_propagate(errp, local_err); 3952d26c9a15SKevin Wolf goto fail; 3953d26c9a15SKevin Wolf } 3954d26c9a15SKevin Wolf 3955d26c9a15SKevin Wolf obj = qmp_output_get_qobject(ov); 3956d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3957d26c9a15SKevin Wolf 3958d26c9a15SKevin Wolf qdict_flatten(qdict); 3959d26c9a15SKevin Wolf 3960be4b67bcSMax Reitz if (options->has_id) { 396118e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 396284d18f06SMarkus Armbruster if (local_err) { 3963b681072dSKevin Wolf error_propagate(errp, local_err); 3964d26c9a15SKevin Wolf goto fail; 3965d26c9a15SKevin Wolf } 3966d26c9a15SKevin Wolf 3967be4b67bcSMax Reitz bs = blk_bs(blk); 3968be4b67bcSMax Reitz } else { 3969be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3970be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3971be4b67bcSMax Reitz "the root node"); 3972be4b67bcSMax Reitz goto fail; 3973be4b67bcSMax Reitz } 3974be4b67bcSMax Reitz 3975bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3976bd745e23SMax Reitz if (!bs) { 3977be4b67bcSMax Reitz goto fail; 3978be4b67bcSMax Reitz } 39799c4218e9SMax Reitz 39809c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3981be4b67bcSMax Reitz } 3982be4b67bcSMax Reitz 3983be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 3984be4b67bcSMax Reitz if (blk) { 3985efaa7c4eSMax Reitz monitor_remove_blk(blk); 398618e46a03SMarkus Armbruster blk_unref(blk); 3987be4b67bcSMax Reitz } else { 39889c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 3989be4b67bcSMax Reitz bdrv_unref(bs); 3990be4b67bcSMax Reitz } 39918ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 39928ae8e904SKevin Wolf goto fail; 39938ae8e904SKevin Wolf } 39948ae8e904SKevin Wolf 3995d26c9a15SKevin Wolf fail: 3996d26c9a15SKevin Wolf qmp_output_visitor_cleanup(ov); 3997d26c9a15SKevin Wolf } 3998d26c9a15SKevin Wolf 399981b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id, 400081b936aeSAlberto Garcia bool has_node_name, const char *node_name, Error **errp) 400181b936aeSAlberto Garcia { 400281b936aeSAlberto Garcia AioContext *aio_context; 400381b936aeSAlberto Garcia BlockBackend *blk; 400481b936aeSAlberto Garcia BlockDriverState *bs; 400581b936aeSAlberto Garcia 400681b936aeSAlberto Garcia if (has_id && has_node_name) { 400781b936aeSAlberto Garcia error_setg(errp, "Only one of id and node-name must be specified"); 400881b936aeSAlberto Garcia return; 400981b936aeSAlberto Garcia } else if (!has_id && !has_node_name) { 401081b936aeSAlberto Garcia error_setg(errp, "No block device specified"); 401181b936aeSAlberto Garcia return; 401281b936aeSAlberto Garcia } 401381b936aeSAlberto Garcia 401481b936aeSAlberto Garcia if (has_id) { 4015efaa7c4eSMax Reitz /* blk_by_name() never returns a BB that is not owned by the monitor */ 401681b936aeSAlberto Garcia blk = blk_by_name(id); 401781b936aeSAlberto Garcia if (!blk) { 401881b936aeSAlberto Garcia error_setg(errp, "Cannot find block backend %s", id); 401981b936aeSAlberto Garcia return; 402081b936aeSAlberto Garcia } 40215cf87fd6SPaolo Bonzini if (blk_legacy_dinfo(blk)) { 40225cf87fd6SPaolo Bonzini error_setg(errp, "Deleting block backend added with drive-add" 40235cf87fd6SPaolo Bonzini " is not supported"); 40245cf87fd6SPaolo Bonzini return; 40255cf87fd6SPaolo Bonzini } 402681b936aeSAlberto Garcia if (blk_get_refcnt(blk) > 1) { 402781b936aeSAlberto Garcia error_setg(errp, "Block backend %s is in use", id); 402881b936aeSAlberto Garcia return; 402981b936aeSAlberto Garcia } 403081b936aeSAlberto Garcia bs = blk_bs(blk); 403181b936aeSAlberto Garcia aio_context = blk_get_aio_context(blk); 403281b936aeSAlberto Garcia } else { 4033*1f0c461bSKevin Wolf blk = NULL; 403481b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 403581b936aeSAlberto Garcia if (!bs) { 403681b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 403781b936aeSAlberto Garcia return; 403881b936aeSAlberto Garcia } 4039*1f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 404081b936aeSAlberto Garcia error_setg(errp, "Node %s is in use by %s", 4041*1f0c461bSKevin Wolf node_name, bdrv_get_parent_name(bs)); 404281b936aeSAlberto Garcia return; 404381b936aeSAlberto Garcia } 404481b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 404581b936aeSAlberto Garcia } 404681b936aeSAlberto Garcia 404781b936aeSAlberto Garcia aio_context_acquire(aio_context); 404881b936aeSAlberto Garcia 404981b936aeSAlberto Garcia if (bs) { 405081b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 405181b936aeSAlberto Garcia goto out; 405281b936aeSAlberto Garcia } 405381b936aeSAlberto Garcia 40549c4218e9SMax Reitz if (!blk && !bs->monitor_list.tqe_prev) { 40559c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 40569c4218e9SMax Reitz bs->node_name); 40579c4218e9SMax Reitz goto out; 40589c4218e9SMax Reitz } 40599c4218e9SMax Reitz 40609c4218e9SMax Reitz if (bs->refcnt > 1) { 406181b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 406281b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 406381b936aeSAlberto Garcia goto out; 406481b936aeSAlberto Garcia } 406581b936aeSAlberto Garcia } 406681b936aeSAlberto Garcia 406781b936aeSAlberto Garcia if (blk) { 4068efaa7c4eSMax Reitz monitor_remove_blk(blk); 406981b936aeSAlberto Garcia blk_unref(blk); 407081b936aeSAlberto Garcia } else { 40719c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 407281b936aeSAlberto Garcia bdrv_unref(bs); 407381b936aeSAlberto Garcia } 407481b936aeSAlberto Garcia 407581b936aeSAlberto Garcia out: 407681b936aeSAlberto Garcia aio_context_release(aio_context); 407781b936aeSAlberto Garcia } 407881b936aeSAlberto Garcia 40797f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 40807f821597SWen Congyang const char *child_name) 40817f821597SWen Congyang { 40827f821597SWen Congyang BdrvChild *child; 40837f821597SWen Congyang 40847f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 40857f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 40867f821597SWen Congyang return child; 40877f821597SWen Congyang } 40887f821597SWen Congyang } 40897f821597SWen Congyang 40907f821597SWen Congyang return NULL; 40917f821597SWen Congyang } 40927f821597SWen Congyang 40937f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 40947f821597SWen Congyang const char *child, bool has_node, 40957f821597SWen Congyang const char *node, Error **errp) 40967f821597SWen Congyang { 40977f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 40987f821597SWen Congyang BdrvChild *p_child; 40997f821597SWen Congyang 41007f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 41017f821597SWen Congyang if (!parent_bs) { 41027f821597SWen Congyang return; 41037f821597SWen Congyang } 41047f821597SWen Congyang 41057f821597SWen Congyang if (has_child == has_node) { 41067f821597SWen Congyang if (has_child) { 41077f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 41087f821597SWen Congyang } else { 41097f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 41107f821597SWen Congyang } 41117f821597SWen Congyang return; 41127f821597SWen Congyang } 41137f821597SWen Congyang 41147f821597SWen Congyang if (has_child) { 41157f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 41167f821597SWen Congyang if (!p_child) { 41177f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 41187f821597SWen Congyang parent, child); 41197f821597SWen Congyang return; 41207f821597SWen Congyang } 41217f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 41227f821597SWen Congyang } 41237f821597SWen Congyang 41247f821597SWen Congyang if (has_node) { 41257f821597SWen Congyang new_bs = bdrv_find_node(node); 41267f821597SWen Congyang if (!new_bs) { 41277f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 41287f821597SWen Congyang return; 41297f821597SWen Congyang } 41307f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 41317f821597SWen Congyang } 41327f821597SWen Congyang } 41337f821597SWen Congyang 4134fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4135fb5458cdSStefan Hajnoczi { 4136fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4137fea68bb6SMarkus Armbruster BlockDriverState *bs; 41387c8eece4SKevin Wolf BdrvNextIterator *it = NULL; 4139fea68bb6SMarkus Armbruster 41407c8eece4SKevin Wolf while ((it = bdrv_next(it, &bs))) { 414169691e72SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 414269691e72SStefan Hajnoczi 414369691e72SStefan Hajnoczi aio_context_acquire(aio_context); 414469691e72SStefan Hajnoczi 4145fea68bb6SMarkus Armbruster if (bs->job) { 4146fea68bb6SMarkus Armbruster BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 4147fea68bb6SMarkus Armbruster elem->value = block_job_query(bs->job); 4148fea68bb6SMarkus Armbruster *p_next = elem; 4149fea68bb6SMarkus Armbruster p_next = &elem->next; 4150fea68bb6SMarkus Armbruster } 415169691e72SStefan Hajnoczi 415269691e72SStefan Hajnoczi aio_context_release(aio_context); 4153fea68bb6SMarkus Armbruster } 4154fea68bb6SMarkus Armbruster 4155fea68bb6SMarkus Armbruster return head; 4156fb5458cdSStefan Hajnoczi } 41574d454574SPaolo Bonzini 41580006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 41594d454574SPaolo Bonzini .name = "drive", 41600006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 41614d454574SPaolo Bonzini .desc = { 41624d454574SPaolo Bonzini { 41634d454574SPaolo Bonzini .name = "snapshot", 41644d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 41654d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 41664d454574SPaolo Bonzini },{ 4167a9384affSPaolo Bonzini .name = "discard", 4168a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 4169a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 4170a9384affSPaolo Bonzini },{ 41714d454574SPaolo Bonzini .name = "aio", 41724d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41734d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 41744d454574SPaolo Bonzini },{ 4175e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4176e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4177e4b24b49SKevin Wolf .help = "Enable writeback mode", 4178e4b24b49SKevin Wolf },{ 41794d454574SPaolo Bonzini .name = "format", 41804d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41814d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 41824d454574SPaolo Bonzini },{ 41834d454574SPaolo Bonzini .name = "rerror", 41844d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41854d454574SPaolo Bonzini .help = "read error action", 41864d454574SPaolo Bonzini },{ 41874d454574SPaolo Bonzini .name = "werror", 41884d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41894d454574SPaolo Bonzini .help = "write error action", 41904d454574SPaolo Bonzini },{ 41910f227a94SKevin Wolf .name = "read-only", 41924d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 41934d454574SPaolo Bonzini .help = "open drive file as read-only", 41944d454574SPaolo Bonzini },{ 419557975222SKevin Wolf .name = "throttling.iops-total", 41964d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41974d454574SPaolo Bonzini .help = "limit total I/O operations per second", 41984d454574SPaolo Bonzini },{ 419957975222SKevin Wolf .name = "throttling.iops-read", 42004d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42014d454574SPaolo Bonzini .help = "limit read operations per second", 42024d454574SPaolo Bonzini },{ 420357975222SKevin Wolf .name = "throttling.iops-write", 42044d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42054d454574SPaolo Bonzini .help = "limit write operations per second", 42064d454574SPaolo Bonzini },{ 420757975222SKevin Wolf .name = "throttling.bps-total", 42084d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42094d454574SPaolo Bonzini .help = "limit total bytes per second", 42104d454574SPaolo Bonzini },{ 421157975222SKevin Wolf .name = "throttling.bps-read", 42124d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42134d454574SPaolo Bonzini .help = "limit read bytes per second", 42144d454574SPaolo Bonzini },{ 421557975222SKevin Wolf .name = "throttling.bps-write", 42164d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42174d454574SPaolo Bonzini .help = "limit write bytes per second", 42184d454574SPaolo Bonzini },{ 42193e9fab69SBenoît Canet .name = "throttling.iops-total-max", 42203e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42213e9fab69SBenoît Canet .help = "I/O operations burst", 42223e9fab69SBenoît Canet },{ 42233e9fab69SBenoît Canet .name = "throttling.iops-read-max", 42243e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42253e9fab69SBenoît Canet .help = "I/O operations read burst", 42263e9fab69SBenoît Canet },{ 42273e9fab69SBenoît Canet .name = "throttling.iops-write-max", 42283e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42293e9fab69SBenoît Canet .help = "I/O operations write burst", 42303e9fab69SBenoît Canet },{ 42313e9fab69SBenoît Canet .name = "throttling.bps-total-max", 42323e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42333e9fab69SBenoît Canet .help = "total bytes burst", 42343e9fab69SBenoît Canet },{ 42353e9fab69SBenoît Canet .name = "throttling.bps-read-max", 42363e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42373e9fab69SBenoît Canet .help = "total bytes read burst", 42383e9fab69SBenoît Canet },{ 42393e9fab69SBenoît Canet .name = "throttling.bps-write-max", 42403e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42413e9fab69SBenoît Canet .help = "total bytes write burst", 42423e9fab69SBenoît Canet },{ 42438a0fc18dSAlberto Garcia .name = "throttling.iops-total-max-length", 42448a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42458a0fc18dSAlberto Garcia .help = "length of the iops-total-max burst period, in seconds", 42468a0fc18dSAlberto Garcia },{ 42478a0fc18dSAlberto Garcia .name = "throttling.iops-read-max-length", 42488a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42498a0fc18dSAlberto Garcia .help = "length of the iops-read-max burst period, in seconds", 42508a0fc18dSAlberto Garcia },{ 42518a0fc18dSAlberto Garcia .name = "throttling.iops-write-max-length", 42528a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42538a0fc18dSAlberto Garcia .help = "length of the iops-write-max burst period, in seconds", 42548a0fc18dSAlberto Garcia },{ 42558a0fc18dSAlberto Garcia .name = "throttling.bps-total-max-length", 42568a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42578a0fc18dSAlberto Garcia .help = "length of the bps-total-max burst period, in seconds", 42588a0fc18dSAlberto Garcia },{ 42598a0fc18dSAlberto Garcia .name = "throttling.bps-read-max-length", 42608a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42618a0fc18dSAlberto Garcia .help = "length of the bps-read-max burst period, in seconds", 42628a0fc18dSAlberto Garcia },{ 42638a0fc18dSAlberto Garcia .name = "throttling.bps-write-max-length", 42648a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42658a0fc18dSAlberto Garcia .help = "length of the bps-write-max burst period, in seconds", 42668a0fc18dSAlberto Garcia },{ 42672024c1dfSBenoît Canet .name = "throttling.iops-size", 42682024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 42692024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 42702024c1dfSBenoît Canet },{ 427176f4afb4SAlberto Garcia .name = "throttling.group", 427276f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 427376f4afb4SAlberto Garcia .help = "name of the block throttling group", 427476f4afb4SAlberto Garcia },{ 42754d454574SPaolo Bonzini .name = "copy-on-read", 42764d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 42774d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4278465bee1dSPeter Lieven },{ 4279465bee1dSPeter Lieven .name = "detect-zeroes", 4280465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4281465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4282362e9299SAlberto Garcia },{ 4283362e9299SAlberto Garcia .name = "stats-account-invalid", 4284362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4285362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4286362e9299SAlberto Garcia "in the statistics", 4287362e9299SAlberto Garcia },{ 4288362e9299SAlberto Garcia .name = "stats-account-failed", 4289362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4290362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4291362e9299SAlberto Garcia "in the statistics", 42924d454574SPaolo Bonzini }, 42934d454574SPaolo Bonzini { /* end of list */ } 42944d454574SPaolo Bonzini }, 42954d454574SPaolo Bonzini }; 42960006383eSKevin Wolf 4297bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 4298bd745e23SMax Reitz .name = "root-bds", 429923f7fcb2SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head), 4300bd745e23SMax Reitz .desc = { 4301bd745e23SMax Reitz { 4302bd745e23SMax Reitz .name = "discard", 4303bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4304bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 4305bd745e23SMax Reitz },{ 4306bd745e23SMax Reitz .name = "aio", 4307bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4308bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 4309bd745e23SMax Reitz },{ 4310bd745e23SMax Reitz .name = "read-only", 4311bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4312bd745e23SMax Reitz .help = "open drive file as read-only", 4313bd745e23SMax Reitz },{ 4314bd745e23SMax Reitz .name = "copy-on-read", 4315bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4316bd745e23SMax Reitz .help = "copy read data from backing file into image file", 4317bd745e23SMax Reitz },{ 4318bd745e23SMax Reitz .name = "detect-zeroes", 4319bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4320bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 4321bd745e23SMax Reitz }, 4322bd745e23SMax Reitz { /* end of list */ } 4323bd745e23SMax Reitz }, 4324bd745e23SMax Reitz }; 4325bd745e23SMax Reitz 43260006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 43270006383eSKevin Wolf .name = "drive", 43280006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 43290006383eSKevin Wolf .desc = { 4330492fdc6fSKevin Wolf /* 4331492fdc6fSKevin Wolf * no elements => accept any params 4332492fdc6fSKevin Wolf * validation will happen later 4333492fdc6fSKevin Wolf */ 43340006383eSKevin Wolf { /* end of list */ } 43350006383eSKevin Wolf }, 43360006383eSKevin Wolf }; 4337