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 * 7627d6bf40SMarkus Armbruster * All controllers used to imlement 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; 47226f54e9aSMarkus Armbruster BlockBackend *blk; 473a0f1eab1SMarkus Armbruster BlockDriverState *bs; 474cc0681c4SBenoît Canet ThrottleConfig cfg; 475666daa68SMarkus Armbruster int snapshot = 0; 476c546194fSStefan Hajnoczi Error *error = NULL; 4770006383eSKevin Wolf QemuOpts *opts; 47840119effSAlberto Garcia QDict *interval_dict = NULL; 47940119effSAlberto Garcia QList *interval_list = NULL; 4800006383eSKevin Wolf const char *id; 481fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 482fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 483fbf8175eSMax Reitz const char *throttling_group = NULL; 484666daa68SMarkus Armbruster 485f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 486f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 487f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4880006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 48984d18f06SMarkus Armbruster if (error) { 490b681072dSKevin Wolf error_propagate(errp, error); 4916376f952SMarkus Armbruster goto err_no_opts; 4920006383eSKevin Wolf } 4930006383eSKevin Wolf 4940006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49584d18f06SMarkus Armbruster if (error) { 496b681072dSKevin Wolf error_propagate(errp, error); 497ec9c10d2SStefan Hajnoczi goto early_err; 4980006383eSKevin Wolf } 4990006383eSKevin Wolf 5000006383eSKevin Wolf if (id) { 5010006383eSKevin Wolf qdict_del(bs_opts, "id"); 5020006383eSKevin Wolf } 5030006383eSKevin Wolf 504666daa68SMarkus Armbruster /* extract parameters */ 505666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 506666daa68SMarkus Armbruster 507362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 508362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 509362e9299SAlberto Garcia 51040119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51140119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51240119effSAlberto Garcia 51340119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 51440119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 51540119effSAlberto Garcia qdict_first(interval_dict)->key); 51640119effSAlberto Garcia goto early_err; 51740119effSAlberto Garcia } 5182be5506fSAlberto Garcia 519fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 520fbf8175eSMax Reitz &detect_zeroes, &error); 521fbf8175eSMax Reitz if (error) { 522fbf8175eSMax Reitz error_propagate(errp, error); 523ec9c10d2SStefan Hajnoczi goto early_err; 524a9384affSPaolo Bonzini } 525666daa68SMarkus Armbruster 526666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 527c8057f95SPeter Maydell if (is_help_option(buf)) { 528807105a7SMarkus Armbruster error_printf("Supported formats:"); 529666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 530807105a7SMarkus Armbruster error_printf("\n"); 531ec9c10d2SStefan Hajnoczi goto early_err; 532666daa68SMarkus Armbruster } 53374fe54f2SKevin Wolf 534e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 535e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 536ec9c10d2SStefan Hajnoczi goto early_err; 5376db5f5d6SMike Qiu } 538e4342ce5SMax Reitz qdict_put(bs_opts, "driver", qstring_from_str(buf)); 539666daa68SMarkus Armbruster } 540666daa68SMarkus Armbruster 54192aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 542666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 543b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 54484d18f06SMarkus Armbruster if (error) { 545b681072dSKevin Wolf error_propagate(errp, error); 546ec9c10d2SStefan Hajnoczi goto early_err; 547666daa68SMarkus Armbruster } 548666daa68SMarkus Armbruster } 549666daa68SMarkus Armbruster 55092aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 551666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 552b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 55384d18f06SMarkus Armbruster if (error) { 554b681072dSKevin Wolf error_propagate(errp, error); 555ec9c10d2SStefan Hajnoczi goto early_err; 556666daa68SMarkus Armbruster } 557666daa68SMarkus Armbruster } 558666daa68SMarkus Armbruster 559666daa68SMarkus Armbruster if (snapshot) { 56091a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 561666daa68SMarkus Armbruster } 562666daa68SMarkus Armbruster 5635ec18f8cSMax Reitz /* init */ 56439c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5655ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5665ec18f8cSMax Reitz 567efaa7c4eSMax Reitz blk = blk_new(errp); 5685ec18f8cSMax Reitz if (!blk) { 5695ec18f8cSMax Reitz goto early_err; 5705ec18f8cSMax Reitz } 5715ec18f8cSMax Reitz 5725ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5735ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 574fbf8175eSMax Reitz blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); 5755ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5765ec18f8cSMax Reitz 5775ec18f8cSMax Reitz if (throttle_enabled(&cfg)) { 5785ec18f8cSMax Reitz if (!throttling_group) { 5795ec18f8cSMax Reitz throttling_group = blk_name(blk); 5805ec18f8cSMax Reitz } 5815ec18f8cSMax Reitz blk_rs->throttle_group = g_strdup(throttling_group); 5825ec18f8cSMax Reitz blk_rs->throttle_state = throttle_group_incref(throttling_group); 5835ec18f8cSMax Reitz blk_rs->throttle_state->cfg = cfg; 5845ec18f8cSMax Reitz } 5855ec18f8cSMax Reitz 5865ec18f8cSMax Reitz QDECREF(bs_opts); 5875ec18f8cSMax Reitz } else { 5885ec18f8cSMax Reitz if (file && !*file) { 5895ec18f8cSMax Reitz file = NULL; 5905ec18f8cSMax Reitz } 5915ec18f8cSMax Reitz 59291a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 59391a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 59491a097e7SKevin Wolf * Apply the defaults here instead. */ 59591a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_WB, "on"); 59691a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 59791a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 59891a097e7SKevin Wolf 59912d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 60012d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 60112d5ee3aSKevin Wolf } 60212d5ee3aSKevin Wolf 603efaa7c4eSMax Reitz blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); 604e4342ce5SMax Reitz if (!blk) { 605e4342ce5SMax Reitz goto err_no_bs_opts; 606e4342ce5SMax Reitz } 607e4342ce5SMax Reitz bs = blk_bs(blk); 6080006383eSKevin Wolf 609e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 610e4342ce5SMax Reitz 611e4342ce5SMax Reitz /* disk I/O throttling */ 612e4342ce5SMax Reitz if (throttle_enabled(&cfg)) { 61376f4afb4SAlberto Garcia if (!throttling_group) { 61476f4afb4SAlberto Garcia throttling_group = blk_name(blk); 61576f4afb4SAlberto Garcia } 61676f4afb4SAlberto Garcia bdrv_io_limits_enable(bs, throttling_group); 617e4342ce5SMax Reitz bdrv_set_io_limits(bs, &cfg); 618666daa68SMarkus Armbruster } 619666daa68SMarkus Armbruster 620a0f1eab1SMarkus Armbruster if (bdrv_key_required(bs)) { 621666daa68SMarkus Armbruster autostart = 0; 622a0f1eab1SMarkus Armbruster } 623362e9299SAlberto Garcia 624362e9299SAlberto Garcia block_acct_init(blk_get_stats(blk), account_invalid, account_failed); 6252be5506fSAlberto Garcia 62640119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6272be5506fSAlberto Garcia blk_unref(blk); 6282be5506fSAlberto Garcia blk = NULL; 6292be5506fSAlberto Garcia goto err_no_bs_opts; 6302be5506fSAlberto Garcia } 6312be5506fSAlberto Garcia } 6325ec18f8cSMax Reitz 6335ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6340006383eSKevin Wolf 635efaa7c4eSMax Reitz if (!monitor_add_blk(blk, qemu_opts_id(opts), errp)) { 636efaa7c4eSMax Reitz blk_unref(blk); 637efaa7c4eSMax Reitz blk = NULL; 638efaa7c4eSMax Reitz goto err_no_bs_opts; 639efaa7c4eSMax Reitz } 640efaa7c4eSMax Reitz 641e4342ce5SMax Reitz err_no_bs_opts: 6420006383eSKevin Wolf qemu_opts_del(opts); 64340119effSAlberto Garcia QDECREF(interval_dict); 64440119effSAlberto Garcia QDECREF(interval_list); 64518e46a03SMarkus Armbruster return blk; 646a9ae2bffSMarkus Armbruster 647ec9c10d2SStefan Hajnoczi early_err: 648ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 64940119effSAlberto Garcia QDECREF(interval_dict); 65040119effSAlberto Garcia QDECREF(interval_list); 6516376f952SMarkus Armbruster err_no_opts: 6526376f952SMarkus Armbruster QDECREF(bs_opts); 653a9ae2bffSMarkus Armbruster return NULL; 654666daa68SMarkus Armbruster } 655666daa68SMarkus Armbruster 656bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts; 657bd745e23SMax Reitz 658bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 659bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 660bd745e23SMax Reitz { 661bd745e23SMax Reitz BlockDriverState *bs; 662bd745e23SMax Reitz QemuOpts *opts; 663bd745e23SMax Reitz Error *local_error = NULL; 664bd745e23SMax Reitz BlockdevDetectZeroesOptions detect_zeroes; 665bd745e23SMax Reitz int ret; 666bd745e23SMax Reitz int bdrv_flags = 0; 667bd745e23SMax Reitz 668bd745e23SMax Reitz opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); 669bd745e23SMax Reitz if (!opts) { 670bd745e23SMax Reitz goto fail; 671bd745e23SMax Reitz } 672bd745e23SMax Reitz 673bd745e23SMax Reitz qemu_opts_absorb_qdict(opts, bs_opts, &local_error); 674bd745e23SMax Reitz if (local_error) { 675bd745e23SMax Reitz error_propagate(errp, local_error); 676bd745e23SMax Reitz goto fail; 677bd745e23SMax Reitz } 678bd745e23SMax Reitz 679bd745e23SMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, 680bd745e23SMax Reitz &detect_zeroes, &local_error); 681bd745e23SMax Reitz if (local_error) { 682bd745e23SMax Reitz error_propagate(errp, local_error); 683bd745e23SMax Reitz goto fail; 684bd745e23SMax Reitz } 685bd745e23SMax Reitz 686a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 687a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults. 688a81d6164SKevin Wolf * Apply the defaults here instead. */ 689a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_WB, "on"); 690a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 691a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 692a81d6164SKevin Wolf 69312d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 69412d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 69512d5ee3aSKevin Wolf } 69612d5ee3aSKevin Wolf 697bd745e23SMax Reitz bs = NULL; 698bd745e23SMax Reitz ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp); 699bd745e23SMax Reitz if (ret < 0) { 700bd745e23SMax Reitz goto fail_no_bs_opts; 701bd745e23SMax Reitz } 702bd745e23SMax Reitz 703bd745e23SMax Reitz bs->detect_zeroes = detect_zeroes; 704bd745e23SMax Reitz 705bd745e23SMax Reitz fail_no_bs_opts: 706bd745e23SMax Reitz qemu_opts_del(opts); 707bd745e23SMax Reitz return bs; 708bd745e23SMax Reitz 709bd745e23SMax Reitz fail: 710bd745e23SMax Reitz qemu_opts_del(opts); 711bd745e23SMax Reitz QDECREF(bs_opts); 712bd745e23SMax Reitz return NULL; 713bd745e23SMax Reitz } 714bd745e23SMax Reitz 7159c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void) 7169c4218e9SMax Reitz { 7179c4218e9SMax Reitz BlockDriverState *bs, *next_bs; 7189c4218e9SMax Reitz 7199c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { 7209c4218e9SMax Reitz AioContext *ctx = bdrv_get_aio_context(bs); 7219c4218e9SMax Reitz 7229c4218e9SMax Reitz aio_context_acquire(ctx); 7239c4218e9SMax Reitz bdrv_unref(bs); 7249c4218e9SMax Reitz aio_context_release(ctx); 7259c4218e9SMax Reitz } 7269c4218e9SMax Reitz } 7279c4218e9SMax Reitz 728262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */ 729262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) 730262b4e8fSMax Reitz { 731262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list) 732262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states); 733262b4e8fSMax Reitz } 734262b4e8fSMax Reitz 7355abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 7365abbf0eeSKevin Wolf Error **errp) 73757975222SKevin Wolf { 73857975222SKevin Wolf const char *value; 73957975222SKevin Wolf 74057975222SKevin Wolf value = qemu_opt_get(opts, from); 74157975222SKevin Wolf if (value) { 7425abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 7435abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 7445abbf0eeSKevin Wolf "same time", to, from); 7455abbf0eeSKevin Wolf return; 7465abbf0eeSKevin Wolf } 74720d6cd47SJun Li } 74820d6cd47SJun Li 74920d6cd47SJun Li /* rename all items in opts */ 75020d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 751f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 75257975222SKevin Wolf qemu_opt_unset(opts, from); 75357975222SKevin Wolf } 75457975222SKevin Wolf } 75557975222SKevin Wolf 75633cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 75733cb7dc8SKevin Wolf .name = "drive", 75833cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 75933cb7dc8SKevin Wolf .desc = { 76033cb7dc8SKevin Wolf { 76187a899c5SKevin Wolf .name = "bus", 76287a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 76387a899c5SKevin Wolf .help = "bus number", 76487a899c5SKevin Wolf },{ 76587a899c5SKevin Wolf .name = "unit", 76687a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 76787a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 76887a899c5SKevin Wolf },{ 76987a899c5SKevin Wolf .name = "index", 77087a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 77187a899c5SKevin Wolf .help = "index number", 77287a899c5SKevin Wolf },{ 77333cb7dc8SKevin Wolf .name = "media", 77433cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 77533cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 776593d464bSKevin Wolf },{ 777593d464bSKevin Wolf .name = "if", 778593d464bSKevin Wolf .type = QEMU_OPT_STRING, 779593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 780b41a7338SKevin Wolf },{ 781b41a7338SKevin Wolf .name = "cyls", 782b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 783b41a7338SKevin Wolf .help = "number of cylinders (ide disk geometry)", 784b41a7338SKevin Wolf },{ 785b41a7338SKevin Wolf .name = "heads", 786b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 787b41a7338SKevin Wolf .help = "number of heads (ide disk geometry)", 788b41a7338SKevin Wolf },{ 789b41a7338SKevin Wolf .name = "secs", 790b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 791b41a7338SKevin Wolf .help = "number of sectors (ide disk geometry)", 792b41a7338SKevin Wolf },{ 793b41a7338SKevin Wolf .name = "trans", 794b41a7338SKevin Wolf .type = QEMU_OPT_STRING, 795b41a7338SKevin Wolf .help = "chs translation (auto, lba, none)", 79626929298SKevin Wolf },{ 79726929298SKevin Wolf .name = "boot", 79826929298SKevin Wolf .type = QEMU_OPT_BOOL, 79926929298SKevin Wolf .help = "(deprecated, ignored)", 800394c7d4dSKevin Wolf },{ 801394c7d4dSKevin Wolf .name = "addr", 802394c7d4dSKevin Wolf .type = QEMU_OPT_STRING, 803394c7d4dSKevin Wolf .help = "pci address (virtio only)", 804d095b465SMax Reitz },{ 805bcf83158SKevin Wolf .name = "serial", 806bcf83158SKevin Wolf .type = QEMU_OPT_STRING, 807bcf83158SKevin Wolf .help = "disk serial number", 808bcf83158SKevin Wolf },{ 809d095b465SMax Reitz .name = "file", 810d095b465SMax Reitz .type = QEMU_OPT_STRING, 811d095b465SMax Reitz .help = "file name", 81233cb7dc8SKevin Wolf }, 8130ebd24e0SKevin Wolf 8140ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 8150ebd24e0SKevin Wolf { 8160ebd24e0SKevin Wolf .name = "read-only", 8170ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8180ebd24e0SKevin Wolf .help = "open drive file as read-only", 8190ebd24e0SKevin Wolf },{ 820ee13ed1cSKevin Wolf .name = "rerror", 821ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 822ee13ed1cSKevin Wolf .help = "read error action", 823ee13ed1cSKevin Wolf },{ 824ee13ed1cSKevin Wolf .name = "werror", 825ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 826ee13ed1cSKevin Wolf .help = "write error action", 827ee13ed1cSKevin Wolf },{ 8280ebd24e0SKevin Wolf .name = "copy-on-read", 8290ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8300ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 8310ebd24e0SKevin Wolf }, 8320ebd24e0SKevin Wolf 83333cb7dc8SKevin Wolf { /* end of list */ } 83433cb7dc8SKevin Wolf }, 83533cb7dc8SKevin Wolf }; 83633cb7dc8SKevin Wolf 83760e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 83857975222SKevin Wolf { 83929c4e2b5SKevin Wolf const char *value; 84018e46a03SMarkus Armbruster BlockBackend *blk; 84133cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 842f298d071SKevin Wolf QDict *bs_opts; 84333cb7dc8SKevin Wolf QemuOpts *legacy_opts; 84433cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 845593d464bSKevin Wolf BlockInterfaceType type; 846b41a7338SKevin Wolf int cyls, heads, secs, translation; 84787a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 848394c7d4dSKevin Wolf const char *devaddr; 849ee13ed1cSKevin Wolf const char *werror, *rerror; 850a7fdbcf0SFam Zheng bool read_only = false; 851a7fdbcf0SFam Zheng bool copy_on_read; 852bcf83158SKevin Wolf const char *serial; 853d095b465SMax Reitz const char *filename; 85433cb7dc8SKevin Wolf Error *local_err = NULL; 855247147fbSKevin Wolf int i; 85629c4e2b5SKevin Wolf 85757975222SKevin Wolf /* Change legacy command line options into QMP ones */ 858247147fbSKevin Wolf static const struct { 859247147fbSKevin Wolf const char *from; 860247147fbSKevin Wolf const char *to; 861247147fbSKevin Wolf } opt_renames[] = { 862247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 863247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 864247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 86557975222SKevin Wolf 866247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 867247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 868247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 86957975222SKevin Wolf 870247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 871247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 872247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8733e9fab69SBenoît Canet 874247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 875247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 876247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8773e9fab69SBenoît Canet 878247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8792024c1dfSBenoît Canet 88076f4afb4SAlberto Garcia { "group", "throttling.group" }, 88176f4afb4SAlberto Garcia 882247147fbSKevin Wolf { "readonly", "read-only" }, 883247147fbSKevin Wolf }; 884247147fbSKevin Wolf 885247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8865abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8875abbf0eeSKevin Wolf &local_err); 8885abbf0eeSKevin Wolf if (local_err) { 889565f65d2SMarkus Armbruster error_report_err(local_err); 8905abbf0eeSKevin Wolf return NULL; 8915abbf0eeSKevin Wolf } 892247147fbSKevin Wolf } 8930f227a94SKevin Wolf 89429c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 89529c4e2b5SKevin Wolf if (value) { 89629c4e2b5SKevin Wolf int flags = 0; 89729c4e2b5SKevin Wolf 89829c4e2b5SKevin Wolf if (bdrv_parse_cache_flags(value, &flags) != 0) { 89929c4e2b5SKevin Wolf error_report("invalid cache option"); 90029c4e2b5SKevin Wolf return NULL; 90129c4e2b5SKevin Wolf } 90229c4e2b5SKevin Wolf 90329c4e2b5SKevin Wolf /* Specific options take precedence */ 90454861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 90554861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 906cccb7967SMarkus Armbruster !!(flags & BDRV_O_CACHE_WB), &error_abort); 90729c4e2b5SKevin Wolf } 90854861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 90954861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 910cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 91129c4e2b5SKevin Wolf } 91254861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 91354861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 914cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 91529c4e2b5SKevin Wolf } 91629c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 91729c4e2b5SKevin Wolf } 91829c4e2b5SKevin Wolf 919f298d071SKevin Wolf /* Get a QDict for processing the options */ 920f298d071SKevin Wolf bs_opts = qdict_new(); 921f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 922f298d071SKevin Wolf 92387ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 92487ea75d5SPeter Crosthwaite &error_abort); 92533cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 92684d18f06SMarkus Armbruster if (local_err) { 927565f65d2SMarkus Armbruster error_report_err(local_err); 92833cb7dc8SKevin Wolf goto fail; 92933cb7dc8SKevin Wolf } 93033cb7dc8SKevin Wolf 93126929298SKevin Wolf /* Deprecated option boot=[on|off] */ 93226929298SKevin Wolf if (qemu_opt_get(legacy_opts, "boot") != NULL) { 93326929298SKevin Wolf fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 93426929298SKevin Wolf "ignored. Future versions will reject this parameter. Please " 93526929298SKevin Wolf "update your scripts.\n"); 93626929298SKevin Wolf } 93726929298SKevin Wolf 93833cb7dc8SKevin Wolf /* Media type */ 93933cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 94033cb7dc8SKevin Wolf if (value) { 94133cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 94233cb7dc8SKevin Wolf media = MEDIA_DISK; 94333cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 94433cb7dc8SKevin Wolf media = MEDIA_CDROM; 945a7fdbcf0SFam Zheng read_only = true; 94633cb7dc8SKevin Wolf } else { 94733cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 94833cb7dc8SKevin Wolf goto fail; 94933cb7dc8SKevin Wolf } 95033cb7dc8SKevin Wolf } 95133cb7dc8SKevin Wolf 9520ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 953a7fdbcf0SFam Zheng read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); 9540ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 9550ebd24e0SKevin Wolf 9560ebd24e0SKevin Wolf if (read_only && copy_on_read) { 9570ebd24e0SKevin Wolf error_report("warning: disabling copy-on-read on read-only drive"); 9580ebd24e0SKevin Wolf copy_on_read = false; 9590ebd24e0SKevin Wolf } 9600ebd24e0SKevin Wolf 9610ebd24e0SKevin Wolf qdict_put(bs_opts, "read-only", 9620ebd24e0SKevin Wolf qstring_from_str(read_only ? "on" : "off")); 9630ebd24e0SKevin Wolf qdict_put(bs_opts, "copy-on-read", 9640ebd24e0SKevin Wolf qstring_from_str(copy_on_read ? "on" :"off")); 9650ebd24e0SKevin Wolf 966593d464bSKevin Wolf /* Controller type */ 967593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 968593d464bSKevin Wolf if (value) { 969593d464bSKevin Wolf for (type = 0; 970593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 971593d464bSKevin Wolf type++) { 972593d464bSKevin Wolf } 973593d464bSKevin Wolf if (type == IF_COUNT) { 974593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 975593d464bSKevin Wolf goto fail; 976593d464bSKevin Wolf } 977593d464bSKevin Wolf } else { 978593d464bSKevin Wolf type = block_default_type; 979593d464bSKevin Wolf } 980593d464bSKevin Wolf 981b41a7338SKevin Wolf /* Geometry */ 982b41a7338SKevin Wolf cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 983b41a7338SKevin Wolf heads = qemu_opt_get_number(legacy_opts, "heads", 0); 984b41a7338SKevin Wolf secs = qemu_opt_get_number(legacy_opts, "secs", 0); 985b41a7338SKevin Wolf 986b41a7338SKevin Wolf if (cyls || heads || secs) { 987b41a7338SKevin Wolf if (cyls < 1) { 988b41a7338SKevin Wolf error_report("invalid physical cyls number"); 989b41a7338SKevin Wolf goto fail; 990b41a7338SKevin Wolf } 991b41a7338SKevin Wolf if (heads < 1) { 992b41a7338SKevin Wolf error_report("invalid physical heads number"); 993b41a7338SKevin Wolf goto fail; 994b41a7338SKevin Wolf } 995b41a7338SKevin Wolf if (secs < 1) { 996b41a7338SKevin Wolf error_report("invalid physical secs number"); 997b41a7338SKevin Wolf goto fail; 998b41a7338SKevin Wolf } 999b41a7338SKevin Wolf } 1000b41a7338SKevin Wolf 1001b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 1002b41a7338SKevin Wolf value = qemu_opt_get(legacy_opts, "trans"); 1003b41a7338SKevin Wolf if (value != NULL) { 1004b41a7338SKevin Wolf if (!cyls) { 1005b41a7338SKevin Wolf error_report("'%s' trans must be used with cyls, heads and secs", 1006b41a7338SKevin Wolf value); 1007b41a7338SKevin Wolf goto fail; 1008b41a7338SKevin Wolf } 1009b41a7338SKevin Wolf if (!strcmp(value, "none")) { 1010b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_NONE; 1011b41a7338SKevin Wolf } else if (!strcmp(value, "lba")) { 1012b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_LBA; 1013f31c41ffSPaolo Bonzini } else if (!strcmp(value, "large")) { 1014f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_LARGE; 1015f31c41ffSPaolo Bonzini } else if (!strcmp(value, "rechs")) { 1016f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_RECHS; 1017b41a7338SKevin Wolf } else if (!strcmp(value, "auto")) { 1018b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 1019b41a7338SKevin Wolf } else { 1020b41a7338SKevin Wolf error_report("'%s' invalid translation type", value); 1021b41a7338SKevin Wolf goto fail; 1022b41a7338SKevin Wolf } 1023b41a7338SKevin Wolf } 1024b41a7338SKevin Wolf 1025b41a7338SKevin Wolf if (media == MEDIA_CDROM) { 1026b41a7338SKevin Wolf if (cyls || secs || heads) { 1027b41a7338SKevin Wolf error_report("CHS can't be set with media=cdrom"); 1028b41a7338SKevin Wolf goto fail; 1029b41a7338SKevin Wolf } 1030b41a7338SKevin Wolf } 1031b41a7338SKevin Wolf 103287a899c5SKevin Wolf /* Device address specified by bus/unit or index. 103387a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 103487a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 103587a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 103687a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 103787a899c5SKevin Wolf 103887a899c5SKevin Wolf max_devs = if_max_devs[type]; 103987a899c5SKevin Wolf 104087a899c5SKevin Wolf if (index != -1) { 104187a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 104287a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 104387a899c5SKevin Wolf goto fail; 104487a899c5SKevin Wolf } 104587a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 104687a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 104787a899c5SKevin Wolf } 104887a899c5SKevin Wolf 104987a899c5SKevin Wolf if (unit_id == -1) { 105087a899c5SKevin Wolf unit_id = 0; 105187a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 105287a899c5SKevin Wolf unit_id++; 105387a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 105487a899c5SKevin Wolf unit_id -= max_devs; 105587a899c5SKevin Wolf bus_id++; 105687a899c5SKevin Wolf } 105787a899c5SKevin Wolf } 105887a899c5SKevin Wolf } 105987a899c5SKevin Wolf 106087a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 106187a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 106287a899c5SKevin Wolf goto fail; 106387a899c5SKevin Wolf } 106487a899c5SKevin Wolf 106587a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 106687a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 106787a899c5SKevin Wolf bus_id, unit_id, index); 106887a899c5SKevin Wolf goto fail; 106987a899c5SKevin Wolf } 107087a899c5SKevin Wolf 1071bcf83158SKevin Wolf /* Serial number */ 1072bcf83158SKevin Wolf serial = qemu_opt_get(legacy_opts, "serial"); 1073bcf83158SKevin Wolf 107487a899c5SKevin Wolf /* no id supplied -> create one */ 107587a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 107687a899c5SKevin Wolf char *new_id; 107787a899c5SKevin Wolf const char *mediastr = ""; 107887a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 107987a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 108087a899c5SKevin Wolf } 108187a899c5SKevin Wolf if (max_devs) { 108287a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 108387a899c5SKevin Wolf mediastr, unit_id); 108487a899c5SKevin Wolf } else { 108587a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 108687a899c5SKevin Wolf mediastr, unit_id); 108787a899c5SKevin Wolf } 108887a899c5SKevin Wolf qdict_put(bs_opts, "id", qstring_from_str(new_id)); 108987a899c5SKevin Wolf g_free(new_id); 109087a899c5SKevin Wolf } 109187a899c5SKevin Wolf 1092394c7d4dSKevin Wolf /* Add virtio block device */ 1093394c7d4dSKevin Wolf devaddr = qemu_opt_get(legacy_opts, "addr"); 1094394c7d4dSKevin Wolf if (devaddr && type != IF_VIRTIO) { 1095394c7d4dSKevin Wolf error_report("addr is not supported by this bus type"); 1096394c7d4dSKevin Wolf goto fail; 1097394c7d4dSKevin Wolf } 1098394c7d4dSKevin Wolf 1099394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 1100394c7d4dSKevin Wolf QemuOpts *devopts; 110187ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 110287ea75d5SPeter Crosthwaite &error_abort); 1103394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 11041f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1105394c7d4dSKevin Wolf } else { 1106f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1107394c7d4dSKevin Wolf } 1108f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1109f43e47dbSMarkus Armbruster &error_abort); 1110394c7d4dSKevin Wolf if (devaddr) { 1111f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1112394c7d4dSKevin Wolf } 1113394c7d4dSKevin Wolf } 1114394c7d4dSKevin Wolf 1115d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 1116d095b465SMax Reitz 1117ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 1118ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 1119ee13ed1cSKevin Wolf if (werror != NULL) { 1120ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1121ee13ed1cSKevin Wolf type != IF_NONE) { 1122ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 1123ee13ed1cSKevin Wolf goto fail; 1124ee13ed1cSKevin Wolf } 1125ee13ed1cSKevin Wolf qdict_put(bs_opts, "werror", qstring_from_str(werror)); 1126ee13ed1cSKevin Wolf } 1127ee13ed1cSKevin Wolf 1128ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 1129ee13ed1cSKevin Wolf if (rerror != NULL) { 1130ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1131ee13ed1cSKevin Wolf type != IF_NONE) { 1132ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 1133ee13ed1cSKevin Wolf goto fail; 1134ee13ed1cSKevin Wolf } 1135ee13ed1cSKevin Wolf qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); 1136ee13ed1cSKevin Wolf } 1137ee13ed1cSKevin Wolf 11382d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 113918e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 11403cb0e25cSMarkus Armbruster bs_opts = NULL; 114118e46a03SMarkus Armbruster if (!blk) { 114284d18f06SMarkus Armbruster if (local_err) { 1143565f65d2SMarkus Armbruster error_report_err(local_err); 1144b681072dSKevin Wolf } 11452d246f01SKevin Wolf goto fail; 1146b681072dSKevin Wolf } else { 114784d18f06SMarkus Armbruster assert(!local_err); 11482d246f01SKevin Wolf } 11492d246f01SKevin Wolf 115026f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 115126f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1152f298d071SKevin Wolf dinfo->opts = all_opts; 11532d246f01SKevin Wolf 1154b41a7338SKevin Wolf dinfo->cyls = cyls; 1155b41a7338SKevin Wolf dinfo->heads = heads; 1156b41a7338SKevin Wolf dinfo->secs = secs; 1157b41a7338SKevin Wolf dinfo->trans = translation; 1158b41a7338SKevin Wolf 1159ee13ed1cSKevin Wolf dinfo->type = type; 116087a899c5SKevin Wolf dinfo->bus = bus_id; 116187a899c5SKevin Wolf dinfo->unit = unit_id; 1162394c7d4dSKevin Wolf dinfo->devaddr = devaddr; 1163bcf83158SKevin Wolf dinfo->serial = g_strdup(serial); 1164bcf83158SKevin Wolf 116526f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 116626f8b3a8SMarkus Armbruster 1167e34ef046SKevin Wolf switch(type) { 1168e34ef046SKevin Wolf case IF_IDE: 1169e34ef046SKevin Wolf case IF_SCSI: 1170e34ef046SKevin Wolf case IF_XEN: 1171e34ef046SKevin Wolf case IF_NONE: 1172e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1173e34ef046SKevin Wolf break; 1174e34ef046SKevin Wolf default: 1175e34ef046SKevin Wolf break; 1176e34ef046SKevin Wolf } 1177e34ef046SKevin Wolf 11782d246f01SKevin Wolf fail: 117933cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 11803cb0e25cSMarkus Armbruster QDECREF(bs_opts); 11812d246f01SKevin Wolf return dinfo; 118257975222SKevin Wolf } 118357975222SKevin Wolf 11843e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1185666daa68SMarkus Armbruster { 1186666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1187a0e8544cSFam Zheng BlockBackend *blk; 11882d3735d3SStefan Hajnoczi int ret; 11892d3735d3SStefan Hajnoczi 1190e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1191da31d594SMax Reitz ret = blk_commit_all(); 1192e8877497SStefan Hajnoczi } else { 119384aa0140SStefan Hajnoczi BlockDriverState *bs; 119484aa0140SStefan Hajnoczi AioContext *aio_context; 119584aa0140SStefan Hajnoczi 1196a0e8544cSFam Zheng blk = blk_by_name(device); 1197a0e8544cSFam Zheng if (!blk) { 119858513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1199ac59eb95SMarkus Armbruster return; 12006ab4b5abSMarkus Armbruster } 12015433c24fSMax Reitz if (!blk_is_available(blk)) { 12025433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 12035433c24fSMax Reitz return; 12045433c24fSMax Reitz } 120584aa0140SStefan Hajnoczi 120684aa0140SStefan Hajnoczi bs = blk_bs(blk); 120784aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 120884aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 120984aa0140SStefan Hajnoczi 121084aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 121184aa0140SStefan Hajnoczi 121284aa0140SStefan Hajnoczi aio_context_release(aio_context); 12132d3735d3SStefan Hajnoczi } 121458513bdeSJeff Cody if (ret < 0) { 121558513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 121658513bdeSJeff Cody strerror(-ret)); 1217666daa68SMarkus Armbruster } 1218666daa68SMarkus Armbruster } 1219666daa68SMarkus Armbruster 122010f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 12216cc2a415SPaolo Bonzini { 1222c8a83e85SKevin Wolf TransactionActionList list; 12236cc2a415SPaolo Bonzini 122410f75907SEric Blake list.value = action; 12256cc2a415SPaolo Bonzini list.next = NULL; 122694d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 12276cc2a415SPaolo Bonzini } 12286cc2a415SPaolo Bonzini 12290901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 12300901f67eSBenoît Canet bool has_node_name, const char *node_name, 12310901f67eSBenoît Canet const char *snapshot_file, 12320901f67eSBenoît Canet bool has_snapshot_node_name, 12330901f67eSBenoît Canet const char *snapshot_node_name, 12346106e249SLuiz Capitulino bool has_format, const char *format, 12350901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1236f8882568SJes Sorensen { 1237a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 12380901f67eSBenoît Canet .has_device = has_device, 12396cc2a415SPaolo Bonzini .device = (char *) device, 12400901f67eSBenoît Canet .has_node_name = has_node_name, 12410901f67eSBenoît Canet .node_name = (char *) node_name, 12426cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 12430901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 12440901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 12456cc2a415SPaolo Bonzini .has_format = has_format, 12466cc2a415SPaolo Bonzini .format = (char *) format, 12476cc2a415SPaolo Bonzini .has_mode = has_mode, 12486cc2a415SPaolo Bonzini .mode = mode, 12496cc2a415SPaolo Bonzini }; 125010f75907SEric Blake TransactionAction action = { 125110f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 125232bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 125310f75907SEric Blake }; 125410f75907SEric Blake blockdev_do_action(&action, errp); 1255f8882568SJes Sorensen } 1256f8882568SJes Sorensen 125743de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 125843de7e2dSAlberto Garcia Error **errp) 125943de7e2dSAlberto Garcia { 126043de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 126143de7e2dSAlberto Garcia .node = (char *) node, 126243de7e2dSAlberto Garcia .overlay = (char *) overlay 126343de7e2dSAlberto Garcia }; 126410f75907SEric Blake TransactionAction action = { 126510f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 126632bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 126710f75907SEric Blake }; 126810f75907SEric Blake blockdev_do_action(&action, errp); 126943de7e2dSAlberto Garcia } 127043de7e2dSAlberto Garcia 1271f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1272f323bc9eSWenchao Xia const char *name, 1273f323bc9eSWenchao Xia Error **errp) 1274f323bc9eSWenchao Xia { 1275f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1276f323bc9eSWenchao Xia .device = (char *) device, 1277f323bc9eSWenchao Xia .name = (char *) name 1278f323bc9eSWenchao Xia }; 127910f75907SEric Blake TransactionAction action = { 128010f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 128132bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 128210f75907SEric Blake }; 128310f75907SEric Blake blockdev_do_action(&action, errp); 1284f323bc9eSWenchao Xia } 1285f323bc9eSWenchao Xia 128644e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 128744e3e053SWenchao Xia bool has_id, 128844e3e053SWenchao Xia const char *id, 128944e3e053SWenchao Xia bool has_name, 129044e3e053SWenchao Xia const char *name, 129144e3e053SWenchao Xia Error **errp) 129244e3e053SWenchao Xia { 1293a0e8544cSFam Zheng BlockDriverState *bs; 1294a0e8544cSFam Zheng BlockBackend *blk; 12954ef3982aSStefan Hajnoczi AioContext *aio_context; 129644e3e053SWenchao Xia QEMUSnapshotInfo sn; 129744e3e053SWenchao Xia Error *local_err = NULL; 129844e3e053SWenchao Xia SnapshotInfo *info = NULL; 129944e3e053SWenchao Xia int ret; 130044e3e053SWenchao Xia 1301a0e8544cSFam Zheng blk = blk_by_name(device); 1302a0e8544cSFam Zheng if (!blk) { 130375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 130475158ebbSMarkus Armbruster "Device '%s' not found", device); 130544e3e053SWenchao Xia return NULL; 130644e3e053SWenchao Xia } 13075433c24fSMax Reitz 13085433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 13095433c24fSMax Reitz aio_context_acquire(aio_context); 131044e3e053SWenchao Xia 131144e3e053SWenchao Xia if (!has_id) { 131244e3e053SWenchao Xia id = NULL; 131344e3e053SWenchao Xia } 131444e3e053SWenchao Xia 131544e3e053SWenchao Xia if (!has_name) { 131644e3e053SWenchao Xia name = NULL; 131744e3e053SWenchao Xia } 131844e3e053SWenchao Xia 131944e3e053SWenchao Xia if (!id && !name) { 132044e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 13215433c24fSMax Reitz goto out_aio_context; 132244e3e053SWenchao Xia } 132344e3e053SWenchao Xia 13245433c24fSMax Reitz if (!blk_is_available(blk)) { 13255433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 13265433c24fSMax Reitz goto out_aio_context; 13275433c24fSMax Reitz } 13285433c24fSMax Reitz bs = blk_bs(blk); 13294ef3982aSStefan Hajnoczi 13300b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 13310b928854SStefan Hajnoczi goto out_aio_context; 13320b928854SStefan Hajnoczi } 13330b928854SStefan Hajnoczi 133444e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 133584d18f06SMarkus Armbruster if (local_err) { 133644e3e053SWenchao Xia error_propagate(errp, local_err); 13374ef3982aSStefan Hajnoczi goto out_aio_context; 133844e3e053SWenchao Xia } 133944e3e053SWenchao Xia if (!ret) { 134044e3e053SWenchao Xia error_setg(errp, 134144e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 134244e3e053SWenchao Xia "device '%s'", 134344e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 13444ef3982aSStefan Hajnoczi goto out_aio_context; 134544e3e053SWenchao Xia } 134644e3e053SWenchao Xia 134744e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 134884d18f06SMarkus Armbruster if (local_err) { 134944e3e053SWenchao Xia error_propagate(errp, local_err); 13504ef3982aSStefan Hajnoczi goto out_aio_context; 135144e3e053SWenchao Xia } 135244e3e053SWenchao Xia 13534ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13544ef3982aSStefan Hajnoczi 13555839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 135644e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 135744e3e053SWenchao Xia info->name = g_strdup(sn.name); 135844e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 135944e3e053SWenchao Xia info->date_sec = sn.date_sec; 136044e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 136144e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 136244e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 136344e3e053SWenchao Xia 136444e3e053SWenchao Xia return info; 13654ef3982aSStefan Hajnoczi 13664ef3982aSStefan Hajnoczi out_aio_context: 13674ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13684ef3982aSStefan Hajnoczi return NULL; 136944e3e053SWenchao Xia } 13708802d1fdSJeff Cody 1371341ebc2fSJohn Snow /** 1372341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1373341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1374341ebc2fSJohn Snow * the node reference and bitmap names. 1375341ebc2fSJohn Snow * 1376341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1377341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1378341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1379341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1380341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1381341ebc2fSJohn Snow * 1382341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1383341ebc2fSJohn Snow */ 1384341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1385341ebc2fSJohn Snow const char *name, 1386341ebc2fSJohn Snow BlockDriverState **pbs, 1387341ebc2fSJohn Snow AioContext **paio, 1388341ebc2fSJohn Snow Error **errp) 1389341ebc2fSJohn Snow { 1390341ebc2fSJohn Snow BlockDriverState *bs; 1391341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1392341ebc2fSJohn Snow AioContext *aio_context; 1393341ebc2fSJohn Snow 1394341ebc2fSJohn Snow if (!node) { 1395341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1396341ebc2fSJohn Snow return NULL; 1397341ebc2fSJohn Snow } 1398341ebc2fSJohn Snow if (!name) { 1399341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1400341ebc2fSJohn Snow return NULL; 1401341ebc2fSJohn Snow } 1402341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1403341ebc2fSJohn Snow if (!bs) { 1404341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1405341ebc2fSJohn Snow return NULL; 1406341ebc2fSJohn Snow } 1407341ebc2fSJohn Snow 1408341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 1409341ebc2fSJohn Snow aio_context_acquire(aio_context); 1410341ebc2fSJohn Snow 1411341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1412341ebc2fSJohn Snow if (!bitmap) { 1413341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 1414341ebc2fSJohn Snow goto fail; 1415341ebc2fSJohn Snow } 1416341ebc2fSJohn Snow 1417341ebc2fSJohn Snow if (pbs) { 1418341ebc2fSJohn Snow *pbs = bs; 1419341ebc2fSJohn Snow } 1420341ebc2fSJohn Snow if (paio) { 1421341ebc2fSJohn Snow *paio = aio_context; 1422341ebc2fSJohn Snow } else { 1423341ebc2fSJohn Snow aio_context_release(aio_context); 1424341ebc2fSJohn Snow } 1425341ebc2fSJohn Snow 1426341ebc2fSJohn Snow return bitmap; 1427341ebc2fSJohn Snow 1428341ebc2fSJohn Snow fail: 1429341ebc2fSJohn Snow aio_context_release(aio_context); 1430341ebc2fSJohn Snow return NULL; 1431341ebc2fSJohn Snow } 1432341ebc2fSJohn Snow 1433b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1434ba0c86a3SWenchao Xia 143550f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1436ba0c86a3SWenchao Xia 143750f43f0fSJohn Snow /** 143850f43f0fSJohn Snow * BlkActionOps: 143950f43f0fSJohn Snow * Table of operations that define an Action. 144050f43f0fSJohn Snow * 144150f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 144250f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 144350f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 144450f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 144550f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 144650f43f0fSJohn Snow * commit() or abort(). Can be NULL. 144750f43f0fSJohn Snow * 144850f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 144950f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1450ba0c86a3SWenchao Xia */ 145150f43f0fSJohn Snow typedef struct BlkActionOps { 145250f43f0fSJohn Snow size_t instance_size; 145350f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 145450f43f0fSJohn Snow void (*commit)(BlkActionState *common); 145550f43f0fSJohn Snow void (*abort)(BlkActionState *common); 145650f43f0fSJohn Snow void (*clean)(BlkActionState *common); 145750f43f0fSJohn Snow } BlkActionOps; 145850f43f0fSJohn Snow 145950f43f0fSJohn Snow /** 146050f43f0fSJohn Snow * BlkActionState: 146150f43f0fSJohn Snow * Describes one Action's state within a Transaction. 146250f43f0fSJohn Snow * 146350f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 146450f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 146594d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 146650f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 146750f43f0fSJohn Snow * 146850f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 146950f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 147050f43f0fSJohn Snow * base class. 147150f43f0fSJohn Snow */ 147250f43f0fSJohn Snow struct BlkActionState { 1473c8a83e85SKevin Wolf TransactionAction *action; 147450f43f0fSJohn Snow const BlkActionOps *ops; 147594d16a64SJohn Snow BlockJobTxn *block_job_txn; 147694d16a64SJohn Snow TransactionProperties *txn_props; 147750f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1478ba0c86a3SWenchao Xia }; 1479ba0c86a3SWenchao Xia 1480bbe86010SWenchao Xia /* internal snapshot private data */ 1481bbe86010SWenchao Xia typedef struct InternalSnapshotState { 148250f43f0fSJohn Snow BlkActionState common; 1483bbe86010SWenchao Xia BlockDriverState *bs; 14845d6e96efSStefan Hajnoczi AioContext *aio_context; 1485bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1486507306ccSFam Zheng bool created; 1487bbe86010SWenchao Xia } InternalSnapshotState; 1488bbe86010SWenchao Xia 148994d16a64SJohn Snow 149094d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 149194d16a64SJohn Snow { 149294d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 149394d16a64SJohn Snow error_setg(errp, 149494d16a64SJohn Snow "Action '%s' does not support Transaction property " 149594d16a64SJohn Snow "completion-mode = %s", 149694d16a64SJohn Snow TransactionActionKind_lookup[s->action->type], 149794d16a64SJohn Snow ActionCompletionMode_lookup[s->txn_props->completion_mode]); 149894d16a64SJohn Snow return -1; 149994d16a64SJohn Snow } 150094d16a64SJohn Snow return 0; 150194d16a64SJohn Snow } 150294d16a64SJohn Snow 150350f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1504bbe86010SWenchao Xia Error **errp) 1505bbe86010SWenchao Xia { 1506f70edf99SMarkus Armbruster Error *local_err = NULL; 1507bbe86010SWenchao Xia const char *device; 1508bbe86010SWenchao Xia const char *name; 1509a0e8544cSFam Zheng BlockBackend *blk; 1510bbe86010SWenchao Xia BlockDriverState *bs; 1511bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1512bbe86010SWenchao Xia bool ret; 1513bbe86010SWenchao Xia qemu_timeval tv; 1514bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1515bbe86010SWenchao Xia InternalSnapshotState *state; 1516bbe86010SWenchao Xia int ret1; 1517bbe86010SWenchao Xia 15186a8f9661SEric Blake g_assert(common->action->type == 1519bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 152032bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1521bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1522bbe86010SWenchao Xia 1523bbe86010SWenchao Xia /* 1. parse input */ 1524bbe86010SWenchao Xia device = internal->device; 1525bbe86010SWenchao Xia name = internal->name; 1526bbe86010SWenchao Xia 1527bbe86010SWenchao Xia /* 2. check for validation */ 152894d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 152994d16a64SJohn Snow return; 153094d16a64SJohn Snow } 153194d16a64SJohn Snow 1532a0e8544cSFam Zheng blk = blk_by_name(device); 1533a0e8544cSFam Zheng if (!blk) { 153475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 153575158ebbSMarkus Armbruster "Device '%s' not found", device); 1536bbe86010SWenchao Xia return; 1537bbe86010SWenchao Xia } 1538bbe86010SWenchao Xia 15395d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 15405433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 15415d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 15425d6e96efSStefan Hajnoczi 15435433c24fSMax Reitz if (!blk_is_available(blk)) { 1544c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1545bbe86010SWenchao Xia return; 1546bbe86010SWenchao Xia } 15475433c24fSMax Reitz bs = blk_bs(blk); 1548bbe86010SWenchao Xia 1549507306ccSFam Zheng state->bs = bs; 1550507306ccSFam Zheng bdrv_drained_begin(bs); 1551507306ccSFam Zheng 15523dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 15533dc7ca3cSStefan Hajnoczi return; 15543dc7ca3cSStefan Hajnoczi } 15553dc7ca3cSStefan Hajnoczi 1556bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 155781e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1558bbe86010SWenchao Xia return; 1559bbe86010SWenchao Xia } 1560bbe86010SWenchao Xia 1561bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 156281e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 156381e5f78aSAlberto Garcia "does not support internal snapshots", 156481e5f78aSAlberto Garcia bs->drv->format_name, device); 1565bbe86010SWenchao Xia return; 1566bbe86010SWenchao Xia } 1567bbe86010SWenchao Xia 1568bbe86010SWenchao Xia if (!strlen(name)) { 1569bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1570bbe86010SWenchao Xia return; 1571bbe86010SWenchao Xia } 1572bbe86010SWenchao Xia 1573bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1574f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1575f70edf99SMarkus Armbruster &local_err); 1576f70edf99SMarkus Armbruster if (local_err) { 1577f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1578bbe86010SWenchao Xia return; 1579bbe86010SWenchao Xia } else if (ret) { 1580bbe86010SWenchao Xia error_setg(errp, 1581bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1582bbe86010SWenchao Xia name, device); 1583bbe86010SWenchao Xia return; 1584bbe86010SWenchao Xia } 1585bbe86010SWenchao Xia 1586bbe86010SWenchao Xia /* 3. take the snapshot */ 1587bbe86010SWenchao Xia sn = &state->sn; 1588bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1589bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1590bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1591bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1592bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1593bbe86010SWenchao Xia 1594bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1595bbe86010SWenchao Xia if (ret1 < 0) { 1596bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1597bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1598bbe86010SWenchao Xia name, device); 1599bbe86010SWenchao Xia return; 1600bbe86010SWenchao Xia } 1601bbe86010SWenchao Xia 1602bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1603507306ccSFam Zheng state->created = true; 1604bbe86010SWenchao Xia } 1605bbe86010SWenchao Xia 160650f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1607bbe86010SWenchao Xia { 1608bbe86010SWenchao Xia InternalSnapshotState *state = 1609bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1610bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1611bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1612bbe86010SWenchao Xia Error *local_error = NULL; 1613bbe86010SWenchao Xia 1614507306ccSFam Zheng if (!state->created) { 1615bbe86010SWenchao Xia return; 1616bbe86010SWenchao Xia } 1617bbe86010SWenchao Xia 1618bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1619c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1620c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1621c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1622c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1623c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1624bbe86010SWenchao Xia } 1625bbe86010SWenchao Xia } 1626bbe86010SWenchao Xia 162750f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 16285d6e96efSStefan Hajnoczi { 16295d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 16305d6e96efSStefan Hajnoczi common, common); 16315d6e96efSStefan Hajnoczi 16325d6e96efSStefan Hajnoczi if (state->aio_context) { 1633507306ccSFam Zheng if (state->bs) { 1634507306ccSFam Zheng bdrv_drained_end(state->bs); 1635507306ccSFam Zheng } 16365d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16375d6e96efSStefan Hajnoczi } 16385d6e96efSStefan Hajnoczi } 16395d6e96efSStefan Hajnoczi 1640ba0c86a3SWenchao Xia /* external snapshot private data */ 1641ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 164250f43f0fSJohn Snow BlkActionState common; 16438802d1fdSJeff Cody BlockDriverState *old_bs; 16448802d1fdSJeff Cody BlockDriverState *new_bs; 16455d6e96efSStefan Hajnoczi AioContext *aio_context; 1646ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16478802d1fdSJeff Cody 164850f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16499b9877eeSWenchao Xia Error **errp) 16509b9877eeSWenchao Xia { 165143de7e2dSAlberto Garcia int flags = 0, ret; 165243de7e2dSAlberto Garcia QDict *options = NULL; 16539b9877eeSWenchao Xia Error *local_err = NULL; 165443de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1655e2a31e87SWenchao Xia const char *device; 16560901f67eSBenoît Canet const char *node_name; 165743de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 165843de7e2dSAlberto Garcia const char *snapshot_ref; 165943de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1660e2a31e87SWenchao Xia const char *new_image_file; 1661ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1662ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1663c8a83e85SKevin Wolf TransactionAction *action = common->action; 16649b9877eeSWenchao Xia 166543de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 166643de7e2dSAlberto Garcia * purpose but a different set of parameters */ 166743de7e2dSAlberto Garcia switch (action->type) { 166843de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 166943de7e2dSAlberto Garcia { 167032bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 167143de7e2dSAlberto Garcia device = s->node; 167243de7e2dSAlberto Garcia node_name = s->node; 167343de7e2dSAlberto Garcia new_image_file = NULL; 167443de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1675e2a31e87SWenchao Xia } 167643de7e2dSAlberto Garcia break; 167743de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 167843de7e2dSAlberto Garcia { 167932bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 168043de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 168143de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 168243de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 168343de7e2dSAlberto Garcia snapshot_ref = NULL; 168443de7e2dSAlberto Garcia } 168543de7e2dSAlberto Garcia break; 168643de7e2dSAlberto Garcia default: 168743de7e2dSAlberto Garcia g_assert_not_reached(); 1688e2a31e87SWenchao Xia } 1689e2a31e87SWenchao Xia 1690e2a31e87SWenchao Xia /* start processing */ 169194d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 169294d16a64SJohn Snow return; 169394d16a64SJohn Snow } 169494d16a64SJohn Snow 169543de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 169643de7e2dSAlberto Garcia if (!state->old_bs) { 16979b9877eeSWenchao Xia return; 16989b9877eeSWenchao Xia } 16999b9877eeSWenchao Xia 17005d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 17015d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 17025d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1703da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 17045d6e96efSStefan Hajnoczi 1705ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1706c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 17079b9877eeSWenchao Xia return; 17089b9877eeSWenchao Xia } 17099b9877eeSWenchao Xia 17103718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 17113718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 17129b9877eeSWenchao Xia return; 17139b9877eeSWenchao Xia } 17149b9877eeSWenchao Xia 1715ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1716ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1717c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 17189b9877eeSWenchao Xia return; 17199b9877eeSWenchao Xia } 17209b9877eeSWenchao Xia } 17219b9877eeSWenchao Xia 1722212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1723c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1724f6186f49SBenoît Canet return; 1725f6186f49SBenoît Canet } 1726f6186f49SBenoît Canet 172743de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 172832bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 172943de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 173043de7e2dSAlberto Garcia enum NewImageMode mode; 173143de7e2dSAlberto Garcia const char *snapshot_node_name = 173243de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 173343de7e2dSAlberto Garcia 173443de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 173543de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 173643de7e2dSAlberto Garcia return; 173743de7e2dSAlberto Garcia } 173843de7e2dSAlberto Garcia 173943de7e2dSAlberto Garcia if (snapshot_node_name && 174043de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 174143de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 174243de7e2dSAlberto Garcia return; 174343de7e2dSAlberto Garcia } 174443de7e2dSAlberto Garcia 1745ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 1746*4c844983SKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); 17479b9877eeSWenchao Xia 17489b9877eeSWenchao Xia /* create new image w/backing file */ 174943de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17509b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1751f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1752f86b8b58SKevin Wolf if (size < 0) { 1753f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 1754f86b8b58SKevin Wolf return; 1755f86b8b58SKevin Wolf } 17569b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1757ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1758ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 1759f86b8b58SKevin Wolf NULL, size, flags, &local_err, false); 176084d18f06SMarkus Armbruster if (local_err) { 17619b9877eeSWenchao Xia error_propagate(errp, local_err); 17629b9877eeSWenchao Xia return; 17639b9877eeSWenchao Xia } 17649b9877eeSWenchao Xia } 17659b9877eeSWenchao Xia 17660901f67eSBenoît Canet options = qdict_new(); 176743de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 17680901f67eSBenoît Canet qdict_put(options, "node-name", 17690901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 17700901f67eSBenoît Canet } 1771e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 17720901f67eSBenoît Canet 177343de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 177443de7e2dSAlberto Garcia } 177543de7e2dSAlberto Garcia 1776f67503e5SMax Reitz assert(state->new_bs == NULL); 177743de7e2dSAlberto Garcia ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options, 177843de7e2dSAlberto Garcia flags, errp); 1779f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17809b9877eeSWenchao Xia if (ret != 0) { 178143de7e2dSAlberto Garcia return; 178243de7e2dSAlberto Garcia } 178343de7e2dSAlberto Garcia 178443de7e2dSAlberto Garcia if (state->new_bs->blk != NULL) { 178543de7e2dSAlberto Garcia error_setg(errp, "The snapshot is already in use by %s", 178643de7e2dSAlberto Garcia blk_name(state->new_bs->blk)); 178743de7e2dSAlberto Garcia return; 178843de7e2dSAlberto Garcia } 178943de7e2dSAlberto Garcia 179043de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 179143de7e2dSAlberto Garcia errp)) { 179243de7e2dSAlberto Garcia return; 179343de7e2dSAlberto Garcia } 179443de7e2dSAlberto Garcia 179543de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 179643de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 179708b24cfeSAlberto Garcia return; 179808b24cfeSAlberto Garcia } 179908b24cfeSAlberto Garcia 180008b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 180108b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 18029b9877eeSWenchao Xia } 18039b9877eeSWenchao Xia } 18049b9877eeSWenchao Xia 180550f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 18063b0047e8SWenchao Xia { 1807ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1808ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1809ba0c86a3SWenchao Xia 18105d6e96efSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, state->aio_context); 18115d6e96efSStefan Hajnoczi 1812ba5d6ab6SStefan Hajnoczi /* This removes our old bs and adds the new bs */ 1813ba5d6ab6SStefan Hajnoczi bdrv_append(state->new_bs, state->old_bs); 18143b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 18153b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 18163b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 1817*4c844983SKevin Wolf if (!state->old_bs->copy_on_read) { 1818dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 18193b0047e8SWenchao Xia NULL); 18203b0047e8SWenchao Xia } 1821*4c844983SKevin Wolf } 18223b0047e8SWenchao Xia 182350f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 182496b86bf7SWenchao Xia { 1825ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1826ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1827ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 18284f6fd349SFam Zheng bdrv_unref(state->new_bs); 182996b86bf7SWenchao Xia } 1830da763e83SFam Zheng } 1831da763e83SFam Zheng 183250f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1833da763e83SFam Zheng { 1834da763e83SFam Zheng ExternalSnapshotState *state = 1835da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 18365d6e96efSStefan Hajnoczi if (state->aio_context) { 1837da763e83SFam Zheng bdrv_drained_end(state->old_bs); 18385d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18395d6e96efSStefan Hajnoczi } 184096b86bf7SWenchao Xia } 184196b86bf7SWenchao Xia 18423037f364SStefan Hajnoczi typedef struct DriveBackupState { 184350f43f0fSJohn Snow BlkActionState common; 18443037f364SStefan Hajnoczi BlockDriverState *bs; 18455d6e96efSStefan Hajnoczi AioContext *aio_context; 18463037f364SStefan Hajnoczi BlockJob *job; 18473037f364SStefan Hajnoczi } DriveBackupState; 18483037f364SStefan Hajnoczi 184978f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 185078f51fdeSJohn Snow bool has_format, const char *format, 185178f51fdeSJohn Snow enum MirrorSyncMode sync, 185278f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 185378f51fdeSJohn Snow bool has_speed, int64_t speed, 185478f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 185578f51fdeSJohn Snow bool has_on_source_error, 185678f51fdeSJohn Snow BlockdevOnError on_source_error, 185778f51fdeSJohn Snow bool has_on_target_error, 185878f51fdeSJohn Snow BlockdevOnError on_target_error, 185978f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 186078f51fdeSJohn Snow 186150f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18623037f364SStefan Hajnoczi { 18633037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1864a0e8544cSFam Zheng BlockBackend *blk; 18653037f364SStefan Hajnoczi DriveBackup *backup; 18663037f364SStefan Hajnoczi Error *local_err = NULL; 18673037f364SStefan Hajnoczi 18686a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 186932bafa8fSEric Blake backup = common->action->u.drive_backup.data; 18703037f364SStefan Hajnoczi 1871a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1872a0e8544cSFam Zheng if (!blk) { 187375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 187475158ebbSMarkus Armbruster "Device '%s' not found", backup->device); 18755d6e96efSStefan Hajnoczi return; 18765d6e96efSStefan Hajnoczi } 18775d6e96efSStefan Hajnoczi 18781fdd4b7bSFam Zheng if (!blk_is_available(blk)) { 18791fdd4b7bSFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 18801fdd4b7bSFam Zheng return; 18811fdd4b7bSFam Zheng } 18821fdd4b7bSFam Zheng 18835d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 18845433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 18855d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 18861fdd4b7bSFam Zheng bdrv_drained_begin(blk_bs(blk)); 18871fdd4b7bSFam Zheng state->bs = blk_bs(blk); 18885d6e96efSStefan Hajnoczi 188978f51fdeSJohn Snow do_drive_backup(backup->device, backup->target, 18903037f364SStefan Hajnoczi backup->has_format, backup->format, 1891b53169eaSStefan Hajnoczi backup->sync, 18923037f364SStefan Hajnoczi backup->has_mode, backup->mode, 18933037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1894d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 18953037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 18963037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 189794d16a64SJohn Snow common->block_job_txn, &local_err); 189884d18f06SMarkus Armbruster if (local_err) { 18993037f364SStefan Hajnoczi error_propagate(errp, local_err); 19003037f364SStefan Hajnoczi return; 19013037f364SStefan Hajnoczi } 19023037f364SStefan Hajnoczi 19033037f364SStefan Hajnoczi state->job = state->bs->job; 19043037f364SStefan Hajnoczi } 19053037f364SStefan Hajnoczi 190650f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 19073037f364SStefan Hajnoczi { 19083037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19093037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 19103037f364SStefan Hajnoczi 19113037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 19123037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 19133037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 19143037f364SStefan Hajnoczi } 19153037f364SStefan Hajnoczi } 19163037f364SStefan Hajnoczi 191750f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 19185d6e96efSStefan Hajnoczi { 19195d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19205d6e96efSStefan Hajnoczi 19215d6e96efSStefan Hajnoczi if (state->aio_context) { 19221fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 19235d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 19245d6e96efSStefan Hajnoczi } 19255d6e96efSStefan Hajnoczi } 19265d6e96efSStefan Hajnoczi 1927bd8baecdSFam Zheng typedef struct BlockdevBackupState { 192850f43f0fSJohn Snow BlkActionState common; 1929bd8baecdSFam Zheng BlockDriverState *bs; 1930bd8baecdSFam Zheng BlockJob *job; 1931bd8baecdSFam Zheng AioContext *aio_context; 1932bd8baecdSFam Zheng } BlockdevBackupState; 1933bd8baecdSFam Zheng 193478f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target, 193578f51fdeSJohn Snow enum MirrorSyncMode sync, 193678f51fdeSJohn Snow bool has_speed, int64_t speed, 193778f51fdeSJohn Snow bool has_on_source_error, 193878f51fdeSJohn Snow BlockdevOnError on_source_error, 193978f51fdeSJohn Snow bool has_on_target_error, 194078f51fdeSJohn Snow BlockdevOnError on_target_error, 194178f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 194278f51fdeSJohn Snow 194350f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1944bd8baecdSFam Zheng { 1945bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1946bd8baecdSFam Zheng BlockdevBackup *backup; 19475433c24fSMax Reitz BlockBackend *blk, *target; 1948bd8baecdSFam Zheng Error *local_err = NULL; 1949bd8baecdSFam Zheng 19506a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 195132bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1952bd8baecdSFam Zheng 1953a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1954a0e8544cSFam Zheng if (!blk) { 19555b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->device); 1956bd8baecdSFam Zheng return; 1957bd8baecdSFam Zheng } 1958bd8baecdSFam Zheng 1959ff52bf36SFam Zheng if (!blk_is_available(blk)) { 1960ff52bf36SFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1961ff52bf36SFam Zheng return; 1962ff52bf36SFam Zheng } 1963ff52bf36SFam Zheng 19645433c24fSMax Reitz target = blk_by_name(backup->target); 19655433c24fSMax Reitz if (!target) { 19665b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->target); 1967bd8baecdSFam Zheng return; 1968bd8baecdSFam Zheng } 1969bd8baecdSFam Zheng 1970bd8baecdSFam Zheng /* AioContext is released in .clean() */ 19715433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 19725433c24fSMax Reitz if (state->aio_context != blk_get_aio_context(target)) { 1973bd8baecdSFam Zheng state->aio_context = NULL; 1974bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1975bd8baecdSFam Zheng return; 1976bd8baecdSFam Zheng } 1977bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1978ff52bf36SFam Zheng state->bs = blk_bs(blk); 1979ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1980bd8baecdSFam Zheng 198178f51fdeSJohn Snow do_blockdev_backup(backup->device, backup->target, 1982bd8baecdSFam Zheng backup->sync, 1983bd8baecdSFam Zheng backup->has_speed, backup->speed, 1984bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1985bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 198694d16a64SJohn Snow common->block_job_txn, &local_err); 1987bd8baecdSFam Zheng if (local_err) { 1988bd8baecdSFam Zheng error_propagate(errp, local_err); 1989bd8baecdSFam Zheng return; 1990bd8baecdSFam Zheng } 1991bd8baecdSFam Zheng 1992bd8baecdSFam Zheng state->job = state->bs->job; 1993bd8baecdSFam Zheng } 1994bd8baecdSFam Zheng 199550f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1996bd8baecdSFam Zheng { 1997bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1998bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1999bd8baecdSFam Zheng 2000bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 2001bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 2002bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 2003bd8baecdSFam Zheng } 2004bd8baecdSFam Zheng } 2005bd8baecdSFam Zheng 200650f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 2007bd8baecdSFam Zheng { 2008bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 2009bd8baecdSFam Zheng 2010bd8baecdSFam Zheng if (state->aio_context) { 2011ff52bf36SFam Zheng bdrv_drained_end(state->bs); 2012bd8baecdSFam Zheng aio_context_release(state->aio_context); 2013bd8baecdSFam Zheng } 2014bd8baecdSFam Zheng } 2015bd8baecdSFam Zheng 2016df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 201750f43f0fSJohn Snow BlkActionState common; 2018df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 2019df9a681dSFam Zheng BlockDriverState *bs; 2020df9a681dSFam Zheng AioContext *aio_context; 2021df9a681dSFam Zheng HBitmap *backup; 2022df9a681dSFam Zheng bool prepared; 2023df9a681dSFam Zheng } BlockDirtyBitmapState; 2024df9a681dSFam Zheng 202550f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 2026df9a681dSFam Zheng Error **errp) 2027df9a681dSFam Zheng { 2028df9a681dSFam Zheng Error *local_err = NULL; 2029df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2030df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2031df9a681dSFam Zheng common, common); 2032df9a681dSFam Zheng 203394d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 203494d16a64SJohn Snow return; 203594d16a64SJohn Snow } 203694d16a64SJohn Snow 203732bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2038df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2039df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2040df9a681dSFam Zheng action->has_granularity, action->granularity, 2041df9a681dSFam Zheng &local_err); 2042df9a681dSFam Zheng 2043df9a681dSFam Zheng if (!local_err) { 2044df9a681dSFam Zheng state->prepared = true; 2045df9a681dSFam Zheng } else { 2046df9a681dSFam Zheng error_propagate(errp, local_err); 2047df9a681dSFam Zheng } 2048df9a681dSFam Zheng } 2049df9a681dSFam Zheng 205050f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2051df9a681dSFam Zheng { 2052df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2053df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2054df9a681dSFam Zheng common, common); 2055df9a681dSFam Zheng 205632bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2057df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2058df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2059df9a681dSFam Zheng */ 2060df9a681dSFam Zheng if (state->prepared) { 2061df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2062df9a681dSFam Zheng } 2063df9a681dSFam Zheng } 2064df9a681dSFam Zheng 206550f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2066df9a681dSFam Zheng Error **errp) 2067df9a681dSFam Zheng { 2068df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2069df9a681dSFam Zheng common, common); 2070df9a681dSFam Zheng BlockDirtyBitmap *action; 2071df9a681dSFam Zheng 207294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 207394d16a64SJohn Snow return; 207494d16a64SJohn Snow } 207594d16a64SJohn Snow 207632bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2077df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2078df9a681dSFam Zheng action->name, 2079df9a681dSFam Zheng &state->bs, 2080df9a681dSFam Zheng &state->aio_context, 2081df9a681dSFam Zheng errp); 2082df9a681dSFam Zheng if (!state->bitmap) { 2083df9a681dSFam Zheng return; 2084df9a681dSFam Zheng } 2085df9a681dSFam Zheng 2086df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2087df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2088df9a681dSFam Zheng return; 2089df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2090df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2091df9a681dSFam Zheng return; 2092df9a681dSFam Zheng } 2093df9a681dSFam Zheng 2094df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2095df9a681dSFam Zheng /* AioContext is released in .clean() */ 2096df9a681dSFam Zheng } 2097df9a681dSFam Zheng 209850f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2099df9a681dSFam Zheng { 2100df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2101df9a681dSFam Zheng common, common); 2102df9a681dSFam Zheng 2103df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2104df9a681dSFam Zheng } 2105df9a681dSFam Zheng 210650f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2107df9a681dSFam Zheng { 2108df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2109df9a681dSFam Zheng common, common); 2110df9a681dSFam Zheng 2111df9a681dSFam Zheng hbitmap_free(state->backup); 2112df9a681dSFam Zheng } 2113df9a681dSFam Zheng 211450f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2115df9a681dSFam Zheng { 2116df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2117df9a681dSFam Zheng common, common); 2118df9a681dSFam Zheng 2119df9a681dSFam Zheng if (state->aio_context) { 2120df9a681dSFam Zheng aio_context_release(state->aio_context); 2121df9a681dSFam Zheng } 2122df9a681dSFam Zheng } 2123df9a681dSFam Zheng 212450f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 212578b18b78SStefan Hajnoczi { 212678b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 212778b18b78SStefan Hajnoczi } 212878b18b78SStefan Hajnoczi 212950f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 213078b18b78SStefan Hajnoczi { 2131dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 213278b18b78SStefan Hajnoczi } 213378b18b78SStefan Hajnoczi 213450f43f0fSJohn Snow static const BlkActionOps actions[] = { 213543de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 213643de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 213743de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 213843de7e2dSAlberto Garcia .commit = external_snapshot_commit, 213943de7e2dSAlberto Garcia .abort = external_snapshot_abort, 21404ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 214143de7e2dSAlberto Garcia }, 2142c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2143ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2144ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2145ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2146ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2147da763e83SFam Zheng .clean = external_snapshot_clean, 2148ba0c86a3SWenchao Xia }, 21493037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21503037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21513037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 21523037f364SStefan Hajnoczi .abort = drive_backup_abort, 21535d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21543037f364SStefan Hajnoczi }, 2155bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2156bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2157bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2158bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2159bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2160bd8baecdSFam Zheng }, 216178b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 216250f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 216378b18b78SStefan Hajnoczi .prepare = abort_prepare, 216478b18b78SStefan Hajnoczi .commit = abort_commit, 216578b18b78SStefan Hajnoczi }, 2166bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2167bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2168bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2169bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21705d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2171bbe86010SWenchao Xia }, 2172df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2173df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2174df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2175df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2176df9a681dSFam Zheng }, 2177df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2178df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2179df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2180df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2181df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2182df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2183df9a681dSFam Zheng } 2184ba0c86a3SWenchao Xia }; 2185ba0c86a3SWenchao Xia 218694d16a64SJohn Snow /** 218794d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 218894d16a64SJohn Snow * that structure with desired defaults if they are unset. 218994d16a64SJohn Snow */ 219094d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 219194d16a64SJohn Snow TransactionProperties *props) 219294d16a64SJohn Snow { 219394d16a64SJohn Snow if (!props) { 219494d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 219594d16a64SJohn Snow } 219694d16a64SJohn Snow 219794d16a64SJohn Snow if (!props->has_completion_mode) { 219894d16a64SJohn Snow props->has_completion_mode = true; 219994d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 220094d16a64SJohn Snow } 220194d16a64SJohn Snow 220294d16a64SJohn Snow return props; 220394d16a64SJohn Snow } 220494d16a64SJohn Snow 22058802d1fdSJeff Cody /* 2206b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2207b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 22088802d1fdSJeff Cody */ 220994d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 221094d16a64SJohn Snow bool has_props, 221194d16a64SJohn Snow struct TransactionProperties *props, 221294d16a64SJohn Snow Error **errp) 22138802d1fdSJeff Cody { 2214c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 221594d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 221650f43f0fSJohn Snow BlkActionState *state, *next; 221743e17041SLuiz Capitulino Error *local_err = NULL; 22188802d1fdSJeff Cody 221950f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 22208802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 22218802d1fdSJeff Cody 222294d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 222394d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 222494d16a64SJohn Snow */ 222594d16a64SJohn Snow props = get_transaction_properties(props); 222694d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 222794d16a64SJohn Snow block_job_txn = block_job_txn_new(); 222894d16a64SJohn Snow } 222994d16a64SJohn Snow 2230b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 22318802d1fdSJeff Cody bdrv_drain_all(); 22328802d1fdSJeff Cody 2233b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 22348802d1fdSJeff Cody while (NULL != dev_entry) { 2235c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 223650f43f0fSJohn Snow const BlkActionOps *ops; 223752e7c241SPaolo Bonzini 22388802d1fdSJeff Cody dev_info = dev_entry->value; 22398802d1fdSJeff Cody dev_entry = dev_entry->next; 22408802d1fdSJeff Cody 22416a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2242ba0c86a3SWenchao Xia 22436a8f9661SEric Blake ops = &actions[dev_info->type]; 2244aa3fe714SMax Reitz assert(ops->instance_size > 0); 2245aa3fe714SMax Reitz 2246ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2247ba5d6ab6SStefan Hajnoczi state->ops = ops; 2248ba5d6ab6SStefan Hajnoczi state->action = dev_info; 224994d16a64SJohn Snow state->block_job_txn = block_job_txn; 225094d16a64SJohn Snow state->txn_props = props; 2251ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22528802d1fdSJeff Cody 2253ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 225484d18f06SMarkus Armbruster if (local_err) { 22559b9877eeSWenchao Xia error_propagate(errp, local_err); 22569b9877eeSWenchao Xia goto delete_and_fail; 22579b9877eeSWenchao Xia } 225852e7c241SPaolo Bonzini } 22598802d1fdSJeff Cody 2260ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2261f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2262ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22638802d1fdSJeff Cody } 2264f9ea81e8SStefan Hajnoczi } 22658802d1fdSJeff Cody 22668802d1fdSJeff Cody /* success */ 22678802d1fdSJeff Cody goto exit; 22688802d1fdSJeff Cody 22698802d1fdSJeff Cody delete_and_fail: 2270b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2271ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2272ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2273ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2274ba0c86a3SWenchao Xia } 22758802d1fdSJeff Cody } 22768802d1fdSJeff Cody exit: 2277ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2278ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2279ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2280ba0c86a3SWenchao Xia } 2281ba5d6ab6SStefan Hajnoczi g_free(state); 22828802d1fdSJeff Cody } 228394d16a64SJohn Snow if (!has_props) { 228494d16a64SJohn Snow qapi_free_TransactionProperties(props); 228594d16a64SJohn Snow } 228694d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22878802d1fdSJeff Cody } 22888802d1fdSJeff Cody 2289c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 2290666daa68SMarkus Armbruster { 229138f54bd1SMax Reitz Error *local_err = NULL; 2292666daa68SMarkus Armbruster 229338f54bd1SMax Reitz qmp_blockdev_open_tray(device, has_force, force, &local_err); 229438f54bd1SMax Reitz if (local_err) { 229538f54bd1SMax Reitz error_propagate(errp, local_err); 2296c245b6a3SLuiz Capitulino return; 2297666daa68SMarkus Armbruster } 229892d48558SLuiz Capitulino 22996e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, errp); 2300666daa68SMarkus Armbruster } 2301666daa68SMarkus Armbruster 230212d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 230312d3ba82SBenoît Canet bool has_node_name, const char *node_name, 230412d3ba82SBenoît Canet const char *password, Error **errp) 2305666daa68SMarkus Armbruster { 230612d3ba82SBenoît Canet Error *local_err = NULL; 2307666daa68SMarkus Armbruster BlockDriverState *bs; 2308e3442099SStefan Hajnoczi AioContext *aio_context; 2309666daa68SMarkus Armbruster 231012d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 231112d3ba82SBenoît Canet has_node_name ? node_name : NULL, 231212d3ba82SBenoît Canet &local_err); 231384d18f06SMarkus Armbruster if (local_err) { 231412d3ba82SBenoît Canet error_propagate(errp, local_err); 2315a4dea8a9SLuiz Capitulino return; 2316666daa68SMarkus Armbruster } 2317666daa68SMarkus Armbruster 2318e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2319e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2320e3442099SStefan Hajnoczi 23214d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2322666daa68SMarkus Armbruster 2323e3442099SStefan Hajnoczi aio_context_release(aio_context); 2324e3442099SStefan Hajnoczi } 2325e3442099SStefan Hajnoczi 23267d8a9f71SMax Reitz void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 23277d8a9f71SMax Reitz Error **errp) 23287d8a9f71SMax Reitz { 23297d8a9f71SMax Reitz BlockBackend *blk; 23307d8a9f71SMax Reitz bool locked; 23317d8a9f71SMax Reitz 23327d8a9f71SMax Reitz if (!has_force) { 23337d8a9f71SMax Reitz force = false; 23347d8a9f71SMax Reitz } 23357d8a9f71SMax Reitz 23367d8a9f71SMax Reitz blk = blk_by_name(device); 23377d8a9f71SMax Reitz if (!blk) { 23387d8a9f71SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 23397d8a9f71SMax Reitz "Device '%s' not found", device); 23407d8a9f71SMax Reitz return; 23417d8a9f71SMax Reitz } 23427d8a9f71SMax Reitz 23437d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23447d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23457d8a9f71SMax Reitz return; 23467d8a9f71SMax Reitz } 23477d8a9f71SMax Reitz 234812c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 234912c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 235012c7ec87SMax Reitz return; 235112c7ec87SMax Reitz } 235212c7ec87SMax Reitz 23537d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23547d8a9f71SMax Reitz return; 23557d8a9f71SMax Reitz } 23567d8a9f71SMax Reitz 23577d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23587d8a9f71SMax Reitz if (locked) { 23597d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23607d8a9f71SMax Reitz } 23617d8a9f71SMax Reitz 23627d8a9f71SMax Reitz if (!locked || force) { 23637d8a9f71SMax Reitz blk_dev_change_media_cb(blk, false); 23647d8a9f71SMax Reitz } 23657d8a9f71SMax Reitz } 23667d8a9f71SMax Reitz 2367abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2368abaaf59dSMax Reitz { 2369abaaf59dSMax Reitz BlockBackend *blk; 2370abaaf59dSMax Reitz 2371abaaf59dSMax Reitz blk = blk_by_name(device); 2372abaaf59dSMax Reitz if (!blk) { 2373abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2374abaaf59dSMax Reitz "Device '%s' not found", device); 2375abaaf59dSMax Reitz return; 2376abaaf59dSMax Reitz } 2377abaaf59dSMax Reitz 2378abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2379abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2380abaaf59dSMax Reitz return; 2381abaaf59dSMax Reitz } 2382abaaf59dSMax Reitz 238312c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 238412c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 238512c7ec87SMax Reitz return; 238612c7ec87SMax Reitz } 238712c7ec87SMax Reitz 2388abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2389abaaf59dSMax Reitz return; 2390abaaf59dSMax Reitz } 2391abaaf59dSMax Reitz 2392abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2393abaaf59dSMax Reitz } 2394abaaf59dSMax Reitz 23956e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 23962814f672SMax Reitz { 23972814f672SMax Reitz BlockBackend *blk; 23982814f672SMax Reitz BlockDriverState *bs; 23992814f672SMax Reitz AioContext *aio_context; 24002814f672SMax Reitz bool has_device; 24012814f672SMax Reitz 24022814f672SMax Reitz blk = blk_by_name(device); 24032814f672SMax Reitz if (!blk) { 24042814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 24052814f672SMax Reitz "Device '%s' not found", device); 24062814f672SMax Reitz return; 24072814f672SMax Reitz } 24082814f672SMax Reitz 24092814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 24102814f672SMax Reitz has_device = blk_get_attached_dev(blk); 24112814f672SMax Reitz 24122814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 24132814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 24142814f672SMax Reitz return; 24152814f672SMax Reitz } 24162814f672SMax Reitz 241712c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 24182814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 24192814f672SMax Reitz return; 24202814f672SMax Reitz } 24212814f672SMax Reitz 24222814f672SMax Reitz bs = blk_bs(blk); 24232814f672SMax Reitz if (!bs) { 24242814f672SMax Reitz return; 24252814f672SMax Reitz } 24262814f672SMax Reitz 24272814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 24282814f672SMax Reitz aio_context_acquire(aio_context); 24292814f672SMax Reitz 24302814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 24312814f672SMax Reitz goto out; 24322814f672SMax Reitz } 24332814f672SMax Reitz 24342814f672SMax Reitz blk_remove_bs(blk); 24352814f672SMax Reitz 243612c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 243712c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 243812c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 243912c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 244012c7ec87SMax Reitz * value passed here (i.e. false). */ 244112c7ec87SMax Reitz blk_dev_change_media_cb(blk, false); 244212c7ec87SMax Reitz } 244312c7ec87SMax Reitz 24442814f672SMax Reitz out: 24452814f672SMax Reitz aio_context_release(aio_context); 24462814f672SMax Reitz } 24472814f672SMax Reitz 2448d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device, 2449d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2450d1299882SMax Reitz { 2451d1299882SMax Reitz BlockBackend *blk; 2452d1299882SMax Reitz bool has_device; 2453d1299882SMax Reitz 2454d1299882SMax Reitz blk = blk_by_name(device); 2455d1299882SMax Reitz if (!blk) { 2456d1299882SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2457d1299882SMax Reitz "Device '%s' not found", device); 2458d1299882SMax Reitz return; 2459d1299882SMax Reitz } 2460d1299882SMax Reitz 2461d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2462d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2463d1299882SMax Reitz 2464d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2465d1299882SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2466d1299882SMax Reitz return; 2467d1299882SMax Reitz } 2468d1299882SMax Reitz 246912c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2470d1299882SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2471d1299882SMax Reitz return; 2472d1299882SMax Reitz } 2473d1299882SMax Reitz 2474d1299882SMax Reitz if (blk_bs(blk)) { 2475d1299882SMax Reitz error_setg(errp, "There already is a medium in device '%s'", device); 2476d1299882SMax Reitz return; 2477d1299882SMax Reitz } 2478d1299882SMax Reitz 2479d1299882SMax Reitz blk_insert_bs(blk, bs); 2480d1299882SMax Reitz 248112c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 248212c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 248312c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 248412c7ec87SMax Reitz * slot here. 248512c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 248612c7ec87SMax Reitz * value passed here (i.e. true). */ 248712c7ec87SMax Reitz blk_dev_change_media_cb(blk, true); 248812c7ec87SMax Reitz } 2489d1299882SMax Reitz } 2490d1299882SMax Reitz 24916e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2492d1299882SMax Reitz Error **errp) 2493d1299882SMax Reitz { 2494d1299882SMax Reitz BlockDriverState *bs; 2495d1299882SMax Reitz 2496d1299882SMax Reitz bs = bdrv_find_node(node_name); 2497d1299882SMax Reitz if (!bs) { 2498d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2499d1299882SMax Reitz return; 2500d1299882SMax Reitz } 2501d1299882SMax Reitz 2502d1299882SMax Reitz if (bs->blk) { 2503d1299882SMax Reitz error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 2504d1299882SMax Reitz blk_name(bs->blk)); 2505d1299882SMax Reitz return; 2506d1299882SMax Reitz } 2507d1299882SMax Reitz 2508d1299882SMax Reitz qmp_blockdev_insert_anon_medium(device, bs, errp); 2509d1299882SMax Reitz } 2510d1299882SMax Reitz 251124fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename, 251224fb4133SMax Reitz bool has_format, const char *format, 251339ff43d9SMax Reitz bool has_read_only, 251439ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 251524fb4133SMax Reitz Error **errp) 2516de2c6c05SMax Reitz { 2517de2c6c05SMax Reitz BlockBackend *blk; 2518de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 2519de2c6c05SMax Reitz int bdrv_flags, ret; 2520de2c6c05SMax Reitz QDict *options = NULL; 2521de2c6c05SMax Reitz Error *err = NULL; 2522de2c6c05SMax Reitz 2523de2c6c05SMax Reitz blk = blk_by_name(device); 2524de2c6c05SMax Reitz if (!blk) { 2525de2c6c05SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2526de2c6c05SMax Reitz "Device '%s' not found", device); 2527de2c6c05SMax Reitz goto fail; 2528de2c6c05SMax Reitz } 2529de2c6c05SMax Reitz 2530de2c6c05SMax Reitz if (blk_bs(blk)) { 2531de2c6c05SMax Reitz blk_update_root_state(blk); 2532de2c6c05SMax Reitz } 2533de2c6c05SMax Reitz 2534de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2535156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2536156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2537de2c6c05SMax Reitz 253839ff43d9SMax Reitz if (!has_read_only) { 253939ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 254039ff43d9SMax Reitz } 254139ff43d9SMax Reitz 254239ff43d9SMax Reitz switch (read_only) { 254339ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 254439ff43d9SMax Reitz break; 254539ff43d9SMax Reitz 254639ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 254739ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 254839ff43d9SMax Reitz break; 254939ff43d9SMax Reitz 255039ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 255139ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 255239ff43d9SMax Reitz break; 255339ff43d9SMax Reitz 255439ff43d9SMax Reitz default: 255539ff43d9SMax Reitz abort(); 255639ff43d9SMax Reitz } 255739ff43d9SMax Reitz 255824fb4133SMax Reitz if (has_format) { 2559de2c6c05SMax Reitz options = qdict_new(); 2560de2c6c05SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2561de2c6c05SMax Reitz } 2562de2c6c05SMax Reitz 2563de2c6c05SMax Reitz assert(!medium_bs); 2564de2c6c05SMax Reitz ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp); 2565de2c6c05SMax Reitz if (ret < 0) { 2566de2c6c05SMax Reitz goto fail; 2567de2c6c05SMax Reitz } 2568de2c6c05SMax Reitz 2569de2c6c05SMax Reitz blk_apply_root_state(blk, medium_bs); 2570de2c6c05SMax Reitz 2571de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2572de2c6c05SMax Reitz if (err) { 2573de2c6c05SMax Reitz error_propagate(errp, err); 2574de2c6c05SMax Reitz goto fail; 2575de2c6c05SMax Reitz } 2576de2c6c05SMax Reitz 2577de2c6c05SMax Reitz qmp_blockdev_open_tray(device, false, false, &err); 2578de2c6c05SMax Reitz if (err) { 2579de2c6c05SMax Reitz error_propagate(errp, err); 2580de2c6c05SMax Reitz goto fail; 2581de2c6c05SMax Reitz } 2582de2c6c05SMax Reitz 25836e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, &err); 2584de2c6c05SMax Reitz if (err) { 2585de2c6c05SMax Reitz error_propagate(errp, err); 2586de2c6c05SMax Reitz goto fail; 2587de2c6c05SMax Reitz } 2588de2c6c05SMax Reitz 2589de2c6c05SMax Reitz qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2590de2c6c05SMax Reitz if (err) { 2591de2c6c05SMax Reitz error_propagate(errp, err); 2592de2c6c05SMax Reitz goto fail; 2593de2c6c05SMax Reitz } 2594de2c6c05SMax Reitz 2595de2c6c05SMax Reitz qmp_blockdev_close_tray(device, errp); 2596de2c6c05SMax Reitz 2597de2c6c05SMax Reitz fail: 2598de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2599de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2600de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2601de2c6c05SMax Reitz bdrv_unref(medium_bs); 2602de2c6c05SMax Reitz } 2603de2c6c05SMax Reitz 2604727f005eSZhi Yong Wu /* throttling disk I/O limits */ 260580047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 26063e9fab69SBenoît Canet int64_t bps_wr, 26073e9fab69SBenoît Canet int64_t iops, 26083e9fab69SBenoît Canet int64_t iops_rd, 26093e9fab69SBenoît Canet int64_t iops_wr, 26103e9fab69SBenoît Canet bool has_bps_max, 26113e9fab69SBenoît Canet int64_t bps_max, 26123e9fab69SBenoît Canet bool has_bps_rd_max, 26133e9fab69SBenoît Canet int64_t bps_rd_max, 26143e9fab69SBenoît Canet bool has_bps_wr_max, 26153e9fab69SBenoît Canet int64_t bps_wr_max, 26163e9fab69SBenoît Canet bool has_iops_max, 26173e9fab69SBenoît Canet int64_t iops_max, 26183e9fab69SBenoît Canet bool has_iops_rd_max, 26193e9fab69SBenoît Canet int64_t iops_rd_max, 26203e9fab69SBenoît Canet bool has_iops_wr_max, 26212024c1dfSBenoît Canet int64_t iops_wr_max, 2622dce13204SAlberto Garcia bool has_bps_max_length, 2623dce13204SAlberto Garcia int64_t bps_max_length, 2624dce13204SAlberto Garcia bool has_bps_rd_max_length, 2625dce13204SAlberto Garcia int64_t bps_rd_max_length, 2626dce13204SAlberto Garcia bool has_bps_wr_max_length, 2627dce13204SAlberto Garcia int64_t bps_wr_max_length, 2628dce13204SAlberto Garcia bool has_iops_max_length, 2629dce13204SAlberto Garcia int64_t iops_max_length, 2630dce13204SAlberto Garcia bool has_iops_rd_max_length, 2631dce13204SAlberto Garcia int64_t iops_rd_max_length, 2632dce13204SAlberto Garcia bool has_iops_wr_max_length, 2633dce13204SAlberto Garcia int64_t iops_wr_max_length, 26342024c1dfSBenoît Canet bool has_iops_size, 263576f4afb4SAlberto Garcia int64_t iops_size, 263676f4afb4SAlberto Garcia bool has_group, 263776f4afb4SAlberto Garcia const char *group, Error **errp) 2638727f005eSZhi Yong Wu { 2639cc0681c4SBenoît Canet ThrottleConfig cfg; 2640727f005eSZhi Yong Wu BlockDriverState *bs; 2641a0e8544cSFam Zheng BlockBackend *blk; 2642b15446fdSStefan Hajnoczi AioContext *aio_context; 2643727f005eSZhi Yong Wu 2644a0e8544cSFam Zheng blk = blk_by_name(device); 2645a0e8544cSFam Zheng if (!blk) { 264675158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 264775158ebbSMarkus Armbruster "Device '%s' not found", device); 264880047da5SLuiz Capitulino return; 2649727f005eSZhi Yong Wu } 26505433c24fSMax Reitz 26515433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 26525433c24fSMax Reitz aio_context_acquire(aio_context); 26535433c24fSMax Reitz 2654a0e8544cSFam Zheng bs = blk_bs(blk); 26555433c24fSMax Reitz if (!bs) { 26565433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 26575433c24fSMax Reitz goto out; 26585433c24fSMax Reitz } 2659727f005eSZhi Yong Wu 26601588ab5dSAlberto Garcia throttle_config_init(&cfg); 2661cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2662cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2663cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2664727f005eSZhi Yong Wu 2665cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2666cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2667cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2668cc0681c4SBenoît Canet 26693e9fab69SBenoît Canet if (has_bps_max) { 26703e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 26713e9fab69SBenoît Canet } 26723e9fab69SBenoît Canet if (has_bps_rd_max) { 26733e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 26743e9fab69SBenoît Canet } 26753e9fab69SBenoît Canet if (has_bps_wr_max) { 26763e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 26773e9fab69SBenoît Canet } 26783e9fab69SBenoît Canet if (has_iops_max) { 26793e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 26803e9fab69SBenoît Canet } 26813e9fab69SBenoît Canet if (has_iops_rd_max) { 26823e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 26833e9fab69SBenoît Canet } 26843e9fab69SBenoît Canet if (has_iops_wr_max) { 26853e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 26863e9fab69SBenoît Canet } 2687cc0681c4SBenoît Canet 2688dce13204SAlberto Garcia if (has_bps_max_length) { 2689dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = bps_max_length; 2690dce13204SAlberto Garcia } 2691dce13204SAlberto Garcia if (has_bps_rd_max_length) { 2692dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_READ].burst_length = bps_rd_max_length; 2693dce13204SAlberto Garcia } 2694dce13204SAlberto Garcia if (has_bps_wr_max_length) { 2695dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_WRITE].burst_length = bps_wr_max_length; 2696dce13204SAlberto Garcia } 2697dce13204SAlberto Garcia if (has_iops_max_length) { 2698dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = iops_max_length; 2699dce13204SAlberto Garcia } 2700dce13204SAlberto Garcia if (has_iops_rd_max_length) { 2701dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_READ].burst_length = iops_rd_max_length; 2702dce13204SAlberto Garcia } 2703dce13204SAlberto Garcia if (has_iops_wr_max_length) { 2704dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_WRITE].burst_length = iops_wr_max_length; 2705dce13204SAlberto Garcia } 2706dce13204SAlberto Garcia 27072024c1dfSBenoît Canet if (has_iops_size) { 27082024c1dfSBenoît Canet cfg.op_size = iops_size; 27092024c1dfSBenoît Canet } 2710cc0681c4SBenoît Canet 2711d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 27125433c24fSMax Reitz goto out; 2713727f005eSZhi Yong Wu } 2714727f005eSZhi Yong Wu 271576f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 271676f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 271776f4afb4SAlberto Garcia * just update the throttling group. */ 2718a0d64a61SAlberto Garcia if (!bs->throttle_state) { 271976f4afb4SAlberto Garcia bdrv_io_limits_enable(bs, has_group ? group : device); 272076f4afb4SAlberto Garcia } else if (has_group) { 272176f4afb4SAlberto Garcia bdrv_io_limits_update_group(bs, group); 2722727f005eSZhi Yong Wu } 272376f4afb4SAlberto Garcia /* Set the new throttling configuration */ 2724cc0681c4SBenoît Canet bdrv_set_io_limits(bs, &cfg); 2725a0d64a61SAlberto Garcia } else if (bs->throttle_state) { 272676f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 272776f4afb4SAlberto Garcia bdrv_io_limits_disable(bs); 2728727f005eSZhi Yong Wu } 2729b15446fdSStefan Hajnoczi 27305433c24fSMax Reitz out: 2731b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2732727f005eSZhi Yong Wu } 2733727f005eSZhi Yong Wu 2734341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2735341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2736341ebc2fSJohn Snow Error **errp) 2737341ebc2fSJohn Snow { 2738341ebc2fSJohn Snow AioContext *aio_context; 2739341ebc2fSJohn Snow BlockDriverState *bs; 2740341ebc2fSJohn Snow 2741341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2742341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2743341ebc2fSJohn Snow return; 2744341ebc2fSJohn Snow } 2745341ebc2fSJohn Snow 2746341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2747341ebc2fSJohn Snow if (!bs) { 2748341ebc2fSJohn Snow return; 2749341ebc2fSJohn Snow } 2750341ebc2fSJohn Snow 2751341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2752341ebc2fSJohn Snow aio_context_acquire(aio_context); 2753341ebc2fSJohn Snow 2754341ebc2fSJohn Snow if (has_granularity) { 2755341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2756341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2757341ebc2fSJohn Snow "and at least 512"); 2758341ebc2fSJohn Snow goto out; 2759341ebc2fSJohn Snow } 2760341ebc2fSJohn Snow } else { 2761341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2762341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2763341ebc2fSJohn Snow } 2764341ebc2fSJohn Snow 2765341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2766341ebc2fSJohn Snow 2767341ebc2fSJohn Snow out: 2768341ebc2fSJohn Snow aio_context_release(aio_context); 2769341ebc2fSJohn Snow } 2770341ebc2fSJohn Snow 2771341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2772341ebc2fSJohn Snow Error **errp) 2773341ebc2fSJohn Snow { 2774341ebc2fSJohn Snow AioContext *aio_context; 2775341ebc2fSJohn Snow BlockDriverState *bs; 2776341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2777341ebc2fSJohn Snow 2778341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2779341ebc2fSJohn Snow if (!bitmap || !bs) { 2780341ebc2fSJohn Snow return; 2781341ebc2fSJohn Snow } 2782341ebc2fSJohn Snow 27839bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 27849bd2b08fSJohn Snow error_setg(errp, 27859bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 27869bd2b08fSJohn Snow name); 27879bd2b08fSJohn Snow goto out; 27889bd2b08fSJohn Snow } 278920dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2790341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2791341ebc2fSJohn Snow 27929bd2b08fSJohn Snow out: 2793341ebc2fSJohn Snow aio_context_release(aio_context); 2794341ebc2fSJohn Snow } 2795341ebc2fSJohn Snow 2796e74e6b78SJohn Snow /** 2797e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2798e74e6b78SJohn Snow * immediately after a full backup operation. 2799e74e6b78SJohn Snow */ 2800e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2801e74e6b78SJohn Snow Error **errp) 2802e74e6b78SJohn Snow { 2803e74e6b78SJohn Snow AioContext *aio_context; 2804e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2805e74e6b78SJohn Snow BlockDriverState *bs; 2806e74e6b78SJohn Snow 2807e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2808e74e6b78SJohn Snow if (!bitmap || !bs) { 2809e74e6b78SJohn Snow return; 2810e74e6b78SJohn Snow } 2811e74e6b78SJohn Snow 2812e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2813e74e6b78SJohn Snow error_setg(errp, 2814e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2815e74e6b78SJohn Snow name); 2816e74e6b78SJohn Snow goto out; 2817e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2818e74e6b78SJohn Snow error_setg(errp, 2819e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2820e74e6b78SJohn Snow name); 2821e74e6b78SJohn Snow goto out; 2822e74e6b78SJohn Snow } 2823e74e6b78SJohn Snow 2824df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2825e74e6b78SJohn Snow 2826e74e6b78SJohn Snow out: 2827e74e6b78SJohn Snow aio_context_release(aio_context); 2828e74e6b78SJohn Snow } 2829e74e6b78SJohn Snow 2830072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 28319063f814SRyan Harper { 28329063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 28337e7d56d9SMarkus Armbruster BlockBackend *blk; 28349063f814SRyan Harper BlockDriverState *bs; 28358ad4202bSStefan Hajnoczi AioContext *aio_context; 28363718d8abSFam Zheng Error *local_err = NULL; 28379063f814SRyan Harper 28382073d410SKevin Wolf bs = bdrv_find_node(id); 28392073d410SKevin Wolf if (bs) { 28402073d410SKevin Wolf qmp_x_blockdev_del(false, NULL, true, id, &local_err); 28412073d410SKevin Wolf if (local_err) { 28422073d410SKevin Wolf error_report_err(local_err); 28432073d410SKevin Wolf } 28442073d410SKevin Wolf return; 28452073d410SKevin Wolf } 28462073d410SKevin Wolf 28477e7d56d9SMarkus Armbruster blk = blk_by_name(id); 28487e7d56d9SMarkus Armbruster if (!blk) { 2849b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2850072ebe6bSMarkus Armbruster return; 28519063f814SRyan Harper } 28528ad4202bSStefan Hajnoczi 285326f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 285448f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 285548f364ddSMarkus Armbruster " is not supported"); 2856072ebe6bSMarkus Armbruster return; 285748f364ddSMarkus Armbruster } 285848f364ddSMarkus Armbruster 28595433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 28608ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 28618ad4202bSStefan Hajnoczi 28625433c24fSMax Reitz bs = blk_bs(blk); 28635433c24fSMax Reitz if (bs) { 28643718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2865565f65d2SMarkus Armbruster error_report_err(local_err); 28668ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2867072ebe6bSMarkus Armbruster return; 28688591675fSMarcelo Tosatti } 28699063f814SRyan Harper 2870938abd43SMax Reitz blk_remove_bs(blk); 28715433c24fSMax Reitz } 28729063f814SRyan Harper 28737c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 2874efaa7c4eSMax Reitz monitor_remove_blk(blk); 2875efaa7c4eSMax Reitz 28767c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 28777c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 2878d22b2f41SRyan Harper */ 2879a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 2880293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2881373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2882293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2883d22b2f41SRyan Harper } else { 2884b9fe8a7aSMarkus Armbruster blk_unref(blk); 2885d22b2f41SRyan Harper } 28869063f814SRyan Harper 28878ad4202bSStefan Hajnoczi aio_context_release(aio_context); 28889063f814SRyan Harper } 28896d4a2b3aSChristoph Hellwig 28903b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 28913b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 28923b1dbd11SBenoît Canet int64_t size, Error **errp) 28936d4a2b3aSChristoph Hellwig { 28943b1dbd11SBenoît Canet Error *local_err = NULL; 28956d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2896927e0e76SStefan Hajnoczi AioContext *aio_context; 28978732901eSKevin Wolf int ret; 28986d4a2b3aSChristoph Hellwig 28993b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 29003b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 29013b1dbd11SBenoît Canet &local_err); 290284d18f06SMarkus Armbruster if (local_err) { 29033b1dbd11SBenoît Canet error_propagate(errp, local_err); 29043b1dbd11SBenoît Canet return; 29053b1dbd11SBenoît Canet } 29063b1dbd11SBenoît Canet 2907927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2908927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2909927e0e76SStefan Hajnoczi 29103b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2911c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2912927e0e76SStefan Hajnoczi goto out; 29136d4a2b3aSChristoph Hellwig } 29146d4a2b3aSChristoph Hellwig 29156d4a2b3aSChristoph Hellwig if (size < 0) { 2916c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2917927e0e76SStefan Hajnoczi goto out; 29186d4a2b3aSChristoph Hellwig } 29196d4a2b3aSChristoph Hellwig 29209c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2921c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2922927e0e76SStefan Hajnoczi goto out; 29239c75e168SJeff Cody } 29249c75e168SJeff Cody 292592b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 292692b7a08dSPeter Lieven bdrv_drain_all(); 292792b7a08dSPeter Lieven 29288732901eSKevin Wolf ret = bdrv_truncate(bs, size); 29298732901eSKevin Wolf switch (ret) { 2930939a1cc3SStefan Hajnoczi case 0: 2931939a1cc3SStefan Hajnoczi break; 2932939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2933c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2934939a1cc3SStefan Hajnoczi break; 2935939a1cc3SStefan Hajnoczi case -ENOTSUP: 2936c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2937939a1cc3SStefan Hajnoczi break; 2938939a1cc3SStefan Hajnoczi case -EACCES: 293981e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2940939a1cc3SStefan Hajnoczi break; 2941939a1cc3SStefan Hajnoczi case -EBUSY: 2942c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2943939a1cc3SStefan Hajnoczi break; 2944939a1cc3SStefan Hajnoczi default: 29458732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2946939a1cc3SStefan Hajnoczi break; 29476d4a2b3aSChristoph Hellwig } 2948927e0e76SStefan Hajnoczi 2949927e0e76SStefan Hajnoczi out: 2950927e0e76SStefan Hajnoczi aio_context_release(aio_context); 29516d4a2b3aSChristoph Hellwig } 295212bd451fSStefan Hajnoczi 29539abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 295412bd451fSStefan Hajnoczi { 2955723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2956723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2957723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2958723c5d93SStefan Hajnoczi */ 2959723c5d93SStefan Hajnoczi 296012bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2961bcada37bSWenchao Xia const char *msg = NULL; 296212bd451fSStefan Hajnoczi 29639abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 296412bd451fSStefan Hajnoczi 296512bd451fSStefan Hajnoczi assert(bs->job); 2966bcada37bSWenchao Xia 296712bd451fSStefan Hajnoczi if (ret < 0) { 2968bcada37bSWenchao Xia msg = strerror(-ret); 296912bd451fSStefan Hajnoczi } 297012bd451fSStefan Hajnoczi 2971370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 2972bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 2973370521a1SStefan Hajnoczi } else { 2974bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 2975370521a1SStefan Hajnoczi } 297612bd451fSStefan Hajnoczi } 297712bd451fSStefan Hajnoczi 297813d8cc51SJeff Cody void qmp_block_stream(const char *device, 297913d8cc51SJeff Cody bool has_base, const char *base, 298013d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 298113d8cc51SJeff Cody bool has_speed, int64_t speed, 29821d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 29831d809098SPaolo Bonzini Error **errp) 298412bd451fSStefan Hajnoczi { 2985a0e8544cSFam Zheng BlockBackend *blk; 298612bd451fSStefan Hajnoczi BlockDriverState *bs; 2987c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 2988f3e69bebSStefan Hajnoczi AioContext *aio_context; 2989fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 299013d8cc51SJeff Cody const char *base_name = NULL; 299112bd451fSStefan Hajnoczi 29921d809098SPaolo Bonzini if (!has_on_error) { 29931d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 29941d809098SPaolo Bonzini } 29951d809098SPaolo Bonzini 2996a0e8544cSFam Zheng blk = blk_by_name(device); 2997a0e8544cSFam Zheng if (!blk) { 299875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 299975158ebbSMarkus Armbruster "Device '%s' not found", device); 300012bd451fSStefan Hajnoczi return; 300112bd451fSStefan Hajnoczi } 300212bd451fSStefan Hajnoczi 30035433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3004f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 3005f3e69bebSStefan Hajnoczi 30065433c24fSMax Reitz if (!blk_is_available(blk)) { 30075433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 30085433c24fSMax Reitz goto out; 30095433c24fSMax Reitz } 30105433c24fSMax Reitz bs = blk_bs(blk); 30115433c24fSMax Reitz 3012628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 3013f3e69bebSStefan Hajnoczi goto out; 3014628ff683SFam Zheng } 3015628ff683SFam Zheng 301613d8cc51SJeff Cody if (has_base) { 3017c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3018c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3019c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3020f3e69bebSStefan Hajnoczi goto out; 302112bd451fSStefan Hajnoczi } 3022f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 302313d8cc51SJeff Cody base_name = base; 3024c8c3080fSMarcelo Tosatti } 302512bd451fSStefan Hajnoczi 302613d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 302713d8cc51SJeff Cody * file, and specifying one is therefore an error */ 302813d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 302913d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 303013d8cc51SJeff Cody "entire chain"); 3031f3e69bebSStefan Hajnoczi goto out; 303213d8cc51SJeff Cody } 303313d8cc51SJeff Cody 303413d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 303513d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 303613d8cc51SJeff Cody 303713d8cc51SJeff Cody stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 30381d809098SPaolo Bonzini on_error, block_job_cb, bs, &local_err); 303984d18f06SMarkus Armbruster if (local_err) { 3040fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3041f3e69bebSStefan Hajnoczi goto out; 304212bd451fSStefan Hajnoczi } 304312bd451fSStefan Hajnoczi 304412bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3045f3e69bebSStefan Hajnoczi 3046f3e69bebSStefan Hajnoczi out: 3047f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 304812bd451fSStefan Hajnoczi } 30492d47c6e9SStefan Hajnoczi 3050ed61fc10SJeff Cody void qmp_block_commit(const char *device, 30517676e2c5SJeff Cody bool has_base, const char *base, 30527676e2c5SJeff Cody bool has_top, const char *top, 305354e26900SJeff Cody bool has_backing_file, const char *backing_file, 3054ed61fc10SJeff Cody bool has_speed, int64_t speed, 3055ed61fc10SJeff Cody Error **errp) 3056ed61fc10SJeff Cody { 3057a0e8544cSFam Zheng BlockBackend *blk; 3058ed61fc10SJeff Cody BlockDriverState *bs; 3059ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 30609e85cd5cSStefan Hajnoczi AioContext *aio_context; 3061ed61fc10SJeff Cody Error *local_err = NULL; 3062ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3063ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3064ed61fc10SJeff Cody */ 306592aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3066ed61fc10SJeff Cody 306754504663SMax Reitz if (!has_speed) { 306854504663SMax Reitz speed = 0; 306954504663SMax Reitz } 307054504663SMax Reitz 30717676e2c5SJeff Cody /* Important Note: 30727676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 30737676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 30747676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 30757676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 3076a0e8544cSFam Zheng blk = blk_by_name(device); 3077a0e8544cSFam Zheng if (!blk) { 307875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 307975158ebbSMarkus Armbruster "Device '%s' not found", device); 3080ed61fc10SJeff Cody return; 3081ed61fc10SJeff Cody } 3082ed61fc10SJeff Cody 30835433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 30849e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 30859e85cd5cSStefan Hajnoczi 30865433c24fSMax Reitz if (!blk_is_available(blk)) { 30875433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 30885433c24fSMax Reitz goto out; 30895433c24fSMax Reitz } 30905433c24fSMax Reitz bs = blk_bs(blk); 30915433c24fSMax Reitz 3092bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 30939e85cd5cSStefan Hajnoczi goto out; 3094628ff683SFam Zheng } 3095628ff683SFam Zheng 3096ed61fc10SJeff Cody /* default top_bs is the active layer */ 3097ed61fc10SJeff Cody top_bs = bs; 3098ed61fc10SJeff Cody 30997676e2c5SJeff Cody if (has_top && top) { 3100ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3101ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3102ed61fc10SJeff Cody } 3103ed61fc10SJeff Cody } 3104ed61fc10SJeff Cody 3105ed61fc10SJeff Cody if (top_bs == NULL) { 3106ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 31079e85cd5cSStefan Hajnoczi goto out; 3108ed61fc10SJeff Cody } 3109ed61fc10SJeff Cody 31109e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 31119e85cd5cSStefan Hajnoczi 3112d5208c45SJeff Cody if (has_base && base) { 3113d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3114d5208c45SJeff Cody } else { 3115d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3116d5208c45SJeff Cody } 3117d5208c45SJeff Cody 3118d5208c45SJeff Cody if (base_bs == NULL) { 3119c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 31209e85cd5cSStefan Hajnoczi goto out; 3121d5208c45SJeff Cody } 3122d5208c45SJeff Cody 31239e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 31249e85cd5cSStefan Hajnoczi 3125bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3126bb00021dSFam Zheng goto out; 3127bb00021dSFam Zheng } 3128bb00021dSFam Zheng 31297676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 31307676e2c5SJeff Cody if (top_bs == base_bs) { 31317676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 31329e85cd5cSStefan Hajnoczi goto out; 31337676e2c5SJeff Cody } 31347676e2c5SJeff Cody 313520a63d2cSFam Zheng if (top_bs == bs) { 313654e26900SJeff Cody if (has_backing_file) { 313754e26900SJeff Cody error_setg(errp, "'backing-file' specified," 313854e26900SJeff Cody " but 'top' is the active layer"); 31399e85cd5cSStefan Hajnoczi goto out; 314054e26900SJeff Cody } 314120a63d2cSFam Zheng commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 314220a63d2cSFam Zheng bs, &local_err); 314320a63d2cSFam Zheng } else { 3144ed61fc10SJeff Cody commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 314554e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 314620a63d2cSFam Zheng } 3147ed61fc10SJeff Cody if (local_err != NULL) { 3148ed61fc10SJeff Cody error_propagate(errp, local_err); 31499e85cd5cSStefan Hajnoczi goto out; 3150ed61fc10SJeff Cody } 31519e85cd5cSStefan Hajnoczi 31529e85cd5cSStefan Hajnoczi out: 31539e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3154ed61fc10SJeff Cody } 3155ed61fc10SJeff Cody 315678f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 315799a9addfSStefan Hajnoczi bool has_format, const char *format, 3158b53169eaSStefan Hajnoczi enum MirrorSyncMode sync, 315999a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 316099a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 3161d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 316278f51fdeSJohn Snow bool has_on_source_error, 316378f51fdeSJohn Snow BlockdevOnError on_source_error, 316478f51fdeSJohn Snow bool has_on_target_error, 316578f51fdeSJohn Snow BlockdevOnError on_target_error, 316678f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 316799a9addfSStefan Hajnoczi { 3168a0e8544cSFam Zheng BlockBackend *blk; 316999a9addfSStefan Hajnoczi BlockDriverState *bs; 317099a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3171fc5d3f84SIan Main BlockDriverState *source = NULL; 3172d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3173761731b1SStefan Hajnoczi AioContext *aio_context; 3174e6641719SMax Reitz QDict *options = NULL; 317599a9addfSStefan Hajnoczi Error *local_err = NULL; 317699a9addfSStefan Hajnoczi int flags; 317799a9addfSStefan Hajnoczi int64_t size; 317899a9addfSStefan Hajnoczi int ret; 317999a9addfSStefan Hajnoczi 318099a9addfSStefan Hajnoczi if (!has_speed) { 318199a9addfSStefan Hajnoczi speed = 0; 318299a9addfSStefan Hajnoczi } 318399a9addfSStefan Hajnoczi if (!has_on_source_error) { 318499a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 318599a9addfSStefan Hajnoczi } 318699a9addfSStefan Hajnoczi if (!has_on_target_error) { 318799a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 318899a9addfSStefan Hajnoczi } 318999a9addfSStefan Hajnoczi if (!has_mode) { 319099a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 319199a9addfSStefan Hajnoczi } 319299a9addfSStefan Hajnoczi 3193a0e8544cSFam Zheng blk = blk_by_name(device); 3194a0e8544cSFam Zheng if (!blk) { 319575158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 319675158ebbSMarkus Armbruster "Device '%s' not found", device); 319799a9addfSStefan Hajnoczi return; 319899a9addfSStefan Hajnoczi } 319999a9addfSStefan Hajnoczi 32005433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3201761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3202761731b1SStefan Hajnoczi 3203c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 3204c29c1dd3SFam Zheng * do an early check redundantly. */ 32055433c24fSMax Reitz if (!blk_is_available(blk)) { 3206c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3207761731b1SStefan Hajnoczi goto out; 320899a9addfSStefan Hajnoczi } 32095433c24fSMax Reitz bs = blk_bs(blk); 321099a9addfSStefan Hajnoczi 321199a9addfSStefan Hajnoczi if (!has_format) { 321299a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 321399a9addfSStefan Hajnoczi } 321499a9addfSStefan Hajnoczi 3215c29c1dd3SFam Zheng /* Early check to avoid creating target */ 32163718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3217761731b1SStefan Hajnoczi goto out; 321899a9addfSStefan Hajnoczi } 321999a9addfSStefan Hajnoczi 322099a9addfSStefan Hajnoczi flags = bs->open_flags | BDRV_O_RDWR; 322199a9addfSStefan Hajnoczi 3222fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3223fc5d3f84SIan Main * on top of. */ 3224fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 3225760e0063SKevin Wolf source = backing_bs(bs); 3226fc5d3f84SIan Main if (!source) { 3227fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 3228fc5d3f84SIan Main } 3229fc5d3f84SIan Main } 3230fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 3231fc5d3f84SIan Main source = bs; 3232fc5d3f84SIan Main } 3233fc5d3f84SIan Main 323499a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 323599a9addfSStefan Hajnoczi if (size < 0) { 323699a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3237761731b1SStefan Hajnoczi goto out; 323899a9addfSStefan Hajnoczi } 323999a9addfSStefan Hajnoczi 324099a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 3241e6641719SMax Reitz assert(format); 3242fc5d3f84SIan Main if (source) { 3243fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 3244fc5d3f84SIan Main source->drv->format_name, NULL, 3245fc5d3f84SIan Main size, flags, &local_err, false); 3246fc5d3f84SIan Main } else { 3247fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 3248fc5d3f84SIan Main size, flags, &local_err, false); 3249fc5d3f84SIan Main } 325099a9addfSStefan Hajnoczi } 325199a9addfSStefan Hajnoczi 325284d18f06SMarkus Armbruster if (local_err) { 325399a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3254761731b1SStefan Hajnoczi goto out; 325599a9addfSStefan Hajnoczi } 325699a9addfSStefan Hajnoczi 3257e6641719SMax Reitz if (format) { 3258e6641719SMax Reitz options = qdict_new(); 3259e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3260e6641719SMax Reitz } 3261e6641719SMax Reitz 3262f67503e5SMax Reitz target_bs = NULL; 32636ebf9aa2SMax Reitz ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err); 326499a9addfSStefan Hajnoczi if (ret < 0) { 326534b5d2c6SMax Reitz error_propagate(errp, local_err); 3266761731b1SStefan Hajnoczi goto out; 326799a9addfSStefan Hajnoczi } 326899a9addfSStefan Hajnoczi 3269761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3270761731b1SStefan Hajnoczi 3271d58d8453SJohn Snow if (has_bitmap) { 3272d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3273d58d8453SJohn Snow if (!bmap) { 3274d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 32750702d3d8SMax Reitz bdrv_unref(target_bs); 3276d58d8453SJohn Snow goto out; 3277d58d8453SJohn Snow } 3278d58d8453SJohn Snow } 3279d58d8453SJohn Snow 3280d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, bmap, 3281d58d8453SJohn Snow on_source_error, on_target_error, 328278f51fdeSJohn Snow block_job_cb, bs, txn, &local_err); 328399a9addfSStefan Hajnoczi if (local_err != NULL) { 32844f6fd349SFam Zheng bdrv_unref(target_bs); 328599a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3286761731b1SStefan Hajnoczi goto out; 328799a9addfSStefan Hajnoczi } 3288761731b1SStefan Hajnoczi 3289761731b1SStefan Hajnoczi out: 3290761731b1SStefan Hajnoczi aio_context_release(aio_context); 329199a9addfSStefan Hajnoczi } 329299a9addfSStefan Hajnoczi 329378f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target, 329478f51fdeSJohn Snow bool has_format, const char *format, 329578f51fdeSJohn Snow enum MirrorSyncMode sync, 329678f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 329778f51fdeSJohn Snow bool has_speed, int64_t speed, 329878f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 329978f51fdeSJohn Snow bool has_on_source_error, BlockdevOnError on_source_error, 330078f51fdeSJohn Snow bool has_on_target_error, BlockdevOnError on_target_error, 330178f51fdeSJohn Snow Error **errp) 330278f51fdeSJohn Snow { 330378f51fdeSJohn Snow return do_drive_backup(device, target, has_format, format, sync, 330478f51fdeSJohn Snow has_mode, mode, has_speed, speed, 330578f51fdeSJohn Snow has_bitmap, bitmap, 330678f51fdeSJohn Snow has_on_source_error, on_source_error, 330778f51fdeSJohn Snow has_on_target_error, on_target_error, 330878f51fdeSJohn Snow NULL, errp); 330978f51fdeSJohn Snow } 331078f51fdeSJohn Snow 3311c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3312c13163fbSBenoît Canet { 3313d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3314c13163fbSBenoît Canet } 3315c13163fbSBenoît Canet 331678f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target, 3317c29c1dd3SFam Zheng enum MirrorSyncMode sync, 3318c29c1dd3SFam Zheng bool has_speed, int64_t speed, 3319c29c1dd3SFam Zheng bool has_on_source_error, 3320c29c1dd3SFam Zheng BlockdevOnError on_source_error, 3321c29c1dd3SFam Zheng bool has_on_target_error, 3322c29c1dd3SFam Zheng BlockdevOnError on_target_error, 332378f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 3324c29c1dd3SFam Zheng { 33255433c24fSMax Reitz BlockBackend *blk, *target_blk; 3326c29c1dd3SFam Zheng BlockDriverState *bs; 3327c29c1dd3SFam Zheng BlockDriverState *target_bs; 3328c29c1dd3SFam Zheng Error *local_err = NULL; 3329c29c1dd3SFam Zheng AioContext *aio_context; 3330c29c1dd3SFam Zheng 3331c29c1dd3SFam Zheng if (!has_speed) { 3332c29c1dd3SFam Zheng speed = 0; 3333c29c1dd3SFam Zheng } 3334c29c1dd3SFam Zheng if (!has_on_source_error) { 3335c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3336c29c1dd3SFam Zheng } 3337c29c1dd3SFam Zheng if (!has_on_target_error) { 3338c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3339c29c1dd3SFam Zheng } 3340c29c1dd3SFam Zheng 3341a0e8544cSFam Zheng blk = blk_by_name(device); 3342a0e8544cSFam Zheng if (!blk) { 33435b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", device); 3344c29c1dd3SFam Zheng return; 3345c29c1dd3SFam Zheng } 3346c29c1dd3SFam Zheng 33475433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3348c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3349c29c1dd3SFam Zheng 33505433c24fSMax Reitz if (!blk_is_available(blk)) { 33515433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 33525433c24fSMax Reitz goto out; 33535433c24fSMax Reitz } 33545433c24fSMax Reitz bs = blk_bs(blk); 33555433c24fSMax Reitz 33565433c24fSMax Reitz target_blk = blk_by_name(target); 33575433c24fSMax Reitz if (!target_blk) { 33585b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", target); 3359c29c1dd3SFam Zheng goto out; 3360c29c1dd3SFam Zheng } 33615433c24fSMax Reitz 33625433c24fSMax Reitz if (!blk_is_available(target_blk)) { 33635433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", target); 33645433c24fSMax Reitz goto out; 33655433c24fSMax Reitz } 33665433c24fSMax Reitz target_bs = blk_bs(target_blk); 3367c29c1dd3SFam Zheng 3368c29c1dd3SFam Zheng bdrv_ref(target_bs); 3369c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3370d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 337178f51fdeSJohn Snow on_target_error, block_job_cb, bs, txn, &local_err); 3372c29c1dd3SFam Zheng if (local_err != NULL) { 3373c29c1dd3SFam Zheng bdrv_unref(target_bs); 3374c29c1dd3SFam Zheng error_propagate(errp, local_err); 3375c29c1dd3SFam Zheng } 3376c29c1dd3SFam Zheng out: 3377c29c1dd3SFam Zheng aio_context_release(aio_context); 3378c29c1dd3SFam Zheng } 3379c29c1dd3SFam Zheng 338078f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target, 338178f51fdeSJohn Snow enum MirrorSyncMode sync, 338278f51fdeSJohn Snow bool has_speed, int64_t speed, 338378f51fdeSJohn Snow bool has_on_source_error, 338478f51fdeSJohn Snow BlockdevOnError on_source_error, 338578f51fdeSJohn Snow bool has_on_target_error, 338678f51fdeSJohn Snow BlockdevOnError on_target_error, 338778f51fdeSJohn Snow Error **errp) 338878f51fdeSJohn Snow { 338978f51fdeSJohn Snow do_blockdev_backup(device, target, sync, has_speed, speed, 339078f51fdeSJohn Snow has_on_source_error, on_source_error, 339178f51fdeSJohn Snow has_on_target_error, on_target_error, 339278f51fdeSJohn Snow NULL, errp); 339378f51fdeSJohn Snow } 339478f51fdeSJohn Snow 33954193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 33964193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 33974193cdd7SFam Zheng **/ 33984193cdd7SFam Zheng static void blockdev_mirror_common(BlockDriverState *bs, 33994193cdd7SFam Zheng BlockDriverState *target, 340009158f00SBenoît Canet bool has_replaces, const char *replaces, 3401d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3402b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3403eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 340408e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 34054193cdd7SFam Zheng bool has_on_source_error, 34064193cdd7SFam Zheng BlockdevOnError on_source_error, 34074193cdd7SFam Zheng bool has_on_target_error, 34084193cdd7SFam Zheng BlockdevOnError on_target_error, 34090fc9f8eaSFam Zheng bool has_unmap, bool unmap, 3410b952b558SPaolo Bonzini Error **errp) 3411d9b902dbSPaolo Bonzini { 3412d9b902dbSPaolo Bonzini 3413d9b902dbSPaolo Bonzini if (!has_speed) { 3414d9b902dbSPaolo Bonzini speed = 0; 3415d9b902dbSPaolo Bonzini } 3416b952b558SPaolo Bonzini if (!has_on_source_error) { 3417b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3418b952b558SPaolo Bonzini } 3419b952b558SPaolo Bonzini if (!has_on_target_error) { 3420b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3421b952b558SPaolo Bonzini } 3422eee13dfeSPaolo Bonzini if (!has_granularity) { 3423eee13dfeSPaolo Bonzini granularity = 0; 3424eee13dfeSPaolo Bonzini } 342508e4ed6cSPaolo Bonzini if (!has_buf_size) { 342648ac0a4dSWen Congyang buf_size = 0; 342708e4ed6cSPaolo Bonzini } 34280fc9f8eaSFam Zheng if (!has_unmap) { 34290fc9f8eaSFam Zheng unmap = true; 34300fc9f8eaSFam Zheng } 343108e4ed6cSPaolo Bonzini 3432eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3433c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 34343cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3435eee13dfeSPaolo Bonzini return; 3436eee13dfeSPaolo Bonzini } 3437eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3438c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3439c6bd8c70SMarkus Armbruster "power of 2"); 3440eee13dfeSPaolo Bonzini return; 3441eee13dfeSPaolo Bonzini } 3442d9b902dbSPaolo Bonzini 34434193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 34444193cdd7SFam Zheng return; 34454193cdd7SFam Zheng } 3446e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3447e40e5027SFam Zheng return; 3448e40e5027SFam Zheng } 3449df92562eSFam Zheng if (target->blk) { 3450df92562eSFam Zheng error_setg(errp, "Cannot mirror to an attached block device"); 3451df92562eSFam Zheng return; 3452df92562eSFam Zheng } 34534193cdd7SFam Zheng 34544193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 34554193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 34564193cdd7SFam Zheng } 34574193cdd7SFam Zheng 34584193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 34594193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 34604193cdd7SFam Zheng */ 34614193cdd7SFam Zheng mirror_start(bs, target, 34624193cdd7SFam Zheng has_replaces ? replaces : NULL, 34634193cdd7SFam Zheng speed, granularity, buf_size, sync, 34644193cdd7SFam Zheng on_source_error, on_target_error, unmap, 34654193cdd7SFam Zheng block_job_cb, bs, errp); 34664193cdd7SFam Zheng } 34674193cdd7SFam Zheng 34684193cdd7SFam Zheng void qmp_drive_mirror(const char *device, const char *target, 34694193cdd7SFam Zheng bool has_format, const char *format, 34704193cdd7SFam Zheng bool has_node_name, const char *node_name, 34714193cdd7SFam Zheng bool has_replaces, const char *replaces, 34724193cdd7SFam Zheng enum MirrorSyncMode sync, 34734193cdd7SFam Zheng bool has_mode, enum NewImageMode mode, 34744193cdd7SFam Zheng bool has_speed, int64_t speed, 34754193cdd7SFam Zheng bool has_granularity, uint32_t granularity, 34764193cdd7SFam Zheng bool has_buf_size, int64_t buf_size, 34774193cdd7SFam Zheng bool has_on_source_error, BlockdevOnError on_source_error, 34784193cdd7SFam Zheng bool has_on_target_error, BlockdevOnError on_target_error, 34794193cdd7SFam Zheng bool has_unmap, bool unmap, 34804193cdd7SFam Zheng Error **errp) 34814193cdd7SFam Zheng { 34824193cdd7SFam Zheng BlockDriverState *bs; 34834193cdd7SFam Zheng BlockBackend *blk; 34844193cdd7SFam Zheng BlockDriverState *source, *target_bs; 34854193cdd7SFam Zheng AioContext *aio_context; 34864193cdd7SFam Zheng Error *local_err = NULL; 34874193cdd7SFam Zheng QDict *options = NULL; 34884193cdd7SFam Zheng int flags; 34894193cdd7SFam Zheng int64_t size; 34904193cdd7SFam Zheng int ret; 34914193cdd7SFam Zheng 3492a0e8544cSFam Zheng blk = blk_by_name(device); 3493a0e8544cSFam Zheng if (!blk) { 349475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 349575158ebbSMarkus Armbruster "Device '%s' not found", device); 3496d9b902dbSPaolo Bonzini return; 3497d9b902dbSPaolo Bonzini } 3498d9b902dbSPaolo Bonzini 34995433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 35005a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 35015a7e7a0bSStefan Hajnoczi 35025433c24fSMax Reitz if (!blk_is_available(blk)) { 3503c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 35045a7e7a0bSStefan Hajnoczi goto out; 3505d9b902dbSPaolo Bonzini } 35065433c24fSMax Reitz bs = blk_bs(blk); 35074193cdd7SFam Zheng if (!has_mode) { 35084193cdd7SFam Zheng mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 35094193cdd7SFam Zheng } 3510d9b902dbSPaolo Bonzini 3511d9b902dbSPaolo Bonzini if (!has_format) { 3512d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3513d9b902dbSPaolo Bonzini } 3514d9b902dbSPaolo Bonzini 3515d9b902dbSPaolo Bonzini flags = bs->open_flags | BDRV_O_RDWR; 3516760e0063SKevin Wolf source = backing_bs(bs); 3517d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3518d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 3519d9b902dbSPaolo Bonzini } 3520117e0c82SMax Reitz if (sync == MIRROR_SYNC_MODE_NONE) { 3521117e0c82SMax Reitz source = bs; 3522117e0c82SMax Reitz } 3523d9b902dbSPaolo Bonzini 3524ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3525ac3c5d83SStefan Hajnoczi if (size < 0) { 3526ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 35275a7e7a0bSStefan Hajnoczi goto out; 3528ac3c5d83SStefan Hajnoczi } 3529ac3c5d83SStefan Hajnoczi 353009158f00SBenoît Canet if (has_replaces) { 353109158f00SBenoît Canet BlockDriverState *to_replace_bs; 35325a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 35335a7e7a0bSStefan Hajnoczi int64_t replace_size; 353409158f00SBenoît Canet 353509158f00SBenoît Canet if (!has_node_name) { 353609158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 353709158f00SBenoît Canet " named node of the graph"); 35385a7e7a0bSStefan Hajnoczi goto out; 353909158f00SBenoît Canet } 354009158f00SBenoît Canet 3541e12f3784SWen Congyang to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 354209158f00SBenoît Canet 354309158f00SBenoît Canet if (!to_replace_bs) { 354409158f00SBenoît Canet error_propagate(errp, local_err); 35455a7e7a0bSStefan Hajnoczi goto out; 354609158f00SBenoît Canet } 354709158f00SBenoît Canet 35485a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 35495a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 35505a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 35515a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 35525a7e7a0bSStefan Hajnoczi 35535a7e7a0bSStefan Hajnoczi if (size != replace_size) { 355409158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 355509158f00SBenoît Canet "different size"); 35565a7e7a0bSStefan Hajnoczi goto out; 355709158f00SBenoît Canet } 355809158f00SBenoît Canet } 355909158f00SBenoît Canet 356014526864SMax Reitz if ((sync == MIRROR_SYNC_MODE_FULL || !source) 356114526864SMax Reitz && mode != NEW_IMAGE_MODE_EXISTING) 356214526864SMax Reitz { 3563d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3564e6641719SMax Reitz assert(format); 3565cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3566f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3567d9b902dbSPaolo Bonzini } else { 3568d9b902dbSPaolo Bonzini switch (mode) { 3569d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3570d9b902dbSPaolo Bonzini break; 3571d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3572d9b902dbSPaolo Bonzini /* create new image with backing file */ 3573cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3574d9b902dbSPaolo Bonzini source->filename, 3575d9b902dbSPaolo Bonzini source->drv->format_name, 3576f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3577d9b902dbSPaolo Bonzini break; 3578d9b902dbSPaolo Bonzini default: 3579d9b902dbSPaolo Bonzini abort(); 3580d9b902dbSPaolo Bonzini } 3581d9b902dbSPaolo Bonzini } 3582d9b902dbSPaolo Bonzini 358384d18f06SMarkus Armbruster if (local_err) { 3584cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 35855a7e7a0bSStefan Hajnoczi goto out; 3586d9b902dbSPaolo Bonzini } 3587d9b902dbSPaolo Bonzini 35884c828dc6SBenoît Canet options = qdict_new(); 3589e6641719SMax Reitz if (has_node_name) { 35904c828dc6SBenoît Canet qdict_put(options, "node-name", qstring_from_str(node_name)); 35914c828dc6SBenoît Canet } 3592e6641719SMax Reitz if (format) { 3593e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3594e6641719SMax Reitz } 35954c828dc6SBenoît Canet 3596b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3597b812f671SPaolo Bonzini * file. 3598b812f671SPaolo Bonzini */ 3599f67503e5SMax Reitz target_bs = NULL; 36004c828dc6SBenoît Canet ret = bdrv_open(&target_bs, target, NULL, options, 36016ebf9aa2SMax Reitz flags | BDRV_O_NO_BACKING, &local_err); 3602d9b902dbSPaolo Bonzini if (ret < 0) { 360334b5d2c6SMax Reitz error_propagate(errp, local_err); 36045a7e7a0bSStefan Hajnoczi goto out; 3605d9b902dbSPaolo Bonzini } 3606d9b902dbSPaolo Bonzini 36075a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 36085a7e7a0bSStefan Hajnoczi 36094193cdd7SFam Zheng blockdev_mirror_common(bs, target_bs, 36104193cdd7SFam Zheng has_replaces, replaces, sync, 36114193cdd7SFam Zheng has_speed, speed, 36124193cdd7SFam Zheng has_granularity, granularity, 36134193cdd7SFam Zheng has_buf_size, buf_size, 36144193cdd7SFam Zheng has_on_source_error, on_source_error, 36154193cdd7SFam Zheng has_on_target_error, on_target_error, 36164193cdd7SFam Zheng has_unmap, unmap, 36174193cdd7SFam Zheng &local_err); 36184193cdd7SFam Zheng if (local_err) { 3619d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 36204193cdd7SFam Zheng bdrv_unref(target_bs); 3621d9b902dbSPaolo Bonzini } 36225a7e7a0bSStefan Hajnoczi out: 36235a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3624d9b902dbSPaolo Bonzini } 3625d9b902dbSPaolo Bonzini 3626df92562eSFam Zheng void qmp_blockdev_mirror(const char *device, const char *target, 3627df92562eSFam Zheng bool has_replaces, const char *replaces, 3628df92562eSFam Zheng MirrorSyncMode sync, 3629df92562eSFam Zheng bool has_speed, int64_t speed, 3630df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3631df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3632df92562eSFam Zheng bool has_on_source_error, 3633df92562eSFam Zheng BlockdevOnError on_source_error, 3634df92562eSFam Zheng bool has_on_target_error, 3635df92562eSFam Zheng BlockdevOnError on_target_error, 3636df92562eSFam Zheng Error **errp) 3637df92562eSFam Zheng { 3638df92562eSFam Zheng BlockDriverState *bs; 3639df92562eSFam Zheng BlockBackend *blk; 3640df92562eSFam Zheng BlockDriverState *target_bs; 3641df92562eSFam Zheng AioContext *aio_context; 3642df92562eSFam Zheng Error *local_err = NULL; 3643df92562eSFam Zheng 3644df92562eSFam Zheng blk = blk_by_name(device); 3645df92562eSFam Zheng if (!blk) { 3646df92562eSFam Zheng error_setg(errp, "Device '%s' not found", device); 3647df92562eSFam Zheng return; 3648df92562eSFam Zheng } 3649df92562eSFam Zheng bs = blk_bs(blk); 3650df92562eSFam Zheng 3651df92562eSFam Zheng if (!bs) { 3652df92562eSFam Zheng error_setg(errp, "Device '%s' has no media", device); 3653df92562eSFam Zheng return; 3654df92562eSFam Zheng } 3655df92562eSFam Zheng 3656df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3657df92562eSFam Zheng if (!target_bs) { 3658df92562eSFam Zheng return; 3659df92562eSFam Zheng } 3660df92562eSFam Zheng 3661df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3662df92562eSFam Zheng aio_context_acquire(aio_context); 3663df92562eSFam Zheng 3664df92562eSFam Zheng bdrv_ref(target_bs); 3665df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3666df92562eSFam Zheng 3667df92562eSFam Zheng blockdev_mirror_common(bs, target_bs, 3668df92562eSFam Zheng has_replaces, replaces, sync, 3669df92562eSFam Zheng has_speed, speed, 3670df92562eSFam Zheng has_granularity, granularity, 3671df92562eSFam Zheng has_buf_size, buf_size, 3672df92562eSFam Zheng has_on_source_error, on_source_error, 3673df92562eSFam Zheng has_on_target_error, on_target_error, 3674df92562eSFam Zheng true, true, 3675df92562eSFam Zheng &local_err); 3676df92562eSFam Zheng if (local_err) { 3677df92562eSFam Zheng error_propagate(errp, local_err); 3678df92562eSFam Zheng bdrv_unref(target_bs); 3679df92562eSFam Zheng } 3680df92562eSFam Zheng 3681df92562eSFam Zheng aio_context_release(aio_context); 3682df92562eSFam Zheng } 3683df92562eSFam Zheng 36843d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */ 368524d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context, 368624d6bffeSMarkus Armbruster Error **errp) 36872d47c6e9SStefan Hajnoczi { 3688a0e8544cSFam Zheng BlockBackend *blk; 36892d47c6e9SStefan Hajnoczi BlockDriverState *bs; 36902d47c6e9SStefan Hajnoczi 36915433c24fSMax Reitz *aio_context = NULL; 36925433c24fSMax Reitz 3693a0e8544cSFam Zheng blk = blk_by_name(device); 3694a0e8544cSFam Zheng if (!blk) { 36953d948cdfSStefan Hajnoczi goto notfound; 36962d47c6e9SStefan Hajnoczi } 36973d948cdfSStefan Hajnoczi 36985433c24fSMax Reitz *aio_context = blk_get_aio_context(blk); 36993d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 37003d948cdfSStefan Hajnoczi 37015433c24fSMax Reitz if (!blk_is_available(blk)) { 37025433c24fSMax Reitz goto notfound; 37035433c24fSMax Reitz } 37045433c24fSMax Reitz bs = blk_bs(blk); 37055433c24fSMax Reitz 37063d948cdfSStefan Hajnoczi if (!bs->job) { 37073d948cdfSStefan Hajnoczi goto notfound; 37083d948cdfSStefan Hajnoczi } 37093d948cdfSStefan Hajnoczi 37102d47c6e9SStefan Hajnoczi return bs->job; 37113d948cdfSStefan Hajnoczi 37123d948cdfSStefan Hajnoczi notfound: 37132e3a0266SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 37142e3a0266SMarkus Armbruster "No active block job on device '%s'", device); 37155433c24fSMax Reitz if (*aio_context) { 37165433c24fSMax Reitz aio_context_release(*aio_context); 37173d948cdfSStefan Hajnoczi *aio_context = NULL; 37185433c24fSMax Reitz } 37193d948cdfSStefan Hajnoczi return NULL; 37202d47c6e9SStefan Hajnoczi } 37212d47c6e9SStefan Hajnoczi 3722882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 37232d47c6e9SStefan Hajnoczi { 37243d948cdfSStefan Hajnoczi AioContext *aio_context; 372524d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37262d47c6e9SStefan Hajnoczi 37272d47c6e9SStefan Hajnoczi if (!job) { 37282d47c6e9SStefan Hajnoczi return; 37292d47c6e9SStefan Hajnoczi } 37302d47c6e9SStefan Hajnoczi 3731882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 37323d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37332d47c6e9SStefan Hajnoczi } 3734370521a1SStefan Hajnoczi 37356e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 37366e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 37376e37fb81SPaolo Bonzini { 37383d948cdfSStefan Hajnoczi AioContext *aio_context; 373924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37406e37fb81SPaolo Bonzini 37416e37fb81SPaolo Bonzini if (!job) { 37426e37fb81SPaolo Bonzini return; 37436e37fb81SPaolo Bonzini } 37443d948cdfSStefan Hajnoczi 37453d948cdfSStefan Hajnoczi if (!has_force) { 37463d948cdfSStefan Hajnoczi force = false; 37473d948cdfSStefan Hajnoczi } 37483d948cdfSStefan Hajnoczi 3749751ebd76SFam Zheng if (job->user_paused && !force) { 3750f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3751f231b88dSCole Robinson device); 37523d948cdfSStefan Hajnoczi goto out; 37536e37fb81SPaolo Bonzini } 37546e37fb81SPaolo Bonzini 37556e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 37566e37fb81SPaolo Bonzini block_job_cancel(job); 37573d948cdfSStefan Hajnoczi out: 37583d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37596e37fb81SPaolo Bonzini } 37606e37fb81SPaolo Bonzini 37616e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3762370521a1SStefan Hajnoczi { 37633d948cdfSStefan Hajnoczi AioContext *aio_context; 376424d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3765370521a1SStefan Hajnoczi 3766751ebd76SFam Zheng if (!job || job->user_paused) { 3767370521a1SStefan Hajnoczi return; 3768370521a1SStefan Hajnoczi } 37696e37fb81SPaolo Bonzini 3770751ebd76SFam Zheng job->user_paused = true; 37716e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 37726e37fb81SPaolo Bonzini block_job_pause(job); 37733d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37746e37fb81SPaolo Bonzini } 37756e37fb81SPaolo Bonzini 37766e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 37776e37fb81SPaolo Bonzini { 37783d948cdfSStefan Hajnoczi AioContext *aio_context; 377924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37806e37fb81SPaolo Bonzini 3781751ebd76SFam Zheng if (!job || !job->user_paused) { 37828acc72a4SPaolo Bonzini return; 37838acc72a4SPaolo Bonzini } 3784370521a1SStefan Hajnoczi 3785751ebd76SFam Zheng job->user_paused = false; 37866e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 37876e37fb81SPaolo Bonzini block_job_resume(job); 37883d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3789370521a1SStefan Hajnoczi } 3790fb5458cdSStefan Hajnoczi 3791aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3792aeae883bSPaolo Bonzini { 37933d948cdfSStefan Hajnoczi AioContext *aio_context; 379424d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3795aeae883bSPaolo Bonzini 3796aeae883bSPaolo Bonzini if (!job) { 3797aeae883bSPaolo Bonzini return; 3798aeae883bSPaolo Bonzini } 3799aeae883bSPaolo Bonzini 3800aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3801aeae883bSPaolo Bonzini block_job_complete(job, errp); 38023d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3803aeae883bSPaolo Bonzini } 3804aeae883bSPaolo Bonzini 3805fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3806fa40e656SJeff Cody const char *image_node_name, 3807fa40e656SJeff Cody const char *backing_file, 3808fa40e656SJeff Cody Error **errp) 3809fa40e656SJeff Cody { 3810a0e8544cSFam Zheng BlockBackend *blk; 3811fa40e656SJeff Cody BlockDriverState *bs = NULL; 3812729962f6SStefan Hajnoczi AioContext *aio_context; 3813fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3814fa40e656SJeff Cody Error *local_err = NULL; 3815fa40e656SJeff Cody bool ro; 3816fa40e656SJeff Cody int open_flags; 3817fa40e656SJeff Cody int ret; 3818fa40e656SJeff Cody 3819a0e8544cSFam Zheng blk = blk_by_name(device); 3820a0e8544cSFam Zheng if (!blk) { 382175158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 382275158ebbSMarkus Armbruster "Device '%s' not found", device); 3823fa40e656SJeff Cody return; 3824fa40e656SJeff Cody } 3825fa40e656SJeff Cody 38265433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3827729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3828729962f6SStefan Hajnoczi 38295433c24fSMax Reitz if (!blk_is_available(blk)) { 38305433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 38315433c24fSMax Reitz goto out; 38325433c24fSMax Reitz } 38335433c24fSMax Reitz bs = blk_bs(blk); 38345433c24fSMax Reitz 3835fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3836fa40e656SJeff Cody if (local_err) { 3837fa40e656SJeff Cody error_propagate(errp, local_err); 3838729962f6SStefan Hajnoczi goto out; 3839fa40e656SJeff Cody } 3840fa40e656SJeff Cody 3841fa40e656SJeff Cody if (!image_bs) { 3842fa40e656SJeff Cody error_setg(errp, "image file not found"); 3843729962f6SStefan Hajnoczi goto out; 3844fa40e656SJeff Cody } 3845fa40e656SJeff Cody 3846fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3847fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3848fa40e656SJeff Cody "without a backing file"); 3849729962f6SStefan Hajnoczi goto out; 3850fa40e656SJeff Cody } 3851fa40e656SJeff Cody 3852fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3853fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3854fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3855729962f6SStefan Hajnoczi goto out; 3856fa40e656SJeff Cody } 3857fa40e656SJeff Cody 3858fa40e656SJeff Cody /* final sanity check */ 3859fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3860fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3861fa40e656SJeff Cody device); 3862729962f6SStefan Hajnoczi goto out; 3863fa40e656SJeff Cody } 3864fa40e656SJeff Cody 3865fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3866fa40e656SJeff Cody open_flags = image_bs->open_flags; 3867fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3868fa40e656SJeff Cody 3869fa40e656SJeff Cody if (ro) { 3870fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3871fa40e656SJeff Cody if (local_err) { 3872fa40e656SJeff Cody error_propagate(errp, local_err); 3873729962f6SStefan Hajnoczi goto out; 3874fa40e656SJeff Cody } 3875fa40e656SJeff Cody } 3876fa40e656SJeff Cody 3877fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3878fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3879fa40e656SJeff Cody 3880fa40e656SJeff Cody if (ret < 0) { 3881fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3882fa40e656SJeff Cody backing_file); 3883fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3884fa40e656SJeff Cody * appropriate */ 3885fa40e656SJeff Cody } 3886fa40e656SJeff Cody 3887fa40e656SJeff Cody if (ro) { 3888fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3889fa40e656SJeff Cody if (local_err) { 3890fa40e656SJeff Cody error_propagate(errp, local_err); /* will preserve prior errp */ 3891fa40e656SJeff Cody } 3892fa40e656SJeff Cody } 3893729962f6SStefan Hajnoczi 3894729962f6SStefan Hajnoczi out: 3895729962f6SStefan Hajnoczi aio_context_release(aio_context); 3896fa40e656SJeff Cody } 3897fa40e656SJeff Cody 3898abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 3899abb21ac3SKevin Wolf { 3900abb21ac3SKevin Wolf QemuOpts *opts; 3901abb21ac3SKevin Wolf QDict *qdict; 3902abb21ac3SKevin Wolf Error *local_err = NULL; 3903abb21ac3SKevin Wolf 3904abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 3905abb21ac3SKevin Wolf if (!opts) { 3906abb21ac3SKevin Wolf return; 3907abb21ac3SKevin Wolf } 3908abb21ac3SKevin Wolf 3909abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 3910abb21ac3SKevin Wolf 3911abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 3912f8746fb8SKevin Wolf QDECREF(qdict); 3913abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 3914abb21ac3SKevin Wolf goto out; 3915abb21ac3SKevin Wolf } 3916abb21ac3SKevin Wolf 3917abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 3918abb21ac3SKevin Wolf if (!bs) { 3919abb21ac3SKevin Wolf error_report_err(local_err); 3920abb21ac3SKevin Wolf goto out; 3921abb21ac3SKevin Wolf } 3922abb21ac3SKevin Wolf 3923abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3924abb21ac3SKevin Wolf 3925abb21ac3SKevin Wolf out: 3926abb21ac3SKevin Wolf qemu_opts_del(opts); 3927abb21ac3SKevin Wolf } 3928abb21ac3SKevin Wolf 3929d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3930d26c9a15SKevin Wolf { 3931d26c9a15SKevin Wolf QmpOutputVisitor *ov = qmp_output_visitor_new(); 3932be4b67bcSMax Reitz BlockDriverState *bs; 3933be4b67bcSMax Reitz BlockBackend *blk = NULL; 3934d26c9a15SKevin Wolf QObject *obj; 3935d26c9a15SKevin Wolf QDict *qdict; 3936d26c9a15SKevin Wolf Error *local_err = NULL; 3937d26c9a15SKevin Wolf 393860e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3939d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 394060e19e06SMarkus Armbruster * when called from drive_new(). 3941d26c9a15SKevin Wolf * 3942d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3943d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3944c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3945c6e0bd9bSKevin Wolf options->cache->has_direct && 3946c6e0bd9bSKevin Wolf options->cache->direct; 3947c6e0bd9bSKevin Wolf if (!direct) { 3948d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3949d26c9a15SKevin Wolf goto fail; 3950d26c9a15SKevin Wolf } 3951d26c9a15SKevin Wolf } 3952d26c9a15SKevin Wolf 395351e72bc1SEric Blake visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options, 395451e72bc1SEric Blake &local_err); 395584d18f06SMarkus Armbruster if (local_err) { 3956d26c9a15SKevin Wolf error_propagate(errp, local_err); 3957d26c9a15SKevin Wolf goto fail; 3958d26c9a15SKevin Wolf } 3959d26c9a15SKevin Wolf 3960d26c9a15SKevin Wolf obj = qmp_output_get_qobject(ov); 3961d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3962d26c9a15SKevin Wolf 3963d26c9a15SKevin Wolf qdict_flatten(qdict); 3964d26c9a15SKevin Wolf 3965be4b67bcSMax Reitz if (options->has_id) { 396618e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 396784d18f06SMarkus Armbruster if (local_err) { 3968b681072dSKevin Wolf error_propagate(errp, local_err); 3969d26c9a15SKevin Wolf goto fail; 3970d26c9a15SKevin Wolf } 3971d26c9a15SKevin Wolf 3972be4b67bcSMax Reitz bs = blk_bs(blk); 3973be4b67bcSMax Reitz } else { 3974be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3975be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3976be4b67bcSMax Reitz "the root node"); 3977be4b67bcSMax Reitz goto fail; 3978be4b67bcSMax Reitz } 3979be4b67bcSMax Reitz 3980bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3981bd745e23SMax Reitz if (!bs) { 3982be4b67bcSMax Reitz goto fail; 3983be4b67bcSMax Reitz } 39849c4218e9SMax Reitz 39859c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3986be4b67bcSMax Reitz } 3987be4b67bcSMax Reitz 3988be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 3989be4b67bcSMax Reitz if (blk) { 3990efaa7c4eSMax Reitz monitor_remove_blk(blk); 399118e46a03SMarkus Armbruster blk_unref(blk); 3992be4b67bcSMax Reitz } else { 39939c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 3994be4b67bcSMax Reitz bdrv_unref(bs); 3995be4b67bcSMax Reitz } 39968ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 39978ae8e904SKevin Wolf goto fail; 39988ae8e904SKevin Wolf } 39998ae8e904SKevin Wolf 4000d26c9a15SKevin Wolf fail: 4001d26c9a15SKevin Wolf qmp_output_visitor_cleanup(ov); 4002d26c9a15SKevin Wolf } 4003d26c9a15SKevin Wolf 400481b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id, 400581b936aeSAlberto Garcia bool has_node_name, const char *node_name, Error **errp) 400681b936aeSAlberto Garcia { 400781b936aeSAlberto Garcia AioContext *aio_context; 400881b936aeSAlberto Garcia BlockBackend *blk; 400981b936aeSAlberto Garcia BlockDriverState *bs; 401081b936aeSAlberto Garcia 401181b936aeSAlberto Garcia if (has_id && has_node_name) { 401281b936aeSAlberto Garcia error_setg(errp, "Only one of id and node-name must be specified"); 401381b936aeSAlberto Garcia return; 401481b936aeSAlberto Garcia } else if (!has_id && !has_node_name) { 401581b936aeSAlberto Garcia error_setg(errp, "No block device specified"); 401681b936aeSAlberto Garcia return; 401781b936aeSAlberto Garcia } 401881b936aeSAlberto Garcia 401981b936aeSAlberto Garcia if (has_id) { 4020efaa7c4eSMax Reitz /* blk_by_name() never returns a BB that is not owned by the monitor */ 402181b936aeSAlberto Garcia blk = blk_by_name(id); 402281b936aeSAlberto Garcia if (!blk) { 402381b936aeSAlberto Garcia error_setg(errp, "Cannot find block backend %s", id); 402481b936aeSAlberto Garcia return; 402581b936aeSAlberto Garcia } 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 { 403381b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 403481b936aeSAlberto Garcia if (!bs) { 403581b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 403681b936aeSAlberto Garcia return; 403781b936aeSAlberto Garcia } 403881b936aeSAlberto Garcia blk = bs->blk; 403981b936aeSAlberto Garcia if (blk) { 404081b936aeSAlberto Garcia error_setg(errp, "Node %s is in use by %s", 404181b936aeSAlberto Garcia node_name, blk_name(blk)); 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 4079fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4080fb5458cdSStefan Hajnoczi { 4081fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4082fea68bb6SMarkus Armbruster BlockDriverState *bs; 4083fea68bb6SMarkus Armbruster 4084fea68bb6SMarkus Armbruster for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { 408569691e72SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 408669691e72SStefan Hajnoczi 408769691e72SStefan Hajnoczi aio_context_acquire(aio_context); 408869691e72SStefan Hajnoczi 4089fea68bb6SMarkus Armbruster if (bs->job) { 4090fea68bb6SMarkus Armbruster BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 4091fea68bb6SMarkus Armbruster elem->value = block_job_query(bs->job); 4092fea68bb6SMarkus Armbruster *p_next = elem; 4093fea68bb6SMarkus Armbruster p_next = &elem->next; 4094fea68bb6SMarkus Armbruster } 409569691e72SStefan Hajnoczi 409669691e72SStefan Hajnoczi aio_context_release(aio_context); 4097fea68bb6SMarkus Armbruster } 4098fea68bb6SMarkus Armbruster 4099fea68bb6SMarkus Armbruster return head; 4100fb5458cdSStefan Hajnoczi } 41014d454574SPaolo Bonzini 41020006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 41034d454574SPaolo Bonzini .name = "drive", 41040006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 41054d454574SPaolo Bonzini .desc = { 41064d454574SPaolo Bonzini { 41074d454574SPaolo Bonzini .name = "snapshot", 41084d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 41094d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 41104d454574SPaolo Bonzini },{ 4111a9384affSPaolo Bonzini .name = "discard", 4112a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 4113a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 4114a9384affSPaolo Bonzini },{ 41154d454574SPaolo Bonzini .name = "aio", 41164d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41174d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 41184d454574SPaolo Bonzini },{ 41194d454574SPaolo Bonzini .name = "format", 41204d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41214d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 41224d454574SPaolo Bonzini },{ 41234d454574SPaolo Bonzini .name = "rerror", 41244d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41254d454574SPaolo Bonzini .help = "read error action", 41264d454574SPaolo Bonzini },{ 41274d454574SPaolo Bonzini .name = "werror", 41284d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41294d454574SPaolo Bonzini .help = "write error action", 41304d454574SPaolo Bonzini },{ 41310f227a94SKevin Wolf .name = "read-only", 41324d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 41334d454574SPaolo Bonzini .help = "open drive file as read-only", 41344d454574SPaolo Bonzini },{ 413557975222SKevin Wolf .name = "throttling.iops-total", 41364d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41374d454574SPaolo Bonzini .help = "limit total I/O operations per second", 41384d454574SPaolo Bonzini },{ 413957975222SKevin Wolf .name = "throttling.iops-read", 41404d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41414d454574SPaolo Bonzini .help = "limit read operations per second", 41424d454574SPaolo Bonzini },{ 414357975222SKevin Wolf .name = "throttling.iops-write", 41444d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41454d454574SPaolo Bonzini .help = "limit write operations per second", 41464d454574SPaolo Bonzini },{ 414757975222SKevin Wolf .name = "throttling.bps-total", 41484d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41494d454574SPaolo Bonzini .help = "limit total bytes per second", 41504d454574SPaolo Bonzini },{ 415157975222SKevin Wolf .name = "throttling.bps-read", 41524d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41534d454574SPaolo Bonzini .help = "limit read bytes per second", 41544d454574SPaolo Bonzini },{ 415557975222SKevin Wolf .name = "throttling.bps-write", 41564d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 41574d454574SPaolo Bonzini .help = "limit write bytes per second", 41584d454574SPaolo Bonzini },{ 41593e9fab69SBenoît Canet .name = "throttling.iops-total-max", 41603e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41613e9fab69SBenoît Canet .help = "I/O operations burst", 41623e9fab69SBenoît Canet },{ 41633e9fab69SBenoît Canet .name = "throttling.iops-read-max", 41643e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41653e9fab69SBenoît Canet .help = "I/O operations read burst", 41663e9fab69SBenoît Canet },{ 41673e9fab69SBenoît Canet .name = "throttling.iops-write-max", 41683e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41693e9fab69SBenoît Canet .help = "I/O operations write burst", 41703e9fab69SBenoît Canet },{ 41713e9fab69SBenoît Canet .name = "throttling.bps-total-max", 41723e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41733e9fab69SBenoît Canet .help = "total bytes burst", 41743e9fab69SBenoît Canet },{ 41753e9fab69SBenoît Canet .name = "throttling.bps-read-max", 41763e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41773e9fab69SBenoît Canet .help = "total bytes read burst", 41783e9fab69SBenoît Canet },{ 41793e9fab69SBenoît Canet .name = "throttling.bps-write-max", 41803e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 41813e9fab69SBenoît Canet .help = "total bytes write burst", 41823e9fab69SBenoît Canet },{ 41838a0fc18dSAlberto Garcia .name = "throttling.iops-total-max-length", 41848a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 41858a0fc18dSAlberto Garcia .help = "length of the iops-total-max burst period, in seconds", 41868a0fc18dSAlberto Garcia },{ 41878a0fc18dSAlberto Garcia .name = "throttling.iops-read-max-length", 41888a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 41898a0fc18dSAlberto Garcia .help = "length of the iops-read-max burst period, in seconds", 41908a0fc18dSAlberto Garcia },{ 41918a0fc18dSAlberto Garcia .name = "throttling.iops-write-max-length", 41928a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 41938a0fc18dSAlberto Garcia .help = "length of the iops-write-max burst period, in seconds", 41948a0fc18dSAlberto Garcia },{ 41958a0fc18dSAlberto Garcia .name = "throttling.bps-total-max-length", 41968a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 41978a0fc18dSAlberto Garcia .help = "length of the bps-total-max burst period, in seconds", 41988a0fc18dSAlberto Garcia },{ 41998a0fc18dSAlberto Garcia .name = "throttling.bps-read-max-length", 42008a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42018a0fc18dSAlberto Garcia .help = "length of the bps-read-max burst period, in seconds", 42028a0fc18dSAlberto Garcia },{ 42038a0fc18dSAlberto Garcia .name = "throttling.bps-write-max-length", 42048a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42058a0fc18dSAlberto Garcia .help = "length of the bps-write-max burst period, in seconds", 42068a0fc18dSAlberto Garcia },{ 42072024c1dfSBenoît Canet .name = "throttling.iops-size", 42082024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 42092024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 42102024c1dfSBenoît Canet },{ 421176f4afb4SAlberto Garcia .name = "throttling.group", 421276f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 421376f4afb4SAlberto Garcia .help = "name of the block throttling group", 421476f4afb4SAlberto Garcia },{ 42154d454574SPaolo Bonzini .name = "copy-on-read", 42164d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 42174d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4218465bee1dSPeter Lieven },{ 4219465bee1dSPeter Lieven .name = "detect-zeroes", 4220465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4221465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4222362e9299SAlberto Garcia },{ 4223362e9299SAlberto Garcia .name = "stats-account-invalid", 4224362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4225362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4226362e9299SAlberto Garcia "in the statistics", 4227362e9299SAlberto Garcia },{ 4228362e9299SAlberto Garcia .name = "stats-account-failed", 4229362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4230362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4231362e9299SAlberto Garcia "in the statistics", 42324d454574SPaolo Bonzini }, 42334d454574SPaolo Bonzini { /* end of list */ } 42344d454574SPaolo Bonzini }, 42354d454574SPaolo Bonzini }; 42360006383eSKevin Wolf 4237bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 4238bd745e23SMax Reitz .name = "root-bds", 423923f7fcb2SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head), 4240bd745e23SMax Reitz .desc = { 4241bd745e23SMax Reitz { 4242bd745e23SMax Reitz .name = "discard", 4243bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4244bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 4245bd745e23SMax Reitz },{ 4246bd745e23SMax Reitz .name = "aio", 4247bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4248bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 4249bd745e23SMax Reitz },{ 4250bd745e23SMax Reitz .name = "read-only", 4251bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4252bd745e23SMax Reitz .help = "open drive file as read-only", 4253bd745e23SMax Reitz },{ 4254bd745e23SMax Reitz .name = "copy-on-read", 4255bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4256bd745e23SMax Reitz .help = "copy read data from backing file into image file", 4257bd745e23SMax Reitz },{ 4258bd745e23SMax Reitz .name = "detect-zeroes", 4259bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4260bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 4261bd745e23SMax Reitz }, 4262bd745e23SMax Reitz { /* end of list */ } 4263bd745e23SMax Reitz }, 4264bd745e23SMax Reitz }; 4265bd745e23SMax Reitz 42660006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 42670006383eSKevin Wolf .name = "drive", 42680006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 42690006383eSKevin Wolf .desc = { 4270492fdc6fSKevin Wolf /* 4271492fdc6fSKevin Wolf * no elements => accept any params 4272492fdc6fSKevin Wolf * validation will happen later 4273492fdc6fSKevin Wolf */ 42740006383eSKevin Wolf { /* end of list */ } 42750006383eSKevin Wolf }, 42760006383eSKevin Wolf }; 4277