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 59*bf18bee5SColin Lord static int do_open_tray(const char *device, bool force, Error **errp); 60*bf18bee5SColin Lord 611960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 621960966dSMarkus Armbruster [IF_NONE] = "none", 631960966dSMarkus Armbruster [IF_IDE] = "ide", 641960966dSMarkus Armbruster [IF_SCSI] = "scsi", 651960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 661960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 671960966dSMarkus Armbruster [IF_MTD] = "mtd", 681960966dSMarkus Armbruster [IF_SD] = "sd", 691960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 701960966dSMarkus Armbruster [IF_XEN] = "xen", 711960966dSMarkus Armbruster }; 721960966dSMarkus Armbruster 7321dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = { 7427d6bf40SMarkus Armbruster /* 7527d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 7627d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 7727d6bf40SMarkus Armbruster * 78547cb157SWei Jiangang * All controllers used to implement if=T drives need to support 7927d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 8027d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 8127d6bf40SMarkus Armbruster * values. 8227d6bf40SMarkus Armbruster * 8327d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 8427d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 8527d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 8627d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 8727d6bf40SMarkus Armbruster */ 8827d6bf40SMarkus Armbruster [IF_IDE] = 2, 8927d6bf40SMarkus Armbruster [IF_SCSI] = 7, 901960966dSMarkus Armbruster }; 911960966dSMarkus Armbruster 9221dff8cfSJohn Snow /** 9321dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides 9421dff8cfSJohn Snow * of the default, global values. 9521dff8cfSJohn Snow */ 9621dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs) 9721dff8cfSJohn Snow { 9818e46a03SMarkus Armbruster BlockBackend *blk; 9921dff8cfSJohn Snow DriveInfo *dinfo; 10021dff8cfSJohn Snow 10121dff8cfSJohn Snow if (max_devs <= 0) { 10221dff8cfSJohn Snow return; 10321dff8cfSJohn Snow } 10421dff8cfSJohn Snow 10518e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 10618e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 10721dff8cfSJohn Snow if (dinfo->type == type) { 10821dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of" 10921dff8cfSJohn Snow " the %s interface, because a drive of that type has" 11021dff8cfSJohn Snow " already been added.\n", if_name[type]); 11121dff8cfSJohn Snow g_assert_not_reached(); 11221dff8cfSJohn Snow } 11321dff8cfSJohn Snow } 11421dff8cfSJohn Snow 11521dff8cfSJohn Snow if_max_devs[type] = max_devs; 11621dff8cfSJohn Snow } 11721dff8cfSJohn Snow 11814bafc54SMarkus Armbruster /* 11914bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 12014bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 12114bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 12214bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 12314bafc54SMarkus Armbruster * when deletion is actually safe. 12414bafc54SMarkus Armbruster */ 1254be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk) 12614bafc54SMarkus Armbruster { 12718e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 1284be74634SMarkus Armbruster BlockDriverState *bs = blk_bs(blk); 12991fddb0dSStefan Hajnoczi AioContext *aio_context; 13014bafc54SMarkus Armbruster 13126f8b3a8SMarkus Armbruster if (!dinfo) { 1322d246f01SKevin Wolf return; 1332d246f01SKevin Wolf } 1342d246f01SKevin Wolf 1355433c24fSMax Reitz if (bs) { 13691fddb0dSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 13791fddb0dSStefan Hajnoczi aio_context_acquire(aio_context); 13891fddb0dSStefan Hajnoczi 13912bde0eeSPaolo Bonzini if (bs->job) { 14012bde0eeSPaolo Bonzini block_job_cancel(bs->job); 14112bde0eeSPaolo Bonzini } 14291fddb0dSStefan Hajnoczi 14391fddb0dSStefan Hajnoczi aio_context_release(aio_context); 1445433c24fSMax Reitz } 14591fddb0dSStefan Hajnoczi 14614bafc54SMarkus Armbruster dinfo->auto_del = 1; 14714bafc54SMarkus Armbruster } 14814bafc54SMarkus Armbruster 1494be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk) 15014bafc54SMarkus Armbruster { 15118e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 15214bafc54SMarkus Armbruster 1530fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 154efaa7c4eSMax Reitz monitor_remove_blk(blk); 155b9fe8a7aSMarkus Armbruster blk_unref(blk); 15614bafc54SMarkus Armbruster } 15714bafc54SMarkus Armbruster } 15814bafc54SMarkus Armbruster 159d8f94e1bSJohn Snow /** 160d8f94e1bSJohn Snow * Returns the current mapping of how many units per bus 161d8f94e1bSJohn Snow * a particular interface can support. 162d8f94e1bSJohn Snow * 163d8f94e1bSJohn Snow * A positive integer indicates n units per bus. 164d8f94e1bSJohn Snow * 0 implies the mapping has not been established. 165d8f94e1bSJohn Snow * -1 indicates an invalid BlockInterfaceType was given. 166d8f94e1bSJohn Snow */ 167d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type) 168d8f94e1bSJohn Snow { 169d8f94e1bSJohn Snow if (type >= IF_IDE && type < IF_COUNT) { 170d8f94e1bSJohn Snow return if_max_devs[type]; 171d8f94e1bSJohn Snow } 172d8f94e1bSJohn Snow 173d8f94e1bSJohn Snow return -1; 174d8f94e1bSJohn Snow } 175d8f94e1bSJohn Snow 176505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 177505a7fb1SMarkus Armbruster { 178505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 179505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 180505a7fb1SMarkus Armbruster } 181505a7fb1SMarkus Armbruster 182505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 183505a7fb1SMarkus Armbruster { 184505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 185505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 186505a7fb1SMarkus Armbruster } 187505a7fb1SMarkus Armbruster 1882292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 1892292ddaeSMarkus Armbruster { 19070b94331SMarkus Armbruster return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 1912292ddaeSMarkus Armbruster } 1922292ddaeSMarkus Armbruster 1932292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 1945645b0f4SMarkus Armbruster const char *optstr) 195666daa68SMarkus Armbruster { 196666daa68SMarkus Armbruster QemuOpts *opts; 197666daa68SMarkus Armbruster 1982292ddaeSMarkus Armbruster opts = drive_def(optstr); 199666daa68SMarkus Armbruster if (!opts) { 200666daa68SMarkus Armbruster return NULL; 201666daa68SMarkus Armbruster } 2022292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 203f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort); 2042292ddaeSMarkus Armbruster } 2052292ddaeSMarkus Armbruster if (index >= 0) { 206a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort); 2072292ddaeSMarkus Armbruster } 208666daa68SMarkus Armbruster if (file) 209f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort); 210666daa68SMarkus Armbruster return opts; 211666daa68SMarkus Armbruster } 212666daa68SMarkus Armbruster 213666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 214666daa68SMarkus Armbruster { 21518e46a03SMarkus Armbruster BlockBackend *blk; 216666daa68SMarkus Armbruster DriveInfo *dinfo; 217666daa68SMarkus Armbruster 21818e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 21918e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 22018e46a03SMarkus Armbruster if (dinfo && dinfo->type == type 22118e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) { 222666daa68SMarkus Armbruster return dinfo; 223666daa68SMarkus Armbruster } 22418e46a03SMarkus Armbruster } 225666daa68SMarkus Armbruster 226666daa68SMarkus Armbruster return NULL; 227666daa68SMarkus Armbruster } 228666daa68SMarkus Armbruster 229a66c9dc7SJohn Snow bool drive_check_orphaned(void) 230a66c9dc7SJohn Snow { 23118e46a03SMarkus Armbruster BlockBackend *blk; 232a66c9dc7SJohn Snow DriveInfo *dinfo; 233a66c9dc7SJohn Snow bool rs = false; 234a66c9dc7SJohn Snow 23518e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 23618e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 237a66c9dc7SJohn Snow /* If dinfo->bdrv->dev is NULL, it has no device attached. */ 238a66c9dc7SJohn Snow /* Unless this is a default drive, this may be an oversight. */ 239a7f53e26SMarkus Armbruster if (!blk_get_attached_dev(blk) && !dinfo->is_default && 240a66c9dc7SJohn Snow dinfo->type != IF_NONE) { 241a66c9dc7SJohn Snow fprintf(stderr, "Warning: Orphaned drive without device: " 242a66c9dc7SJohn Snow "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", 2435433c24fSMax Reitz blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "", 2445433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit); 245a66c9dc7SJohn Snow rs = true; 246a66c9dc7SJohn Snow } 247a66c9dc7SJohn Snow } 248a66c9dc7SJohn Snow 249a66c9dc7SJohn Snow return rs; 250a66c9dc7SJohn Snow } 251a66c9dc7SJohn Snow 252f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 253f1bd51acSMarkus Armbruster { 254f1bd51acSMarkus Armbruster return drive_get(type, 255f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 256f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 257f1bd51acSMarkus Armbruster } 258f1bd51acSMarkus Armbruster 259666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 260666daa68SMarkus Armbruster { 261666daa68SMarkus Armbruster int max_bus; 26218e46a03SMarkus Armbruster BlockBackend *blk; 263666daa68SMarkus Armbruster DriveInfo *dinfo; 264666daa68SMarkus Armbruster 265666daa68SMarkus Armbruster max_bus = -1; 26618e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 26718e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 26818e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 269666daa68SMarkus Armbruster max_bus = dinfo->bus; 270666daa68SMarkus Armbruster } 27118e46a03SMarkus Armbruster } 272666daa68SMarkus Armbruster return max_bus; 273666daa68SMarkus Armbruster } 274666daa68SMarkus Armbruster 27513839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 27613839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 27713839974SMarkus Armbruster appropriate bus. */ 27813839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 27913839974SMarkus Armbruster { 28013839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 28113839974SMarkus Armbruster 28213839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 28313839974SMarkus Armbruster } 28413839974SMarkus Armbruster 285666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 286666daa68SMarkus Armbruster { 287807105a7SMarkus Armbruster error_printf(" %s", name); 288666daa68SMarkus Armbruster } 289666daa68SMarkus Armbruster 290aa398a5cSStefan Hajnoczi typedef struct { 291aa398a5cSStefan Hajnoczi QEMUBH *bh; 292fa510ebfSFam Zheng BlockDriverState *bs; 293fa510ebfSFam Zheng } BDRVPutRefBH; 294aa398a5cSStefan Hajnoczi 295b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 296666daa68SMarkus Armbruster { 297666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 29892aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 299666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 30092aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 301666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 30292aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 303666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 30492aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 305666daa68SMarkus Armbruster } else { 306b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 307666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 308666daa68SMarkus Armbruster return -1; 309666daa68SMarkus Armbruster } 310666daa68SMarkus Armbruster } 311666daa68SMarkus Armbruster 31240119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 31340119effSAlberto Garcia Error **errp) 31440119effSAlberto Garcia { 31540119effSAlberto Garcia const QListEntry *entry; 31640119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 31740119effSAlberto Garcia switch (qobject_type(entry->value)) { 31840119effSAlberto Garcia 31940119effSAlberto Garcia case QTYPE_QSTRING: { 32040119effSAlberto Garcia unsigned long long length; 32140119effSAlberto Garcia const char *str = qstring_get_str(qobject_to_qstring(entry->value)); 32240119effSAlberto Garcia if (parse_uint_full(str, &length, 10) == 0 && 32340119effSAlberto Garcia length > 0 && length <= UINT_MAX) { 32440119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 32540119effSAlberto Garcia } else { 32640119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str); 32740119effSAlberto Garcia return false; 32840119effSAlberto Garcia } 32940119effSAlberto Garcia break; 33040119effSAlberto Garcia } 33140119effSAlberto Garcia 33240119effSAlberto Garcia case QTYPE_QINT: { 33340119effSAlberto Garcia int64_t length = qint_get_int(qobject_to_qint(entry->value)); 33440119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) { 33540119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 33640119effSAlberto Garcia } else { 33740119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length); 33840119effSAlberto Garcia return false; 33940119effSAlberto Garcia } 34040119effSAlberto Garcia break; 34140119effSAlberto Garcia } 34240119effSAlberto Garcia 34340119effSAlberto Garcia default: 34440119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid"); 34540119effSAlberto Garcia return false; 34640119effSAlberto Garcia } 34740119effSAlberto Garcia } 34840119effSAlberto Garcia return true; 34940119effSAlberto Garcia } 35040119effSAlberto Garcia 35133cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 35233cb7dc8SKevin Wolf 353fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 354fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 355fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 356fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 357fbf8175eSMax Reitz { 358fbf8175eSMax Reitz const char *discard; 359fbf8175eSMax Reitz Error *local_error = NULL; 360fbf8175eSMax Reitz const char *aio; 361fbf8175eSMax Reitz 362fbf8175eSMax Reitz if (bdrv_flags) { 363fbf8175eSMax Reitz if (!qemu_opt_get_bool(opts, "read-only", false)) { 364fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_RDWR; 365fbf8175eSMax Reitz } 366fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 367fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 368fbf8175eSMax Reitz } 369fbf8175eSMax Reitz 370fbf8175eSMax Reitz if ((discard = qemu_opt_get(opts, "discard")) != NULL) { 371fbf8175eSMax Reitz if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { 372fbf8175eSMax Reitz error_setg(errp, "Invalid discard option"); 373fbf8175eSMax Reitz return; 374fbf8175eSMax Reitz } 375fbf8175eSMax Reitz } 376fbf8175eSMax Reitz 377fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 378fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 379fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 380fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 381fbf8175eSMax Reitz /* this is the default */ 382fbf8175eSMax Reitz } else { 383fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 384fbf8175eSMax Reitz return; 385fbf8175eSMax Reitz } 386fbf8175eSMax Reitz } 387fbf8175eSMax Reitz } 388fbf8175eSMax Reitz 389fbf8175eSMax Reitz /* disk I/O throttling */ 390fbf8175eSMax Reitz if (throttling_group) { 391fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 392fbf8175eSMax Reitz } 393fbf8175eSMax Reitz 394fbf8175eSMax Reitz if (throttle_cfg) { 3951588ab5dSAlberto Garcia throttle_config_init(throttle_cfg); 396fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 397fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 398fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 399fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 400fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 401fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 402fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 403fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 404fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 405fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 406fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 407fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 408fbf8175eSMax Reitz 409fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 410fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 411fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 412fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 413fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 414fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 415fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 416fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 417fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 418fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 419fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 420fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 421fbf8175eSMax Reitz 4228a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = 4238a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); 4248a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = 4258a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); 4268a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = 4278a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); 4288a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = 4298a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); 4308a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = 4318a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); 4328a0fc18dSAlberto Garcia throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = 4338a0fc18dSAlberto Garcia qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); 4348a0fc18dSAlberto Garcia 435fbf8175eSMax Reitz throttle_cfg->op_size = 436fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 437fbf8175eSMax Reitz 438d5851089SAlberto Garcia if (!throttle_is_valid(throttle_cfg, errp)) { 439fbf8175eSMax Reitz return; 440fbf8175eSMax Reitz } 441fbf8175eSMax Reitz } 442fbf8175eSMax Reitz 443fbf8175eSMax Reitz if (detect_zeroes) { 444fbf8175eSMax Reitz *detect_zeroes = 445fbf8175eSMax Reitz qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, 446fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 4477fb1cf16SEric Blake BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX, 448fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 449fbf8175eSMax Reitz &local_error); 450fbf8175eSMax Reitz if (local_error) { 451fbf8175eSMax Reitz error_propagate(errp, local_error); 452fbf8175eSMax Reitz return; 453fbf8175eSMax Reitz } 454fbf8175eSMax Reitz 455fbf8175eSMax Reitz if (bdrv_flags && 456fbf8175eSMax Reitz *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 457fbf8175eSMax Reitz !(*bdrv_flags & BDRV_O_UNMAP)) 458fbf8175eSMax Reitz { 459fbf8175eSMax Reitz error_setg(errp, "setting detect-zeroes to unmap is not allowed " 460fbf8175eSMax Reitz "without setting discard operation to unmap"); 461fbf8175eSMax Reitz return; 462fbf8175eSMax Reitz } 463fbf8175eSMax Reitz } 464fbf8175eSMax Reitz } 465fbf8175eSMax Reitz 466f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 46718e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 468b681072dSKevin Wolf Error **errp) 469666daa68SMarkus Armbruster { 470666daa68SMarkus Armbruster const char *buf; 471666daa68SMarkus Armbruster int bdrv_flags = 0; 472666daa68SMarkus Armbruster int on_read_error, on_write_error; 473362e9299SAlberto Garcia bool account_invalid, account_failed; 474e4b24b49SKevin Wolf bool writethrough; 47526f54e9aSMarkus Armbruster BlockBackend *blk; 476a0f1eab1SMarkus Armbruster BlockDriverState *bs; 477cc0681c4SBenoît Canet ThrottleConfig cfg; 478666daa68SMarkus Armbruster int snapshot = 0; 479c546194fSStefan Hajnoczi Error *error = NULL; 4800006383eSKevin Wolf QemuOpts *opts; 48140119effSAlberto Garcia QDict *interval_dict = NULL; 48240119effSAlberto Garcia QList *interval_list = NULL; 4830006383eSKevin Wolf const char *id; 484fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 485fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 486fbf8175eSMax Reitz const char *throttling_group = NULL; 487666daa68SMarkus Armbruster 488f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 489f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 490f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4910006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 49284d18f06SMarkus Armbruster if (error) { 493b681072dSKevin Wolf error_propagate(errp, error); 4946376f952SMarkus Armbruster goto err_no_opts; 4950006383eSKevin Wolf } 4960006383eSKevin Wolf 4970006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49884d18f06SMarkus Armbruster if (error) { 499b681072dSKevin Wolf error_propagate(errp, error); 500ec9c10d2SStefan Hajnoczi goto early_err; 5010006383eSKevin Wolf } 5020006383eSKevin Wolf 5030006383eSKevin Wolf if (id) { 5040006383eSKevin Wolf qdict_del(bs_opts, "id"); 5050006383eSKevin Wolf } 5060006383eSKevin Wolf 507666daa68SMarkus Armbruster /* extract parameters */ 508666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 509666daa68SMarkus Armbruster 510362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 511362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 512362e9299SAlberto Garcia 513e4b24b49SKevin Wolf writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); 514e4b24b49SKevin Wolf 51540119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51640119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51740119effSAlberto Garcia 51840119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 51940119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 52040119effSAlberto Garcia qdict_first(interval_dict)->key); 52140119effSAlberto Garcia goto early_err; 52240119effSAlberto Garcia } 5232be5506fSAlberto Garcia 524fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 525fbf8175eSMax Reitz &detect_zeroes, &error); 526fbf8175eSMax Reitz if (error) { 527fbf8175eSMax Reitz error_propagate(errp, error); 528ec9c10d2SStefan Hajnoczi goto early_err; 529a9384affSPaolo Bonzini } 530666daa68SMarkus Armbruster 531666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 532c8057f95SPeter Maydell if (is_help_option(buf)) { 533807105a7SMarkus Armbruster error_printf("Supported formats:"); 534666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 535807105a7SMarkus Armbruster error_printf("\n"); 536ec9c10d2SStefan Hajnoczi goto early_err; 537666daa68SMarkus Armbruster } 53874fe54f2SKevin Wolf 539e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 540e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 541ec9c10d2SStefan Hajnoczi goto early_err; 5426db5f5d6SMike Qiu } 543e4342ce5SMax Reitz qdict_put(bs_opts, "driver", qstring_from_str(buf)); 544666daa68SMarkus Armbruster } 545666daa68SMarkus Armbruster 54692aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 547666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 548b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 54984d18f06SMarkus Armbruster if (error) { 550b681072dSKevin Wolf error_propagate(errp, error); 551ec9c10d2SStefan Hajnoczi goto early_err; 552666daa68SMarkus Armbruster } 553666daa68SMarkus Armbruster } 554666daa68SMarkus Armbruster 55592aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 556666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 557b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 55884d18f06SMarkus Armbruster if (error) { 559b681072dSKevin Wolf error_propagate(errp, error); 560ec9c10d2SStefan Hajnoczi goto early_err; 561666daa68SMarkus Armbruster } 562666daa68SMarkus Armbruster } 563666daa68SMarkus Armbruster 564666daa68SMarkus Armbruster if (snapshot) { 56591a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 566666daa68SMarkus Armbruster } 567666daa68SMarkus Armbruster 5685ec18f8cSMax Reitz /* init */ 56939c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5705ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5715ec18f8cSMax Reitz 572109525adSMax Reitz blk = blk_new(); 5735ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5745ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 575fbf8175eSMax Reitz blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); 5765ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5775ec18f8cSMax Reitz 5785ec18f8cSMax Reitz QDECREF(bs_opts); 5795ec18f8cSMax Reitz } else { 5805ec18f8cSMax Reitz if (file && !*file) { 5815ec18f8cSMax Reitz file = NULL; 5825ec18f8cSMax Reitz } 5835ec18f8cSMax Reitz 58491a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 58591a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 58691a097e7SKevin Wolf * Apply the defaults here instead. */ 58791a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 58891a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 58972e775c7SKevin Wolf assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); 59091a097e7SKevin Wolf 59112d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 59212d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 59312d5ee3aSKevin Wolf } 59412d5ee3aSKevin Wolf 595efaa7c4eSMax Reitz blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); 596e4342ce5SMax Reitz if (!blk) { 597e4342ce5SMax Reitz goto err_no_bs_opts; 598e4342ce5SMax Reitz } 599e4342ce5SMax Reitz bs = blk_bs(blk); 6000006383eSKevin Wolf 601e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 602e4342ce5SMax Reitz 603a0f1eab1SMarkus Armbruster if (bdrv_key_required(bs)) { 604666daa68SMarkus Armbruster autostart = 0; 605a0f1eab1SMarkus Armbruster } 606362e9299SAlberto Garcia 607362e9299SAlberto Garcia block_acct_init(blk_get_stats(blk), account_invalid, account_failed); 6082be5506fSAlberto Garcia 60940119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6102be5506fSAlberto Garcia blk_unref(blk); 6112be5506fSAlberto Garcia blk = NULL; 6122be5506fSAlberto Garcia goto err_no_bs_opts; 6132be5506fSAlberto Garcia } 6142be5506fSAlberto Garcia } 6155ec18f8cSMax Reitz 6167ca7f0f6SKevin Wolf /* disk I/O throttling */ 6177ca7f0f6SKevin Wolf if (throttle_enabled(&cfg)) { 6187ca7f0f6SKevin Wolf if (!throttling_group) { 6197ca7f0f6SKevin Wolf throttling_group = blk_name(blk); 6207ca7f0f6SKevin Wolf } 6217ca7f0f6SKevin Wolf blk_io_limits_enable(blk, throttling_group); 6227ca7f0f6SKevin Wolf blk_set_io_limits(blk, &cfg); 6237ca7f0f6SKevin Wolf } 6247ca7f0f6SKevin Wolf 625e4b24b49SKevin Wolf blk_set_enable_write_cache(blk, !writethrough); 6265ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6270006383eSKevin Wolf 628efaa7c4eSMax Reitz if (!monitor_add_blk(blk, qemu_opts_id(opts), errp)) { 629efaa7c4eSMax Reitz blk_unref(blk); 630efaa7c4eSMax Reitz blk = NULL; 631efaa7c4eSMax Reitz goto err_no_bs_opts; 632efaa7c4eSMax Reitz } 633efaa7c4eSMax Reitz 634e4342ce5SMax Reitz err_no_bs_opts: 6350006383eSKevin Wolf qemu_opts_del(opts); 63640119effSAlberto Garcia QDECREF(interval_dict); 63740119effSAlberto Garcia QDECREF(interval_list); 63818e46a03SMarkus Armbruster return blk; 639a9ae2bffSMarkus Armbruster 640ec9c10d2SStefan Hajnoczi early_err: 641ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 64240119effSAlberto Garcia QDECREF(interval_dict); 64340119effSAlberto Garcia QDECREF(interval_list); 6446376f952SMarkus Armbruster err_no_opts: 6456376f952SMarkus Armbruster QDECREF(bs_opts); 646a9ae2bffSMarkus Armbruster return NULL; 647666daa68SMarkus Armbruster } 648666daa68SMarkus Armbruster 649bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts; 650bd745e23SMax Reitz 651bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 652bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 653bd745e23SMax Reitz { 654bd745e23SMax Reitz BlockDriverState *bs; 655bd745e23SMax Reitz QemuOpts *opts; 656bd745e23SMax Reitz Error *local_error = NULL; 657bd745e23SMax Reitz BlockdevDetectZeroesOptions detect_zeroes; 658bd745e23SMax Reitz int bdrv_flags = 0; 659bd745e23SMax Reitz 660bd745e23SMax Reitz opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); 661bd745e23SMax Reitz if (!opts) { 662bd745e23SMax Reitz goto fail; 663bd745e23SMax Reitz } 664bd745e23SMax Reitz 665bd745e23SMax Reitz qemu_opts_absorb_qdict(opts, bs_opts, &local_error); 666bd745e23SMax Reitz if (local_error) { 667bd745e23SMax Reitz error_propagate(errp, local_error); 668bd745e23SMax Reitz goto fail; 669bd745e23SMax Reitz } 670bd745e23SMax Reitz 671bd745e23SMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, 672bd745e23SMax Reitz &detect_zeroes, &local_error); 673bd745e23SMax Reitz if (local_error) { 674bd745e23SMax Reitz error_propagate(errp, local_error); 675bd745e23SMax Reitz goto fail; 676bd745e23SMax Reitz } 677bd745e23SMax Reitz 678a81d6164SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 679a81d6164SKevin Wolf * with other callers) rather than what we want as the real defaults. 680a81d6164SKevin Wolf * Apply the defaults here instead. */ 681a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 682a81d6164SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 683a81d6164SKevin Wolf 68412d5ee3aSKevin Wolf if (runstate_check(RUN_STATE_INMIGRATE)) { 68512d5ee3aSKevin Wolf bdrv_flags |= BDRV_O_INACTIVE; 68612d5ee3aSKevin Wolf } 68712d5ee3aSKevin Wolf 6885b363937SMax Reitz bs = bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); 6895b363937SMax Reitz if (!bs) { 690bd745e23SMax Reitz goto fail_no_bs_opts; 691bd745e23SMax Reitz } 692bd745e23SMax Reitz 693bd745e23SMax Reitz bs->detect_zeroes = detect_zeroes; 694bd745e23SMax Reitz 695bd745e23SMax Reitz fail_no_bs_opts: 696bd745e23SMax Reitz qemu_opts_del(opts); 697bd745e23SMax Reitz return bs; 698bd745e23SMax Reitz 699bd745e23SMax Reitz fail: 700bd745e23SMax Reitz qemu_opts_del(opts); 701bd745e23SMax Reitz QDECREF(bs_opts); 702bd745e23SMax Reitz return NULL; 703bd745e23SMax Reitz } 704bd745e23SMax Reitz 7059c4218e9SMax Reitz void blockdev_close_all_bdrv_states(void) 7069c4218e9SMax Reitz { 7079c4218e9SMax Reitz BlockDriverState *bs, *next_bs; 7089c4218e9SMax Reitz 7099c4218e9SMax Reitz QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { 7109c4218e9SMax Reitz AioContext *ctx = bdrv_get_aio_context(bs); 7119c4218e9SMax Reitz 7129c4218e9SMax Reitz aio_context_acquire(ctx); 7139c4218e9SMax Reitz bdrv_unref(bs); 7149c4218e9SMax Reitz aio_context_release(ctx); 7159c4218e9SMax Reitz } 7169c4218e9SMax Reitz } 7179c4218e9SMax Reitz 718262b4e8fSMax Reitz /* Iterates over the list of monitor-owned BlockDriverStates */ 719262b4e8fSMax Reitz BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) 720262b4e8fSMax Reitz { 721262b4e8fSMax Reitz return bs ? QTAILQ_NEXT(bs, monitor_list) 722262b4e8fSMax Reitz : QTAILQ_FIRST(&monitor_bdrv_states); 723262b4e8fSMax Reitz } 724262b4e8fSMax Reitz 7255abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 7265abbf0eeSKevin Wolf Error **errp) 72757975222SKevin Wolf { 72857975222SKevin Wolf const char *value; 72957975222SKevin Wolf 73057975222SKevin Wolf value = qemu_opt_get(opts, from); 73157975222SKevin Wolf if (value) { 7325abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 7335abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 7345abbf0eeSKevin Wolf "same time", to, from); 7355abbf0eeSKevin Wolf return; 7365abbf0eeSKevin Wolf } 73720d6cd47SJun Li } 73820d6cd47SJun Li 73920d6cd47SJun Li /* rename all items in opts */ 74020d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 741f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 74257975222SKevin Wolf qemu_opt_unset(opts, from); 74357975222SKevin Wolf } 74457975222SKevin Wolf } 74557975222SKevin Wolf 74633cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 74733cb7dc8SKevin Wolf .name = "drive", 74833cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 74933cb7dc8SKevin Wolf .desc = { 75033cb7dc8SKevin Wolf { 75187a899c5SKevin Wolf .name = "bus", 75287a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 75387a899c5SKevin Wolf .help = "bus number", 75487a899c5SKevin Wolf },{ 75587a899c5SKevin Wolf .name = "unit", 75687a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 75787a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 75887a899c5SKevin Wolf },{ 75987a899c5SKevin Wolf .name = "index", 76087a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 76187a899c5SKevin Wolf .help = "index number", 76287a899c5SKevin Wolf },{ 76333cb7dc8SKevin Wolf .name = "media", 76433cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 76533cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 766593d464bSKevin Wolf },{ 767593d464bSKevin Wolf .name = "if", 768593d464bSKevin Wolf .type = QEMU_OPT_STRING, 769593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 770b41a7338SKevin Wolf },{ 771b41a7338SKevin Wolf .name = "cyls", 772b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 773b41a7338SKevin Wolf .help = "number of cylinders (ide disk geometry)", 774b41a7338SKevin Wolf },{ 775b41a7338SKevin Wolf .name = "heads", 776b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 777b41a7338SKevin Wolf .help = "number of heads (ide disk geometry)", 778b41a7338SKevin Wolf },{ 779b41a7338SKevin Wolf .name = "secs", 780b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 781b41a7338SKevin Wolf .help = "number of sectors (ide disk geometry)", 782b41a7338SKevin Wolf },{ 783b41a7338SKevin Wolf .name = "trans", 784b41a7338SKevin Wolf .type = QEMU_OPT_STRING, 785b41a7338SKevin Wolf .help = "chs translation (auto, lba, none)", 78626929298SKevin Wolf },{ 78726929298SKevin Wolf .name = "boot", 78826929298SKevin Wolf .type = QEMU_OPT_BOOL, 78926929298SKevin Wolf .help = "(deprecated, ignored)", 790394c7d4dSKevin Wolf },{ 791394c7d4dSKevin Wolf .name = "addr", 792394c7d4dSKevin Wolf .type = QEMU_OPT_STRING, 793394c7d4dSKevin Wolf .help = "pci address (virtio only)", 794d095b465SMax Reitz },{ 795bcf83158SKevin Wolf .name = "serial", 796bcf83158SKevin Wolf .type = QEMU_OPT_STRING, 797bcf83158SKevin Wolf .help = "disk serial number", 798bcf83158SKevin Wolf },{ 799d095b465SMax Reitz .name = "file", 800d095b465SMax Reitz .type = QEMU_OPT_STRING, 801d095b465SMax Reitz .help = "file name", 80233cb7dc8SKevin Wolf }, 8030ebd24e0SKevin Wolf 8040ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 8050ebd24e0SKevin Wolf { 8060ebd24e0SKevin Wolf .name = "read-only", 8070ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8080ebd24e0SKevin Wolf .help = "open drive file as read-only", 8090ebd24e0SKevin Wolf },{ 810ee13ed1cSKevin Wolf .name = "rerror", 811ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 812ee13ed1cSKevin Wolf .help = "read error action", 813ee13ed1cSKevin Wolf },{ 814ee13ed1cSKevin Wolf .name = "werror", 815ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 816ee13ed1cSKevin Wolf .help = "write error action", 817ee13ed1cSKevin Wolf },{ 8180ebd24e0SKevin Wolf .name = "copy-on-read", 8190ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8200ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 8210ebd24e0SKevin Wolf }, 8220ebd24e0SKevin Wolf 82333cb7dc8SKevin Wolf { /* end of list */ } 82433cb7dc8SKevin Wolf }, 82533cb7dc8SKevin Wolf }; 82633cb7dc8SKevin Wolf 82760e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 82857975222SKevin Wolf { 82929c4e2b5SKevin Wolf const char *value; 83018e46a03SMarkus Armbruster BlockBackend *blk; 83133cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 832f298d071SKevin Wolf QDict *bs_opts; 83333cb7dc8SKevin Wolf QemuOpts *legacy_opts; 83433cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 835593d464bSKevin Wolf BlockInterfaceType type; 836b41a7338SKevin Wolf int cyls, heads, secs, translation; 83787a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 838394c7d4dSKevin Wolf const char *devaddr; 839ee13ed1cSKevin Wolf const char *werror, *rerror; 840a7fdbcf0SFam Zheng bool read_only = false; 841a7fdbcf0SFam Zheng bool copy_on_read; 842bcf83158SKevin Wolf const char *serial; 843d095b465SMax Reitz const char *filename; 84433cb7dc8SKevin Wolf Error *local_err = NULL; 845247147fbSKevin Wolf int i; 84629c4e2b5SKevin Wolf 84757975222SKevin Wolf /* Change legacy command line options into QMP ones */ 848247147fbSKevin Wolf static const struct { 849247147fbSKevin Wolf const char *from; 850247147fbSKevin Wolf const char *to; 851247147fbSKevin Wolf } opt_renames[] = { 852247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 853247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 854247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 85557975222SKevin Wolf 856247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 857247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 858247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 85957975222SKevin Wolf 860247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 861247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 862247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8633e9fab69SBenoît Canet 864247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 865247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 866247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8673e9fab69SBenoît Canet 868247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8692024c1dfSBenoît Canet 87076f4afb4SAlberto Garcia { "group", "throttling.group" }, 87176f4afb4SAlberto Garcia 872247147fbSKevin Wolf { "readonly", "read-only" }, 873247147fbSKevin Wolf }; 874247147fbSKevin Wolf 875247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8765abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8775abbf0eeSKevin Wolf &local_err); 8785abbf0eeSKevin Wolf if (local_err) { 879565f65d2SMarkus Armbruster error_report_err(local_err); 8805abbf0eeSKevin Wolf return NULL; 8815abbf0eeSKevin Wolf } 882247147fbSKevin Wolf } 8830f227a94SKevin Wolf 88429c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 88529c4e2b5SKevin Wolf if (value) { 88629c4e2b5SKevin Wolf int flags = 0; 88704feb4a5SKevin Wolf bool writethrough; 88829c4e2b5SKevin Wolf 88904feb4a5SKevin Wolf if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { 89029c4e2b5SKevin Wolf error_report("invalid cache option"); 89129c4e2b5SKevin Wolf return NULL; 89229c4e2b5SKevin Wolf } 89329c4e2b5SKevin Wolf 89429c4e2b5SKevin Wolf /* Specific options take precedence */ 89554861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 89654861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 89704feb4a5SKevin Wolf !writethrough, &error_abort); 89829c4e2b5SKevin Wolf } 89954861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 90054861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 901cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 90229c4e2b5SKevin Wolf } 90354861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 90454861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 905cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 90629c4e2b5SKevin Wolf } 90729c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 90829c4e2b5SKevin Wolf } 90929c4e2b5SKevin Wolf 910f298d071SKevin Wolf /* Get a QDict for processing the options */ 911f298d071SKevin Wolf bs_opts = qdict_new(); 912f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 913f298d071SKevin Wolf 91487ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 91587ea75d5SPeter Crosthwaite &error_abort); 91633cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 91784d18f06SMarkus Armbruster if (local_err) { 918565f65d2SMarkus Armbruster error_report_err(local_err); 91933cb7dc8SKevin Wolf goto fail; 92033cb7dc8SKevin Wolf } 92133cb7dc8SKevin Wolf 92226929298SKevin Wolf /* Deprecated option boot=[on|off] */ 92326929298SKevin Wolf if (qemu_opt_get(legacy_opts, "boot") != NULL) { 92426929298SKevin Wolf fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 92526929298SKevin Wolf "ignored. Future versions will reject this parameter. Please " 92626929298SKevin Wolf "update your scripts.\n"); 92726929298SKevin Wolf } 92826929298SKevin Wolf 92933cb7dc8SKevin Wolf /* Media type */ 93033cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 93133cb7dc8SKevin Wolf if (value) { 93233cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 93333cb7dc8SKevin Wolf media = MEDIA_DISK; 93433cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 93533cb7dc8SKevin Wolf media = MEDIA_CDROM; 936a7fdbcf0SFam Zheng read_only = true; 93733cb7dc8SKevin Wolf } else { 93833cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 93933cb7dc8SKevin Wolf goto fail; 94033cb7dc8SKevin Wolf } 94133cb7dc8SKevin Wolf } 94233cb7dc8SKevin Wolf 9430ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 944a7fdbcf0SFam Zheng read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); 9450ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 9460ebd24e0SKevin Wolf 9470ebd24e0SKevin Wolf if (read_only && copy_on_read) { 9480ebd24e0SKevin Wolf error_report("warning: disabling copy-on-read on read-only drive"); 9490ebd24e0SKevin Wolf copy_on_read = false; 9500ebd24e0SKevin Wolf } 9510ebd24e0SKevin Wolf 9520ebd24e0SKevin Wolf qdict_put(bs_opts, "read-only", 9530ebd24e0SKevin Wolf qstring_from_str(read_only ? "on" : "off")); 9540ebd24e0SKevin Wolf qdict_put(bs_opts, "copy-on-read", 9550ebd24e0SKevin Wolf qstring_from_str(copy_on_read ? "on" :"off")); 9560ebd24e0SKevin Wolf 957593d464bSKevin Wolf /* Controller type */ 958593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 959593d464bSKevin Wolf if (value) { 960593d464bSKevin Wolf for (type = 0; 961593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 962593d464bSKevin Wolf type++) { 963593d464bSKevin Wolf } 964593d464bSKevin Wolf if (type == IF_COUNT) { 965593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 966593d464bSKevin Wolf goto fail; 967593d464bSKevin Wolf } 968593d464bSKevin Wolf } else { 969593d464bSKevin Wolf type = block_default_type; 970593d464bSKevin Wolf } 971593d464bSKevin Wolf 972b41a7338SKevin Wolf /* Geometry */ 973b41a7338SKevin Wolf cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 974b41a7338SKevin Wolf heads = qemu_opt_get_number(legacy_opts, "heads", 0); 975b41a7338SKevin Wolf secs = qemu_opt_get_number(legacy_opts, "secs", 0); 976b41a7338SKevin Wolf 977b41a7338SKevin Wolf if (cyls || heads || secs) { 978b41a7338SKevin Wolf if (cyls < 1) { 979b41a7338SKevin Wolf error_report("invalid physical cyls number"); 980b41a7338SKevin Wolf goto fail; 981b41a7338SKevin Wolf } 982b41a7338SKevin Wolf if (heads < 1) { 983b41a7338SKevin Wolf error_report("invalid physical heads number"); 984b41a7338SKevin Wolf goto fail; 985b41a7338SKevin Wolf } 986b41a7338SKevin Wolf if (secs < 1) { 987b41a7338SKevin Wolf error_report("invalid physical secs number"); 988b41a7338SKevin Wolf goto fail; 989b41a7338SKevin Wolf } 990b41a7338SKevin Wolf } 991b41a7338SKevin Wolf 992b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 993b41a7338SKevin Wolf value = qemu_opt_get(legacy_opts, "trans"); 994b41a7338SKevin Wolf if (value != NULL) { 995b41a7338SKevin Wolf if (!cyls) { 996b41a7338SKevin Wolf error_report("'%s' trans must be used with cyls, heads and secs", 997b41a7338SKevin Wolf value); 998b41a7338SKevin Wolf goto fail; 999b41a7338SKevin Wolf } 1000b41a7338SKevin Wolf if (!strcmp(value, "none")) { 1001b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_NONE; 1002b41a7338SKevin Wolf } else if (!strcmp(value, "lba")) { 1003b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_LBA; 1004f31c41ffSPaolo Bonzini } else if (!strcmp(value, "large")) { 1005f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_LARGE; 1006f31c41ffSPaolo Bonzini } else if (!strcmp(value, "rechs")) { 1007f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_RECHS; 1008b41a7338SKevin Wolf } else if (!strcmp(value, "auto")) { 1009b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 1010b41a7338SKevin Wolf } else { 1011b41a7338SKevin Wolf error_report("'%s' invalid translation type", value); 1012b41a7338SKevin Wolf goto fail; 1013b41a7338SKevin Wolf } 1014b41a7338SKevin Wolf } 1015b41a7338SKevin Wolf 1016b41a7338SKevin Wolf if (media == MEDIA_CDROM) { 1017b41a7338SKevin Wolf if (cyls || secs || heads) { 1018b41a7338SKevin Wolf error_report("CHS can't be set with media=cdrom"); 1019b41a7338SKevin Wolf goto fail; 1020b41a7338SKevin Wolf } 1021b41a7338SKevin Wolf } 1022b41a7338SKevin Wolf 102387a899c5SKevin Wolf /* Device address specified by bus/unit or index. 102487a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 102587a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 102687a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 102787a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 102887a899c5SKevin Wolf 102987a899c5SKevin Wolf max_devs = if_max_devs[type]; 103087a899c5SKevin Wolf 103187a899c5SKevin Wolf if (index != -1) { 103287a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 103387a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 103487a899c5SKevin Wolf goto fail; 103587a899c5SKevin Wolf } 103687a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 103787a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 103887a899c5SKevin Wolf } 103987a899c5SKevin Wolf 104087a899c5SKevin Wolf if (unit_id == -1) { 104187a899c5SKevin Wolf unit_id = 0; 104287a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 104387a899c5SKevin Wolf unit_id++; 104487a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 104587a899c5SKevin Wolf unit_id -= max_devs; 104687a899c5SKevin Wolf bus_id++; 104787a899c5SKevin Wolf } 104887a899c5SKevin Wolf } 104987a899c5SKevin Wolf } 105087a899c5SKevin Wolf 105187a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 105287a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 105387a899c5SKevin Wolf goto fail; 105487a899c5SKevin Wolf } 105587a899c5SKevin Wolf 105687a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 105787a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 105887a899c5SKevin Wolf bus_id, unit_id, index); 105987a899c5SKevin Wolf goto fail; 106087a899c5SKevin Wolf } 106187a899c5SKevin Wolf 1062bcf83158SKevin Wolf /* Serial number */ 1063bcf83158SKevin Wolf serial = qemu_opt_get(legacy_opts, "serial"); 1064bcf83158SKevin Wolf 106587a899c5SKevin Wolf /* no id supplied -> create one */ 106687a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 106787a899c5SKevin Wolf char *new_id; 106887a899c5SKevin Wolf const char *mediastr = ""; 106987a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 107087a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 107187a899c5SKevin Wolf } 107287a899c5SKevin Wolf if (max_devs) { 107387a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 107487a899c5SKevin Wolf mediastr, unit_id); 107587a899c5SKevin Wolf } else { 107687a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 107787a899c5SKevin Wolf mediastr, unit_id); 107887a899c5SKevin Wolf } 107987a899c5SKevin Wolf qdict_put(bs_opts, "id", qstring_from_str(new_id)); 108087a899c5SKevin Wolf g_free(new_id); 108187a899c5SKevin Wolf } 108287a899c5SKevin Wolf 1083394c7d4dSKevin Wolf /* Add virtio block device */ 1084394c7d4dSKevin Wolf devaddr = qemu_opt_get(legacy_opts, "addr"); 1085394c7d4dSKevin Wolf if (devaddr && type != IF_VIRTIO) { 1086394c7d4dSKevin Wolf error_report("addr is not supported by this bus type"); 1087394c7d4dSKevin Wolf goto fail; 1088394c7d4dSKevin Wolf } 1089394c7d4dSKevin Wolf 1090394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 1091394c7d4dSKevin Wolf QemuOpts *devopts; 109287ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 109387ea75d5SPeter Crosthwaite &error_abort); 1094394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 10951f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1096394c7d4dSKevin Wolf } else { 1097f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1098394c7d4dSKevin Wolf } 1099f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1100f43e47dbSMarkus Armbruster &error_abort); 1101394c7d4dSKevin Wolf if (devaddr) { 1102f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1103394c7d4dSKevin Wolf } 1104394c7d4dSKevin Wolf } 1105394c7d4dSKevin Wolf 1106d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 1107d095b465SMax Reitz 1108ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 1109ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 1110ee13ed1cSKevin Wolf if (werror != NULL) { 1111ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1112ee13ed1cSKevin Wolf type != IF_NONE) { 1113ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 1114ee13ed1cSKevin Wolf goto fail; 1115ee13ed1cSKevin Wolf } 1116ee13ed1cSKevin Wolf qdict_put(bs_opts, "werror", qstring_from_str(werror)); 1117ee13ed1cSKevin Wolf } 1118ee13ed1cSKevin Wolf 1119ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 1120ee13ed1cSKevin Wolf if (rerror != NULL) { 1121ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1122ee13ed1cSKevin Wolf type != IF_NONE) { 1123ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 1124ee13ed1cSKevin Wolf goto fail; 1125ee13ed1cSKevin Wolf } 1126ee13ed1cSKevin Wolf qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); 1127ee13ed1cSKevin Wolf } 1128ee13ed1cSKevin Wolf 11292d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 113018e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 11313cb0e25cSMarkus Armbruster bs_opts = NULL; 113218e46a03SMarkus Armbruster if (!blk) { 113384d18f06SMarkus Armbruster if (local_err) { 1134565f65d2SMarkus Armbruster error_report_err(local_err); 1135b681072dSKevin Wolf } 11362d246f01SKevin Wolf goto fail; 1137b681072dSKevin Wolf } else { 113884d18f06SMarkus Armbruster assert(!local_err); 11392d246f01SKevin Wolf } 11402d246f01SKevin Wolf 114126f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 114226f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1143f298d071SKevin Wolf dinfo->opts = all_opts; 11442d246f01SKevin Wolf 1145b41a7338SKevin Wolf dinfo->cyls = cyls; 1146b41a7338SKevin Wolf dinfo->heads = heads; 1147b41a7338SKevin Wolf dinfo->secs = secs; 1148b41a7338SKevin Wolf dinfo->trans = translation; 1149b41a7338SKevin Wolf 1150ee13ed1cSKevin Wolf dinfo->type = type; 115187a899c5SKevin Wolf dinfo->bus = bus_id; 115287a899c5SKevin Wolf dinfo->unit = unit_id; 1153394c7d4dSKevin Wolf dinfo->devaddr = devaddr; 1154bcf83158SKevin Wolf dinfo->serial = g_strdup(serial); 1155bcf83158SKevin Wolf 115626f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 115726f8b3a8SMarkus Armbruster 1158e34ef046SKevin Wolf switch(type) { 1159e34ef046SKevin Wolf case IF_IDE: 1160e34ef046SKevin Wolf case IF_SCSI: 1161e34ef046SKevin Wolf case IF_XEN: 1162e34ef046SKevin Wolf case IF_NONE: 1163e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1164e34ef046SKevin Wolf break; 1165e34ef046SKevin Wolf default: 1166e34ef046SKevin Wolf break; 1167e34ef046SKevin Wolf } 1168e34ef046SKevin Wolf 11692d246f01SKevin Wolf fail: 117033cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 11713cb0e25cSMarkus Armbruster QDECREF(bs_opts); 11722d246f01SKevin Wolf return dinfo; 117357975222SKevin Wolf } 117457975222SKevin Wolf 11753e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1176666daa68SMarkus Armbruster { 1177666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1178a0e8544cSFam Zheng BlockBackend *blk; 11792d3735d3SStefan Hajnoczi int ret; 11802d3735d3SStefan Hajnoczi 1181e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1182da31d594SMax Reitz ret = blk_commit_all(); 1183e8877497SStefan Hajnoczi } else { 118484aa0140SStefan Hajnoczi BlockDriverState *bs; 118584aa0140SStefan Hajnoczi AioContext *aio_context; 118684aa0140SStefan Hajnoczi 1187a0e8544cSFam Zheng blk = blk_by_name(device); 1188a0e8544cSFam Zheng if (!blk) { 118958513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1190ac59eb95SMarkus Armbruster return; 11916ab4b5abSMarkus Armbruster } 11925433c24fSMax Reitz if (!blk_is_available(blk)) { 11935433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 11945433c24fSMax Reitz return; 11955433c24fSMax Reitz } 119684aa0140SStefan Hajnoczi 119784aa0140SStefan Hajnoczi bs = blk_bs(blk); 119884aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 119984aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 120084aa0140SStefan Hajnoczi 120184aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 120284aa0140SStefan Hajnoczi 120384aa0140SStefan Hajnoczi aio_context_release(aio_context); 12042d3735d3SStefan Hajnoczi } 120558513bdeSJeff Cody if (ret < 0) { 120658513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 120758513bdeSJeff Cody strerror(-ret)); 1208666daa68SMarkus Armbruster } 1209666daa68SMarkus Armbruster } 1210666daa68SMarkus Armbruster 121110f75907SEric Blake static void blockdev_do_action(TransactionAction *action, Error **errp) 12126cc2a415SPaolo Bonzini { 1213c8a83e85SKevin Wolf TransactionActionList list; 12146cc2a415SPaolo Bonzini 121510f75907SEric Blake list.value = action; 12166cc2a415SPaolo Bonzini list.next = NULL; 121794d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 12186cc2a415SPaolo Bonzini } 12196cc2a415SPaolo Bonzini 12200901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 12210901f67eSBenoît Canet bool has_node_name, const char *node_name, 12220901f67eSBenoît Canet const char *snapshot_file, 12230901f67eSBenoît Canet bool has_snapshot_node_name, 12240901f67eSBenoît Canet const char *snapshot_node_name, 12256106e249SLuiz Capitulino bool has_format, const char *format, 12260901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1227f8882568SJes Sorensen { 1228a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 12290901f67eSBenoît Canet .has_device = has_device, 12306cc2a415SPaolo Bonzini .device = (char *) device, 12310901f67eSBenoît Canet .has_node_name = has_node_name, 12320901f67eSBenoît Canet .node_name = (char *) node_name, 12336cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 12340901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 12350901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 12366cc2a415SPaolo Bonzini .has_format = has_format, 12376cc2a415SPaolo Bonzini .format = (char *) format, 12386cc2a415SPaolo Bonzini .has_mode = has_mode, 12396cc2a415SPaolo Bonzini .mode = mode, 12406cc2a415SPaolo Bonzini }; 124110f75907SEric Blake TransactionAction action = { 124210f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 124332bafa8fSEric Blake .u.blockdev_snapshot_sync.data = &snapshot, 124410f75907SEric Blake }; 124510f75907SEric Blake blockdev_do_action(&action, errp); 1246f8882568SJes Sorensen } 1247f8882568SJes Sorensen 124843de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 124943de7e2dSAlberto Garcia Error **errp) 125043de7e2dSAlberto Garcia { 125143de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 125243de7e2dSAlberto Garcia .node = (char *) node, 125343de7e2dSAlberto Garcia .overlay = (char *) overlay 125443de7e2dSAlberto Garcia }; 125510f75907SEric Blake TransactionAction action = { 125610f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 125732bafa8fSEric Blake .u.blockdev_snapshot.data = &snapshot_data, 125810f75907SEric Blake }; 125910f75907SEric Blake blockdev_do_action(&action, errp); 126043de7e2dSAlberto Garcia } 126143de7e2dSAlberto Garcia 1262f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1263f323bc9eSWenchao Xia const char *name, 1264f323bc9eSWenchao Xia Error **errp) 1265f323bc9eSWenchao Xia { 1266f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1267f323bc9eSWenchao Xia .device = (char *) device, 1268f323bc9eSWenchao Xia .name = (char *) name 1269f323bc9eSWenchao Xia }; 127010f75907SEric Blake TransactionAction action = { 127110f75907SEric Blake .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 127232bafa8fSEric Blake .u.blockdev_snapshot_internal_sync.data = &snapshot, 127310f75907SEric Blake }; 127410f75907SEric Blake blockdev_do_action(&action, errp); 1275f323bc9eSWenchao Xia } 1276f323bc9eSWenchao Xia 127744e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 127844e3e053SWenchao Xia bool has_id, 127944e3e053SWenchao Xia const char *id, 128044e3e053SWenchao Xia bool has_name, 128144e3e053SWenchao Xia const char *name, 128244e3e053SWenchao Xia Error **errp) 128344e3e053SWenchao Xia { 1284a0e8544cSFam Zheng BlockDriverState *bs; 1285a0e8544cSFam Zheng BlockBackend *blk; 12864ef3982aSStefan Hajnoczi AioContext *aio_context; 128744e3e053SWenchao Xia QEMUSnapshotInfo sn; 128844e3e053SWenchao Xia Error *local_err = NULL; 128944e3e053SWenchao Xia SnapshotInfo *info = NULL; 129044e3e053SWenchao Xia int ret; 129144e3e053SWenchao Xia 1292a0e8544cSFam Zheng blk = blk_by_name(device); 1293a0e8544cSFam Zheng if (!blk) { 129475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 129575158ebbSMarkus Armbruster "Device '%s' not found", device); 129644e3e053SWenchao Xia return NULL; 129744e3e053SWenchao Xia } 12985433c24fSMax Reitz 12995433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 13005433c24fSMax Reitz aio_context_acquire(aio_context); 130144e3e053SWenchao Xia 130244e3e053SWenchao Xia if (!has_id) { 130344e3e053SWenchao Xia id = NULL; 130444e3e053SWenchao Xia } 130544e3e053SWenchao Xia 130644e3e053SWenchao Xia if (!has_name) { 130744e3e053SWenchao Xia name = NULL; 130844e3e053SWenchao Xia } 130944e3e053SWenchao Xia 131044e3e053SWenchao Xia if (!id && !name) { 131144e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 13125433c24fSMax Reitz goto out_aio_context; 131344e3e053SWenchao Xia } 131444e3e053SWenchao Xia 13155433c24fSMax Reitz if (!blk_is_available(blk)) { 13165433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 13175433c24fSMax Reitz goto out_aio_context; 13185433c24fSMax Reitz } 13195433c24fSMax Reitz bs = blk_bs(blk); 13204ef3982aSStefan Hajnoczi 13210b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 13220b928854SStefan Hajnoczi goto out_aio_context; 13230b928854SStefan Hajnoczi } 13240b928854SStefan Hajnoczi 132544e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 132684d18f06SMarkus Armbruster if (local_err) { 132744e3e053SWenchao Xia error_propagate(errp, local_err); 13284ef3982aSStefan Hajnoczi goto out_aio_context; 132944e3e053SWenchao Xia } 133044e3e053SWenchao Xia if (!ret) { 133144e3e053SWenchao Xia error_setg(errp, 133244e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 133344e3e053SWenchao Xia "device '%s'", 133444e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 13354ef3982aSStefan Hajnoczi goto out_aio_context; 133644e3e053SWenchao Xia } 133744e3e053SWenchao Xia 133844e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 133984d18f06SMarkus Armbruster if (local_err) { 134044e3e053SWenchao Xia error_propagate(errp, local_err); 13414ef3982aSStefan Hajnoczi goto out_aio_context; 134244e3e053SWenchao Xia } 134344e3e053SWenchao Xia 13444ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13454ef3982aSStefan Hajnoczi 13465839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 134744e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 134844e3e053SWenchao Xia info->name = g_strdup(sn.name); 134944e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 135044e3e053SWenchao Xia info->date_sec = sn.date_sec; 135144e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 135244e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 135344e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 135444e3e053SWenchao Xia 135544e3e053SWenchao Xia return info; 13564ef3982aSStefan Hajnoczi 13574ef3982aSStefan Hajnoczi out_aio_context: 13584ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13594ef3982aSStefan Hajnoczi return NULL; 136044e3e053SWenchao Xia } 13618802d1fdSJeff Cody 1362341ebc2fSJohn Snow /** 1363341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1364341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1365341ebc2fSJohn Snow * the node reference and bitmap names. 1366341ebc2fSJohn Snow * 1367341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1368341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1369341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1370341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1371341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1372341ebc2fSJohn Snow * 1373341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1374341ebc2fSJohn Snow */ 1375341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1376341ebc2fSJohn Snow const char *name, 1377341ebc2fSJohn Snow BlockDriverState **pbs, 1378341ebc2fSJohn Snow AioContext **paio, 1379341ebc2fSJohn Snow Error **errp) 1380341ebc2fSJohn Snow { 1381341ebc2fSJohn Snow BlockDriverState *bs; 1382341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1383341ebc2fSJohn Snow AioContext *aio_context; 1384341ebc2fSJohn Snow 1385341ebc2fSJohn Snow if (!node) { 1386341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1387341ebc2fSJohn Snow return NULL; 1388341ebc2fSJohn Snow } 1389341ebc2fSJohn Snow if (!name) { 1390341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1391341ebc2fSJohn Snow return NULL; 1392341ebc2fSJohn Snow } 1393341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1394341ebc2fSJohn Snow if (!bs) { 1395341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1396341ebc2fSJohn Snow return NULL; 1397341ebc2fSJohn Snow } 1398341ebc2fSJohn Snow 1399341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 1400341ebc2fSJohn Snow aio_context_acquire(aio_context); 1401341ebc2fSJohn Snow 1402341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1403341ebc2fSJohn Snow if (!bitmap) { 1404341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 1405341ebc2fSJohn Snow goto fail; 1406341ebc2fSJohn Snow } 1407341ebc2fSJohn Snow 1408341ebc2fSJohn Snow if (pbs) { 1409341ebc2fSJohn Snow *pbs = bs; 1410341ebc2fSJohn Snow } 1411341ebc2fSJohn Snow if (paio) { 1412341ebc2fSJohn Snow *paio = aio_context; 1413341ebc2fSJohn Snow } else { 1414341ebc2fSJohn Snow aio_context_release(aio_context); 1415341ebc2fSJohn Snow } 1416341ebc2fSJohn Snow 1417341ebc2fSJohn Snow return bitmap; 1418341ebc2fSJohn Snow 1419341ebc2fSJohn Snow fail: 1420341ebc2fSJohn Snow aio_context_release(aio_context); 1421341ebc2fSJohn Snow return NULL; 1422341ebc2fSJohn Snow } 1423341ebc2fSJohn Snow 1424b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1425ba0c86a3SWenchao Xia 142650f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1427ba0c86a3SWenchao Xia 142850f43f0fSJohn Snow /** 142950f43f0fSJohn Snow * BlkActionOps: 143050f43f0fSJohn Snow * Table of operations that define an Action. 143150f43f0fSJohn Snow * 143250f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 143350f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 143450f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 143550f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 143650f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 143750f43f0fSJohn Snow * commit() or abort(). Can be NULL. 143850f43f0fSJohn Snow * 143950f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 144050f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1441ba0c86a3SWenchao Xia */ 144250f43f0fSJohn Snow typedef struct BlkActionOps { 144350f43f0fSJohn Snow size_t instance_size; 144450f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 144550f43f0fSJohn Snow void (*commit)(BlkActionState *common); 144650f43f0fSJohn Snow void (*abort)(BlkActionState *common); 144750f43f0fSJohn Snow void (*clean)(BlkActionState *common); 144850f43f0fSJohn Snow } BlkActionOps; 144950f43f0fSJohn Snow 145050f43f0fSJohn Snow /** 145150f43f0fSJohn Snow * BlkActionState: 145250f43f0fSJohn Snow * Describes one Action's state within a Transaction. 145350f43f0fSJohn Snow * 145450f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 145550f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 145694d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 145750f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 145850f43f0fSJohn Snow * 145950f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 146050f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 146150f43f0fSJohn Snow * base class. 146250f43f0fSJohn Snow */ 146350f43f0fSJohn Snow struct BlkActionState { 1464c8a83e85SKevin Wolf TransactionAction *action; 146550f43f0fSJohn Snow const BlkActionOps *ops; 146694d16a64SJohn Snow BlockJobTxn *block_job_txn; 146794d16a64SJohn Snow TransactionProperties *txn_props; 146850f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1469ba0c86a3SWenchao Xia }; 1470ba0c86a3SWenchao Xia 1471bbe86010SWenchao Xia /* internal snapshot private data */ 1472bbe86010SWenchao Xia typedef struct InternalSnapshotState { 147350f43f0fSJohn Snow BlkActionState common; 1474bbe86010SWenchao Xia BlockDriverState *bs; 14755d6e96efSStefan Hajnoczi AioContext *aio_context; 1476bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1477507306ccSFam Zheng bool created; 1478bbe86010SWenchao Xia } InternalSnapshotState; 1479bbe86010SWenchao Xia 148094d16a64SJohn Snow 148194d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 148294d16a64SJohn Snow { 148394d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 148494d16a64SJohn Snow error_setg(errp, 148594d16a64SJohn Snow "Action '%s' does not support Transaction property " 148694d16a64SJohn Snow "completion-mode = %s", 148794d16a64SJohn Snow TransactionActionKind_lookup[s->action->type], 148894d16a64SJohn Snow ActionCompletionMode_lookup[s->txn_props->completion_mode]); 148994d16a64SJohn Snow return -1; 149094d16a64SJohn Snow } 149194d16a64SJohn Snow return 0; 149294d16a64SJohn Snow } 149394d16a64SJohn Snow 149450f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1495bbe86010SWenchao Xia Error **errp) 1496bbe86010SWenchao Xia { 1497f70edf99SMarkus Armbruster Error *local_err = NULL; 1498bbe86010SWenchao Xia const char *device; 1499bbe86010SWenchao Xia const char *name; 1500a0e8544cSFam Zheng BlockBackend *blk; 1501bbe86010SWenchao Xia BlockDriverState *bs; 1502bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1503bbe86010SWenchao Xia bool ret; 1504bbe86010SWenchao Xia qemu_timeval tv; 1505bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1506bbe86010SWenchao Xia InternalSnapshotState *state; 1507bbe86010SWenchao Xia int ret1; 1508bbe86010SWenchao Xia 15096a8f9661SEric Blake g_assert(common->action->type == 1510bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 151132bafa8fSEric Blake internal = common->action->u.blockdev_snapshot_internal_sync.data; 1512bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1513bbe86010SWenchao Xia 1514bbe86010SWenchao Xia /* 1. parse input */ 1515bbe86010SWenchao Xia device = internal->device; 1516bbe86010SWenchao Xia name = internal->name; 1517bbe86010SWenchao Xia 1518bbe86010SWenchao Xia /* 2. check for validation */ 151994d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 152094d16a64SJohn Snow return; 152194d16a64SJohn Snow } 152294d16a64SJohn Snow 1523a0e8544cSFam Zheng blk = blk_by_name(device); 1524a0e8544cSFam Zheng if (!blk) { 152575158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 152675158ebbSMarkus Armbruster "Device '%s' not found", device); 1527bbe86010SWenchao Xia return; 1528bbe86010SWenchao Xia } 1529bbe86010SWenchao Xia 15305d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 15315433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 15325d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 15335d6e96efSStefan Hajnoczi 15345433c24fSMax Reitz if (!blk_is_available(blk)) { 1535c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1536bbe86010SWenchao Xia return; 1537bbe86010SWenchao Xia } 15385433c24fSMax Reitz bs = blk_bs(blk); 1539bbe86010SWenchao Xia 1540507306ccSFam Zheng state->bs = bs; 1541507306ccSFam Zheng bdrv_drained_begin(bs); 1542507306ccSFam Zheng 15433dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 15443dc7ca3cSStefan Hajnoczi return; 15453dc7ca3cSStefan Hajnoczi } 15463dc7ca3cSStefan Hajnoczi 1547bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 154881e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1549bbe86010SWenchao Xia return; 1550bbe86010SWenchao Xia } 1551bbe86010SWenchao Xia 1552bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 155381e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 155481e5f78aSAlberto Garcia "does not support internal snapshots", 155581e5f78aSAlberto Garcia bs->drv->format_name, device); 1556bbe86010SWenchao Xia return; 1557bbe86010SWenchao Xia } 1558bbe86010SWenchao Xia 1559bbe86010SWenchao Xia if (!strlen(name)) { 1560bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1561bbe86010SWenchao Xia return; 1562bbe86010SWenchao Xia } 1563bbe86010SWenchao Xia 1564bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1565f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1566f70edf99SMarkus Armbruster &local_err); 1567f70edf99SMarkus Armbruster if (local_err) { 1568f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1569bbe86010SWenchao Xia return; 1570bbe86010SWenchao Xia } else if (ret) { 1571bbe86010SWenchao Xia error_setg(errp, 1572bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1573bbe86010SWenchao Xia name, device); 1574bbe86010SWenchao Xia return; 1575bbe86010SWenchao Xia } 1576bbe86010SWenchao Xia 1577bbe86010SWenchao Xia /* 3. take the snapshot */ 1578bbe86010SWenchao Xia sn = &state->sn; 1579bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1580bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1581bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1582bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1583bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1584bbe86010SWenchao Xia 1585bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1586bbe86010SWenchao Xia if (ret1 < 0) { 1587bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1588bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1589bbe86010SWenchao Xia name, device); 1590bbe86010SWenchao Xia return; 1591bbe86010SWenchao Xia } 1592bbe86010SWenchao Xia 1593bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1594507306ccSFam Zheng state->created = true; 1595bbe86010SWenchao Xia } 1596bbe86010SWenchao Xia 159750f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1598bbe86010SWenchao Xia { 1599bbe86010SWenchao Xia InternalSnapshotState *state = 1600bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1601bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1602bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1603bbe86010SWenchao Xia Error *local_error = NULL; 1604bbe86010SWenchao Xia 1605507306ccSFam Zheng if (!state->created) { 1606bbe86010SWenchao Xia return; 1607bbe86010SWenchao Xia } 1608bbe86010SWenchao Xia 1609bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1610c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1611c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1612c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1613c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1614c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1615bbe86010SWenchao Xia } 1616bbe86010SWenchao Xia } 1617bbe86010SWenchao Xia 161850f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 16195d6e96efSStefan Hajnoczi { 16205d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 16215d6e96efSStefan Hajnoczi common, common); 16225d6e96efSStefan Hajnoczi 16235d6e96efSStefan Hajnoczi if (state->aio_context) { 1624507306ccSFam Zheng if (state->bs) { 1625507306ccSFam Zheng bdrv_drained_end(state->bs); 1626507306ccSFam Zheng } 16275d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16285d6e96efSStefan Hajnoczi } 16295d6e96efSStefan Hajnoczi } 16305d6e96efSStefan Hajnoczi 1631ba0c86a3SWenchao Xia /* external snapshot private data */ 1632ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 163350f43f0fSJohn Snow BlkActionState common; 16348802d1fdSJeff Cody BlockDriverState *old_bs; 16358802d1fdSJeff Cody BlockDriverState *new_bs; 16365d6e96efSStefan Hajnoczi AioContext *aio_context; 1637ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16388802d1fdSJeff Cody 163950f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16409b9877eeSWenchao Xia Error **errp) 16419b9877eeSWenchao Xia { 16425b363937SMax Reitz int flags = 0; 164343de7e2dSAlberto Garcia QDict *options = NULL; 16449b9877eeSWenchao Xia Error *local_err = NULL; 164543de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1646e2a31e87SWenchao Xia const char *device; 16470901f67eSBenoît Canet const char *node_name; 164843de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 164943de7e2dSAlberto Garcia const char *snapshot_ref; 165043de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1651e2a31e87SWenchao Xia const char *new_image_file; 1652ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1653ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1654c8a83e85SKevin Wolf TransactionAction *action = common->action; 16559b9877eeSWenchao Xia 165643de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 165743de7e2dSAlberto Garcia * purpose but a different set of parameters */ 165843de7e2dSAlberto Garcia switch (action->type) { 165943de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 166043de7e2dSAlberto Garcia { 166132bafa8fSEric Blake BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 166243de7e2dSAlberto Garcia device = s->node; 166343de7e2dSAlberto Garcia node_name = s->node; 166443de7e2dSAlberto Garcia new_image_file = NULL; 166543de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1666e2a31e87SWenchao Xia } 166743de7e2dSAlberto Garcia break; 166843de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 166943de7e2dSAlberto Garcia { 167032bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 167143de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 167243de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 167343de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 167443de7e2dSAlberto Garcia snapshot_ref = NULL; 167543de7e2dSAlberto Garcia } 167643de7e2dSAlberto Garcia break; 167743de7e2dSAlberto Garcia default: 167843de7e2dSAlberto Garcia g_assert_not_reached(); 1679e2a31e87SWenchao Xia } 1680e2a31e87SWenchao Xia 1681e2a31e87SWenchao Xia /* start processing */ 168294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 168394d16a64SJohn Snow return; 168494d16a64SJohn Snow } 168594d16a64SJohn Snow 168643de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 168743de7e2dSAlberto Garcia if (!state->old_bs) { 16889b9877eeSWenchao Xia return; 16899b9877eeSWenchao Xia } 16909b9877eeSWenchao Xia 16915d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 16925d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 16935d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1694da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 16955d6e96efSStefan Hajnoczi 1696ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1697c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 16989b9877eeSWenchao Xia return; 16999b9877eeSWenchao Xia } 17009b9877eeSWenchao Xia 17013718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 17023718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 17039b9877eeSWenchao Xia return; 17049b9877eeSWenchao Xia } 17059b9877eeSWenchao Xia 1706ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1707ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1708c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 17099b9877eeSWenchao Xia return; 17109b9877eeSWenchao Xia } 17119b9877eeSWenchao Xia } 17129b9877eeSWenchao Xia 1713212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1714c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1715f6186f49SBenoît Canet return; 1716f6186f49SBenoît Canet } 1717f6186f49SBenoît Canet 171843de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 171932bafa8fSEric Blake BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 172043de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 172143de7e2dSAlberto Garcia enum NewImageMode mode; 172243de7e2dSAlberto Garcia const char *snapshot_node_name = 172343de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 172443de7e2dSAlberto Garcia 172543de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 172643de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 172743de7e2dSAlberto Garcia return; 172843de7e2dSAlberto Garcia } 172943de7e2dSAlberto Garcia 173043de7e2dSAlberto Garcia if (snapshot_node_name && 173143de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 173243de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 173343de7e2dSAlberto Garcia return; 173443de7e2dSAlberto Garcia } 173543de7e2dSAlberto Garcia 1736ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17374c844983SKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); 17389b9877eeSWenchao Xia 17399b9877eeSWenchao Xia /* create new image w/backing file */ 174043de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17419b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 1742f86b8b58SKevin Wolf int64_t size = bdrv_getlength(state->old_bs); 1743f86b8b58SKevin Wolf if (size < 0) { 1744f86b8b58SKevin Wolf error_setg_errno(errp, -size, "bdrv_getlength failed"); 1745f86b8b58SKevin Wolf return; 1746f86b8b58SKevin Wolf } 17479b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1748ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1749ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 1750f86b8b58SKevin Wolf NULL, size, flags, &local_err, false); 175184d18f06SMarkus Armbruster if (local_err) { 17529b9877eeSWenchao Xia error_propagate(errp, local_err); 17539b9877eeSWenchao Xia return; 17549b9877eeSWenchao Xia } 17559b9877eeSWenchao Xia } 17569b9877eeSWenchao Xia 17570901f67eSBenoît Canet options = qdict_new(); 175843de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 17590901f67eSBenoît Canet qdict_put(options, "node-name", 17600901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 17610901f67eSBenoît Canet } 1762e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 17630901f67eSBenoît Canet 176443de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 176543de7e2dSAlberto Garcia } 176643de7e2dSAlberto Garcia 17675b363937SMax Reitz state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, 17685b363937SMax Reitz errp); 1769f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17705b363937SMax Reitz if (!state->new_bs) { 177143de7e2dSAlberto Garcia return; 177243de7e2dSAlberto Garcia } 177343de7e2dSAlberto Garcia 17741f0c461bSKevin Wolf if (bdrv_has_blk(state->new_bs)) { 177543de7e2dSAlberto Garcia error_setg(errp, "The snapshot is already in use by %s", 17761f0c461bSKevin Wolf bdrv_get_parent_name(state->new_bs)); 177743de7e2dSAlberto Garcia return; 177843de7e2dSAlberto Garcia } 177943de7e2dSAlberto Garcia 178043de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 178143de7e2dSAlberto Garcia errp)) { 178243de7e2dSAlberto Garcia return; 178343de7e2dSAlberto Garcia } 178443de7e2dSAlberto Garcia 178543de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 178643de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 178708b24cfeSAlberto Garcia return; 178808b24cfeSAlberto Garcia } 178908b24cfeSAlberto Garcia 179008b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 179108b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 17929b9877eeSWenchao Xia } 17939b9877eeSWenchao Xia } 17949b9877eeSWenchao Xia 179550f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 17963b0047e8SWenchao Xia { 1797ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1798ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1799ba0c86a3SWenchao Xia 18005d6e96efSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, state->aio_context); 18015d6e96efSStefan Hajnoczi 1802ba5d6ab6SStefan Hajnoczi /* This removes our old bs and adds the new bs */ 1803ba5d6ab6SStefan Hajnoczi bdrv_append(state->new_bs, state->old_bs); 18043b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 18053b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 18063b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 18074c844983SKevin Wolf if (!state->old_bs->copy_on_read) { 1808dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 18093b0047e8SWenchao Xia NULL); 18103b0047e8SWenchao Xia } 18114c844983SKevin Wolf } 18123b0047e8SWenchao Xia 181350f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 181496b86bf7SWenchao Xia { 1815ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1816ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1817ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 18184f6fd349SFam Zheng bdrv_unref(state->new_bs); 181996b86bf7SWenchao Xia } 1820da763e83SFam Zheng } 1821da763e83SFam Zheng 182250f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1823da763e83SFam Zheng { 1824da763e83SFam Zheng ExternalSnapshotState *state = 1825da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 18265d6e96efSStefan Hajnoczi if (state->aio_context) { 1827da763e83SFam Zheng bdrv_drained_end(state->old_bs); 18285d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18295d6e96efSStefan Hajnoczi } 183096b86bf7SWenchao Xia } 183196b86bf7SWenchao Xia 18323037f364SStefan Hajnoczi typedef struct DriveBackupState { 183350f43f0fSJohn Snow BlkActionState common; 18343037f364SStefan Hajnoczi BlockDriverState *bs; 18355d6e96efSStefan Hajnoczi AioContext *aio_context; 18363037f364SStefan Hajnoczi BlockJob *job; 18373037f364SStefan Hajnoczi } DriveBackupState; 18383037f364SStefan Hajnoczi 183978f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 184078f51fdeSJohn Snow bool has_format, const char *format, 184178f51fdeSJohn Snow enum MirrorSyncMode sync, 184278f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 184378f51fdeSJohn Snow bool has_speed, int64_t speed, 184478f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 184578f51fdeSJohn Snow bool has_on_source_error, 184678f51fdeSJohn Snow BlockdevOnError on_source_error, 184778f51fdeSJohn Snow bool has_on_target_error, 184878f51fdeSJohn Snow BlockdevOnError on_target_error, 184978f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 185078f51fdeSJohn Snow 185150f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18523037f364SStefan Hajnoczi { 18533037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1854a0e8544cSFam Zheng BlockBackend *blk; 18553037f364SStefan Hajnoczi DriveBackup *backup; 18563037f364SStefan Hajnoczi Error *local_err = NULL; 18573037f364SStefan Hajnoczi 18586a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 185932bafa8fSEric Blake backup = common->action->u.drive_backup.data; 18603037f364SStefan Hajnoczi 1861a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1862a0e8544cSFam Zheng if (!blk) { 186375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 186475158ebbSMarkus Armbruster "Device '%s' not found", backup->device); 18655d6e96efSStefan Hajnoczi return; 18665d6e96efSStefan Hajnoczi } 18675d6e96efSStefan Hajnoczi 18681fdd4b7bSFam Zheng if (!blk_is_available(blk)) { 18691fdd4b7bSFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 18701fdd4b7bSFam Zheng return; 18711fdd4b7bSFam Zheng } 18721fdd4b7bSFam Zheng 18735d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 18745433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 18755d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 18761fdd4b7bSFam Zheng bdrv_drained_begin(blk_bs(blk)); 18771fdd4b7bSFam Zheng state->bs = blk_bs(blk); 18785d6e96efSStefan Hajnoczi 187978f51fdeSJohn Snow do_drive_backup(backup->device, backup->target, 18803037f364SStefan Hajnoczi backup->has_format, backup->format, 1881b53169eaSStefan Hajnoczi backup->sync, 18823037f364SStefan Hajnoczi backup->has_mode, backup->mode, 18833037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1884d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 18853037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 18863037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 188794d16a64SJohn Snow common->block_job_txn, &local_err); 188884d18f06SMarkus Armbruster if (local_err) { 18893037f364SStefan Hajnoczi error_propagate(errp, local_err); 18903037f364SStefan Hajnoczi return; 18913037f364SStefan Hajnoczi } 18923037f364SStefan Hajnoczi 18933037f364SStefan Hajnoczi state->job = state->bs->job; 18943037f364SStefan Hajnoczi } 18953037f364SStefan Hajnoczi 189650f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 18973037f364SStefan Hajnoczi { 18983037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18993037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 19003037f364SStefan Hajnoczi 19013037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 19023037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 19033037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 19043037f364SStefan Hajnoczi } 19053037f364SStefan Hajnoczi } 19063037f364SStefan Hajnoczi 190750f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 19085d6e96efSStefan Hajnoczi { 19095d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 19105d6e96efSStefan Hajnoczi 19115d6e96efSStefan Hajnoczi if (state->aio_context) { 19121fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 19135d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 19145d6e96efSStefan Hajnoczi } 19155d6e96efSStefan Hajnoczi } 19165d6e96efSStefan Hajnoczi 1917bd8baecdSFam Zheng typedef struct BlockdevBackupState { 191850f43f0fSJohn Snow BlkActionState common; 1919bd8baecdSFam Zheng BlockDriverState *bs; 1920bd8baecdSFam Zheng BlockJob *job; 1921bd8baecdSFam Zheng AioContext *aio_context; 1922bd8baecdSFam Zheng } BlockdevBackupState; 1923bd8baecdSFam Zheng 192478f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target, 192578f51fdeSJohn Snow enum MirrorSyncMode sync, 192678f51fdeSJohn Snow bool has_speed, int64_t speed, 192778f51fdeSJohn Snow bool has_on_source_error, 192878f51fdeSJohn Snow BlockdevOnError on_source_error, 192978f51fdeSJohn Snow bool has_on_target_error, 193078f51fdeSJohn Snow BlockdevOnError on_target_error, 193178f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 193278f51fdeSJohn Snow 193350f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1934bd8baecdSFam Zheng { 1935bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1936bd8baecdSFam Zheng BlockdevBackup *backup; 19375433c24fSMax Reitz BlockBackend *blk, *target; 1938bd8baecdSFam Zheng Error *local_err = NULL; 1939bd8baecdSFam Zheng 19406a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 194132bafa8fSEric Blake backup = common->action->u.blockdev_backup.data; 1942bd8baecdSFam Zheng 1943a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1944a0e8544cSFam Zheng if (!blk) { 19455b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->device); 1946bd8baecdSFam Zheng return; 1947bd8baecdSFam Zheng } 1948bd8baecdSFam Zheng 1949ff52bf36SFam Zheng if (!blk_is_available(blk)) { 1950ff52bf36SFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1951ff52bf36SFam Zheng return; 1952ff52bf36SFam Zheng } 1953ff52bf36SFam Zheng 19545433c24fSMax Reitz target = blk_by_name(backup->target); 19555433c24fSMax Reitz if (!target) { 19565b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->target); 1957bd8baecdSFam Zheng return; 1958bd8baecdSFam Zheng } 1959bd8baecdSFam Zheng 1960bd8baecdSFam Zheng /* AioContext is released in .clean() */ 19615433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 19625433c24fSMax Reitz if (state->aio_context != blk_get_aio_context(target)) { 1963bd8baecdSFam Zheng state->aio_context = NULL; 1964bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1965bd8baecdSFam Zheng return; 1966bd8baecdSFam Zheng } 1967bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1968ff52bf36SFam Zheng state->bs = blk_bs(blk); 1969ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1970bd8baecdSFam Zheng 197178f51fdeSJohn Snow do_blockdev_backup(backup->device, backup->target, 1972bd8baecdSFam Zheng backup->sync, 1973bd8baecdSFam Zheng backup->has_speed, backup->speed, 1974bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1975bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 197694d16a64SJohn Snow common->block_job_txn, &local_err); 1977bd8baecdSFam Zheng if (local_err) { 1978bd8baecdSFam Zheng error_propagate(errp, local_err); 1979bd8baecdSFam Zheng return; 1980bd8baecdSFam Zheng } 1981bd8baecdSFam Zheng 1982bd8baecdSFam Zheng state->job = state->bs->job; 1983bd8baecdSFam Zheng } 1984bd8baecdSFam Zheng 198550f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1986bd8baecdSFam Zheng { 1987bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1988bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1989bd8baecdSFam Zheng 1990bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 1991bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 1992bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 1993bd8baecdSFam Zheng } 1994bd8baecdSFam Zheng } 1995bd8baecdSFam Zheng 199650f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 1997bd8baecdSFam Zheng { 1998bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1999bd8baecdSFam Zheng 2000bd8baecdSFam Zheng if (state->aio_context) { 2001ff52bf36SFam Zheng bdrv_drained_end(state->bs); 2002bd8baecdSFam Zheng aio_context_release(state->aio_context); 2003bd8baecdSFam Zheng } 2004bd8baecdSFam Zheng } 2005bd8baecdSFam Zheng 2006df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 200750f43f0fSJohn Snow BlkActionState common; 2008df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 2009df9a681dSFam Zheng BlockDriverState *bs; 2010df9a681dSFam Zheng AioContext *aio_context; 2011df9a681dSFam Zheng HBitmap *backup; 2012df9a681dSFam Zheng bool prepared; 2013df9a681dSFam Zheng } BlockDirtyBitmapState; 2014df9a681dSFam Zheng 201550f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 2016df9a681dSFam Zheng Error **errp) 2017df9a681dSFam Zheng { 2018df9a681dSFam Zheng Error *local_err = NULL; 2019df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2020df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2021df9a681dSFam Zheng common, common); 2022df9a681dSFam Zheng 202394d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 202494d16a64SJohn Snow return; 202594d16a64SJohn Snow } 202694d16a64SJohn Snow 202732bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2028df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2029df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2030df9a681dSFam Zheng action->has_granularity, action->granularity, 2031df9a681dSFam Zheng &local_err); 2032df9a681dSFam Zheng 2033df9a681dSFam Zheng if (!local_err) { 2034df9a681dSFam Zheng state->prepared = true; 2035df9a681dSFam Zheng } else { 2036df9a681dSFam Zheng error_propagate(errp, local_err); 2037df9a681dSFam Zheng } 2038df9a681dSFam Zheng } 2039df9a681dSFam Zheng 204050f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2041df9a681dSFam Zheng { 2042df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2043df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2044df9a681dSFam Zheng common, common); 2045df9a681dSFam Zheng 204632bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_add.data; 2047df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2048df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2049df9a681dSFam Zheng */ 2050df9a681dSFam Zheng if (state->prepared) { 2051df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2052df9a681dSFam Zheng } 2053df9a681dSFam Zheng } 2054df9a681dSFam Zheng 205550f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2056df9a681dSFam Zheng Error **errp) 2057df9a681dSFam Zheng { 2058df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2059df9a681dSFam Zheng common, common); 2060df9a681dSFam Zheng BlockDirtyBitmap *action; 2061df9a681dSFam Zheng 206294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 206394d16a64SJohn Snow return; 206494d16a64SJohn Snow } 206594d16a64SJohn Snow 206632bafa8fSEric Blake action = common->action->u.block_dirty_bitmap_clear.data; 2067df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2068df9a681dSFam Zheng action->name, 2069df9a681dSFam Zheng &state->bs, 2070df9a681dSFam Zheng &state->aio_context, 2071df9a681dSFam Zheng errp); 2072df9a681dSFam Zheng if (!state->bitmap) { 2073df9a681dSFam Zheng return; 2074df9a681dSFam Zheng } 2075df9a681dSFam Zheng 2076df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2077df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2078df9a681dSFam Zheng return; 2079df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2080df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2081df9a681dSFam Zheng return; 2082df9a681dSFam Zheng } 2083df9a681dSFam Zheng 2084df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2085df9a681dSFam Zheng /* AioContext is released in .clean() */ 2086df9a681dSFam Zheng } 2087df9a681dSFam Zheng 208850f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2089df9a681dSFam Zheng { 2090df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2091df9a681dSFam Zheng common, common); 2092df9a681dSFam Zheng 2093df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2094df9a681dSFam Zheng } 2095df9a681dSFam Zheng 209650f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2097df9a681dSFam Zheng { 2098df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2099df9a681dSFam Zheng common, common); 2100df9a681dSFam Zheng 2101df9a681dSFam Zheng hbitmap_free(state->backup); 2102df9a681dSFam Zheng } 2103df9a681dSFam Zheng 210450f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2105df9a681dSFam Zheng { 2106df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2107df9a681dSFam Zheng common, common); 2108df9a681dSFam Zheng 2109df9a681dSFam Zheng if (state->aio_context) { 2110df9a681dSFam Zheng aio_context_release(state->aio_context); 2111df9a681dSFam Zheng } 2112df9a681dSFam Zheng } 2113df9a681dSFam Zheng 211450f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 211578b18b78SStefan Hajnoczi { 211678b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 211778b18b78SStefan Hajnoczi } 211878b18b78SStefan Hajnoczi 211950f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 212078b18b78SStefan Hajnoczi { 2121dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 212278b18b78SStefan Hajnoczi } 212378b18b78SStefan Hajnoczi 212450f43f0fSJohn Snow static const BlkActionOps actions[] = { 212543de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 212643de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 212743de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 212843de7e2dSAlberto Garcia .commit = external_snapshot_commit, 212943de7e2dSAlberto Garcia .abort = external_snapshot_abort, 21304ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 213143de7e2dSAlberto Garcia }, 2132c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2133ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2134ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2135ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2136ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2137da763e83SFam Zheng .clean = external_snapshot_clean, 2138ba0c86a3SWenchao Xia }, 21393037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21403037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21413037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 21423037f364SStefan Hajnoczi .abort = drive_backup_abort, 21435d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21443037f364SStefan Hajnoczi }, 2145bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2146bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2147bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2148bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2149bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2150bd8baecdSFam Zheng }, 215178b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 215250f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 215378b18b78SStefan Hajnoczi .prepare = abort_prepare, 215478b18b78SStefan Hajnoczi .commit = abort_commit, 215578b18b78SStefan Hajnoczi }, 2156bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2157bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2158bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2159bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21605d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2161bbe86010SWenchao Xia }, 2162df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2163df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2164df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2165df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2166df9a681dSFam Zheng }, 2167df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2168df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2169df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2170df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2171df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2172df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2173df9a681dSFam Zheng } 2174ba0c86a3SWenchao Xia }; 2175ba0c86a3SWenchao Xia 217694d16a64SJohn Snow /** 217794d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 217894d16a64SJohn Snow * that structure with desired defaults if they are unset. 217994d16a64SJohn Snow */ 218094d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 218194d16a64SJohn Snow TransactionProperties *props) 218294d16a64SJohn Snow { 218394d16a64SJohn Snow if (!props) { 218494d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 218594d16a64SJohn Snow } 218694d16a64SJohn Snow 218794d16a64SJohn Snow if (!props->has_completion_mode) { 218894d16a64SJohn Snow props->has_completion_mode = true; 218994d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 219094d16a64SJohn Snow } 219194d16a64SJohn Snow 219294d16a64SJohn Snow return props; 219394d16a64SJohn Snow } 219494d16a64SJohn Snow 21958802d1fdSJeff Cody /* 2196b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2197b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 21988802d1fdSJeff Cody */ 219994d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 220094d16a64SJohn Snow bool has_props, 220194d16a64SJohn Snow struct TransactionProperties *props, 220294d16a64SJohn Snow Error **errp) 22038802d1fdSJeff Cody { 2204c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 220594d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 220650f43f0fSJohn Snow BlkActionState *state, *next; 220743e17041SLuiz Capitulino Error *local_err = NULL; 22088802d1fdSJeff Cody 220950f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 22108802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 22118802d1fdSJeff Cody 221294d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 221394d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 221494d16a64SJohn Snow */ 221594d16a64SJohn Snow props = get_transaction_properties(props); 221694d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 221794d16a64SJohn Snow block_job_txn = block_job_txn_new(); 221894d16a64SJohn Snow } 221994d16a64SJohn Snow 2220b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 22218802d1fdSJeff Cody bdrv_drain_all(); 22228802d1fdSJeff Cody 2223b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 22248802d1fdSJeff Cody while (NULL != dev_entry) { 2225c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 222650f43f0fSJohn Snow const BlkActionOps *ops; 222752e7c241SPaolo Bonzini 22288802d1fdSJeff Cody dev_info = dev_entry->value; 22298802d1fdSJeff Cody dev_entry = dev_entry->next; 22308802d1fdSJeff Cody 22316a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2232ba0c86a3SWenchao Xia 22336a8f9661SEric Blake ops = &actions[dev_info->type]; 2234aa3fe714SMax Reitz assert(ops->instance_size > 0); 2235aa3fe714SMax Reitz 2236ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2237ba5d6ab6SStefan Hajnoczi state->ops = ops; 2238ba5d6ab6SStefan Hajnoczi state->action = dev_info; 223994d16a64SJohn Snow state->block_job_txn = block_job_txn; 224094d16a64SJohn Snow state->txn_props = props; 2241ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22428802d1fdSJeff Cody 2243ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 224484d18f06SMarkus Armbruster if (local_err) { 22459b9877eeSWenchao Xia error_propagate(errp, local_err); 22469b9877eeSWenchao Xia goto delete_and_fail; 22479b9877eeSWenchao Xia } 224852e7c241SPaolo Bonzini } 22498802d1fdSJeff Cody 2250ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2251f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2252ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22538802d1fdSJeff Cody } 2254f9ea81e8SStefan Hajnoczi } 22558802d1fdSJeff Cody 22568802d1fdSJeff Cody /* success */ 22578802d1fdSJeff Cody goto exit; 22588802d1fdSJeff Cody 22598802d1fdSJeff Cody delete_and_fail: 2260b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2261ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2262ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2263ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2264ba0c86a3SWenchao Xia } 22658802d1fdSJeff Cody } 22668802d1fdSJeff Cody exit: 2267ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2268ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2269ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2270ba0c86a3SWenchao Xia } 2271ba5d6ab6SStefan Hajnoczi g_free(state); 22728802d1fdSJeff Cody } 227394d16a64SJohn Snow if (!has_props) { 227494d16a64SJohn Snow qapi_free_TransactionProperties(props); 227594d16a64SJohn Snow } 227694d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22778802d1fdSJeff Cody } 22788802d1fdSJeff Cody 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); 2289*bf18bee5SColin Lord if (rc && rc != -ENOSYS) { 229038f54bd1SMax Reitz error_propagate(errp, local_err); 2291c245b6a3SLuiz Capitulino return; 2292666daa68SMarkus Armbruster } 2293*bf18bee5SColin Lord error_free(local_err); 22943a3086b7SJohn Snow 22956e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, errp); 2296666daa68SMarkus Armbruster } 2297666daa68SMarkus Armbruster 229812d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 229912d3ba82SBenoît Canet bool has_node_name, const char *node_name, 230012d3ba82SBenoît Canet const char *password, Error **errp) 2301666daa68SMarkus Armbruster { 230212d3ba82SBenoît Canet Error *local_err = NULL; 2303666daa68SMarkus Armbruster BlockDriverState *bs; 2304e3442099SStefan Hajnoczi AioContext *aio_context; 2305666daa68SMarkus Armbruster 230612d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 230712d3ba82SBenoît Canet has_node_name ? node_name : NULL, 230812d3ba82SBenoît Canet &local_err); 230984d18f06SMarkus Armbruster if (local_err) { 231012d3ba82SBenoît Canet error_propagate(errp, local_err); 2311a4dea8a9SLuiz Capitulino return; 2312666daa68SMarkus Armbruster } 2313666daa68SMarkus Armbruster 2314e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2315e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2316e3442099SStefan Hajnoczi 23174d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2318666daa68SMarkus Armbruster 2319e3442099SStefan Hajnoczi aio_context_release(aio_context); 2320e3442099SStefan Hajnoczi } 2321e3442099SStefan Hajnoczi 2322*bf18bee5SColin Lord /* 2323*bf18bee5SColin Lord * Attempt to open the tray of @device. 2324*bf18bee5SColin Lord * If @force, ignore its tray lock. 2325*bf18bee5SColin Lord * Else, if the tray is locked, don't open it, but ask the guest to open it. 2326*bf18bee5SColin Lord * On error, store an error through @errp and return -errno. 2327*bf18bee5SColin Lord * If @device does not exist, return -ENODEV. 2328*bf18bee5SColin Lord * If it has no removable media, return -ENOTSUP. 2329*bf18bee5SColin Lord * If it has no tray, return -ENOSYS. 2330*bf18bee5SColin Lord * If the guest was asked to open the tray, return -EINPROGRESS. 2331*bf18bee5SColin Lord * Else, return 0. 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)) { 2351*bf18bee5SColin Lord error_setg(errp, "Device '%s' does not have a tray", device); 2352*bf18bee5SColin Lord 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) { 2369*bf18bee5SColin Lord error_setg(errp, "Device '%s' is locked and force was not specified, " 2370*bf18bee5SColin Lord "wait for tray to open and try again", device); 2371*bf18bee5SColin Lord return -EINPROGRESS; 23723a3086b7SJohn Snow } 23733a3086b7SJohn Snow 23743a3086b7SJohn Snow return 0; 23753a3086b7SJohn Snow } 23763a3086b7SJohn Snow 23773a3086b7SJohn Snow void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 23783a3086b7SJohn Snow Error **errp) 23793a3086b7SJohn Snow { 2380*bf18bee5SColin Lord Error *local_err = NULL; 2381*bf18bee5SColin Lord int rc; 2382*bf18bee5SColin Lord 23833a3086b7SJohn Snow if (!has_force) { 23843a3086b7SJohn Snow force = false; 23853a3086b7SJohn Snow } 2386*bf18bee5SColin Lord rc = do_open_tray(device, force, &local_err); 2387*bf18bee5SColin Lord if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { 2388*bf18bee5SColin Lord error_propagate(errp, local_err); 2389*bf18bee5SColin Lord return; 2390*bf18bee5SColin Lord } 2391*bf18bee5SColin Lord error_free(local_err); 23927d8a9f71SMax Reitz } 23937d8a9f71SMax Reitz 2394abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2395abaaf59dSMax Reitz { 2396abaaf59dSMax Reitz BlockBackend *blk; 2397abaaf59dSMax Reitz 2398abaaf59dSMax Reitz blk = blk_by_name(device); 2399abaaf59dSMax Reitz if (!blk) { 2400abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2401abaaf59dSMax Reitz "Device '%s' not found", device); 2402abaaf59dSMax Reitz return; 2403abaaf59dSMax Reitz } 2404abaaf59dSMax Reitz 2405abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2406abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2407abaaf59dSMax Reitz return; 2408abaaf59dSMax Reitz } 2409abaaf59dSMax Reitz 241012c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 241112c7ec87SMax Reitz /* Ignore this command on tray-less devices */ 241212c7ec87SMax Reitz return; 241312c7ec87SMax Reitz } 241412c7ec87SMax Reitz 2415abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2416abaaf59dSMax Reitz return; 2417abaaf59dSMax Reitz } 2418abaaf59dSMax Reitz 2419abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2420abaaf59dSMax Reitz } 2421abaaf59dSMax Reitz 24226e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 24232814f672SMax Reitz { 24242814f672SMax Reitz BlockBackend *blk; 24252814f672SMax Reitz BlockDriverState *bs; 24262814f672SMax Reitz AioContext *aio_context; 24272814f672SMax Reitz bool has_device; 24282814f672SMax Reitz 24292814f672SMax Reitz blk = blk_by_name(device); 24302814f672SMax Reitz if (!blk) { 24312814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 24322814f672SMax Reitz "Device '%s' not found", device); 24332814f672SMax Reitz return; 24342814f672SMax Reitz } 24352814f672SMax Reitz 24362814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 24372814f672SMax Reitz has_device = blk_get_attached_dev(blk); 24382814f672SMax Reitz 24392814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 24402814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 24412814f672SMax Reitz return; 24422814f672SMax Reitz } 24432814f672SMax Reitz 244412c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 24452814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 24462814f672SMax Reitz return; 24472814f672SMax Reitz } 24482814f672SMax Reitz 24492814f672SMax Reitz bs = blk_bs(blk); 24502814f672SMax Reitz if (!bs) { 24512814f672SMax Reitz return; 24522814f672SMax Reitz } 24532814f672SMax Reitz 24542814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 24552814f672SMax Reitz aio_context_acquire(aio_context); 24562814f672SMax Reitz 24572814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 24582814f672SMax Reitz goto out; 24592814f672SMax Reitz } 24602814f672SMax Reitz 24612814f672SMax Reitz blk_remove_bs(blk); 24622814f672SMax Reitz 246312c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 246412c7ec87SMax Reitz /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 246512c7ec87SMax Reitz * called at all); therefore, the medium needs to be ejected here. 246612c7ec87SMax Reitz * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 246712c7ec87SMax Reitz * value passed here (i.e. false). */ 246812c7ec87SMax Reitz blk_dev_change_media_cb(blk, false); 246912c7ec87SMax Reitz } 247012c7ec87SMax Reitz 24712814f672SMax Reitz out: 24722814f672SMax Reitz aio_context_release(aio_context); 24732814f672SMax Reitz } 24742814f672SMax Reitz 2475d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device, 2476d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2477d1299882SMax Reitz { 2478d1299882SMax Reitz BlockBackend *blk; 2479d1299882SMax Reitz bool has_device; 2480d1299882SMax Reitz 2481d1299882SMax Reitz blk = blk_by_name(device); 2482d1299882SMax Reitz if (!blk) { 2483d1299882SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2484d1299882SMax Reitz "Device '%s' not found", device); 2485d1299882SMax Reitz return; 2486d1299882SMax Reitz } 2487d1299882SMax Reitz 2488d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2489d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2490d1299882SMax Reitz 2491d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2492d1299882SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2493d1299882SMax Reitz return; 2494d1299882SMax Reitz } 2495d1299882SMax Reitz 249612c7ec87SMax Reitz if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2497d1299882SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2498d1299882SMax Reitz return; 2499d1299882SMax Reitz } 2500d1299882SMax Reitz 2501d1299882SMax Reitz if (blk_bs(blk)) { 2502d1299882SMax Reitz error_setg(errp, "There already is a medium in device '%s'", device); 2503d1299882SMax Reitz return; 2504d1299882SMax Reitz } 2505d1299882SMax Reitz 2506d1299882SMax Reitz blk_insert_bs(blk, bs); 2507d1299882SMax Reitz 250812c7ec87SMax Reitz if (!blk_dev_has_tray(blk)) { 250912c7ec87SMax Reitz /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 251012c7ec87SMax Reitz * called at all); therefore, the medium needs to be pushed into the 251112c7ec87SMax Reitz * slot here. 251212c7ec87SMax Reitz * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 251312c7ec87SMax Reitz * value passed here (i.e. true). */ 251412c7ec87SMax Reitz blk_dev_change_media_cb(blk, true); 251512c7ec87SMax Reitz } 2516d1299882SMax Reitz } 2517d1299882SMax Reitz 25186e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2519d1299882SMax Reitz Error **errp) 2520d1299882SMax Reitz { 2521d1299882SMax Reitz BlockDriverState *bs; 2522d1299882SMax Reitz 2523d1299882SMax Reitz bs = bdrv_find_node(node_name); 2524d1299882SMax Reitz if (!bs) { 2525d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2526d1299882SMax Reitz return; 2527d1299882SMax Reitz } 2528d1299882SMax Reitz 25291f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 2530d1299882SMax Reitz error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 25311f0c461bSKevin Wolf bdrv_get_parent_name(bs)); 2532d1299882SMax Reitz return; 2533d1299882SMax Reitz } 2534d1299882SMax Reitz 2535d1299882SMax Reitz qmp_blockdev_insert_anon_medium(device, bs, errp); 2536d1299882SMax Reitz } 2537d1299882SMax Reitz 253824fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename, 253924fb4133SMax Reitz bool has_format, const char *format, 254039ff43d9SMax Reitz bool has_read_only, 254139ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 254224fb4133SMax Reitz Error **errp) 2543de2c6c05SMax Reitz { 2544de2c6c05SMax Reitz BlockBackend *blk; 2545de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 25465b363937SMax Reitz int bdrv_flags; 2547de2c6c05SMax Reitz QDict *options = NULL; 2548de2c6c05SMax Reitz Error *err = NULL; 2549de2c6c05SMax Reitz 2550de2c6c05SMax Reitz blk = blk_by_name(device); 2551de2c6c05SMax Reitz if (!blk) { 2552de2c6c05SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2553de2c6c05SMax Reitz "Device '%s' not found", device); 2554de2c6c05SMax Reitz goto fail; 2555de2c6c05SMax Reitz } 2556de2c6c05SMax Reitz 2557de2c6c05SMax Reitz if (blk_bs(blk)) { 2558de2c6c05SMax Reitz blk_update_root_state(blk); 2559de2c6c05SMax Reitz } 2560de2c6c05SMax Reitz 2561de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2562156abc2fSAlyssa Milburn bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2563156abc2fSAlyssa Milburn BDRV_O_PROTOCOL); 2564de2c6c05SMax Reitz 256539ff43d9SMax Reitz if (!has_read_only) { 256639ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 256739ff43d9SMax Reitz } 256839ff43d9SMax Reitz 256939ff43d9SMax Reitz switch (read_only) { 257039ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 257139ff43d9SMax Reitz break; 257239ff43d9SMax Reitz 257339ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 257439ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 257539ff43d9SMax Reitz break; 257639ff43d9SMax Reitz 257739ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 257839ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 257939ff43d9SMax Reitz break; 258039ff43d9SMax Reitz 258139ff43d9SMax Reitz default: 258239ff43d9SMax Reitz abort(); 258339ff43d9SMax Reitz } 258439ff43d9SMax Reitz 258524fb4133SMax Reitz if (has_format) { 2586de2c6c05SMax Reitz options = qdict_new(); 2587de2c6c05SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2588de2c6c05SMax Reitz } 2589de2c6c05SMax Reitz 25905b363937SMax Reitz medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 25915b363937SMax Reitz if (!medium_bs) { 2592de2c6c05SMax Reitz goto fail; 2593de2c6c05SMax Reitz } 2594de2c6c05SMax Reitz 2595de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2596de2c6c05SMax Reitz if (err) { 2597de2c6c05SMax Reitz error_propagate(errp, err); 2598de2c6c05SMax Reitz goto fail; 2599de2c6c05SMax Reitz } 2600de2c6c05SMax Reitz 2601de2c6c05SMax Reitz qmp_blockdev_open_tray(device, false, false, &err); 2602de2c6c05SMax Reitz if (err) { 2603de2c6c05SMax Reitz error_propagate(errp, err); 2604de2c6c05SMax Reitz goto fail; 2605de2c6c05SMax Reitz } 2606de2c6c05SMax Reitz 26076e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, &err); 2608de2c6c05SMax Reitz if (err) { 2609de2c6c05SMax Reitz error_propagate(errp, err); 2610de2c6c05SMax Reitz goto fail; 2611de2c6c05SMax Reitz } 2612de2c6c05SMax Reitz 2613de2c6c05SMax Reitz qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2614de2c6c05SMax Reitz if (err) { 2615de2c6c05SMax Reitz error_propagate(errp, err); 2616de2c6c05SMax Reitz goto fail; 2617de2c6c05SMax Reitz } 2618de2c6c05SMax Reitz 2619a5614993SKevin Wolf blk_apply_root_state(blk, medium_bs); 2620a5614993SKevin Wolf 2621de2c6c05SMax Reitz qmp_blockdev_close_tray(device, errp); 2622de2c6c05SMax Reitz 2623de2c6c05SMax Reitz fail: 2624de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2625de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2626de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2627de2c6c05SMax Reitz bdrv_unref(medium_bs); 2628de2c6c05SMax Reitz } 2629de2c6c05SMax Reitz 2630727f005eSZhi Yong Wu /* throttling disk I/O limits */ 263180047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 26323e9fab69SBenoît Canet int64_t bps_wr, 26333e9fab69SBenoît Canet int64_t iops, 26343e9fab69SBenoît Canet int64_t iops_rd, 26353e9fab69SBenoît Canet int64_t iops_wr, 26363e9fab69SBenoît Canet bool has_bps_max, 26373e9fab69SBenoît Canet int64_t bps_max, 26383e9fab69SBenoît Canet bool has_bps_rd_max, 26393e9fab69SBenoît Canet int64_t bps_rd_max, 26403e9fab69SBenoît Canet bool has_bps_wr_max, 26413e9fab69SBenoît Canet int64_t bps_wr_max, 26423e9fab69SBenoît Canet bool has_iops_max, 26433e9fab69SBenoît Canet int64_t iops_max, 26443e9fab69SBenoît Canet bool has_iops_rd_max, 26453e9fab69SBenoît Canet int64_t iops_rd_max, 26463e9fab69SBenoît Canet bool has_iops_wr_max, 26472024c1dfSBenoît Canet int64_t iops_wr_max, 2648dce13204SAlberto Garcia bool has_bps_max_length, 2649dce13204SAlberto Garcia int64_t bps_max_length, 2650dce13204SAlberto Garcia bool has_bps_rd_max_length, 2651dce13204SAlberto Garcia int64_t bps_rd_max_length, 2652dce13204SAlberto Garcia bool has_bps_wr_max_length, 2653dce13204SAlberto Garcia int64_t bps_wr_max_length, 2654dce13204SAlberto Garcia bool has_iops_max_length, 2655dce13204SAlberto Garcia int64_t iops_max_length, 2656dce13204SAlberto Garcia bool has_iops_rd_max_length, 2657dce13204SAlberto Garcia int64_t iops_rd_max_length, 2658dce13204SAlberto Garcia bool has_iops_wr_max_length, 2659dce13204SAlberto Garcia int64_t iops_wr_max_length, 26602024c1dfSBenoît Canet bool has_iops_size, 266176f4afb4SAlberto Garcia int64_t iops_size, 266276f4afb4SAlberto Garcia bool has_group, 266376f4afb4SAlberto Garcia const char *group, Error **errp) 2664727f005eSZhi Yong Wu { 2665cc0681c4SBenoît Canet ThrottleConfig cfg; 2666727f005eSZhi Yong Wu BlockDriverState *bs; 2667a0e8544cSFam Zheng BlockBackend *blk; 2668b15446fdSStefan Hajnoczi AioContext *aio_context; 2669727f005eSZhi Yong Wu 2670a0e8544cSFam Zheng blk = blk_by_name(device); 2671a0e8544cSFam Zheng if (!blk) { 267275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 267375158ebbSMarkus Armbruster "Device '%s' not found", device); 267480047da5SLuiz Capitulino return; 2675727f005eSZhi Yong Wu } 26765433c24fSMax Reitz 26775433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 26785433c24fSMax Reitz aio_context_acquire(aio_context); 26795433c24fSMax Reitz 2680a0e8544cSFam Zheng bs = blk_bs(blk); 26815433c24fSMax Reitz if (!bs) { 26825433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 26835433c24fSMax Reitz goto out; 26845433c24fSMax Reitz } 2685727f005eSZhi Yong Wu 26861588ab5dSAlberto Garcia throttle_config_init(&cfg); 2687cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2688cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2689cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2690727f005eSZhi Yong Wu 2691cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2692cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2693cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2694cc0681c4SBenoît Canet 26953e9fab69SBenoît Canet if (has_bps_max) { 26963e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 26973e9fab69SBenoît Canet } 26983e9fab69SBenoît Canet if (has_bps_rd_max) { 26993e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 27003e9fab69SBenoît Canet } 27013e9fab69SBenoît Canet if (has_bps_wr_max) { 27023e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 27033e9fab69SBenoît Canet } 27043e9fab69SBenoît Canet if (has_iops_max) { 27053e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 27063e9fab69SBenoît Canet } 27073e9fab69SBenoît Canet if (has_iops_rd_max) { 27083e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 27093e9fab69SBenoît Canet } 27103e9fab69SBenoît Canet if (has_iops_wr_max) { 27113e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 27123e9fab69SBenoît Canet } 2713cc0681c4SBenoît Canet 2714dce13204SAlberto Garcia if (has_bps_max_length) { 2715dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = bps_max_length; 2716dce13204SAlberto Garcia } 2717dce13204SAlberto Garcia if (has_bps_rd_max_length) { 2718dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_READ].burst_length = bps_rd_max_length; 2719dce13204SAlberto Garcia } 2720dce13204SAlberto Garcia if (has_bps_wr_max_length) { 2721dce13204SAlberto Garcia cfg.buckets[THROTTLE_BPS_WRITE].burst_length = bps_wr_max_length; 2722dce13204SAlberto Garcia } 2723dce13204SAlberto Garcia if (has_iops_max_length) { 2724dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = iops_max_length; 2725dce13204SAlberto Garcia } 2726dce13204SAlberto Garcia if (has_iops_rd_max_length) { 2727dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_READ].burst_length = iops_rd_max_length; 2728dce13204SAlberto Garcia } 2729dce13204SAlberto Garcia if (has_iops_wr_max_length) { 2730dce13204SAlberto Garcia cfg.buckets[THROTTLE_OPS_WRITE].burst_length = iops_wr_max_length; 2731dce13204SAlberto Garcia } 2732dce13204SAlberto Garcia 27332024c1dfSBenoît Canet if (has_iops_size) { 27342024c1dfSBenoît Canet cfg.op_size = iops_size; 27352024c1dfSBenoît Canet } 2736cc0681c4SBenoît Canet 2737d5851089SAlberto Garcia if (!throttle_is_valid(&cfg, errp)) { 27385433c24fSMax Reitz goto out; 2739727f005eSZhi Yong Wu } 2740727f005eSZhi Yong Wu 274176f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 274276f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 274376f4afb4SAlberto Garcia * just update the throttling group. */ 274497148076SKevin Wolf if (!blk_get_public(blk)->throttle_state) { 274597148076SKevin Wolf blk_io_limits_enable(blk, has_group ? group : device); 274676f4afb4SAlberto Garcia } else if (has_group) { 274797148076SKevin Wolf blk_io_limits_update_group(blk, group); 2748727f005eSZhi Yong Wu } 274976f4afb4SAlberto Garcia /* Set the new throttling configuration */ 275097148076SKevin Wolf blk_set_io_limits(blk, &cfg); 275197148076SKevin Wolf } else if (blk_get_public(blk)->throttle_state) { 275276f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 275397148076SKevin Wolf blk_io_limits_disable(blk); 2754727f005eSZhi Yong Wu } 2755b15446fdSStefan Hajnoczi 27565433c24fSMax Reitz out: 2757b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2758727f005eSZhi Yong Wu } 2759727f005eSZhi Yong Wu 2760341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2761341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2762341ebc2fSJohn Snow Error **errp) 2763341ebc2fSJohn Snow { 2764341ebc2fSJohn Snow AioContext *aio_context; 2765341ebc2fSJohn Snow BlockDriverState *bs; 2766341ebc2fSJohn Snow 2767341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2768341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2769341ebc2fSJohn Snow return; 2770341ebc2fSJohn Snow } 2771341ebc2fSJohn Snow 2772341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2773341ebc2fSJohn Snow if (!bs) { 2774341ebc2fSJohn Snow return; 2775341ebc2fSJohn Snow } 2776341ebc2fSJohn Snow 2777341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2778341ebc2fSJohn Snow aio_context_acquire(aio_context); 2779341ebc2fSJohn Snow 2780341ebc2fSJohn Snow if (has_granularity) { 2781341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2782341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2783341ebc2fSJohn Snow "and at least 512"); 2784341ebc2fSJohn Snow goto out; 2785341ebc2fSJohn Snow } 2786341ebc2fSJohn Snow } else { 2787341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2788341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2789341ebc2fSJohn Snow } 2790341ebc2fSJohn Snow 2791341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2792341ebc2fSJohn Snow 2793341ebc2fSJohn Snow out: 2794341ebc2fSJohn Snow aio_context_release(aio_context); 2795341ebc2fSJohn Snow } 2796341ebc2fSJohn Snow 2797341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2798341ebc2fSJohn Snow Error **errp) 2799341ebc2fSJohn Snow { 2800341ebc2fSJohn Snow AioContext *aio_context; 2801341ebc2fSJohn Snow BlockDriverState *bs; 2802341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2803341ebc2fSJohn Snow 2804341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2805341ebc2fSJohn Snow if (!bitmap || !bs) { 2806341ebc2fSJohn Snow return; 2807341ebc2fSJohn Snow } 2808341ebc2fSJohn Snow 28099bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 28109bd2b08fSJohn Snow error_setg(errp, 28119bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 28129bd2b08fSJohn Snow name); 28139bd2b08fSJohn Snow goto out; 28149bd2b08fSJohn Snow } 281520dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2816341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2817341ebc2fSJohn Snow 28189bd2b08fSJohn Snow out: 2819341ebc2fSJohn Snow aio_context_release(aio_context); 2820341ebc2fSJohn Snow } 2821341ebc2fSJohn Snow 2822e74e6b78SJohn Snow /** 2823e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2824e74e6b78SJohn Snow * immediately after a full backup operation. 2825e74e6b78SJohn Snow */ 2826e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2827e74e6b78SJohn Snow Error **errp) 2828e74e6b78SJohn Snow { 2829e74e6b78SJohn Snow AioContext *aio_context; 2830e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2831e74e6b78SJohn Snow BlockDriverState *bs; 2832e74e6b78SJohn Snow 2833e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2834e74e6b78SJohn Snow if (!bitmap || !bs) { 2835e74e6b78SJohn Snow return; 2836e74e6b78SJohn Snow } 2837e74e6b78SJohn Snow 2838e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2839e74e6b78SJohn Snow error_setg(errp, 2840e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2841e74e6b78SJohn Snow name); 2842e74e6b78SJohn Snow goto out; 2843e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2844e74e6b78SJohn Snow error_setg(errp, 2845e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2846e74e6b78SJohn Snow name); 2847e74e6b78SJohn Snow goto out; 2848e74e6b78SJohn Snow } 2849e74e6b78SJohn Snow 2850df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2851e74e6b78SJohn Snow 2852e74e6b78SJohn Snow out: 2853e74e6b78SJohn Snow aio_context_release(aio_context); 2854e74e6b78SJohn Snow } 2855e74e6b78SJohn Snow 2856072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 28579063f814SRyan Harper { 28589063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 28597e7d56d9SMarkus Armbruster BlockBackend *blk; 28609063f814SRyan Harper BlockDriverState *bs; 28618ad4202bSStefan Hajnoczi AioContext *aio_context; 28623718d8abSFam Zheng Error *local_err = NULL; 28639063f814SRyan Harper 28642073d410SKevin Wolf bs = bdrv_find_node(id); 28652073d410SKevin Wolf if (bs) { 28662073d410SKevin Wolf qmp_x_blockdev_del(false, NULL, true, id, &local_err); 28672073d410SKevin Wolf if (local_err) { 28682073d410SKevin Wolf error_report_err(local_err); 28692073d410SKevin Wolf } 28702073d410SKevin Wolf return; 28712073d410SKevin Wolf } 28722073d410SKevin Wolf 28737e7d56d9SMarkus Armbruster blk = blk_by_name(id); 28747e7d56d9SMarkus Armbruster if (!blk) { 2875b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2876072ebe6bSMarkus Armbruster return; 28779063f814SRyan Harper } 28788ad4202bSStefan Hajnoczi 287926f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 288048f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 288148f364ddSMarkus Armbruster " is not supported"); 2882072ebe6bSMarkus Armbruster return; 288348f364ddSMarkus Armbruster } 288448f364ddSMarkus Armbruster 28855433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 28868ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 28878ad4202bSStefan Hajnoczi 28885433c24fSMax Reitz bs = blk_bs(blk); 28895433c24fSMax Reitz if (bs) { 28903718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2891565f65d2SMarkus Armbruster error_report_err(local_err); 28928ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2893072ebe6bSMarkus Armbruster return; 28948591675fSMarcelo Tosatti } 28959063f814SRyan Harper 2896938abd43SMax Reitz blk_remove_bs(blk); 28975433c24fSMax Reitz } 28989063f814SRyan Harper 28997c735873SMax Reitz /* Make the BlockBackend and the attached BlockDriverState anonymous */ 2900efaa7c4eSMax Reitz monitor_remove_blk(blk); 2901efaa7c4eSMax Reitz 29027c735873SMax Reitz /* If this BlockBackend has a device attached to it, its refcount will be 29037c735873SMax Reitz * decremented when the device is removed; otherwise we have to do so here. 2904d22b2f41SRyan Harper */ 2905a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 2906293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2907373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2908293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2909d22b2f41SRyan Harper } else { 2910b9fe8a7aSMarkus Armbruster blk_unref(blk); 2911d22b2f41SRyan Harper } 29129063f814SRyan Harper 29138ad4202bSStefan Hajnoczi aio_context_release(aio_context); 29149063f814SRyan Harper } 29156d4a2b3aSChristoph Hellwig 29163b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 29173b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 29183b1dbd11SBenoît Canet int64_t size, Error **errp) 29196d4a2b3aSChristoph Hellwig { 29203b1dbd11SBenoît Canet Error *local_err = NULL; 29216d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2922927e0e76SStefan Hajnoczi AioContext *aio_context; 29238732901eSKevin Wolf int ret; 29246d4a2b3aSChristoph Hellwig 29253b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 29263b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 29273b1dbd11SBenoît Canet &local_err); 292884d18f06SMarkus Armbruster if (local_err) { 29293b1dbd11SBenoît Canet error_propagate(errp, local_err); 29303b1dbd11SBenoît Canet return; 29313b1dbd11SBenoît Canet } 29323b1dbd11SBenoît Canet 2933927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2934927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2935927e0e76SStefan Hajnoczi 29363b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2937c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2938927e0e76SStefan Hajnoczi goto out; 29396d4a2b3aSChristoph Hellwig } 29406d4a2b3aSChristoph Hellwig 29416d4a2b3aSChristoph Hellwig if (size < 0) { 2942c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2943927e0e76SStefan Hajnoczi goto out; 29446d4a2b3aSChristoph Hellwig } 29456d4a2b3aSChristoph Hellwig 29469c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2947c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2948927e0e76SStefan Hajnoczi goto out; 29499c75e168SJeff Cody } 29509c75e168SJeff Cody 295192b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 295292b7a08dSPeter Lieven bdrv_drain_all(); 295392b7a08dSPeter Lieven 29548732901eSKevin Wolf ret = bdrv_truncate(bs, size); 29558732901eSKevin Wolf switch (ret) { 2956939a1cc3SStefan Hajnoczi case 0: 2957939a1cc3SStefan Hajnoczi break; 2958939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2959c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2960939a1cc3SStefan Hajnoczi break; 2961939a1cc3SStefan Hajnoczi case -ENOTSUP: 2962c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2963939a1cc3SStefan Hajnoczi break; 2964939a1cc3SStefan Hajnoczi case -EACCES: 296581e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2966939a1cc3SStefan Hajnoczi break; 2967939a1cc3SStefan Hajnoczi case -EBUSY: 2968c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2969939a1cc3SStefan Hajnoczi break; 2970939a1cc3SStefan Hajnoczi default: 29718732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2972939a1cc3SStefan Hajnoczi break; 29736d4a2b3aSChristoph Hellwig } 2974927e0e76SStefan Hajnoczi 2975927e0e76SStefan Hajnoczi out: 2976927e0e76SStefan Hajnoczi aio_context_release(aio_context); 29776d4a2b3aSChristoph Hellwig } 297812bd451fSStefan Hajnoczi 29799abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 298012bd451fSStefan Hajnoczi { 2981723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2982723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2983723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2984723c5d93SStefan Hajnoczi */ 2985723c5d93SStefan Hajnoczi 298612bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2987bcada37bSWenchao Xia const char *msg = NULL; 298812bd451fSStefan Hajnoczi 29899abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 299012bd451fSStefan Hajnoczi 299112bd451fSStefan Hajnoczi assert(bs->job); 2992bcada37bSWenchao Xia 299312bd451fSStefan Hajnoczi if (ret < 0) { 2994bcada37bSWenchao Xia msg = strerror(-ret); 299512bd451fSStefan Hajnoczi } 299612bd451fSStefan Hajnoczi 2997370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 2998bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 2999370521a1SStefan Hajnoczi } else { 3000bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 3001370521a1SStefan Hajnoczi } 300212bd451fSStefan Hajnoczi } 300312bd451fSStefan Hajnoczi 300413d8cc51SJeff Cody void qmp_block_stream(const char *device, 300513d8cc51SJeff Cody bool has_base, const char *base, 300613d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 300713d8cc51SJeff Cody bool has_speed, int64_t speed, 30081d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 30091d809098SPaolo Bonzini Error **errp) 301012bd451fSStefan Hajnoczi { 3011a0e8544cSFam Zheng BlockBackend *blk; 301212bd451fSStefan Hajnoczi BlockDriverState *bs; 3013c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 3014f3e69bebSStefan Hajnoczi AioContext *aio_context; 3015fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 301613d8cc51SJeff Cody const char *base_name = NULL; 301712bd451fSStefan Hajnoczi 30181d809098SPaolo Bonzini if (!has_on_error) { 30191d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 30201d809098SPaolo Bonzini } 30211d809098SPaolo Bonzini 3022a0e8544cSFam Zheng blk = blk_by_name(device); 3023a0e8544cSFam Zheng if (!blk) { 302475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 302575158ebbSMarkus Armbruster "Device '%s' not found", device); 302612bd451fSStefan Hajnoczi return; 302712bd451fSStefan Hajnoczi } 302812bd451fSStefan Hajnoczi 30295433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3030f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 3031f3e69bebSStefan Hajnoczi 30325433c24fSMax Reitz if (!blk_is_available(blk)) { 30335433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 30345433c24fSMax Reitz goto out; 30355433c24fSMax Reitz } 30365433c24fSMax Reitz bs = blk_bs(blk); 30375433c24fSMax Reitz 3038628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 3039f3e69bebSStefan Hajnoczi goto out; 3040628ff683SFam Zheng } 3041628ff683SFam Zheng 304213d8cc51SJeff Cody if (has_base) { 3043c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 3044c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 3045c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 3046f3e69bebSStefan Hajnoczi goto out; 304712bd451fSStefan Hajnoczi } 3048f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 304913d8cc51SJeff Cody base_name = base; 3050c8c3080fSMarcelo Tosatti } 305112bd451fSStefan Hajnoczi 305213d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 305313d8cc51SJeff Cody * file, and specifying one is therefore an error */ 305413d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 305513d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 305613d8cc51SJeff Cody "entire chain"); 3057f3e69bebSStefan Hajnoczi goto out; 305813d8cc51SJeff Cody } 305913d8cc51SJeff Cody 306013d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 306113d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 306213d8cc51SJeff Cody 306313d8cc51SJeff Cody stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 30641d809098SPaolo Bonzini on_error, block_job_cb, bs, &local_err); 306584d18f06SMarkus Armbruster if (local_err) { 3066fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 3067f3e69bebSStefan Hajnoczi goto out; 306812bd451fSStefan Hajnoczi } 306912bd451fSStefan Hajnoczi 307012bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 3071f3e69bebSStefan Hajnoczi 3072f3e69bebSStefan Hajnoczi out: 3073f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 307412bd451fSStefan Hajnoczi } 30752d47c6e9SStefan Hajnoczi 3076ed61fc10SJeff Cody void qmp_block_commit(const char *device, 30777676e2c5SJeff Cody bool has_base, const char *base, 30787676e2c5SJeff Cody bool has_top, const char *top, 307954e26900SJeff Cody bool has_backing_file, const char *backing_file, 3080ed61fc10SJeff Cody bool has_speed, int64_t speed, 3081ed61fc10SJeff Cody Error **errp) 3082ed61fc10SJeff Cody { 3083a0e8544cSFam Zheng BlockBackend *blk; 3084ed61fc10SJeff Cody BlockDriverState *bs; 3085ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 30869e85cd5cSStefan Hajnoczi AioContext *aio_context; 3087ed61fc10SJeff Cody Error *local_err = NULL; 3088ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 3089ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 3090ed61fc10SJeff Cody */ 309192aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3092ed61fc10SJeff Cody 309354504663SMax Reitz if (!has_speed) { 309454504663SMax Reitz speed = 0; 309554504663SMax Reitz } 309654504663SMax Reitz 30977676e2c5SJeff Cody /* Important Note: 30987676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 30997676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 31007676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 31017676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 3102a0e8544cSFam Zheng blk = blk_by_name(device); 3103a0e8544cSFam Zheng if (!blk) { 310475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 310575158ebbSMarkus Armbruster "Device '%s' not found", device); 3106ed61fc10SJeff Cody return; 3107ed61fc10SJeff Cody } 3108ed61fc10SJeff Cody 31095433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 31109e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 31119e85cd5cSStefan Hajnoczi 31125433c24fSMax Reitz if (!blk_is_available(blk)) { 31135433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 31145433c24fSMax Reitz goto out; 31155433c24fSMax Reitz } 31165433c24fSMax Reitz bs = blk_bs(blk); 31175433c24fSMax Reitz 3118bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 31199e85cd5cSStefan Hajnoczi goto out; 3120628ff683SFam Zheng } 3121628ff683SFam Zheng 3122ed61fc10SJeff Cody /* default top_bs is the active layer */ 3123ed61fc10SJeff Cody top_bs = bs; 3124ed61fc10SJeff Cody 31257676e2c5SJeff Cody if (has_top && top) { 3126ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3127ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3128ed61fc10SJeff Cody } 3129ed61fc10SJeff Cody } 3130ed61fc10SJeff Cody 3131ed61fc10SJeff Cody if (top_bs == NULL) { 3132ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 31339e85cd5cSStefan Hajnoczi goto out; 3134ed61fc10SJeff Cody } 3135ed61fc10SJeff Cody 31369e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 31379e85cd5cSStefan Hajnoczi 3138d5208c45SJeff Cody if (has_base && base) { 3139d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3140d5208c45SJeff Cody } else { 3141d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3142d5208c45SJeff Cody } 3143d5208c45SJeff Cody 3144d5208c45SJeff Cody if (base_bs == NULL) { 3145c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 31469e85cd5cSStefan Hajnoczi goto out; 3147d5208c45SJeff Cody } 3148d5208c45SJeff Cody 31499e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 31509e85cd5cSStefan Hajnoczi 3151bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3152bb00021dSFam Zheng goto out; 3153bb00021dSFam Zheng } 3154bb00021dSFam Zheng 31557676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 31567676e2c5SJeff Cody if (top_bs == base_bs) { 31577676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 31589e85cd5cSStefan Hajnoczi goto out; 31597676e2c5SJeff Cody } 31607676e2c5SJeff Cody 316120a63d2cSFam Zheng if (top_bs == bs) { 316254e26900SJeff Cody if (has_backing_file) { 316354e26900SJeff Cody error_setg(errp, "'backing-file' specified," 316454e26900SJeff Cody " but 'top' is the active layer"); 31659e85cd5cSStefan Hajnoczi goto out; 316654e26900SJeff Cody } 316720a63d2cSFam Zheng commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 316820a63d2cSFam Zheng bs, &local_err); 316920a63d2cSFam Zheng } else { 3170ed61fc10SJeff Cody commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 317154e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 317220a63d2cSFam Zheng } 3173ed61fc10SJeff Cody if (local_err != NULL) { 3174ed61fc10SJeff Cody error_propagate(errp, local_err); 31759e85cd5cSStefan Hajnoczi goto out; 3176ed61fc10SJeff Cody } 31779e85cd5cSStefan Hajnoczi 31789e85cd5cSStefan Hajnoczi out: 31799e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3180ed61fc10SJeff Cody } 3181ed61fc10SJeff Cody 318278f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 318399a9addfSStefan Hajnoczi bool has_format, const char *format, 3184b53169eaSStefan Hajnoczi enum MirrorSyncMode sync, 318599a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 318699a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 3187d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 318878f51fdeSJohn Snow bool has_on_source_error, 318978f51fdeSJohn Snow BlockdevOnError on_source_error, 319078f51fdeSJohn Snow bool has_on_target_error, 319178f51fdeSJohn Snow BlockdevOnError on_target_error, 319278f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 319399a9addfSStefan Hajnoczi { 3194a0e8544cSFam Zheng BlockBackend *blk; 319599a9addfSStefan Hajnoczi BlockDriverState *bs; 319699a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3197fc5d3f84SIan Main BlockDriverState *source = NULL; 3198d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3199761731b1SStefan Hajnoczi AioContext *aio_context; 3200e6641719SMax Reitz QDict *options = NULL; 320199a9addfSStefan Hajnoczi Error *local_err = NULL; 320299a9addfSStefan Hajnoczi int flags; 320399a9addfSStefan Hajnoczi int64_t size; 320499a9addfSStefan Hajnoczi 320599a9addfSStefan Hajnoczi if (!has_speed) { 320699a9addfSStefan Hajnoczi speed = 0; 320799a9addfSStefan Hajnoczi } 320899a9addfSStefan Hajnoczi if (!has_on_source_error) { 320999a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 321099a9addfSStefan Hajnoczi } 321199a9addfSStefan Hajnoczi if (!has_on_target_error) { 321299a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 321399a9addfSStefan Hajnoczi } 321499a9addfSStefan Hajnoczi if (!has_mode) { 321599a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 321699a9addfSStefan Hajnoczi } 321799a9addfSStefan Hajnoczi 3218a0e8544cSFam Zheng blk = blk_by_name(device); 3219a0e8544cSFam Zheng if (!blk) { 322075158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 322175158ebbSMarkus Armbruster "Device '%s' not found", device); 322299a9addfSStefan Hajnoczi return; 322399a9addfSStefan Hajnoczi } 322499a9addfSStefan Hajnoczi 32255433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3226761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3227761731b1SStefan Hajnoczi 3228c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 3229c29c1dd3SFam Zheng * do an early check redundantly. */ 32305433c24fSMax Reitz if (!blk_is_available(blk)) { 3231c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3232761731b1SStefan Hajnoczi goto out; 323399a9addfSStefan Hajnoczi } 32345433c24fSMax Reitz bs = blk_bs(blk); 323599a9addfSStefan Hajnoczi 323699a9addfSStefan Hajnoczi if (!has_format) { 323799a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 323899a9addfSStefan Hajnoczi } 323999a9addfSStefan Hajnoczi 3240c29c1dd3SFam Zheng /* Early check to avoid creating target */ 32413718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3242761731b1SStefan Hajnoczi goto out; 324399a9addfSStefan Hajnoczi } 324499a9addfSStefan Hajnoczi 324561de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 324699a9addfSStefan Hajnoczi 3247fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3248fc5d3f84SIan Main * on top of. */ 3249fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 3250760e0063SKevin Wolf source = backing_bs(bs); 3251fc5d3f84SIan Main if (!source) { 3252fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 3253fc5d3f84SIan Main } 3254fc5d3f84SIan Main } 3255fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 3256fc5d3f84SIan Main source = bs; 3257fc5d3f84SIan Main } 3258fc5d3f84SIan Main 325999a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 326099a9addfSStefan Hajnoczi if (size < 0) { 326199a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3262761731b1SStefan Hajnoczi goto out; 326399a9addfSStefan Hajnoczi } 326499a9addfSStefan Hajnoczi 326599a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 3266e6641719SMax Reitz assert(format); 3267fc5d3f84SIan Main if (source) { 3268fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 3269fc5d3f84SIan Main source->drv->format_name, NULL, 3270fc5d3f84SIan Main size, flags, &local_err, false); 3271fc5d3f84SIan Main } else { 3272fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 3273fc5d3f84SIan Main size, flags, &local_err, false); 3274fc5d3f84SIan Main } 327599a9addfSStefan Hajnoczi } 327699a9addfSStefan Hajnoczi 327784d18f06SMarkus Armbruster if (local_err) { 327899a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3279761731b1SStefan Hajnoczi goto out; 328099a9addfSStefan Hajnoczi } 328199a9addfSStefan Hajnoczi 3282e6641719SMax Reitz if (format) { 3283e6641719SMax Reitz options = qdict_new(); 3284e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3285e6641719SMax Reitz } 3286e6641719SMax Reitz 32875b363937SMax Reitz target_bs = bdrv_open(target, NULL, options, flags, errp); 32885b363937SMax Reitz if (!target_bs) { 3289761731b1SStefan Hajnoczi goto out; 329099a9addfSStefan Hajnoczi } 329199a9addfSStefan Hajnoczi 3292761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3293761731b1SStefan Hajnoczi 3294d58d8453SJohn Snow if (has_bitmap) { 3295d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3296d58d8453SJohn Snow if (!bmap) { 3297d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 32980702d3d8SMax Reitz bdrv_unref(target_bs); 3299d58d8453SJohn Snow goto out; 3300d58d8453SJohn Snow } 3301d58d8453SJohn Snow } 3302d58d8453SJohn Snow 3303d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, bmap, 3304d58d8453SJohn Snow on_source_error, on_target_error, 330578f51fdeSJohn Snow block_job_cb, bs, txn, &local_err); 33064f6fd349SFam Zheng bdrv_unref(target_bs); 33075c438bc6SKevin Wolf if (local_err != NULL) { 330899a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3309761731b1SStefan Hajnoczi goto out; 331099a9addfSStefan Hajnoczi } 3311761731b1SStefan Hajnoczi 3312761731b1SStefan Hajnoczi out: 3313761731b1SStefan Hajnoczi aio_context_release(aio_context); 331499a9addfSStefan Hajnoczi } 331599a9addfSStefan Hajnoczi 331678f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target, 331778f51fdeSJohn Snow bool has_format, const char *format, 331878f51fdeSJohn Snow enum MirrorSyncMode sync, 331978f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 332078f51fdeSJohn Snow bool has_speed, int64_t speed, 332178f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 332278f51fdeSJohn Snow bool has_on_source_error, BlockdevOnError on_source_error, 332378f51fdeSJohn Snow bool has_on_target_error, BlockdevOnError on_target_error, 332478f51fdeSJohn Snow Error **errp) 332578f51fdeSJohn Snow { 332678f51fdeSJohn Snow return do_drive_backup(device, target, has_format, format, sync, 332778f51fdeSJohn Snow has_mode, mode, has_speed, speed, 332878f51fdeSJohn Snow has_bitmap, bitmap, 332978f51fdeSJohn Snow has_on_source_error, on_source_error, 333078f51fdeSJohn Snow has_on_target_error, on_target_error, 333178f51fdeSJohn Snow NULL, errp); 333278f51fdeSJohn Snow } 333378f51fdeSJohn Snow 3334c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3335c13163fbSBenoît Canet { 3336d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3337c13163fbSBenoît Canet } 3338c13163fbSBenoît Canet 333978f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target, 3340c29c1dd3SFam Zheng enum MirrorSyncMode sync, 3341c29c1dd3SFam Zheng bool has_speed, int64_t speed, 3342c29c1dd3SFam Zheng bool has_on_source_error, 3343c29c1dd3SFam Zheng BlockdevOnError on_source_error, 3344c29c1dd3SFam Zheng bool has_on_target_error, 3345c29c1dd3SFam Zheng BlockdevOnError on_target_error, 334678f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 3347c29c1dd3SFam Zheng { 33480d978913SFam Zheng BlockBackend *blk; 3349c29c1dd3SFam Zheng BlockDriverState *bs; 3350c29c1dd3SFam Zheng BlockDriverState *target_bs; 3351c29c1dd3SFam Zheng Error *local_err = NULL; 3352c29c1dd3SFam Zheng AioContext *aio_context; 3353c29c1dd3SFam Zheng 3354c29c1dd3SFam Zheng if (!has_speed) { 3355c29c1dd3SFam Zheng speed = 0; 3356c29c1dd3SFam Zheng } 3357c29c1dd3SFam Zheng if (!has_on_source_error) { 3358c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3359c29c1dd3SFam Zheng } 3360c29c1dd3SFam Zheng if (!has_on_target_error) { 3361c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3362c29c1dd3SFam Zheng } 3363c29c1dd3SFam Zheng 3364a0e8544cSFam Zheng blk = blk_by_name(device); 3365a0e8544cSFam Zheng if (!blk) { 33665b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", device); 3367c29c1dd3SFam Zheng return; 3368c29c1dd3SFam Zheng } 3369c29c1dd3SFam Zheng 33705433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3371c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3372c29c1dd3SFam Zheng 33735433c24fSMax Reitz if (!blk_is_available(blk)) { 33745433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 33755433c24fSMax Reitz goto out; 33765433c24fSMax Reitz } 33775433c24fSMax Reitz bs = blk_bs(blk); 33785433c24fSMax Reitz 33790d978913SFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 33800d978913SFam Zheng if (!target_bs) { 3381c29c1dd3SFam Zheng goto out; 3382c29c1dd3SFam Zheng } 33835433c24fSMax Reitz 3384efd75567SFam Zheng if (bdrv_get_aio_context(target_bs) != aio_context) { 3385efd75567SFam Zheng if (!bdrv_has_blk(target_bs)) { 3386efd75567SFam Zheng /* The target BDS is not attached, we can safely move it to another 3387efd75567SFam Zheng * AioContext. */ 3388c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3389efd75567SFam Zheng } else { 3390efd75567SFam Zheng error_setg(errp, "Target is attached to a different thread from " 3391efd75567SFam Zheng "source."); 3392efd75567SFam Zheng goto out; 3393efd75567SFam Zheng } 3394efd75567SFam Zheng } 3395d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 339678f51fdeSJohn Snow on_target_error, block_job_cb, bs, txn, &local_err); 3397c29c1dd3SFam Zheng if (local_err != NULL) { 3398c29c1dd3SFam Zheng error_propagate(errp, local_err); 3399c29c1dd3SFam Zheng } 3400c29c1dd3SFam Zheng out: 3401c29c1dd3SFam Zheng aio_context_release(aio_context); 3402c29c1dd3SFam Zheng } 3403c29c1dd3SFam Zheng 340478f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target, 340578f51fdeSJohn Snow enum MirrorSyncMode sync, 340678f51fdeSJohn Snow bool has_speed, int64_t speed, 340778f51fdeSJohn Snow bool has_on_source_error, 340878f51fdeSJohn Snow BlockdevOnError on_source_error, 340978f51fdeSJohn Snow bool has_on_target_error, 341078f51fdeSJohn Snow BlockdevOnError on_target_error, 341178f51fdeSJohn Snow Error **errp) 341278f51fdeSJohn Snow { 341378f51fdeSJohn Snow do_blockdev_backup(device, target, sync, has_speed, speed, 341478f51fdeSJohn Snow has_on_source_error, on_source_error, 341578f51fdeSJohn Snow has_on_target_error, on_target_error, 341678f51fdeSJohn Snow NULL, errp); 341778f51fdeSJohn Snow } 341878f51fdeSJohn Snow 34194193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 34204193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 34214193cdd7SFam Zheng **/ 34224193cdd7SFam Zheng static void blockdev_mirror_common(BlockDriverState *bs, 34234193cdd7SFam Zheng BlockDriverState *target, 342409158f00SBenoît Canet bool has_replaces, const char *replaces, 3425d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3426b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3427eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 342808e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 34294193cdd7SFam Zheng bool has_on_source_error, 34304193cdd7SFam Zheng BlockdevOnError on_source_error, 34314193cdd7SFam Zheng bool has_on_target_error, 34324193cdd7SFam Zheng BlockdevOnError on_target_error, 34330fc9f8eaSFam Zheng bool has_unmap, bool unmap, 3434b952b558SPaolo Bonzini Error **errp) 3435d9b902dbSPaolo Bonzini { 3436d9b902dbSPaolo Bonzini 3437d9b902dbSPaolo Bonzini if (!has_speed) { 3438d9b902dbSPaolo Bonzini speed = 0; 3439d9b902dbSPaolo Bonzini } 3440b952b558SPaolo Bonzini if (!has_on_source_error) { 3441b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3442b952b558SPaolo Bonzini } 3443b952b558SPaolo Bonzini if (!has_on_target_error) { 3444b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3445b952b558SPaolo Bonzini } 3446eee13dfeSPaolo Bonzini if (!has_granularity) { 3447eee13dfeSPaolo Bonzini granularity = 0; 3448eee13dfeSPaolo Bonzini } 344908e4ed6cSPaolo Bonzini if (!has_buf_size) { 345048ac0a4dSWen Congyang buf_size = 0; 345108e4ed6cSPaolo Bonzini } 34520fc9f8eaSFam Zheng if (!has_unmap) { 34530fc9f8eaSFam Zheng unmap = true; 34540fc9f8eaSFam Zheng } 345508e4ed6cSPaolo Bonzini 3456eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3457c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 34583cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3459eee13dfeSPaolo Bonzini return; 3460eee13dfeSPaolo Bonzini } 3461eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3462c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3463c6bd8c70SMarkus Armbruster "power of 2"); 3464eee13dfeSPaolo Bonzini return; 3465eee13dfeSPaolo Bonzini } 3466d9b902dbSPaolo Bonzini 34674193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 34684193cdd7SFam Zheng return; 34694193cdd7SFam Zheng } 3470e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3471e40e5027SFam Zheng return; 3472e40e5027SFam Zheng } 34734193cdd7SFam Zheng 34744193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 34754193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 34764193cdd7SFam Zheng } 34774193cdd7SFam Zheng 34784193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 34794193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 34804193cdd7SFam Zheng */ 34814193cdd7SFam Zheng mirror_start(bs, target, 34824193cdd7SFam Zheng has_replaces ? replaces : NULL, 34834193cdd7SFam Zheng speed, granularity, buf_size, sync, 34844193cdd7SFam Zheng on_source_error, on_target_error, unmap, 34854193cdd7SFam Zheng block_job_cb, bs, errp); 34864193cdd7SFam Zheng } 34874193cdd7SFam Zheng 34884193cdd7SFam Zheng void qmp_drive_mirror(const char *device, const char *target, 34894193cdd7SFam Zheng bool has_format, const char *format, 34904193cdd7SFam Zheng bool has_node_name, const char *node_name, 34914193cdd7SFam Zheng bool has_replaces, const char *replaces, 34924193cdd7SFam Zheng enum MirrorSyncMode sync, 34934193cdd7SFam Zheng bool has_mode, enum NewImageMode mode, 34944193cdd7SFam Zheng bool has_speed, int64_t speed, 34954193cdd7SFam Zheng bool has_granularity, uint32_t granularity, 34964193cdd7SFam Zheng bool has_buf_size, int64_t buf_size, 34974193cdd7SFam Zheng bool has_on_source_error, BlockdevOnError on_source_error, 34984193cdd7SFam Zheng bool has_on_target_error, BlockdevOnError on_target_error, 34994193cdd7SFam Zheng bool has_unmap, bool unmap, 35004193cdd7SFam Zheng Error **errp) 35014193cdd7SFam Zheng { 35024193cdd7SFam Zheng BlockDriverState *bs; 35034193cdd7SFam Zheng BlockBackend *blk; 35044193cdd7SFam Zheng BlockDriverState *source, *target_bs; 35054193cdd7SFam Zheng AioContext *aio_context; 35064193cdd7SFam Zheng Error *local_err = NULL; 35074193cdd7SFam Zheng QDict *options = NULL; 35084193cdd7SFam Zheng int flags; 35094193cdd7SFam Zheng int64_t size; 35104193cdd7SFam Zheng 3511a0e8544cSFam Zheng blk = blk_by_name(device); 3512a0e8544cSFam Zheng if (!blk) { 351375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 351475158ebbSMarkus Armbruster "Device '%s' not found", device); 3515d9b902dbSPaolo Bonzini return; 3516d9b902dbSPaolo Bonzini } 3517d9b902dbSPaolo Bonzini 35185433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 35195a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 35205a7e7a0bSStefan Hajnoczi 35215433c24fSMax Reitz if (!blk_is_available(blk)) { 3522c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 35235a7e7a0bSStefan Hajnoczi goto out; 3524d9b902dbSPaolo Bonzini } 35255433c24fSMax Reitz bs = blk_bs(blk); 35264193cdd7SFam Zheng if (!has_mode) { 35274193cdd7SFam Zheng mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 35284193cdd7SFam Zheng } 3529d9b902dbSPaolo Bonzini 3530d9b902dbSPaolo Bonzini if (!has_format) { 3531d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3532d9b902dbSPaolo Bonzini } 3533d9b902dbSPaolo Bonzini 353461de4c68SKevin Wolf flags = bs->open_flags | BDRV_O_RDWR; 3535760e0063SKevin Wolf source = backing_bs(bs); 3536d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3537d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 3538d9b902dbSPaolo Bonzini } 3539117e0c82SMax Reitz if (sync == MIRROR_SYNC_MODE_NONE) { 3540117e0c82SMax Reitz source = bs; 3541117e0c82SMax Reitz } 3542d9b902dbSPaolo Bonzini 3543ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3544ac3c5d83SStefan Hajnoczi if (size < 0) { 3545ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 35465a7e7a0bSStefan Hajnoczi goto out; 3547ac3c5d83SStefan Hajnoczi } 3548ac3c5d83SStefan Hajnoczi 354909158f00SBenoît Canet if (has_replaces) { 355009158f00SBenoît Canet BlockDriverState *to_replace_bs; 35515a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 35525a7e7a0bSStefan Hajnoczi int64_t replace_size; 355309158f00SBenoît Canet 355409158f00SBenoît Canet if (!has_node_name) { 355509158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 355609158f00SBenoît Canet " named node of the graph"); 35575a7e7a0bSStefan Hajnoczi goto out; 355809158f00SBenoît Canet } 355909158f00SBenoît Canet 3560e12f3784SWen Congyang to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 356109158f00SBenoît Canet 356209158f00SBenoît Canet if (!to_replace_bs) { 356309158f00SBenoît Canet error_propagate(errp, local_err); 35645a7e7a0bSStefan Hajnoczi goto out; 356509158f00SBenoît Canet } 356609158f00SBenoît Canet 35675a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 35685a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 35695a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 35705a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 35715a7e7a0bSStefan Hajnoczi 35725a7e7a0bSStefan Hajnoczi if (size != replace_size) { 357309158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 357409158f00SBenoît Canet "different size"); 35755a7e7a0bSStefan Hajnoczi goto out; 357609158f00SBenoît Canet } 357709158f00SBenoît Canet } 357809158f00SBenoît Canet 357914526864SMax Reitz if ((sync == MIRROR_SYNC_MODE_FULL || !source) 358014526864SMax Reitz && mode != NEW_IMAGE_MODE_EXISTING) 358114526864SMax Reitz { 3582d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3583e6641719SMax Reitz assert(format); 3584cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3585f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3586d9b902dbSPaolo Bonzini } else { 3587d9b902dbSPaolo Bonzini switch (mode) { 3588d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3589d9b902dbSPaolo Bonzini break; 3590d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3591d9b902dbSPaolo Bonzini /* create new image with backing file */ 3592cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3593d9b902dbSPaolo Bonzini source->filename, 3594d9b902dbSPaolo Bonzini source->drv->format_name, 3595f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3596d9b902dbSPaolo Bonzini break; 3597d9b902dbSPaolo Bonzini default: 3598d9b902dbSPaolo Bonzini abort(); 3599d9b902dbSPaolo Bonzini } 3600d9b902dbSPaolo Bonzini } 3601d9b902dbSPaolo Bonzini 360284d18f06SMarkus Armbruster if (local_err) { 3603cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 36045a7e7a0bSStefan Hajnoczi goto out; 3605d9b902dbSPaolo Bonzini } 3606d9b902dbSPaolo Bonzini 36074c828dc6SBenoît Canet options = qdict_new(); 3608e6641719SMax Reitz if (has_node_name) { 36094c828dc6SBenoît Canet qdict_put(options, "node-name", qstring_from_str(node_name)); 36104c828dc6SBenoît Canet } 3611e6641719SMax Reitz if (format) { 3612e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3613e6641719SMax Reitz } 36144c828dc6SBenoît Canet 3615b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3616b812f671SPaolo Bonzini * file. 3617b812f671SPaolo Bonzini */ 36185b363937SMax Reitz target_bs = bdrv_open(target, NULL, options, flags | BDRV_O_NO_BACKING, 36195b363937SMax Reitz errp); 36205b363937SMax Reitz if (!target_bs) { 36215a7e7a0bSStefan Hajnoczi goto out; 3622d9b902dbSPaolo Bonzini } 3623d9b902dbSPaolo Bonzini 36245a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 36255a7e7a0bSStefan Hajnoczi 36264193cdd7SFam Zheng blockdev_mirror_common(bs, target_bs, 36274193cdd7SFam Zheng has_replaces, replaces, sync, 36284193cdd7SFam Zheng has_speed, speed, 36294193cdd7SFam Zheng has_granularity, granularity, 36304193cdd7SFam Zheng has_buf_size, buf_size, 36314193cdd7SFam Zheng has_on_source_error, on_source_error, 36324193cdd7SFam Zheng has_on_target_error, on_target_error, 36334193cdd7SFam Zheng has_unmap, unmap, 36344193cdd7SFam Zheng &local_err); 3635e253f4b8SKevin Wolf bdrv_unref(target_bs); 36364193cdd7SFam Zheng if (local_err) { 3637d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 3638d9b902dbSPaolo Bonzini } 36395a7e7a0bSStefan Hajnoczi out: 36405a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3641d9b902dbSPaolo Bonzini } 3642d9b902dbSPaolo Bonzini 3643df92562eSFam Zheng void qmp_blockdev_mirror(const char *device, const char *target, 3644df92562eSFam Zheng bool has_replaces, const char *replaces, 3645df92562eSFam Zheng MirrorSyncMode sync, 3646df92562eSFam Zheng bool has_speed, int64_t speed, 3647df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3648df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3649df92562eSFam Zheng bool has_on_source_error, 3650df92562eSFam Zheng BlockdevOnError on_source_error, 3651df92562eSFam Zheng bool has_on_target_error, 3652df92562eSFam Zheng BlockdevOnError on_target_error, 3653df92562eSFam Zheng Error **errp) 3654df92562eSFam Zheng { 3655df92562eSFam Zheng BlockDriverState *bs; 3656df92562eSFam Zheng BlockBackend *blk; 3657df92562eSFam Zheng BlockDriverState *target_bs; 3658df92562eSFam Zheng AioContext *aio_context; 3659df92562eSFam Zheng Error *local_err = NULL; 3660df92562eSFam Zheng 3661df92562eSFam Zheng blk = blk_by_name(device); 3662df92562eSFam Zheng if (!blk) { 3663df92562eSFam Zheng error_setg(errp, "Device '%s' not found", device); 3664df92562eSFam Zheng return; 3665df92562eSFam Zheng } 3666df92562eSFam Zheng bs = blk_bs(blk); 3667df92562eSFam Zheng 3668df92562eSFam Zheng if (!bs) { 3669df92562eSFam Zheng error_setg(errp, "Device '%s' has no media", device); 3670df92562eSFam Zheng return; 3671df92562eSFam Zheng } 3672df92562eSFam Zheng 3673df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3674df92562eSFam Zheng if (!target_bs) { 3675df92562eSFam Zheng return; 3676df92562eSFam Zheng } 3677df92562eSFam Zheng 3678df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3679df92562eSFam Zheng aio_context_acquire(aio_context); 3680df92562eSFam Zheng 3681df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3682df92562eSFam Zheng 3683df92562eSFam Zheng blockdev_mirror_common(bs, target_bs, 3684df92562eSFam Zheng has_replaces, replaces, sync, 3685df92562eSFam Zheng has_speed, speed, 3686df92562eSFam Zheng has_granularity, granularity, 3687df92562eSFam Zheng has_buf_size, buf_size, 3688df92562eSFam Zheng has_on_source_error, on_source_error, 3689df92562eSFam Zheng has_on_target_error, on_target_error, 3690df92562eSFam Zheng true, true, 3691df92562eSFam Zheng &local_err); 3692df92562eSFam Zheng if (local_err) { 3693df92562eSFam Zheng error_propagate(errp, local_err); 3694df92562eSFam Zheng } 3695df92562eSFam Zheng 3696df92562eSFam Zheng aio_context_release(aio_context); 3697df92562eSFam Zheng } 3698df92562eSFam Zheng 36993d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */ 370024d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context, 370124d6bffeSMarkus Armbruster Error **errp) 37022d47c6e9SStefan Hajnoczi { 3703a0e8544cSFam Zheng BlockBackend *blk; 37042d47c6e9SStefan Hajnoczi BlockDriverState *bs; 37052d47c6e9SStefan Hajnoczi 37065433c24fSMax Reitz *aio_context = NULL; 37075433c24fSMax Reitz 3708a0e8544cSFam Zheng blk = blk_by_name(device); 3709a0e8544cSFam Zheng if (!blk) { 37103d948cdfSStefan Hajnoczi goto notfound; 37112d47c6e9SStefan Hajnoczi } 37123d948cdfSStefan Hajnoczi 37135433c24fSMax Reitz *aio_context = blk_get_aio_context(blk); 37143d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 37153d948cdfSStefan Hajnoczi 37165433c24fSMax Reitz if (!blk_is_available(blk)) { 37175433c24fSMax Reitz goto notfound; 37185433c24fSMax Reitz } 37195433c24fSMax Reitz bs = blk_bs(blk); 37205433c24fSMax Reitz 37213d948cdfSStefan Hajnoczi if (!bs->job) { 37223d948cdfSStefan Hajnoczi goto notfound; 37233d948cdfSStefan Hajnoczi } 37243d948cdfSStefan Hajnoczi 37252d47c6e9SStefan Hajnoczi return bs->job; 37263d948cdfSStefan Hajnoczi 37273d948cdfSStefan Hajnoczi notfound: 37282e3a0266SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 37292e3a0266SMarkus Armbruster "No active block job on device '%s'", device); 37305433c24fSMax Reitz if (*aio_context) { 37315433c24fSMax Reitz aio_context_release(*aio_context); 37323d948cdfSStefan Hajnoczi *aio_context = NULL; 37335433c24fSMax Reitz } 37343d948cdfSStefan Hajnoczi return NULL; 37352d47c6e9SStefan Hajnoczi } 37362d47c6e9SStefan Hajnoczi 3737882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 37382d47c6e9SStefan Hajnoczi { 37393d948cdfSStefan Hajnoczi AioContext *aio_context; 374024d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37412d47c6e9SStefan Hajnoczi 37422d47c6e9SStefan Hajnoczi if (!job) { 37432d47c6e9SStefan Hajnoczi return; 37442d47c6e9SStefan Hajnoczi } 37452d47c6e9SStefan Hajnoczi 3746882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 37473d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37482d47c6e9SStefan Hajnoczi } 3749370521a1SStefan Hajnoczi 37506e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 37516e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 37526e37fb81SPaolo Bonzini { 37533d948cdfSStefan Hajnoczi AioContext *aio_context; 375424d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37556e37fb81SPaolo Bonzini 37566e37fb81SPaolo Bonzini if (!job) { 37576e37fb81SPaolo Bonzini return; 37586e37fb81SPaolo Bonzini } 37593d948cdfSStefan Hajnoczi 37603d948cdfSStefan Hajnoczi if (!has_force) { 37613d948cdfSStefan Hajnoczi force = false; 37623d948cdfSStefan Hajnoczi } 37633d948cdfSStefan Hajnoczi 3764751ebd76SFam Zheng if (job->user_paused && !force) { 3765f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3766f231b88dSCole Robinson device); 37673d948cdfSStefan Hajnoczi goto out; 37686e37fb81SPaolo Bonzini } 37696e37fb81SPaolo Bonzini 37706e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 37716e37fb81SPaolo Bonzini block_job_cancel(job); 37723d948cdfSStefan Hajnoczi out: 37733d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37746e37fb81SPaolo Bonzini } 37756e37fb81SPaolo Bonzini 37766e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3777370521a1SStefan Hajnoczi { 37783d948cdfSStefan Hajnoczi AioContext *aio_context; 377924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3780370521a1SStefan Hajnoczi 3781751ebd76SFam Zheng if (!job || job->user_paused) { 3782370521a1SStefan Hajnoczi return; 3783370521a1SStefan Hajnoczi } 37846e37fb81SPaolo Bonzini 3785751ebd76SFam Zheng job->user_paused = true; 37866e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 37876e37fb81SPaolo Bonzini block_job_pause(job); 37883d948cdfSStefan Hajnoczi aio_context_release(aio_context); 37896e37fb81SPaolo Bonzini } 37906e37fb81SPaolo Bonzini 37916e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 37926e37fb81SPaolo Bonzini { 37933d948cdfSStefan Hajnoczi AioContext *aio_context; 379424d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 37956e37fb81SPaolo Bonzini 3796751ebd76SFam Zheng if (!job || !job->user_paused) { 37978acc72a4SPaolo Bonzini return; 37988acc72a4SPaolo Bonzini } 3799370521a1SStefan Hajnoczi 3800751ebd76SFam Zheng job->user_paused = false; 38016e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 38026e37fb81SPaolo Bonzini block_job_resume(job); 38033d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3804370521a1SStefan Hajnoczi } 3805fb5458cdSStefan Hajnoczi 3806aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3807aeae883bSPaolo Bonzini { 38083d948cdfSStefan Hajnoczi AioContext *aio_context; 380924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3810aeae883bSPaolo Bonzini 3811aeae883bSPaolo Bonzini if (!job) { 3812aeae883bSPaolo Bonzini return; 3813aeae883bSPaolo Bonzini } 3814aeae883bSPaolo Bonzini 3815aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3816aeae883bSPaolo Bonzini block_job_complete(job, errp); 38173d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3818aeae883bSPaolo Bonzini } 3819aeae883bSPaolo Bonzini 3820fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3821fa40e656SJeff Cody const char *image_node_name, 3822fa40e656SJeff Cody const char *backing_file, 3823fa40e656SJeff Cody Error **errp) 3824fa40e656SJeff Cody { 3825a0e8544cSFam Zheng BlockBackend *blk; 3826fa40e656SJeff Cody BlockDriverState *bs = NULL; 3827729962f6SStefan Hajnoczi AioContext *aio_context; 3828fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3829fa40e656SJeff Cody Error *local_err = NULL; 3830fa40e656SJeff Cody bool ro; 3831fa40e656SJeff Cody int open_flags; 3832fa40e656SJeff Cody int ret; 3833fa40e656SJeff Cody 3834a0e8544cSFam Zheng blk = blk_by_name(device); 3835a0e8544cSFam Zheng if (!blk) { 383675158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 383775158ebbSMarkus Armbruster "Device '%s' not found", device); 3838fa40e656SJeff Cody return; 3839fa40e656SJeff Cody } 3840fa40e656SJeff Cody 38415433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3842729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3843729962f6SStefan Hajnoczi 38445433c24fSMax Reitz if (!blk_is_available(blk)) { 38455433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 38465433c24fSMax Reitz goto out; 38475433c24fSMax Reitz } 38485433c24fSMax Reitz bs = blk_bs(blk); 38495433c24fSMax Reitz 3850fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3851fa40e656SJeff Cody if (local_err) { 3852fa40e656SJeff Cody error_propagate(errp, local_err); 3853729962f6SStefan Hajnoczi goto out; 3854fa40e656SJeff Cody } 3855fa40e656SJeff Cody 3856fa40e656SJeff Cody if (!image_bs) { 3857fa40e656SJeff Cody error_setg(errp, "image file not found"); 3858729962f6SStefan Hajnoczi goto out; 3859fa40e656SJeff Cody } 3860fa40e656SJeff Cody 3861fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3862fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3863fa40e656SJeff Cody "without a backing file"); 3864729962f6SStefan Hajnoczi goto out; 3865fa40e656SJeff Cody } 3866fa40e656SJeff Cody 3867fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3868fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3869fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3870729962f6SStefan Hajnoczi goto out; 3871fa40e656SJeff Cody } 3872fa40e656SJeff Cody 3873fa40e656SJeff Cody /* final sanity check */ 3874fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3875fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3876fa40e656SJeff Cody device); 3877729962f6SStefan Hajnoczi goto out; 3878fa40e656SJeff Cody } 3879fa40e656SJeff Cody 3880fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3881fa40e656SJeff Cody open_flags = image_bs->open_flags; 3882fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3883fa40e656SJeff Cody 3884fa40e656SJeff Cody if (ro) { 3885fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3886fa40e656SJeff Cody if (local_err) { 3887fa40e656SJeff Cody error_propagate(errp, local_err); 3888729962f6SStefan Hajnoczi goto out; 3889fa40e656SJeff Cody } 3890fa40e656SJeff Cody } 3891fa40e656SJeff Cody 3892fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3893fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3894fa40e656SJeff Cody 3895fa40e656SJeff Cody if (ret < 0) { 3896fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3897fa40e656SJeff Cody backing_file); 3898fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3899fa40e656SJeff Cody * appropriate */ 3900fa40e656SJeff Cody } 3901fa40e656SJeff Cody 3902fa40e656SJeff Cody if (ro) { 3903fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3904fa40e656SJeff Cody if (local_err) { 3905fa40e656SJeff Cody error_propagate(errp, local_err); /* will preserve prior errp */ 3906fa40e656SJeff Cody } 3907fa40e656SJeff Cody } 3908729962f6SStefan Hajnoczi 3909729962f6SStefan Hajnoczi out: 3910729962f6SStefan Hajnoczi aio_context_release(aio_context); 3911fa40e656SJeff Cody } 3912fa40e656SJeff Cody 3913abb21ac3SKevin Wolf void hmp_drive_add_node(Monitor *mon, const char *optstr) 3914abb21ac3SKevin Wolf { 3915abb21ac3SKevin Wolf QemuOpts *opts; 3916abb21ac3SKevin Wolf QDict *qdict; 3917abb21ac3SKevin Wolf Error *local_err = NULL; 3918abb21ac3SKevin Wolf 3919abb21ac3SKevin Wolf opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 3920abb21ac3SKevin Wolf if (!opts) { 3921abb21ac3SKevin Wolf return; 3922abb21ac3SKevin Wolf } 3923abb21ac3SKevin Wolf 3924abb21ac3SKevin Wolf qdict = qemu_opts_to_qdict(opts, NULL); 3925abb21ac3SKevin Wolf 3926abb21ac3SKevin Wolf if (!qdict_get_try_str(qdict, "node-name")) { 3927f8746fb8SKevin Wolf QDECREF(qdict); 3928abb21ac3SKevin Wolf error_report("'node-name' needs to be specified"); 3929abb21ac3SKevin Wolf goto out; 3930abb21ac3SKevin Wolf } 3931abb21ac3SKevin Wolf 3932abb21ac3SKevin Wolf BlockDriverState *bs = bds_tree_init(qdict, &local_err); 3933abb21ac3SKevin Wolf if (!bs) { 3934abb21ac3SKevin Wolf error_report_err(local_err); 3935abb21ac3SKevin Wolf goto out; 3936abb21ac3SKevin Wolf } 3937abb21ac3SKevin Wolf 3938abb21ac3SKevin Wolf QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3939abb21ac3SKevin Wolf 3940abb21ac3SKevin Wolf out: 3941abb21ac3SKevin Wolf qemu_opts_del(opts); 3942abb21ac3SKevin Wolf } 3943abb21ac3SKevin Wolf 3944d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3945d26c9a15SKevin Wolf { 3946d26c9a15SKevin Wolf QmpOutputVisitor *ov = qmp_output_visitor_new(); 3947be4b67bcSMax Reitz BlockDriverState *bs; 3948be4b67bcSMax Reitz BlockBackend *blk = NULL; 3949d26c9a15SKevin Wolf QObject *obj; 3950d26c9a15SKevin Wolf QDict *qdict; 3951d26c9a15SKevin Wolf Error *local_err = NULL; 3952d26c9a15SKevin Wolf 395360e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3954d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 395560e19e06SMarkus Armbruster * when called from drive_new(). 3956d26c9a15SKevin Wolf * 3957d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3958d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3959c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3960c6e0bd9bSKevin Wolf options->cache->has_direct && 3961c6e0bd9bSKevin Wolf options->cache->direct; 3962c6e0bd9bSKevin Wolf if (!direct) { 3963d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3964d26c9a15SKevin Wolf goto fail; 3965d26c9a15SKevin Wolf } 3966d26c9a15SKevin Wolf } 3967d26c9a15SKevin Wolf 396851e72bc1SEric Blake visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options, 396951e72bc1SEric Blake &local_err); 397084d18f06SMarkus Armbruster if (local_err) { 3971d26c9a15SKevin Wolf error_propagate(errp, local_err); 3972d26c9a15SKevin Wolf goto fail; 3973d26c9a15SKevin Wolf } 3974d26c9a15SKevin Wolf 3975d26c9a15SKevin Wolf obj = qmp_output_get_qobject(ov); 3976d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3977d26c9a15SKevin Wolf 3978d26c9a15SKevin Wolf qdict_flatten(qdict); 3979d26c9a15SKevin Wolf 3980be4b67bcSMax Reitz if (options->has_id) { 398118e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 398284d18f06SMarkus Armbruster if (local_err) { 3983b681072dSKevin Wolf error_propagate(errp, local_err); 3984d26c9a15SKevin Wolf goto fail; 3985d26c9a15SKevin Wolf } 3986d26c9a15SKevin Wolf 3987be4b67bcSMax Reitz bs = blk_bs(blk); 3988be4b67bcSMax Reitz } else { 3989be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3990be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3991be4b67bcSMax Reitz "the root node"); 3992be4b67bcSMax Reitz goto fail; 3993be4b67bcSMax Reitz } 3994be4b67bcSMax Reitz 3995bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3996bd745e23SMax Reitz if (!bs) { 3997be4b67bcSMax Reitz goto fail; 3998be4b67bcSMax Reitz } 39999c4218e9SMax Reitz 40009c4218e9SMax Reitz QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 4001be4b67bcSMax Reitz } 4002be4b67bcSMax Reitz 4003be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 4004be4b67bcSMax Reitz if (blk) { 4005efaa7c4eSMax Reitz monitor_remove_blk(blk); 400618e46a03SMarkus Armbruster blk_unref(blk); 4007be4b67bcSMax Reitz } else { 40089c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 4009be4b67bcSMax Reitz bdrv_unref(bs); 4010be4b67bcSMax Reitz } 40118ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 40128ae8e904SKevin Wolf goto fail; 40138ae8e904SKevin Wolf } 40148ae8e904SKevin Wolf 4015d26c9a15SKevin Wolf fail: 4016d26c9a15SKevin Wolf qmp_output_visitor_cleanup(ov); 4017d26c9a15SKevin Wolf } 4018d26c9a15SKevin Wolf 401981b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id, 402081b936aeSAlberto Garcia bool has_node_name, const char *node_name, Error **errp) 402181b936aeSAlberto Garcia { 402281b936aeSAlberto Garcia AioContext *aio_context; 402381b936aeSAlberto Garcia BlockBackend *blk; 402481b936aeSAlberto Garcia BlockDriverState *bs; 402581b936aeSAlberto Garcia 402681b936aeSAlberto Garcia if (has_id && has_node_name) { 402781b936aeSAlberto Garcia error_setg(errp, "Only one of id and node-name must be specified"); 402881b936aeSAlberto Garcia return; 402981b936aeSAlberto Garcia } else if (!has_id && !has_node_name) { 403081b936aeSAlberto Garcia error_setg(errp, "No block device specified"); 403181b936aeSAlberto Garcia return; 403281b936aeSAlberto Garcia } 403381b936aeSAlberto Garcia 403481b936aeSAlberto Garcia if (has_id) { 4035efaa7c4eSMax Reitz /* blk_by_name() never returns a BB that is not owned by the monitor */ 403681b936aeSAlberto Garcia blk = blk_by_name(id); 403781b936aeSAlberto Garcia if (!blk) { 403881b936aeSAlberto Garcia error_setg(errp, "Cannot find block backend %s", id); 403981b936aeSAlberto Garcia return; 404081b936aeSAlberto Garcia } 40415cf87fd6SPaolo Bonzini if (blk_legacy_dinfo(blk)) { 40425cf87fd6SPaolo Bonzini error_setg(errp, "Deleting block backend added with drive-add" 40435cf87fd6SPaolo Bonzini " is not supported"); 40445cf87fd6SPaolo Bonzini return; 40455cf87fd6SPaolo Bonzini } 404681b936aeSAlberto Garcia if (blk_get_refcnt(blk) > 1) { 404781b936aeSAlberto Garcia error_setg(errp, "Block backend %s is in use", id); 404881b936aeSAlberto Garcia return; 404981b936aeSAlberto Garcia } 405081b936aeSAlberto Garcia bs = blk_bs(blk); 405181b936aeSAlberto Garcia aio_context = blk_get_aio_context(blk); 405281b936aeSAlberto Garcia } else { 40531f0c461bSKevin Wolf blk = NULL; 405481b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 405581b936aeSAlberto Garcia if (!bs) { 405681b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 405781b936aeSAlberto Garcia return; 405881b936aeSAlberto Garcia } 40591f0c461bSKevin Wolf if (bdrv_has_blk(bs)) { 406081b936aeSAlberto Garcia error_setg(errp, "Node %s is in use by %s", 40611f0c461bSKevin Wolf node_name, bdrv_get_parent_name(bs)); 406281b936aeSAlberto Garcia return; 406381b936aeSAlberto Garcia } 406481b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 406581b936aeSAlberto Garcia } 406681b936aeSAlberto Garcia 406781b936aeSAlberto Garcia aio_context_acquire(aio_context); 406881b936aeSAlberto Garcia 406981b936aeSAlberto Garcia if (bs) { 407081b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 407181b936aeSAlberto Garcia goto out; 407281b936aeSAlberto Garcia } 407381b936aeSAlberto Garcia 40749c4218e9SMax Reitz if (!blk && !bs->monitor_list.tqe_prev) { 40759c4218e9SMax Reitz error_setg(errp, "Node %s is not owned by the monitor", 40769c4218e9SMax Reitz bs->node_name); 40779c4218e9SMax Reitz goto out; 40789c4218e9SMax Reitz } 40799c4218e9SMax Reitz 40809c4218e9SMax Reitz if (bs->refcnt > 1) { 408181b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 408281b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 408381b936aeSAlberto Garcia goto out; 408481b936aeSAlberto Garcia } 408581b936aeSAlberto Garcia } 408681b936aeSAlberto Garcia 408781b936aeSAlberto Garcia if (blk) { 4088efaa7c4eSMax Reitz monitor_remove_blk(blk); 408981b936aeSAlberto Garcia blk_unref(blk); 409081b936aeSAlberto Garcia } else { 40919c4218e9SMax Reitz QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 409281b936aeSAlberto Garcia bdrv_unref(bs); 409381b936aeSAlberto Garcia } 409481b936aeSAlberto Garcia 409581b936aeSAlberto Garcia out: 409681b936aeSAlberto Garcia aio_context_release(aio_context); 409781b936aeSAlberto Garcia } 409881b936aeSAlberto Garcia 40997f821597SWen Congyang static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 41007f821597SWen Congyang const char *child_name) 41017f821597SWen Congyang { 41027f821597SWen Congyang BdrvChild *child; 41037f821597SWen Congyang 41047f821597SWen Congyang QLIST_FOREACH(child, &parent_bs->children, next) { 41057f821597SWen Congyang if (strcmp(child->name, child_name) == 0) { 41067f821597SWen Congyang return child; 41077f821597SWen Congyang } 41087f821597SWen Congyang } 41097f821597SWen Congyang 41107f821597SWen Congyang return NULL; 41117f821597SWen Congyang } 41127f821597SWen Congyang 41137f821597SWen Congyang void qmp_x_blockdev_change(const char *parent, bool has_child, 41147f821597SWen Congyang const char *child, bool has_node, 41157f821597SWen Congyang const char *node, Error **errp) 41167f821597SWen Congyang { 41177f821597SWen Congyang BlockDriverState *parent_bs, *new_bs = NULL; 41187f821597SWen Congyang BdrvChild *p_child; 41197f821597SWen Congyang 41207f821597SWen Congyang parent_bs = bdrv_lookup_bs(parent, parent, errp); 41217f821597SWen Congyang if (!parent_bs) { 41227f821597SWen Congyang return; 41237f821597SWen Congyang } 41247f821597SWen Congyang 41257f821597SWen Congyang if (has_child == has_node) { 41267f821597SWen Congyang if (has_child) { 41277f821597SWen Congyang error_setg(errp, "The parameters child and node are in conflict"); 41287f821597SWen Congyang } else { 41297f821597SWen Congyang error_setg(errp, "Either child or node must be specified"); 41307f821597SWen Congyang } 41317f821597SWen Congyang return; 41327f821597SWen Congyang } 41337f821597SWen Congyang 41347f821597SWen Congyang if (has_child) { 41357f821597SWen Congyang p_child = bdrv_find_child(parent_bs, child); 41367f821597SWen Congyang if (!p_child) { 41377f821597SWen Congyang error_setg(errp, "Node '%s' does not have child '%s'", 41387f821597SWen Congyang parent, child); 41397f821597SWen Congyang return; 41407f821597SWen Congyang } 41417f821597SWen Congyang bdrv_del_child(parent_bs, p_child, errp); 41427f821597SWen Congyang } 41437f821597SWen Congyang 41447f821597SWen Congyang if (has_node) { 41457f821597SWen Congyang new_bs = bdrv_find_node(node); 41467f821597SWen Congyang if (!new_bs) { 41477f821597SWen Congyang error_setg(errp, "Node '%s' not found", node); 41487f821597SWen Congyang return; 41497f821597SWen Congyang } 41507f821597SWen Congyang bdrv_add_child(parent_bs, new_bs, errp); 41517f821597SWen Congyang } 41527f821597SWen Congyang } 41537f821597SWen Congyang 4154fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 4155fb5458cdSStefan Hajnoczi { 4156fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 4157fea68bb6SMarkus Armbruster BlockDriverState *bs; 415888be7b4bSKevin Wolf BdrvNextIterator it; 4159fea68bb6SMarkus Armbruster 416088be7b4bSKevin Wolf for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { 416169691e72SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 416269691e72SStefan Hajnoczi 416369691e72SStefan Hajnoczi aio_context_acquire(aio_context); 416469691e72SStefan Hajnoczi 4165fea68bb6SMarkus Armbruster if (bs->job) { 4166fea68bb6SMarkus Armbruster BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 4167fea68bb6SMarkus Armbruster elem->value = block_job_query(bs->job); 4168fea68bb6SMarkus Armbruster *p_next = elem; 4169fea68bb6SMarkus Armbruster p_next = &elem->next; 4170fea68bb6SMarkus Armbruster } 417169691e72SStefan Hajnoczi 417269691e72SStefan Hajnoczi aio_context_release(aio_context); 4173fea68bb6SMarkus Armbruster } 4174fea68bb6SMarkus Armbruster 4175fea68bb6SMarkus Armbruster return head; 4176fb5458cdSStefan Hajnoczi } 41774d454574SPaolo Bonzini 41780006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 41794d454574SPaolo Bonzini .name = "drive", 41800006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 41814d454574SPaolo Bonzini .desc = { 41824d454574SPaolo Bonzini { 41834d454574SPaolo Bonzini .name = "snapshot", 41844d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 41854d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 41864d454574SPaolo Bonzini },{ 4187a9384affSPaolo Bonzini .name = "discard", 4188a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 4189a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 4190a9384affSPaolo Bonzini },{ 41914d454574SPaolo Bonzini .name = "aio", 41924d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 41934d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 41944d454574SPaolo Bonzini },{ 4195e4b24b49SKevin Wolf .name = BDRV_OPT_CACHE_WB, 4196e4b24b49SKevin Wolf .type = QEMU_OPT_BOOL, 4197e4b24b49SKevin Wolf .help = "Enable writeback mode", 4198e4b24b49SKevin Wolf },{ 41994d454574SPaolo Bonzini .name = "format", 42004d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 42014d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 42024d454574SPaolo Bonzini },{ 42034d454574SPaolo Bonzini .name = "rerror", 42044d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 42054d454574SPaolo Bonzini .help = "read error action", 42064d454574SPaolo Bonzini },{ 42074d454574SPaolo Bonzini .name = "werror", 42084d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 42094d454574SPaolo Bonzini .help = "write error action", 42104d454574SPaolo Bonzini },{ 42110f227a94SKevin Wolf .name = "read-only", 42124d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 42134d454574SPaolo Bonzini .help = "open drive file as read-only", 42144d454574SPaolo Bonzini },{ 421557975222SKevin Wolf .name = "throttling.iops-total", 42164d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42174d454574SPaolo Bonzini .help = "limit total I/O operations per second", 42184d454574SPaolo Bonzini },{ 421957975222SKevin Wolf .name = "throttling.iops-read", 42204d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42214d454574SPaolo Bonzini .help = "limit read operations per second", 42224d454574SPaolo Bonzini },{ 422357975222SKevin Wolf .name = "throttling.iops-write", 42244d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42254d454574SPaolo Bonzini .help = "limit write operations per second", 42264d454574SPaolo Bonzini },{ 422757975222SKevin Wolf .name = "throttling.bps-total", 42284d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42294d454574SPaolo Bonzini .help = "limit total bytes per second", 42304d454574SPaolo Bonzini },{ 423157975222SKevin Wolf .name = "throttling.bps-read", 42324d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42334d454574SPaolo Bonzini .help = "limit read bytes per second", 42344d454574SPaolo Bonzini },{ 423557975222SKevin Wolf .name = "throttling.bps-write", 42364d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 42374d454574SPaolo Bonzini .help = "limit write bytes per second", 42384d454574SPaolo Bonzini },{ 42393e9fab69SBenoît Canet .name = "throttling.iops-total-max", 42403e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42413e9fab69SBenoît Canet .help = "I/O operations burst", 42423e9fab69SBenoît Canet },{ 42433e9fab69SBenoît Canet .name = "throttling.iops-read-max", 42443e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42453e9fab69SBenoît Canet .help = "I/O operations read burst", 42463e9fab69SBenoît Canet },{ 42473e9fab69SBenoît Canet .name = "throttling.iops-write-max", 42483e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42493e9fab69SBenoît Canet .help = "I/O operations write burst", 42503e9fab69SBenoît Canet },{ 42513e9fab69SBenoît Canet .name = "throttling.bps-total-max", 42523e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42533e9fab69SBenoît Canet .help = "total bytes burst", 42543e9fab69SBenoît Canet },{ 42553e9fab69SBenoît Canet .name = "throttling.bps-read-max", 42563e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42573e9fab69SBenoît Canet .help = "total bytes read burst", 42583e9fab69SBenoît Canet },{ 42593e9fab69SBenoît Canet .name = "throttling.bps-write-max", 42603e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 42613e9fab69SBenoît Canet .help = "total bytes write burst", 42623e9fab69SBenoît Canet },{ 42638a0fc18dSAlberto Garcia .name = "throttling.iops-total-max-length", 42648a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42658a0fc18dSAlberto Garcia .help = "length of the iops-total-max burst period, in seconds", 42668a0fc18dSAlberto Garcia },{ 42678a0fc18dSAlberto Garcia .name = "throttling.iops-read-max-length", 42688a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42698a0fc18dSAlberto Garcia .help = "length of the iops-read-max burst period, in seconds", 42708a0fc18dSAlberto Garcia },{ 42718a0fc18dSAlberto Garcia .name = "throttling.iops-write-max-length", 42728a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42738a0fc18dSAlberto Garcia .help = "length of the iops-write-max burst period, in seconds", 42748a0fc18dSAlberto Garcia },{ 42758a0fc18dSAlberto Garcia .name = "throttling.bps-total-max-length", 42768a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42778a0fc18dSAlberto Garcia .help = "length of the bps-total-max burst period, in seconds", 42788a0fc18dSAlberto Garcia },{ 42798a0fc18dSAlberto Garcia .name = "throttling.bps-read-max-length", 42808a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42818a0fc18dSAlberto Garcia .help = "length of the bps-read-max burst period, in seconds", 42828a0fc18dSAlberto Garcia },{ 42838a0fc18dSAlberto Garcia .name = "throttling.bps-write-max-length", 42848a0fc18dSAlberto Garcia .type = QEMU_OPT_NUMBER, 42858a0fc18dSAlberto Garcia .help = "length of the bps-write-max burst period, in seconds", 42868a0fc18dSAlberto Garcia },{ 42872024c1dfSBenoît Canet .name = "throttling.iops-size", 42882024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 42892024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 42902024c1dfSBenoît Canet },{ 429176f4afb4SAlberto Garcia .name = "throttling.group", 429276f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 429376f4afb4SAlberto Garcia .help = "name of the block throttling group", 429476f4afb4SAlberto Garcia },{ 42954d454574SPaolo Bonzini .name = "copy-on-read", 42964d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 42974d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4298465bee1dSPeter Lieven },{ 4299465bee1dSPeter Lieven .name = "detect-zeroes", 4300465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4301465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4302362e9299SAlberto Garcia },{ 4303362e9299SAlberto Garcia .name = "stats-account-invalid", 4304362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4305362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4306362e9299SAlberto Garcia "in the statistics", 4307362e9299SAlberto Garcia },{ 4308362e9299SAlberto Garcia .name = "stats-account-failed", 4309362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4310362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4311362e9299SAlberto Garcia "in the statistics", 43124d454574SPaolo Bonzini }, 43134d454574SPaolo Bonzini { /* end of list */ } 43144d454574SPaolo Bonzini }, 43154d454574SPaolo Bonzini }; 43160006383eSKevin Wolf 4317bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 4318bd745e23SMax Reitz .name = "root-bds", 431923f7fcb2SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head), 4320bd745e23SMax Reitz .desc = { 4321bd745e23SMax Reitz { 4322bd745e23SMax Reitz .name = "discard", 4323bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4324bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 4325bd745e23SMax Reitz },{ 4326bd745e23SMax Reitz .name = "aio", 4327bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4328bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 4329bd745e23SMax Reitz },{ 4330bd745e23SMax Reitz .name = "read-only", 4331bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4332bd745e23SMax Reitz .help = "open drive file as read-only", 4333bd745e23SMax Reitz },{ 4334bd745e23SMax Reitz .name = "copy-on-read", 4335bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4336bd745e23SMax Reitz .help = "copy read data from backing file into image file", 4337bd745e23SMax Reitz },{ 4338bd745e23SMax Reitz .name = "detect-zeroes", 4339bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4340bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 4341bd745e23SMax Reitz }, 4342bd745e23SMax Reitz { /* end of list */ } 4343bd745e23SMax Reitz }, 4344bd745e23SMax Reitz }; 4345bd745e23SMax Reitz 43460006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 43470006383eSKevin Wolf .name = "drive", 43480006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 43490006383eSKevin Wolf .desc = { 4350492fdc6fSKevin Wolf /* 4351492fdc6fSKevin Wolf * no elements => accept any params 4352492fdc6fSKevin Wolf * validation will happen later 4353492fdc6fSKevin Wolf */ 43540006383eSKevin Wolf { /* end of list */ } 43550006383eSKevin Wolf }, 43560006383eSKevin Wolf }; 4357