1666daa68SMarkus Armbruster /* 2666daa68SMarkus Armbruster * QEMU host block devices 3666daa68SMarkus Armbruster * 4666daa68SMarkus Armbruster * Copyright (c) 2003-2008 Fabrice Bellard 5666daa68SMarkus Armbruster * 6666daa68SMarkus Armbruster * This work is licensed under the terms of the GNU GPL, version 2 or 7666daa68SMarkus Armbruster * later. See the COPYING file in the top-level directory. 83618a094SMarkus Armbruster * 93618a094SMarkus Armbruster * This file incorporates work covered by the following copyright and 103618a094SMarkus Armbruster * permission notice: 113618a094SMarkus Armbruster * 123618a094SMarkus Armbruster * Copyright (c) 2003-2008 Fabrice Bellard 133618a094SMarkus Armbruster * 143618a094SMarkus Armbruster * Permission is hereby granted, free of charge, to any person obtaining a copy 153618a094SMarkus Armbruster * of this software and associated documentation files (the "Software"), to deal 163618a094SMarkus Armbruster * in the Software without restriction, including without limitation the rights 173618a094SMarkus Armbruster * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 183618a094SMarkus Armbruster * copies of the Software, and to permit persons to whom the Software is 193618a094SMarkus Armbruster * furnished to do so, subject to the following conditions: 203618a094SMarkus Armbruster * 213618a094SMarkus Armbruster * The above copyright notice and this permission notice shall be included in 223618a094SMarkus Armbruster * all copies or substantial portions of the Software. 233618a094SMarkus Armbruster * 243618a094SMarkus Armbruster * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 253618a094SMarkus Armbruster * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 263618a094SMarkus Armbruster * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 273618a094SMarkus Armbruster * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 283618a094SMarkus Armbruster * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 293618a094SMarkus Armbruster * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 303618a094SMarkus Armbruster * THE SOFTWARE. 31666daa68SMarkus Armbruster */ 32666daa68SMarkus Armbruster 33d38ea87aSPeter Maydell #include "qemu/osdep.h" 3426f54e9aSMarkus Armbruster #include "sysemu/block-backend.h" 359c17d615SPaolo Bonzini #include "sysemu/blockdev.h" 360d09e41aSPaolo Bonzini #include "hw/block/block.h" 37737e150eSPaolo Bonzini #include "block/blockjob.h" 3876f4afb4SAlberto Garcia #include "block/throttle-groups.h" 3983c9089eSPaolo Bonzini #include "monitor/monitor.h" 40d49b6836SMarkus Armbruster #include "qemu/error-report.h" 411de7afc9SPaolo Bonzini #include "qemu/option.h" 421de7afc9SPaolo Bonzini #include "qemu/config-file.h" 437b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h" 44d26c9a15SKevin Wolf #include "qapi-visit.h" 45cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 46d26c9a15SKevin Wolf #include "qapi/qmp-output-visitor.h" 479e7dac7cSPeter Lieven #include "qapi/util.h" 489c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 49737e150eSPaolo Bonzini #include "block/block_int.h" 50a4dea8a9SLuiz Capitulino #include "qmp-commands.h" 5112bd451fSStefan Hajnoczi #include "trace.h" 529c17d615SPaolo Bonzini #include "sysemu/arch_init.h" 53f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 54f348b6d1SVeronia Bahaa #include "qemu/help_option.h" 55666daa68SMarkus Armbruster 569c4218e9SMax Reitz static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = 579c4218e9SMax Reitz QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); 589c4218e9SMax Reitz 591960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 601960966dSMarkus Armbruster [IF_NONE] = "none", 611960966dSMarkus Armbruster [IF_IDE] = "ide", 621960966dSMarkus Armbruster [IF_SCSI] = "scsi", 631960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 641960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 651960966dSMarkus Armbruster [IF_MTD] = "mtd", 661960966dSMarkus Armbruster [IF_SD] = "sd", 671960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 681960966dSMarkus Armbruster [IF_XEN] = "xen", 691960966dSMarkus Armbruster }; 701960966dSMarkus Armbruster 7121dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = { 7227d6bf40SMarkus Armbruster /* 7327d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 7427d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 7527d6bf40SMarkus Armbruster * 76547cb157SWei Jiangang * All controllers used to implement if=T drives need to support 7727d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 7827d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 7927d6bf40SMarkus Armbruster * values. 8027d6bf40SMarkus Armbruster * 8127d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 8227d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 8327d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 8427d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 8527d6bf40SMarkus Armbruster */ 8627d6bf40SMarkus Armbruster [IF_IDE] = 2, 8727d6bf40SMarkus Armbruster [IF_SCSI] = 7, 881960966dSMarkus Armbruster }; 891960966dSMarkus Armbruster 9021dff8cfSJohn Snow /** 9121dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides 9221dff8cfSJohn Snow * of the default, global values. 9321dff8cfSJohn Snow */ 9421dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs) 9521dff8cfSJohn Snow { 9618e46a03SMarkus Armbruster BlockBackend *blk; 9721dff8cfSJohn Snow DriveInfo *dinfo; 9821dff8cfSJohn Snow 9921dff8cfSJohn Snow if (max_devs <= 0) { 10021dff8cfSJohn Snow return; 10121dff8cfSJohn Snow } 10221dff8cfSJohn Snow 10318e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 10418e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 10521dff8cfSJohn Snow if (dinfo->type == type) { 10621dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of" 10721dff8cfSJohn Snow " the %s interface, because a drive of that type has" 10821dff8cfSJohn Snow " already been added.\n", if_name[type]); 10921dff8cfSJohn Snow g_assert_not_reached(); 11021dff8cfSJohn Snow } 11121dff8cfSJohn Snow } 11221dff8cfSJohn Snow 11321dff8cfSJohn Snow if_max_devs[type] = max_devs; 11421dff8cfSJohn Snow } 11521dff8cfSJohn Snow 11614bafc54SMarkus Armbruster /* 11714bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 11814bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 11914bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 12014bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 12114bafc54SMarkus Armbruster * when deletion is actually safe. 12214bafc54SMarkus Armbruster */ 1234be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk) 12414bafc54SMarkus Armbruster { 12518e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 1264be74634SMarkus Armbruster BlockDriverState *bs = blk_bs(blk); 12791fddb0dSStefan Hajnoczi AioContext *aio_context; 12814bafc54SMarkus Armbruster 12926f8b3a8SMarkus Armbruster if (!dinfo) { 1302d246f01SKevin Wolf return; 1312d246f01SKevin Wolf } 1322d246f01SKevin Wolf 1335433c24fSMax Reitz if (bs) { 13491fddb0dSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 13591fddb0dSStefan Hajnoczi aio_context_acquire(aio_context); 13691fddb0dSStefan Hajnoczi 13712bde0eeSPaolo Bonzini if (bs->job) { 13812bde0eeSPaolo Bonzini block_job_cancel(bs->job); 13912bde0eeSPaolo Bonzini } 14091fddb0dSStefan Hajnoczi 14191fddb0dSStefan Hajnoczi aio_context_release(aio_context); 1425433c24fSMax Reitz } 14391fddb0dSStefan Hajnoczi 14414bafc54SMarkus Armbruster dinfo->auto_del = 1; 14514bafc54SMarkus Armbruster } 14614bafc54SMarkus Armbruster 1474be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk) 14814bafc54SMarkus Armbruster { 14918e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 15014bafc54SMarkus Armbruster 1510fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 152efaa7c4eSMax Reitz monitor_remove_blk(blk); 153b9fe8a7aSMarkus Armbruster blk_unref(blk); 15414bafc54SMarkus Armbruster } 15514bafc54SMarkus Armbruster } 15614bafc54SMarkus Armbruster 157d8f94e1bSJohn Snow /** 158d8f94e1bSJohn Snow * Returns the current mapping of how many units per bus 159d8f94e1bSJohn Snow * a particular interface can support. 160d8f94e1bSJohn Snow * 161d8f94e1bSJohn Snow * A positive integer indicates n units per bus. 162d8f94e1bSJohn Snow * 0 implies the mapping has not been established. 163d8f94e1bSJohn Snow * -1 indicates an invalid BlockInterfaceType was given. 164d8f94e1bSJohn Snow */ 165d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type) 166d8f94e1bSJohn Snow { 167d8f94e1bSJohn Snow if (type >= IF_IDE && type < IF_COUNT) { 168d8f94e1bSJohn Snow return if_max_devs[type]; 169d8f94e1bSJohn Snow } 170d8f94e1bSJohn Snow 171d8f94e1bSJohn Snow return -1; 172d8f94e1bSJohn Snow } 173d8f94e1bSJohn Snow 174505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 175505a7fb1SMarkus Armbruster { 176505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 177505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 178505a7fb1SMarkus Armbruster } 179505a7fb1SMarkus Armbruster 180505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 181505a7fb1SMarkus Armbruster { 182505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 183505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 184505a7fb1SMarkus Armbruster } 185505a7fb1SMarkus Armbruster 1862292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 1872292ddaeSMarkus Armbruster { 18870b94331SMarkus Armbruster return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 1892292ddaeSMarkus Armbruster } 1902292ddaeSMarkus Armbruster 1912292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 1925645b0f4SMarkus Armbruster const char *optstr) 193666daa68SMarkus Armbruster { 194666daa68SMarkus Armbruster QemuOpts *opts; 195666daa68SMarkus Armbruster 1962292ddaeSMarkus Armbruster opts = drive_def(optstr); 197666daa68SMarkus Armbruster if (!opts) { 198666daa68SMarkus Armbruster return NULL; 199666daa68SMarkus Armbruster } 2002292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 201f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort); 2022292ddaeSMarkus Armbruster } 2032292ddaeSMarkus Armbruster if (index >= 0) { 204a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort); 2052292ddaeSMarkus Armbruster } 206666daa68SMarkus Armbruster if (file) 207f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort); 208666daa68SMarkus Armbruster return opts; 209666daa68SMarkus Armbruster } 210666daa68SMarkus Armbruster 211666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 212666daa68SMarkus Armbruster { 21318e46a03SMarkus Armbruster BlockBackend *blk; 214666daa68SMarkus Armbruster DriveInfo *dinfo; 215666daa68SMarkus Armbruster 21618e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 21718e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 21818e46a03SMarkus Armbruster if (dinfo && dinfo->type == type 21918e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) { 220666daa68SMarkus Armbruster return dinfo; 221666daa68SMarkus Armbruster } 22218e46a03SMarkus Armbruster } 223666daa68SMarkus Armbruster 224666daa68SMarkus Armbruster return NULL; 225666daa68SMarkus Armbruster } 226666daa68SMarkus Armbruster 227a66c9dc7SJohn Snow bool drive_check_orphaned(void) 228a66c9dc7SJohn Snow { 22918e46a03SMarkus Armbruster BlockBackend *blk; 230a66c9dc7SJohn Snow DriveInfo *dinfo; 231a66c9dc7SJohn Snow bool rs = false; 232a66c9dc7SJohn Snow 23318e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 23418e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 235a66c9dc7SJohn Snow /* If dinfo->bdrv->dev is NULL, it has no device attached. */ 236a66c9dc7SJohn Snow /* Unless this is a default drive, this may be an oversight. */ 237a7f53e26SMarkus Armbruster if (!blk_get_attached_dev(blk) && !dinfo->is_default && 238a66c9dc7SJohn Snow dinfo->type != IF_NONE) { 239a66c9dc7SJohn Snow fprintf(stderr, "Warning: Orphaned drive without device: " 240a66c9dc7SJohn Snow "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", 2415433c24fSMax Reitz blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "", 2425433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit); 243a66c9dc7SJohn Snow rs = true; 244a66c9dc7SJohn Snow } 245a66c9dc7SJohn Snow } 246a66c9dc7SJohn Snow 247a66c9dc7SJohn Snow return rs; 248a66c9dc7SJohn Snow } 249a66c9dc7SJohn Snow 250f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 251f1bd51acSMarkus Armbruster { 252f1bd51acSMarkus Armbruster return drive_get(type, 253f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 254f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 255f1bd51acSMarkus Armbruster } 256f1bd51acSMarkus Armbruster 257666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 258666daa68SMarkus Armbruster { 259666daa68SMarkus Armbruster int max_bus; 26018e46a03SMarkus Armbruster BlockBackend *blk; 261666daa68SMarkus Armbruster DriveInfo *dinfo; 262666daa68SMarkus Armbruster 263666daa68SMarkus Armbruster max_bus = -1; 26418e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 26518e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 26618e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 267666daa68SMarkus Armbruster max_bus = dinfo->bus; 268666daa68SMarkus Armbruster } 26918e46a03SMarkus Armbruster } 270666daa68SMarkus Armbruster return max_bus; 271666daa68SMarkus Armbruster } 272666daa68SMarkus Armbruster 27313839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 27413839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 27513839974SMarkus Armbruster appropriate bus. */ 27613839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 27713839974SMarkus Armbruster { 27813839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 27913839974SMarkus Armbruster 28013839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 28113839974SMarkus Armbruster } 28213839974SMarkus Armbruster 283666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 284666daa68SMarkus Armbruster { 285807105a7SMarkus Armbruster error_printf(" %s", name); 286666daa68SMarkus Armbruster } 287666daa68SMarkus Armbruster 288aa398a5cSStefan Hajnoczi typedef struct { 289aa398a5cSStefan Hajnoczi QEMUBH *bh; 290fa510ebfSFam Zheng BlockDriverState *bs; 291fa510ebfSFam Zheng } BDRVPutRefBH; 292aa398a5cSStefan Hajnoczi 293b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 294666daa68SMarkus Armbruster { 295666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 29692aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 297666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 29892aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 299666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 30092aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 301666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 30292aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 303666daa68SMarkus Armbruster } else { 304b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 305666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 306666daa68SMarkus Armbruster return -1; 307666daa68SMarkus Armbruster } 308666daa68SMarkus Armbruster } 309666daa68SMarkus Armbruster 31040119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 31140119effSAlberto Garcia Error **errp) 31240119effSAlberto Garcia { 31340119effSAlberto Garcia const QListEntry *entry; 31440119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 31540119effSAlberto Garcia switch (qobject_type(entry->value)) { 31640119effSAlberto Garcia 31740119effSAlberto Garcia case QTYPE_QSTRING: { 31840119effSAlberto Garcia unsigned long long length; 31940119effSAlberto Garcia const char *str = qstring_get_str(qobject_to_qstring(entry->value)); 32040119effSAlberto Garcia if (parse_uint_full(str, &length, 10) == 0 && 32140119effSAlberto Garcia length > 0 && length <= UINT_MAX) { 32240119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 32340119effSAlberto Garcia } else { 32440119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str); 32540119effSAlberto Garcia return false; 32640119effSAlberto Garcia } 32740119effSAlberto Garcia break; 32840119effSAlberto Garcia } 32940119effSAlberto Garcia 33040119effSAlberto Garcia case QTYPE_QINT: { 33140119effSAlberto Garcia int64_t length = qint_get_int(qobject_to_qint(entry->value)); 33240119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) { 33340119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 33440119effSAlberto Garcia } else { 33540119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length); 33640119effSAlberto Garcia return false; 33740119effSAlberto Garcia } 33840119effSAlberto Garcia break; 33940119effSAlberto Garcia } 34040119effSAlberto Garcia 34140119effSAlberto Garcia default: 34240119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid"); 34340119effSAlberto Garcia return false; 34440119effSAlberto Garcia } 34540119effSAlberto Garcia } 34640119effSAlberto Garcia return true; 34740119effSAlberto Garcia } 34840119effSAlberto Garcia 34933cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 35033cb7dc8SKevin Wolf 351fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 352fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 353fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 354fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 355fbf8175eSMax Reitz { 356fbf8175eSMax Reitz const char *discard; 357fbf8175eSMax Reitz Error *local_error = NULL; 358fbf8175eSMax Reitz const char *aio; 359fbf8175eSMax Reitz 360fbf8175eSMax Reitz if (bdrv_flags) { 361fbf8175eSMax Reitz if (!qemu_opt_get_bool(opts, "read-only", false)) { 362fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_RDWR; 363fbf8175eSMax Reitz } 364fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 365fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 366fbf8175eSMax Reitz } 367fbf8175eSMax Reitz 368fbf8175eSMax Reitz if ((discard = qemu_opt_get(opts, "discard")) != NULL) { 369fbf8175eSMax Reitz if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { 370fbf8175eSMax Reitz error_setg(errp, "Invalid discard option"); 371fbf8175eSMax Reitz return; 372fbf8175eSMax Reitz } 373fbf8175eSMax Reitz } 374fbf8175eSMax Reitz 375fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 376fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 377fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 378fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 379fbf8175eSMax Reitz /* this is the default */ 380fbf8175eSMax Reitz } else { 381fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 382fbf8175eSMax Reitz return; 383fbf8175eSMax Reitz } 384fbf8175eSMax Reitz } 385fbf8175eSMax Reitz } 386fbf8175eSMax Reitz 387fbf8175eSMax Reitz /* disk I/O throttling */ 388fbf8175eSMax Reitz if (throttling_group) { 389fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 390fbf8175eSMax Reitz } 391fbf8175eSMax Reitz 392fbf8175eSMax Reitz if (throttle_cfg) { 3931588ab5dSAlberto Garcia throttle_config_init(throttle_cfg); 394fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 395fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 396fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 397fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 398fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 399fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 400fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 401fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 402fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 403fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 404fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 405fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 406fbf8175eSMax Reitz 407fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 408fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 409fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 410fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 411fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 412fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 413fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 414fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 415fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 416fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 417fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 418fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 419fbf8175eSMax Reitz 4208a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = 4218a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); 4228a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = 4238a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); 4248a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = 4258a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); 4268a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = 4278a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); 4288a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = 4298a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); 4308a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = 4318a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); 4328a0fc18dSAlberto Garcia 433fbf8175eSMax Reitz throttle_cfg->op_size = 434fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 435fbf8175eSMax Reitz 436d5851089SAlberto Garcia if (!throttle_is_valid(throttle_cfg, errp)) { 437fbf8175eSMax Reitz return; 438fbf8175eSMax Reitz } 439fbf8175eSMax Reitz } 440fbf8175eSMax Reitz 441fbf8175eSMax Reitz if (detect_zeroes) { 442fbf8175eSMax Reitz *detect_zeroes = 443fbf8175eSMax Reitz qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, 444fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 4457fb1cf16SEric Blake BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX, 446fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 447fbf8175eSMax Reitz &local_error); 448fbf8175eSMax Reitz if (local_error) { 449fbf8175eSMax Reitz error_propagate(errp, local_error); 450fbf8175eSMax Reitz return; 451fbf8175eSMax Reitz } 452fbf8175eSMax Reitz 453fbf8175eSMax Reitz if (bdrv_flags && 454fbf8175eSMax Reitz *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 455fbf8175eSMax Reitz !(*bdrv_flags & BDRV_O_UNMAP)) 456fbf8175eSMax Reitz { 457fbf8175eSMax Reitz error_setg(errp, "setting detect-zeroes to unmap is not allowed " 458fbf8175eSMax Reitz "without setting discard operation to unmap"); 459fbf8175eSMax Reitz return; 460fbf8175eSMax Reitz } 461fbf8175eSMax Reitz } 462fbf8175eSMax Reitz } 463fbf8175eSMax Reitz 464f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 46518e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 466b681072dSKevin Wolf Error **errp) 467666daa68SMarkus Armbruster { 468666daa68SMarkus Armbruster const char *buf; 469666daa68SMarkus Armbruster int bdrv_flags = 0; 470666daa68SMarkus Armbruster int on_read_error, on_write_error; 471362e9299SAlberto Garcia bool account_invalid, account_failed; 472e4b24b49SKevin Wolf bool writethrough; 47326f54e9aSMarkus Armbruster BlockBackend *blk; 474a0f1eab1SMarkus Armbruster BlockDriverState *bs; 475cc0681c4SBenoît Canet ThrottleConfig cfg; 476666daa68SMarkus Armbruster int snapshot = 0; 477c546194fSStefan Hajnoczi Error *error = NULL; 4780006383eSKevin Wolf QemuOpts *opts; 47940119effSAlberto Garcia QDict *interval_dict = NULL; 48040119effSAlberto Garcia QList *interval_list = NULL; 4810006383eSKevin Wolf const char *id; 482fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 483fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 484fbf8175eSMax Reitz const char *throttling_group = NULL; 485666daa68SMarkus Armbruster 486f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 487f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 488f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4890006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 49084d18f06SMarkus Armbruster if (error) { 491b681072dSKevin Wolf error_propagate(errp, error); 4926376f952SMarkus Armbruster goto err_no_opts; 4930006383eSKevin Wolf } 4940006383eSKevin Wolf 4950006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49684d18f06SMarkus Armbruster if (error) { 497b681072dSKevin Wolf error_propagate(errp, error); 498ec9c10d2SStefan Hajnoczi goto early_err; 4990006383eSKevin Wolf } 5000006383eSKevin Wolf 5010006383eSKevin Wolf if (id) { 5020006383eSKevin Wolf qdict_del(bs_opts, "id"); 5030006383eSKevin Wolf } 5040006383eSKevin Wolf 505666daa68SMarkus Armbruster /* extract parameters */ 506666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 507666daa68SMarkus Armbruster 508362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 509362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 510362e9299SAlberto Garcia 511e4b24b49SKevin Wolf writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); 512e4b24b49SKevin Wolf 51340119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51440119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51540119effSAlberto Garcia 51640119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 51740119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 51840119effSAlberto Garcia qdict_first(interval_dict)->key); 51940119effSAlberto Garcia goto early_err; 52040119effSAlberto Garcia } 5212be5506fSAlberto Garcia 522fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 523fbf8175eSMax Reitz &detect_zeroes, &error); 524fbf8175eSMax Reitz if (error) { 525fbf8175eSMax Reitz error_propagate(errp, error); 526ec9c10d2SStefan Hajnoczi goto early_err; 527a9384affSPaolo Bonzini } 528666daa68SMarkus Armbruster 529666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 530c8057f95SPeter Maydell if (is_help_option(buf)) { 531807105a7SMarkus Armbruster error_printf("Supported formats:"); 532666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 533807105a7SMarkus Armbruster error_printf("\n"); 534ec9c10d2SStefan Hajnoczi goto early_err; 535666daa68SMarkus Armbruster } 53674fe54f2SKevin Wolf 537e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 538e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 539ec9c10d2SStefan Hajnoczi goto early_err; 5406db5f5d6SMike Qiu } 541e4342ce5SMax Reitz qdict_put(bs_opts, "driver", qstring_from_str(buf)); 542666daa68SMarkus Armbruster } 543666daa68SMarkus Armbruster 54492aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 545666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 546b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 54784d18f06SMarkus Armbruster if (error) { 548b681072dSKevin Wolf error_propagate(errp, error); 549ec9c10d2SStefan Hajnoczi goto early_err; 550666daa68SMarkus Armbruster } 551666daa68SMarkus Armbruster } 552666daa68SMarkus Armbruster 55392aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 554666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 555b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 55684d18f06SMarkus Armbruster if (error) { 557b681072dSKevin Wolf error_propagate(errp, error); 558ec9c10d2SStefan Hajnoczi goto early_err; 559666daa68SMarkus Armbruster } 560666daa68SMarkus Armbruster } 561666daa68SMarkus Armbruster 562666daa68SMarkus Armbruster if (snapshot) { 56391a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 564666daa68SMarkus Armbruster } 565666daa68SMarkus Armbruster 5665ec18f8cSMax Reitz /* init */ 56739c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5685ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5695ec18f8cSMax Reitz 570109525adSMax Reitz blk = blk_new(); 5715ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5725ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 573fbf8175eSMax Reitz blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); 5745ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5755ec18f8cSMax Reitz 5765ec18f8cSMax Reitz QDECREF(bs_opts); 5775ec18f8cSMax Reitz } else { 5785ec18f8cSMax Reitz if (file && !*file) { 5795ec18f8cSMax Reitz file = NULL; 5805ec18f8cSMax Reitz } 5815ec18f8cSMax Reitz 58291a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 58391a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 58491a097e7SKevin Wolf * Apply the defaults here instead. */ 58591a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 58691a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 58772e775c7SKevin Wolf assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); 58891a097e7SKevin Wolf 58912d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 59012d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 59112d5ee3aSKevin Wolf } 59212d5ee3aSKevin Wolf 593efaa7c4eSMax Reitz blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); 594e4342ce5SMax Reitz if (!blk) { 595e4342ce5SMax Reitz goto err_no_bs_opts; 596e4342ce5SMax Reitz } 597e4342ce5SMax Reitz bs = blk_bs(blk); 5980006383eSKevin Wolf 599e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 600e4342ce5SMax Reitz 601a0f1eab1SMarkus Armbruster if (bdrv_key_required(bs)) { 602666daa68SMarkus Armbruster autostart = 0; 603a0f1eab1SMarkus Armbruster } 604362e9299SAlberto Garcia 605362e9299SAlberto Garcia block_acct_init(blk_get_stats(blk), account_invalid, account_failed); 6062be5506fSAlberto Garcia 60740119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6082be5506fSAlberto Garcia blk_unref(blk); 6092be5506fSAlberto Garcia blk = NULL; 6102be5506fSAlberto Garcia goto err_no_bs_opts; 6112be5506fSAlberto Garcia } 6122be5506fSAlberto Garcia } 6135ec18f8cSMax Reitz 6147ca7f0f6SKevin Wolf /* disk I/O throttling */ 6157ca7f0f6SKevin Wolf if (throttle_enabled(&cfg)) { 6167ca7f0f6SKevin Wolf if (!throttling_group) { 6177ca7f0f6SKevin Wolf throttling_group = blk_name(blk); 6187ca7f0f6SKevin Wolf } 6197ca7f0f6SKevin Wolf blk_io_limits_enable(blk, throttling_group); 6207ca7f0f6SKevin Wolf blk_set_io_limits(blk, &cfg); 6217ca7f0f6SKevin Wolf } 6227ca7f0f6SKevin Wolf 623e4b24b49SKevin Wolf blk_set_enable_write_cache(blk, !writethrough); 6245ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6250006383eSKevin Wolf 626efaa7c4eSMax Reitz if (!monitor_add_blk(blk, qemu_opts_id(opts), errp)) { 627efaa7c4eSMax Reitz blk_unref(blk); 628efaa7c4eSMax Reitz blk = NULL; 629efaa7c4eSMax Reitz goto err_no_bs_opts; 630efaa7c4eSMax Reitz } 631efaa7c4eSMax Reitz 632e4342ce5SMax Reitz err_no_bs_opts: 6330006383eSKevin Wolf qemu_opts_del(opts); 63440119effSAlberto Garcia QDECREF(interval_dict); 63540119effSAlberto Garcia QDECREF(interval_list); 63618e46a03SMarkus Armbruster return blk; 637a9ae2bffSMarkus Armbruster 638ec9c10d2SStefan Hajnoczi early_err: 639ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 64040119effSAlberto Garcia QDECREF(interval_dict); 64140119effSAlberto Garcia QDECREF(interval_list); 6426376f952SMarkus Armbruster err_no_opts: 6436376f952SMarkus Armbruster QDECREF(bs_opts); 644a9ae2bffSMarkus Armbruster return NULL; 645666daa68SMarkus Armbruster } 646666daa68SMarkus Armbruster 647bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts; 648bd745e23SMax Reitz 649bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 650bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 651bd745e23SMax Reitz { 652bd745e23SMax Reitz BlockDriverState *bs; 653bd745e23SMax Reitz QemuOpts *opts; 654bd745e23SMax Reitz Error *local_error = NULL; 655bd745e23SMax Reitz BlockdevDetectZeroesOptions detect_zeroes; 656bd745e23SMax Reitz int bdrv_flags = 0; 657bd745e23SMax Reitz 658bd745e23SMax Reitz opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); 659bd745e23SMax Reitz if (!opts) { 660bd745e23SMax Reitz goto fail; 661bd745e23SMax Reitz } 662bd745e23SMax Reitz 663bd745e23SMax Reitz qemu_opts_absorb_qdict(opts, bs_opts, &local_error); 664bd745e23SMax Reitz if (local_error) { 665bd745e23SMax Reitz error_propagate(errp, local_error); 666bd745e23SMax Reitz goto fail; 667bd745e23SMax Reitz } 668bd745e23SMax Reitz 669bd745e23SMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, 670bd745e23SMax Reitz &detect_zeroes, &local_error); 671bd745e23SMax Reitz if (local_error) { 672bd745e23SMax Reitz error_propagate(errp, local_error); 673bd745e23SMax Reitz goto fail; 674bd745e23SMax Reitz } 675bd745e23SMax Reitz 676a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 677a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults. 678a81d6164SKevin Wolf * Apply the defaults here instead. */ 679a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 680a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 681a81d6164SKevin Wolf 68212d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 68312d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 68412d5ee3aSKevin Wolf } 68512d5ee3aSKevin Wolf 6865b363937SMax Reitz bs = bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); 6875b363937SMax Reitz if (!bs) { 688bd745e23SMax Reitz goto fail_no_bs_opts; 689bd745e23SMax Reitz } 690bd745e23SMax Reitz 691bd745e23SMax Reitz bs->detect_zeroes = detect_zeroes; 692bd745e23SMax Reitz 693bd745e23SMax Reitz fail_no_bs_opts: 694bd745e23SMax Reitz qemu_opts_del(opts); 695bd745e23SMax Reitz return bs; 696bd745e23SMax Reitz 697bd745e23SMax Reitz fail: 698bd745e23SMax Reitz qemu_opts_del(opts); 699bd745e23SMax Reitz QDECREF(bs_opts); 700bd745e23SMax Reitz return NULL; 701bd745e23SMax Reitz } 702bd745e23SMax Reitz 7039c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void) 7049c4218e9SMax Reitz { 7059c4218e9SMax Reitz BlockDriverState *bs, *next_bs; 7069c4218e9SMax Reitz 7079c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { 7089c4218e9SMax Reitz AioContext *ctx = bdrv_get_aio_context(bs); 7099c4218e9SMax Reitz 7109c4218e9SMax Reitz aio_context_acquire(ctx); 7119c4218e9SMax Reitz bdrv_unref(bs); 7129c4218e9SMax Reitz aio_context_release(ctx); 7139c4218e9SMax Reitz } 7149c4218e9SMax Reitz } 7159c4218e9SMax Reitz 716262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */ 717262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) 718262b4e8fSMax Reitz { 719262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list) 720262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states); 721262b4e8fSMax Reitz } 722262b4e8fSMax Reitz 7235abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 7245abbf0eeSKevin Wolf Error **errp) 72557975222SKevin Wolf { 72657975222SKevin Wolf const char *value; 72757975222SKevin Wolf 72857975222SKevin Wolf value = qemu_opt_get(opts, from); 72957975222SKevin Wolf if (value) { 7305abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 7315abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 7325abbf0eeSKevin Wolf "same time", to, from); 7335abbf0eeSKevin Wolf return; 7345abbf0eeSKevin Wolf } 73520d6cd47SJun Li } 73620d6cd47SJun Li 73720d6cd47SJun Li /* rename all items in opts */ 73820d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 739f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 74057975222SKevin Wolf qemu_opt_unset(opts, from); 74157975222SKevin Wolf } 74257975222SKevin Wolf } 74357975222SKevin Wolf 74433cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 74533cb7dc8SKevin Wolf .name = "drive", 74633cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 74733cb7dc8SKevin Wolf .desc = { 74833cb7dc8SKevin Wolf { 74987a899c5SKevin Wolf .name = "bus", 75087a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 75187a899c5SKevin Wolf .help = "bus number", 75287a899c5SKevin Wolf },{ 75387a899c5SKevin Wolf .name = "unit", 75487a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 75587a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 75687a899c5SKevin Wolf },{ 75787a899c5SKevin Wolf .name = "index", 75887a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 75987a899c5SKevin Wolf .help = "index number", 76087a899c5SKevin Wolf },{ 76133cb7dc8SKevin Wolf .name = "media", 76233cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 76333cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 764593d464bSKevin Wolf },{ 765593d464bSKevin Wolf .name = "if", 766593d464bSKevin Wolf .type = QEMU_OPT_STRING, 767593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 768b41a7338SKevin Wolf },{ 769b41a7338SKevin Wolf .name = "cyls", 770b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 771b41a7338SKevin Wolf .help = "number of cylinders (ide disk geometry)", 772b41a7338SKevin Wolf },{ 773b41a7338SKevin Wolf .name = "heads", 774b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 775b41a7338SKevin Wolf .help = "number of heads (ide disk geometry)", 776b41a7338SKevin Wolf },{ 777b41a7338SKevin Wolf .name = "secs", 778b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 779b41a7338SKevin Wolf .help = "number of sectors (ide disk geometry)", 780b41a7338SKevin Wolf },{ 781b41a7338SKevin Wolf .name = "trans", 782b41a7338SKevin Wolf .type = QEMU_OPT_STRING, 783b41a7338SKevin Wolf .help = "chs translation (auto, lba, none)", 78426929298SKevin Wolf },{ 78526929298SKevin Wolf .name = "boot", 78626929298SKevin Wolf .type = QEMU_OPT_BOOL, 78726929298SKevin Wolf .help = "(deprecated, ignored)", 788394c7d4dSKevin Wolf },{ 789394c7d4dSKevin Wolf .name = "addr", 790394c7d4dSKevin Wolf .type = QEMU_OPT_STRING, 791394c7d4dSKevin Wolf .help = "pci address (virtio only)", 792d095b465SMax Reitz },{ 793bcf83158SKevin Wolf .name = "serial", 794bcf83158SKevin Wolf .type = QEMU_OPT_STRING, 795bcf83158SKevin Wolf .help = "disk serial number", 796bcf83158SKevin Wolf },{ 797d095b465SMax Reitz .name = "file", 798d095b465SMax Reitz .type = QEMU_OPT_STRING, 799d095b465SMax Reitz .help = "file name", 80033cb7dc8SKevin Wolf }, 8010ebd24e0SKevin Wolf 8020ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 8030ebd24e0SKevin Wolf { 8040ebd24e0SKevin Wolf .name = "read-only", 8050ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8060ebd24e0SKevin Wolf .help = "open drive file as read-only", 8070ebd24e0SKevin Wolf },{ 808ee13ed1cSKevin Wolf .name = "rerror", 809ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 810ee13ed1cSKevin Wolf .help = "read error action", 811ee13ed1cSKevin Wolf },{ 812ee13ed1cSKevin Wolf .name = "werror", 813ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 814ee13ed1cSKevin Wolf .help = "write error action", 815ee13ed1cSKevin Wolf },{ 8160ebd24e0SKevin Wolf .name = "copy-on-read", 8170ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8180ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 8190ebd24e0SKevin Wolf }, 8200ebd24e0SKevin Wolf 82133cb7dc8SKevin Wolf { /* end of list */ } 82233cb7dc8SKevin Wolf }, 82333cb7dc8SKevin Wolf }; 82433cb7dc8SKevin Wolf 82560e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 82657975222SKevin Wolf { 82729c4e2b5SKevin Wolf const char *value; 82818e46a03SMarkus Armbruster BlockBackend *blk; 82933cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 830f298d071SKevin Wolf QDict *bs_opts; 83133cb7dc8SKevin Wolf QemuOpts *legacy_opts; 83233cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 833593d464bSKevin Wolf BlockInterfaceType type; 834b41a7338SKevin Wolf int cyls, heads, secs, translation; 83587a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 836394c7d4dSKevin Wolf const char *devaddr; 837ee13ed1cSKevin Wolf const char *werror, *rerror; 838a7fdbcf0SFam Zheng bool read_only = false; 839a7fdbcf0SFam Zheng bool copy_on_read; 840bcf83158SKevin Wolf const char *serial; 841d095b465SMax Reitz const char *filename; 84233cb7dc8SKevin Wolf Error *local_err = NULL; 843247147fbSKevin Wolf int i; 84429c4e2b5SKevin Wolf 84557975222SKevin Wolf /* Change legacy command line options into QMP ones */ 846247147fbSKevin Wolf static const struct { 847247147fbSKevin Wolf const char *from; 848247147fbSKevin Wolf const char *to; 849247147fbSKevin Wolf } opt_renames[] = { 850247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 851247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 852247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 85357975222SKevin Wolf 854247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 855247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 856247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 85757975222SKevin Wolf 858247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 859247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 860247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8613e9fab69SBenoît Canet 862247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 863247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 864247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8653e9fab69SBenoît Canet 866247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8672024c1dfSBenoît Canet 86876f4afb4SAlberto Garcia { "group", "throttling.group" }, 86976f4afb4SAlberto Garcia 870247147fbSKevin Wolf { "readonly", "read-only" }, 871247147fbSKevin Wolf }; 872247147fbSKevin Wolf 873247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8745abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8755abbf0eeSKevin Wolf &local_err); 8765abbf0eeSKevin Wolf if (local_err) { 877565f65d2SMarkus Armbruster error_report_err(local_err); 8785abbf0eeSKevin Wolf return NULL; 8795abbf0eeSKevin Wolf } 880247147fbSKevin Wolf } 8810f227a94SKevin Wolf 88229c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 88329c4e2b5SKevin Wolf if (value) { 88429c4e2b5SKevin Wolf int flags = 0; 88504feb4a5SKevin Wolf bool writethrough; 88629c4e2b5SKevin Wolf 88704feb4a5SKevin Wolf if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { 88829c4e2b5SKevin Wolf error_report("invalid cache option"); 88929c4e2b5SKevin Wolf return NULL; 89029c4e2b5SKevin Wolf } 89129c4e2b5SKevin Wolf 89229c4e2b5SKevin Wolf /* Specific options take precedence */ 89354861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 89454861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 89504feb4a5SKevin Wolf !writethrough, &error_abort); 89629c4e2b5SKevin Wolf } 89754861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 89854861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 899cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 90029c4e2b5SKevin Wolf } 90154861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 90254861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 903cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 90429c4e2b5SKevin Wolf } 90529c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 90629c4e2b5SKevin Wolf } 90729c4e2b5SKevin Wolf 908f298d071SKevin Wolf /* Get a QDict for processing the options */ 909f298d071SKevin Wolf bs_opts = qdict_new(); 910f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 911f298d071SKevin Wolf 91287ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 91387ea75d5SPeter Crosthwaite &error_abort); 91433cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 91584d18f06SMarkus Armbruster if (local_err) { 916565f65d2SMarkus Armbruster error_report_err(local_err); 91733cb7dc8SKevin Wolf goto fail; 91833cb7dc8SKevin Wolf } 91933cb7dc8SKevin Wolf 92026929298SKevin Wolf /* Deprecated option boot=[on|off] */ 92126929298SKevin Wolf if (qemu_opt_get(legacy_opts, "boot") != NULL) { 92226929298SKevin Wolf fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 92326929298SKevin Wolf "ignored. Future versions will reject this parameter. Please " 92426929298SKevin Wolf "update your scripts.\n"); 92526929298SKevin Wolf } 92626929298SKevin Wolf 92733cb7dc8SKevin Wolf /* Media type */ 92833cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 92933cb7dc8SKevin Wolf if (value) { 93033cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 93133cb7dc8SKevin Wolf media = MEDIA_DISK; 93233cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 93333cb7dc8SKevin Wolf media = MEDIA_CDROM; 934a7fdbcf0SFam Zheng read_only = true; 93533cb7dc8SKevin Wolf } else { 93633cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 93733cb7dc8SKevin Wolf goto fail; 93833cb7dc8SKevin Wolf } 93933cb7dc8SKevin Wolf } 94033cb7dc8SKevin Wolf 9410ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 942a7fdbcf0SFam Zheng read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); 9430ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 9440ebd24e0SKevin Wolf 9450ebd24e0SKevin Wolf if (read_only && copy_on_read) { 9460ebd24e0SKevin Wolf error_report("warning: disabling copy-on-read on read-only drive"); 9470ebd24e0SKevin Wolf copy_on_read = false; 9480ebd24e0SKevin Wolf } 9490ebd24e0SKevin Wolf 9500ebd24e0SKevin Wolf qdict_put(bs_opts, "read-only", 9510ebd24e0SKevin Wolf qstring_from_str(read_only ? "on" : "off")); 9520ebd24e0SKevin Wolf qdict_put(bs_opts, "copy-on-read", 9530ebd24e0SKevin Wolf qstring_from_str(copy_on_read ? "on" :"off")); 9540ebd24e0SKevin Wolf 955593d464bSKevin Wolf /* Controller type */ 956593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 957593d464bSKevin Wolf if (value) { 958593d464bSKevin Wolf for (type = 0; 959593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 960593d464bSKevin Wolf type++) { 961593d464bSKevin Wolf } 962593d464bSKevin Wolf if (type == IF_COUNT) { 963593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 964593d464bSKevin Wolf goto fail; 965593d464bSKevin Wolf } 966593d464bSKevin Wolf } else { 967593d464bSKevin Wolf type = block_default_type; 968593d464bSKevin Wolf } 969593d464bSKevin Wolf 970b41a7338SKevin Wolf /* Geometry */ 971b41a7338SKevin Wolf cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 972b41a7338SKevin Wolf heads = qemu_opt_get_number(legacy_opts, "heads", 0); 973b41a7338SKevin Wolf secs = qemu_opt_get_number(legacy_opts, "secs", 0); 974b41a7338SKevin Wolf 975b41a7338SKevin Wolf if (cyls || heads || secs) { 976b41a7338SKevin Wolf if (cyls < 1) { 977b41a7338SKevin Wolf error_report("invalid physical cyls number"); 978b41a7338SKevin Wolf goto fail; 979b41a7338SKevin Wolf } 980b41a7338SKevin Wolf if (heads < 1) { 981b41a7338SKevin Wolf error_report("invalid physical heads number"); 982b41a7338SKevin Wolf goto fail; 983b41a7338SKevin Wolf } 984b41a7338SKevin Wolf if (secs < 1) { 985b41a7338SKevin Wolf error_report("invalid physical secs number"); 986b41a7338SKevin Wolf goto fail; 987b41a7338SKevin Wolf } 988b41a7338SKevin Wolf } 989b41a7338SKevin Wolf 990b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 991b41a7338SKevin Wolf value = qemu_opt_get(legacy_opts, "trans"); 992b41a7338SKevin Wolf if (value != NULL) { 993b41a7338SKevin Wolf if (!cyls) { 994b41a7338SKevin Wolf error_report("'%s' trans must be used with cyls, heads and secs", 995b41a7338SKevin Wolf value); 996b41a7338SKevin Wolf goto fail; 997b41a7338SKevin Wolf } 998b41a7338SKevin Wolf if (!strcmp(value, "none")) { 999b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_NONE; 1000b41a7338SKevin Wolf } else if (!strcmp(value, "lba")) { 1001b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_LBA; 1002f31c41ffSPaolo Bonzini } else if (!strcmp(value, "large")) { 1003f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_LARGE; 1004f31c41ffSPaolo Bonzini } else if (!strcmp(value, "rechs")) { 1005f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_RECHS; 1006b41a7338SKevin Wolf } else if (!strcmp(value, "auto")) { 1007b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 1008b41a7338SKevin Wolf } else { 1009b41a7338SKevin Wolf error_report("'%s' invalid translation type", value); 1010b41a7338SKevin Wolf goto fail; 1011b41a7338SKevin Wolf } 1012b41a7338SKevin Wolf } 1013b41a7338SKevin Wolf 1014b41a7338SKevin Wolf if (media == MEDIA_CDROM) { 1015b41a7338SKevin Wolf if (cyls || secs || heads) { 1016b41a7338SKevin Wolf error_report("CHS can't be set with media=cdrom"); 1017b41a7338SKevin Wolf goto fail; 1018b41a7338SKevin Wolf } 1019b41a7338SKevin Wolf } 1020b41a7338SKevin Wolf 102187a899c5SKevin Wolf /* Device address specified by bus/unit or index. 102287a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 102387a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 102487a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 102587a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 102687a899c5SKevin Wolf 102787a899c5SKevin Wolf max_devs = if_max_devs[type]; 102887a899c5SKevin Wolf 102987a899c5SKevin Wolf if (index != -1) { 103087a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 103187a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 103287a899c5SKevin Wolf goto fail; 103387a899c5SKevin Wolf } 103487a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 103587a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 103687a899c5SKevin Wolf } 103787a899c5SKevin Wolf 103887a899c5SKevin Wolf if (unit_id == -1) { 103987a899c5SKevin Wolf unit_id = 0; 104087a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 104187a899c5SKevin Wolf unit_id++; 104287a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 104387a899c5SKevin Wolf unit_id -= max_devs; 104487a899c5SKevin Wolf bus_id++; 104587a899c5SKevin Wolf } 104687a899c5SKevin Wolf } 104787a899c5SKevin Wolf } 104887a899c5SKevin Wolf 104987a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 105087a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 105187a899c5SKevin Wolf goto fail; 105287a899c5SKevin Wolf } 105387a899c5SKevin Wolf 105487a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 105587a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 105687a899c5SKevin Wolf bus_id, unit_id, index); 105787a899c5SKevin Wolf goto fail; 105887a899c5SKevin Wolf } 105987a899c5SKevin Wolf 1060bcf83158SKevin Wolf /* Serial number */ 1061bcf83158SKevin Wolf serial = qemu_opt_get(legacy_opts, "serial"); 1062bcf83158SKevin Wolf 106387a899c5SKevin Wolf /* no id supplied -> create one */ 106487a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 106587a899c5SKevin Wolf char *new_id; 106687a899c5SKevin Wolf const char *mediastr = ""; 106787a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 106887a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 106987a899c5SKevin Wolf } 107087a899c5SKevin Wolf if (max_devs) { 107187a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 107287a899c5SKevin Wolf mediastr, unit_id); 107387a899c5SKevin Wolf } else { 107487a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 107587a899c5SKevin Wolf mediastr, unit_id); 107687a899c5SKevin Wolf } 107787a899c5SKevin Wolf qdict_put(bs_opts, "id", qstring_from_str(new_id)); 107887a899c5SKevin Wolf g_free(new_id); 107987a899c5SKevin Wolf } 108087a899c5SKevin Wolf 1081394c7d4dSKevin Wolf /* Add virtio block device */ 1082394c7d4dSKevin Wolf devaddr = qemu_opt_get(legacy_opts, "addr"); 1083394c7d4dSKevin Wolf if (devaddr && type != IF_VIRTIO) { 1084394c7d4dSKevin Wolf error_report("addr is not supported by this bus type"); 1085394c7d4dSKevin Wolf goto fail; 1086394c7d4dSKevin Wolf } 1087394c7d4dSKevin Wolf 1088394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 1089394c7d4dSKevin Wolf QemuOpts *devopts; 109087ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 109187ea75d5SPeter Crosthwaite &error_abort); 1092394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 10931f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1094394c7d4dSKevin Wolf } else { 1095f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1096394c7d4dSKevin Wolf } 1097f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1098f43e47dbSMarkus Armbruster &error_abort); 1099394c7d4dSKevin Wolf if (devaddr) { 1100f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1101394c7d4dSKevin Wolf } 1102394c7d4dSKevin Wolf } 1103394c7d4dSKevin Wolf 1104d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 1105d095b465SMax Reitz 1106ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 1107ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 1108ee13ed1cSKevin Wolf if (werror != NULL) { 1109ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1110ee13ed1cSKevin Wolf type != IF_NONE) { 1111ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 1112ee13ed1cSKevin Wolf goto fail; 1113ee13ed1cSKevin Wolf } 1114ee13ed1cSKevin Wolf qdict_put(bs_opts, "werror", qstring_from_str(werror)); 1115ee13ed1cSKevin Wolf } 1116ee13ed1cSKevin Wolf 1117ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 1118ee13ed1cSKevin Wolf if (rerror != NULL) { 1119ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1120ee13ed1cSKevin Wolf type != IF_NONE) { 1121ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 1122ee13ed1cSKevin Wolf goto fail; 1123ee13ed1cSKevin Wolf } 1124ee13ed1cSKevin Wolf qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); 1125ee13ed1cSKevin Wolf } 1126ee13ed1cSKevin Wolf 11272d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 112818e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 11293cb0e25cSMarkus Armbruster bs_opts = NULL; 113018e46a03SMarkus Armbruster if (!blk) { 113184d18f06SMarkus Armbruster if (local_err) { 1132565f65d2SMarkus Armbruster error_report_err(local_err); 1133b681072dSKevin Wolf } 11342d246f01SKevin Wolf goto fail; 1135b681072dSKevin Wolf } else { 113684d18f06SMarkus Armbruster assert(!local_err); 11372d246f01SKevin Wolf } 11382d246f01SKevin Wolf 113926f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 114026f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1141f298d071SKevin Wolf dinfo->opts = all_opts; 11422d246f01SKevin Wolf 1143b41a7338SKevin Wolf dinfo->cyls = cyls; 1144b41a7338SKevin Wolf dinfo->heads = heads; 1145b41a7338SKevin Wolf dinfo->secs = secs; 1146b41a7338SKevin Wolf dinfo->trans = translation; 1147b41a7338SKevin Wolf 1148ee13ed1cSKevin Wolf dinfo->type = type; 114987a899c5SKevin Wolf dinfo->bus = bus_id; 115087a899c5SKevin Wolf dinfo->unit = unit_id; 1151394c7d4dSKevin Wolf dinfo->devaddr = devaddr; 1152bcf83158SKevin Wolf dinfo->serial = g_strdup(serial); 1153bcf83158SKevin Wolf 115426f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 115526f8b3a8SMarkus Armbruster 1156e34ef046SKevin Wolf switch(type) { 1157e34ef046SKevin Wolf case IF_IDE: 1158e34ef046SKevin Wolf case IF_SCSI: 1159e34ef046SKevin Wolf case IF_XEN: 1160e34ef046SKevin Wolf case IF_NONE: 1161e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1162e34ef046SKevin Wolf break; 1163e34ef046SKevin Wolf default: 1164e34ef046SKevin Wolf break; 1165e34ef046SKevin Wolf } 1166e34ef046SKevin Wolf 11672d246f01SKevin Wolf fail: 116833cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 11693cb0e25cSMarkus Armbruster QDECREF(bs_opts); 11702d246f01SKevin Wolf return dinfo; 117157975222SKevin Wolf } 117257975222SKevin Wolf 11733e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1174666daa68SMarkus Armbruster { 1175666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1176a0e8544cSFam Zheng BlockBackend *blk; 11772d3735d3SStefan Hajnoczi int ret; 11782d3735d3SStefan Hajnoczi 1179e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1180da31d594SMax Reitz ret = blk_commit_all(); 1181e8877497SStefan Hajnoczi } else { 118284aa0140SStefan Hajnoczi BlockDriverState *bs; 118384aa0140SStefan Hajnoczi AioContext *aio_context; 118484aa0140SStefan Hajnoczi 1185a0e8544cSFam Zheng blk = blk_by_name(device); 1186a0e8544cSFam Zheng if (!blk) { 118758513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1188ac59eb95SMarkus Armbruster return; 11896ab4b5abSMarkus Armbruster } 11905433c24fSMax Reitz if (!blk_is_available(blk)) { 11915433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 11925433c24fSMax Reitz return; 11935433c24fSMax Reitz } 119484aa0140SStefan Hajnoczi 119584aa0140SStefan Hajnoczi bs = blk_bs(blk); 119684aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 119784aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 119884aa0140SStefan Hajnoczi 119984aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 120084aa0140SStefan Hajnoczi 120184aa0140SStefan Hajnoczi aio_context_release(aio_context); 12022d3735d3SStefan Hajnoczi } 120358513bdeSJeff Cody if (ret < 0) { 120458513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 120558513bdeSJeff Cody strerror(-ret)); 1206666daa68SMarkus Armbruster } 1207666daa68SMarkus Armbruster } 1208666daa68SMarkus Armbruster 120910f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 12106cc2a415SPaolo Bonzini { 1211c8a83e85SKevin Wolf TransactionActionList list; 12126cc2a415SPaolo Bonzini 121310f75907SEric Blake list.value = action; 12146cc2a415SPaolo Bonzini list.next = NULL; 121594d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 12166cc2a415SPaolo Bonzini } 12176cc2a415SPaolo Bonzini 12180901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 12190901f67eSBenoît Canet bool has_node_name, const char *node_name, 12200901f67eSBenoît Canet const char *snapshot_file, 12210901f67eSBenoît Canet bool has_snapshot_node_name, 12220901f67eSBenoît Canet const char *snapshot_node_name, 12236106e249SLuiz Capitulino bool has_format, const char *format, 12240901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1225f8882568SJes Sorensen { 1226a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 12270901f67eSBenoît Canet .has_device = has_device, 12286cc2a415SPaolo Bonzini .device = (char *) device, 12290901f67eSBenoît Canet .has_node_name = has_node_name, 12300901f67eSBenoît Canet .node_name = (char *) node_name, 12316cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 12320901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 12330901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 12346cc2a415SPaolo Bonzini .has_format = has_format, 12356cc2a415SPaolo Bonzini .format = (char *) format, 12366cc2a415SPaolo Bonzini .has_mode = has_mode, 12376cc2a415SPaolo Bonzini .mode = mode, 12386cc2a415SPaolo Bonzini }; 123910f75907SEric Blake TransactionAction action = { 124010f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 124132bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 124210f75907SEric Blake }; 124310f75907SEric Blake blockdev_do_action(&action, errp); 1244f8882568SJes Sorensen } 1245f8882568SJes Sorensen 124643de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 124743de7e2dSAlberto Garcia Error **errp) 124843de7e2dSAlberto Garcia { 124943de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 125043de7e2dSAlberto Garcia .node = (char *) node, 125143de7e2dSAlberto Garcia .overlay = (char *) overlay 125243de7e2dSAlberto Garcia }; 125310f75907SEric Blake TransactionAction action = { 125410f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 125532bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 125610f75907SEric Blake }; 125710f75907SEric Blake blockdev_do_action(&action, errp); 125843de7e2dSAlberto Garcia } 125943de7e2dSAlberto Garcia 1260f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1261f323bc9eSWenchao Xia const char *name, 1262f323bc9eSWenchao Xia Error **errp) 1263f323bc9eSWenchao Xia { 1264f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1265f323bc9eSWenchao Xia .device = (char *) device, 1266f323bc9eSWenchao Xia .name = (char *) name 1267f323bc9eSWenchao Xia }; 126810f75907SEric Blake TransactionAction action = { 126910f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 127032bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 127110f75907SEric Blake }; 127210f75907SEric Blake blockdev_do_action(&action, errp); 1273f323bc9eSWenchao Xia } 1274f323bc9eSWenchao Xia 127544e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 127644e3e053SWenchao Xia bool has_id, 127744e3e053SWenchao Xia const char *id, 127844e3e053SWenchao Xia bool has_name, 127944e3e053SWenchao Xia const char *name, 128044e3e053SWenchao Xia Error **errp) 128144e3e053SWenchao Xia { 1282a0e8544cSFam Zheng BlockDriverState *bs; 1283a0e8544cSFam Zheng BlockBackend *blk; 12844ef3982aSStefan Hajnoczi AioContext *aio_context; 128544e3e053SWenchao Xia QEMUSnapshotInfo sn; 128644e3e053SWenchao Xia Error *local_err = NULL; 128744e3e053SWenchao Xia SnapshotInfo *info = NULL; 128844e3e053SWenchao Xia int ret; 128944e3e053SWenchao Xia 1290a0e8544cSFam Zheng blk = blk_by_name(device); 1291a0e8544cSFam Zheng if (!blk) { 129275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 129375158ebbSMarkus Armbruster "Device '%s' not found", device); 129444e3e053SWenchao Xia return NULL; 129544e3e053SWenchao Xia } 12965433c24fSMax Reitz 12975433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 12985433c24fSMax Reitz aio_context_acquire(aio_context); 129944e3e053SWenchao Xia 130044e3e053SWenchao Xia if (!has_id) { 130144e3e053SWenchao Xia id = NULL; 130244e3e053SWenchao Xia } 130344e3e053SWenchao Xia 130444e3e053SWenchao Xia if (!has_name) { 130544e3e053SWenchao Xia name = NULL; 130644e3e053SWenchao Xia } 130744e3e053SWenchao Xia 130844e3e053SWenchao Xia if (!id && !name) { 130944e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 13105433c24fSMax Reitz goto out_aio_context; 131144e3e053SWenchao Xia } 131244e3e053SWenchao Xia 13135433c24fSMax Reitz if (!blk_is_available(blk)) { 13145433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 13155433c24fSMax Reitz goto out_aio_context; 13165433c24fSMax Reitz } 13175433c24fSMax Reitz bs = blk_bs(blk); 13184ef3982aSStefan Hajnoczi 13190b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 13200b928854SStefan Hajnoczi goto out_aio_context; 13210b928854SStefan Hajnoczi } 13220b928854SStefan Hajnoczi 132344e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 132484d18f06SMarkus Armbruster if (local_err) { 132544e3e053SWenchao Xia error_propagate(errp, local_err); 13264ef3982aSStefan Hajnoczi goto out_aio_context; 132744e3e053SWenchao Xia } 132844e3e053SWenchao Xia if (!ret) { 132944e3e053SWenchao Xia error_setg(errp, 133044e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 133144e3e053SWenchao Xia "device '%s'", 133244e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 13334ef3982aSStefan Hajnoczi goto out_aio_context; 133444e3e053SWenchao Xia } 133544e3e053SWenchao Xia 133644e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 133784d18f06SMarkus Armbruster if (local_err) { 133844e3e053SWenchao Xia error_propagate(errp, local_err); 13394ef3982aSStefan Hajnoczi goto out_aio_context; 134044e3e053SWenchao Xia } 134144e3e053SWenchao Xia 13424ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13434ef3982aSStefan Hajnoczi 13445839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 134544e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 134644e3e053SWenchao Xia info->name = g_strdup(sn.name); 134744e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 134844e3e053SWenchao Xia info->date_sec = sn.date_sec; 134944e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 135044e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 135144e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 135244e3e053SWenchao Xia 135344e3e053SWenchao Xia return info; 13544ef3982aSStefan Hajnoczi 13554ef3982aSStefan Hajnoczi out_aio_context: 13564ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13574ef3982aSStefan Hajnoczi return NULL; 135844e3e053SWenchao Xia } 13598802d1fdSJeff Cody 1360341ebc2fSJohn Snow /** 1361341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1362341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1363341ebc2fSJohn Snow * the node reference and bitmap names. 1364341ebc2fSJohn Snow * 1365341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1366341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1367341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1368341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1369341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1370341ebc2fSJohn Snow * 1371341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1372341ebc2fSJohn Snow */ 1373341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1374341ebc2fSJohn Snow const char *name, 1375341ebc2fSJohn Snow BlockDriverState **pbs, 1376341ebc2fSJohn Snow AioContext **paio, 1377341ebc2fSJohn Snow Error **errp) 1378341ebc2fSJohn Snow { 1379341ebc2fSJohn Snow BlockDriverState *bs; 1380341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1381341ebc2fSJohn Snow AioContext *aio_context; 1382341ebc2fSJohn Snow 1383341ebc2fSJohn Snow if (!node) { 1384341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1385341ebc2fSJohn Snow return NULL; 1386341ebc2fSJohn Snow } 1387341ebc2fSJohn Snow if (!name) { 1388341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1389341ebc2fSJohn Snow return NULL; 1390341ebc2fSJohn Snow } 1391341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1392341ebc2fSJohn Snow if (!bs) { 1393341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1394341ebc2fSJohn Snow return NULL; 1395341ebc2fSJohn Snow } 1396341ebc2fSJohn Snow 1397341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 1398341ebc2fSJohn Snow aio_context_acquire(aio_context); 1399341ebc2fSJohn Snow 1400341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1401341ebc2fSJohn Snow if (!bitmap) { 1402341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 1403341ebc2fSJohn Snow goto fail; 1404341ebc2fSJohn Snow } 1405341ebc2fSJohn Snow 1406341ebc2fSJohn Snow if (pbs) { 1407341ebc2fSJohn Snow *pbs = bs; 1408341ebc2fSJohn Snow } 1409341ebc2fSJohn Snow if (paio) { 1410341ebc2fSJohn Snow *paio = aio_context; 1411341ebc2fSJohn Snow } else { 1412341ebc2fSJohn Snow aio_context_release(aio_context); 1413341ebc2fSJohn Snow } 1414341ebc2fSJohn Snow 1415341ebc2fSJohn Snow return bitmap; 1416341ebc2fSJohn Snow 1417341ebc2fSJohn Snow fail: 1418341ebc2fSJohn Snow aio_context_release(aio_context); 1419341ebc2fSJohn Snow return NULL; 1420341ebc2fSJohn Snow } 1421341ebc2fSJohn Snow 1422b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1423ba0c86a3SWenchao Xia 142450f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1425ba0c86a3SWenchao Xia 142650f43f0fSJohn Snow /** 142750f43f0fSJohn Snow * BlkActionOps: 142850f43f0fSJohn Snow * Table of operations that define an Action. 142950f43f0fSJohn Snow * 143050f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 143150f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 143250f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 143350f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 143450f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 143550f43f0fSJohn Snow * commit() or abort(). Can be NULL. 143650f43f0fSJohn Snow * 143750f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 143850f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1439ba0c86a3SWenchao Xia */ 144050f43f0fSJohn Snow typedef struct BlkActionOps { 144150f43f0fSJohn Snow size_t instance_size; 144250f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 144350f43f0fSJohn Snow void (*commit)(BlkActionState *common); 144450f43f0fSJohn Snow void (*abort)(BlkActionState *common); 144550f43f0fSJohn Snow void (*clean)(BlkActionState *common); 144650f43f0fSJohn Snow } BlkActionOps; 144750f43f0fSJohn Snow 144850f43f0fSJohn Snow /** 144950f43f0fSJohn Snow * BlkActionState: 145050f43f0fSJohn Snow * Describes one Action's state within a Transaction. 145150f43f0fSJohn Snow * 145250f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 145350f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 145494d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 145550f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 145650f43f0fSJohn Snow * 145750f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 145850f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 145950f43f0fSJohn Snow * base class. 146050f43f0fSJohn Snow */ 146150f43f0fSJohn Snow struct BlkActionState { 1462c8a83e85SKevin Wolf TransactionAction *action; 146350f43f0fSJohn Snow const BlkActionOps *ops; 146494d16a64SJohn Snow BlockJobTxn *block_job_txn; 146594d16a64SJohn Snow TransactionProperties *txn_props; 146650f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1467ba0c86a3SWenchao Xia }; 1468ba0c86a3SWenchao Xia 1469bbe86010SWenchao Xia /* internal snapshot private data */ 1470bbe86010SWenchao Xia typedef struct InternalSnapshotState { 147150f43f0fSJohn Snow BlkActionState common; 1472bbe86010SWenchao Xia BlockDriverState *bs; 14735d6e96efSStefan Hajnoczi AioContext *aio_context; 1474bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1475507306ccSFam Zheng bool created; 1476bbe86010SWenchao Xia } InternalSnapshotState; 1477bbe86010SWenchao Xia 147894d16a64SJohn Snow 147994d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 148094d16a64SJohn Snow { 148194d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 148294d16a64SJohn Snow error_setg(errp, 148394d16a64SJohn Snow "Action '%s' does not support Transaction property " 148494d16a64SJohn Snow "completion-mode = %s", 148594d16a64SJohn Snow TransactionActionKind_lookup[s->action->type], 148694d16a64SJohn Snow ActionCompletionMode_lookup[s->txn_props->completion_mode]); 148794d16a64SJohn Snow return -1; 148894d16a64SJohn Snow } 148994d16a64SJohn Snow return 0; 149094d16a64SJohn Snow } 149194d16a64SJohn Snow 149250f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1493bbe86010SWenchao Xia Error **errp) 1494bbe86010SWenchao Xia { 1495f70edf99SMarkus Armbruster Error *local_err = NULL; 1496bbe86010SWenchao Xia const char *device; 1497bbe86010SWenchao Xia const char *name; 1498a0e8544cSFam Zheng BlockBackend *blk; 1499bbe86010SWenchao Xia BlockDriverState *bs; 1500bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1501bbe86010SWenchao Xia bool ret; 1502bbe86010SWenchao Xia qemu_timeval tv; 1503bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1504bbe86010SWenchao Xia InternalSnapshotState *state; 1505bbe86010SWenchao Xia int ret1; 1506bbe86010SWenchao Xia 15076a8f9661SEric Blake g_assert(common->action->type == 1508bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 150932bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1510bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1511bbe86010SWenchao Xia 1512bbe86010SWenchao Xia /* 1. parse input */ 1513bbe86010SWenchao Xia device = internal->device; 1514bbe86010SWenchao Xia name = internal->name; 1515bbe86010SWenchao Xia 1516bbe86010SWenchao Xia /* 2. check for validation */ 151794d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 151894d16a64SJohn Snow return; 151994d16a64SJohn Snow } 152094d16a64SJohn Snow 1521a0e8544cSFam Zheng blk = blk_by_name(device); 1522a0e8544cSFam Zheng if (!blk) { 152375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 152475158ebbSMarkus Armbruster "Device '%s' not found", device); 1525bbe86010SWenchao Xia return; 1526bbe86010SWenchao Xia } 1527bbe86010SWenchao Xia 15285d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 15295433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 15305d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 15315d6e96efSStefan Hajnoczi 15325433c24fSMax Reitz if (!blk_is_available(blk)) { 1533c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1534bbe86010SWenchao Xia return; 1535bbe86010SWenchao Xia } 15365433c24fSMax Reitz bs = blk_bs(blk); 1537bbe86010SWenchao Xia 1538507306ccSFam Zheng state->bs = bs; 1539507306ccSFam Zheng bdrv_drained_begin(bs); 1540507306ccSFam Zheng 15413dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 15423dc7ca3cSStefan Hajnoczi return; 15433dc7ca3cSStefan Hajnoczi } 15443dc7ca3cSStefan Hajnoczi 1545bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 154681e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1547bbe86010SWenchao Xia return; 1548bbe86010SWenchao Xia } 1549bbe86010SWenchao Xia 1550bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 155181e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 155281e5f78aSAlberto Garcia "does not support internal snapshots", 155381e5f78aSAlberto Garcia bs->drv->format_name, device); 1554bbe86010SWenchao Xia return; 1555bbe86010SWenchao Xia } 1556bbe86010SWenchao Xia 1557bbe86010SWenchao Xia if (!strlen(name)) { 1558bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1559bbe86010SWenchao Xia return; 1560bbe86010SWenchao Xia } 1561bbe86010SWenchao Xia 1562bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1563f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1564f70edf99SMarkus Armbruster &local_err); 1565f70edf99SMarkus Armbruster if (local_err) { 1566f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1567bbe86010SWenchao Xia return; 1568bbe86010SWenchao Xia } else if (ret) { 1569bbe86010SWenchao Xia error_setg(errp, 1570bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1571bbe86010SWenchao Xia name, device); 1572bbe86010SWenchao Xia return; 1573bbe86010SWenchao Xia } 1574bbe86010SWenchao Xia 1575bbe86010SWenchao Xia /* 3. take the snapshot */ 1576bbe86010SWenchao Xia sn = &state->sn; 1577bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1578bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1579bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1580bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1581bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1582bbe86010SWenchao Xia 1583bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1584bbe86010SWenchao Xia if (ret1 < 0) { 1585bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1586bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1587bbe86010SWenchao Xia name, device); 1588bbe86010SWenchao Xia return; 1589bbe86010SWenchao Xia } 1590bbe86010SWenchao Xia 1591bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1592507306ccSFam Zheng state->created = true; 1593bbe86010SWenchao Xia } 1594bbe86010SWenchao Xia 159550f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1596bbe86010SWenchao Xia { 1597bbe86010SWenchao Xia InternalSnapshotState *state = 1598bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1599bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1600bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1601bbe86010SWenchao Xia Error *local_error = NULL; 1602bbe86010SWenchao Xia 1603507306ccSFam Zheng if (!state->created) { 1604bbe86010SWenchao Xia return; 1605bbe86010SWenchao Xia } 1606bbe86010SWenchao Xia 1607bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1608c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1609c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1610c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1611c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1612c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1613bbe86010SWenchao Xia } 1614bbe86010SWenchao Xia } 1615bbe86010SWenchao Xia 161650f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 16175d6e96efSStefan Hajnoczi { 16185d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 16195d6e96efSStefan Hajnoczi common, common); 16205d6e96efSStefan Hajnoczi 16215d6e96efSStefan Hajnoczi if (state->aio_context) { 1622507306ccSFam Zheng if (state->bs) { 1623507306ccSFam Zheng bdrv_drained_end(state->bs); 1624507306ccSFam Zheng } 16255d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16265d6e96efSStefan Hajnoczi } 16275d6e96efSStefan Hajnoczi } 16285d6e96efSStefan Hajnoczi 1629ba0c86a3SWenchao Xia /* external snapshot private data */ 1630ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 163150f43f0fSJohn Snow BlkActionState common; 16328802d1fdSJeff Cody BlockDriverState *old_bs; 16338802d1fdSJeff Cody BlockDriverState *new_bs; 16345d6e96efSStefan Hajnoczi AioContext *aio_context; 1635ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16368802d1fdSJeff Cody 163750f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16389b9877eeSWenchao Xia Error **errp) 16399b9877eeSWenchao Xia { 16405b363937SMax Reitz int flags = 0; 164143de7e2dSAlberto Garcia QDict *options = NULL; 16429b9877eeSWenchao Xia Error *local_err = NULL; 164343de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1644e2a31e87SWenchao Xia const char *device; 16450901f67eSBenoît Canet const char *node_name; 164643de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 164743de7e2dSAlberto Garcia const char *snapshot_ref; 164843de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1649e2a31e87SWenchao Xia const char *new_image_file; 1650ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1651ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1652c8a83e85SKevin Wolf TransactionAction *action = common->action; 16539b9877eeSWenchao Xia 165443de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 165543de7e2dSAlberto Garcia * purpose but a different set of parameters */ 165643de7e2dSAlberto Garcia switch (action->type) { 165743de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 165843de7e2dSAlberto Garcia { 165932bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 166043de7e2dSAlberto Garcia device = s->node; 166143de7e2dSAlberto Garcia node_name = s->node; 166243de7e2dSAlberto Garcia new_image_file = NULL; 166343de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1664e2a31e87SWenchao Xia } 166543de7e2dSAlberto Garcia break; 166643de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 166743de7e2dSAlberto Garcia { 166832bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 166943de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 167043de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 167143de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 167243de7e2dSAlberto Garcia snapshot_ref = NULL; 167343de7e2dSAlberto Garcia } 167443de7e2dSAlberto Garcia break; 167543de7e2dSAlberto Garcia default: 167643de7e2dSAlberto Garcia g_assert_not_reached(); 1677e2a31e87SWenchao Xia } 1678e2a31e87SWenchao Xia 1679e2a31e87SWenchao Xia /* start processing */ 168094d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 168194d16a64SJohn Snow return; 168294d16a64SJohn Snow } 168394d16a64SJohn Snow 168443de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 168543de7e2dSAlberto Garcia if (!state->old_bs) { 16869b9877eeSWenchao Xia return; 16879b9877eeSWenchao Xia } 16889b9877eeSWenchao Xia 16895d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 16905d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 16915d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1692da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 16935d6e96efSStefan Hajnoczi 1694ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1695c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 16969b9877eeSWenchao Xia return; 16979b9877eeSWenchao Xia } 16989b9877eeSWenchao Xia 16993718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 17003718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 17019b9877eeSWenchao Xia return; 17029b9877eeSWenchao Xia } 17039b9877eeSWenchao Xia 1704ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1705ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1706c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 17079b9877eeSWenchao Xia return; 17089b9877eeSWenchao Xia } 17099b9877eeSWenchao Xia } 17109b9877eeSWenchao Xia 1711212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1712c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1713f6186f49SBenoît Canet return; 1714f6186f49SBenoît Canet } 1715f6186f49SBenoît Canet 171643de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 171732bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 171843de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 171943de7e2dSAlberto Garcia enum NewImageMode mode; 172043de7e2dSAlberto Garcia const char *snapshot_node_name = 172143de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 172243de7e2dSAlberto Garcia 172343de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 172443de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 172543de7e2dSAlberto Garcia return; 172643de7e2dSAlberto Garcia } 172743de7e2dSAlberto Garcia 172843de7e2dSAlberto Garcia if (snapshot_node_name && 172943de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 173043de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 173143de7e2dSAlberto Garcia return; 173243de7e2dSAlberto Garcia } 173343de7e2dSAlberto Garcia 1734ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17354c844983SKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); 17369b9877eeSWenchao Xia 17379b9877eeSWenchao Xia /* create new image w/backing file */ 173843de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17399b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1740f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1741f86b8b58SKevin Wolf if (size < 0) { 1742f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 1743f86b8b58SKevin Wolf return; 1744f86b8b58SKevin Wolf } 17459b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1746ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1747ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 1748f86b8b58SKevin Wolf NULL, size, flags, &local_err, false); 174984d18f06SMarkus Armbruster if (local_err) { 17509b9877eeSWenchao Xia error_propagate(errp, local_err); 17519b9877eeSWenchao Xia return; 17529b9877eeSWenchao Xia } 17539b9877eeSWenchao Xia } 17549b9877eeSWenchao Xia 17550901f67eSBenoît Canet options = qdict_new(); 175643de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 17570901f67eSBenoît Canet qdict_put(options, "node-name", 17580901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 17590901f67eSBenoît Canet } 1760e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 17610901f67eSBenoît Canet 176243de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 176343de7e2dSAlberto Garcia } 176443de7e2dSAlberto Garcia 17655b363937SMax Reitz state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, 17665b363937SMax Reitz errp); 1767f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17685b363937SMax Reitz if (!state->new_bs) { 176943de7e2dSAlberto Garcia return; 177043de7e2dSAlberto Garcia } 177143de7e2dSAlberto Garcia 17721f0c461bSKevin Wolf if (bdrv_has_blk(state->new_bs)) { 177343de7e2dSAlberto Garcia error_setg(errp, "The snapshot is already in use by %s", 17741f0c461bSKevin Wolf bdrv_get_parent_name(state->new_bs)); 177543de7e2dSAlberto Garcia return; 177643de7e2dSAlberto Garcia } 177743de7e2dSAlberto Garcia 177843de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 177943de7e2dSAlberto Garcia errp)) { 178043de7e2dSAlberto Garcia return; 178143de7e2dSAlberto Garcia } 178243de7e2dSAlberto Garcia 178343de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 178443de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 178508b24cfeSAlberto Garcia return; 178608b24cfeSAlberto Garcia } 178708b24cfeSAlberto Garcia 178808b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 178908b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 17909b9877eeSWenchao Xia } 17919b9877eeSWenchao Xia } 17929b9877eeSWenchao Xia 179350f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 17943b0047e8SWenchao Xia { 1795ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1796ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1797ba0c86a3SWenchao Xia 17985d6e96efSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, state->aio_context); 17995d6e96efSStefan Hajnoczi 1800ba5d6ab6SStefan Hajnoczi /* This removes our old bs and adds the new bs */ 1801ba5d6ab6SStefan Hajnoczi bdrv_append(state->new_bs, state->old_bs); 18023b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 18033b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 18043b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 18054c844983SKevin Wolf if (!state->old_bs->copy_on_read) { 1806dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 18073b0047e8SWenchao Xia NULL); 18083b0047e8SWenchao Xia } 18094c844983SKevin Wolf } 18103b0047e8SWenchao Xia 181150f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 181296b86bf7SWenchao Xia { 1813ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1814ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1815ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 18164f6fd349SFam Zheng bdrv_unref(state->new_bs); 181796b86bf7SWenchao Xia } 1818da763e83SFam Zheng } 1819da763e83SFam Zheng 182050f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1821da763e83SFam Zheng { 1822da763e83SFam Zheng ExternalSnapshotState *state = 1823da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 18245d6e96efSStefan Hajnoczi if (state->aio_context) { 1825da763e83SFam Zheng bdrv_drained_end(state->old_bs); 18265d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18275d6e96efSStefan Hajnoczi } 182896b86bf7SWenchao Xia } 182996b86bf7SWenchao Xia 18303037f364SStefan Hajnoczi typedef struct DriveBackupState { 183150f43f0fSJohn Snow BlkActionState common; 18323037f364SStefan Hajnoczi BlockDriverState *bs; 18335d6e96efSStefan Hajnoczi AioContext *aio_context; 18343037f364SStefan Hajnoczi BlockJob *job; 18353037f364SStefan Hajnoczi } DriveBackupState; 18363037f364SStefan Hajnoczi 183778f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 183878f51fdeSJohn Snow bool has_format, const char *format, 183978f51fdeSJohn Snow enum MirrorSyncMode sync, 184078f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 184178f51fdeSJohn Snow bool has_speed, int64_t speed, 184278f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 184378f51fdeSJohn Snow bool has_on_source_error, 184478f51fdeSJohn Snow BlockdevOnError on_source_error, 184578f51fdeSJohn Snow bool has_on_target_error, 184678f51fdeSJohn Snow BlockdevOnError on_target_error, 184778f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 184878f51fdeSJohn Snow 184950f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18503037f364SStefan Hajnoczi { 18513037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1852a0e8544cSFam Zheng BlockBackend *blk; 18533037f364SStefan Hajnoczi DriveBackup *backup; 18543037f364SStefan Hajnoczi Error *local_err = NULL; 18553037f364SStefan Hajnoczi 18566a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 185732bafa8fSEric Blake backup = common->action->u.drive_backup.data; 18583037f364SStefan Hajnoczi 1859a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1860a0e8544cSFam Zheng if (!blk) { 186175158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 186275158ebbSMarkus Armbruster "Device '%s' not found", backup->device); 18635d6e96efSStefan Hajnoczi return; 18645d6e96efSStefan Hajnoczi } 18655d6e96efSStefan Hajnoczi 18661fdd4b7bSFam Zheng if (!blk_is_available(blk)) { 18671fdd4b7bSFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 18681fdd4b7bSFam Zheng return; 18691fdd4b7bSFam Zheng } 18701fdd4b7bSFam Zheng 18715d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 18725433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 18735d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 18741fdd4b7bSFam Zheng bdrv_drained_begin(blk_bs(blk)); 18751fdd4b7bSFam Zheng state->bs = blk_bs(blk); 18765d6e96efSStefan Hajnoczi 187778f51fdeSJohn Snow do_drive_backup(backup->device, backup->target, 18783037f364SStefan Hajnoczi backup->has_format, backup->format, 1879b53169eaSStefan Hajnoczi backup->sync, 18803037f364SStefan Hajnoczi backup->has_mode, backup->mode, 18813037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1882d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 18833037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 18843037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 188594d16a64SJohn Snow common->block_job_txn, &local_err); 188684d18f06SMarkus Armbruster if (local_err) { 18873037f364SStefan Hajnoczi error_propagate(errp, local_err); 18883037f364SStefan Hajnoczi return; 18893037f364SStefan Hajnoczi } 18903037f364SStefan Hajnoczi 18913037f364SStefan Hajnoczi state->job = state->bs->job; 18923037f364SStefan Hajnoczi } 18933037f364SStefan Hajnoczi 189450f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 18953037f364SStefan Hajnoczi { 18963037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18973037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 18983037f364SStefan Hajnoczi 18993037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 19003037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 19013037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 19023037f364SStefan Hajnoczi } 19033037f364SStefan Hajnoczi } 19043037f364SStefan Hajnoczi 190550f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 19065d6e96efSStefan Hajnoczi { 19075d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19085d6e96efSStefan Hajnoczi 19095d6e96efSStefan Hajnoczi if (state->aio_context) { 19101fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 19115d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 19125d6e96efSStefan Hajnoczi } 19135d6e96efSStefan Hajnoczi } 19145d6e96efSStefan Hajnoczi 1915bd8baecdSFam Zheng typedef struct BlockdevBackupState { 191650f43f0fSJohn Snow BlkActionState common; 1917bd8baecdSFam Zheng BlockDriverState *bs; 1918bd8baecdSFam Zheng BlockJob *job; 1919bd8baecdSFam Zheng AioContext *aio_context; 1920bd8baecdSFam Zheng } BlockdevBackupState; 1921bd8baecdSFam Zheng 192278f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target, 192378f51fdeSJohn Snow enum MirrorSyncMode sync, 192478f51fdeSJohn Snow bool has_speed, int64_t speed, 192578f51fdeSJohn Snow bool has_on_source_error, 192678f51fdeSJohn Snow BlockdevOnError on_source_error, 192778f51fdeSJohn Snow bool has_on_target_error, 192878f51fdeSJohn Snow BlockdevOnError on_target_error, 192978f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 193078f51fdeSJohn Snow 193150f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1932bd8baecdSFam Zheng { 1933bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1934bd8baecdSFam Zheng BlockdevBackup *backup; 19355433c24fSMax Reitz BlockBackend *blk, *target; 1936bd8baecdSFam Zheng Error *local_err = NULL; 1937bd8baecdSFam Zheng 19386a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 193932bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1940bd8baecdSFam Zheng 1941a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1942a0e8544cSFam Zheng if (!blk) { 19435b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->device); 1944bd8baecdSFam Zheng return; 1945bd8baecdSFam Zheng } 1946bd8baecdSFam Zheng 1947ff52bf36SFam Zheng if (!blk_is_available(blk)) { 1948ff52bf36SFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1949ff52bf36SFam Zheng return; 1950ff52bf36SFam Zheng } 1951ff52bf36SFam Zheng 19525433c24fSMax Reitz target = blk_by_name(backup->target); 19535433c24fSMax Reitz if (!target) { 19545b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->target); 1955bd8baecdSFam Zheng return; 1956bd8baecdSFam Zheng } 1957bd8baecdSFam Zheng 1958bd8baecdSFam Zheng /* AioContext is released in .clean() */ 19595433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 19605433c24fSMax Reitz if (state->aio_context != blk_get_aio_context(target)) { 1961bd8baecdSFam Zheng state->aio_context = NULL; 1962bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1963bd8baecdSFam Zheng return; 1964bd8baecdSFam Zheng } 1965bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1966ff52bf36SFam Zheng state->bs = blk_bs(blk); 1967ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1968bd8baecdSFam Zheng 196978f51fdeSJohn Snow do_blockdev_backup(backup->device, backup->target, 1970bd8baecdSFam Zheng backup->sync, 1971bd8baecdSFam Zheng backup->has_speed, backup->speed, 1972bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1973bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 197494d16a64SJohn Snow common->block_job_txn, &local_err); 1975bd8baecdSFam Zheng if (local_err) { 1976bd8baecdSFam Zheng error_propagate(errp, local_err); 1977bd8baecdSFam Zheng return; 1978bd8baecdSFam Zheng } 1979bd8baecdSFam Zheng 1980bd8baecdSFam Zheng state->job = state->bs->job; 1981bd8baecdSFam Zheng } 1982bd8baecdSFam Zheng 198350f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1984bd8baecdSFam Zheng { 1985bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1986bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1987bd8baecdSFam Zheng 1988bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 1989bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 1990bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 1991bd8baecdSFam Zheng } 1992bd8baecdSFam Zheng } 1993bd8baecdSFam Zheng 199450f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 1995bd8baecdSFam Zheng { 1996bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1997bd8baecdSFam Zheng 1998bd8baecdSFam Zheng if (state->aio_context) { 1999ff52bf36SFam Zheng bdrv_drained_end(state->bs); 2000bd8baecdSFam Zheng aio_context_release(state->aio_context); 2001bd8baecdSFam Zheng } 2002bd8baecdSFam Zheng } 2003bd8baecdSFam Zheng 2004df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 200550f43f0fSJohn Snow BlkActionState common; 2006df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 2007df9a681dSFam Zheng BlockDriverState *bs; 2008df9a681dSFam Zheng AioContext *aio_context; 2009df9a681dSFam Zheng HBitmap *backup; 2010df9a681dSFam Zheng bool prepared; 2011df9a681dSFam Zheng } BlockDirtyBitmapState; 2012df9a681dSFam Zheng 201350f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 2014df9a681dSFam Zheng Error **errp) 2015df9a681dSFam Zheng { 2016df9a681dSFam Zheng Error *local_err = NULL; 2017df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2018df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2019df9a681dSFam Zheng common, common); 2020df9a681dSFam Zheng 202194d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 202294d16a64SJohn Snow return; 202394d16a64SJohn Snow } 202494d16a64SJohn Snow 202532bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2026df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2027df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2028df9a681dSFam Zheng action->has_granularity, action->granularity, 2029df9a681dSFam Zheng &local_err); 2030df9a681dSFam Zheng 2031df9a681dSFam Zheng if (!local_err) { 2032df9a681dSFam Zheng state->prepared = true; 2033df9a681dSFam Zheng } else { 2034df9a681dSFam Zheng error_propagate(errp, local_err); 2035df9a681dSFam Zheng } 2036df9a681dSFam Zheng } 2037df9a681dSFam Zheng 203850f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2039df9a681dSFam Zheng { 2040df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2041df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2042df9a681dSFam Zheng common, common); 2043df9a681dSFam Zheng 204432bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2045df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2046df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2047df9a681dSFam Zheng */ 2048df9a681dSFam Zheng if (state->prepared) { 2049df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2050df9a681dSFam Zheng } 2051df9a681dSFam Zheng } 2052df9a681dSFam Zheng 205350f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2054df9a681dSFam Zheng Error **errp) 2055df9a681dSFam Zheng { 2056df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2057df9a681dSFam Zheng common, common); 2058df9a681dSFam Zheng BlockDirtyBitmap *action; 2059df9a681dSFam Zheng 206094d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 206194d16a64SJohn Snow return; 206294d16a64SJohn Snow } 206394d16a64SJohn Snow 206432bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2065df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2066df9a681dSFam Zheng action->name, 2067df9a681dSFam Zheng &state->bs, 2068df9a681dSFam Zheng &state->aio_context, 2069df9a681dSFam Zheng errp); 2070df9a681dSFam Zheng if (!state->bitmap) { 2071df9a681dSFam Zheng return; 2072df9a681dSFam Zheng } 2073df9a681dSFam Zheng 2074df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2075df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2076df9a681dSFam Zheng return; 2077df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2078df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2079df9a681dSFam Zheng return; 2080df9a681dSFam Zheng } 2081df9a681dSFam Zheng 2082df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2083df9a681dSFam Zheng /* AioContext is released in .clean() */ 2084df9a681dSFam Zheng } 2085df9a681dSFam Zheng 208650f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2087df9a681dSFam Zheng { 2088df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2089df9a681dSFam Zheng common, common); 2090df9a681dSFam Zheng 2091df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2092df9a681dSFam Zheng } 2093df9a681dSFam Zheng 209450f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2095df9a681dSFam Zheng { 2096df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2097df9a681dSFam Zheng common, common); 2098df9a681dSFam Zheng 2099df9a681dSFam Zheng hbitmap_free(state->backup); 2100df9a681dSFam Zheng } 2101df9a681dSFam Zheng 210250f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2103df9a681dSFam Zheng { 2104df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2105df9a681dSFam Zheng common, common); 2106df9a681dSFam Zheng 2107df9a681dSFam Zheng if (state->aio_context) { 2108df9a681dSFam Zheng aio_context_release(state->aio_context); 2109df9a681dSFam Zheng } 2110df9a681dSFam Zheng } 2111df9a681dSFam Zheng 211250f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 211378b18b78SStefan Hajnoczi { 211478b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 211578b18b78SStefan Hajnoczi } 211678b18b78SStefan Hajnoczi 211750f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 211878b18b78SStefan Hajnoczi { 2119dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 212078b18b78SStefan Hajnoczi } 212178b18b78SStefan Hajnoczi 212250f43f0fSJohn Snow static const BlkActionOps actions[] = { 212343de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 212443de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 212543de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 212643de7e2dSAlberto Garcia .commit = external_snapshot_commit, 212743de7e2dSAlberto Garcia .abort = external_snapshot_abort, 21284ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 212943de7e2dSAlberto Garcia }, 2130c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2131ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2132ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2133ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2134ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2135da763e83SFam Zheng .clean = external_snapshot_clean, 2136ba0c86a3SWenchao Xia }, 21373037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21383037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21393037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 21403037f364SStefan Hajnoczi .abort = drive_backup_abort, 21415d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21423037f364SStefan Hajnoczi }, 2143bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2144bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2145bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2146bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2147bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2148bd8baecdSFam Zheng }, 214978b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 215050f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 215178b18b78SStefan Hajnoczi .prepare = abort_prepare, 215278b18b78SStefan Hajnoczi .commit = abort_commit, 215378b18b78SStefan Hajnoczi }, 2154bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2155bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2156bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2157bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21585d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2159bbe86010SWenchao Xia }, 2160df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2161df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2162df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2163df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2164df9a681dSFam Zheng }, 2165df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2166df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2167df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2168df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2169df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2170df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2171df9a681dSFam Zheng } 2172ba0c86a3SWenchao Xia }; 2173ba0c86a3SWenchao Xia 217494d16a64SJohn Snow /** 217594d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 217694d16a64SJohn Snow * that structure with desired defaults if they are unset. 217794d16a64SJohn Snow */ 217894d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 217994d16a64SJohn Snow TransactionProperties *props) 218094d16a64SJohn Snow { 218194d16a64SJohn Snow if (!props) { 218294d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 218394d16a64SJohn Snow } 218494d16a64SJohn Snow 218594d16a64SJohn Snow if (!props->has_completion_mode) { 218694d16a64SJohn Snow props->has_completion_mode = true; 218794d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 218894d16a64SJohn Snow } 218994d16a64SJohn Snow 219094d16a64SJohn Snow return props; 219194d16a64SJohn Snow } 219294d16a64SJohn Snow 21938802d1fdSJeff Cody /* 2194b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2195b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 21968802d1fdSJeff Cody */ 219794d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 219894d16a64SJohn Snow bool has_props, 219994d16a64SJohn Snow struct TransactionProperties *props, 220094d16a64SJohn Snow Error **errp) 22018802d1fdSJeff Cody { 2202c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 220394d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 220450f43f0fSJohn Snow BlkActionState *state, *next; 220543e17041SLuiz Capitulino Error *local_err = NULL; 22068802d1fdSJeff Cody 220750f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 22088802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 22098802d1fdSJeff Cody 221094d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 221194d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 221294d16a64SJohn Snow */ 221394d16a64SJohn Snow props = get_transaction_properties(props); 221494d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 221594d16a64SJohn Snow block_job_txn = block_job_txn_new(); 221694d16a64SJohn Snow } 221794d16a64SJohn Snow 2218b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 22198802d1fdSJeff Cody bdrv_drain_all(); 22208802d1fdSJeff Cody 2221b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 22228802d1fdSJeff Cody while (NULL != dev_entry) { 2223c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 222450f43f0fSJohn Snow const BlkActionOps *ops; 222552e7c241SPaolo Bonzini 22268802d1fdSJeff Cody dev_info = dev_entry->value; 22278802d1fdSJeff Cody dev_entry = dev_entry->next; 22288802d1fdSJeff Cody 22296a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2230ba0c86a3SWenchao Xia 22316a8f9661SEric Blake ops = &actions[dev_info->type]; 2232aa3fe714SMax Reitz assert(ops->instance_size > 0); 2233aa3fe714SMax Reitz 2234ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2235ba5d6ab6SStefan Hajnoczi state->ops = ops; 2236ba5d6ab6SStefan Hajnoczi state->action = dev_info; 223794d16a64SJohn Snow state->block_job_txn = block_job_txn; 223894d16a64SJohn Snow state->txn_props = props; 2239ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22408802d1fdSJeff Cody 2241ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 224284d18f06SMarkus Armbruster if (local_err) { 22439b9877eeSWenchao Xia error_propagate(errp, local_err); 22449b9877eeSWenchao Xia goto delete_and_fail; 22459b9877eeSWenchao Xia } 224652e7c241SPaolo Bonzini } 22478802d1fdSJeff Cody 2248ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2249f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2250ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22518802d1fdSJeff Cody } 2252f9ea81e8SStefan Hajnoczi } 22538802d1fdSJeff Cody 22548802d1fdSJeff Cody /* success */ 22558802d1fdSJeff Cody goto exit; 22568802d1fdSJeff Cody 22578802d1fdSJeff Cody delete_and_fail: 2258b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2259ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2260ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2261ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2262ba0c86a3SWenchao Xia } 22638802d1fdSJeff Cody } 22648802d1fdSJeff Cody exit: 2265ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2266ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2267ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2268ba0c86a3SWenchao Xia } 2269ba5d6ab6SStefan Hajnoczi g_free(state); 22708802d1fdSJeff Cody } 227194d16a64SJohn Snow if (!has_props) { 227294d16a64SJohn Snow qapi_free_TransactionProperties(props); 227394d16a64SJohn Snow } 227494d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22758802d1fdSJeff Cody } 22768802d1fdSJeff Cody 22773a3086b7SJohn Snow static int do_open_tray(const char *device, bool force, Error **errp); 22783a3086b7SJohn Snow 2279c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 2280666daa68SMarkus Armbruster { 228138f54bd1SMax Reitz Error *local_err = NULL; 22823a3086b7SJohn Snow int rc; 2283666daa68SMarkus Armbruster 22843a3086b7SJohn Snow if (!has_force) { 22853a3086b7SJohn Snow force = false; 22863a3086b7SJohn Snow } 22873a3086b7SJohn Snow 22883a3086b7SJohn Snow rc = do_open_tray(device, force, &local_err); 228938f54bd1SMax Reitz if (local_err) { 229038f54bd1SMax Reitz error_propagate(errp, local_err); 2291c245b6a3SLuiz Capitulino return; 2292666daa68SMarkus Armbruster } 229392d48558SLuiz Capitulino 22943a3086b7SJohn Snow if (rc == EINPROGRESS) { 22953a3086b7SJohn Snow error_setg(errp, "Device '%s' is locked and force was not specified, " 22963a3086b7SJohn Snow "wait for tray to open and try again", device); 22973a3086b7SJohn Snow return; 22983a3086b7SJohn Snow } 22993a3086b7SJohn Snow 23006e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, errp); 2301666daa68SMarkus Armbruster } 2302666daa68SMarkus Armbruster 230312d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 230412d3ba82SBenoît Canet bool has_node_name, const char *node_name, 230512d3ba82SBenoît Canet const char *password, Error **errp) 2306666daa68SMarkus Armbruster { 230712d3ba82SBenoît Canet Error *local_err = NULL; 2308666daa68SMarkus Armbruster BlockDriverState *bs; 2309e3442099SStefan Hajnoczi AioContext *aio_context; 2310666daa68SMarkus Armbruster 231112d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 231212d3ba82SBenoît Canet has_node_name ? node_name : NULL, 231312d3ba82SBenoît Canet &local_err); 231484d18f06SMarkus Armbruster if (local_err) { 231512d3ba82SBenoît Canet error_propagate(errp, local_err); 2316a4dea8a9SLuiz Capitulino return; 2317666daa68SMarkus Armbruster } 2318666daa68SMarkus Armbruster 2319e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2320e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2321e3442099SStefan Hajnoczi 23224d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2323666daa68SMarkus Armbruster 2324e3442099SStefan Hajnoczi aio_context_release(aio_context); 2325e3442099SStefan Hajnoczi } 2326e3442099SStefan Hajnoczi 23273a3086b7SJohn Snow /** 23283a3086b7SJohn Snow * returns -errno on fatal error, +errno for non-fatal situations. 23293a3086b7SJohn Snow * errp will always be set when the return code is negative. 23303a3086b7SJohn Snow * May return +ENOSYS if the device has no tray, 23313a3086b7SJohn Snow * or +EINPROGRESS if the tray is locked and the guest has been notified. 23323a3086b7SJohn Snow */ 23333a3086b7SJohn Snow static int do_open_tray(const char *device, bool force, Error **errp) 23347d8a9f71SMax Reitz { 23357d8a9f71SMax Reitz BlockBackend *blk; 23367d8a9f71SMax Reitz bool locked; 23377d8a9f71SMax Reitz 23387d8a9f71SMax Reitz blk = blk_by_name(device); 23397d8a9f71SMax Reitz if (!blk) { 23407d8a9f71SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 23417d8a9f71SMax Reitz "Device '%s' not found", device); 23423a3086b7SJohn Snow return -ENODEV; 23437d8a9f71SMax Reitz } 23447d8a9f71SMax Reitz 23457d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23467d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23473a3086b7SJohn Snow return -ENOTSUP; 23487d8a9f71SMax Reitz } 23497d8a9f71SMax Reitz 235012c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 235112c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 23523a3086b7SJohn Snow return ENOSYS; 235312c7ec87SMax Reitz } 235412c7ec87SMax Reitz 23557d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23563a3086b7SJohn Snow return 0; 23577d8a9f71SMax Reitz } 23587d8a9f71SMax Reitz 23597d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23607d8a9f71SMax Reitz if (locked) { 23617d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23627d8a9f71SMax Reitz } 23637d8a9f71SMax Reitz 23647d8a9f71SMax Reitz if (!locked || force) { 23657d8a9f71SMax Reitz blk_dev_change_media_cb(blk, false); 23667d8a9f71SMax Reitz } 23673a3086b7SJohn Snow 23683a3086b7SJohn Snow if (locked && !force) { 23693a3086b7SJohn Snow return EINPROGRESS; 23703a3086b7SJohn Snow } 23713a3086b7SJohn Snow 23723a3086b7SJohn Snow return 0; 23733a3086b7SJohn Snow } 23743a3086b7SJohn Snow 23753a3086b7SJohn Snow void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 23763a3086b7SJohn Snow Error **errp) 23773a3086b7SJohn Snow { 23783a3086b7SJohn Snow if (!has_force) { 23793a3086b7SJohn Snow force = false; 23803a3086b7SJohn Snow } 23813a3086b7SJohn Snow do_open_tray(device, force, errp); 23827d8a9f71SMax Reitz } 23837d8a9f71SMax Reitz 2384abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2385abaaf59dSMax Reitz { 2386abaaf59dSMax Reitz BlockBackend *blk; 2387abaaf59dSMax Reitz 2388abaaf59dSMax Reitz blk = blk_by_name(device); 2389abaaf59dSMax Reitz if (!blk) { 2390abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2391abaaf59dSMax Reitz "Device '%s' not found", device); 2392abaaf59dSMax Reitz return; 2393abaaf59dSMax Reitz } 2394abaaf59dSMax Reitz 2395abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2396abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2397abaaf59dSMax Reitz return; 2398abaaf59dSMax Reitz } 2399abaaf59dSMax Reitz 240012c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 240112c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 240212c7ec87SMax Reitz return; 240312c7ec87SMax Reitz } 240412c7ec87SMax Reitz 2405abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2406abaaf59dSMax Reitz return; 2407abaaf59dSMax Reitz } 2408abaaf59dSMax Reitz 2409abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2410abaaf59dSMax Reitz } 2411abaaf59dSMax Reitz 24126e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 24132814f672SMax Reitz { 24142814f672SMax Reitz BlockBackend *blk; 24152814f672SMax Reitz BlockDriverState *bs; 24162814f672SMax Reitz AioContext *aio_context; 24172814f672SMax Reitz bool has_device; 24182814f672SMax Reitz 24192814f672SMax Reitz blk = blk_by_name(device); 24202814f672SMax Reitz if (!blk) { 24212814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 24222814f672SMax Reitz "Device '%s' not found", device); 24232814f672SMax Reitz return; 24242814f672SMax Reitz } 24252814f672SMax Reitz 24262814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 24272814f672SMax Reitz has_device = blk_get_attached_dev(blk); 24282814f672SMax Reitz 24292814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 24302814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 24312814f672SMax Reitz return; 24322814f672SMax Reitz } 24332814f672SMax Reitz 243412c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 24352814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 24362814f672SMax Reitz return; 24372814f672SMax Reitz } 24382814f672SMax Reitz 24392814f672SMax Reitz bs = blk_bs(blk); 24402814f672SMax Reitz if (!bs) { 24412814f672SMax Reitz return; 24422814f672SMax Reitz } 24432814f672SMax Reitz 24442814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 24452814f672SMax Reitz aio_context_acquire(aio_context); 24462814f672SMax Reitz 24472814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 24482814f672SMax Reitz goto out; 24492814f672SMax Reitz } 24502814f672SMax Reitz 24512814f672SMax Reitz blk_remove_bs(blk); 24522814f672SMax Reitz 245312c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 245412c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 245512c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 245612c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 245712c7ec87SMax Reitz * value passed here (i.e. false). */ 245812c7ec87SMax Reitz blk_dev_change_media_cb(blk, false); 245912c7ec87SMax Reitz } 246012c7ec87SMax Reitz 24612814f672SMax Reitz out: 24622814f672SMax Reitz aio_context_release(aio_context); 24632814f672SMax Reitz } 24642814f672SMax Reitz 2465d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device, 2466d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2467d1299882SMax Reitz { 2468d1299882SMax Reitz BlockBackend *blk; 2469d1299882SMax Reitz bool has_device; 2470d1299882SMax Reitz 2471d1299882SMax Reitz blk = blk_by_name(device); 2472d1299882SMax Reitz if (!blk) { 2473d1299882SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2474d1299882SMax Reitz "Device '%s' not found", device); 2475d1299882SMax Reitz return; 2476d1299882SMax Reitz } 2477d1299882SMax Reitz 2478d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2479d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2480d1299882SMax Reitz 2481d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2482d1299882SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2483d1299882SMax Reitz return; 2484d1299882SMax Reitz } 2485d1299882SMax Reitz 248612c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2487d1299882SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2488d1299882SMax Reitz return; 2489d1299882SMax Reitz } 2490d1299882SMax Reitz 2491d1299882SMax Reitz if (blk_bs(blk)) { 2492d1299882SMax Reitz error_setg(errp, "There already is a medium in device '%s'", device); 2493d1299882SMax Reitz return; 2494d1299882SMax Reitz } 2495d1299882SMax Reitz 2496d1299882SMax Reitz blk_insert_bs(blk, bs); 2497d1299882SMax Reitz 249812c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 249912c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 250012c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 250112c7ec87SMax Reitz * slot here. 250212c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 250312c7ec87SMax Reitz * value passed here (i.e. true). */ 250412c7ec87SMax Reitz blk_dev_change_media_cb(blk, true); 250512c7ec87SMax Reitz } 2506d1299882SMax Reitz } 2507d1299882SMax Reitz 25086e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2509d1299882SMax Reitz Error **errp) 2510d1299882SMax Reitz { 2511d1299882SMax Reitz BlockDriverState *bs; 2512d1299882SMax Reitz 2513d1299882SMax Reitz bs = bdrv_find_node(node_name); 2514d1299882SMax Reitz if (!bs) { 2515d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2516d1299882SMax Reitz return; 2517d1299882SMax Reitz } 2518d1299882SMax Reitz 25191f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2520d1299882SMax Reitz error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 25211f0c461bSKevin Wolf bdrv_get_parent_name(bs)); 2522d1299882SMax Reitz return; 2523d1299882SMax Reitz } 2524d1299882SMax Reitz 2525d1299882SMax Reitz qmp_blockdev_insert_anon_medium(device, bs, errp); 2526d1299882SMax Reitz } 2527d1299882SMax Reitz 252824fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename, 252924fb4133SMax Reitz bool has_format, const char *format, 253039ff43d9SMax Reitz bool has_read_only, 253139ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 253224fb4133SMax Reitz Error **errp) 2533de2c6c05SMax Reitz { 2534de2c6c05SMax Reitz BlockBackend *blk; 2535de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 25365b363937SMax Reitz int bdrv_flags; 2537de2c6c05SMax Reitz QDict *options = NULL; 2538de2c6c05SMax Reitz Error *err = NULL; 2539de2c6c05SMax Reitz 2540de2c6c05SMax Reitz blk = blk_by_name(device); 2541de2c6c05SMax Reitz if (!blk) { 2542de2c6c05SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2543de2c6c05SMax Reitz "Device '%s' not found", device); 2544de2c6c05SMax Reitz goto fail; 2545de2c6c05SMax Reitz } 2546de2c6c05SMax Reitz 2547de2c6c05SMax Reitz if (blk_bs(blk)) { 2548de2c6c05SMax Reitz blk_update_root_state(blk); 2549de2c6c05SMax Reitz } 2550de2c6c05SMax Reitz 2551de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2552156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2553156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2554de2c6c05SMax Reitz 255539ff43d9SMax Reitz if (!has_read_only) { 255639ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 255739ff43d9SMax Reitz } 255839ff43d9SMax Reitz 255939ff43d9SMax Reitz switch (read_only) { 256039ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 256139ff43d9SMax Reitz break; 256239ff43d9SMax Reitz 256339ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 256439ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 256539ff43d9SMax Reitz break; 256639ff43d9SMax Reitz 256739ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 256839ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 256939ff43d9SMax Reitz break; 257039ff43d9SMax Reitz 257139ff43d9SMax Reitz default: 257239ff43d9SMax Reitz abort(); 257339ff43d9SMax Reitz } 257439ff43d9SMax Reitz 257524fb4133SMax Reitz if (has_format) { 2576de2c6c05SMax Reitz options = qdict_new(); 2577de2c6c05SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2578de2c6c05SMax Reitz } 2579de2c6c05SMax Reitz 25805b363937SMax Reitz medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 25815b363937SMax Reitz if (!medium_bs) { 2582de2c6c05SMax Reitz goto fail; 2583de2c6c05SMax Reitz } 2584de2c6c05SMax Reitz 2585de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2586de2c6c05SMax Reitz if (err) { 2587de2c6c05SMax Reitz error_propagate(errp, err); 2588de2c6c05SMax Reitz goto fail; 2589de2c6c05SMax Reitz } 2590de2c6c05SMax Reitz 2591de2c6c05SMax Reitz qmp_blockdev_open_tray(device, false, false, &err); 2592de2c6c05SMax Reitz if (err) { 2593de2c6c05SMax Reitz error_propagate(errp, err); 2594de2c6c05SMax Reitz goto fail; 2595de2c6c05SMax Reitz } 2596de2c6c05SMax Reitz 25976e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, &err); 2598de2c6c05SMax Reitz if (err) { 2599de2c6c05SMax Reitz error_propagate(errp, err); 2600de2c6c05SMax Reitz goto fail; 2601de2c6c05SMax Reitz } 2602de2c6c05SMax Reitz 2603de2c6c05SMax Reitz qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2604de2c6c05SMax Reitz if (err) { 2605de2c6c05SMax Reitz error_propagate(errp, err); 2606de2c6c05SMax Reitz goto fail; 2607de2c6c05SMax Reitz } 2608de2c6c05SMax Reitz 2609a5614993SKevin Wolf blk_apply_root_state(blk, medium_bs); 2610a5614993SKevin Wolf 2611de2c6c05SMax Reitz qmp_blockdev_close_tray(device, errp); 2612de2c6c05SMax Reitz 2613de2c6c05SMax Reitz fail: 2614de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2615de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2616de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2617de2c6c05SMax Reitz bdrv_unref(medium_bs); 2618de2c6c05SMax Reitz } 2619de2c6c05SMax Reitz 2620727f005eSZhi Yong Wu /* throttling disk I/O limits */ 262180047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 26223e9fab69SBenoît Canet int64_t bps_wr, 26233e9fab69SBenoît Canet int64_t iops, 26243e9fab69SBenoît Canet int64_t iops_rd, 26253e9fab69SBenoît Canet int64_t iops_wr, 26263e9fab69SBenoît Canet bool has_bps_max, 26273e9fab69SBenoît Canet int64_t bps_max, 26283e9fab69SBenoît Canet bool has_bps_rd_max, 26293e9fab69SBenoît Canet int64_t bps_rd_max, 26303e9fab69SBenoît Canet bool has_bps_wr_max, 26313e9fab69SBenoît Canet int64_t bps_wr_max, 26323e9fab69SBenoît Canet bool has_iops_max, 26333e9fab69SBenoît Canet int64_t iops_max, 26343e9fab69SBenoît Canet bool has_iops_rd_max, 26353e9fab69SBenoît Canet int64_t iops_rd_max, 26363e9fab69SBenoît Canet bool has_iops_wr_max, 26372024c1dfSBenoît Canet int64_t iops_wr_max, 2638dce13204SAlberto Garcia bool has_bps_max_length, 2639dce13204SAlberto Garcia int64_t bps_max_length, 2640dce13204SAlberto Garcia bool has_bps_rd_max_length, 2641dce13204SAlberto Garcia int64_t bps_rd_max_length, 2642dce13204SAlberto Garcia bool has_bps_wr_max_length, 2643dce13204SAlberto Garcia int64_t bps_wr_max_length, 2644dce13204SAlberto Garcia bool has_iops_max_length, 2645dce13204SAlberto Garcia int64_t iops_max_length, 2646dce13204SAlberto Garcia bool has_iops_rd_max_length, 2647dce13204SAlberto Garcia int64_t iops_rd_max_length, 2648dce13204SAlberto Garcia bool has_iops_wr_max_length, 2649dce13204SAlberto Garcia int64_t iops_wr_max_length, 26502024c1dfSBenoît Canet bool has_iops_size, 265176f4afb4SAlberto Garcia int64_t iops_size, 265276f4afb4SAlberto Garcia bool has_group, 265376f4afb4SAlberto Garcia const char *group, Error **errp) 2654727f005eSZhi Yong Wu { 2655cc0681c4SBenoît Canet ThrottleConfig cfg; 2656727f005eSZhi Yong Wu BlockDriverState *bs; 2657a0e8544cSFam Zheng BlockBackend *blk; 2658b15446fdSStefan Hajnoczi AioContext *aio_context; 2659727f005eSZhi Yong Wu 2660a0e8544cSFam Zheng blk = blk_by_name(device); 2661a0e8544cSFam Zheng if (!blk) { 266275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 266375158ebbSMarkus Armbruster "Device '%s' not found", device); 266480047da5SLuiz Capitulino return; 2665727f005eSZhi Yong Wu } 26665433c24fSMax Reitz 26675433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 26685433c24fSMax Reitz aio_context_acquire(aio_context); 26695433c24fSMax Reitz 2670a0e8544cSFam Zheng bs = blk_bs(blk); 26715433c24fSMax Reitz if (!bs) { 26725433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 26735433c24fSMax Reitz goto out; 26745433c24fSMax Reitz } 2675727f005eSZhi Yong Wu 26761588ab5dSAlberto Garcia throttle_config_init(&cfg); 2677cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2678cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2679cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2680727f005eSZhi Yong Wu 2681cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2682cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2683cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2684cc0681c4SBenoît Canet 26853e9fab69SBenoît Canet if (has_bps_max) { 26863e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 26873e9fab69SBenoît Canet } 26883e9fab69SBenoît Canet if (has_bps_rd_max) { 26893e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 26903e9fab69SBenoît Canet } 26913e9fab69SBenoît Canet if (has_bps_wr_max) { 26923e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 26933e9fab69SBenoît Canet } 26943e9fab69SBenoît Canet if (has_iops_max) { 26953e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 26963e9fab69SBenoît Canet } 26973e9fab69SBenoît Canet if (has_iops_rd_max) { 26983e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 26993e9fab69SBenoît Canet } 27003e9fab69SBenoît Canet if (has_iops_wr_max) { 27013e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 27023e9fab69SBenoît Canet } 2703cc0681c4SBenoît Canet 2704dce13204SAlberto Garcia if (has_bps_max_length) { 2705dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = bps_max_length; 2706dce13204SAlberto Garcia } 2707dce13204SAlberto Garcia if (has_bps_rd_max_length) { 2708dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_READ].burst_length = bps_rd_max_length; 2709dce13204SAlberto Garcia } 2710dce13204SAlberto Garcia if (has_bps_wr_max_length) { 2711dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_WRITE].burst_length = bps_wr_max_length; 2712dce13204SAlberto Garcia } 2713dce13204SAlberto Garcia if (has_iops_max_length) { 2714dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = iops_max_length; 2715dce13204SAlberto Garcia } 2716dce13204SAlberto Garcia if (has_iops_rd_max_length) { 2717dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_READ].burst_length = iops_rd_max_length; 2718dce13204SAlberto Garcia } 2719dce13204SAlberto Garcia if (has_iops_wr_max_length) { 2720dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_WRITE].burst_length = iops_wr_max_length; 2721dce13204SAlberto Garcia } 2722dce13204SAlberto Garcia 27232024c1dfSBenoît Canet if (has_iops_size) { 27242024c1dfSBenoît Canet cfg.op_size = iops_size; 27252024c1dfSBenoît Canet } 2726cc0681c4SBenoît Canet 2727d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 27285433c24fSMax Reitz goto out; 2729727f005eSZhi Yong Wu } 2730727f005eSZhi Yong Wu 273176f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 273276f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 273376f4afb4SAlberto Garcia * just update the throttling group. */ 273497148076SKevin Wolf if (!blk_get_public(blk)->throttle_state) { 273597148076SKevin Wolf blk_io_limits_enable(blk, has_group ? group : device); 273676f4afb4SAlberto Garcia } else if (has_group) { 273797148076SKevin Wolf blk_io_limits_update_group(blk, group); 2738727f005eSZhi Yong Wu } 273976f4afb4SAlberto Garcia /* Set the new throttling configuration */ 274097148076SKevin Wolf blk_set_io_limits(blk, &cfg); 274197148076SKevin Wolf } else if (blk_get_public(blk)->throttle_state) { 274276f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 274397148076SKevin Wolf blk_io_limits_disable(blk); 2744727f005eSZhi Yong Wu } 2745b15446fdSStefan Hajnoczi 27465433c24fSMax Reitz out: 2747b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2748727f005eSZhi Yong Wu } 2749727f005eSZhi Yong Wu 2750341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2751341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2752341ebc2fSJohn Snow Error **errp) 2753341ebc2fSJohn Snow { 2754341ebc2fSJohn Snow AioContext *aio_context; 2755341ebc2fSJohn Snow BlockDriverState *bs; 2756341ebc2fSJohn Snow 2757341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2758341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2759341ebc2fSJohn Snow return; 2760341ebc2fSJohn Snow } 2761341ebc2fSJohn Snow 2762341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2763341ebc2fSJohn Snow if (!bs) { 2764341ebc2fSJohn Snow return; 2765341ebc2fSJohn Snow } 2766341ebc2fSJohn Snow 2767341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2768341ebc2fSJohn Snow aio_context_acquire(aio_context); 2769341ebc2fSJohn Snow 2770341ebc2fSJohn Snow if (has_granularity) { 2771341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2772341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2773341ebc2fSJohn Snow "and at least 512"); 2774341ebc2fSJohn Snow goto out; 2775341ebc2fSJohn Snow } 2776341ebc2fSJohn Snow } else { 2777341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2778341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2779341ebc2fSJohn Snow } 2780341ebc2fSJohn Snow 2781341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2782341ebc2fSJohn Snow 2783341ebc2fSJohn Snow out: 2784341ebc2fSJohn Snow aio_context_release(aio_context); 2785341ebc2fSJohn Snow } 2786341ebc2fSJohn Snow 2787341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2788341ebc2fSJohn Snow Error **errp) 2789341ebc2fSJohn Snow { 2790341ebc2fSJohn Snow AioContext *aio_context; 2791341ebc2fSJohn Snow BlockDriverState *bs; 2792341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2793341ebc2fSJohn Snow 2794341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2795341ebc2fSJohn Snow if (!bitmap || !bs) { 2796341ebc2fSJohn Snow return; 2797341ebc2fSJohn Snow } 2798341ebc2fSJohn Snow 27999bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 28009bd2b08fSJohn Snow error_setg(errp, 28019bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 28029bd2b08fSJohn Snow name); 28039bd2b08fSJohn Snow goto out; 28049bd2b08fSJohn Snow } 280520dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2806341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2807341ebc2fSJohn Snow 28089bd2b08fSJohn Snow out: 2809341ebc2fSJohn Snow aio_context_release(aio_context); 2810341ebc2fSJohn Snow } 2811341ebc2fSJohn Snow 2812e74e6b78SJohn Snow /** 2813e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2814e74e6b78SJohn Snow * immediately after a full backup operation. 2815e74e6b78SJohn Snow */ 2816e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2817e74e6b78SJohn Snow Error **errp) 2818e74e6b78SJohn Snow { 2819e74e6b78SJohn Snow AioContext *aio_context; 2820e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2821e74e6b78SJohn Snow BlockDriverState *bs; 2822e74e6b78SJohn Snow 2823e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2824e74e6b78SJohn Snow if (!bitmap || !bs) { 2825e74e6b78SJohn Snow return; 2826e74e6b78SJohn Snow } 2827e74e6b78SJohn Snow 2828e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2829e74e6b78SJohn Snow error_setg(errp, 2830e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2831e74e6b78SJohn Snow name); 2832e74e6b78SJohn Snow goto out; 2833e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2834e74e6b78SJohn Snow error_setg(errp, 2835e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2836e74e6b78SJohn Snow name); 2837e74e6b78SJohn Snow goto out; 2838e74e6b78SJohn Snow } 2839e74e6b78SJohn Snow 2840df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2841e74e6b78SJohn Snow 2842e74e6b78SJohn Snow out: 2843e74e6b78SJohn Snow aio_context_release(aio_context); 2844e74e6b78SJohn Snow } 2845e74e6b78SJohn Snow 2846072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 28479063f814SRyan Harper { 28489063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 28497e7d56d9SMarkus Armbruster BlockBackend *blk; 28509063f814SRyan Harper BlockDriverState *bs; 28518ad4202bSStefan Hajnoczi AioContext *aio_context; 28523718d8abSFam Zheng Error *local_err = NULL; 28539063f814SRyan Harper 28542073d410SKevin Wolf bs = bdrv_find_node(id); 28552073d410SKevin Wolf if (bs) { 28562073d410SKevin Wolf qmp_x_blockdev_del(false, NULL, true, id, &local_err); 28572073d410SKevin Wolf if (local_err) { 28582073d410SKevin Wolf error_report_err(local_err); 28592073d410SKevin Wolf } 28602073d410SKevin Wolf return; 28612073d410SKevin Wolf } 28622073d410SKevin Wolf 28637e7d56d9SMarkus Armbruster blk = blk_by_name(id); 28647e7d56d9SMarkus Armbruster if (!blk) { 2865b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2866072ebe6bSMarkus Armbruster return; 28679063f814SRyan Harper } 28688ad4202bSStefan Hajnoczi 286926f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 287048f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 287148f364ddSMarkus Armbruster " is not supported"); 2872072ebe6bSMarkus Armbruster return; 287348f364ddSMarkus Armbruster } 287448f364ddSMarkus Armbruster 28755433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 28768ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 28778ad4202bSStefan Hajnoczi 28785433c24fSMax Reitz bs = blk_bs(blk); 28795433c24fSMax Reitz if (bs) { 28803718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2881565f65d2SMarkus Armbruster error_report_err(local_err); 28828ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2883072ebe6bSMarkus Armbruster return; 28848591675fSMarcelo Tosatti } 28859063f814SRyan Harper 2886938abd43SMax Reitz blk_remove_bs(blk); 28875433c24fSMax Reitz } 28889063f814SRyan Harper 28897c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 2890efaa7c4eSMax Reitz monitor_remove_blk(blk); 2891efaa7c4eSMax Reitz 28927c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 28937c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 2894d22b2f41SRyan Harper */ 2895a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 2896293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2897373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2898293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2899d22b2f41SRyan Harper } else { 2900b9fe8a7aSMarkus Armbruster blk_unref(blk); 2901d22b2f41SRyan Harper } 29029063f814SRyan Harper 29038ad4202bSStefan Hajnoczi aio_context_release(aio_context); 29049063f814SRyan Harper } 29056d4a2b3aSChristoph Hellwig 29063b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 29073b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 29083b1dbd11SBenoît Canet int64_t size, Error **errp) 29096d4a2b3aSChristoph Hellwig { 29103b1dbd11SBenoît Canet Error *local_err = NULL; 29116d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2912927e0e76SStefan Hajnoczi AioContext *aio_context; 29138732901eSKevin Wolf int ret; 29146d4a2b3aSChristoph Hellwig 29153b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 29163b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 29173b1dbd11SBenoît Canet &local_err); 291884d18f06SMarkus Armbruster if (local_err) { 29193b1dbd11SBenoît Canet error_propagate(errp, local_err); 29203b1dbd11SBenoît Canet return; 29213b1dbd11SBenoît Canet } 29223b1dbd11SBenoît Canet 2923927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2924927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2925927e0e76SStefan Hajnoczi 29263b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2927c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2928927e0e76SStefan Hajnoczi goto out; 29296d4a2b3aSChristoph Hellwig } 29306d4a2b3aSChristoph Hellwig 29316d4a2b3aSChristoph Hellwig if (size < 0) { 2932c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2933927e0e76SStefan Hajnoczi goto out; 29346d4a2b3aSChristoph Hellwig } 29356d4a2b3aSChristoph Hellwig 29369c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2937c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2938927e0e76SStefan Hajnoczi goto out; 29399c75e168SJeff Cody } 29409c75e168SJeff Cody 294192b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 294292b7a08dSPeter Lieven bdrv_drain_all(); 294392b7a08dSPeter Lieven 29448732901eSKevin Wolf ret = bdrv_truncate(bs, size); 29458732901eSKevin Wolf switch (ret) { 2946939a1cc3SStefan Hajnoczi case 0: 2947939a1cc3SStefan Hajnoczi break; 2948939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2949c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2950939a1cc3SStefan Hajnoczi break; 2951939a1cc3SStefan Hajnoczi case -ENOTSUP: 2952c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2953939a1cc3SStefan Hajnoczi break; 2954939a1cc3SStefan Hajnoczi case -EACCES: 295581e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2956939a1cc3SStefan Hajnoczi break; 2957939a1cc3SStefan Hajnoczi case -EBUSY: 2958c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2959939a1cc3SStefan Hajnoczi break; 2960939a1cc3SStefan Hajnoczi default: 29618732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2962939a1cc3SStefan Hajnoczi break; 29636d4a2b3aSChristoph Hellwig } 2964927e0e76SStefan Hajnoczi 2965927e0e76SStefan Hajnoczi out: 2966927e0e76SStefan Hajnoczi aio_context_release(aio_context); 29676d4a2b3aSChristoph Hellwig } 296812bd451fSStefan Hajnoczi 29699abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 297012bd451fSStefan Hajnoczi { 2971723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2972723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2973723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2974723c5d93SStefan Hajnoczi */ 2975723c5d93SStefan Hajnoczi 297612bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2977bcada37bSWenchao Xia const char *msg = NULL; 297812bd451fSStefan Hajnoczi 29799abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 298012bd451fSStefan Hajnoczi 298112bd451fSStefan Hajnoczi assert(bs->job); 2982bcada37bSWenchao Xia 298312bd451fSStefan Hajnoczi if (ret < 0) { 2984bcada37bSWenchao Xia msg = strerror(-ret); 298512bd451fSStefan Hajnoczi } 298612bd451fSStefan Hajnoczi 2987370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 2988bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 2989370521a1SStefan Hajnoczi } else { 2990bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 2991370521a1SStefan Hajnoczi } 299212bd451fSStefan Hajnoczi } 299312bd451fSStefan Hajnoczi 299413d8cc51SJeff Cody void qmp_block_stream(const char *device, 299513d8cc51SJeff Cody bool has_base, const char *base, 299613d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 299713d8cc51SJeff Cody bool has_speed, int64_t speed, 29981d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 29991d809098SPaolo Bonzini Error **errp) 300012bd451fSStefan Hajnoczi { 3001a0e8544cSFam Zheng BlockBackend *blk; 300212bd451fSStefan Hajnoczi BlockDriverState *bs; 3003c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 3004f3e69bebSStefan Hajnoczi AioContext *aio_context; 3005fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 300613d8cc51SJeff Cody const char *base_name = NULL; 300712bd451fSStefan Hajnoczi 30081d809098SPaolo Bonzini if (!has_on_error) { 30091d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 30101d809098SPaolo Bonzini } 30111d809098SPaolo Bonzini 3012a0e8544cSFam Zheng blk = blk_by_name(device); 3013a0e8544cSFam Zheng if (!blk) { 301475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 301575158ebbSMarkus Armbruster "Device '%s' not found", device); 301612bd451fSStefan Hajnoczi return; 301712bd451fSStefan Hajnoczi } 301812bd451fSStefan Hajnoczi 30195433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3020f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 3021f3e69bebSStefan Hajnoczi 30225433c24fSMax Reitz if (!blk_is_available(blk)) { 30235433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 30245433c24fSMax Reitz goto out; 30255433c24fSMax Reitz } 30265433c24fSMax Reitz bs = blk_bs(blk); 30275433c24fSMax Reitz 3028628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 3029f3e69bebSStefan Hajnoczi goto out; 3030628ff683SFam Zheng } 3031628ff683SFam Zheng 303213d8cc51SJeff Cody if (has_base) { 3033c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3034c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3035c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3036f3e69bebSStefan Hajnoczi goto out; 303712bd451fSStefan Hajnoczi } 3038f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 303913d8cc51SJeff Cody base_name = base; 3040c8c3080fSMarcelo Tosatti } 304112bd451fSStefan Hajnoczi 304213d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 304313d8cc51SJeff Cody * file, and specifying one is therefore an error */ 304413d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 304513d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 304613d8cc51SJeff Cody "entire chain"); 3047f3e69bebSStefan Hajnoczi goto out; 304813d8cc51SJeff Cody } 304913d8cc51SJeff Cody 305013d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 305113d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 305213d8cc51SJeff Cody 305313d8cc51SJeff Cody stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 30541d809098SPaolo Bonzini on_error, block_job_cb, bs, &local_err); 305584d18f06SMarkus Armbruster if (local_err) { 3056fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3057f3e69bebSStefan Hajnoczi goto out; 305812bd451fSStefan Hajnoczi } 305912bd451fSStefan Hajnoczi 306012bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3061f3e69bebSStefan Hajnoczi 3062f3e69bebSStefan Hajnoczi out: 3063f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 306412bd451fSStefan Hajnoczi } 30652d47c6e9SStefan Hajnoczi 3066ed61fc10SJeff Cody void qmp_block_commit(const char *device, 30677676e2c5SJeff Cody bool has_base, const char *base, 30687676e2c5SJeff Cody bool has_top, const char *top, 306954e26900SJeff Cody bool has_backing_file, const char *backing_file, 3070ed61fc10SJeff Cody bool has_speed, int64_t speed, 3071ed61fc10SJeff Cody Error **errp) 3072ed61fc10SJeff Cody { 3073a0e8544cSFam Zheng BlockBackend *blk; 3074ed61fc10SJeff Cody BlockDriverState *bs; 3075ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 30769e85cd5cSStefan Hajnoczi AioContext *aio_context; 3077ed61fc10SJeff Cody Error *local_err = NULL; 3078ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3079ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3080ed61fc10SJeff Cody */ 308192aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3082ed61fc10SJeff Cody 308354504663SMax Reitz if (!has_speed) { 308454504663SMax Reitz speed = 0; 308554504663SMax Reitz } 308654504663SMax Reitz 30877676e2c5SJeff Cody /* Important Note: 30887676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 30897676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 30907676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 30917676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 3092a0e8544cSFam Zheng blk = blk_by_name(device); 3093a0e8544cSFam Zheng if (!blk) { 309475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 309575158ebbSMarkus Armbruster "Device '%s' not found", device); 3096ed61fc10SJeff Cody return; 3097ed61fc10SJeff Cody } 3098ed61fc10SJeff Cody 30995433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 31009e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 31019e85cd5cSStefan Hajnoczi 31025433c24fSMax Reitz if (!blk_is_available(blk)) { 31035433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 31045433c24fSMax Reitz goto out; 31055433c24fSMax Reitz } 31065433c24fSMax Reitz bs = blk_bs(blk); 31075433c24fSMax Reitz 3108bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 31099e85cd5cSStefan Hajnoczi goto out; 3110628ff683SFam Zheng } 3111628ff683SFam Zheng 3112ed61fc10SJeff Cody /* default top_bs is the active layer */ 3113ed61fc10SJeff Cody top_bs = bs; 3114ed61fc10SJeff Cody 31157676e2c5SJeff Cody if (has_top && top) { 3116ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3117ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3118ed61fc10SJeff Cody } 3119ed61fc10SJeff Cody } 3120ed61fc10SJeff Cody 3121ed61fc10SJeff Cody if (top_bs == NULL) { 3122ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 31239e85cd5cSStefan Hajnoczi goto out; 3124ed61fc10SJeff Cody } 3125ed61fc10SJeff Cody 31269e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 31279e85cd5cSStefan Hajnoczi 3128d5208c45SJeff Cody if (has_base && base) { 3129d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3130d5208c45SJeff Cody } else { 3131d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3132d5208c45SJeff Cody } 3133d5208c45SJeff Cody 3134d5208c45SJeff Cody if (base_bs == NULL) { 3135c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 31369e85cd5cSStefan Hajnoczi goto out; 3137d5208c45SJeff Cody } 3138d5208c45SJeff Cody 31399e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 31409e85cd5cSStefan Hajnoczi 3141bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3142bb00021dSFam Zheng goto out; 3143bb00021dSFam Zheng } 3144bb00021dSFam Zheng 31457676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 31467676e2c5SJeff Cody if (top_bs == base_bs) { 31477676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 31489e85cd5cSStefan Hajnoczi goto out; 31497676e2c5SJeff Cody } 31507676e2c5SJeff Cody 315120a63d2cSFam Zheng if (top_bs == bs) { 315254e26900SJeff Cody if (has_backing_file) { 315354e26900SJeff Cody error_setg(errp, "'backing-file' specified," 315454e26900SJeff Cody " but 'top' is the active layer"); 31559e85cd5cSStefan Hajnoczi goto out; 315654e26900SJeff Cody } 315720a63d2cSFam Zheng commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 315820a63d2cSFam Zheng bs, &local_err); 315920a63d2cSFam Zheng } else { 3160ed61fc10SJeff Cody commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 316154e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 316220a63d2cSFam Zheng } 3163ed61fc10SJeff Cody if (local_err != NULL) { 3164ed61fc10SJeff Cody error_propagate(errp, local_err); 31659e85cd5cSStefan Hajnoczi goto out; 3166ed61fc10SJeff Cody } 31679e85cd5cSStefan Hajnoczi 31689e85cd5cSStefan Hajnoczi out: 31699e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3170ed61fc10SJeff Cody } 3171ed61fc10SJeff Cody 317278f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 317399a9addfSStefan Hajnoczi bool has_format, const char *format, 3174b53169eaSStefan Hajnoczi enum MirrorSyncMode sync, 317599a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 317699a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 3177d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 317878f51fdeSJohn Snow bool has_on_source_error, 317978f51fdeSJohn Snow BlockdevOnError on_source_error, 318078f51fdeSJohn Snow bool has_on_target_error, 318178f51fdeSJohn Snow BlockdevOnError on_target_error, 318278f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 318399a9addfSStefan Hajnoczi { 3184a0e8544cSFam Zheng BlockBackend *blk; 318599a9addfSStefan Hajnoczi BlockDriverState *bs; 318699a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3187fc5d3f84SIan Main BlockDriverState *source = NULL; 3188d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3189761731b1SStefan Hajnoczi AioContext *aio_context; 3190e6641719SMax Reitz QDict *options = NULL; 319199a9addfSStefan Hajnoczi Error *local_err = NULL; 319299a9addfSStefan Hajnoczi int flags; 319399a9addfSStefan Hajnoczi int64_t size; 319499a9addfSStefan Hajnoczi 319599a9addfSStefan Hajnoczi if (!has_speed) { 319699a9addfSStefan Hajnoczi speed = 0; 319799a9addfSStefan Hajnoczi } 319899a9addfSStefan Hajnoczi if (!has_on_source_error) { 319999a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 320099a9addfSStefan Hajnoczi } 320199a9addfSStefan Hajnoczi if (!has_on_target_error) { 320299a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 320399a9addfSStefan Hajnoczi } 320499a9addfSStefan Hajnoczi if (!has_mode) { 320599a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 320699a9addfSStefan Hajnoczi } 320799a9addfSStefan Hajnoczi 3208a0e8544cSFam Zheng blk = blk_by_name(device); 3209a0e8544cSFam Zheng if (!blk) { 321075158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 321175158ebbSMarkus Armbruster "Device '%s' not found", device); 321299a9addfSStefan Hajnoczi return; 321399a9addfSStefan Hajnoczi } 321499a9addfSStefan Hajnoczi 32155433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3216761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3217761731b1SStefan Hajnoczi 3218c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 3219c29c1dd3SFam Zheng * do an early check redundantly. */ 32205433c24fSMax Reitz if (!blk_is_available(blk)) { 3221c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3222761731b1SStefan Hajnoczi goto out; 322399a9addfSStefan Hajnoczi } 32245433c24fSMax Reitz bs = blk_bs(blk); 322599a9addfSStefan Hajnoczi 322699a9addfSStefan Hajnoczi if (!has_format) { 322799a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 322899a9addfSStefan Hajnoczi } 322999a9addfSStefan Hajnoczi 3230c29c1dd3SFam Zheng /* Early check to avoid creating target */ 32313718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3232761731b1SStefan Hajnoczi goto out; 323399a9addfSStefan Hajnoczi } 323499a9addfSStefan Hajnoczi 323561de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 323699a9addfSStefan Hajnoczi 3237fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3238fc5d3f84SIan Main * on top of. */ 3239fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 3240760e0063SKevin Wolf source = backing_bs(bs); 3241fc5d3f84SIan Main if (!source) { 3242fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 3243fc5d3f84SIan Main } 3244fc5d3f84SIan Main } 3245fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 3246fc5d3f84SIan Main source = bs; 3247fc5d3f84SIan Main } 3248fc5d3f84SIan Main 324999a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 325099a9addfSStefan Hajnoczi if (size < 0) { 325199a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3252761731b1SStefan Hajnoczi goto out; 325399a9addfSStefan Hajnoczi } 325499a9addfSStefan Hajnoczi 325599a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 3256e6641719SMax Reitz assert(format); 3257fc5d3f84SIan Main if (source) { 3258fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 3259fc5d3f84SIan Main source->drv->format_name, NULL, 3260fc5d3f84SIan Main size, flags, &local_err, false); 3261fc5d3f84SIan Main } else { 3262fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 3263fc5d3f84SIan Main size, flags, &local_err, false); 3264fc5d3f84SIan Main } 326599a9addfSStefan Hajnoczi } 326699a9addfSStefan Hajnoczi 326784d18f06SMarkus Armbruster if (local_err) { 326899a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3269761731b1SStefan Hajnoczi goto out; 327099a9addfSStefan Hajnoczi } 327199a9addfSStefan Hajnoczi 3272e6641719SMax Reitz if (format) { 3273e6641719SMax Reitz options = qdict_new(); 3274e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3275e6641719SMax Reitz } 3276e6641719SMax Reitz 32775b363937SMax Reitz target_bs = bdrv_open(target, NULL, options, flags, errp); 32785b363937SMax Reitz if (!target_bs) { 3279761731b1SStefan Hajnoczi goto out; 328099a9addfSStefan Hajnoczi } 328199a9addfSStefan Hajnoczi 3282761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3283761731b1SStefan Hajnoczi 3284d58d8453SJohn Snow if (has_bitmap) { 3285d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3286d58d8453SJohn Snow if (!bmap) { 3287d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 32880702d3d8SMax Reitz bdrv_unref(target_bs); 3289d58d8453SJohn Snow goto out; 3290d58d8453SJohn Snow } 3291d58d8453SJohn Snow } 3292d58d8453SJohn Snow 3293d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, bmap, 3294d58d8453SJohn Snow on_source_error, on_target_error, 329578f51fdeSJohn Snow block_job_cb, bs, txn, &local_err); 32964f6fd349SFam Zheng bdrv_unref(target_bs); 3297*5c438bc6SKevin Wolf if (local_err != NULL) { 329899a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3299761731b1SStefan Hajnoczi goto out; 330099a9addfSStefan Hajnoczi } 3301761731b1SStefan Hajnoczi 3302761731b1SStefan Hajnoczi out: 3303761731b1SStefan Hajnoczi aio_context_release(aio_context); 330499a9addfSStefan Hajnoczi } 330599a9addfSStefan Hajnoczi 330678f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target, 330778f51fdeSJohn Snow bool has_format, const char *format, 330878f51fdeSJohn Snow enum MirrorSyncMode sync, 330978f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 331078f51fdeSJohn Snow bool has_speed, int64_t speed, 331178f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 331278f51fdeSJohn Snow bool has_on_source_error, BlockdevOnError on_source_error, 331378f51fdeSJohn Snow bool has_on_target_error, BlockdevOnError on_target_error, 331478f51fdeSJohn Snow Error **errp) 331578f51fdeSJohn Snow { 331678f51fdeSJohn Snow return do_drive_backup(device, target, has_format, format, sync, 331778f51fdeSJohn Snow has_mode, mode, has_speed, speed, 331878f51fdeSJohn Snow has_bitmap, bitmap, 331978f51fdeSJohn Snow has_on_source_error, on_source_error, 332078f51fdeSJohn Snow has_on_target_error, on_target_error, 332178f51fdeSJohn Snow NULL, errp); 332278f51fdeSJohn Snow } 332378f51fdeSJohn Snow 3324c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3325c13163fbSBenoît Canet { 3326d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3327c13163fbSBenoît Canet } 3328c13163fbSBenoît Canet 332978f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target, 3330c29c1dd3SFam Zheng enum MirrorSyncMode sync, 3331c29c1dd3SFam Zheng bool has_speed, int64_t speed, 3332c29c1dd3SFam Zheng bool has_on_source_error, 3333c29c1dd3SFam Zheng BlockdevOnError on_source_error, 3334c29c1dd3SFam Zheng bool has_on_target_error, 3335c29c1dd3SFam Zheng BlockdevOnError on_target_error, 333678f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 3337c29c1dd3SFam Zheng { 33385433c24fSMax Reitz BlockBackend *blk, *target_blk; 3339c29c1dd3SFam Zheng BlockDriverState *bs; 3340c29c1dd3SFam Zheng BlockDriverState *target_bs; 3341c29c1dd3SFam Zheng Error *local_err = NULL; 3342c29c1dd3SFam Zheng AioContext *aio_context; 3343c29c1dd3SFam Zheng 3344c29c1dd3SFam Zheng if (!has_speed) { 3345c29c1dd3SFam Zheng speed = 0; 3346c29c1dd3SFam Zheng } 3347c29c1dd3SFam Zheng if (!has_on_source_error) { 3348c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3349c29c1dd3SFam Zheng } 3350c29c1dd3SFam Zheng if (!has_on_target_error) { 3351c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3352c29c1dd3SFam Zheng } 3353c29c1dd3SFam Zheng 3354a0e8544cSFam Zheng blk = blk_by_name(device); 3355a0e8544cSFam Zheng if (!blk) { 33565b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", device); 3357c29c1dd3SFam Zheng return; 3358c29c1dd3SFam Zheng } 3359c29c1dd3SFam Zheng 33605433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3361c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3362c29c1dd3SFam Zheng 33635433c24fSMax Reitz if (!blk_is_available(blk)) { 33645433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 33655433c24fSMax Reitz goto out; 33665433c24fSMax Reitz } 33675433c24fSMax Reitz bs = blk_bs(blk); 33685433c24fSMax Reitz 33695433c24fSMax Reitz target_blk = blk_by_name(target); 33705433c24fSMax Reitz if (!target_blk) { 33715b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", target); 3372c29c1dd3SFam Zheng goto out; 3373c29c1dd3SFam Zheng } 33745433c24fSMax Reitz 33755433c24fSMax Reitz if (!blk_is_available(target_blk)) { 33765433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", target); 33775433c24fSMax Reitz goto out; 33785433c24fSMax Reitz } 33795433c24fSMax Reitz target_bs = blk_bs(target_blk); 3380c29c1dd3SFam Zheng 3381c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3382d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 338378f51fdeSJohn Snow on_target_error, block_job_cb, bs, txn, &local_err); 3384c29c1dd3SFam Zheng if (local_err != NULL) { 3385c29c1dd3SFam Zheng error_propagate(errp, local_err); 3386c29c1dd3SFam Zheng } 3387c29c1dd3SFam Zheng out: 3388c29c1dd3SFam Zheng aio_context_release(aio_context); 3389c29c1dd3SFam Zheng } 3390c29c1dd3SFam Zheng 339178f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target, 339278f51fdeSJohn Snow enum MirrorSyncMode sync, 339378f51fdeSJohn Snow bool has_speed, int64_t speed, 339478f51fdeSJohn Snow bool has_on_source_error, 339578f51fdeSJohn Snow BlockdevOnError on_source_error, 339678f51fdeSJohn Snow bool has_on_target_error, 339778f51fdeSJohn Snow BlockdevOnError on_target_error, 339878f51fdeSJohn Snow Error **errp) 339978f51fdeSJohn Snow { 340078f51fdeSJohn Snow do_blockdev_backup(device, target, sync, has_speed, speed, 340178f51fdeSJohn Snow has_on_source_error, on_source_error, 340278f51fdeSJohn Snow has_on_target_error, on_target_error, 340378f51fdeSJohn Snow NULL, errp); 340478f51fdeSJohn Snow } 340578f51fdeSJohn Snow 34064193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 34074193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 34084193cdd7SFam Zheng **/ 34094193cdd7SFam Zheng static void blockdev_mirror_common(BlockDriverState *bs, 34104193cdd7SFam Zheng BlockDriverState *target, 341109158f00SBenoît Canet bool has_replaces, const char *replaces, 3412d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3413b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3414eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 341508e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 34164193cdd7SFam Zheng bool has_on_source_error, 34174193cdd7SFam Zheng BlockdevOnError on_source_error, 34184193cdd7SFam Zheng bool has_on_target_error, 34194193cdd7SFam Zheng BlockdevOnError on_target_error, 34200fc9f8eaSFam Zheng bool has_unmap, bool unmap, 3421b952b558SPaolo Bonzini Error **errp) 3422d9b902dbSPaolo Bonzini { 3423d9b902dbSPaolo Bonzini 3424d9b902dbSPaolo Bonzini if (!has_speed) { 3425d9b902dbSPaolo Bonzini speed = 0; 3426d9b902dbSPaolo Bonzini } 3427b952b558SPaolo Bonzini if (!has_on_source_error) { 3428b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3429b952b558SPaolo Bonzini } 3430b952b558SPaolo Bonzini if (!has_on_target_error) { 3431b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3432b952b558SPaolo Bonzini } 3433eee13dfeSPaolo Bonzini if (!has_granularity) { 3434eee13dfeSPaolo Bonzini granularity = 0; 3435eee13dfeSPaolo Bonzini } 343608e4ed6cSPaolo Bonzini if (!has_buf_size) { 343748ac0a4dSWen Congyang buf_size = 0; 343808e4ed6cSPaolo Bonzini } 34390fc9f8eaSFam Zheng if (!has_unmap) { 34400fc9f8eaSFam Zheng unmap = true; 34410fc9f8eaSFam Zheng } 344208e4ed6cSPaolo Bonzini 3443eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3444c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 34453cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3446eee13dfeSPaolo Bonzini return; 3447eee13dfeSPaolo Bonzini } 3448eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3449c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3450c6bd8c70SMarkus Armbruster "power of 2"); 3451eee13dfeSPaolo Bonzini return; 3452eee13dfeSPaolo Bonzini } 3453d9b902dbSPaolo Bonzini 34544193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 34554193cdd7SFam Zheng return; 34564193cdd7SFam Zheng } 3457e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3458e40e5027SFam Zheng return; 3459e40e5027SFam Zheng } 34604193cdd7SFam Zheng 34614193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 34624193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 34634193cdd7SFam Zheng } 34644193cdd7SFam Zheng 34654193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 34664193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 34674193cdd7SFam Zheng */ 34684193cdd7SFam Zheng mirror_start(bs, target, 34694193cdd7SFam Zheng has_replaces ? replaces : NULL, 34704193cdd7SFam Zheng speed, granularity, buf_size, sync, 34714193cdd7SFam Zheng on_source_error, on_target_error, unmap, 34724193cdd7SFam Zheng block_job_cb, bs, errp); 34734193cdd7SFam Zheng } 34744193cdd7SFam Zheng 34754193cdd7SFam Zheng void qmp_drive_mirror(const char *device, const char *target, 34764193cdd7SFam Zheng bool has_format, const char *format, 34774193cdd7SFam Zheng bool has_node_name, const char *node_name, 34784193cdd7SFam Zheng bool has_replaces, const char *replaces, 34794193cdd7SFam Zheng enum MirrorSyncMode sync, 34804193cdd7SFam Zheng bool has_mode, enum NewImageMode mode, 34814193cdd7SFam Zheng bool has_speed, int64_t speed, 34824193cdd7SFam Zheng bool has_granularity, uint32_t granularity, 34834193cdd7SFam Zheng bool has_buf_size, int64_t buf_size, 34844193cdd7SFam Zheng bool has_on_source_error, BlockdevOnError on_source_error, 34854193cdd7SFam Zheng bool has_on_target_error, BlockdevOnError on_target_error, 34864193cdd7SFam Zheng bool has_unmap, bool unmap, 34874193cdd7SFam Zheng Error **errp) 34884193cdd7SFam Zheng { 34894193cdd7SFam Zheng BlockDriverState *bs; 34904193cdd7SFam Zheng BlockBackend *blk; 34914193cdd7SFam Zheng BlockDriverState *source, *target_bs; 34924193cdd7SFam Zheng AioContext *aio_context; 34934193cdd7SFam Zheng Error *local_err = NULL; 34944193cdd7SFam Zheng QDict *options = NULL; 34954193cdd7SFam Zheng int flags; 34964193cdd7SFam Zheng int64_t size; 34974193cdd7SFam Zheng 3498a0e8544cSFam Zheng blk = blk_by_name(device); 3499a0e8544cSFam Zheng if (!blk) { 350075158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 350175158ebbSMarkus Armbruster "Device '%s' not found", device); 3502d9b902dbSPaolo Bonzini return; 3503d9b902dbSPaolo Bonzini } 3504d9b902dbSPaolo Bonzini 35055433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 35065a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 35075a7e7a0bSStefan Hajnoczi 35085433c24fSMax Reitz if (!blk_is_available(blk)) { 3509c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 35105a7e7a0bSStefan Hajnoczi goto out; 3511d9b902dbSPaolo Bonzini } 35125433c24fSMax Reitz bs = blk_bs(blk); 35134193cdd7SFam Zheng if (!has_mode) { 35144193cdd7SFam Zheng mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 35154193cdd7SFam Zheng } 3516d9b902dbSPaolo Bonzini 3517d9b902dbSPaolo Bonzini if (!has_format) { 3518d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3519d9b902dbSPaolo Bonzini } 3520d9b902dbSPaolo Bonzini 352161de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3522760e0063SKevin Wolf source = backing_bs(bs); 3523d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3524d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 3525d9b902dbSPaolo Bonzini } 3526117e0c82SMax Reitz if (sync == MIRROR_SYNC_MODE_NONE) { 3527117e0c82SMax Reitz source = bs; 3528117e0c82SMax Reitz } 3529d9b902dbSPaolo Bonzini 3530ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3531ac3c5d83SStefan Hajnoczi if (size < 0) { 3532ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 35335a7e7a0bSStefan Hajnoczi goto out; 3534ac3c5d83SStefan Hajnoczi } 3535ac3c5d83SStefan Hajnoczi 353609158f00SBenoît Canet if (has_replaces) { 353709158f00SBenoît Canet BlockDriverState *to_replace_bs; 35385a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 35395a7e7a0bSStefan Hajnoczi int64_t replace_size; 354009158f00SBenoît Canet 354109158f00SBenoît Canet if (!has_node_name) { 354209158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 354309158f00SBenoît Canet " named node of the graph"); 35445a7e7a0bSStefan Hajnoczi goto out; 354509158f00SBenoît Canet } 354609158f00SBenoît Canet 3547e12f3784SWen Congyang to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 354809158f00SBenoît Canet 354909158f00SBenoît Canet if (!to_replace_bs) { 355009158f00SBenoît Canet error_propagate(errp, local_err); 35515a7e7a0bSStefan Hajnoczi goto out; 355209158f00SBenoît Canet } 355309158f00SBenoît Canet 35545a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 35555a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 35565a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 35575a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 35585a7e7a0bSStefan Hajnoczi 35595a7e7a0bSStefan Hajnoczi if (size != replace_size) { 356009158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 356109158f00SBenoît Canet "different size"); 35625a7e7a0bSStefan Hajnoczi goto out; 356309158f00SBenoît Canet } 356409158f00SBenoît Canet } 356509158f00SBenoît Canet 356614526864SMax Reitz if ((sync == MIRROR_SYNC_MODE_FULL || !source) 356714526864SMax Reitz && mode != NEW_IMAGE_MODE_EXISTING) 356814526864SMax Reitz { 3569d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3570e6641719SMax Reitz assert(format); 3571cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3572f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3573d9b902dbSPaolo Bonzini } else { 3574d9b902dbSPaolo Bonzini switch (mode) { 3575d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3576d9b902dbSPaolo Bonzini break; 3577d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3578d9b902dbSPaolo Bonzini /* create new image with backing file */ 3579cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3580d9b902dbSPaolo Bonzini source->filename, 3581d9b902dbSPaolo Bonzini source->drv->format_name, 3582f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3583d9b902dbSPaolo Bonzini break; 3584d9b902dbSPaolo Bonzini default: 3585d9b902dbSPaolo Bonzini abort(); 3586d9b902dbSPaolo Bonzini } 3587d9b902dbSPaolo Bonzini } 3588d9b902dbSPaolo Bonzini 358984d18f06SMarkus Armbruster if (local_err) { 3590cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 35915a7e7a0bSStefan Hajnoczi goto out; 3592d9b902dbSPaolo Bonzini } 3593d9b902dbSPaolo Bonzini 35944c828dc6SBenoît Canet options = qdict_new(); 3595e6641719SMax Reitz if (has_node_name) { 35964c828dc6SBenoît Canet qdict_put(options, "node-name", qstring_from_str(node_name)); 35974c828dc6SBenoît Canet } 3598e6641719SMax Reitz if (format) { 3599e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3600e6641719SMax Reitz } 36014c828dc6SBenoît Canet 3602b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3603b812f671SPaolo Bonzini * file. 3604b812f671SPaolo Bonzini */ 36055b363937SMax Reitz target_bs = bdrv_open(target, NULL, options, flags | BDRV_O_NO_BACKING, 36065b363937SMax Reitz errp); 36075b363937SMax Reitz if (!target_bs) { 36085a7e7a0bSStefan Hajnoczi goto out; 3609d9b902dbSPaolo Bonzini } 3610d9b902dbSPaolo Bonzini 36115a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 36125a7e7a0bSStefan Hajnoczi 36134193cdd7SFam Zheng blockdev_mirror_common(bs, target_bs, 36144193cdd7SFam Zheng has_replaces, replaces, sync, 36154193cdd7SFam Zheng has_speed, speed, 36164193cdd7SFam Zheng has_granularity, granularity, 36174193cdd7SFam Zheng has_buf_size, buf_size, 36184193cdd7SFam Zheng has_on_source_error, on_source_error, 36194193cdd7SFam Zheng has_on_target_error, on_target_error, 36204193cdd7SFam Zheng has_unmap, unmap, 36214193cdd7SFam Zheng &local_err); 3622e253f4b8SKevin Wolf bdrv_unref(target_bs); 36234193cdd7SFam Zheng if (local_err) { 3624d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 3625d9b902dbSPaolo Bonzini } 36265a7e7a0bSStefan Hajnoczi out: 36275a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3628d9b902dbSPaolo Bonzini } 3629d9b902dbSPaolo Bonzini 3630df92562eSFam Zheng void qmp_blockdev_mirror(const char *device, const char *target, 3631df92562eSFam Zheng bool has_replaces, const char *replaces, 3632df92562eSFam Zheng MirrorSyncMode sync, 3633df92562eSFam Zheng bool has_speed, int64_t speed, 3634df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3635df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3636df92562eSFam Zheng bool has_on_source_error, 3637df92562eSFam Zheng BlockdevOnError on_source_error, 3638df92562eSFam Zheng bool has_on_target_error, 3639df92562eSFam Zheng BlockdevOnError on_target_error, 3640df92562eSFam Zheng Error **errp) 3641df92562eSFam Zheng { 3642df92562eSFam Zheng BlockDriverState *bs; 3643df92562eSFam Zheng BlockBackend *blk; 3644df92562eSFam Zheng BlockDriverState *target_bs; 3645df92562eSFam Zheng AioContext *aio_context; 3646df92562eSFam Zheng Error *local_err = NULL; 3647df92562eSFam Zheng 3648df92562eSFam Zheng blk = blk_by_name(device); 3649df92562eSFam Zheng if (!blk) { 3650df92562eSFam Zheng error_setg(errp, "Device '%s' not found", device); 3651df92562eSFam Zheng return; 3652df92562eSFam Zheng } 3653df92562eSFam Zheng bs = blk_bs(blk); 3654df92562eSFam Zheng 3655df92562eSFam Zheng if (!bs) { 3656df92562eSFam Zheng error_setg(errp, "Device '%s' has no media", device); 3657df92562eSFam Zheng return; 3658df92562eSFam Zheng } 3659df92562eSFam Zheng 3660df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3661df92562eSFam Zheng if (!target_bs) { 3662df92562eSFam Zheng return; 3663df92562eSFam Zheng } 3664df92562eSFam Zheng 3665df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3666df92562eSFam Zheng aio_context_acquire(aio_context); 3667df92562eSFam Zheng 3668df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3669df92562eSFam Zheng 3670df92562eSFam Zheng blockdev_mirror_common(bs, target_bs, 3671df92562eSFam Zheng has_replaces, replaces, sync, 3672df92562eSFam Zheng has_speed, speed, 3673df92562eSFam Zheng has_granularity, granularity, 3674df92562eSFam Zheng has_buf_size, buf_size, 3675df92562eSFam Zheng has_on_source_error, on_source_error, 3676df92562eSFam Zheng has_on_target_error, on_target_error, 3677df92562eSFam Zheng true, true, 3678df92562eSFam Zheng &local_err); 3679df92562eSFam Zheng if (local_err) { 3680df92562eSFam Zheng error_propagate(errp, local_err); 3681df92562eSFam Zheng } 3682df92562eSFam Zheng 3683df92562eSFam Zheng aio_context_release(aio_context); 3684df92562eSFam Zheng } 3685df92562eSFam Zheng 36863d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */ 368724d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context, 368824d6bffeSMarkus Armbruster Error **errp) 36892d47c6e9SStefan Hajnoczi { 3690a0e8544cSFam Zheng BlockBackend *blk; 36912d47c6e9SStefan Hajnoczi BlockDriverState *bs; 36922d47c6e9SStefan Hajnoczi 36935433c24fSMax Reitz *aio_context = NULL; 36945433c24fSMax Reitz 3695a0e8544cSFam Zheng blk = blk_by_name(device); 3696a0e8544cSFam Zheng if (!blk) { 36973d948cdfSStefan Hajnoczi goto notfound; 36982d47c6e9SStefan Hajnoczi } 36993d948cdfSStefan Hajnoczi 37005433c24fSMax Reitz *aio_context = blk_get_aio_context(blk); 37013d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 37023d948cdfSStefan Hajnoczi 37035433c24fSMax Reitz if (!blk_is_available(blk)) { 37045433c24fSMax Reitz goto notfound; 37055433c24fSMax Reitz } 37065433c24fSMax Reitz bs = blk_bs(blk); 37075433c24fSMax Reitz 37083d948cdfSStefan Hajnoczi if (!bs->job) { 37093d948cdfSStefan Hajnoczi goto notfound; 37103d948cdfSStefan Hajnoczi } 37113d948cdfSStefan Hajnoczi 37122d47c6e9SStefan Hajnoczi return bs->job; 37133d948cdfSStefan Hajnoczi 37143d948cdfSStefan Hajnoczi notfound: 37152e3a0266SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 37162e3a0266SMarkus Armbruster "No active block job on device '%s'", device); 37175433c24fSMax Reitz if (*aio_context) { 37185433c24fSMax Reitz aio_context_release(*aio_context); 37193d948cdfSStefan Hajnoczi *aio_context = NULL; 37205433c24fSMax Reitz } 37213d948cdfSStefan Hajnoczi return NULL; 37222d47c6e9SStefan Hajnoczi } 37232d47c6e9SStefan Hajnoczi 3724882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 37252d47c6e9SStefan Hajnoczi { 37263d948cdfSStefan Hajnoczi AioContext *aio_context; 372724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37282d47c6e9SStefan Hajnoczi 37292d47c6e9SStefan Hajnoczi if (!job) { 37302d47c6e9SStefan Hajnoczi return; 37312d47c6e9SStefan Hajnoczi } 37322d47c6e9SStefan Hajnoczi 3733882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 37343d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37352d47c6e9SStefan Hajnoczi } 3736370521a1SStefan Hajnoczi 37376e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 37386e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 37396e37fb81SPaolo Bonzini { 37403d948cdfSStefan Hajnoczi AioContext *aio_context; 374124d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37426e37fb81SPaolo Bonzini 37436e37fb81SPaolo Bonzini if (!job) { 37446e37fb81SPaolo Bonzini return; 37456e37fb81SPaolo Bonzini } 37463d948cdfSStefan Hajnoczi 37473d948cdfSStefan Hajnoczi if (!has_force) { 37483d948cdfSStefan Hajnoczi force = false; 37493d948cdfSStefan Hajnoczi } 37503d948cdfSStefan Hajnoczi 3751751ebd76SFam Zheng if (job->user_paused && !force) { 3752f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3753f231b88dSCole Robinson device); 37543d948cdfSStefan Hajnoczi goto out; 37556e37fb81SPaolo Bonzini } 37566e37fb81SPaolo Bonzini 37576e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 37586e37fb81SPaolo Bonzini block_job_cancel(job); 37593d948cdfSStefan Hajnoczi out: 37603d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37616e37fb81SPaolo Bonzini } 37626e37fb81SPaolo Bonzini 37636e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3764370521a1SStefan Hajnoczi { 37653d948cdfSStefan Hajnoczi AioContext *aio_context; 376624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3767370521a1SStefan Hajnoczi 3768751ebd76SFam Zheng if (!job || job->user_paused) { 3769370521a1SStefan Hajnoczi return; 3770370521a1SStefan Hajnoczi } 37716e37fb81SPaolo Bonzini 3772751ebd76SFam Zheng job->user_paused = true; 37736e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 37746e37fb81SPaolo Bonzini block_job_pause(job); 37753d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37766e37fb81SPaolo Bonzini } 37776e37fb81SPaolo Bonzini 37786e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 37796e37fb81SPaolo Bonzini { 37803d948cdfSStefan Hajnoczi AioContext *aio_context; 378124d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37826e37fb81SPaolo Bonzini 3783751ebd76SFam Zheng if (!job || !job->user_paused) { 37848acc72a4SPaolo Bonzini return; 37858acc72a4SPaolo Bonzini } 3786370521a1SStefan Hajnoczi 3787751ebd76SFam Zheng job->user_paused = false; 37886e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 37896e37fb81SPaolo Bonzini block_job_resume(job); 37903d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3791370521a1SStefan Hajnoczi } 3792fb5458cdSStefan Hajnoczi 3793aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3794aeae883bSPaolo Bonzini { 37953d948cdfSStefan Hajnoczi AioContext *aio_context; 379624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3797aeae883bSPaolo Bonzini 3798aeae883bSPaolo Bonzini if (!job) { 3799aeae883bSPaolo Bonzini return; 3800aeae883bSPaolo Bonzini } 3801aeae883bSPaolo Bonzini 3802aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3803aeae883bSPaolo Bonzini block_job_complete(job, errp); 38043d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3805aeae883bSPaolo Bonzini } 3806aeae883bSPaolo Bonzini 3807fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3808fa40e656SJeff Cody const char *image_node_name, 3809fa40e656SJeff Cody const char *backing_file, 3810fa40e656SJeff Cody Error **errp) 3811fa40e656SJeff Cody { 3812a0e8544cSFam Zheng BlockBackend *blk; 3813fa40e656SJeff Cody BlockDriverState *bs = NULL; 3814729962f6SStefan Hajnoczi AioContext *aio_context; 3815fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3816fa40e656SJeff Cody Error *local_err = NULL; 3817fa40e656SJeff Cody bool ro; 3818fa40e656SJeff Cody int open_flags; 3819fa40e656SJeff Cody int ret; 3820fa40e656SJeff Cody 3821a0e8544cSFam Zheng blk = blk_by_name(device); 3822a0e8544cSFam Zheng if (!blk) { 382375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 382475158ebbSMarkus Armbruster "Device '%s' not found", device); 3825fa40e656SJeff Cody return; 3826fa40e656SJeff Cody } 3827fa40e656SJeff Cody 38285433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3829729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3830729962f6SStefan Hajnoczi 38315433c24fSMax Reitz if (!blk_is_available(blk)) { 38325433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 38335433c24fSMax Reitz goto out; 38345433c24fSMax Reitz } 38355433c24fSMax Reitz bs = blk_bs(blk); 38365433c24fSMax Reitz 3837fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3838fa40e656SJeff Cody if (local_err) { 3839fa40e656SJeff Cody error_propagate(errp, local_err); 3840729962f6SStefan Hajnoczi goto out; 3841fa40e656SJeff Cody } 3842fa40e656SJeff Cody 3843fa40e656SJeff Cody if (!image_bs) { 3844fa40e656SJeff Cody error_setg(errp, "image file not found"); 3845729962f6SStefan Hajnoczi goto out; 3846fa40e656SJeff Cody } 3847fa40e656SJeff Cody 3848fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3849fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3850fa40e656SJeff Cody "without a backing file"); 3851729962f6SStefan Hajnoczi goto out; 3852fa40e656SJeff Cody } 3853fa40e656SJeff Cody 3854fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3855fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3856fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3857729962f6SStefan Hajnoczi goto out; 3858fa40e656SJeff Cody } 3859fa40e656SJeff Cody 3860fa40e656SJeff Cody /* final sanity check */ 3861fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3862fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3863fa40e656SJeff Cody device); 3864729962f6SStefan Hajnoczi goto out; 3865fa40e656SJeff Cody } 3866fa40e656SJeff Cody 3867fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3868fa40e656SJeff Cody open_flags = image_bs->open_flags; 3869fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3870fa40e656SJeff Cody 3871fa40e656SJeff Cody if (ro) { 3872fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3873fa40e656SJeff Cody if (local_err) { 3874fa40e656SJeff Cody error_propagate(errp, local_err); 3875729962f6SStefan Hajnoczi goto out; 3876fa40e656SJeff Cody } 3877fa40e656SJeff Cody } 3878fa40e656SJeff Cody 3879fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3880fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3881fa40e656SJeff Cody 3882fa40e656SJeff Cody if (ret < 0) { 3883fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3884fa40e656SJeff Cody backing_file); 3885fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3886fa40e656SJeff Cody * appropriate */ 3887fa40e656SJeff Cody } 3888fa40e656SJeff Cody 3889fa40e656SJeff Cody if (ro) { 3890fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3891fa40e656SJeff Cody if (local_err) { 3892fa40e656SJeff Cody error_propagate(errp, local_err); /* will preserve prior errp */ 3893fa40e656SJeff Cody } 3894fa40e656SJeff Cody } 3895729962f6SStefan Hajnoczi 3896729962f6SStefan Hajnoczi out: 3897729962f6SStefan Hajnoczi aio_context_release(aio_context); 3898fa40e656SJeff Cody } 3899fa40e656SJeff Cody 3900abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 3901abb21ac3SKevin Wolf { 3902abb21ac3SKevin Wolf QemuOpts *opts; 3903abb21ac3SKevin Wolf QDict *qdict; 3904abb21ac3SKevin Wolf Error *local_err = NULL; 3905abb21ac3SKevin Wolf 3906abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 3907abb21ac3SKevin Wolf if (!opts) { 3908abb21ac3SKevin Wolf return; 3909abb21ac3SKevin Wolf } 3910abb21ac3SKevin Wolf 3911abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 3912abb21ac3SKevin Wolf 3913abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 3914f8746fb8SKevin Wolf QDECREF(qdict); 3915abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 3916abb21ac3SKevin Wolf goto out; 3917abb21ac3SKevin Wolf } 3918abb21ac3SKevin Wolf 3919abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 3920abb21ac3SKevin Wolf if (!bs) { 3921abb21ac3SKevin Wolf error_report_err(local_err); 3922abb21ac3SKevin Wolf goto out; 3923abb21ac3SKevin Wolf } 3924abb21ac3SKevin Wolf 3925abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3926abb21ac3SKevin Wolf 3927abb21ac3SKevin Wolf out: 3928abb21ac3SKevin Wolf qemu_opts_del(opts); 3929abb21ac3SKevin Wolf } 3930abb21ac3SKevin Wolf 3931d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3932d26c9a15SKevin Wolf { 3933d26c9a15SKevin Wolf QmpOutputVisitor *ov = qmp_output_visitor_new(); 3934be4b67bcSMax Reitz BlockDriverState *bs; 3935be4b67bcSMax Reitz BlockBackend *blk = NULL; 3936d26c9a15SKevin Wolf QObject *obj; 3937d26c9a15SKevin Wolf QDict *qdict; 3938d26c9a15SKevin Wolf Error *local_err = NULL; 3939d26c9a15SKevin Wolf 394060e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3941d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 394260e19e06SMarkus Armbruster * when called from drive_new(). 3943d26c9a15SKevin Wolf * 3944d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3945d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3946c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3947c6e0bd9bSKevin Wolf options->cache->has_direct && 3948c6e0bd9bSKevin Wolf options->cache->direct; 3949c6e0bd9bSKevin Wolf if (!direct) { 3950d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3951d26c9a15SKevin Wolf goto fail; 3952d26c9a15SKevin Wolf } 3953d26c9a15SKevin Wolf } 3954d26c9a15SKevin Wolf 395551e72bc1SEric Blake visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options, 395651e72bc1SEric Blake &local_err); 395784d18f06SMarkus Armbruster if (local_err) { 3958d26c9a15SKevin Wolf error_propagate(errp, local_err); 3959d26c9a15SKevin Wolf goto fail; 3960d26c9a15SKevin Wolf } 3961d26c9a15SKevin Wolf 3962d26c9a15SKevin Wolf obj = qmp_output_get_qobject(ov); 3963d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3964d26c9a15SKevin Wolf 3965d26c9a15SKevin Wolf qdict_flatten(qdict); 3966d26c9a15SKevin Wolf 3967be4b67bcSMax Reitz if (options->has_id) { 396818e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 396984d18f06SMarkus Armbruster if (local_err) { 3970b681072dSKevin Wolf error_propagate(errp, local_err); 3971d26c9a15SKevin Wolf goto fail; 3972d26c9a15SKevin Wolf } 3973d26c9a15SKevin Wolf 3974be4b67bcSMax Reitz bs = blk_bs(blk); 3975be4b67bcSMax Reitz } else { 3976be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3977be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3978be4b67bcSMax Reitz "the root node"); 3979be4b67bcSMax Reitz goto fail; 3980be4b67bcSMax Reitz } 3981be4b67bcSMax Reitz 3982bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3983bd745e23SMax Reitz if (!bs) { 3984be4b67bcSMax Reitz goto fail; 3985be4b67bcSMax Reitz } 39869c4218e9SMax Reitz 39879c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3988be4b67bcSMax Reitz } 3989be4b67bcSMax Reitz 3990be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 3991be4b67bcSMax Reitz if (blk) { 3992efaa7c4eSMax Reitz monitor_remove_blk(blk); 399318e46a03SMarkus Armbruster blk_unref(blk); 3994be4b67bcSMax Reitz } else { 39959c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 3996be4b67bcSMax Reitz bdrv_unref(bs); 3997be4b67bcSMax Reitz } 39988ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 39998ae8e904SKevin Wolf goto fail; 40008ae8e904SKevin Wolf } 40018ae8e904SKevin Wolf 4002d26c9a15SKevin Wolf fail: 4003d26c9a15SKevin Wolf qmp_output_visitor_cleanup(ov); 4004d26c9a15SKevin Wolf } 4005d26c9a15SKevin Wolf 400681b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id, 400781b936aeSAlberto Garcia bool has_node_name, const char *node_name, Error **errp) 400881b936aeSAlberto Garcia { 400981b936aeSAlberto Garcia AioContext *aio_context; 401081b936aeSAlberto Garcia BlockBackend *blk; 401181b936aeSAlberto Garcia BlockDriverState *bs; 401281b936aeSAlberto Garcia 401381b936aeSAlberto Garcia if (has_id && has_node_name) { 401481b936aeSAlberto Garcia error_setg(errp, "Only one of id and node-name must be specified"); 401581b936aeSAlberto Garcia return; 401681b936aeSAlberto Garcia } else if (!has_id && !has_node_name) { 401781b936aeSAlberto Garcia error_setg(errp, "No block device specified"); 401881b936aeSAlberto Garcia return; 401981b936aeSAlberto Garcia } 402081b936aeSAlberto Garcia 402181b936aeSAlberto Garcia if (has_id) { 4022efaa7c4eSMax Reitz /* blk_by_name() never returns a BB that is not owned by the monitor */ 402381b936aeSAlberto Garcia blk = blk_by_name(id); 402481b936aeSAlberto Garcia if (!blk) { 402581b936aeSAlberto Garcia error_setg(errp, "Cannot find block backend %s", id); 402681b936aeSAlberto Garcia return; 402781b936aeSAlberto Garcia } 40285cf87fd6SPaolo Bonzini if (blk_legacy_dinfo(blk)) { 40295cf87fd6SPaolo Bonzini error_setg(errp, "Deleting block backend added with drive-add" 40305cf87fd6SPaolo Bonzini " is not supported"); 40315cf87fd6SPaolo Bonzini return; 40325cf87fd6SPaolo Bonzini } 403381b936aeSAlberto Garcia if (blk_get_refcnt(blk) > 1) { 403481b936aeSAlberto Garcia error_setg(errp, "Block backend %s is in use", id); 403581b936aeSAlberto Garcia return; 403681b936aeSAlberto Garcia } 403781b936aeSAlberto Garcia bs = blk_bs(blk); 403881b936aeSAlberto Garcia aio_context = blk_get_aio_context(blk); 403981b936aeSAlberto Garcia } else { 40401f0c461bSKevin Wolf blk = NULL; 404181b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 404281b936aeSAlberto Garcia if (!bs) { 404381b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 404481b936aeSAlberto Garcia return; 404581b936aeSAlberto Garcia } 40461f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 404781b936aeSAlberto Garcia error_setg(errp, "Node %s is in use by %s", 40481f0c461bSKevin Wolf node_name, bdrv_get_parent_name(bs)); 404981b936aeSAlberto Garcia return; 405081b936aeSAlberto Garcia } 405181b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 405281b936aeSAlberto Garcia } 405381b936aeSAlberto Garcia 405481b936aeSAlberto Garcia aio_context_acquire(aio_context); 405581b936aeSAlberto Garcia 405681b936aeSAlberto Garcia if (bs) { 405781b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 405881b936aeSAlberto Garcia goto out; 405981b936aeSAlberto Garcia } 406081b936aeSAlberto Garcia 40619c4218e9SMax Reitz if (!blk && !bs->monitor_list.tqe_prev) { 40629c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 40639c4218e9SMax Reitz bs->node_name); 40649c4218e9SMax Reitz goto out; 40659c4218e9SMax Reitz } 40669c4218e9SMax Reitz 40679c4218e9SMax Reitz if (bs->refcnt > 1) { 406881b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 406981b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 407081b936aeSAlberto Garcia goto out; 407181b936aeSAlberto Garcia } 407281b936aeSAlberto Garcia } 407381b936aeSAlberto Garcia 407481b936aeSAlberto Garcia if (blk) { 4075efaa7c4eSMax Reitz monitor_remove_blk(blk); 407681b936aeSAlberto Garcia blk_unref(blk); 407781b936aeSAlberto Garcia } else { 40789c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 407981b936aeSAlberto Garcia bdrv_unref(bs); 408081b936aeSAlberto Garcia } 408181b936aeSAlberto Garcia 408281b936aeSAlberto Garcia out: 408381b936aeSAlberto Garcia aio_context_release(aio_context); 408481b936aeSAlberto Garcia } 408581b936aeSAlberto Garcia 40867f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 40877f821597SWen Congyang const char *child_name) 40887f821597SWen Congyang { 40897f821597SWen Congyang BdrvChild *child; 40907f821597SWen Congyang 40917f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 40927f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 40937f821597SWen Congyang return child; 40947f821597SWen Congyang } 40957f821597SWen Congyang } 40967f821597SWen Congyang 40977f821597SWen Congyang return NULL; 40987f821597SWen Congyang } 40997f821597SWen Congyang 41007f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 41017f821597SWen Congyang const char *child, bool has_node, 41027f821597SWen Congyang const char *node, Error **errp) 41037f821597SWen Congyang { 41047f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 41057f821597SWen Congyang BdrvChild *p_child; 41067f821597SWen Congyang 41077f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 41087f821597SWen Congyang if (!parent_bs) { 41097f821597SWen Congyang return; 41107f821597SWen Congyang } 41117f821597SWen Congyang 41127f821597SWen Congyang if (has_child == has_node) { 41137f821597SWen Congyang if (has_child) { 41147f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 41157f821597SWen Congyang } else { 41167f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 41177f821597SWen Congyang } 41187f821597SWen Congyang return; 41197f821597SWen Congyang } 41207f821597SWen Congyang 41217f821597SWen Congyang if (has_child) { 41227f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 41237f821597SWen Congyang if (!p_child) { 41247f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 41257f821597SWen Congyang parent, child); 41267f821597SWen Congyang return; 41277f821597SWen Congyang } 41287f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 41297f821597SWen Congyang } 41307f821597SWen Congyang 41317f821597SWen Congyang if (has_node) { 41327f821597SWen Congyang new_bs = bdrv_find_node(node); 41337f821597SWen Congyang if (!new_bs) { 41347f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 41357f821597SWen Congyang return; 41367f821597SWen Congyang } 41377f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 41387f821597SWen Congyang } 41397f821597SWen Congyang } 41407f821597SWen Congyang 4141fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4142fb5458cdSStefan Hajnoczi { 4143fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4144fea68bb6SMarkus Armbruster BlockDriverState *bs; 414588be7b4bSKevin Wolf BdrvNextIterator it; 4146fea68bb6SMarkus Armbruster 414788be7b4bSKevin Wolf for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { 414869691e72SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 414969691e72SStefan Hajnoczi 415069691e72SStefan Hajnoczi aio_context_acquire(aio_context); 415169691e72SStefan Hajnoczi 4152fea68bb6SMarkus Armbruster if (bs->job) { 4153fea68bb6SMarkus Armbruster BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 4154fea68bb6SMarkus Armbruster elem->value = block_job_query(bs->job); 4155fea68bb6SMarkus Armbruster *p_next = elem; 4156fea68bb6SMarkus Armbruster p_next = &elem->next; 4157fea68bb6SMarkus Armbruster } 415869691e72SStefan Hajnoczi 415969691e72SStefan Hajnoczi aio_context_release(aio_context); 4160fea68bb6SMarkus Armbruster } 4161fea68bb6SMarkus Armbruster 4162fea68bb6SMarkus Armbruster return head; 4163fb5458cdSStefan Hajnoczi } 41644d454574SPaolo Bonzini 41650006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 41664d454574SPaolo Bonzini .name = "drive", 41670006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 41684d454574SPaolo Bonzini .desc = { 41694d454574SPaolo Bonzini { 41704d454574SPaolo Bonzini .name = "snapshot", 41714d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 41724d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 41734d454574SPaolo Bonzini },{ 4174a9384affSPaolo Bonzini .name = "discard", 4175a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 4176a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 4177a9384affSPaolo Bonzini },{ 41784d454574SPaolo Bonzini .name = "aio", 41794d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41804d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 41814d454574SPaolo Bonzini },{ 4182e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4183e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4184e4b24b49SKevin Wolf .help = "Enable writeback mode", 4185e4b24b49SKevin Wolf },{ 41864d454574SPaolo Bonzini .name = "format", 41874d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41884d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 41894d454574SPaolo Bonzini },{ 41904d454574SPaolo Bonzini .name = "rerror", 41914d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41924d454574SPaolo Bonzini .help = "read error action", 41934d454574SPaolo Bonzini },{ 41944d454574SPaolo Bonzini .name = "werror", 41954d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41964d454574SPaolo Bonzini .help = "write error action", 41974d454574SPaolo Bonzini },{ 41980f227a94SKevin Wolf .name = "read-only", 41994d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 42004d454574SPaolo Bonzini .help = "open drive file as read-only", 42014d454574SPaolo Bonzini },{ 420257975222SKevin Wolf .name = "throttling.iops-total", 42034d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42044d454574SPaolo Bonzini .help = "limit total I/O operations per second", 42054d454574SPaolo Bonzini },{ 420657975222SKevin Wolf .name = "throttling.iops-read", 42074d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42084d454574SPaolo Bonzini .help = "limit read operations per second", 42094d454574SPaolo Bonzini },{ 421057975222SKevin Wolf .name = "throttling.iops-write", 42114d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42124d454574SPaolo Bonzini .help = "limit write operations per second", 42134d454574SPaolo Bonzini },{ 421457975222SKevin Wolf .name = "throttling.bps-total", 42154d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42164d454574SPaolo Bonzini .help = "limit total bytes per second", 42174d454574SPaolo Bonzini },{ 421857975222SKevin Wolf .name = "throttling.bps-read", 42194d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42204d454574SPaolo Bonzini .help = "limit read bytes per second", 42214d454574SPaolo Bonzini },{ 422257975222SKevin Wolf .name = "throttling.bps-write", 42234d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42244d454574SPaolo Bonzini .help = "limit write bytes per second", 42254d454574SPaolo Bonzini },{ 42263e9fab69SBenoît Canet .name = "throttling.iops-total-max", 42273e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42283e9fab69SBenoît Canet .help = "I/O operations burst", 42293e9fab69SBenoît Canet },{ 42303e9fab69SBenoît Canet .name = "throttling.iops-read-max", 42313e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42323e9fab69SBenoît Canet .help = "I/O operations read burst", 42333e9fab69SBenoît Canet },{ 42343e9fab69SBenoît Canet .name = "throttling.iops-write-max", 42353e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42363e9fab69SBenoît Canet .help = "I/O operations write burst", 42373e9fab69SBenoît Canet },{ 42383e9fab69SBenoît Canet .name = "throttling.bps-total-max", 42393e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42403e9fab69SBenoît Canet .help = "total bytes burst", 42413e9fab69SBenoît Canet },{ 42423e9fab69SBenoît Canet .name = "throttling.bps-read-max", 42433e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42443e9fab69SBenoît Canet .help = "total bytes read burst", 42453e9fab69SBenoît Canet },{ 42463e9fab69SBenoît Canet .name = "throttling.bps-write-max", 42473e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42483e9fab69SBenoît Canet .help = "total bytes write burst", 42493e9fab69SBenoît Canet },{ 42508a0fc18dSAlberto Garcia .name = "throttling.iops-total-max-length", 42518a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42528a0fc18dSAlberto Garcia .help = "length of the iops-total-max burst period, in seconds", 42538a0fc18dSAlberto Garcia },{ 42548a0fc18dSAlberto Garcia .name = "throttling.iops-read-max-length", 42558a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42568a0fc18dSAlberto Garcia .help = "length of the iops-read-max burst period, in seconds", 42578a0fc18dSAlberto Garcia },{ 42588a0fc18dSAlberto Garcia .name = "throttling.iops-write-max-length", 42598a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42608a0fc18dSAlberto Garcia .help = "length of the iops-write-max burst period, in seconds", 42618a0fc18dSAlberto Garcia },{ 42628a0fc18dSAlberto Garcia .name = "throttling.bps-total-max-length", 42638a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42648a0fc18dSAlberto Garcia .help = "length of the bps-total-max burst period, in seconds", 42658a0fc18dSAlberto Garcia },{ 42668a0fc18dSAlberto Garcia .name = "throttling.bps-read-max-length", 42678a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42688a0fc18dSAlberto Garcia .help = "length of the bps-read-max burst period, in seconds", 42698a0fc18dSAlberto Garcia },{ 42708a0fc18dSAlberto Garcia .name = "throttling.bps-write-max-length", 42718a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42728a0fc18dSAlberto Garcia .help = "length of the bps-write-max burst period, in seconds", 42738a0fc18dSAlberto Garcia },{ 42742024c1dfSBenoît Canet .name = "throttling.iops-size", 42752024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 42762024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 42772024c1dfSBenoît Canet },{ 427876f4afb4SAlberto Garcia .name = "throttling.group", 427976f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 428076f4afb4SAlberto Garcia .help = "name of the block throttling group", 428176f4afb4SAlberto Garcia },{ 42824d454574SPaolo Bonzini .name = "copy-on-read", 42834d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 42844d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4285465bee1dSPeter Lieven },{ 4286465bee1dSPeter Lieven .name = "detect-zeroes", 4287465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4288465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4289362e9299SAlberto Garcia },{ 4290362e9299SAlberto Garcia .name = "stats-account-invalid", 4291362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4292362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4293362e9299SAlberto Garcia "in the statistics", 4294362e9299SAlberto Garcia },{ 4295362e9299SAlberto Garcia .name = "stats-account-failed", 4296362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4297362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4298362e9299SAlberto Garcia "in the statistics", 42994d454574SPaolo Bonzini }, 43004d454574SPaolo Bonzini { /* end of list */ } 43014d454574SPaolo Bonzini }, 43024d454574SPaolo Bonzini }; 43030006383eSKevin Wolf 4304bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 4305bd745e23SMax Reitz .name = "root-bds", 430623f7fcb2SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head), 4307bd745e23SMax Reitz .desc = { 4308bd745e23SMax Reitz { 4309bd745e23SMax Reitz .name = "discard", 4310bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4311bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 4312bd745e23SMax Reitz },{ 4313bd745e23SMax Reitz .name = "aio", 4314bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4315bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 4316bd745e23SMax Reitz },{ 4317bd745e23SMax Reitz .name = "read-only", 4318bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4319bd745e23SMax Reitz .help = "open drive file as read-only", 4320bd745e23SMax Reitz },{ 4321bd745e23SMax Reitz .name = "copy-on-read", 4322bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4323bd745e23SMax Reitz .help = "copy read data from backing file into image file", 4324bd745e23SMax Reitz },{ 4325bd745e23SMax Reitz .name = "detect-zeroes", 4326bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4327bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 4328bd745e23SMax Reitz }, 4329bd745e23SMax Reitz { /* end of list */ } 4330bd745e23SMax Reitz }, 4331bd745e23SMax Reitz }; 4332bd745e23SMax Reitz 43330006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 43340006383eSKevin Wolf .name = "drive", 43350006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 43360006383eSKevin Wolf .desc = { 4337492fdc6fSKevin Wolf /* 4338492fdc6fSKevin Wolf * no elements => accept any params 4339492fdc6fSKevin Wolf * validation will happen later 4340492fdc6fSKevin Wolf */ 43410006383eSKevin Wolf { /* end of list */ } 43420006383eSKevin Wolf }, 43430006383eSKevin Wolf }; 4344